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

spielwiese
Last change on this file since d2b2a7 was d2b2a7, checked in by Kai Krüger <krueger@…>, 26 years ago
Modified Files: libparse.l utils.cc LIB/classify.lib LIB/deform.lib LIB/elim.lib LIB/factor.lib LIB/fastsolv.lib LIB/finvar.lib LIB/general.lib LIB/hnoether.lib LIB/homolog.lib LIB/inout.lib LIB/invar.lib LIB/makedbm.lib LIB/matrix.lib LIB/normal.lib LIB/poly.lib LIB/presolve.lib LIB/primdec.lib LIB/primitiv.lib LIB/random.lib LIB/ring.lib LIB/sing.lib LIB/standard.lib LIB/tex.lib LIB/tst.lib Changed help section o procedures to have an quoted help-string between proc-definition and proc-body. git-svn-id: file:///usr/local/Singular/svn/trunk@1601 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.8 KB
Line 
1// $Id: random.lib,v 1.5 1998-05-05 11:55:36 krueger Exp $
2//system("random",787422842);
3//(GMG/BM, last modified 22.06.96)
4///////////////////////////////////////////////////////////////////////////////
5
6version="$Id: random.lib,v 1.5 1998-05-05 11:55:36 krueger Exp $";
7info="
8LIBRARY:  random.lib    PROCEDURES OF RANDOM MATRIX AND POLY OPERATIONS
9
10 genericid(id[,p,b]);   generic sparse linear combinations of generators of id
11 randomid(id,[k,b]);    random linear combinations of generators of id
12 randommat(n,m[,id,b]); nxm matrix of random linear combinations of id
13 sparseid(k,u[,o,p,b]); ideal of k random sparse poly's of degree d [u<=d<=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           (parameters in square brackets [] are optional)
18";
19
20LIB "inout.lib";
21LIB "general.lib";
22////////////////////////////////////////////////////////////////////////////////
23
24proc genericid (id, list #)
25"USAGE:   genericid(id,[,p,b]);  id ideal/module, k,p,b integers
26RETURN:  system of generators of id which are generic, sparse, triagonal linear
27         combinations of given generators with coefficients in [1,b] and
28         sparsety p percent, bigger p being sparser (default: p=75, b=30000)
29NOTE:    For performance reasons try small bound b in characteristic 0
30EXAMPLE: example genericid; shows an example
31"
32{
33//----------------------------- set defaults ----------------------------------
34   if( size(#)>=2 ) { int p=#[1]; int b=#[2]; }
35   if( size(#)==1 ) { int p=#[1]; int b=30000}
36   if( size(#)==0 ) { int p=75; int b=30000; }
37//---------------- use sparsetriag for creation of genericid ------------------
38   def i = simplify(id,10);
39   i = i*sparsetriag(ncols(i),ncols(i),p,b);
40   return(i);
41}
42example
43{ "EXAMPLE:"; echo = 2;
44   ring r=0,(t,x,y,z),ds;
45   ideal i= x3+y4,z4+yx,t+x+y+z;
46   genericid(i,0,10);
47   module m=[x,0,0,0],[0,y2,0,0],[0,0,z3,0],[0,0,0,t4];
48   print(genericid(m));
49}
50////////////////////////////////////////////////////////////////////////////////
51
52proc randomid (id, list #)
53"USAGE:   randomid(id,[k,b]);  id ideal/module, b,k integers
54RETURN:  ideal/module having k generators which are random linear combinations
55         of generators of id with coefficients in the interval [-b,b]
56         (default: b=30000, k=size(id))
57NOTE:    For performance reasons try small bound b in characteristic 0
58EXAMPLE: example randomid; shows an example
59"
60{
61//----------------------------- set defaults ----------------------------------
62   if( size(#)>=2 ) { int k=#[1]; int b=#[2]; }
63   if( size(#)==1 ) { int k=#[1]; int b=30000; }
64   if( size(#)==0 ) { int k=size(id); int b=30000; }
65//--------------------------- create randomid ---------------------------------
66   def i = id;
67   i = matrix(id)*random(b,ncols(id),k);
68   return(i);
69}
70example
71{ "EXAMPLE:"; echo = 2;
72   ring r=0,(x,y,z),dp;
73   randomid(maxideal(2),2,9);
74   module m=[x,0,1],[0,y2,0],[y,0,z3];
75   show(randomid(m));
76}
77////////////////////////////////////////////////////////////////////////////////
78
79proc randommat (int n, int m, list #)
80"USAGE:   randommat(n,m[,id,b]);  n,m,b integers, id ideal
81RETURN:  nxm matrix, entries are random linear combinations of elements
82         of id and coefficients in [-b,b]
83         [default: (id,b) = (maxideal(1),30000)]
84NOTE:    For performance reasons try small bound b in char 0
85EXAMPLE:  example randommat; shows an example
86"
87{
88//----------------------------- set defaults ----------------------------------
89   if( size(#)>=2 ) { ideal id=#[1]; int b=#[2]; }
90   if( size(#)==1 ) { ideal id=#[1]; int b=30000; }
91   if( size(#)==0 ) { ideal id=maxideal(1); int b=30000; }
92//--------------------------- create randommat --------------------------------
93   id=simplify(id,2);
94   int g=ncols(id);
95   matrix rand[n][m]; matrix ra[1][m];
96   for (int k=1; k<=n; k=k+1)
97   {
98      ra = id*random(b,g,m);
99      rand[k,1..m]=ra[1,1..m];
100   }
101   return(rand);
102}
103example
104{ "EXAMPLE:"; echo = 2;
105   ring r=0,(x,y,z),dp;
106   matrix A=randommat(3,3,maxideal(2),9);
107   print(A);
108   A=randommat(2,3);
109   print(A);
110}
111///////////////////////////////////////////////////////////////////////////////
112
113proc sparseid (int k, int u, list #)
114"USAGE:   sparseid(k,u[,o,p,b]);  k,u,o,p,b integers
115RETURN:  ideal having k generators in each degree d, u<=d<=o, p percent of
116         terms in degree d are 0, the remaining have random coefficients
117         in the interval [1,b], (default: o=u=d, p=75, b=30000)
118EXAMPLE: example sparseid; shows an example
119"
120{
121//----------------------------- set defaults ----------------------------------
122   if( size(#)>=3 ) { int o=#[1]; int p=#[2]; int b=#[3]; }
123   if( size(#)==2 ) { int o=#[1]; int p=#[2]; int b=30000; }
124   if( size(#)==1 ) { int o=#[1]; int p=75; int b=30000; }
125   if( size(#)==0 ) { int o=u; int p=75; int b=30000; }
126//------------------ use sparsemat for creation of sparseid -------------------
127   int ii; ideal i; intmat m;
128   for ( ii=u; ii<=o; ii=ii+1)
129   {
130       m = sparsemat(size(maxideal(ii)),k,p,b);
131       i = i+ideal(matrix(maxideal(ii))*m);
132   }
133   return(i);
134}
135example
136{ "EXAMPLE:"; echo = 2;
137   ring r = 0,(a,b,c,d),ds;
138   sparseid(3,4);"";
139   sparseid(2,2,5,90,9);
140}
141///////////////////////////////////////////////////////////////////////////////
142
143proc sparsemat (int n, int m, list #)
144"USAGE:   sparsemat(n,m[,p,b]);  n,m,p,b integers
145RETURN:  nxm integer matrix, p percent of the entries are 0, the remaining
146         are random coefficients >=1 and <= b; [defaults: (p,b) = (75,1)]
147EXAMPLE: example sparsemat; shows an example
148"
149{
150   int r,h,ii;
151   int t = n*m;
152   intmat v[1][t];
153//----------------------------- set defaults ----------------------------------
154   if( size(#)>=2 ) { int p=#[1]; int b=#[2]; }
155   if( size(#)==1 ) { int p=#[1]; int b=1; }
156   if( size(#)==0 ) { int p=75; int b=1; }
157//------------------------- check trivial cases ------------------------------
158   if( p<0 ) { p = 0; }
159   if(p>100) { p=100; }
160//--------------- this is faster for not very sparse matrices ----------------
161   if( p<40 )
162   {
163      for( ii=1; ii<=t; ii=ii+1 )
164      { r=( random(1,100)>p ); v[1,ii]=r*random(1,b); h=h+r; }
165   }
166  int bb = t*(100-p);
167  if( 100*h > bb )
168  {
169     while( 100*h > bb )
170     { r=random(1,t); h=h-( v[1,r]>0 ); v[1,r]=0; }
171  }
172  else
173  {
174//------------------- this is faster for sparse matrices ---------------------
175     while ( 100*h < bb )
176     { r=random(1,t); h=h+(v[1,r]==0); v[1,r]=random(1,b); }
177  }
178  intmat M[n][m] = v[1,1..t];
179  return(M);
180}
181example
182{ "EXAMPLE:"; echo = 2;
183   sparsemat(5,5);"";
184   sparsemat(5,5,95);"";
185   sparsemat(5,5,5);"";
186   sparsemat(5,5,50,100);
187}
188///////////////////////////////////////////////////////////////////////////////
189
190proc sparsepoly (int u, list #)
191"USAGE:   sparsepoly(u[,o,p,b]);  u,o,p,b integers
192RETURN:  poly having only terms in degree d, u<=d<=o, p percent of the terms
193         in degree d are 0, the remaining have random coefficients in [1,b),
194         (defaults: o=u=d, p=75, b=30000)
195EXAMPLE:  example sparsepoly; shows an example
196"
197{
198//----------------------------- set defaults ----------------------------------
199   if( size(#)>=3 ) { int o=#[1]; int p=#[2]; int b=#[3]; }
200   if( size(#)==2 ) { int o=#[1]; int p=#[2]; int b=30000; }
201   if( size(#)==1 ) { int o=#[1]; int p=75; int b=30000; }
202   if( size(#)==0 ) { int o=u; int p=75; int b=30000; }
203   int ii; poly f;
204//----------------- use sparseid for creation of sparsepoly -------------------
205   for( ii=u; ii<=o; ii=ii+1 ) { f=f+sparseid(1,ii,ii,p,b)[1]; }
206   return(f);
207}
208example
209{ "EXAMPLE:"; echo = 2;
210   ring r=0,(x,y,z),dp;
211   sparsepoly(5);"";
212   sparsepoly(3,5,90,9);
213}
214///////////////////////////////////////////////////////////////////////////////
215
216proc sparsetriag (int n, int m, list #)
217"USAGE:   sparsetriag(n,m[,p,b]);  n,m,p,b integers
218RETURN:  nxm lower triagonal integer matrix, diagonal entries equal to 1, about
219         p percent of lower diagonal entries are 0, the remaining are random
220         integers >=1 and <= b; [defaults: (p,b) = (75,1)]
221EXAMPLE: example sparsetriag; shows an example
222"
223{
224   int ii,min,l,r; intmat M[n][m];
225   int t=(n*(n-1)) div 2;
226//----------------------------- set defaults ----------------------------------
227   if( size(#)>=2 ) { int p=#[1]; int b=#[2]; }
228   if( size(#)==1 ) { int p=#[1]; int b=1; }
229   if( size(#)==0 ) { int p=75; int b=1; }
230//---------------- use sparsemat for creation of sparsetriag ------------------
231   intmat v[1][t]=sparsemat(1,t,p,b);
232   if( n<=m ) { min=n-1; M[n,n]=1; }
233   else { min=m; }
234   for( ii=1; ii<=min; ii=ii+1 )
235   {
236      l=r+1; r=r+n-ii;
237      M[ii..n,ii]=1,v[1,l..r];
238   }
239   return(M);
240}
241example
242{ "EXAMPLE:"; echo = 2;
243   sparsetriag(5,7);"";
244   sparsetriag(7,5,90);"";
245   sparsetriag(5,5,0);"";
246   sparsetriag(5,5,50,100);
247}
248///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.