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

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