1 | /////////////////////////////////////////////////////////////////////////////// |
---|
2 | version="$Id: ntsolve.lib,v 1.6 2000-07-06 13:32:25 pohl Exp $"; |
---|
3 | info=" |
---|
4 | LIBRARY: ntsolve.lib ONE REAL SOLUTION OF POLYNOMIAL SYSTEMS (NEWTON ITERATION) |
---|
5 | AUTHOR: Wilfred Pohl, email: pohl@mathematik.uni-kl.de |
---|
6 | |
---|
7 | PROCEDURES: |
---|
8 | nt_solve(i,..); find one real root of 0-dimensional ideal |
---|
9 | "; |
---|
10 | /////////////////////////////////////////////////////////////////////////////// |
---|
11 | |
---|
12 | proc nt_solve( ideal gls, ideal ini, intvec ipar ) |
---|
13 | "USAGE: nt_solve(gls,ini,ipar); |
---|
14 | gls: the equations |
---|
15 | ini: the ideal of initial values |
---|
16 | ipar: control |
---|
17 | ipar[1] - max. number of iterations |
---|
18 | ipar[2] - accuracy, have the l2-norm ||.|| |
---|
19 | for the initial error eps0 = ||gls(ini)|| |
---|
20 | accept solution sol with |
---|
21 | ||gls(sol)|| < eps0*(0.1^ipar[2]) |
---|
22 | ipar[3] - some output for contol if != 0 |
---|
23 | defaults - 100, 10, 0 |
---|
24 | ASSUME: gls is a zerodimensional ideal with |
---|
25 | nvars(basering) = size(gls) (> 1) |
---|
26 | RETURN: ideal of one solution (if found) |
---|
27 | 0 (else) |
---|
28 | EXAMPLE: example nt_solve; shows an example |
---|
29 | " |
---|
30 | { |
---|
31 | def rn = basering; |
---|
32 | int di = size(gls); |
---|
33 | if (nvars(basering) != di){ |
---|
34 | ERROR("wrong dimension");} |
---|
35 | if (size(ini) != di){ |
---|
36 | ERROR("wrong number of initial values");} |
---|
37 | int prec = system("getPrecDigits"); // precision |
---|
38 | |
---|
39 | int i1,i2,i3; |
---|
40 | i1 = size(ipar); |
---|
41 | int itmax, acc, prot; |
---|
42 | if (i1 < 1){itmax = 100;}else{itmax = ipar[1];} |
---|
43 | if (i1 < 2){acc = prec/2;}else{acc = ipar[2];} |
---|
44 | if (i1 < 3){prot = 0;}else{prot = ipar[3];} |
---|
45 | if ((acc <= 0)||(acc > prec-1)){acc = prec-1;} |
---|
46 | |
---|
47 | int dpl = di+1; |
---|
48 | string out; // for prot != 0 and more |
---|
49 | out = "ring rnewton=(real,prec),("+varstr(basering)+"),(c,dp);"; |
---|
50 | execute(out); |
---|
51 | ideal gls1=imap(rn,gls); |
---|
52 | module nt,sub; |
---|
53 | sub = transpose(jacob(gls1)); |
---|
54 | for (i1=di;i1>0;i1--){ |
---|
55 | if(sub[i1]==0){break;}} |
---|
56 | if (i1>0){ |
---|
57 | setring rn; kill rnewton; |
---|
58 | ERROR("one var not in equation");} |
---|
59 | list direction; |
---|
60 | ideal ini1; |
---|
61 | ini1 = imap(rn,ini); |
---|
62 | number dum,y1,y2,y3,genau; |
---|
63 | genau = 0.1; |
---|
64 | dum = genau; |
---|
65 | genau = genau^acc; |
---|
66 | for (i1=di;i1>0;i1--){ |
---|
67 | sub[i1]=sub[i1]+gls1[i1]*gen(dpl);} |
---|
68 | nt = sub; |
---|
69 | for (i1=di;i1>0;i1--){ |
---|
70 | nt = subst(nt,var(i1),ini1[i1]);} |
---|
71 | // now we have in sub the general structure |
---|
72 | // and in nt the structure with subst. vars |
---|
73 | |
---|
74 | // compute initial error |
---|
75 | y1 = ml2norm(nt,genau); |
---|
76 | if(prot){out=" initial error = "+string(y1);out;} |
---|
77 | y2 = genau*y1; |
---|
78 | |
---|
79 | // begin of iteration |
---|
80 | for(i3=1;i3<=itmax;i3++){ |
---|
81 | if(prot){out=" Nr. "+string(i3);out;} |
---|
82 | |
---|
83 | // find newton direction |
---|
84 | direction=bareiss(nt,1,-1); |
---|
85 | |
---|
86 | // find dumping |
---|
87 | dum = linesearch(gls1,ini1,direction[1],y1,dum,genau); |
---|
88 | if (i3%5 == 0) |
---|
89 | { |
---|
90 | if (dum <= 0.000001) |
---|
91 | { |
---|
92 | dum = 1.0; |
---|
93 | } |
---|
94 | } |
---|
95 | if(prot){out=" dumping = "+string(dum);out;} |
---|
96 | |
---|
97 | // new value |
---|
98 | for(i1=di;i1>0;i1--){ |
---|
99 | ini1[i1]=ini1[i1]-dum*direction[1][i1];} |
---|
100 | nt = sub; |
---|
101 | for (i1=di;i1>0;i1--){ |
---|
102 | nt = subst(nt,var(i1),ini1[i1]);} |
---|
103 | y1 = ml2norm(nt,genau); |
---|
104 | if(prot){out=" error = "+string(y1);out;} |
---|
105 | if(y1<y2){break;} // we are ready |
---|
106 | } |
---|
107 | |
---|
108 | if (y1>y2){ |
---|
109 | "WARNING: no convergence";} |
---|
110 | setring rn; |
---|
111 | ini = imap(rnewton,ini1); |
---|
112 | kill rnewton; |
---|
113 | return(ini); |
---|
114 | } |
---|
115 | example |
---|
116 | { |
---|
117 | "EXAMPLE:";echo=2; |
---|
118 | ring rsq = (real,40),(x,y,z,w),lp; |
---|
119 | ideal gls = x2+y2+z2-10, y2+z3+w-8, xy+yz+xz+w5 - 1,w3+y; |
---|
120 | ideal ini = 3.1,2.9,1.1,0.5; |
---|
121 | intvec ipar = 200,0,1; |
---|
122 | ideal sol = nt_solve(gls,ini,ipar); |
---|
123 | sol; |
---|
124 | } |
---|
125 | /////////////////////////////////////////////////////////////////////////////// |
---|
126 | |
---|
127 | static proc sqrt (number wr, number wa, number wg) |
---|
128 | { |
---|
129 | number es,we; |
---|
130 | number wb=wa; |
---|
131 | number wf=wb*wb-wr; |
---|
132 | if(wf>0){ |
---|
133 | es=wf;} |
---|
134 | else{ |
---|
135 | es=-wf;} |
---|
136 | we=wg*es; |
---|
137 | while (es>we) |
---|
138 | { |
---|
139 | wf=wf/(wb+wb); |
---|
140 | wb=wb-wf; |
---|
141 | wf=wb*wb-wr; |
---|
142 | if(wf>0){ |
---|
143 | es=wf;} |
---|
144 | else{ |
---|
145 | es=-wf;} |
---|
146 | } |
---|
147 | return(wb); |
---|
148 | } |
---|
149 | |
---|
150 | static proc il2norm (ideal H, number wg) |
---|
151 | { |
---|
152 | number wa,wb; |
---|
153 | int wi,dpl; |
---|
154 | wa = leadcoef(H[1]); |
---|
155 | wa = wa*wa; |
---|
156 | for(wi=size(H);wi>1;wi--) |
---|
157 | { |
---|
158 | wb=leadcoef(H[wi]); |
---|
159 | wa=wa+wb*wb; |
---|
160 | } |
---|
161 | return(sqrt(wa,wa,wg)); |
---|
162 | } |
---|
163 | |
---|
164 | static proc ml2norm (module H, number wg) |
---|
165 | { |
---|
166 | number wa,wb; |
---|
167 | int wi,dpl; |
---|
168 | dpl = size(H)+1; |
---|
169 | wa = leadcoef(H[1][dpl]); |
---|
170 | wa = wa*wa; |
---|
171 | for(wi=size(H);wi>1;wi--) |
---|
172 | { |
---|
173 | wb=leadcoef(H[wi][dpl]); |
---|
174 | wa=wa+wb*wb; |
---|
175 | } |
---|
176 | return(sqrt(wa,wa,wg)); |
---|
177 | } |
---|
178 | |
---|
179 | static |
---|
180 | proc linesearch(ideal nl, ideal aa, ideal bb, |
---|
181 | number z1, number tt, number gg) |
---|
182 | { |
---|
183 | int ii,d; |
---|
184 | ideal cc,jn; |
---|
185 | number ss,z2,z3,mm; |
---|
186 | |
---|
187 | mm=0.000001; |
---|
188 | ss=tt; |
---|
189 | d=size(nl); |
---|
190 | cc=aa; |
---|
191 | for(ii=d;ii>0;ii--){cc[ii]=cc[ii]-ss*bb[ii];} |
---|
192 | jn=nl; |
---|
193 | for(ii=d;ii>0;ii--){jn=subst(jn,var(ii),cc[ii]);} |
---|
194 | z2=il2norm(jn,gg); |
---|
195 | z3=-1; |
---|
196 | while(z2>=z1) |
---|
197 | { |
---|
198 | ss=0.5*ss; |
---|
199 | if(ss<mm){return (mm);} |
---|
200 | cc=aa; |
---|
201 | for(ii=d;ii>0;ii--) |
---|
202 | { |
---|
203 | cc[ii]=cc[ii]-ss*bb[ii]; |
---|
204 | } |
---|
205 | jn=nl; |
---|
206 | for(ii=d;ii>0;ii--){jn=subst(jn,var(ii),cc[ii]);} |
---|
207 | z3=z2; |
---|
208 | z2=il2norm(jn,gg); |
---|
209 | } |
---|
210 | if(z3<0) |
---|
211 | { |
---|
212 | while(z3<z2) |
---|
213 | { |
---|
214 | ss=ss+ss; |
---|
215 | cc=aa; |
---|
216 | for(ii=d;ii>0;ii--) |
---|
217 | { |
---|
218 | cc[ii]=cc[ii]-ss*bb[ii]; |
---|
219 | } |
---|
220 | jn=nl; |
---|
221 | for(ii=d;ii>0;ii--){jn=subst(jn,var(ii),cc[ii]);} |
---|
222 | if(z3>0){z2=z3;} |
---|
223 | z3=il2norm(jn,gg); |
---|
224 | } |
---|
225 | } |
---|
226 | z2=z2-z1; |
---|
227 | z3=z3-z1; |
---|
228 | ss=0.25*ss*(z3-4*z2)/(z3-2*z2); |
---|
229 | if(ss>1.0){return (1.0);} |
---|
230 | if(ss<mm){return (mm);} |
---|
231 | return(ss); |
---|
232 | } |
---|
233 | /////////////////////////////////////////////////////////////////////////////// |
---|
234 | |
---|
235 | // End: *** |
---|