1 | %{ |
---|
2 | /* |
---|
3 | * Test mod fuer modgen |
---|
4 | */ |
---|
5 | |
---|
6 | #include <stdio.h> |
---|
7 | #include <kernel/mod2.h> |
---|
8 | #include <tok.h> |
---|
9 | #include <structs.h> |
---|
10 | #include <ipid.h> |
---|
11 | #include <intvec.h> |
---|
12 | #include <numbers.h> |
---|
13 | #include <polys.h> |
---|
14 | #include <ideals.h> |
---|
15 | #include <lists.h> |
---|
16 | #include <matpol.h> |
---|
17 | #include <clapsing.h> |
---|
18 | #include <febase.h> |
---|
19 | |
---|
20 | #ifndef EIGENVAL_H |
---|
21 | #define EIGENVAL_H |
---|
22 | |
---|
23 | matrix evSwap(matrix M,int i,int j); |
---|
24 | BOOLEAN evSwap(leftv res,leftv h); |
---|
25 | matrix evRowElim(matrix M,int i,int j,int k); |
---|
26 | BOOLEAN evRowElim(leftv res,leftv h); |
---|
27 | matrix evColElim(matrix M,int i,int j,int k); |
---|
28 | BOOLEAN evColElim(leftv res,leftv h); |
---|
29 | matrix evHessenberg(matrix M); |
---|
30 | BOOLEAN evHessenberg(leftv res,leftv h); |
---|
31 | lists evEigenvals(matrix M); |
---|
32 | BOOLEAN evEigenvals(leftv res,leftv h); |
---|
33 | |
---|
34 | #endif /* EIGENVAL_H */ |
---|
35 | %} |
---|
36 | |
---|
37 | |
---|
38 | package="eigenval"; |
---|
39 | version="$Id$"; |
---|
40 | info=" |
---|
41 | LIBRARY: EIGENVALUES AND HESSENBERG FORM |
---|
42 | AUTHOR: Mathias Schulze, email: mschulze@mathematik.uni-kl.de |
---|
43 | |
---|
44 | some help still missing |
---|
45 | "; |
---|
46 | |
---|
47 | %modinitial |
---|
48 | // no commands to be run upon loading the module |
---|
49 | %endinitial |
---|
50 | |
---|
51 | %procedures |
---|
52 | |
---|
53 | |
---|
54 | matrix hessenberg(matrix M) { |
---|
55 | %declaration; |
---|
56 | %typecheck; |
---|
57 | %return(evHessenberg(M)); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | list eigenvals(matrix M) { |
---|
62 | %declaration; |
---|
63 | %typecheck; |
---|
64 | %return(evEigenvals(M)); |
---|
65 | } |
---|
66 | |
---|
67 | %C |
---|
68 | |
---|
69 | matrix evSwap(matrix M,int i,int j) |
---|
70 | { |
---|
71 | if(i==j) |
---|
72 | return(M); |
---|
73 | |
---|
74 | for(int k=1;k<=MATROWS(M);k++) |
---|
75 | { |
---|
76 | poly p=MATELEM(M,i,k); |
---|
77 | MATELEM(M,i,k)=MATELEM(M,j,k); |
---|
78 | MATELEM(M,j,k)=p; |
---|
79 | } |
---|
80 | |
---|
81 | for(int k=1;k<=MATCOLS(M);k++) |
---|
82 | { |
---|
83 | poly p=MATELEM(M,k,i); |
---|
84 | MATELEM(M,k,i)=MATELEM(M,k,j); |
---|
85 | MATELEM(M,k,j)=p; |
---|
86 | } |
---|
87 | |
---|
88 | return(M); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | BOOLEAN evSwap(leftv res,leftv h) |
---|
93 | { |
---|
94 | if(currRingHdl) |
---|
95 | { |
---|
96 | if(h&&h->Typ()==MATRIX_CMD) |
---|
97 | { |
---|
98 | matrix M=(matrix)h->Data(); |
---|
99 | h=h->next; |
---|
100 | if(h&&h->Typ()==INT_CMD) |
---|
101 | { |
---|
102 | int i=(int)(long)h->Data(); |
---|
103 | h=h->next; |
---|
104 | if(h&&h->Typ()==INT_CMD) |
---|
105 | { |
---|
106 | int j=(int)(long)h->Data(); |
---|
107 | res->rtyp=MATRIX_CMD; |
---|
108 | res->data=(void *)evSwap(mpCopy(M),i,j); |
---|
109 | return FALSE; |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | WerrorS("<matrix>,<int>,<int> expected"); |
---|
114 | return TRUE; |
---|
115 | } |
---|
116 | WerrorS("no ring active"); |
---|
117 | return TRUE; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | matrix evRowElim(matrix M,int i,int j,int k) |
---|
122 | { |
---|
123 | if(MATELEM(M,i,k)==NULL||MATELEM(M,j,k)==NULL) |
---|
124 | return(M); |
---|
125 | |
---|
126 | poly p=pNSet(nDiv(pGetCoeff(MATELEM(M,i,k)),pGetCoeff(MATELEM(M,j,k)))); |
---|
127 | pNormalize(p); |
---|
128 | |
---|
129 | for(int l=1;l<=MATCOLS(M);l++) |
---|
130 | { |
---|
131 | MATELEM(M,i,l)=pSub(MATELEM(M,i,l),ppMult_qq(p,MATELEM(M,j,l))); |
---|
132 | pNormalize(MATELEM(M,i,l)); |
---|
133 | } |
---|
134 | for(int l=1;l<=MATROWS(M);l++) |
---|
135 | { |
---|
136 | MATELEM(M,l,j)=pAdd(MATELEM(M,l,j),ppMult_qq(p,MATELEM(M,l,i))); |
---|
137 | pNormalize(MATELEM(M,l,j)); |
---|
138 | } |
---|
139 | |
---|
140 | pDelete(&p); |
---|
141 | |
---|
142 | return(M); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | BOOLEAN evRowElim(leftv res,leftv h) |
---|
147 | { |
---|
148 | if(currRingHdl) |
---|
149 | { |
---|
150 | if(h&&h->Typ()==MATRIX_CMD) |
---|
151 | { |
---|
152 | matrix M=(matrix)h->Data(); |
---|
153 | h=h->next; |
---|
154 | if(h&&h->Typ()==INT_CMD) |
---|
155 | { |
---|
156 | int i=(int)(long)h->Data(); |
---|
157 | h=h->next; |
---|
158 | if(h&&h->Typ()==INT_CMD) |
---|
159 | { |
---|
160 | int j=(int)(long)h->Data(); |
---|
161 | h=h->next; |
---|
162 | if(h&&h->Typ()==INT_CMD) |
---|
163 | { |
---|
164 | int k=(int)(long)h->Data(); |
---|
165 | res->rtyp=MATRIX_CMD; |
---|
166 | res->data=(void *)evRowElim(mpCopy(M),i,j,k); |
---|
167 | return FALSE; |
---|
168 | } |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | WerrorS("<matrix>,<int>,<int>,<int> expected"); |
---|
173 | return TRUE; |
---|
174 | } |
---|
175 | WerrorS("no ring active"); |
---|
176 | return TRUE; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | matrix evColElim(matrix M,int i,int j,int k) |
---|
181 | { |
---|
182 | if(MATELEM(M,k,i)==0||MATELEM(M,k,j)==0) |
---|
183 | return(M); |
---|
184 | |
---|
185 | poly p=pNSet(nDiv(pGetCoeff(MATELEM(M,k,i)),pGetCoeff(MATELEM(M,k,j)))); |
---|
186 | pNormalize(p); |
---|
187 | |
---|
188 | for(int l=1;l<=MATROWS(M);l++) |
---|
189 | { |
---|
190 | MATELEM(M,l,i)=pSub(MATELEM(M,l,i),ppMult_qq(p,MATELEM(M,l,j))); |
---|
191 | pNormalize(MATELEM(M,l,i)); |
---|
192 | } |
---|
193 | for(int l=1;l<=MATCOLS(M);l++) |
---|
194 | { |
---|
195 | MATELEM(M,j,l)=pAdd(MATELEM(M,j,l),ppMult_qq(p,MATELEM(M,i,l))); |
---|
196 | pNormalize(MATELEM(M,j,l)); |
---|
197 | } |
---|
198 | |
---|
199 | pDelete(&p); |
---|
200 | |
---|
201 | return(M); |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | BOOLEAN evColElim(leftv res,leftv h) |
---|
206 | { |
---|
207 | if(currRingHdl) |
---|
208 | { |
---|
209 | if(h&&h->Typ()==MATRIX_CMD) |
---|
210 | { |
---|
211 | matrix M=(matrix)h->Data(); |
---|
212 | h=h->next; |
---|
213 | if(h&&h->Typ()==INT_CMD) |
---|
214 | { |
---|
215 | int i=(int)(long)h->Data(); |
---|
216 | h=h->next; |
---|
217 | if(h&&h->Typ()==INT_CMD) |
---|
218 | { |
---|
219 | int j=(int)(long)h->Data(); |
---|
220 | h=h->next; |
---|
221 | if(h&&h->Typ()==INT_CMD) |
---|
222 | { |
---|
223 | int k=(int)(long)h->Data(); |
---|
224 | res->rtyp=MATRIX_CMD; |
---|
225 | res->data=(void *)evColElim(mpCopy(M),i,j,k); |
---|
226 | return FALSE; |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|
231 | WerrorS("<matrix>,<int>,<int>,<int> expected"); |
---|
232 | return TRUE; |
---|
233 | } |
---|
234 | WerrorS("no ring active"); |
---|
235 | return TRUE; |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | matrix evHessenberg(matrix M) |
---|
240 | { |
---|
241 | int n=MATROWS(M); |
---|
242 | if(n!=MATCOLS(M)) |
---|
243 | return(M); |
---|
244 | |
---|
245 | for(int k=1,j=2;k<n-1;k++,j=k+1) |
---|
246 | { |
---|
247 | while(j<=n&&MATELEM(M,j,k)==0) |
---|
248 | j++; |
---|
249 | |
---|
250 | if(j<=n) |
---|
251 | { |
---|
252 | M=evSwap(M,j,k+1); |
---|
253 | |
---|
254 | for(int i=j+1;i<=n;i++) |
---|
255 | M=evRowElim(M,i,k+1,k); |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | return(M); |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | BOOLEAN evHessenberg(leftv res,leftv h) |
---|
264 | { |
---|
265 | if(currRingHdl) |
---|
266 | { |
---|
267 | if(h&&h->Typ()==MATRIX_CMD) |
---|
268 | { |
---|
269 | matrix M=(matrix)h->Data(); |
---|
270 | res->rtyp=MATRIX_CMD; |
---|
271 | res->data=(void *)evHessenberg(mpCopy(M)); |
---|
272 | return FALSE; |
---|
273 | } |
---|
274 | WerrorS("<matrix> expected"); |
---|
275 | return TRUE; |
---|
276 | } |
---|
277 | WerrorS("no ring active"); |
---|
278 | return TRUE; |
---|
279 | } |
---|
280 | |
---|
281 | |
---|
282 | lists evEigenvals(matrix M) |
---|
283 | { |
---|
284 | lists l=(lists)omAllocBin(slists_bin); |
---|
285 | if(MATROWS(M)!=MATCOLS(M)) |
---|
286 | { |
---|
287 | l->Init(0); |
---|
288 | return(l); |
---|
289 | } |
---|
290 | |
---|
291 | M=evHessenberg((matrix)idJet((ideal)M,0)); |
---|
292 | |
---|
293 | int n=MATROWS(M); |
---|
294 | ideal e=idInit(n,1); |
---|
295 | intvec *m=new intvec(n); |
---|
296 | |
---|
297 | poly t=pOne(); |
---|
298 | pSetExp(t,1,1); |
---|
299 | pSetm(t); |
---|
300 | |
---|
301 | for(int j0=1,j=2,k=0;j<=n+1;j0=j,j++) |
---|
302 | { |
---|
303 | while(j<=n&&MATELEM(M,j,j-1)!=NULL) |
---|
304 | j++; |
---|
305 | if(j==j0+1) |
---|
306 | { |
---|
307 | e->m[k]=pHead(MATELEM(M,j0,j0)); |
---|
308 | (*m)[k]=1; |
---|
309 | k++; |
---|
310 | } |
---|
311 | else |
---|
312 | { |
---|
313 | int n0=j-j0; |
---|
314 | matrix M0=mpNew(n0,n0); |
---|
315 | |
---|
316 | j0--; |
---|
317 | for(int i=1;i<=n0;i++) |
---|
318 | for(int j=1;j<=n0;j++) |
---|
319 | MATELEM(M0,i,j)=pCopy(MATELEM(M,j0+i,j0+j)); |
---|
320 | for(int i=1;i<=n0;i++) |
---|
321 | MATELEM(M0,i,i)=pSub(MATELEM(M0,i,i),pCopy(t)); |
---|
322 | |
---|
323 | intvec *m0; |
---|
324 | ideal e0=singclap_factorize(mpDetBareiss(M0),&m0,2); |
---|
325 | |
---|
326 | for(int i=0;i<IDELEMS(e0);i++) |
---|
327 | { |
---|
328 | if(pNext(e0->m[i])==NULL) |
---|
329 | { |
---|
330 | (*m)[k]=(*m0)[i]; |
---|
331 | k++; |
---|
332 | } |
---|
333 | else |
---|
334 | if(pGetExp(e0->m[i],1)<2&&pGetExp(pNext(e0->m[i]),1)<2&& |
---|
335 | pNext(pNext(e0->m[i]))==NULL) |
---|
336 | { |
---|
337 | number e1=nNeg(nCopy(pGetCoeff(e0->m[i]))); |
---|
338 | if(pGetExp(pNext(e0->m[i]),1)==0) |
---|
339 | e->m[k]=pNSet(nDiv(pGetCoeff(pNext(e0->m[i])),e1)); |
---|
340 | else |
---|
341 | e->m[k]=pNSet(nDiv(e1,pGetCoeff(pNext(e0->m[i])))); |
---|
342 | nDelete(&e1); |
---|
343 | pNormalize(e->m[k]); |
---|
344 | (*m)[k]=(*m0)[i]; |
---|
345 | k++; |
---|
346 | } |
---|
347 | else |
---|
348 | { |
---|
349 | e->m[k]=e0->m[i]; |
---|
350 | pNormalize(e->m[k]); |
---|
351 | e0->m[i]=NULL; |
---|
352 | (*m)[k]=(*m0)[i]; |
---|
353 | k++; |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | delete(m0); |
---|
358 | idDelete(&e0); |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | pDelete(&t); |
---|
363 | idDelete((ideal *)&M); |
---|
364 | |
---|
365 | for(int i0=0;i0<n-1;i0++) |
---|
366 | { |
---|
367 | for(int i1=i0+1;i1<n;i1++) |
---|
368 | { |
---|
369 | if(pEqualPolys(e->m[i0],e->m[i1])) |
---|
370 | { |
---|
371 | (*m)[i0]+=(*m)[i1]; |
---|
372 | (*m)[i1]=0; |
---|
373 | } |
---|
374 | else |
---|
375 | { |
---|
376 | if(e->m[i0]==NULL&&!nGreaterZero(pGetCoeff(e->m[i1]))|| |
---|
377 | e->m[i1]==NULL&& |
---|
378 | (nGreaterZero(pGetCoeff(e->m[i0]))||pNext(e->m[i0])!=NULL)|| |
---|
379 | e->m[i0]!=NULL&&e->m[i1]!=NULL&& |
---|
380 | (pNext(e->m[i0])!=NULL&&pNext(e->m[i1])==NULL|| |
---|
381 | pNext(e->m[i0])==NULL&&pNext(e->m[i1])==NULL&& |
---|
382 | nGreater(pGetCoeff(e->m[i0]),pGetCoeff(e->m[i1])))) |
---|
383 | { |
---|
384 | poly e1=e->m[i0]; |
---|
385 | e->m[i0]=e->m[i1]; |
---|
386 | e->m[i1]=e1; |
---|
387 | int m1=(*m)[i0]; |
---|
388 | (*m)[i0]=(*m)[i1]; |
---|
389 | (*m)[i1]=m1; |
---|
390 | } |
---|
391 | } |
---|
392 | } |
---|
393 | } |
---|
394 | |
---|
395 | int n0=0; |
---|
396 | for(int i=0;i<n;i++) |
---|
397 | if((*m)[i]>0) |
---|
398 | n0++; |
---|
399 | |
---|
400 | ideal e0=idInit(n0,1); |
---|
401 | intvec *m0=new intvec(n0); |
---|
402 | |
---|
403 | for(int i=0,i0=0;i<n;i++) |
---|
404 | if((*m)[i]>0) |
---|
405 | { |
---|
406 | e0->m[i0]=e->m[i]; |
---|
407 | e->m[i]=NULL; |
---|
408 | (*m0)[i0]=(*m)[i]; |
---|
409 | i0++; |
---|
410 | } |
---|
411 | |
---|
412 | idDelete(&e); |
---|
413 | delete(m); |
---|
414 | |
---|
415 | l->Init(2); |
---|
416 | l->m[0].rtyp=IDEAL_CMD; |
---|
417 | l->m[0].data=e0; |
---|
418 | l->m[1].rtyp=INTVEC_CMD; |
---|
419 | l->m[1].data=m0; |
---|
420 | |
---|
421 | return(l); |
---|
422 | } |
---|
423 | |
---|
424 | |
---|
425 | BOOLEAN evEigenvals(leftv res,leftv h) |
---|
426 | { |
---|
427 | if(currRingHdl) |
---|
428 | { |
---|
429 | if(h&&h->Typ()==MATRIX_CMD) |
---|
430 | { |
---|
431 | matrix M=(matrix)h->Data(); |
---|
432 | res->rtyp=LIST_CMD; |
---|
433 | res->data=(void *)evEigenvals(mpCopy(M)); |
---|
434 | return FALSE; |
---|
435 | } |
---|
436 | WerrorS("<matrix> expected"); |
---|
437 | return TRUE; |
---|
438 | } |
---|
439 | WerrorS("no ring active"); |
---|
440 | return TRUE; |
---|
441 | } |
---|
442 | |
---|