[380a17b] | 1 | //////////////////////////////////////////////////////////////////////////// |
---|
[3686937] | 2 | version="version solve.lib 4.0.0.0 Jun_2013 "; // $Id$ |
---|
[fd3fb7] | 3 | category="Symbolic-numerical solving"; |
---|
[0f5091] | 4 | info=" |
---|
[8bb77b] | 5 | LIBRARY: solve.lib Complex Solving of Polynomial Systems |
---|
[0f5091] | 6 | AUTHOR: Moritz Wenk, email: wenk@mathematik.uni-kl.de |
---|
[8b96123] | 7 | Wilfred Pohl, email: pohl@mathematik.uni-kl.de |
---|
[0f5091] | 8 | |
---|
[f34c37c] | 9 | PROCEDURES: |
---|
[0ba413] | 10 | laguerre_solve(p,[..]); find all roots of univariate polynomial p |
---|
[8b96123] | 11 | solve(i,[..]); all roots of 0-dim. ideal i using triangular sets |
---|
| 12 | ures_solve(i,[..]); find all roots of 0-dimensional ideal i with resultants |
---|
[9173792] | 13 | mp_res_mat(i,[..]); multipolynomial resultant matrix of ideal i |
---|
[3754ca] | 14 | interpolate(p,v,d); interpolate polynomial from evaluation points i and results j |
---|
[9173792] | 15 | fglm_solve(i,[..]); find roots of 0-dim. ideal using FGLM and lex_solve |
---|
[0ba413] | 16 | lex_solve(i,p,[..]); find roots of reduced lexicographic standard basis |
---|
[fb86b6b] | 17 | simplexOut(l); prints solution of simplex in nice format |
---|
[9173792] | 18 | triangLf_solve(l,[..]); find roots using triangular sys. (factorizing Lazard) |
---|
| 19 | triangM_solve(l,[..]); find roots of given triangular system (Moeller) |
---|
[0ba413] | 20 | triangL_solve(l,[..]); find roots using triangular system (Lazard) |
---|
[9173792] | 21 | triang_solve(l,p,[..]); find roots of given triangular system |
---|
[8b96123] | 22 | "; |
---|
[0f5091] | 23 | |
---|
[0ba413] | 24 | LIB "triang.lib"; // needed for triang_solve |
---|
[5c67581] | 25 | |
---|
[0f5091] | 26 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 27 | |
---|
| 28 | proc laguerre_solve( poly f, list # ) |
---|
[8b96123] | 29 | "USAGE: laguerre_solve(f [, m, l, n, s] ); f = polynomial,@* |
---|
[0bc582c] | 30 | m, l, n, s = integers (control parameters of the method)@* |
---|
[3c4dcc] | 31 | m: precision of output in digits ( 4 <= m), if basering is not ring of |
---|
[fb86b6b] | 32 | complex numbers; |
---|
| 33 | l: precision of internal computation in decimal digits ( l >=8 ) |
---|
[0bc582c] | 34 | only if the basering is not complex or complex with smaller precision;@* |
---|
[fb86b6b] | 35 | n: control of multiplicity of roots or of splitting of f into |
---|
| 36 | squarefree factors |
---|
| 37 | n < 0, no split of f (good, if all roots are simple) |
---|
| 38 | n >= 0, try to split |
---|
| 39 | n = 0, return only different roots |
---|
| 40 | n > 0, find all roots (with multiplicity) |
---|
[3c4dcc] | 41 | s: s != 0, returns ERROR if | f(root) | > 0.1^m (when computing in the |
---|
[fb86b6b] | 42 | current ring) |
---|
| 43 | ( default: m, l, n, s = 8, 30, 1, 0 ) |
---|
| 44 | ASSUME: f is a univariate polynomial;@* |
---|
| 45 | basering has characteristic 0 and is either complex or without |
---|
[3c4dcc] | 46 | parameters. |
---|
| 47 | RETURN: list of (complex) roots of the polynomial f, depending on n. The |
---|
[837fed] | 48 | entries of the result are of type@* |
---|
[6ed15c] | 49 | string: if the basering is not complex,@* |
---|
| 50 | number: otherwise. |
---|
[0ba413] | 51 | NOTE: If printlevel >0: displays comments ( default = 0 ). |
---|
[8b96123] | 52 | If s != 0 and if the procedure stops with ERROR, try a higher |
---|
[0ba413] | 53 | internal precision m. |
---|
[0f5091] | 54 | EXAMPLE: example laguerre_solve; shows an example |
---|
| 55 | " |
---|
| 56 | { |
---|
[fb86b6b] | 57 | if (char(basering)!=0){ERROR("characteristic of basering not 0");} |
---|
| 58 | if ((charstr(basering)[1]=="0") and (npars(basering)!=0)) |
---|
| 59 | {ERROR("basering has parameters");} |
---|
[0ba413] | 60 | int OLD_COMPLEX=0; |
---|
[3c4dcc] | 61 | int iv=checkv(f); // check for variable appearing in f |
---|
[f10ed0] | 62 | if(iv==0){ERROR("Wrong polynomial!");} |
---|
[fb86b6b] | 63 | poly v=var(iv); // f univariate in v |
---|
[0ba413] | 64 | |
---|
[8b96123] | 65 | int solutionprec=8;// set the control |
---|
| 66 | int numberprec=30; |
---|
[f10ed0] | 67 | int splitcontrol=1; |
---|
| 68 | int rootcheck=0; |
---|
[8b96123] | 69 | if(size(#)>0){solutionprec=#[1];if(solutionprec<4){solutionprec=4;}} |
---|
[fb86b6b] | 70 | if(size(#)>1){numberprec=#[2];if(numberprec<8){numberprec=8;}} |
---|
[8b96123] | 71 | if(solutionprec>numberprec){numberprec=solutionprec;} |
---|
[f10ed0] | 72 | if(size(#)>2){splitcontrol=#[3];} |
---|
| 73 | if(size(#)>3){rootcheck=#[4];} |
---|
| 74 | int prot=printlevel-voice+2; |
---|
[0ba413] | 75 | int ringprec=0; |
---|
| 76 | |
---|
[fb86b6b] | 77 | poly p=divzero(f,iv); // divide out zeros as solution |
---|
| 78 | int iz=deg(f)-deg(p); // multiplicity of zero solution |
---|
[f10ed0] | 79 | if(prot!=0) |
---|
| 80 | { |
---|
| 81 | string pout; |
---|
| 82 | string nl=newline; |
---|
| 83 | pout="//BEGIN laguerre_solve"; |
---|
| 84 | if(iz!=0){pout=pout+nl+"//zeros: divide out "+string(iz);} |
---|
| 85 | dbprint(prot,pout); |
---|
| 86 | } |
---|
| 87 | string ss,tt,oo; |
---|
[0ba413] | 88 | ss="";oo=ss; |
---|
[f10ed0] | 89 | if(npars(basering)==1) |
---|
| 90 | { |
---|
[0ba413] | 91 | if(OLD_COMPLEX) |
---|
| 92 | { |
---|
| 93 | tt="1,"+string(par(1)); |
---|
| 94 | if(tt==charstr(basering)) |
---|
| 95 | {ss=tt;ringprec=system("getPrecDigits");} |
---|
| 96 | } |
---|
| 97 | else |
---|
| 98 | { |
---|
| 99 | tt=charstr(basering); |
---|
| 100 | if(size(tt)>7) |
---|
| 101 | { |
---|
[8b96123] | 102 | if(string(tt[1..7])=="complex") |
---|
[0ba413] | 103 | { |
---|
| 104 | ss=tt; |
---|
| 105 | ringprec=system("getPrecDigits"); |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | } |
---|
[f10ed0] | 109 | } |
---|
[0ba413] | 110 | |
---|
[f10ed0] | 111 | list roots,simple; |
---|
[3c4dcc] | 112 | if(deg(p)==0) // only zero was root |
---|
[f10ed0] | 113 | { |
---|
| 114 | roots=addzero(roots,ss,iz,splitcontrol); |
---|
| 115 | if(prot!=0){dbprint(prot,"//END laguerre_solve");} |
---|
| 116 | return(roots); |
---|
| 117 | } |
---|
[0ba413] | 118 | |
---|
[f10ed0] | 119 | if(prot!=0)// more informations |
---|
| 120 | { |
---|
| 121 | pout="//control: complex ring with precision "+string(numberprec); |
---|
| 122 | if(size(ss)==0){pout=pout+nl+ |
---|
| 123 | "// basering not complex, hence solutiontype string"; |
---|
| 124 | if(solutionprec<numberprec){pout=pout+nl+ |
---|
| 125 | "// with precision "+string(solutionprec);}} |
---|
[0ba413] | 126 | if(splitcontrol<0){pout=pout+nl+ "// no spliting";} |
---|
| 127 | if(splitcontrol==0){pout=pout+nl+"// output without multiple roots";} |
---|
[f10ed0] | 128 | if(rootcheck){pout=pout+nl+ |
---|
| 129 | "// check roots with precision "+string(solutionprec);} |
---|
| 130 | dbprint(prot,pout); |
---|
| 131 | } |
---|
[0f5091] | 132 | |
---|
[8b96123] | 133 | def rn = basering;// set the complex ground field |
---|
[0ba413] | 134 | if (ringprec<numberprec) |
---|
| 135 | { |
---|
| 136 | tt="ring lagc=(complex,"+string(numberprec)+","+string(numberprec)+ |
---|
| 137 | "),"+string(var(iv))+",lp;"; |
---|
| 138 | execute(tt); |
---|
| 139 | poly p=imap(rn,p); |
---|
| 140 | poly v=var(1); |
---|
| 141 | } |
---|
[f10ed0] | 142 | int ima=0; |
---|
| 143 | if(size(ss)!=0){ima=checkim(p);} |
---|
| 144 | number prc=0.1;// set precision of the solution |
---|
| 145 | prc=prc^solutionprec; |
---|
| 146 | if(prot!=0) |
---|
| 147 | { |
---|
[0ba413] | 148 | if(ringprec<numberprec){pout="//working in: "+tt;} |
---|
[f10ed0] | 149 | if((size(ss)!=0)&&(ima!=0)){pout=pout+nl+ |
---|
| 150 | "// polynomial has complex coefficients";} |
---|
| 151 | dbprint(prot,pout); |
---|
| 152 | } |
---|
[0ba413] | 153 | |
---|
[f10ed0] | 154 | int i1=1; |
---|
| 155 | int i2=1; |
---|
| 156 | ideal SPLIT=p; |
---|
[fb86b6b] | 157 | if(splitcontrol>=0)// splitting |
---|
[f10ed0] | 158 | { |
---|
| 159 | if(prot!=0){dbprint(prot,"//split in working ring:");} |
---|
| 160 | SPLIT=splitsqrfree(p,v); |
---|
| 161 | i1=size(SPLIT); |
---|
| 162 | if((i1==1)&&(charstr(rn)=="0")) |
---|
[5c67581] | 163 | { |
---|
[f10ed0] | 164 | if(prot!=0){dbprint(prot,"//split exact in basering:");} |
---|
| 165 | setring rn; |
---|
| 166 | if(v>1) |
---|
| 167 | { |
---|
| 168 | ideal SQQQQ=splitsqrfree(p,v); |
---|
| 169 | setring lagc; |
---|
| 170 | SPLIT=imap(rn,SQQQQ); |
---|
| 171 | } |
---|
| 172 | else |
---|
| 173 | { |
---|
| 174 | oo="ring exa=0,"+string(var(1))+",lp;"; |
---|
| 175 | execute(oo); |
---|
| 176 | ideal SQQQQ=splitsqrfree(imap(rn,p),var(1)); |
---|
| 177 | setring lagc; |
---|
| 178 | SPLIT=imap(exa,SQQQQ); |
---|
| 179 | kill exa; |
---|
| 180 | } |
---|
| 181 | i1=size(SPLIT); |
---|
[5c67581] | 182 | } |
---|
[f10ed0] | 183 | if(prot!=0) |
---|
[0f5091] | 184 | { |
---|
[f10ed0] | 185 | if(i1>1) |
---|
| 186 | { |
---|
| 187 | int i3=deg(SPLIT[1]); |
---|
| 188 | pout="//results of split(the squarefree factors):"; |
---|
| 189 | if(i3>0){pout=pout+nl+ |
---|
| 190 | "// multiplicity "+string(i2)+", degree "+string(i3);} |
---|
| 191 | while(i2<i1) |
---|
[cb8d1d6] | 192 | { |
---|
[f10ed0] | 193 | i2++; |
---|
| 194 | i3=deg(SPLIT[i2]); |
---|
| 195 | if(i3>0){pout=pout+nl+ |
---|
| 196 | "// multiplicity "+string(i2)+", degree "+string(i3);} |
---|
[cb8d1d6] | 197 | } |
---|
[f10ed0] | 198 | dbprint(prot,pout); |
---|
| 199 | i2=1; |
---|
| 200 | } |
---|
| 201 | else |
---|
| 202 | { |
---|
| 203 | if(charstr(rn)=="0"){dbprint(prot,"// polynomial is squarefree");} |
---|
[0bc582c] | 204 | else{dbprint(prot,"//split without result");} |
---|
[f10ed0] | 205 | } |
---|
[0d69875] | 206 | } |
---|
[f10ed0] | 207 | } |
---|
| 208 | |
---|
| 209 | p=SPLIT[1];// the first part |
---|
| 210 | if(deg(p)>0) |
---|
| 211 | { |
---|
| 212 | roots=laguerre(p,numberprec,1);// the ring is already complex, hence numberprec is dummy |
---|
| 213 | if((size(roots)==0)||(string(roots[1])=="0")){ERROR("laguerre: no roots found");} |
---|
| 214 | if(rootcheck){checkroots(p,v,roots,ima,prc);} |
---|
| 215 | } |
---|
| 216 | while(i2<i1) |
---|
| 217 | { |
---|
| 218 | i2++; |
---|
| 219 | p=SPLIT[i2];// the part with multiplicity i2 |
---|
| 220 | if(deg(p)>0) |
---|
[0d69875] | 221 | { |
---|
[f10ed0] | 222 | simple=laguerre(p,numberprec,1); |
---|
| 223 | if((size(simple)==0)||(string(simple[1])=="0")){ERROR("laguerre: no roots found");} |
---|
| 224 | if(rootcheck){checkroots(p,v,simple,ima,prc);} |
---|
| 225 | if(splitcontrol==0)// no multiple roots |
---|
| 226 | { |
---|
| 227 | roots=roots+simple; |
---|
| 228 | } |
---|
| 229 | else// multiple roots |
---|
| 230 | { |
---|
| 231 | roots=roots+makemult(simple,i2); |
---|
| 232 | } |
---|
[0f5091] | 233 | } |
---|
[f10ed0] | 234 | } |
---|
[0f5091] | 235 | |
---|
[f10ed0] | 236 | if((solutionprec<numberprec)&&(size(ss)==0))// shorter output |
---|
| 237 | { |
---|
| 238 | oo="ring lout=(complex,"+string(solutionprec)+",1)," |
---|
| 239 | +string(var(1))+",lp;"; |
---|
| 240 | execute(oo); |
---|
| 241 | list roots=imap(lagc,roots); |
---|
| 242 | roots=transroots(roots); |
---|
| 243 | if(iz>0){roots=addzero(roots,ss,iz,splitcontrol);} |
---|
| 244 | if(prot!=0){dbprint(prot,"//END laguerre_solve");} |
---|
| 245 | return(roots); |
---|
| 246 | } |
---|
| 247 | if(size(ss)==0){roots=transroots(roots);}// transform to string |
---|
| 248 | else // or map in basering |
---|
| 249 | { |
---|
[0ba413] | 250 | if(ringprec<numberprec) |
---|
| 251 | { |
---|
| 252 | setring rn; |
---|
| 253 | list roots=imap(lagc,roots); |
---|
| 254 | } |
---|
[f10ed0] | 255 | } |
---|
| 256 | if(iz>0){roots=addzero(roots,ss,iz,splitcontrol);} |
---|
| 257 | if(prot!=0){dbprint(prot,"//END laguerre_solve");} |
---|
| 258 | return(roots); |
---|
[0f5091] | 259 | } |
---|
| 260 | example |
---|
| 261 | { |
---|
[5c67581] | 262 | "EXAMPLE:";echo=2; |
---|
| 263 | // Find all roots of an univariate polynomial using Laguerre's method: |
---|
| 264 | ring rs1= 0,(x,y),lp; |
---|
| 265 | poly f = 15x5 + x3 + x2 - 10; |
---|
[9173792] | 266 | // 10 digits precision |
---|
[5c67581] | 267 | laguerre_solve(f,10); |
---|
[0f5091] | 268 | |
---|
[f10ed0] | 269 | // Now with complex coefficients, |
---|
| 270 | // internal precision is 30 digits (default) |
---|
| 271 | printlevel=2; |
---|
[8b96123] | 272 | ring rsc= (real,10,i),x,lp; |
---|
| 273 | poly f = (15.4+i*5)*x^5 + (25.0e-2+i*2)*x^3 + x2 - 10*i; |
---|
[5c67581] | 274 | list l = laguerre_solve(f); |
---|
| 275 | l; |
---|
[3754ca] | 276 | // check result, value of substituted polynomial should be near to zero |
---|
[8b96123] | 277 | // remember that l contains a list of strings |
---|
| 278 | // in the case of a different ring |
---|
[5c67581] | 279 | subst(f,x,l[1]); |
---|
| 280 | subst(f,x,l[2]); |
---|
[0f5091] | 281 | } |
---|
[0ba413] | 282 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 283 | // subprocedures for laguerre_solve |
---|
[f10ed0] | 284 | /* |
---|
| 285 | * if p depends only on var(i) |
---|
| 286 | * returns i |
---|
| 287 | * otherwise 0 |
---|
| 288 | */ |
---|
| 289 | static proc checkv(poly p) |
---|
| 290 | { |
---|
| 291 | int n=nvars(basering); |
---|
| 292 | int i=0; |
---|
| 293 | int v; |
---|
[0ba413] | 294 | |
---|
[f10ed0] | 295 | while (n>0) |
---|
| 296 | { |
---|
| 297 | if ((p-subst(p,var(n),0))!=0) |
---|
| 298 | { |
---|
| 299 | i++; |
---|
| 300 | if (i>1){return(0);} |
---|
| 301 | v=n; |
---|
| 302 | } |
---|
| 303 | n--; |
---|
| 304 | } |
---|
| 305 | return(v); |
---|
| 306 | } |
---|
| 307 | /* |
---|
[8b96123] | 308 | * if p has only real coefficients |
---|
[f10ed0] | 309 | * returns 0 |
---|
| 310 | * otherwise 1 |
---|
| 311 | */ |
---|
| 312 | static proc checkim(poly p) |
---|
| 313 | { |
---|
| 314 | poly q=p; |
---|
[0ba413] | 315 | |
---|
[f10ed0] | 316 | while(q!=0) |
---|
| 317 | { |
---|
| 318 | if(impart(leadcoef(q))!=0){return(1);} |
---|
| 319 | q=q-lead(q); |
---|
| 320 | } |
---|
| 321 | return(0); |
---|
| 322 | } |
---|
| 323 | /* |
---|
| 324 | * make multiplicity m |
---|
| 325 | */ |
---|
| 326 | static proc makemult(list si,int m) |
---|
| 327 | { |
---|
| 328 | int k0=0; |
---|
| 329 | int k1=size(si); |
---|
| 330 | int k2,k3; |
---|
| 331 | number ro; |
---|
| 332 | list msi; |
---|
[0ba413] | 333 | |
---|
[f10ed0] | 334 | for(k2=1;k2<=k1;k2++) |
---|
| 335 | { |
---|
| 336 | ro=si[k2]; |
---|
| 337 | for(k3=m;k3>0;k3--){k0++;msi[k0]=ro;} |
---|
| 338 | } |
---|
| 339 | return(msi); |
---|
| 340 | } |
---|
| 341 | /* |
---|
| 342 | * returns 1 for n<1 |
---|
| 343 | */ |
---|
| 344 | static proc cmp1(number n) |
---|
| 345 | { |
---|
| 346 | number r=repart(n); |
---|
| 347 | number i=impart(n); |
---|
| 348 | number c=r*r+i*i; |
---|
| 349 | if(c>1){return(1);} |
---|
| 350 | else{return(0);} |
---|
| 351 | } |
---|
| 352 | /* |
---|
| 353 | * exact division of polys f/g |
---|
| 354 | * (should be internal) |
---|
| 355 | */ |
---|
| 356 | static proc exdiv(poly f,poly g,poly v) |
---|
| 357 | { |
---|
| 358 | int d1=deg(f); |
---|
| 359 | int d2=deg(g); |
---|
| 360 | poly r0=f; |
---|
| 361 | poly rf=0; |
---|
| 362 | poly h; |
---|
| 363 | number n,m; |
---|
| 364 | |
---|
| 365 | m=leadcoef(g); |
---|
| 366 | while ((r0!=0)&&(d1>=d2)) |
---|
| 367 | { |
---|
| 368 | n=leadcoef(r0)/m; |
---|
| 369 | h=n*v^(d1-d2); |
---|
| 370 | rf=rf+h; |
---|
| 371 | r0=r0-h*g; |
---|
| 372 | d1=deg(r0); |
---|
| 373 | } |
---|
| 374 | return(cleardenom(rf)); |
---|
| 375 | } |
---|
| 376 | /* |
---|
[8b96123] | 377 | * p is univariant in x |
---|
[f10ed0] | 378 | * perform a split of p into squarefree factors |
---|
[0ba413] | 379 | * such that the returned ideal 'split' consists of |
---|
| 380 | * the faktors, i.e. |
---|
[f10ed0] | 381 | * p = n * product ( split[i]^i ) , n a number |
---|
| 382 | */ |
---|
[8b96123] | 383 | static proc splitsqrfree(poly p, poly x) |
---|
[f10ed0] | 384 | { |
---|
| 385 | int dd=deg(p); |
---|
[8b96123] | 386 | if(dd==1){return(p);} |
---|
| 387 | int i=1; |
---|
[f10ed0] | 388 | int j; |
---|
[68cb555] | 389 | ideal h,split; |
---|
| 390 | poly high; |
---|
[f10ed0] | 391 | |
---|
[68cb555] | 392 | h=interred(ideal(p,diff(p,x))); |
---|
| 393 | if(deg(h[1])==0){return(p);} |
---|
| 394 | high=h[1]; |
---|
[f10ed0] | 395 | split[1]=exdiv(p,high,x); |
---|
| 396 | while(1) |
---|
| 397 | { |
---|
[68cb555] | 398 | h=interred(ideal(split[i],high)); |
---|
| 399 | j=deg(h[1]); |
---|
[f10ed0] | 400 | if(j==0){return(p);} |
---|
[68cb555] | 401 | if(deg(h[1])==deg(split[i])) |
---|
[f10ed0] | 402 | { |
---|
| 403 | split=split,split[i]; |
---|
| 404 | split[i]=1; |
---|
| 405 | } |
---|
| 406 | else |
---|
| 407 | { |
---|
| 408 | split[i]=exdiv(split[i],h[1],x); |
---|
| 409 | split=split,h[1]; |
---|
| 410 | dd=dd-deg(split[i])*i; |
---|
| 411 | } |
---|
| 412 | j=j*(i+1); |
---|
| 413 | if(j==dd){break;} |
---|
| 414 | if(j>dd){return(p);} |
---|
| 415 | high=exdiv(high,h[1],x); |
---|
[62dc18e] | 416 | if(deg(high)==0){return(p);} |
---|
[f10ed0] | 417 | i++; |
---|
| 418 | } |
---|
| 419 | return(split); |
---|
| 420 | } |
---|
| 421 | /* |
---|
| 422 | * see checkroots |
---|
| 423 | */ |
---|
| 424 | static proc nerr(number n,number m) |
---|
| 425 | { |
---|
| 426 | int r; |
---|
| 427 | number z=0; |
---|
| 428 | number nr=repart(n); |
---|
| 429 | number ni=impart(n); |
---|
| 430 | if(nr<z){nr=z-nr;} |
---|
| 431 | if(ni<z){ni=nr-ni;} |
---|
| 432 | else{ni=nr+ni;} |
---|
| 433 | if(ni<m){r=0;} |
---|
| 434 | else{r=1;} |
---|
| 435 | return(r); |
---|
| 436 | } |
---|
| 437 | /* |
---|
| 438 | * returns ERROR for nerr(p(r[i]))>=pr |
---|
| 439 | */ |
---|
| 440 | static proc checkroots(poly p,poly v,list r,int ima,number pr) |
---|
| 441 | { |
---|
| 442 | int i=0; |
---|
| 443 | int j; |
---|
| 444 | number n,m; |
---|
| 445 | ideal li; |
---|
[0ba413] | 446 | |
---|
[f10ed0] | 447 | while(i<size(r)) |
---|
| 448 | { |
---|
| 449 | i++; |
---|
| 450 | n=r[i]; |
---|
| 451 | j=cmp1(n); |
---|
| 452 | if(j!=0){li[1]=v/n-1;m=1;} |
---|
| 453 | else{li[1]=v-n;m=n;} |
---|
| 454 | if((ima==0)&&(impart(n)!=0)) |
---|
| 455 | { |
---|
| 456 | i++; |
---|
| 457 | n=r[i]; |
---|
| 458 | if(j!=0){li[1]=li[1]*(v/n-1);} |
---|
| 459 | else{li[1]=li[1]*(v-n);m=m*n;} |
---|
| 460 | } |
---|
| 461 | attrib(li,"isSB",1); |
---|
| 462 | n=leadcoef(reduce(p,li));n=n/m; |
---|
| 463 | if(n!=0) |
---|
| 464 | {if(nerr(n,pr)!=0){ERROR("Unsufficient accuracy!");}} |
---|
| 465 | } |
---|
| 466 | } |
---|
| 467 | /* |
---|
| 468 | * transforms thr result to string |
---|
| 469 | */ |
---|
| 470 | static proc transroots(list r) |
---|
| 471 | { |
---|
| 472 | int i=size(r); |
---|
| 473 | while (i>0) |
---|
| 474 | { |
---|
| 475 | r[i]=string(r[i]); |
---|
| 476 | i--; |
---|
| 477 | } |
---|
| 478 | return(r); |
---|
| 479 | } |
---|
| 480 | /* |
---|
[3754ca] | 481 | * returns a polynomial without zeroroots |
---|
[f10ed0] | 482 | */ |
---|
[3c7701] | 483 | static proc divzero(poly f,int iv) |
---|
[f10ed0] | 484 | { |
---|
| 485 | poly p=f; |
---|
| 486 | poly q=p; |
---|
| 487 | poly r; |
---|
| 488 | while(p==q) |
---|
| 489 | { |
---|
| 490 | q=p/var(iv); |
---|
| 491 | r=q*var(iv); |
---|
| 492 | if(r==p){p=q;} |
---|
| 493 | } |
---|
| 494 | return(p); |
---|
| 495 | } |
---|
| 496 | /* |
---|
| 497 | * add zeros to solution |
---|
| 498 | */ |
---|
| 499 | static proc addzero(list zz,string ss,int iz,int a) |
---|
| 500 | { |
---|
| 501 | int i=1; |
---|
| 502 | int j=size(zz); |
---|
[0ba413] | 503 | |
---|
[f10ed0] | 504 | if(size(ss)==0){zz[j+1]="0";} |
---|
| 505 | else{zz[j+1]=number(0);} |
---|
| 506 | if(a==0){return(zz);} |
---|
| 507 | while(i<iz) |
---|
| 508 | { |
---|
| 509 | i++; |
---|
| 510 | if(size(ss)==0){zz[j+i]="0";} |
---|
| 511 | else{zz[j+i]=number(0);} |
---|
| 512 | } |
---|
| 513 | return(zz); |
---|
| 514 | } |
---|
[0f5091] | 515 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 516 | |
---|
[8b96123] | 517 | proc solve( ideal G, list # ) |
---|
[6ed15c] | 518 | "USAGE: solve(G [, m, n [, l]] [,\"oldring\"] [,\"nodisplay\"] ); G = ideal, |
---|
[0bc582c] | 519 | m, n, l = integers (control parameters of the method), outR ring,@* |
---|
[dd73043] | 520 | m: precision of output in digits ( 4 <= m) and of the generated ring |
---|
| 521 | of complex numbers; |
---|
[8b96123] | 522 | n: control of multiplicity |
---|
| 523 | n = 0, return all different roots |
---|
| 524 | n != 0, find all roots (with multiplicity) |
---|
| 525 | l: precision of internal computation in decimal digits ( l >=8 ) |
---|
| 526 | only if the basering is not complex or complex with smaller |
---|
[6ed15c] | 527 | precision, @* |
---|
| 528 | [default: (m,n,l) = (8,0,30), or if only (m,n) are set explicitly |
---|
| 529 | with n!=0, then (m,n,l) = (m,n,60) ] |
---|
[fb86b6b] | 530 | ASSUME: the ideal is 0-dimensional;@* |
---|
[3c4dcc] | 531 | basering has characteristic 0 and is either complex or without |
---|
[6ed15c] | 532 | parameters; |
---|
| 533 | RETURN: (1) If called without the additional parameter @code{\"oldring\"}: @* |
---|
[3c4dcc] | 534 | ring @code{R} with the same number of variables but with complex |
---|
| 535 | coefficients (and precision m). @code{R} comes with a list |
---|
[6ed15c] | 536 | @code{SOL} of numbers, in which complex roots of G are stored: @* |
---|
[3c4dcc] | 537 | * If n = 0, @code{SOL} is the list of all different solutions, each |
---|
[6ed15c] | 538 | of them being represented by a list of numbers. @* |
---|
[3c4dcc] | 539 | * If n != 0, @code{SOL} is a list of two list: SOL[i][1] is the list |
---|
[6ed15c] | 540 | of all different solutions with the multiplicity SOL[i][2].@* |
---|
| 541 | SOL is ordered w.r.t. multiplicity (the smallest first). @* |
---|
[3c4dcc] | 542 | (2) If called with the additional parameter @code{\"oldring\"}, the |
---|
[dd73043] | 543 | procedure looks for an appropriate ring (at top level) in which |
---|
[6ed15c] | 544 | the solutions can be stored (interactive). @* |
---|
[3c4dcc] | 545 | The user may then select an appropriate ring and choose a name for |
---|
| 546 | the output list in this ring. The list is exported directly to the |
---|
[6ed15c] | 547 | selected ring and the return value is a string \"result exported to\" |
---|
| 548 | + name of the selected ring. |
---|
[3c4dcc] | 549 | NOTE: If the problem is not 0-dim. the procedure stops with ERROR. If the |
---|
| 550 | ideal G is not a lexicographic Groebner basis, the lexicographic |
---|
[6ed15c] | 551 | Groebner basis is computed internally (Hilbert driven). @* |
---|
| 552 | The computed solutions are displayed, unless @code{solve} is called |
---|
| 553 | with the additional parameter @code{\"nodisplay\"}. |
---|
[8b96123] | 554 | EXAMPLE: example solve; shows an example |
---|
| 555 | " |
---|
| 556 | { |
---|
[fb86b6b] | 557 | // test if basering admissible |
---|
| 558 | if (char(basering)!=0){ERROR("characteristic of basering not 0");} |
---|
[518545] | 559 | if ((charstr(basering)[1]=="0") and (npars(basering)!=0)) |
---|
| 560 | { ERROR("basering has parameters"); } |
---|
[fb86b6b] | 561 | |
---|
[8b96123] | 562 | // some global settings and control |
---|
[6ed15c] | 563 | int oldr, nodisp, ii, jj; |
---|
| 564 | list LL; |
---|
[8b96123] | 565 | int outprec = 8; |
---|
| 566 | int mu = 0; |
---|
| 567 | int prec = 30; |
---|
[6ed15c] | 568 | // check additional parameters... |
---|
[518545] | 569 | if (size(#)>0) |
---|
| 570 | { |
---|
[6ed15c] | 571 | int sofar=1; |
---|
[2a4de84] | 572 | if (typeof(#[1])=="int") |
---|
| 573 | { |
---|
[6ed15c] | 574 | outprec = #[1]; |
---|
| 575 | if (outprec<4){outprec = 4;} |
---|
[2a4de84] | 576 | if (size(#)>1) |
---|
| 577 | { |
---|
| 578 | if (typeof(#[2])=="int") |
---|
| 579 | { |
---|
[6ed15c] | 580 | mu = #[2]; |
---|
[2a4de84] | 581 | if (size(#)>2) |
---|
| 582 | { |
---|
| 583 | if (typeof(#[3])=="int") |
---|
| 584 | { |
---|
[6ed15c] | 585 | prec = #[3]; |
---|
| 586 | if (prec<8){prec = 8;} |
---|
| 587 | } |
---|
[2a4de84] | 588 | else |
---|
| 589 | { |
---|
[6ed15c] | 590 | if(mu!=0){prec = 60;} |
---|
| 591 | if (#[3]=="oldring"){ oldr=1; } |
---|
| 592 | if (#[3]=="nodisplay"){ nodisp=1; } |
---|
| 593 | } |
---|
| 594 | sofar=3; |
---|
| 595 | } |
---|
| 596 | } |
---|
[2a4de84] | 597 | else |
---|
| 598 | { |
---|
[6ed15c] | 599 | if (#[2]=="oldring"){ oldr=1; } |
---|
| 600 | if (#[2]=="nodisplay"){ nodisp=1; } |
---|
| 601 | } |
---|
| 602 | sofar=2; |
---|
| 603 | } |
---|
| 604 | } |
---|
[2a4de84] | 605 | else |
---|
| 606 | { |
---|
[6ed15c] | 607 | if (#[1]=="oldring"){ oldr=1; } |
---|
| 608 | if (#[1]=="nodisplay"){ nodisp=1; } |
---|
[3c4dcc] | 609 | } |
---|
[2a4de84] | 610 | for (ii=sofar+1;ii<=size(#);ii++) |
---|
| 611 | { // check for additional strings |
---|
| 612 | if (typeof(#[ii])=="string") |
---|
| 613 | { |
---|
| 614 | if (#[ii]=="oldring"){ oldr=1; } |
---|
| 615 | if (#[ii]=="nodisplay"){ nodisp=1; } |
---|
| 616 | } |
---|
[6ed15c] | 617 | } |
---|
[3c4dcc] | 618 | } |
---|
[8b96123] | 619 | if (outprec>prec){prec = outprec;} |
---|
[6ed15c] | 620 | // if interaktive version is chosen -- choice of basering (Top::`outR`) |
---|
| 621 | // and name for list of solutions (outL): |
---|
[518545] | 622 | if (oldr==1) |
---|
| 623 | { |
---|
[3c4dcc] | 624 | list Out; |
---|
[6ed15c] | 625 | LL=names(Top); |
---|
| 626 | for (ii=1;ii<=size(LL);ii++) |
---|
[3c4dcc] | 627 | { |
---|
[2a4de84] | 628 | if (typeof(`LL[ii]`)=="ring") |
---|
| 629 | { |
---|
| 630 | if (find(charstr(`LL[ii]`),"complex,"+string(outprec))) |
---|
| 631 | { |
---|
[6ed15c] | 632 | jj++; |
---|
| 633 | Out[jj]=LL[ii]; |
---|
| 634 | } |
---|
| 635 | } |
---|
| 636 | } |
---|
[2a4de84] | 637 | if (size(Out)>0) |
---|
| 638 | { |
---|
[6ed15c] | 639 | print("// *** You may select between the following rings for storing "+ |
---|
| 640 | "the list of"); |
---|
| 641 | print("// *** complex solutions:"); |
---|
| 642 | Out; |
---|
| 643 | print("// *** Enter the number of the chosen ring"); |
---|
| 644 | print("// *** (0: none of them => new ring created and returned)"); |
---|
| 645 | string chosen; |
---|
| 646 | while (chosen=="") { chosen=read(""); } |
---|
| 647 | execute("def tchosen = "+chosen); |
---|
[2a4de84] | 648 | if (typeof(tchosen)=="int") |
---|
| 649 | { |
---|
| 650 | if ((tchosen>0) and (tchosen<=size(Out))) |
---|
| 651 | { |
---|
[6ed15c] | 652 | string outR = Out[tchosen]; |
---|
| 653 | print("// *** You have chosen the ring "+ outR +". In this ring" |
---|
| 654 | +" the following objects"); |
---|
| 655 | print("//*** are defined:"); |
---|
| 656 | listvar(Top::`outR`); |
---|
| 657 | print("// *** Enter a name for the list of solutions (different "+ |
---|
[3c4dcc] | 658 | "from existing names):"); |
---|
[6ed15c] | 659 | string outL; |
---|
| 660 | while (outL==""){ outL=read(""); } |
---|
| 661 | } |
---|
| 662 | } |
---|
| 663 | } |
---|
[2a4de84] | 664 | else |
---|
| 665 | { |
---|
[6ed15c] | 666 | print("No appropriate ring for storing the list of solutions found " + |
---|
| 667 | "=> new ring created and returned"); |
---|
| 668 | } |
---|
[3c4dcc] | 669 | if (not(defined(outR))) { oldr=0; } |
---|
[6ed15c] | 670 | } |
---|
| 671 | |
---|
| 672 | // string rinC = nameof(basering)+"C"; |
---|
[8b96123] | 673 | string sord = ordstr(basering); |
---|
| 674 | int nv = nvars(basering); |
---|
| 675 | def rin = basering; |
---|
| 676 | intvec ovec = option(get); |
---|
| 677 | option(redSB); |
---|
| 678 | option(returnSB); |
---|
| 679 | int sb = attrib(G,"isSB"); |
---|
| 680 | int lp = 0; |
---|
| 681 | if (size(sord)==size("C,lp()"+string(nv))) |
---|
| 682 | { |
---|
| 683 | lp = find(sord,"lp"); |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | // ERROR |
---|
| 687 | if (sb){if (dim(G)!=0){ERROR("ideal not zero-dimensional");}} |
---|
| 688 | |
---|
[6ed15c] | 689 | // the trivial homogeneous case (unique solution: (0,...0)) |
---|
[8b96123] | 690 | if (homog(G)) |
---|
| 691 | { |
---|
| 692 | if (sb==0) |
---|
| 693 | { |
---|
| 694 | execute("ring dphom=("+charstr(rin)+"),("+ |
---|
| 695 | varstr(rin)+"),dp;"); |
---|
| 696 | ideal G = std(imap(rin,G)); |
---|
| 697 | if (dim(G)!=0){ERROR("ideal not zero-dimensional");} |
---|
[6ed15c] | 698 | int vdG=vdim(G); |
---|
| 699 | } |
---|
[2a4de84] | 700 | if (oldr!=1) |
---|
| 701 | { |
---|
[6ed15c] | 702 | execute("ring rinC =(complex,"+string(outprec)+ |
---|
[3c4dcc] | 703 | "),("+varstr(basering)+"),lp;"); |
---|
[6ed15c] | 704 | list SOL; |
---|
| 705 | if (mu==0){SOL[1] = zerolist(nv);} |
---|
| 706 | else{SOL[1] = list(zerolist(nv),list(vdG));} |
---|
| 707 | export SOL; |
---|
| 708 | if (nodisp==0) { print(SOL); } |
---|
| 709 | option(set,ovec); |
---|
| 710 | dbprint( printlevel-voice+3," |
---|
| 711 | // 'solve' created a ring, in which a list SOL of numbers (the complex solutions) |
---|
[3c4dcc] | 712 | // is stored. |
---|
| 713 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 714 | // to the return value): |
---|
[6ed15c] | 715 | setring R; SOL; "); |
---|
| 716 | return(rinC); |
---|
| 717 | } |
---|
[2a4de84] | 718 | else |
---|
| 719 | { |
---|
[6ed15c] | 720 | setring (Top::`outR`); |
---|
| 721 | list SOL; |
---|
| 722 | if (mu==0){SOL[1] = zerolist(nv);} |
---|
| 723 | else{SOL[1] = list(zerolist(nv),list(vdG));} |
---|
| 724 | execute("def "+outL + "=SOL;"); |
---|
| 725 | execute("export "+outL+";"); |
---|
| 726 | if (nodisp==0) { print(SOL); } |
---|
| 727 | option(set,ovec); |
---|
| 728 | kill SOL; |
---|
| 729 | return("result exported to "+outR+" as list "+outL); |
---|
[8b96123] | 730 | } |
---|
| 731 | } |
---|
| 732 | |
---|
| 733 | // look for reduced standard basis in lex |
---|
[2a4de84] | 734 | if (sb*lp==0) |
---|
| 735 | { |
---|
| 736 | if (sb==0) |
---|
| 737 | { |
---|
[6ed15c] | 738 | execute("ring dphilb=("+charstr(rin)+"),("+ varstr(rin)+"),dp;"); |
---|
[fb86b6b] | 739 | ideal G = imap(rin,G); |
---|
| 740 | G = std(G); |
---|
| 741 | if (dim(G)!=0){ERROR("ideal not zero-dimensional");} |
---|
| 742 | } |
---|
[2a4de84] | 743 | else |
---|
| 744 | { |
---|
[3c4dcc] | 745 | def dphilb = basering; |
---|
[68cb555] | 746 | G=interred(G); |
---|
| 747 | attrib(G,"isSB",1); |
---|
[3c4dcc] | 748 | } |
---|
[6ed15c] | 749 | execute("ring lexhilb=("+charstr(rin)+"),("+ varstr(rin)+"),lp;"); |
---|
[8b96123] | 750 | option(redTail); |
---|
[fb86b6b] | 751 | ideal H = fglm(dphilb,G); |
---|
[8b96123] | 752 | kill dphilb; |
---|
| 753 | H = simplify(H,2); |
---|
| 754 | if (lp){setring rin;} |
---|
[2a4de84] | 755 | else |
---|
| 756 | { |
---|
| 757 | execute("ring lplex=("+charstr(rin)+"),("+varstr(rin)+"),lp;"); |
---|
[8b96123] | 758 | } |
---|
| 759 | ideal H = imap(lexhilb,H); |
---|
| 760 | kill lexhilb; |
---|
| 761 | } |
---|
[68cb555] | 762 | else{ideal H = interred(G);} |
---|
[8b96123] | 763 | |
---|
[fb86b6b] | 764 | // only 1 variable |
---|
[8b96123] | 765 | def hr = basering; |
---|
[2a4de84] | 766 | if (nv==1) |
---|
| 767 | { |
---|
| 768 | if ((mu==0) and (charstr(basering)[1]=="0")) |
---|
| 769 | { // special case |
---|
[fb86b6b] | 770 | list L = laguerre_solve(H[1],prec,prec,mu,0); // list of strings |
---|
[2a4de84] | 771 | if (oldr!=1) |
---|
| 772 | { |
---|
| 773 | execute("ring rinC =(complex,"+string(outprec)+"),("+varstr(basering)+"),lp;"); |
---|
[6ed15c] | 774 | list SOL; |
---|
[7f1e81] | 775 | for (ii=1; ii<=size(L); ii++ ) { execute("SOL[ii]=number("+L[ii]+");"); } |
---|
[6ed15c] | 776 | export SOL; |
---|
| 777 | if (nodisp==0) { print(SOL); } |
---|
| 778 | option(set,ovec); |
---|
| 779 | dbprint( printlevel-voice+3," |
---|
| 780 | // 'solve' created a ring, in which a list SOL of numbers (the complex solutions) |
---|
[3c4dcc] | 781 | // is stored. |
---|
| 782 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 783 | // to the return value): |
---|
[6ed15c] | 784 | setring R; SOL; "); |
---|
| 785 | return(rinC); |
---|
| 786 | } |
---|
[2a4de84] | 787 | else |
---|
| 788 | { |
---|
[6ed15c] | 789 | setring (Top::`outR`); |
---|
| 790 | list SOL; |
---|
| 791 | for (ii=1; ii<=size(L); ii++ ) { execute("SOL[ii]="+L[ii]+";"); } |
---|
| 792 | execute("def "+outL + "=SOL;"); |
---|
| 793 | execute("export "+outL+";"); |
---|
| 794 | if (nodisp==0) { print(SOL); } |
---|
| 795 | option(set,ovec); |
---|
| 796 | kill SOL; |
---|
| 797 | return("result exported to "+outR+" as list "+outL); |
---|
[fb86b6b] | 798 | } |
---|
[8b96123] | 799 | } |
---|
[2a4de84] | 800 | else |
---|
| 801 | { |
---|
| 802 | execute("ring internC=(complex,"+string(prec)+"),("+varstr(basering)+"),lp;"); |
---|
[fb86b6b] | 803 | ideal H = imap(hr,H); |
---|
| 804 | list sp = splittolist(splitsqrfree(H[1],var(1))); |
---|
[6ed15c] | 805 | jj = size(sp); |
---|
[fb86b6b] | 806 | while(jj>0) |
---|
| 807 | { |
---|
| 808 | sp[jj][1] = laguerre(sp[jj][1],prec,1); |
---|
| 809 | jj--; |
---|
| 810 | } |
---|
| 811 | setring hr; |
---|
[2a4de84] | 812 | if (oldr!=1) |
---|
| 813 | { |
---|
| 814 | execute("ring rinC =(complex,"+string(outprec)+"),("+varstr(basering)+"),lp;"); |
---|
[6ed15c] | 815 | list SOL; |
---|
| 816 | list sp=imap(internC,sp); |
---|
| 817 | if(mu!=0){ SOL=sp; } |
---|
[2a4de84] | 818 | else |
---|
| 819 | { |
---|
[6ed15c] | 820 | jj = size(sp); |
---|
| 821 | SOL=sp[jj][1]; |
---|
[2a4de84] | 822 | while(jj>1) |
---|
| 823 | { |
---|
[6ed15c] | 824 | jj--; |
---|
| 825 | SOL = sp[jj][1]+SOL; |
---|
| 826 | } |
---|
| 827 | } |
---|
| 828 | export SOL; |
---|
| 829 | if (nodisp==0) { print(SOL); } |
---|
| 830 | option(set,ovec); |
---|
| 831 | dbprint( printlevel-voice+3," |
---|
| 832 | // 'solve' created a ring, in which a list SOL of numbers (the complex solutions) |
---|
[3c4dcc] | 833 | // is stored. |
---|
| 834 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 835 | // to the return value): |
---|
[6ed15c] | 836 | setring R; SOL; "); |
---|
| 837 | return(rinC); |
---|
| 838 | } |
---|
[2a4de84] | 839 | else |
---|
| 840 | { |
---|
[6ed15c] | 841 | setring (Top::`outR`); |
---|
| 842 | list SOL; |
---|
| 843 | list sp=imap(internC,sp); |
---|
| 844 | if(mu!=0){ SOL=sp; } |
---|
[2a4de84] | 845 | else |
---|
| 846 | { |
---|
[6ed15c] | 847 | jj = size(sp); |
---|
| 848 | SOL=sp[jj][1]; |
---|
[2a4de84] | 849 | while(jj>1) |
---|
| 850 | { |
---|
[6ed15c] | 851 | jj--; |
---|
| 852 | SOL = sp[jj][1]+SOL; |
---|
| 853 | } |
---|
| 854 | } |
---|
| 855 | kill sp; |
---|
| 856 | execute("def "+outL + "=SOL;"); |
---|
| 857 | execute("export "+outL+";"); |
---|
| 858 | if (nodisp==0) { print(SOL); } |
---|
| 859 | option(set,ovec); |
---|
| 860 | kill SOL; |
---|
| 861 | return("result exported to "+outR+" as list "+outL); |
---|
[fb86b6b] | 862 | } |
---|
[3c4dcc] | 863 | } |
---|
| 864 | } |
---|
| 865 | |
---|
[fb86b6b] | 866 | // the triangular sets (not univariate case) |
---|
[8b96123] | 867 | attrib(H,"isSB",1); |
---|
| 868 | if (mu==0) |
---|
| 869 | { |
---|
[fb86b6b] | 870 | list sp = triangMH(H); // faster, but destroy multiplicity |
---|
[8b96123] | 871 | } |
---|
| 872 | else |
---|
| 873 | { |
---|
| 874 | list sp = triangM(H); |
---|
| 875 | } |
---|
| 876 | |
---|
| 877 | // create the complex ring and map the result |
---|
| 878 | if (outprec<prec) |
---|
| 879 | { |
---|
[2a4de84] | 880 | execute("ring internC=(complex,"+string(prec)+"),("+varstr(hr)+"),lp;"); |
---|
[8b96123] | 881 | } |
---|
| 882 | else |
---|
| 883 | { |
---|
[2a4de84] | 884 | execute("ring rinC =(complex,"+string(outprec)+"),("+varstr(basering)+"),lp;"); |
---|
[8b96123] | 885 | } |
---|
| 886 | list triC = imap(hr,sp); |
---|
| 887 | |
---|
| 888 | // solve the tridiagonal systems |
---|
| 889 | int js = size(triC); |
---|
| 890 | list ret1; |
---|
| 891 | if (mu==0) |
---|
| 892 | { |
---|
| 893 | ret1 = trisolve(list(),triC[1],prec); |
---|
| 894 | while (js>1) |
---|
| 895 | { |
---|
| 896 | ret1 = trisolve(list(),triC[js],prec)+ret1; |
---|
| 897 | js--; |
---|
| 898 | } |
---|
| 899 | } |
---|
| 900 | else |
---|
| 901 | { |
---|
| 902 | ret1 = mutrisolve(list(),triC[1],prec); |
---|
| 903 | while (js>1) |
---|
| 904 | { |
---|
| 905 | ret1 = addlist(mutrisolve(list(),triC[js],prec),ret1,1); |
---|
| 906 | js--; |
---|
| 907 | } |
---|
| 908 | ret1 = finalclear(ret1); |
---|
| 909 | } |
---|
| 910 | |
---|
| 911 | // final computations |
---|
| 912 | option(set,ovec); |
---|
[2a4de84] | 913 | if (outprec==prec) |
---|
| 914 | { // we are in ring rinC |
---|
| 915 | if (oldr!=1) |
---|
| 916 | { |
---|
[6ed15c] | 917 | list SOL=ret1; |
---|
| 918 | export SOL; |
---|
| 919 | if (nodisp==0) { print(SOL); } |
---|
| 920 | dbprint( printlevel-voice+3," |
---|
| 921 | // 'solve' created a ring, in which a list SOL of numbers (the complex solutions) |
---|
[3c4dcc] | 922 | // is stored. |
---|
| 923 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 924 | // to the return value): |
---|
[6ed15c] | 925 | setring R; SOL; "); |
---|
| 926 | return(rinC); |
---|
| 927 | } |
---|
[2a4de84] | 928 | else |
---|
| 929 | { |
---|
[6ed15c] | 930 | setring (Top::`outR`); |
---|
| 931 | list SOL=imap(rinC,ret1); |
---|
| 932 | execute("def "+outL + "=SOL;"); |
---|
| 933 | execute("export "+outL+";"); |
---|
| 934 | if (nodisp==0) { print(SOL); } |
---|
| 935 | kill SOL; |
---|
[3c4dcc] | 936 | return("result exported to "+outR+" as list "+outL); |
---|
[6ed15c] | 937 | } |
---|
| 938 | } |
---|
[2a4de84] | 939 | else |
---|
| 940 | { |
---|
| 941 | if (oldr!=1) |
---|
| 942 | { |
---|
| 943 | execute("ring rinC =(complex,"+string(outprec)+"),("+varstr(basering)+"),lp;"); |
---|
[6ed15c] | 944 | list SOL=imap(internC,ret1); |
---|
| 945 | export SOL; |
---|
| 946 | if (nodisp==0) { print(SOL); } |
---|
| 947 | dbprint( printlevel-voice+3," |
---|
| 948 | // 'solve' created a ring, in which a list SOL of numbers (the complex solutions) |
---|
[3c4dcc] | 949 | // is stored. |
---|
| 950 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 951 | // to the return value): |
---|
[6ed15c] | 952 | setring R; SOL; "); |
---|
| 953 | return(rinC); |
---|
| 954 | } |
---|
[2a4de84] | 955 | else |
---|
| 956 | { |
---|
[6ed15c] | 957 | setring (Top::`outR`); |
---|
| 958 | list SOL=imap(internC,ret1); |
---|
| 959 | execute("def "+outL + "=SOL;"); |
---|
| 960 | execute("export "+outL+";"); |
---|
| 961 | if (nodisp==0) { print(SOL); } |
---|
| 962 | kill SOL; |
---|
[3c4dcc] | 963 | return("result exported to "+outR+" as list "+outL); |
---|
[6ed15c] | 964 | } |
---|
[8b96123] | 965 | } |
---|
| 966 | } |
---|
| 967 | example |
---|
| 968 | { |
---|
| 969 | "EXAMPLE:";echo=2; |
---|
| 970 | // Find all roots of a multivariate ideal using triangular sets: |
---|
[3c4dcc] | 971 | int d,t,s = 4,3,2 ; |
---|
[8b96123] | 972 | int i; |
---|
[6ed15c] | 973 | ring A=0,x(1..d),dp; |
---|
[8b96123] | 974 | poly p=-1; |
---|
[6ed15c] | 975 | for (i=d; i>0; i--) { p=p+x(i)^s; } |
---|
| 976 | ideal I = x(d)^t-x(d)^s+p; |
---|
| 977 | for (i=d-1; i>0; i--) { I=x(i)^t-x(i)^s+p,I; } |
---|
[8b96123] | 978 | I; |
---|
[6ed15c] | 979 | // the multiplicity is |
---|
[8b96123] | 980 | vdim(std(I)); |
---|
[6ed15c] | 981 | def AC=solve(I,6,0,"nodisplay"); // solutions should not be displayed |
---|
| 982 | // list of solutions is stored in AC as the list SOL (default name) |
---|
| 983 | setring AC; |
---|
| 984 | size(SOL); // number of different solutions |
---|
| 985 | SOL[5]; // the 5th solution |
---|
[8b96123] | 986 | // you must start with char. 0 |
---|
| 987 | setring A; |
---|
[6ed15c] | 988 | def AC1=solve(I,6,1,"nodisplay"); |
---|
| 989 | setring AC1; |
---|
| 990 | size(SOL); // number of different multiplicities |
---|
| 991 | SOL[1][1][1]; // a solution with |
---|
| 992 | SOL[1][2]; // multiplicity 1 |
---|
| 993 | SOL[2][1][1]; // a solution with |
---|
| 994 | SOL[2][2]; // multiplicity 12 |
---|
| 995 | // the number of different solutions is equal to |
---|
| 996 | size(SOL[1][1])+size(SOL[2][1]); |
---|
| 997 | // the number of complex solutions (counted with multiplicities) is |
---|
| 998 | size(SOL[1][1])*SOL[1][2]+size(SOL[2][1])*SOL[2][2]; |
---|
[8b96123] | 999 | } |
---|
| 1000 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 1001 | // subprocedures for solve |
---|
| 1002 | |
---|
| 1003 | |
---|
| 1004 | /* |
---|
| 1005 | * return one zero-solution |
---|
| 1006 | */ |
---|
| 1007 | static proc zerolist(int nv) |
---|
| 1008 | { |
---|
| 1009 | list ret; |
---|
| 1010 | int i; |
---|
| 1011 | number o=0; |
---|
| 1012 | |
---|
| 1013 | for (i=nv;i>0;i--){ret[i] = o;} |
---|
| 1014 | return(ret); |
---|
| 1015 | } |
---|
| 1016 | |
---|
| 1017 | /* ----------------------- check solution ----------------------- */ |
---|
| 1018 | static proc multsol(list ff, int c) |
---|
| 1019 | { |
---|
| 1020 | int i,j; |
---|
| 1021 | |
---|
| 1022 | i = 0; |
---|
| 1023 | j = size(ff); |
---|
| 1024 | while (j>0) |
---|
| 1025 | { |
---|
| 1026 | if(c){i = i+ff[j][2]*size(ff[j][1]);} |
---|
| 1027 | else{i = i+size(ff[j][1]);} |
---|
| 1028 | j--; |
---|
| 1029 | } |
---|
| 1030 | return(i); |
---|
| 1031 | } |
---|
| 1032 | |
---|
| 1033 | /* |
---|
| 1034 | * the inputideal A => zero ? |
---|
| 1035 | */ |
---|
| 1036 | static proc checksol(ideal A, list lr) |
---|
| 1037 | { |
---|
| 1038 | int d = nvars(basering); |
---|
| 1039 | list ro; |
---|
| 1040 | ideal re,h; |
---|
| 1041 | int i,j,k; |
---|
| 1042 | |
---|
| 1043 | for (i=size(lr);i>0;i--) |
---|
| 1044 | { |
---|
| 1045 | ro = lr[i]; |
---|
| 1046 | for (j=d;j>0;j--) |
---|
| 1047 | { |
---|
| 1048 | re[j] = var(j)-ro[j]; |
---|
| 1049 | } |
---|
| 1050 | attrib(re,"isSB",1); |
---|
[2a4de84] | 1051 | k = size(reduce(A,re,1)); |
---|
[8b96123] | 1052 | if (k){return(i);} |
---|
| 1053 | } |
---|
| 1054 | return(0); |
---|
| 1055 | } |
---|
| 1056 | |
---|
| 1057 | /* |
---|
| 1058 | * compare 2 solutions: returns 0 for equal |
---|
| 1059 | */ |
---|
| 1060 | static proc cmpn(list a,list b) |
---|
| 1061 | { |
---|
| 1062 | int ii; |
---|
| 1063 | |
---|
| 1064 | for(ii=size(a);ii>0;ii--){if(a[ii]!=b[ii]) break;} |
---|
| 1065 | return(ii); |
---|
| 1066 | } |
---|
| 1067 | |
---|
| 1068 | /* |
---|
| 1069 | * delete equal solutions in the list |
---|
| 1070 | */ |
---|
| 1071 | static proc delequal(list r, int w) |
---|
| 1072 | { |
---|
| 1073 | list h; |
---|
| 1074 | int i,j,k,c; |
---|
| 1075 | |
---|
| 1076 | if (w) |
---|
| 1077 | { |
---|
| 1078 | k = size(r); |
---|
| 1079 | h = r[k][1]; |
---|
| 1080 | k--; |
---|
| 1081 | while (k>0) |
---|
| 1082 | { |
---|
| 1083 | h = r[k][1]+h; |
---|
| 1084 | k--; |
---|
| 1085 | } |
---|
| 1086 | } |
---|
| 1087 | else{h = r;} |
---|
| 1088 | k=size(h); |
---|
| 1089 | i=1; |
---|
| 1090 | while(i<k) |
---|
| 1091 | { |
---|
| 1092 | j=k; |
---|
| 1093 | while(j>i) |
---|
| 1094 | { |
---|
| 1095 | c=cmpn(h[i],h[j]); |
---|
| 1096 | if(c==0) |
---|
| 1097 | { |
---|
| 1098 | h=delete(h,j); |
---|
| 1099 | k--; |
---|
| 1100 | } |
---|
| 1101 | j--; |
---|
| 1102 | } |
---|
| 1103 | i++; |
---|
| 1104 | } |
---|
| 1105 | return(h); |
---|
| 1106 | } |
---|
| 1107 | |
---|
| 1108 | /* ----------------------- substitution ----------------------- */ |
---|
| 1109 | /* |
---|
| 1110 | * instead of subst(T,var(v),n), much faster |
---|
| 1111 | * need option(redSB) ! |
---|
| 1112 | */ |
---|
| 1113 | static proc linreduce(ideal T, int v, number n) |
---|
| 1114 | { |
---|
| 1115 | ideal re = var(v)-n; |
---|
| 1116 | attrib (re,"isSB",1); |
---|
| 1117 | return (reduce(T,re)); |
---|
| 1118 | } |
---|
| 1119 | |
---|
| 1120 | /* ----------------------- triangular solution ----------------------- */ |
---|
| 1121 | /* |
---|
| 1122 | * solution of one tridiagonal system T |
---|
| 1123 | * with precision prec |
---|
| 1124 | * T[1] is univariant in var(1) |
---|
| 1125 | * list o is empty for the first call |
---|
| 1126 | */ |
---|
| 1127 | static proc trisolve(list o, ideal T, int prec) |
---|
| 1128 | { |
---|
| 1129 | list lroots,ll; |
---|
| 1130 | ideal S; |
---|
| 1131 | int i,d; |
---|
| 1132 | |
---|
| 1133 | d = size(T); |
---|
[68cb555] | 1134 | S = interred(ideal(T[1],diff(T[1],var(d)))); |
---|
[8b96123] | 1135 | if (deg(S[1])) |
---|
| 1136 | { |
---|
| 1137 | T[1] = exdiv(T[1],S[1],var(d)); |
---|
| 1138 | } |
---|
| 1139 | ll = laguerre(T[1],prec,1); |
---|
| 1140 | for (i=size(ll);i>0;i--){ll[i] = list(ll[i])+o;} |
---|
| 1141 | if (d==1){return(ll);} |
---|
| 1142 | for (i=size(ll);i>0;i--) |
---|
| 1143 | { |
---|
| 1144 | S = linreduce(ideal(T[2..d]),d,ll[i][1]); |
---|
| 1145 | lroots = trisolve(ll[i],S,prec)+lroots; |
---|
| 1146 | } |
---|
| 1147 | return(lroots); |
---|
| 1148 | } |
---|
| 1149 | |
---|
| 1150 | /* ------------------- triangular solution (mult) ------------------- */ |
---|
| 1151 | /* |
---|
| 1152 | * recompute equal solutions w.r.t. multiplicity |
---|
| 1153 | */ |
---|
| 1154 | static proc finalclear(list b) |
---|
| 1155 | { |
---|
| 1156 | list a = b; |
---|
| 1157 | list r; |
---|
| 1158 | int i,l,ju,j,k,ku,mu,c; |
---|
| 1159 | |
---|
| 1160 | // a[i] only |
---|
| 1161 | i = 1; |
---|
| 1162 | while (i<=size(a)) |
---|
| 1163 | { |
---|
| 1164 | ju = size(a[i][1]); |
---|
| 1165 | j = 1; |
---|
| 1166 | while (j<=ju) |
---|
| 1167 | { |
---|
| 1168 | mu = 1; |
---|
| 1169 | k = j+1; |
---|
| 1170 | while (k<=ju) |
---|
| 1171 | { |
---|
| 1172 | c = cmpn(a[i][1][j],a[i][1][k]); |
---|
| 1173 | if (c==0) |
---|
| 1174 | { |
---|
| 1175 | a[i][1] = delete(a[i][1],k); |
---|
| 1176 | ju--; |
---|
| 1177 | mu++; |
---|
| 1178 | } |
---|
| 1179 | else{k++;} |
---|
| 1180 | } |
---|
| 1181 | if (mu>1) |
---|
| 1182 | { |
---|
| 1183 | r[1] = a[i]; |
---|
| 1184 | r[1][1] = list(a[i][1][j]); |
---|
| 1185 | a[i][1] = delete(a[i][1],j); |
---|
| 1186 | a = addlist(r,a,mu); |
---|
| 1187 | ju--; |
---|
| 1188 | } |
---|
| 1189 | else{j++;} |
---|
| 1190 | } |
---|
| 1191 | if (ju==0){a = delete(a,i);} |
---|
| 1192 | else{i++;} |
---|
| 1193 | } |
---|
| 1194 | // a[i], a[l] |
---|
| 1195 | i = 1; |
---|
| 1196 | while (i<size(a)) |
---|
| 1197 | { |
---|
| 1198 | ju = size(a[i][1]); |
---|
| 1199 | l = i+1; |
---|
| 1200 | while (l<=size(a)) |
---|
| 1201 | { |
---|
| 1202 | ku = size(a[l][1]); |
---|
| 1203 | j = 1; |
---|
| 1204 | while (j<=ju) |
---|
| 1205 | { |
---|
| 1206 | mu = 0; |
---|
| 1207 | k = 1; |
---|
| 1208 | while (k<=ku) |
---|
| 1209 | { |
---|
| 1210 | c = cmpn(a[i][1][j],a[l][1][k]); |
---|
| 1211 | if (c==0) |
---|
| 1212 | { |
---|
| 1213 | mu = a[i][2]+a[l][2]; |
---|
| 1214 | r[1] = a[l]; |
---|
| 1215 | r[1][1] = list(a[l][1][k]); |
---|
| 1216 | r[1][2] = mu; |
---|
| 1217 | a[l][1] = delete(a[l][1],k); |
---|
| 1218 | a = addlist(r,a,1); |
---|
| 1219 | ku--; |
---|
| 1220 | break; |
---|
| 1221 | } |
---|
| 1222 | else{k++;} |
---|
| 1223 | } |
---|
| 1224 | if (mu) |
---|
| 1225 | { |
---|
| 1226 | a[i][1] = delete(a[i][1],j); |
---|
| 1227 | ju--; |
---|
| 1228 | } |
---|
| 1229 | else{j++;} |
---|
| 1230 | } |
---|
| 1231 | if (ku){l++;} |
---|
| 1232 | else |
---|
| 1233 | { |
---|
| 1234 | a = delete(a,l); |
---|
| 1235 | } |
---|
| 1236 | } |
---|
| 1237 | if (ju){i++;} |
---|
| 1238 | else |
---|
| 1239 | { |
---|
| 1240 | a = delete(a,i); |
---|
| 1241 | } |
---|
| 1242 | } |
---|
| 1243 | return(a); |
---|
| 1244 | } |
---|
| 1245 | |
---|
| 1246 | /* |
---|
| 1247 | * convert to list |
---|
| 1248 | */ |
---|
| 1249 | static proc splittolist(ideal sp) |
---|
| 1250 | { |
---|
| 1251 | int j = size(sp); |
---|
| 1252 | list spl = list(list(sp[j],j)); |
---|
| 1253 | |
---|
| 1254 | j--; |
---|
| 1255 | while (j>0) |
---|
| 1256 | { |
---|
| 1257 | if (deg(sp[j])) |
---|
| 1258 | { |
---|
| 1259 | spl = list(list(sp[j],j))+spl; |
---|
| 1260 | } |
---|
| 1261 | j--; |
---|
| 1262 | } |
---|
| 1263 | return(spl); |
---|
| 1264 | } |
---|
| 1265 | |
---|
| 1266 | /* |
---|
| 1267 | * multiply the multiplicity |
---|
| 1268 | */ |
---|
| 1269 | static proc multlist(list a, int m) |
---|
| 1270 | { |
---|
| 1271 | int i; |
---|
| 1272 | for (i=size(a);i>0;i--){a[i][2] = a[i][2]*m;} |
---|
| 1273 | return(a); |
---|
| 1274 | } |
---|
| 1275 | |
---|
| 1276 | /* |
---|
| 1277 | * a+b w.r.t. to multiplicity as ordering |
---|
| 1278 | * (programming like spolys) |
---|
| 1279 | */ |
---|
| 1280 | static proc addlist(list a, list b, int m) |
---|
| 1281 | { |
---|
| 1282 | int i,j,k,l,s; |
---|
| 1283 | list r = list(); |
---|
| 1284 | |
---|
| 1285 | if (m>1){a = multlist(a,m);} |
---|
| 1286 | k = size(a); |
---|
| 1287 | l = size(b); |
---|
| 1288 | i = 1; |
---|
| 1289 | j = 1; |
---|
| 1290 | while ((i<=k)&&(j<=l)) |
---|
| 1291 | { |
---|
| 1292 | s = a[i][2]-b[j][2]; |
---|
| 1293 | if (s>=0) |
---|
| 1294 | { |
---|
| 1295 | r = r+list(b[j]); |
---|
| 1296 | j++; |
---|
| 1297 | if (s==0) |
---|
| 1298 | { |
---|
| 1299 | s = size(r); |
---|
| 1300 | r[s][1] = r[s][1]+a[i][1]; |
---|
| 1301 | i++; |
---|
| 1302 | } |
---|
| 1303 | } |
---|
| 1304 | else |
---|
| 1305 | { |
---|
| 1306 | r = r+list(a[i]); |
---|
| 1307 | i++; |
---|
| 1308 | } |
---|
| 1309 | } |
---|
| 1310 | if (i>k) |
---|
| 1311 | { |
---|
| 1312 | if (j<=l){r = r+list(b[j..l]);} |
---|
| 1313 | } |
---|
| 1314 | else{r = r+list(a[i..k]);} |
---|
| 1315 | return(r); |
---|
| 1316 | } |
---|
| 1317 | |
---|
| 1318 | /* |
---|
| 1319 | * solution of one tridiagonal system T with multiplicity |
---|
| 1320 | * with precision prec |
---|
| 1321 | * T[1] is univariant in var(1) |
---|
| 1322 | * list o is empty for the first call |
---|
| 1323 | */ |
---|
| 1324 | static proc mutrisolve(list o, ideal T, int prec) |
---|
| 1325 | { |
---|
| 1326 | list lroots,ll,sp; |
---|
| 1327 | ideal S,h; |
---|
| 1328 | int i,d,m,z; |
---|
| 1329 | |
---|
| 1330 | d = size(T); |
---|
| 1331 | sp = splittolist(splitsqrfree(T[1],var(d))); |
---|
| 1332 | if (d==1){return(l_mutrisolve(sp,o,prec));} |
---|
| 1333 | z = size(sp); |
---|
| 1334 | while (z>0) |
---|
| 1335 | { |
---|
| 1336 | m = sp[z][2]; |
---|
| 1337 | ll = laguerre(sp[z][1],prec,1); |
---|
| 1338 | i = size(ll); |
---|
| 1339 | while(i>0) |
---|
| 1340 | { |
---|
| 1341 | h = linreduce(ideal(T[2..d]),d,ll[i]); |
---|
| 1342 | if (size(lroots)) |
---|
| 1343 | { |
---|
| 1344 | lroots = addlist(mutrisolve(list(ll[i])+o,h,prec),lroots,m); |
---|
| 1345 | } |
---|
| 1346 | else |
---|
| 1347 | { |
---|
| 1348 | lroots = mutrisolve(list(ll[i])+o,h,prec); |
---|
| 1349 | if (m>1){lroots=multlist(lroots,m);} |
---|
| 1350 | } |
---|
| 1351 | i--; |
---|
| 1352 | } |
---|
| 1353 | z--; |
---|
| 1354 | } |
---|
| 1355 | return(lroots); |
---|
| 1356 | } |
---|
| 1357 | |
---|
| 1358 | /* |
---|
| 1359 | * the last call, we are ready |
---|
| 1360 | */ |
---|
| 1361 | static proc l_mutrisolve(list sp, list o, int prec) |
---|
| 1362 | { |
---|
| 1363 | list lroots,ll; |
---|
| 1364 | int z,m,i; |
---|
| 1365 | |
---|
| 1366 | z = size(sp); |
---|
| 1367 | while (z>0) |
---|
| 1368 | { |
---|
| 1369 | m = sp[z][2]; |
---|
| 1370 | ll = laguerre(sp[z][1],prec,1); |
---|
| 1371 | for (i=size(ll);i>0;i--){ll[i] = list(ll[i])+o;} |
---|
| 1372 | if (size(lroots)) |
---|
| 1373 | { |
---|
| 1374 | lroots = addlist(list(list(ll,m)),lroots,1); |
---|
| 1375 | } |
---|
| 1376 | else |
---|
| 1377 | { |
---|
| 1378 | lroots = list(list(ll,m)); |
---|
| 1379 | } |
---|
| 1380 | z--; |
---|
| 1381 | } |
---|
| 1382 | return(lroots); |
---|
| 1383 | } |
---|
| 1384 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 1385 | |
---|
| 1386 | proc ures_solve( ideal gls, list # ) |
---|
| 1387 | "USAGE: ures_solve(i [, k, p] ); i = ideal, k, p = integers |
---|
[837fed] | 1388 | k=0: use sparse resultant matrix of Gelfand, Kapranov and Zelevinsky, @* |
---|
[3c4dcc] | 1389 | k=1: use resultant matrix of Macaulay which works only for |
---|
[837fed] | 1390 | homogeneous ideals,@* |
---|
[fb86b6b] | 1391 | p>0: defines precision of the long floats for internal computation |
---|
| 1392 | if the basering is not complex (in decimal digits), |
---|
| 1393 | (default: k=0, p=30) |
---|
[837fed] | 1394 | ASSUME: i is a zerodimensional ideal given by a quadratic system, that is,@* |
---|
| 1395 | nvars(basering) = ncols(i) = number of vars actually occurring in i, |
---|
| 1396 | RETURN: If the ground field is the field of complex numbers: list of numbers |
---|
| 1397 | (the complex roots of the polynomial system i=0). @* |
---|
[3c4dcc] | 1398 | Otherwise: ring @code{R} with the same number of variables but with |
---|
| 1399 | complex coefficients (and precision p). @code{R} comes with a list |
---|
| 1400 | @code{SOL} of numbers, in which complex roots of the polynomial |
---|
[837fed] | 1401 | system i are stored: @* |
---|
[8b96123] | 1402 | EXAMPLE: example ures_solve; shows an example |
---|
| 1403 | " |
---|
| 1404 | { |
---|
[2a4de84] | 1405 | int typ=0;// defaults |
---|
| 1406 | int prec=30; |
---|
[8b96123] | 1407 | |
---|
[2a4de84] | 1408 | if ( size(#) > 0 ) |
---|
| 1409 | { |
---|
| 1410 | typ= #[1]; |
---|
| 1411 | if ( typ < 0 || typ > 1 ) |
---|
[8b96123] | 1412 | { |
---|
[2a4de84] | 1413 | ERROR("Valid values for second parameter k are: |
---|
[8b96123] | 1414 | 0: use sparse Resultant (default) |
---|
| 1415 | 1: use Macaulay Resultant"); |
---|
| 1416 | } |
---|
[2a4de84] | 1417 | } |
---|
| 1418 | if ( size(#) > 1 ) |
---|
| 1419 | { |
---|
| 1420 | prec= #[2]; |
---|
| 1421 | if ( prec < 8 ) |
---|
[8b96123] | 1422 | { |
---|
[2a4de84] | 1423 | prec = 8; |
---|
[8b96123] | 1424 | } |
---|
[2a4de84] | 1425 | } |
---|
[8b96123] | 1426 | |
---|
[2a4de84] | 1427 | list LL=uressolve(gls,typ,prec,1); |
---|
| 1428 | int sizeLL=size(LL); |
---|
| 1429 | if (sizeLL==0) |
---|
| 1430 | { |
---|
| 1431 | dbprint(printlevel-voice+3,"No solution found!"); |
---|
| 1432 | return(list()); |
---|
| 1433 | } |
---|
| 1434 | if (typeof(LL[1][1])=="string") |
---|
| 1435 | { |
---|
| 1436 | int ii,jj; |
---|
| 1437 | int nv=size(LL[1]); |
---|
| 1438 | execute("ring rinC =(complex,"+string(prec)+",I),(" |
---|
[3c4dcc] | 1439 | +varstr(basering)+"),lp;"); |
---|
[2a4de84] | 1440 | list SOL,SOLnew; |
---|
| 1441 | for (ii=1; ii<=sizeLL; ii++) |
---|
| 1442 | { |
---|
| 1443 | SOLnew=list(); |
---|
| 1444 | for (jj=1; jj<=nv; jj++) |
---|
| 1445 | { |
---|
| 1446 | execute("SOLnew["+string(jj)+"]="+LL[ii][jj]+";"); |
---|
[837fed] | 1447 | } |
---|
[2a4de84] | 1448 | SOL[ii]=SOLnew; |
---|
| 1449 | } |
---|
| 1450 | kill SOLnew; |
---|
| 1451 | export SOL; |
---|
| 1452 | dbprint( printlevel-voice+3," |
---|
[3c4dcc] | 1453 | // 'ures_solve' created a ring, in which a list SOL of numbers (the complex |
---|
| 1454 | // solutions) is stored. |
---|
| 1455 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 1456 | // to the return value): |
---|
[837fed] | 1457 | setring R; SOL; "); |
---|
[2a4de84] | 1458 | return(rinC); |
---|
| 1459 | } |
---|
| 1460 | else |
---|
| 1461 | { |
---|
| 1462 | return(LL); |
---|
| 1463 | } |
---|
[8b96123] | 1464 | } |
---|
| 1465 | example |
---|
| 1466 | { |
---|
| 1467 | "EXAMPLE:";echo=2; |
---|
| 1468 | // compute the intersection points of two curves |
---|
| 1469 | ring rsq = 0,(x,y),lp; |
---|
| 1470 | ideal gls= x2 + y2 - 10, x2 + xy + 2y2 - 16; |
---|
[837fed] | 1471 | def R=ures_solve(gls,0,16); |
---|
| 1472 | setring R; SOL; |
---|
[8b96123] | 1473 | } |
---|
| 1474 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 1475 | |
---|
[0d69875] | 1476 | proc mp_res_mat( ideal i, list # ) |
---|
[0ba413] | 1477 | "USAGE: mp_res_mat(i [, k] ); i ideal, k integer, |
---|
[0bc582c] | 1478 | k=0: sparse resultant matrix of Gelfand, Kapranov and Zelevinsky,@* |
---|
[fb86b6b] | 1479 | k=1: resultant matrix of Macaulay (k=0 is default) |
---|
[3c4dcc] | 1480 | ASSUME: The number of elements in the input system must be the number of |
---|
[fb86b6b] | 1481 | variables in the basering plus one; |
---|
| 1482 | if k=1 then i must be homogeneous. |
---|
[917fb5] | 1483 | RETURN: module representing the multipolynomial resultant matrix |
---|
[0f5091] | 1484 | EXAMPLE: example mp_res_mat; shows an example |
---|
| 1485 | " |
---|
| 1486 | { |
---|
[2a4de84] | 1487 | int typ=0; |
---|
[0f5091] | 1488 | |
---|
[2a4de84] | 1489 | if ( size(#) > 0 ) |
---|
| 1490 | { |
---|
| 1491 | typ= #[1]; |
---|
| 1492 | if ( typ < 0 || typ > 1 ) |
---|
[0d69875] | 1493 | { |
---|
[2a4de84] | 1494 | ERROR("Valid values for third parameter are: |
---|
[5c67581] | 1495 | 0: sparse resultant (default) |
---|
| 1496 | 1: Macaulay resultant"); |
---|
| 1497 | } |
---|
[2a4de84] | 1498 | } |
---|
| 1499 | return(mpresmat(i,typ)); |
---|
[0f5091] | 1500 | } |
---|
| 1501 | example |
---|
| 1502 | { |
---|
[5c67581] | 1503 | "EXAMPLE:";echo=2; |
---|
| 1504 | // compute resultant matrix in ring with parameters (sparse resultant matrix) |
---|
| 1505 | ring rsq= (0,u0,u1,u2),(x1,x2),lp; |
---|
| 1506 | ideal i= u0+u1*x1+u2*x2,x1^2 + x2^2 - 10,x1^2 + x1*x2 + 2*x2^2 - 16; |
---|
| 1507 | module m = mp_res_mat(i); |
---|
| 1508 | print(m); |
---|
| 1509 | // computing sparse resultant |
---|
| 1510 | det(m); |
---|
| 1511 | |
---|
| 1512 | // compute resultant matrix (Macaulay resultant matrix) |
---|
| 1513 | ring rdq= (0,u0,u1,u2),(x0,x1,x2),lp; |
---|
| 1514 | ideal h= homog(imap(rsq,i),x0); |
---|
| 1515 | h; |
---|
[b9b906] | 1516 | |
---|
[5c67581] | 1517 | module m = mp_res_mat(h,1); |
---|
| 1518 | print(m); |
---|
| 1519 | // computing Macaulay resultant (should be the same as above!) |
---|
| 1520 | det(m); |
---|
| 1521 | |
---|
| 1522 | // compute numerical sparse resultant matrix |
---|
| 1523 | setring rsq; |
---|
| 1524 | ideal ir= 15+2*x1+5*x2,x1^2 + x2^2 - 10,x1^2 + x1*x2 + 2*x2^2 - 16; |
---|
| 1525 | module mn = mp_res_mat(ir); |
---|
| 1526 | print(mn); |
---|
| 1527 | // computing sparse resultant |
---|
| 1528 | det(mn); |
---|
[0f5091] | 1529 | } |
---|
| 1530 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 1531 | |
---|
[0d69875] | 1532 | proc interpolate( ideal p, ideal w, int d ) |
---|
[0ba413] | 1533 | "USAGE: interpolate(p,v,d); p,v=ideals of numbers, d=integer |
---|
[3c4dcc] | 1534 | ASSUME: Ground field K is the field of rational numbers, p and v are lists |
---|
| 1535 | of elements of the ground field K with p[j] != -1,0,1, size(p) = n |
---|
[fb86b6b] | 1536 | (= number of vars) and size(v)=N=(d+1)^n. |
---|
[3c4dcc] | 1537 | RETURN: poly f, the unique polynomial f of degree n*d with prescribed values |
---|
[fb86b6b] | 1538 | v[i] at the points p(i)=(p[1]^(i-1),..,p[n]^(i-1)), i=1,..,N. |
---|
[3c4dcc] | 1539 | NOTE: mainly useful when n=1, i.e. f is satisfying f(p^(i-1)) = v[i], |
---|
[fb86b6b] | 1540 | i=1..d+1. |
---|
| 1541 | SEE ALSO: vandermonde. |
---|
[0f5091] | 1542 | EXAMPLE: example interpolate; shows an example |
---|
| 1543 | " |
---|
| 1544 | { |
---|
[5c67581] | 1545 | return(vandermonde(p,w,d)); |
---|
| 1546 | } |
---|
| 1547 | example |
---|
| 1548 | { |
---|
| 1549 | "EXAMPLE:"; echo=2; |
---|
| 1550 | ring r1 = 0,(x),lp; |
---|
| 1551 | // determine f with deg(f) = 4 and |
---|
| 1552 | // v = values of f at points 3^0, 3^1, 3^2, 3^3, 3^4 |
---|
| 1553 | ideal v=16,0,11376,1046880,85949136; |
---|
| 1554 | interpolate( 3, v, 4 ); |
---|
| 1555 | } |
---|
| 1556 | |
---|
[6ed15c] | 1557 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 1558 | // changed for Singular 3 |
---|
| 1559 | // Return value is now a list: (rlist, rn@) |
---|
[0ba413] | 1560 | static proc psubst( int d, int dd, int n, list resl, |
---|
[6ed15c] | 1561 | ideal fi, int elem, int nv, int prec, int rn@, list rlist) |
---|
[5c67581] | 1562 | { |
---|
| 1563 | // nv: number of ring variables (fixed value) |
---|
| 1564 | // elem: number of elements in ideal fi (fixed value) |
---|
| 1565 | // fi: input ideal (fixed value) |
---|
| 1566 | // rl: output list of roots |
---|
[0ba413] | 1567 | // resl: actual list of roots |
---|
[b9b906] | 1568 | // n: |
---|
[5c67581] | 1569 | // dd: actual element of fi |
---|
[b9b906] | 1570 | // d: actual variable |
---|
[5c67581] | 1571 | |
---|
[6ed15c] | 1572 | list LL; |
---|
| 1573 | int pdebug; |
---|
[5c67581] | 1574 | int olddd=dd; |
---|
| 1575 | |
---|
[6ed15c] | 1576 | dbprint(printlevel-voice+2, "// 0 step "+string(dd)+" of "+string(elem) ); |
---|
[b9b906] | 1577 | |
---|
| 1578 | if ( dd <= elem ) |
---|
[5c67581] | 1579 | { |
---|
[cb8d1d6] | 1580 | int loop = 1; |
---|
| 1581 | int k; |
---|
| 1582 | list lsr,lh; |
---|
| 1583 | poly ps; |
---|
| 1584 | int thedd; |
---|
| 1585 | |
---|
[6ed15c] | 1586 | dbprint( printlevel-voice+1,"// 1 dd = "+string(dd) ); |
---|
[cb8d1d6] | 1587 | |
---|
| 1588 | thedd=0; |
---|
| 1589 | while ( (dd+1 <= elem) && loop ) |
---|
| 1590 | { |
---|
| 1591 | ps= fi[dd+1]; |
---|
| 1592 | |
---|
| 1593 | if ( n-1 > 0 ) |
---|
| 1594 | { |
---|
[6ed15c] | 1595 | dbprint( printlevel-voice, |
---|
[cb8d1d6] | 1596 | "// 2 ps=fi["+string(dd+1)+"]"+" size=" |
---|
| 1597 | +string(size(coeffs(ps,var(n-1)))) |
---|
[6ed15c] | 1598 | +" leadexp(ps)="+string(leadexp(ps)) ); |
---|
| 1599 | |
---|
[cb8d1d6] | 1600 | if ( size(coeffs(ps,var(n-1))) == 1 ) |
---|
| 1601 | { |
---|
| 1602 | dd++; |
---|
[0ba413] | 1603 | // hier Leading-Exponent pruefen??? |
---|
[3754ca] | 1604 | // oder ist das Polynom immer als letztes in der Liste?!? |
---|
[cb8d1d6] | 1605 | // leadexp(ps) |
---|
| 1606 | } |
---|
| 1607 | else |
---|
| 1608 | { |
---|
| 1609 | loop=0; |
---|
| 1610 | } |
---|
| 1611 | } |
---|
| 1612 | else |
---|
| 1613 | { |
---|
[6ed15c] | 1614 | dbprint( printlevel-voice, |
---|
[cb8d1d6] | 1615 | "// 2 ps=fi["+string(dd+1)+"]"+" leadexp(ps)=" |
---|
[6ed15c] | 1616 | +string(leadexp(ps)) ); |
---|
[cb8d1d6] | 1617 | dd++; |
---|
| 1618 | } |
---|
| 1619 | } |
---|
| 1620 | thedd=dd; |
---|
| 1621 | ps= fi[thedd]; |
---|
| 1622 | |
---|
[6ed15c] | 1623 | dbprint( printlevel-voice+1, |
---|
[cb8d1d6] | 1624 | "// 3 fi["+string(thedd-1)+"]"+" leadexp(fi[thedd-1])=" |
---|
[6ed15c] | 1625 | +string(leadexp(fi[thedd-1])) ); |
---|
| 1626 | dbprint( printlevel-voice+1, |
---|
[cb8d1d6] | 1627 | "// 3 ps=fi["+string(thedd)+"]"+" leadexp(ps)=" |
---|
[6ed15c] | 1628 | +string(leadexp(ps)) ); |
---|
[cb8d1d6] | 1629 | |
---|
| 1630 | for ( k= nv; k > nv-d; k-- ) |
---|
| 1631 | { |
---|
[6ed15c] | 1632 | dbprint( printlevel-voice, |
---|
[cb8d1d6] | 1633 | "// 4 subst(fi["+string(thedd)+"]," |
---|
[6ed15c] | 1634 | +string(var(k))+","+string(resl[k])+");" ); |
---|
[0ba413] | 1635 | ps = subst(ps,var(k),resl[k]); |
---|
[cb8d1d6] | 1636 | } |
---|
| 1637 | |
---|
[3c4dcc] | 1638 | dbprint( printlevel-voice, "// 5 substituted ps="+string(ps) ); |
---|
[cb8d1d6] | 1639 | |
---|
| 1640 | if ( ps != 0 ) |
---|
| 1641 | { |
---|
[8b96123] | 1642 | lsr= laguerre_solve( ps, prec, prec, 0 ); |
---|
[cb8d1d6] | 1643 | } |
---|
| 1644 | else |
---|
| 1645 | { |
---|
[6ed15c] | 1646 | dbprint( printlevel-voice+1,"// 30 ps == 0, thats not cool..."); |
---|
[3c4dcc] | 1647 | lsr=list(number(0)); |
---|
[cb8d1d6] | 1648 | } |
---|
| 1649 | |
---|
[3c4dcc] | 1650 | dbprint( printlevel-voice+1, |
---|
| 1651 | "// 6 laguerre_solve found roots: lsr["+string(size(lsr))+"]" ); |
---|
[cb8d1d6] | 1652 | |
---|
| 1653 | if ( size(lsr) > 1 ) |
---|
| 1654 | { |
---|
[3c4dcc] | 1655 | dbprint( printlevel-voice+1, |
---|
[cb8d1d6] | 1656 | "// 10 checking roots found before, range " |
---|
[6ed15c] | 1657 | +string(dd-olddd)+" -- "+string(dd) ); |
---|
[3c4dcc] | 1658 | dbprint( printlevel-voice+1, |
---|
[6ed15c] | 1659 | "// 10 thedd = "+string(thedd) ); |
---|
[cb8d1d6] | 1660 | |
---|
| 1661 | int i,j,l; |
---|
| 1662 | int ls=size(lsr); |
---|
| 1663 | int lss; |
---|
| 1664 | poly pss; |
---|
| 1665 | list nares; |
---|
| 1666 | int rroot; |
---|
| 1667 | int nares_size; |
---|
| 1668 | |
---|
| 1669 | |
---|
| 1670 | for ( i = 1; i <= ls; i++ ) // lsr[1..ls] |
---|
| 1671 | { |
---|
| 1672 | rroot=1; |
---|
| 1673 | |
---|
| 1674 | if ( pdebug>=2 ) |
---|
| 1675 | {"// 13 root lsr["+string(i)+"] = "+string(lsr[i]);} |
---|
| 1676 | for ( l = 0; l <= dd-olddd; l++ ) |
---|
| 1677 | { |
---|
| 1678 | if ( l+olddd != thedd ) |
---|
| 1679 | { |
---|
| 1680 | if ( pdebug>=2 ) |
---|
| 1681 | {"// 11 checking ideal element "+string(l+olddd);} |
---|
| 1682 | ps=fi[l+olddd]; |
---|
| 1683 | if ( pdebug>=3 ) |
---|
| 1684 | {"// 14 ps=fi["+string(l+olddd)+"]";} |
---|
| 1685 | for ( k= nv; k > nv-d; k-- ) |
---|
| 1686 | { |
---|
| 1687 | if ( pdebug>=3 ) |
---|
| 1688 | { |
---|
| 1689 | "// 11 subst(fi["+string(olddd+l)+"]," |
---|
[0ba413] | 1690 | +string(var(k))+","+string(resl[k])+");"; |
---|
[cb8d1d6] | 1691 | } |
---|
[0ba413] | 1692 | ps = subst(ps,var(k),resl[k]); |
---|
[cb8d1d6] | 1693 | |
---|
| 1694 | } |
---|
| 1695 | |
---|
| 1696 | pss=subst(ps,var(k),lsr[i]); // k=nv-d |
---|
| 1697 | if ( pdebug>=3 ) |
---|
| 1698 | { "// 15 0 == "+string(pss); } |
---|
| 1699 | if ( pss != 0 ) |
---|
| 1700 | { |
---|
| 1701 | if ( system("complexNearZero", |
---|
| 1702 | leadcoef(pss), |
---|
[0ba413] | 1703 | prec) ) |
---|
[cb8d1d6] | 1704 | { |
---|
| 1705 | if ( pdebug>=2 ) |
---|
| 1706 | { "// 16 root "+string(i)+" is a real root"; } |
---|
| 1707 | } |
---|
| 1708 | else |
---|
| 1709 | { |
---|
| 1710 | if ( pdebug>=2 ) |
---|
| 1711 | { "// 17 0 == "+string(pss); } |
---|
| 1712 | rroot=0; |
---|
| 1713 | } |
---|
| 1714 | } |
---|
| 1715 | |
---|
| 1716 | } |
---|
| 1717 | } |
---|
| 1718 | |
---|
| 1719 | if ( rroot == 1 ) // add root to list ? |
---|
| 1720 | { |
---|
| 1721 | if ( size(nares) > 0 ) |
---|
| 1722 | { |
---|
| 1723 | nares=nares[1..size(nares)],lsr[i]; |
---|
| 1724 | } |
---|
| 1725 | else |
---|
| 1726 | { |
---|
| 1727 | nares=lsr[i]; |
---|
| 1728 | } |
---|
| 1729 | if ( pdebug>=2 ) |
---|
| 1730 | { "// 18 added root to list nares"; } |
---|
| 1731 | } |
---|
| 1732 | } |
---|
| 1733 | |
---|
| 1734 | nares_size=size(nares); |
---|
| 1735 | if ( nares_size == 0 ) |
---|
| 1736 | { |
---|
| 1737 | "Numerical problem: No root found..."; |
---|
| 1738 | "Output may be incorrect!"; |
---|
[6ed15c] | 1739 | nares=list(number(0)); |
---|
[cb8d1d6] | 1740 | } |
---|
| 1741 | |
---|
| 1742 | if ( pdebug>=1 ) |
---|
| 1743 | { "// 20 found <"+string(size(nares))+"> roots"; } |
---|
| 1744 | |
---|
| 1745 | for ( i= 1; i <= nares_size; i++ ) |
---|
| 1746 | { |
---|
[0ba413] | 1747 | resl[nv-d]= nares[i]; |
---|
[cb8d1d6] | 1748 | |
---|
| 1749 | if ( dd < elem ) |
---|
| 1750 | { |
---|
| 1751 | if ( i > 1 ) |
---|
| 1752 | { |
---|
[3c4dcc] | 1753 | rn@++; |
---|
[cb8d1d6] | 1754 | } |
---|
[3c4dcc] | 1755 | LL = psubst( d+1, dd+1, n-1, resl, fi, elem, nv, prec, |
---|
[6ed15c] | 1756 | rn@, rlist ); |
---|
| 1757 | rlist = LL[1]; |
---|
| 1758 | rn@ = LL[2]; |
---|
[cb8d1d6] | 1759 | } |
---|
| 1760 | else |
---|
| 1761 | { |
---|
[6ed15c] | 1762 | if ( i > 1 ) { rn@++; } //bug found by O.Labs |
---|
| 1763 | if ( pdebug>=1 ) |
---|
| 1764 | {"// 30_1 <"+string(rn@)+"> "+string(size(resl))+" <-----";} |
---|
| 1765 | if ( pdebug>=2 ){ resl; } |
---|
| 1766 | rlist[rn@]=resl; |
---|
[cb8d1d6] | 1767 | } |
---|
| 1768 | } |
---|
| 1769 | } |
---|
| 1770 | else |
---|
| 1771 | { |
---|
| 1772 | if ( pdebug>=2 ) |
---|
| 1773 | { "// 21 found root to be: "+string(lsr[1]); } |
---|
[0ba413] | 1774 | resl[nv-d]= lsr[1]; |
---|
[cb8d1d6] | 1775 | |
---|
| 1776 | if ( dd < elem ) |
---|
| 1777 | { |
---|
[3c4dcc] | 1778 | LL= psubst( d+1, dd+1, n-1, resl, fi, elem, nv, prec, |
---|
[6ed15c] | 1779 | rn@, rlist ); |
---|
| 1780 | rlist = LL[1]; |
---|
| 1781 | rn@ = LL[2]; |
---|
[cb8d1d6] | 1782 | } |
---|
| 1783 | else |
---|
| 1784 | { |
---|
| 1785 | if ( pdebug>=1 ) |
---|
[0ba413] | 1786 | { "// 30_2 <"+string(rn@)+"> "+string(size(resl))+" <-----";} |
---|
[cb8d1d6] | 1787 | if ( pdebug>=2 ) |
---|
[0ba413] | 1788 | { resl; } |
---|
| 1789 | rlist[rn@]=resl; |
---|
[cb8d1d6] | 1790 | } |
---|
| 1791 | } |
---|
[5c67581] | 1792 | } |
---|
[6ed15c] | 1793 | return(list(rlist,rn@)); |
---|
[5c67581] | 1794 | } |
---|
| 1795 | |
---|
| 1796 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 1797 | |
---|
| 1798 | proc fglm_solve( ideal fi, list # ) |
---|
[fb86b6b] | 1799 | "USAGE: fglm_solve(i [, p] ); i ideal, p integer |
---|
[9173792] | 1800 | ASSUME: the ground field has char 0. |
---|
[3c4dcc] | 1801 | RETURN: ring @code{R} with the same number of variables but with complex |
---|
| 1802 | coefficients (and precision p). @code{R} comes with a list |
---|
[6ed15c] | 1803 | @code{rlist} of numbers, in which the complex roots of i are stored.@* |
---|
[3c4dcc] | 1804 | p>0: gives precision of complex numbers in decimal digits [default: |
---|
| 1805 | p=30]. |
---|
| 1806 | NOTE: The procedure uses a standard basis of i to determine all complex |
---|
[0ba413] | 1807 | roots of i. |
---|
[5c67581] | 1808 | EXAMPLE: example fglm_solve; shows an example |
---|
| 1809 | " |
---|
| 1810 | { |
---|
| 1811 | int prec=30; |
---|
| 1812 | |
---|
| 1813 | if ( size(#)>=1 && typeof(#[1])=="int") |
---|
| 1814 | { |
---|
[cb8d1d6] | 1815 | prec=#[1]; |
---|
[5c67581] | 1816 | } |
---|
| 1817 | |
---|
[6ed15c] | 1818 | def R = lex_solve(stdfglm(fi),prec); |
---|
| 1819 | dbprint( printlevel-voice+3," |
---|
| 1820 | // 'fglm_solve' created a ring, in which a list rlist of numbers (the |
---|
[3c4dcc] | 1821 | // complex solutions) is stored. |
---|
| 1822 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 1823 | // to the return value): |
---|
[6ed15c] | 1824 | setring R; rlist; "); |
---|
| 1825 | return(R); |
---|
[5c67581] | 1826 | } |
---|
| 1827 | example |
---|
| 1828 | { |
---|
| 1829 | "EXAMPLE:";echo=2; |
---|
| 1830 | ring r = 0,(x,y),lp; |
---|
| 1831 | // compute the intersection points of two curves |
---|
[6ed15c] | 1832 | ideal s = x2 + y2 - 10, x2 + xy + 2y2 - 16; |
---|
| 1833 | def R = fglm_solve(s,10); |
---|
| 1834 | setring R; rlist; |
---|
[5c67581] | 1835 | } |
---|
| 1836 | |
---|
| 1837 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 1838 | |
---|
[0ba413] | 1839 | proc lex_solve( ideal fi, list # ) |
---|
| 1840 | "USAGE: lex_solve( i[,p] ); i=ideal, p=integer, |
---|
[fb86b6b] | 1841 | p>0: gives precision of complex numbers in decimal digits (default: p=30). |
---|
[3c4dcc] | 1842 | ASSUME: i is a reduced lexicographical Groebner bases of a zero-dimensional |
---|
[fb86b6b] | 1843 | ideal, sorted by increasing leading terms. |
---|
[3c4dcc] | 1844 | RETURN: ring @code{R} with the same number of variables but with complex |
---|
| 1845 | coefficients (and precision p). @code{R} comes with a list |
---|
[6ed15c] | 1846 | @code{rlist} of numbers, in which the complex roots of i are stored. |
---|
[5c67581] | 1847 | EXAMPLE: example lex_solve; shows an example |
---|
| 1848 | " |
---|
| 1849 | { |
---|
[0ba413] | 1850 | int prec=30; |
---|
[6ed15c] | 1851 | list LL; |
---|
[0ba413] | 1852 | |
---|
| 1853 | if ( size(#)>=1 && typeof(#[1])=="int") |
---|
| 1854 | { |
---|
| 1855 | prec=#[1]; |
---|
| 1856 | } |
---|
| 1857 | |
---|
[6ed15c] | 1858 | if ( !defined(pdebug) ) { int pdebug; } |
---|
[5c67581] | 1859 | def oring= basering; |
---|
| 1860 | |
---|
[b9b906] | 1861 | // change the ground field to complex numbers |
---|
[6ed15c] | 1862 | string nrings= "ring RC =(complex,"+string(prec) |
---|
[0ba413] | 1863 | +"),("+varstr(basering)+"),lp;"; |
---|
[38c165] | 1864 | execute(nrings); |
---|
[5c67581] | 1865 | |
---|
| 1866 | // map fi from old to new ring |
---|
| 1867 | ideal fi= imap(oring,fi); |
---|
| 1868 | |
---|
| 1869 | int idelem= size(fi); |
---|
| 1870 | int nv= nvars(basering); |
---|
| 1871 | int i,j,k,lis; |
---|
[0ba413] | 1872 | list resl,li; |
---|
[5c67581] | 1873 | |
---|
| 1874 | if ( !defined(rlist) ) |
---|
| 1875 | { |
---|
[cb8d1d6] | 1876 | list rlist; |
---|
| 1877 | export rlist; |
---|
[5c67581] | 1878 | } |
---|
[b9b906] | 1879 | |
---|
[8b96123] | 1880 | li= laguerre_solve(fi[1],prec,prec,0); |
---|
[5c67581] | 1881 | lis= size(li); |
---|
| 1882 | |
---|
[6ed15c] | 1883 | dbprint(printlevel-voice+2,"// laguerre found roots: "+string(size(li))); |
---|
| 1884 | int rn@; |
---|
[b9b906] | 1885 | |
---|
[5c67581] | 1886 | for ( j= 1; j <= lis; j++ ) |
---|
| 1887 | { |
---|
[6ed15c] | 1888 | dbprint(printlevel-voice+1,"// root "+string(j) ); |
---|
[cb8d1d6] | 1889 | rn@++; |
---|
[0ba413] | 1890 | resl[nv]= li[j]; |
---|
[6ed15c] | 1891 | LL = psubst( 1, 2, nv-1, resl, fi, idelem, nv, prec, rn@, rlist ); |
---|
| 1892 | rlist=LL[1]; |
---|
| 1893 | rn@=LL[2]; |
---|
[5c67581] | 1894 | } |
---|
[b9b906] | 1895 | |
---|
[6ed15c] | 1896 | dbprint( printlevel-voice+3," |
---|
| 1897 | // 'lex_solve' created a ring, in which a list rlist of numbers (the |
---|
[3c4dcc] | 1898 | // complex solutions) is stored. |
---|
| 1899 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 1900 | // to the return value): |
---|
[6ed15c] | 1901 | setring R; rlist; "); |
---|
[5c67581] | 1902 | |
---|
[6ed15c] | 1903 | return(RC); |
---|
[5c67581] | 1904 | } |
---|
| 1905 | example |
---|
| 1906 | { |
---|
| 1907 | "EXAMPLE:";echo=2; |
---|
| 1908 | ring r = 0,(x,y),lp; |
---|
| 1909 | // compute the intersection points of two curves |
---|
[6ed15c] | 1910 | ideal s = x2 + y2 - 10, x2 + xy + 2y2 - 16; |
---|
| 1911 | def R = lex_solve(stdfglm(s),10); |
---|
| 1912 | setring R; rlist; |
---|
[5c67581] | 1913 | } |
---|
| 1914 | |
---|
| 1915 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 1916 | |
---|
| 1917 | proc triangLf_solve( ideal fi, list # ) |
---|
[9173792] | 1918 | "USAGE: triangLf_solve(i [, p] ); i ideal, p integer, |
---|
[fb86b6b] | 1919 | p>0: gives precision of complex numbers in digits (default: p=30). |
---|
| 1920 | ASSUME: the ground field has char 0; i is a zero-dimensional ideal |
---|
[3c4dcc] | 1921 | RETURN: ring @code{R} with the same number of variables but with complex |
---|
| 1922 | coefficients (and precision p). @code{R} comes with a list |
---|
[6ed15c] | 1923 | @code{rlist} of numbers, in which the complex roots of i are stored. |
---|
[dd73043] | 1924 | NOTE: The procedure uses a triangular system (Lazard's Algorithm with |
---|
[3c4dcc] | 1925 | factorization) computed from a standard basis to determine |
---|
| 1926 | recursively all complex roots of the input ideal i with Laguerre's |
---|
| 1927 | algorithm. |
---|
[5c67581] | 1928 | EXAMPLE: example triangLf_solve; shows an example |
---|
| 1929 | " |
---|
| 1930 | { |
---|
| 1931 | int prec=30; |
---|
| 1932 | |
---|
| 1933 | if ( size(#)>=1 && typeof(#[1])=="int") |
---|
| 1934 | { |
---|
[cb8d1d6] | 1935 | prec=#[1]; |
---|
[5c67581] | 1936 | } |
---|
| 1937 | |
---|
[6ed15c] | 1938 | def R=triang_solve(triangLfak(stdfglm(fi)),prec); |
---|
| 1939 | dbprint( printlevel-voice+3," |
---|
| 1940 | // 'triangLf_solve' created a ring, in which a list rlist of numbers (the |
---|
[3c4dcc] | 1941 | // complex solutions) is stored. |
---|
| 1942 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 1943 | // to the return value): |
---|
[6ed15c] | 1944 | setring R; rlist; "); |
---|
| 1945 | return(R); |
---|
[5c67581] | 1946 | } |
---|
| 1947 | example |
---|
| 1948 | { |
---|
| 1949 | "EXAMPLE:";echo=2; |
---|
| 1950 | ring r = 0,(x,y),lp; |
---|
| 1951 | // compute the intersection points of two curves |
---|
[6ed15c] | 1952 | ideal s = x2 + y2 - 10, x2 + xy + 2y2 - 16; |
---|
| 1953 | def R = triangLf_solve(s,10); |
---|
| 1954 | setring R; rlist; |
---|
[5c67581] | 1955 | } |
---|
| 1956 | |
---|
| 1957 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 1958 | |
---|
| 1959 | proc triangM_solve( ideal fi, list # ) |
---|
[9173792] | 1960 | "USAGE: triangM_solve(i [, p ] ); i=ideal, p=integer, |
---|
[fb86b6b] | 1961 | p>0: gives precision of complex numbers in digits (default: p=30). |
---|
[9173792] | 1962 | ASSUME: the ground field has char 0;@* |
---|
[5c67581] | 1963 | i zero-dimensional ideal |
---|
[3c4dcc] | 1964 | RETURN: ring @code{R} with the same number of variables but with complex |
---|
| 1965 | coefficients (and precision p). @code{R} comes with a list |
---|
[6ed15c] | 1966 | @code{rlist} of numbers, in which the complex roots of i are stored. |
---|
[dd73043] | 1967 | NOTE: The procedure uses a triangular system (Moellers Algorithm) computed |
---|
| 1968 | from a standard basis of input ideal i to determine recursively all |
---|
| 1969 | complex roots with Laguerre's algorithm. |
---|
[5c67581] | 1970 | EXAMPLE: example triangM_solve; shows an example |
---|
| 1971 | " |
---|
| 1972 | { |
---|
| 1973 | int prec=30; |
---|
| 1974 | |
---|
| 1975 | if ( size(#)>=1 && typeof(#[1])=="int") |
---|
| 1976 | { |
---|
[cb8d1d6] | 1977 | prec=#[1]; |
---|
[5c67581] | 1978 | } |
---|
| 1979 | |
---|
[6ed15c] | 1980 | def R = triang_solve(triangM(stdfglm(fi)),prec); |
---|
| 1981 | dbprint( printlevel-voice+3," |
---|
| 1982 | // 'triangM_solve' created a ring, in which a list rlist of numbers (the |
---|
[3c4dcc] | 1983 | // complex solutions) is stored. |
---|
| 1984 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 1985 | // to the return value): |
---|
[6ed15c] | 1986 | setring R; rlist; "); |
---|
| 1987 | return(R); |
---|
[5c67581] | 1988 | } |
---|
| 1989 | example |
---|
| 1990 | { |
---|
| 1991 | "EXAMPLE:";echo=2; |
---|
| 1992 | ring r = 0,(x,y),lp; |
---|
| 1993 | // compute the intersection points of two curves |
---|
[6ed15c] | 1994 | ideal s = x2 + y2 - 10, x2 + xy + 2y2 - 16; |
---|
| 1995 | def R = triangM_solve(s,10); |
---|
| 1996 | setring R; rlist; |
---|
[5c67581] | 1997 | } |
---|
| 1998 | |
---|
| 1999 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 2000 | |
---|
| 2001 | proc triangL_solve( ideal fi, list # ) |
---|
[9173792] | 2002 | "USAGE: triangL_solve(i [, p] ); i=ideal, p=integer,@* |
---|
[fb86b6b] | 2003 | p>0: gives precision of complex numbers in digits (default: p=30). |
---|
| 2004 | ASSUME: the ground field has char 0; i is a zero-dimensional ideal. |
---|
[dd73043] | 2005 | RETURN: ring @code{R} with the same number of variables, but with complex |
---|
[3c4dcc] | 2006 | coefficients (and precision p). @code{R} comes with a list |
---|
[6ed15c] | 2007 | @code{rlist} of numbers, in which the complex roots of i are stored. |
---|
[dd73043] | 2008 | NOTE: The procedure uses a triangular system (Lazard's Algorithm) computed |
---|
| 2009 | from a standard basis of input ideal i to determine recursively all |
---|
| 2010 | complex roots with Laguerre's algorithm. |
---|
[5c67581] | 2011 | EXAMPLE: example triangL_solve; shows an example |
---|
| 2012 | " |
---|
| 2013 | { |
---|
| 2014 | int prec=30; |
---|
| 2015 | |
---|
| 2016 | if ( size(#)>=1 && typeof(#[1])=="int") |
---|
| 2017 | { |
---|
[cb8d1d6] | 2018 | prec=#[1]; |
---|
[5c67581] | 2019 | } |
---|
| 2020 | |
---|
[6ed15c] | 2021 | def R=triang_solve(triangL(stdfglm(fi)),prec); |
---|
| 2022 | dbprint( printlevel-voice+3," |
---|
| 2023 | // 'triangL_solve' created a ring, in which a list rlist of numbers (the |
---|
[3c4dcc] | 2024 | // complex solutions) is stored. |
---|
| 2025 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 2026 | // to the return value): |
---|
[6ed15c] | 2027 | setring R; rlist; "); |
---|
| 2028 | return(R); |
---|
[5c67581] | 2029 | } |
---|
| 2030 | example |
---|
| 2031 | { |
---|
| 2032 | "EXAMPLE:";echo=2; |
---|
| 2033 | ring r = 0,(x,y),lp; |
---|
| 2034 | // compute the intersection points of two curves |
---|
[6ed15c] | 2035 | ideal s = x2 + y2 - 10, x2 + xy + 2y2 - 16; |
---|
| 2036 | def R = triangL_solve(s,10); |
---|
| 2037 | setring R; rlist; |
---|
[5c67581] | 2038 | } |
---|
| 2039 | |
---|
| 2040 | |
---|
| 2041 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 2042 | |
---|
| 2043 | proc triang_solve( list lfi, int prec, list # ) |
---|
[6ed15c] | 2044 | "USAGE: triang_solve(l,p [,d] ); l=list, p,d=integers@* |
---|
[0bc582c] | 2045 | l is a list of finitely many triangular systems, such that the union of |
---|
[fb86b6b] | 2046 | their varieties equals the variety of the initial ideal.@* |
---|
| 2047 | p>0: gives precision of complex numbers in digits,@* |
---|
| 2048 | d>0: gives precision (1<d<p) for near-zero-determination,@* |
---|
| 2049 | (default: d=1/2*p). |
---|
[9173792] | 2050 | ASSUME: the ground field has char 0;@* |
---|
[dd73043] | 2051 | l was computed using the algorithm of Lazard or the algorithm of |
---|
| 2052 | Moeller (see triang.lib). |
---|
| 2053 | RETURN: ring @code{R} with the same number of variables, but with complex |
---|
[3c4dcc] | 2054 | coefficients (and precision p). @code{R} comes with a list |
---|
[6ed15c] | 2055 | @code{rlist} of numbers, in which the complex roots of l are stored.@* |
---|
[5c67581] | 2056 | EXAMPLE: example triang_solve; shows an example |
---|
| 2057 | " |
---|
| 2058 | { |
---|
| 2059 | def oring= basering; |
---|
[6ed15c] | 2060 | list LL; |
---|
[5c67581] | 2061 | |
---|
[b9b906] | 2062 | // change the ground field to complex numbers |
---|
[6ed15c] | 2063 | string nrings= "ring RC =(complex,"+string(prec) |
---|
[cb8d1d6] | 2064 | +",I),("+varstr(basering)+"),lp;"; |
---|
[38c165] | 2065 | execute(nrings); |
---|
[5c67581] | 2066 | |
---|
| 2067 | // list with entry 0 (number) |
---|
| 2068 | number nn=0; |
---|
| 2069 | |
---|
| 2070 | // set number of digits for zero-comparison of roots |
---|
| 2071 | if ( !defined(myCompDigits) ) |
---|
[b9b906] | 2072 | { |
---|
[cb8d1d6] | 2073 | int myCompDigits; |
---|
[5c67581] | 2074 | } |
---|
| 2075 | if ( size(#)>=1 && typeof(#[1])=="int" ) |
---|
| 2076 | { |
---|
[cb8d1d6] | 2077 | myCompDigits=#[1]; |
---|
[5c67581] | 2078 | } |
---|
| 2079 | else |
---|
| 2080 | { |
---|
[0ba413] | 2081 | myCompDigits=(system("getPrecDigits")); |
---|
[5c67581] | 2082 | } |
---|
| 2083 | |
---|
[6ed15c] | 2084 | dbprint( printlevel-voice+2,"// myCompDigits="+string(myCompDigits) ); |
---|
[5c67581] | 2085 | |
---|
| 2086 | int idelem; |
---|
| 2087 | int nv= nvars(basering); |
---|
[6ed15c] | 2088 | int i,j,lis; |
---|
[8b96123] | 2089 | list resu,li; |
---|
[5c67581] | 2090 | |
---|
| 2091 | if ( !defined(rlist) ) |
---|
| 2092 | { |
---|
[cb8d1d6] | 2093 | list rlist; |
---|
| 2094 | export rlist; |
---|
[5c67581] | 2095 | } |
---|
[b9b906] | 2096 | |
---|
[6ed15c] | 2097 | int rn@=0; |
---|
[5c67581] | 2098 | |
---|
| 2099 | // map the list |
---|
| 2100 | list lfi= imap(oring,lfi); |
---|
| 2101 | int slfi= size(lfi); |
---|
| 2102 | |
---|
[6ed15c] | 2103 | ideal fi; |
---|
[b9b906] | 2104 | for ( i= 1; i <= slfi; i++ ) |
---|
[5c67581] | 2105 | { |
---|
[cb8d1d6] | 2106 | // map fi from old to new ring |
---|
| 2107 | fi= lfi[i]; //imap(oring,lfi[i]); |
---|
| 2108 | |
---|
| 2109 | idelem= size(fi); |
---|
| 2110 | |
---|
| 2111 | // solve fi[1] |
---|
[8b96123] | 2112 | li= laguerre_solve(fi[1],myCompDigits,myCompDigits,0); |
---|
[cb8d1d6] | 2113 | lis= size(li); |
---|
| 2114 | |
---|
[6ed15c] | 2115 | dbprint( printlevel-voice+2,"// laguerre found roots: "+string(lis) ); |
---|
[cb8d1d6] | 2116 | |
---|
| 2117 | for ( j= 1; j <= lis; j++ ) |
---|
| 2118 | { |
---|
[6ed15c] | 2119 | dbprint( printlevel-voice+2,"// root "+string(j) ); |
---|
[cb8d1d6] | 2120 | rn@++; |
---|
[8b96123] | 2121 | resu[nv]= li[j]; |
---|
[3c4dcc] | 2122 | LL = psubst( 1, 2, nv-1, resu, fi, idelem, nv, myCompDigits, |
---|
[6ed15c] | 2123 | rn@, rlist ); |
---|
| 2124 | rlist = LL[1]; |
---|
| 2125 | rn@ = LL[2]; |
---|
[cb8d1d6] | 2126 | } |
---|
[5c67581] | 2127 | } |
---|
| 2128 | |
---|
[6ed15c] | 2129 | dbprint( printlevel-voice+3," |
---|
| 2130 | // 'triang_solve' created a ring, in which a list rlist of numbers (the |
---|
[3c4dcc] | 2131 | // complex solutions) is stored. |
---|
| 2132 | // To access the list of complex solutions, type (if the name R was assigned |
---|
| 2133 | // to the return value): |
---|
[6ed15c] | 2134 | setring R; rlist; "); |
---|
| 2135 | |
---|
| 2136 | return(RC); |
---|
[0f5091] | 2137 | } |
---|
| 2138 | example |
---|
| 2139 | { |
---|
[5c67581] | 2140 | "EXAMPLE:";echo=2; |
---|
| 2141 | ring r = 0,(x,y),lp; |
---|
| 2142 | // compute the intersection points of two curves |
---|
| 2143 | ideal s= x2 + y2 - 10, x2 + xy + 2y2 - 16; |
---|
[6ed15c] | 2144 | def R=triang_solve(triangLfak(stdfglm(s)),10); |
---|
| 2145 | setring R; rlist; |
---|
[0f5091] | 2146 | } |
---|
[5c67581] | 2147 | |
---|
[fb86b6b] | 2148 | /////////////////////////////////////////////////////////////////////////////// |
---|
[c7f3b7] | 2149 | |
---|
| 2150 | proc simplexOut(list l) |
---|
[fb86b6b] | 2151 | "USAGE: simplexOut(l); l list |
---|
| 2152 | ASSUME: l is the output of simplex. |
---|
[dd73043] | 2153 | RETURN: Nothing. The procedure prints the computed solution of simplex |
---|
[3c4dcc] | 2154 | (as strings) in a nice format. |
---|
[fb86b6b] | 2155 | SEE ALSO: simplex |
---|
[c7f3b7] | 2156 | EXAMPLE: example simplexOut; shows an example |
---|
| 2157 | " |
---|
| 2158 | { |
---|
| 2159 | int i,j; |
---|
| 2160 | matrix m= l[1]; |
---|
| 2161 | intvec iposv= l[3]; |
---|
| 2162 | int icase= l[2]; |
---|
| 2163 | |
---|
| 2164 | int cols= ncols(m); |
---|
| 2165 | int rows= nrows(m); |
---|
| 2166 | |
---|
| 2167 | int N= l[6]; |
---|
| 2168 | |
---|
[fb86b6b] | 2169 | if ( 1 == icase ) // objective function is unbound |
---|
[c7f3b7] | 2170 | { |
---|
| 2171 | "objective function is unbound"; |
---|
[fb86b6b] | 2172 | return(); |
---|
[c7f3b7] | 2173 | } |
---|
[fb86b6b] | 2174 | if ( -1 == icase ) // no solution satisfies the given constraints |
---|
[c7f3b7] | 2175 | { |
---|
| 2176 | "no solution satisfies the given constraints"; |
---|
[fb86b6b] | 2177 | return(); |
---|
[c7f3b7] | 2178 | } |
---|
| 2179 | if ( -2 == icase ) // other error |
---|
| 2180 | { |
---|
| 2181 | "an error occurred during simplex computation!"; |
---|
[fb86b6b] | 2182 | return(); |
---|
[c7f3b7] | 2183 | } |
---|
| 2184 | |
---|
| 2185 | for ( i = 1; i <= rows; i++ ) |
---|
| 2186 | { |
---|
[b9b906] | 2187 | if (i == 1) |
---|
[c7f3b7] | 2188 | { |
---|
| 2189 | "z = "+string(m[1][1]); |
---|
| 2190 | } |
---|
[b9b906] | 2191 | else |
---|
[c7f3b7] | 2192 | { |
---|
[fb86b6b] | 2193 | if ( iposv[i-1] <= N ) |
---|
[c7f3b7] | 2194 | { |
---|
[fb86b6b] | 2195 | "x"+string(iposv[i-1])+" = "+string(m[i,1]); |
---|
[c7f3b7] | 2196 | } |
---|
[b9b906] | 2197 | // else |
---|
[c7f3b7] | 2198 | // { |
---|
[cb8d1d6] | 2199 | // "Y"; iposv[i-1]-N+1; |
---|
[c7f3b7] | 2200 | // } |
---|
| 2201 | } |
---|
[b9b906] | 2202 | } |
---|
[c7f3b7] | 2203 | } |
---|
| 2204 | example |
---|
| 2205 | { |
---|
[fb86b6b] | 2206 | "EXAMPLE:";echo=2; |
---|
| 2207 | ring r = (real,10),(x),lp; |
---|
| 2208 | |
---|
| 2209 | // consider the max. problem: |
---|
| 2210 | // |
---|
| 2211 | // maximize x(1) + x(2) + 3*x(3) - 0.5*x(4) |
---|
| 2212 | // |
---|
| 2213 | // with constraints: x(1) + 2*x(3) <= 740 |
---|
| 2214 | // 2*x(2) - 7*x(4) <= 0 |
---|
| 2215 | // x(2) - x(3) + 2*x(4) >= 0.5 |
---|
| 2216 | // x(1) + x(2) + x(3) + x(4) = 9 |
---|
| 2217 | // |
---|
| 2218 | matrix sm[5][5]= 0, 1, 1, 3,-0.5, |
---|
| 2219 | 740,-1, 0,-2, 0, |
---|
| 2220 | 0, 0,-2, 0, 7, |
---|
| 2221 | 0.5, 0,-1, 1,-2, |
---|
| 2222 | 9,-1,-1,-1,-1; |
---|
| 2223 | |
---|
| 2224 | int n = 4; // number of constraints |
---|
| 2225 | int m = 4; // number of variables |
---|
| 2226 | int m1= 2; // number of <= constraints |
---|
| 2227 | int m2= 1; // number of >= constraints |
---|
| 2228 | int m3= 1; // number of == constraints |
---|
| 2229 | |
---|
| 2230 | list sol=simplex(sm, n, m, m1, m2, m3); |
---|
| 2231 | simplexOut(sol); |
---|
[c7f3b7] | 2232 | } |
---|
| 2233 | |
---|
[5c67581] | 2234 | |
---|
| 2235 | // local Variables: *** |
---|
| 2236 | // c-set-style: bsd *** |
---|
| 2237 | // End: *** |
---|