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