source: git/Singular/LIB/random.lib @ cda1ad

fieker-DuValspielwiese
Last change on this file since cda1ad was cda1ad, checked in by Gert-Martin Greuel <greuel@…>, 23 years ago
* GMG: neue proc sparsematrix zugefuegt git-svn-id: file:///usr/local/Singular/svn/trunk@5010 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.5 KB
Line 
1//(GMG/BM, last modified 22.06.96)
2///////////////////////////////////////////////////////////////////////////////
3version="$Id: random.lib,v 1.14 2001-01-03 01:21:37 greuel Exp $";
4category="General purpose";
5info="
6LIBRARY:  random.lib    Creating Random and Sparse Matrices, Ideals, Polys
7
8PROCEDURES:
9 genericid(i[,p,b]);     generic sparse linear combinations of generators of i
10 randomid(id,[k,b]);     random linear combinations of generators of id
11 randommat(n,m[,id,b]);  nxm matrix of random linear combinations of id
12 sparseid(k,u[,o,p,b]);  ideal of k random sparse poly's of degree d [u<=d<=o]
13 sparsemat(n,m[,p,b]);   nxm sparse integer matrix with random coefficients
14 sparsepoly(u[,o,p,b]);  random sparse polynomial with terms of degree in [u,o]
15 sparsetriag(n,m[,.]);   nxm sparse lower-triag intmat with random coefficients
16 randomLast(b);          random transformation of the last variable
17 randomBinomial(k,u,..); binomial ideal, k random generators of degree >=u
18           (parameters in square brackets [] are optional)
19";
20
21LIB "inout.lib";
22LIB "general.lib";
23LIB "matrix.lib";
24///////////////////////////////////////////////////////////////////////////////
25
26proc genericid (id, list #)
27"USAGE:   genericid(id,[,p,b]);  id ideal/module, k,p,b integers
28RETURN:  system of generators of id which are generic, sparse, triagonal linear
29         combinations of given generators with coefficients in [1,b] and
30         sparsety p percent, bigger p being sparser (default: p=75, b=30000)
31NOTE:    For performance reasons try small bound b in characteristic 0
32EXAMPLE: example genericid; shows an example
33"
34{
35//----------------------------- set defaults ----------------------------------
36   if( size(#)>=2 ) { int p=#[1]; int b=#[2]; }
37   if( size(#)==1 ) { int p=#[1]; int b=30000}
38   if( size(#)==0 ) { int p=75; int b=30000; }
39//---------------- use sparsetriag for creation of genericid ------------------
40   def i = simplify(id,10);
41   i = i*sparsetriag(ncols(i),ncols(i),p,b);
42   return(i);
43}
44example
45{ "EXAMPLE:"; echo = 2;
46   ring r=0,(t,x,y,z),ds;
47   ideal i= x3+y4,z4+yx,t+x+y+z;
48   genericid(i,0,10);
49   module m=[x,0,0,0],[0,y2,0,0],[0,0,z3,0],[0,0,0,t4];
50   print(genericid(m));
51}
52///////////////////////////////////////////////////////////////////////////////
53
54proc randomid (id, list #)
55"USAGE:   randomid(id,[k,b]);  id ideal/module, b,k integers
56RETURN:  ideal/module having k generators which are random linear combinations
57         of generators of id with coefficients in the interval [-b,b]
58         (default: b=30000, k=size(id))
59NOTE:    For performance reasons try small bound b in characteristic 0
60EXAMPLE: example randomid; shows an example
61"
62{
63//----------------------------- set defaults ----------------------------------
64   if( size(#)>=2 ) { int k=#[1]; int b=#[2]; }
65   if( size(#)==1 ) { int k=#[1]; int b=30000; }
66   if( size(#)==0 ) { int k=size(id); int b=30000; }
67//--------------------------- create randomid ---------------------------------
68   def i = id;
69   i = matrix(id)*random(b,ncols(id),k);
70   return(i);
71}
72example
73{ "EXAMPLE:"; echo = 2;
74   ring r=0,(x,y,z),dp;
75   randomid(maxideal(2),2,9);
76   module m=[x,0,1],[0,y2,0],[y,0,z3];
77   show(randomid(m));
78}
79///////////////////////////////////////////////////////////////////////////////
80
81proc randommat (int n, int m, list #)
82"USAGE:   randommat(n,m[,id,b]);  n,m,b integers, id ideal
83RETURN:  nxm matrix, entries are random linear combinations of elements
84         of id and coefficients in [-b,b]
85         [default: (id,b) = (maxideal(1),30000)]
86NOTE:    For performance reasons try small bound b in char 0
87EXAMPLE:  example randommat; shows an example
88"
89{
90//----------------------------- set defaults ----------------------------------
91   if( size(#)>=2 ) { ideal id=#[1]; int b=#[2]; }
92   if( size(#)==1 ) { ideal id=#[1]; int b=30000; }
93   if( size(#)==0 ) { ideal id=maxideal(1); int b=30000; }
94//--------------------------- create randommat --------------------------------
95   id=simplify(id,2);
96   int g=ncols(id);
97   matrix rand[n][m]; matrix ra[1][m];
98   for (int k=1; k<=n; k=k+1)
99   {
100      ra = id*random(b,g,m);
101      rand[k,1..m]=ra[1,1..m];
102   }
103   return(rand);
104}
105example
106{ "EXAMPLE:"; echo = 2;
107   ring r=0,(x,y,z),dp;
108   matrix A=randommat(3,3,maxideal(2),9);
109   print(A);
110   A=randommat(2,3);
111   print(A);
112}
113///////////////////////////////////////////////////////////////////////////////
114
115proc sparseid (int k, int u, list #)
116"USAGE:   sparseid(k,u[,o,p,b]);  k,u,o,p,b integers
117RETURN:  ideal having k generators, each of degree d, u<=d<=o, p percent of
118         terms in degree d are 0, the remaining have random coefficients
119         in the interval [1,b], (default: o=u=d, p=75, b=30000)
120EXAMPLE: example sparseid; shows an example
121"
122{
123//----------------------------- set defaults ----------------------------------
124   if( size(#)>=3 ) { int o=#[1]; int p=#[2]; int b=#[3]; }
125   if( size(#)==2 ) { int o=#[1]; int p=#[2]; int b=30000; }
126   if( size(#)==1 ) { int o=#[1]; int p=75; int b=30000; }
127   if( size(#)==0 ) { int o=u; int p=75; int b=30000; }
128//------------------ use sparsemat for creation of sparseid -------------------
129   int ii; matrix i[1][k]; intmat m;
130   if( u <=0 )
131   {
132      m = sparsemat(1,k,p,b);
133      i = m;
134      u=1;
135   }
136   for ( ii=u; ii<=o; ii=ii+1)
137   {
138       m = sparsemat(size(maxideal(ii)),k,p,b);
139       i = i+matrix(maxideal(ii))*m;
140   }
141   return(ideal(i));
142}
143example
144{ "EXAMPLE:"; echo = 2;
145   ring r = 0,(a,b,c,d),ds;
146   sparseid(2,3);"";
147   sparseid(3,0,4,90,9);
148}
149///////////////////////////////////////////////////////////////////////////////
150
151proc sparsemat (int n, int m, list #)
152"USAGE:   sparsemat(n,m[,p,b]);  n,m,p,b integers
153RETURN:  nxm integer matrix, p percent of the entries are 0, the remaining
154         are random coefficients >=1 and <= b; [defaults: (p,b) = (75,1)]
155EXAMPLE: example sparsemat; shows an example
156"
157{
158   int r,h,ii;
159   int t = n*m;
160   intmat v[1][t];
161//----------------------------- set defaults ----------------------------------
162   if( size(#)>=2 ) { int p=#[1]; int b=#[2]; }
163   if( size(#)==1 ) { int p=#[1]; int b=1; }
164   if( size(#)==0 ) { int p=75; int b=1; }
165//------------------------- check trivial cases ------------------------------
166   if( p<0 ) { p = 0; }
167   if(p>100) { p=100; }
168//--------------- this is faster for not very sparse matrices ----------------
169   if( p<40 )
170   {
171      for( ii=1; ii<=t; ii=ii+1 )
172      { r=( random(1,100)>p ); v[1,ii]=r*random(1,b); h=h+r; }
173   }
174  int bb = t*(100-p);
175  if( 100*h > bb )
176  {
177     while( 100*h > bb )
178     { r=random(1,t); h=h-( v[1,r]>0 ); v[1,r]=0; }
179  }
180  else
181  {
182//------------------- this is faster for sparse matrices ---------------------
183     while ( 100*h < bb )
184     { r=random(1,t); h=h+(v[1,r]==0); v[1,r]=random(1,b); }
185  }
186  intmat M[n][m] = v[1,1..t];
187  return(M);
188}
189example
190{ "EXAMPLE:"; echo = 2;
191   sparsemat(5,5);"";
192   sparsemat(5,5,95);"";
193   sparsemat(5,5,5);"";
194   sparsemat(5,5,50,100);
195}
196///////////////////////////////////////////////////////////////////////////////
197proc sparsematrix (int n, int m, int o, list #)
198"USAGE:   sparsematrix(n,m,o[,u,pe,pp,b]);  n,m,o,u,pe,pp,b integers
199RETURN:  nxm matrix, about pe percent of the entries are 0, the remaining
200         are random polynomials of degree d, u<=d<=o, with  pp percent of
201         the terms being 0, the remaining have random coefficients
202         in the interval [1,b] [default: (pe,u,pp,b) = (0,50,75,100)]
203EXAMPLE: example sparsematrix; shows an example
204"
205{
206   int ii,jj;
207   ideal id;
208   matrix M[n][m];
209//----------------------------- set defaults ----------------------------------
210   int pe=50;int u=0;int pp=75;int b=100;
211   if( size(#)==4 ) { u=#[1]; pe=#[2]; pp=#[3]; b=#[4]; }
212   if( size(#)==3 ) { u=#[1]; pe=#[2]; pp=#[3]; }
213   if( size(#)==2 ) { u=#[1]; pe=#[2]; }
214   if( size(#)==1 ) { u=#[1]; }
215//------------------- use sparsemat and sparseid  -----------------------------
216   intmat I = sparsemat(n,m,pe,1);
217   for(ii=1; ii<=n;ii++)
218   {
219     id = sparseid(m,u,o,pp,b);
220     for(jj=1; jj<=m; jj++)
221     {
222        if( I[ii,jj] !=0)
223        {
224          M[ii,jj]=id[jj];
225        }
226     }
227   }
228   return(M);
229}
230example
231{ "EXAMPLE:"; echo = 2;
232   ring r = 0,(a,b,c,d),dp;
233   // sparse matrix of sparse polys of degree <=2:
234   print(sparsematrix(3,4,2));"";       
235   // dense matrix of sparse linear forms:
236   print(sparsematrix(3,3,1,1,0,55,9));
237}
238///////////////////////////////////////////////////////////////////////////////
239
240proc sparsepoly (int u, list #)
241"USAGE:   sparsepoly(u[,o,p,b]);  u,o,p,b integers
242RETURN:  poly having only terms in degree d, u<=d<=o, p percent of the terms
243         in degree d are 0, the remaining have random coefficients in [1,b),
244         (defaults: o=u=d, p=75, b=30000)
245EXAMPLE:  example sparsepoly; shows an example
246"
247{
248//----------------------------- set defaults ----------------------------------
249   if( size(#)>=3 ) { int o=#[1]; int p=#[2]; int b=#[3]; }
250   if( size(#)==2 ) { int o=#[1]; int p=#[2]; int b=30000; }
251   if( size(#)==1 ) { int o=#[1]; int p=75; int b=30000; }
252   if( size(#)==0 ) { int o=u; int p=75; int b=30000; }
253   int ii; poly f;
254//----------------- use sparseid for creation of sparsepoly -------------------
255   for( ii=u; ii<=o; ii=ii+1 ) { f=f+sparseid(1,ii,ii,p,b)[1]; }
256   return(f);
257}
258example
259{ "EXAMPLE:"; echo = 2;
260   ring r=0,(x,y,z),dp;
261   sparsepoly(5);"";
262   sparsepoly(3,5,90,9);
263}
264///////////////////////////////////////////////////////////////////////////////
265
266proc sparsetriag (int n, int m, list #)
267"USAGE:   sparsetriag(n,m[,p,b]);  n,m,p,b integers
268RETURN:  nxm lower triagonal integer matrix, diagonal entries equal to 1, about
269         p percent of lower diagonal entries are 0, the remaining are random
270         integers >=1 and <= b; [defaults: (p,b) = (75,1)]
271EXAMPLE: example sparsetriag; shows an example
272"
273{
274   int ii,min,l,r; intmat M[n][m];
275   int t=(n*(n-1)) div 2;
276//----------------------------- set defaults ----------------------------------
277   if( size(#)>=2 ) { int p=#[1]; int b=#[2]; }
278   if( size(#)==1 ) { int p=#[1]; int b=1; }
279   if( size(#)==0 ) { int p=75; int b=1; }
280//---------------- use sparsemat for creation of sparsetriag ------------------
281   intmat v[1][t]=sparsemat(1,t,p,b);
282   if( n<=m ) { min=n-1; M[n,n]=1; }
283   else { min=m; }
284   for( ii=1; ii<=min; ii=ii+1 )
285   {
286      l=r+1; r=r+n-ii;
287      M[ii..n,ii]=1,v[1,l..r];
288   }
289   return(M);
290}
291example
292{ "EXAMPLE:"; echo = 2;
293   sparsetriag(5,7);"";
294   sparsetriag(7,5,90);"";
295   sparsetriag(5,5,0);"";
296   sparsetriag(5,5,50,100);
297}
298///////////////////////////////////////////////////////////////////////////////
299
300proc randomLast(int b)
301"USAGE:   randomLast(b);  b int
302RETURN:  ideal = maxideal(1), but the last variable is exchanged by a random
303         linear combination of all variables, with coefficients in the
304         interval [-b,b].
305EXAMPLE: example randomLast; shows an example
306"
307{
308  ideal i=maxideal(1);
309  int k=size(i);
310  i[k]=0;
311  i=randomid(i,size(i),b);
312  ideal ires=maxideal(1);
313  ires[k]=i[1]+var(k);
314  return(ires);
315}
316example
317{ "EXAMPLE:"; echo = 2;
318   ring  r = 0,(x,y,z),lp;
319   ideal i = randomLast(10);
320   i;
321}
322///////////////////////////////////////////////////////////////////////////////
323
324proc randomBinomial(int k, int u, list #)
325"USAGE:   randomBinomial(k,u[,o,b]);  k,u,o,b integers
326RETURN:  binomial ideal, k homogeneous generators of degree d, u<=d<=o, with
327         randomly choosen monomials and coefficients in the interval [-b,b]
328         (default: u=o, b=10).
329EXAMPLE: example randomBinomial; shows an example
330"
331{
332//----------------------------- set defaults ----------------------------------
333   if( size(#)>=2 ) { int o=#[1]; int b=#[2]; }
334   if( size(#)==1 ) { int o=#[1]; int b=10; }
335   if( size(#)==0 ) { int o=u; int b=10; }
336//------------------ use sparsemat for creation of sparseid -------------------
337   ideal i,m;
338   int ii,jj,s,r1,r2;
339   if ( o<u ) { o=u; }
340   int a = k div (o-u+1);
341   int c = k mod (o-u+1);
342   for ( ii = u; ii<=o; ii++ )
343   { m = maxideal(ii);
344     s = size(m);
345     for ( jj=1; jj<=a; jj++ )
346     { r1 = random(-b,b);
347       r1 = r1 + (r1==0)*random(1,b);
348       r2 = random(-b,b);
349       r2 = r2 + (r2==0)*random(-b,-1);
350       i = i,r1*m[random(1,s/2)] + r1*m[random(s/2+1,s)];
351       if ( ii < c+u )
352       {  r1 = random(-b,b);
353          r1 = r1 + (r1==0)*random(1,b);
354          r2 = random(-b,b);
355          r2 = r2 + (r2==0)*random(-b,-1);
356          i = i,r1*m[random(1,s/2)] + r2*m[random(s/2+1,s)];
357       }
358     }
359   }
360   i = i[2..k+1];
361   return(i);
362}
363example
364{ "EXAMPLE:"; echo = 2;
365   ring  r = 0,(x,y,z),lp;
366   ideal i = randomBinomial(4,5,6);
367   i;
368}
369///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.