1 | //GMG last modified: 04/25/2000 |
---|
2 | ////////////////////////////////////////////////////////////////////////////// |
---|
3 | version="$Id: linalg.lib,v 1.37 2005-04-28 09:22:15 Singular Exp $"; |
---|
4 | category="Linear Algebra"; |
---|
5 | info=" |
---|
6 | LIBRARY: linalg.lib Algorithmic Linear Algebra |
---|
7 | AUTHORS: Ivor Saynisch (ivs@math.tu-cottbus.de) |
---|
8 | @* Mathias Schulze (mschulze@mathematik.uni-kl.de) |
---|
9 | |
---|
10 | PROCEDURES: |
---|
11 | inverse(A); matrix, the inverse of A |
---|
12 | inverse_B(A); list(matrix Inv,poly p),Inv*A=p*En ( using busadj(A) ) |
---|
13 | inverse_L(A); list(matrix Inv,poly p),Inv*A=p*En ( using lift ) |
---|
14 | sym_gauss(A); symmetric gaussian algorithm |
---|
15 | orthogonalize(A); Gram-Schmidt orthogonalization |
---|
16 | diag_test(A); test whether A can be diagnolized |
---|
17 | busadj(A); coefficients of Adj(E*t-A) and coefficients of det(E*t-A) |
---|
18 | charpoly(A,v); characteristic polynomial of A ( using busadj(A) ) |
---|
19 | adjoint(A); adjoint of A ( using busadj(A) ) |
---|
20 | det_B(A); determinant of A ( using busadj(A) ) |
---|
21 | gaussred(A); gaussian reduction: P*A=U*S, S a row reduced form of A |
---|
22 | gaussred_pivot(A); gaussian reduction: P*A=U*S, uses row pivoting |
---|
23 | gauss_nf(A); gaussian normal form of A |
---|
24 | mat_rk(A); rank of constant matrix A |
---|
25 | U_D_O(A); P*A=U*D*O, P,D,U,O=permutaion,diag,lower-,upper-triang |
---|
26 | pos_def(A,i); test symmetric matrix for positive definiteness |
---|
27 | hessenberg(M); Hessenberg form of M |
---|
28 | eigenvals(M); eigenvalues with multiplicities of M |
---|
29 | minipoly(M); minimal polynomial of M |
---|
30 | spnf(sp); normal form of spectrum sp |
---|
31 | spprint(sp); print spectrum sp |
---|
32 | jordan(M); Jordan data of M |
---|
33 | jordanbasis(M); Jordan basis and weight filtration of M |
---|
34 | jordanmatrix(jd); Jordan matrix with Jordan data jd |
---|
35 | jordannf(M); Jordan normal form of M |
---|
36 | "; |
---|
37 | |
---|
38 | LIB "matrix.lib"; |
---|
39 | LIB "ring.lib"; |
---|
40 | LIB "elim.lib"; |
---|
41 | LIB "general.lib"; |
---|
42 | ////////////////////////////////////////////////////////////////////////////// |
---|
43 | // help functions |
---|
44 | ////////////////////////////////////////////////////////////////////////////// |
---|
45 | static proc const_mat(matrix A) |
---|
46 | "RETURN: 1 (0) if A is (is not) a constant matrix" |
---|
47 | { |
---|
48 | int i; |
---|
49 | int n=ncols(A); |
---|
50 | def BR=basering; |
---|
51 | def @R=changeord("dp,c",BR); |
---|
52 | setring @R; |
---|
53 | matrix A=fetch(BR,A); |
---|
54 | for(i=1;i<=n;i=i+1){ |
---|
55 | if(deg(lead(A)[i])>=1){ |
---|
56 | //"input is not a constant matrix"; |
---|
57 | kill @R; |
---|
58 | setring BR; |
---|
59 | return(0); |
---|
60 | } |
---|
61 | } |
---|
62 | kill @R; |
---|
63 | setring BR; |
---|
64 | return(1); |
---|
65 | } |
---|
66 | ////////////////////////////////////////////////////////////////////////////// |
---|
67 | static proc red(matrix A,int i,int j) |
---|
68 | "USAGE: red(A,i,j); A = constant matrix |
---|
69 | reduces column j with respect to A[i,i] and column i |
---|
70 | reduces row j with respect to A[i,i] and row i |
---|
71 | RETURN: matrix |
---|
72 | " |
---|
73 | { |
---|
74 | module m=module(A); |
---|
75 | |
---|
76 | if(A[i,i]==0){ |
---|
77 | m[i]=m[i]+m[j]; |
---|
78 | m=module(transpose(matrix(m))); |
---|
79 | m[i]=m[i]+m[j]; |
---|
80 | m=module(transpose(matrix(m))); |
---|
81 | } |
---|
82 | |
---|
83 | A=matrix(m); |
---|
84 | m[j]=m[j]-(A[i,j]/A[i,i])*m[i]; |
---|
85 | m=module(transpose(matrix(m))); |
---|
86 | m[j]=m[j]-(A[i,j]/A[i,i])*m[i]; |
---|
87 | m=module(transpose(matrix(m))); |
---|
88 | |
---|
89 | return(matrix(m)); |
---|
90 | } |
---|
91 | |
---|
92 | ////////////////////////////////////////////////////////////////////////////// |
---|
93 | proc inner_product(vector v1,vector v2) |
---|
94 | "RETURN: inner product <v1,v2> " |
---|
95 | { |
---|
96 | int k; |
---|
97 | if (nrows(v2)>nrows(v1)) { k=nrows(v2); } else { k=nrows(v1); } |
---|
98 | return ((transpose(matrix(v1,k,1))*matrix(v2,k,1))[1,1]); |
---|
99 | } |
---|
100 | |
---|
101 | ///////////////////////////////////////////////////////////////////////////// |
---|
102 | // user functions |
---|
103 | ///////////////////////////////////////////////////////////////////////////// |
---|
104 | |
---|
105 | proc inverse(matrix A, list #) |
---|
106 | "USAGE: inverse(A [,opt]); A a square matrix, opt integer |
---|
107 | RETURN: |
---|
108 | @format |
---|
109 | a matrix: |
---|
110 | - the inverse matrix of A, if A is invertible; |
---|
111 | - the 1x1 0-matrix if A is not invertible (in the polynomial ring!). |
---|
112 | There are the following options: |
---|
113 | - opt=0 or not given: heuristically best option from below |
---|
114 | - opt=1 : apply std to (transpose(E,A)), ordering (C,dp). |
---|
115 | - opt=2 : apply interred (transpose(E,A)), ordering (C,dp). |
---|
116 | - opt=3 : apply lift(A,E), ordering (C,dp). |
---|
117 | @end format |
---|
118 | NOTE: parameters and minpoly are allowed; opt=2 is only correct for |
---|
119 | matrices with entries in a field |
---|
120 | SEE ALSO: inverse_B, inverse_L |
---|
121 | EXAMPLE: example inverse; shows an example |
---|
122 | " |
---|
123 | { |
---|
124 | //--------------------------- initialization and check ------------------------ |
---|
125 | int ii,u,i,opt; |
---|
126 | matrix invA; |
---|
127 | int db = printlevel-voice+3; //used for comments |
---|
128 | def R=basering; |
---|
129 | string mp = string(minpoly); |
---|
130 | int n = nrows(A); |
---|
131 | module M = A; |
---|
132 | intvec v = option(get); //get options to reset it later |
---|
133 | if ( ncols(A)!=n ) |
---|
134 | { |
---|
135 | ERROR("// ** no square matrix"); |
---|
136 | } |
---|
137 | //----------------------- choose heurisitically best option ------------------ |
---|
138 | // This may change later, depending on improvements of the implemantation |
---|
139 | // at the monent we use if opt=0 or opt not given: |
---|
140 | // opt = 1 (std) for everything |
---|
141 | // opt = 2 (interred) for nothing, NOTE: interred is ok for constant matricea |
---|
142 | // opt = 3 (lift) for nothing |
---|
143 | // NOTE: interred is ok for constant matrices only (Beispiele am Ende der lib) |
---|
144 | if(size(#) != 0) {opt = #[1];} |
---|
145 | if(opt == 0) |
---|
146 | { |
---|
147 | if(npars(R) == 0) //no parameters |
---|
148 | { |
---|
149 | if( size(ideal(A-jet(A,0))) == 0 ) //constant matrix |
---|
150 | {opt = 1;} |
---|
151 | else |
---|
152 | {opt = 1;} |
---|
153 | } |
---|
154 | else {opt = 1;} |
---|
155 | } |
---|
156 | //------------------------- change ring if necessary ------------------------- |
---|
157 | if( ordstr(R) != "C,dp(nvars(R))" ) |
---|
158 | { |
---|
159 | u=1; |
---|
160 | def @R=changeord("C,dp",R); |
---|
161 | setring @R; |
---|
162 | module M = fetch(R,M); |
---|
163 | execute("minpoly="+mp+";"); |
---|
164 | } |
---|
165 | //----------------------------- opt=3: use lift ------------------------------ |
---|
166 | if( opt==3 ) |
---|
167 | { |
---|
168 | module D2; |
---|
169 | D2 = lift(M,freemodule(n)); |
---|
170 | if (size(ideal(D2))==0) |
---|
171 | { //catch error in lift |
---|
172 | dbprint(db,"// ** matrix is not invertible"); |
---|
173 | setring R; |
---|
174 | if (u==1) { kill @R;} |
---|
175 | return(invA); |
---|
176 | } |
---|
177 | } |
---|
178 | //-------------- opt = 1 resp. opt = 2: use std resp. interred -------------- |
---|
179 | if( opt==1 or opt==2 ) |
---|
180 | { |
---|
181 | option(redSB); |
---|
182 | module B = freemodule(n),M; |
---|
183 | if(opt == 2) |
---|
184 | { |
---|
185 | module D = interred(transpose(B)); |
---|
186 | D = transpose(simplify(D,1)); |
---|
187 | } |
---|
188 | if(opt == 1) |
---|
189 | { |
---|
190 | module D = std(transpose(B)); |
---|
191 | D = transpose(simplify(D,1)); |
---|
192 | } |
---|
193 | module D2 = D[1..n]; |
---|
194 | module D1 = D[n+1..2*n]; |
---|
195 | //----------------------- check if matrix is invertible ---------------------- |
---|
196 | for (ii=1; ii<=n; ii++) |
---|
197 | { |
---|
198 | if ( D1[ii] != gen(ii) ) |
---|
199 | { |
---|
200 | dbprint(db,"// ** matrix is not invertible"); |
---|
201 | i = 1; |
---|
202 | break; |
---|
203 | } |
---|
204 | } |
---|
205 | } |
---|
206 | option(set,v); |
---|
207 | //------------------ return to basering and return result --------------------- |
---|
208 | if ( u==1 ) |
---|
209 | { |
---|
210 | setring R; |
---|
211 | module D2 = fetch(@R,D2); |
---|
212 | if( opt==1 or opt==2 ) |
---|
213 | { module D1 = fetch(@R,D1);} |
---|
214 | kill @R; |
---|
215 | } |
---|
216 | if( i==1 ) { return(invA); } //matrix not invetible |
---|
217 | else { return(matrix(D2)); } //matrix invertible with inverse D2 |
---|
218 | |
---|
219 | } |
---|
220 | example |
---|
221 | { "EXAMPLE:"; echo = 2; |
---|
222 | ring r=0,(x,y,z),lp; |
---|
223 | matrix A[3][3]= |
---|
224 | 1,4,3, |
---|
225 | 1,5,7, |
---|
226 | 0,4,17; |
---|
227 | print(inverse(A));""; |
---|
228 | matrix B[3][3]= |
---|
229 | y+1, x+y, y, |
---|
230 | z, z+1, z, |
---|
231 | y+z+2,x+y+z+2,y+z+1; |
---|
232 | print(inverse(B)); |
---|
233 | print(B*inverse(B)); |
---|
234 | } |
---|
235 | |
---|
236 | ////////////////////////////////////////////////////////////////////////////// |
---|
237 | proc sym_gauss(matrix A) |
---|
238 | "USAGE: sym_gauss(A); A = symmetric matrix |
---|
239 | RETURN: matrix, diagonalisation with symmetric gauss algorithm |
---|
240 | EXAMPLE: example sym_gauss; shows an example" |
---|
241 | { |
---|
242 | int i,j; |
---|
243 | int n=nrows(A); |
---|
244 | |
---|
245 | if (ncols(A)!=n){ |
---|
246 | "// ** input is not a square matrix";; |
---|
247 | return(A); |
---|
248 | } |
---|
249 | |
---|
250 | if(!const_mat(A)){ |
---|
251 | "// ** input is not a constant matrix"; |
---|
252 | return(A); |
---|
253 | } |
---|
254 | |
---|
255 | if(deg(std(A-transpose(A))[1])!=-1){ |
---|
256 | "// ** input is not a symmetric matrix"; |
---|
257 | return(A); |
---|
258 | } |
---|
259 | |
---|
260 | for(i=1; i<n; i++){ |
---|
261 | for(j=i+1; j<=n; j++){ |
---|
262 | if(A[i,j]!=0){ A=red(A,i,j); } |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | return(A); |
---|
267 | } |
---|
268 | example |
---|
269 | {"EXAMPLE:"; echo = 2; |
---|
270 | ring r=0,(x),lp; |
---|
271 | matrix A[2][2]=1,4,4,15; |
---|
272 | print(A); |
---|
273 | print(sym_gauss(A)); |
---|
274 | } |
---|
275 | |
---|
276 | ////////////////////////////////////////////////////////////////////////////// |
---|
277 | proc orthogonalize(matrix A) |
---|
278 | "USAGE: orthogonalize(A); A = constant matrix |
---|
279 | RETURN: matrix, orthogonal basis of the colum space of A |
---|
280 | EXAMPLE: example orthogonalize; shows an example " |
---|
281 | { |
---|
282 | int i,j; |
---|
283 | int n=ncols(A); |
---|
284 | poly k; |
---|
285 | |
---|
286 | if(!const_mat(A)){ |
---|
287 | "// ** input is not a constant matrix"; |
---|
288 | matrix B; |
---|
289 | return(B); |
---|
290 | } |
---|
291 | |
---|
292 | module B=module(interred(A)); |
---|
293 | |
---|
294 | for(i=1;i<=n;i=i+1) { |
---|
295 | for(j=1;j<i;j=j+1) { |
---|
296 | k=inner_product(B[j],B[j]); |
---|
297 | if (k==0) { "Error: vector of length zero"; return(matrix(B)); } |
---|
298 | B[i]=B[i]-(inner_product(B[i],B[j])/k)*B[j]; |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | return(matrix(B)); |
---|
303 | } |
---|
304 | example |
---|
305 | { "EXAMPLE:"; echo = 2; |
---|
306 | ring r=0,(x),lp; |
---|
307 | matrix A[4][4]=5,6,12,4,7,3,2,6,12,1,1,2,6,4,2,10; |
---|
308 | print(A); |
---|
309 | print(orthogonalize(A)); |
---|
310 | } |
---|
311 | |
---|
312 | //////////////////////////////////////////////////////////////////////////// |
---|
313 | proc diag_test(matrix A) |
---|
314 | "USAGE: diag_test(A); A = const square matrix |
---|
315 | RETURN: int, 1 if A is diagonalisable, 0 if not |
---|
316 | -1 no statement is possible, since A does not split. |
---|
317 | NOTE: The test works only for split matrices, i.e if eigenvalues of A |
---|
318 | are in the ground field. |
---|
319 | Does not work with parameters (uses factorize,gcd). |
---|
320 | EXAMPLE: example diag_test; shows an example" |
---|
321 | { |
---|
322 | int i,j; |
---|
323 | int n = nrows(A); |
---|
324 | string mp = string(minpoly); |
---|
325 | string cs = charstr(basering); |
---|
326 | int np=0; |
---|
327 | |
---|
328 | if(ncols(A) != n) { |
---|
329 | "// input is not a square matrix"; |
---|
330 | return(-1); |
---|
331 | } |
---|
332 | |
---|
333 | if(!const_mat(A)){ |
---|
334 | "// input is not a constant matrix"; |
---|
335 | return(-1); |
---|
336 | } |
---|
337 | |
---|
338 | //Parameterring wegen factorize nicht erlaubt |
---|
339 | for(i=1;i<size(cs);i=i+1){ |
---|
340 | if(cs[i]==","){np=np+1;} //Anzahl der Parameter |
---|
341 | } |
---|
342 | if(np>0){ |
---|
343 | "// rings with parameters not allowed"; |
---|
344 | return(-1); |
---|
345 | } |
---|
346 | |
---|
347 | //speichern des aktuellen Rings |
---|
348 | def BR=basering; |
---|
349 | //setze R[t] |
---|
350 | execute("ring rt=("+charstr(basering)+"),(@t,"+varstr(basering)+"),lp;"); |
---|
351 | execute("minpoly="+mp+";"); |
---|
352 | matrix A=imap(BR,A); |
---|
353 | |
---|
354 | intvec z; |
---|
355 | intvec s; |
---|
356 | poly X; //characteristisches Polynom |
---|
357 | poly dXdt; //Ableitung von X nach t |
---|
358 | ideal g; //ggT(X,dXdt) |
---|
359 | poly b; //Komponente der Busadjunkten-Matrix |
---|
360 | matrix E[n][n]; //Einheitsmatrix |
---|
361 | |
---|
362 | E=E+1; |
---|
363 | A=E*@t-A; |
---|
364 | X=det(A); |
---|
365 | |
---|
366 | matrix Xfactors=matrix(factorize(X,1)); //zerfaellt die Matrtix ? |
---|
367 | int nf=ncols(Xfactors); |
---|
368 | |
---|
369 | for(i=1;i<=nf;i++){ |
---|
370 | if(lead(Xfactors[1,i])>=@t^2){ |
---|
371 | //" matrix does not split"; |
---|
372 | setring BR; |
---|
373 | return(-1); |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | dXdt=diff(X,@t); |
---|
378 | g=std(ideal(gcd(X,dXdt))); |
---|
379 | |
---|
380 | //Busadjunkte |
---|
381 | z=2..n; |
---|
382 | for(i=1;i<=n;i++){ |
---|
383 | s=2..n; |
---|
384 | for(j=1;j<=n;j++){ |
---|
385 | b=det(submat(A,z,s)); |
---|
386 | |
---|
387 | if(0!=reduce(b,g)){ |
---|
388 | //" matrix not diagonalizable"; |
---|
389 | setring BR; |
---|
390 | return(0); |
---|
391 | } |
---|
392 | |
---|
393 | s[j]=j; |
---|
394 | } |
---|
395 | z[i]=i; |
---|
396 | } |
---|
397 | |
---|
398 | //"Die Matrix ist diagonalisierbar"; |
---|
399 | setring BR; |
---|
400 | return(1); |
---|
401 | } |
---|
402 | example |
---|
403 | { "EXAMPLE:"; echo = 2; |
---|
404 | ring r=0,(x),dp; |
---|
405 | matrix A[4][4]=6,0,0,0,0,0,6,0,0,6,0,0,0,0,0,6; |
---|
406 | print(A); |
---|
407 | diag_test(A); |
---|
408 | } |
---|
409 | |
---|
410 | ////////////////////////////////////////////////////////////////////////////// |
---|
411 | proc busadj(matrix A) |
---|
412 | "USAGE: busadj(A); A = square matrix (of size nxn) |
---|
413 | RETURN: list L: |
---|
414 | @format |
---|
415 | L[1] contains the (n+1) coefficients of the characteristic |
---|
416 | polynomial X of A, i.e. |
---|
417 | X = L[1][1]+..+L[1][k]*t^(k-1)+..+(L[1][n+1])*t^n |
---|
418 | L[2] contains the n (nxn)-matrices Hk which are the coefficients of |
---|
419 | the busadjoint bA = adjoint(E*t-A) of A, i.e. |
---|
420 | bA = (Hn-1)*t^(n-1)+...+Hk*t^k+...+H0, ( Hk=L[2][k+1] ) |
---|
421 | @end format |
---|
422 | EXAMPLE: example busadj; shows an example" |
---|
423 | { |
---|
424 | int k; |
---|
425 | int n = nrows(A); |
---|
426 | matrix E = unitmat(n); |
---|
427 | matrix H[n][n]; |
---|
428 | matrix B[n][n]; |
---|
429 | list bA, X, L; |
---|
430 | poly a; |
---|
431 | |
---|
432 | if(ncols(A) != n) { |
---|
433 | "input is not a square matrix"; |
---|
434 | return(L); |
---|
435 | } |
---|
436 | |
---|
437 | bA = E; |
---|
438 | X[1] = 1; |
---|
439 | for(k=1; k<n; k++){ |
---|
440 | B = A*bA[1]; //bA[1] is the last H |
---|
441 | a = -trace(B)/k; |
---|
442 | H = B+a*E; |
---|
443 | bA = insert(bA,H); |
---|
444 | X = insert(X,a); |
---|
445 | } |
---|
446 | B = A*bA[1]; |
---|
447 | a = -trace(B)/n; |
---|
448 | X = insert(X,a); |
---|
449 | |
---|
450 | L = insert(L,bA); |
---|
451 | L = insert(L,X); |
---|
452 | return(L); |
---|
453 | } |
---|
454 | example |
---|
455 | { "EXAMPLE"; echo = 2; |
---|
456 | ring r = 0,(t,x),lp; |
---|
457 | matrix A[2][2] = 1,x2,x,x2+3x; |
---|
458 | print(A); |
---|
459 | list L = busadj(A); |
---|
460 | poly X = L[1][1]+L[1][2]*t+L[1][3]*t2; X; |
---|
461 | matrix bA[2][2] = L[2][1]+L[2][2]*t; |
---|
462 | print(bA); //the busadjoint of A; |
---|
463 | print(bA*(t*unitmat(2)-A)); |
---|
464 | } |
---|
465 | |
---|
466 | ////////////////////////////////////////////////////////////////////////////// |
---|
467 | proc charpoly(matrix A, list #) |
---|
468 | "USAGE: charpoly(A[,v]); A square matrix, v string, name of a variable |
---|
469 | RETURN: poly, the characteristic polynomial det(E*v-A) |
---|
470 | (default: v=name of last variable) |
---|
471 | NOTE: A must be independent of the variable v. The computation uses det. |
---|
472 | If printlevel>0, det(E*v-A) is diplayed recursively. |
---|
473 | EXAMPLE: example charpoly; shows an example" |
---|
474 | { |
---|
475 | int n = nrows(A); |
---|
476 | int z = nvars(basering); |
---|
477 | int i,j; |
---|
478 | string v; |
---|
479 | poly X; |
---|
480 | if(ncols(A) != n) |
---|
481 | { |
---|
482 | "// input is not a square matrix"; |
---|
483 | return(X); |
---|
484 | } |
---|
485 | //---------------------- test for correct variable ------------------------- |
---|
486 | if( size(#)==0 ){ |
---|
487 | #[1] = varstr(z); |
---|
488 | } |
---|
489 | if( typeof(#[1]) == "string") { v = #[1]; } |
---|
490 | else |
---|
491 | { |
---|
492 | "// 2nd argument must be a name of a variable not contained in the matrix"; |
---|
493 | return(X); |
---|
494 | } |
---|
495 | j=-1; |
---|
496 | for(i=1; i<=z; i++) |
---|
497 | { |
---|
498 | if(varstr(i)==v){j=i;} |
---|
499 | } |
---|
500 | if(j==-1) |
---|
501 | { |
---|
502 | "// "+v+" is not a variable in the basering"; |
---|
503 | return(X); |
---|
504 | } |
---|
505 | if ( size(select1(module(A),j)) != 0 ) |
---|
506 | { |
---|
507 | "// matrix must not contain the variable "+v; |
---|
508 | "// change to a ring with an extra variable, if necessary." |
---|
509 | return(X); |
---|
510 | } |
---|
511 | //-------------------------- compute charpoly ------------------------------ |
---|
512 | X = det(var(j)*unitmat(n)-A); |
---|
513 | if( printlevel-voice+2 >0) { showrecursive(X,var(j));} |
---|
514 | return(X); |
---|
515 | } |
---|
516 | example |
---|
517 | { "EXAMPLE"; echo=2; |
---|
518 | ring r=0,(x,t),dp; |
---|
519 | matrix A[3][3]=1,x2,x,x2,6,4,x,4,1; |
---|
520 | print(A); |
---|
521 | charpoly(A,"t"); |
---|
522 | } |
---|
523 | |
---|
524 | ////////////////////////////////////////////////////////////////////////////// |
---|
525 | proc charpoly_B(matrix A, list #) |
---|
526 | "USAGE: charpoly_B(A[,v]); A square matrix, v string, name of a variable |
---|
527 | RETURN: poly, the characteristic polynomial det(E*v-A) |
---|
528 | (default: v=name of last variable) |
---|
529 | NOTE: A must be constant in the variable v. The computation uses busadj(A). |
---|
530 | EXAMPLE: example charpoly_B; shows an example" |
---|
531 | { |
---|
532 | int i,j; |
---|
533 | string s,v; |
---|
534 | list L; |
---|
535 | int n = nrows(A); |
---|
536 | poly X = 0; |
---|
537 | def BR = basering; |
---|
538 | string mp = string(minpoly); |
---|
539 | |
---|
540 | if(ncols(A) != n){ |
---|
541 | "// input is not a square matrix"; |
---|
542 | return(X); |
---|
543 | } |
---|
544 | |
---|
545 | //test for correct variable |
---|
546 | if( size(#)==0 ){ |
---|
547 | #[1] = varstr(nvars(BR)); |
---|
548 | } |
---|
549 | if( typeof(#[1]) == "string"){ |
---|
550 | v = #[1]; |
---|
551 | } |
---|
552 | else{ |
---|
553 | "// 2nd argument must be a name of a variable not contained in the matrix"; |
---|
554 | return(X); |
---|
555 | } |
---|
556 | |
---|
557 | j=-1; |
---|
558 | for(i=1; i<=nvars(BR); i++){ |
---|
559 | if(varstr(i)==v){j=i;} |
---|
560 | } |
---|
561 | if(j==-1){ |
---|
562 | "// "+v+" is not a variable in the basering"; |
---|
563 | return(X); |
---|
564 | } |
---|
565 | |
---|
566 | //var can not be in A |
---|
567 | s="Wp("; |
---|
568 | for( i=1; i<=nvars(BR); i++ ){ |
---|
569 | if(i!=j){ s=s+"0";} |
---|
570 | else{ s=s+"1";} |
---|
571 | if( i<nvars(BR)) {s=s+",";} |
---|
572 | } |
---|
573 | s=s+")"; |
---|
574 | |
---|
575 | def @R=changeord(s); |
---|
576 | setring @R; |
---|
577 | execute("minpoly="+mp+";"); |
---|
578 | matrix A = imap(BR,A); |
---|
579 | for(i=1; i<=n; i++){ |
---|
580 | if(deg(lead(A)[i])>=1){ |
---|
581 | "// matrix must not contain the variable "+v; |
---|
582 | kill @R; |
---|
583 | setring BR; |
---|
584 | return(X); |
---|
585 | } |
---|
586 | } |
---|
587 | |
---|
588 | //get coefficients and build the char. poly |
---|
589 | kill @R; |
---|
590 | setring BR; |
---|
591 | L = busadj(A); |
---|
592 | for(i=1; i<=n+1; i++){ |
---|
593 | execute("X=X+L[1][i]*"+v+"^"+string(i-1)+";"); |
---|
594 | } |
---|
595 | |
---|
596 | return(X); |
---|
597 | } |
---|
598 | example |
---|
599 | { "EXAMPLE"; echo=2; |
---|
600 | ring r=0,(x,t),dp; |
---|
601 | matrix A[3][3]=1,x2,x,x2,6,4,x,4,1; |
---|
602 | print(A); |
---|
603 | charpoly_B(A,"t"); |
---|
604 | } |
---|
605 | |
---|
606 | ////////////////////////////////////////////////////////////////////////////// |
---|
607 | proc adjoint(matrix A) |
---|
608 | "USAGE: adjoint(A); A = square matrix |
---|
609 | RETURN: adjoint matrix of A, i.e. Adj*A=det(A)*E |
---|
610 | NOTE: computation uses busadj(A) |
---|
611 | EXAMPLE: example adjoint; shows an example" |
---|
612 | { |
---|
613 | int n=nrows(A); |
---|
614 | matrix Adj[n][n]; |
---|
615 | list L; |
---|
616 | |
---|
617 | if(ncols(A) != n) { |
---|
618 | "// input is not a square matrix"; |
---|
619 | return(Adj); |
---|
620 | } |
---|
621 | |
---|
622 | L = busadj(A); |
---|
623 | Adj= (-1)^(n-1)*L[2][1]; |
---|
624 | return(Adj); |
---|
625 | |
---|
626 | } |
---|
627 | example |
---|
628 | { "EXAMPLE"; echo=2; |
---|
629 | ring r=0,(t,x),lp; |
---|
630 | matrix A[2][2]=1,x2,x,x2+3x; |
---|
631 | print(A); |
---|
632 | matrix Adj[2][2]=adjoint(A); |
---|
633 | print(Adj); //Adj*A=det(A)*E |
---|
634 | print(Adj*A); |
---|
635 | } |
---|
636 | |
---|
637 | ////////////////////////////////////////////////////////////////////////////// |
---|
638 | proc inverse_B(matrix A) |
---|
639 | "USAGE: inverse_B(A); A = square matrix |
---|
640 | RETURN: list Inv with |
---|
641 | - Inv[1] = matrix I and |
---|
642 | - Inv[2] = poly p |
---|
643 | such that I*A = unitmat(n)*p; |
---|
644 | NOTE: p=1 if 1/det(A) is computable and p=det(A) if not; |
---|
645 | the computation uses busadj. |
---|
646 | SEE ALSO: inverse, inverse_L |
---|
647 | EXAMPLE: example inverse_B; shows an example" |
---|
648 | { |
---|
649 | int i; |
---|
650 | int n=nrows(A); |
---|
651 | matrix I[n][n]; |
---|
652 | poly factor; |
---|
653 | list L; |
---|
654 | list Inv; |
---|
655 | |
---|
656 | if(ncols(A) != n) { |
---|
657 | "input is not a square matrix"; |
---|
658 | return(I); |
---|
659 | } |
---|
660 | |
---|
661 | L=busadj(A); |
---|
662 | I=module(-L[2][1]); //+-Adj(A) |
---|
663 | |
---|
664 | if(reduce(1,std(L[1][1]))==0){ |
---|
665 | I=I*lift(L[1][1],1)[1][1]; |
---|
666 | factor=1; |
---|
667 | } |
---|
668 | else{ factor=L[1][1];} //=+-det(A) or 1 |
---|
669 | Inv=insert(Inv,factor); |
---|
670 | Inv=insert(Inv,matrix(I)); |
---|
671 | |
---|
672 | return(Inv); |
---|
673 | } |
---|
674 | example |
---|
675 | { "EXAMPLE"; echo=2; |
---|
676 | ring r=0,(x,y),lp; |
---|
677 | matrix A[3][3]=x,y,1,1,x2,y,x,6,0; |
---|
678 | print(A); |
---|
679 | list Inv=inverse_B(A); |
---|
680 | print(Inv[1]); |
---|
681 | print(Inv[2]); |
---|
682 | print(Inv[1]*A); |
---|
683 | } |
---|
684 | |
---|
685 | ////////////////////////////////////////////////////////////////////////////// |
---|
686 | proc det_B(matrix A) |
---|
687 | "USAGE: det_B(A); A any matrix |
---|
688 | RETURN: returns the determinant of A |
---|
689 | NOTE: the computation uses the busadj algorithm |
---|
690 | EXAMPLE: example det_B; shows an example" |
---|
691 | { |
---|
692 | int n=nrows(A); |
---|
693 | list L; |
---|
694 | |
---|
695 | if(ncols(A) != n){ return(0);} |
---|
696 | |
---|
697 | L=busadj(A); |
---|
698 | return((-1)^n*L[1][1]); |
---|
699 | } |
---|
700 | example |
---|
701 | { "EXAMPLE"; echo=2; |
---|
702 | ring r=0,(x),dp; |
---|
703 | matrix A[10][10]=random(2,10,10)+unitmat(10)*x; |
---|
704 | print(A); |
---|
705 | det_B(A); |
---|
706 | } |
---|
707 | |
---|
708 | ////////////////////////////////////////////////////////////////////////////// |
---|
709 | proc inverse_L(matrix A) |
---|
710 | "USAGE: inverse_L(A); A = square matrix |
---|
711 | RETURN: list Inv representing a left inverse of A, i.e |
---|
712 | - Inv[1] = matrix I and |
---|
713 | - Inv[2] = poly p |
---|
714 | such that I*A = unitmat(n)*p; |
---|
715 | NOTE: p=1 if 1/det(A) is computable and p=det(A) if not; |
---|
716 | the computation computes first det(A) and then uses lift |
---|
717 | SEE ALSO: inverse, inverse_B |
---|
718 | EXAMPLE: example inverse_L; shows an example" |
---|
719 | { |
---|
720 | int n=nrows(A); |
---|
721 | matrix I; |
---|
722 | matrix E[n][n]=unitmat(n); |
---|
723 | poly factor; |
---|
724 | poly d=1; |
---|
725 | list Inv; |
---|
726 | |
---|
727 | if (ncols(A)!=n){ |
---|
728 | "// input is not a square matrix"; |
---|
729 | return(I); |
---|
730 | } |
---|
731 | |
---|
732 | d=det(A); |
---|
733 | if(d==0){ |
---|
734 | "// matrix is not invertible"; |
---|
735 | return(Inv); |
---|
736 | } |
---|
737 | |
---|
738 | // test if 1/det(A) exists |
---|
739 | if(reduce(1,std(d))!=0){ E=E*d;} |
---|
740 | |
---|
741 | I=lift(A,E); |
---|
742 | if(I==unitmat(n)-unitmat(n)){ //catch error in lift |
---|
743 | "// matrix is not invertible"; |
---|
744 | return(Inv); |
---|
745 | } |
---|
746 | |
---|
747 | factor=d; //=det(A) or 1 |
---|
748 | Inv=insert(Inv,factor); |
---|
749 | Inv=insert(Inv,I); |
---|
750 | |
---|
751 | return(Inv); |
---|
752 | } |
---|
753 | example |
---|
754 | { "EXAMPLE"; echo=2; |
---|
755 | ring r=0,(x,y),lp; |
---|
756 | matrix A[3][3]=x,y,1,1,x2,y,x,6,0; |
---|
757 | print(A); |
---|
758 | list Inv=inverse_L(A); |
---|
759 | print(Inv[1]); |
---|
760 | print(Inv[2]); |
---|
761 | print(Inv[1]*A); |
---|
762 | } |
---|
763 | |
---|
764 | ////////////////////////////////////////////////////////////////////////////// |
---|
765 | proc gaussred(matrix A) |
---|
766 | "USAGE: gaussred(A); A any constant matrix |
---|
767 | RETURN: list Z: Z[1]=P , Z[2]=U , Z[3]=S , Z[4]=rank(A) |
---|
768 | gives a row reduced matrix S, a permutation matrix P and a |
---|
769 | normalized lower triangular matrix U, with P*A=U*S |
---|
770 | NOTE: This procedure is designed for teaching purposes mainly. |
---|
771 | The straight forward implementation in the interpreted library |
---|
772 | is not very efficient (no standard basis computation). |
---|
773 | EXAMPLE: example gaussred; shows an example" |
---|
774 | { |
---|
775 | int i,j,l,k,jp,rang; |
---|
776 | poly c,pivo; |
---|
777 | list Z; |
---|
778 | int n = nrows(A); |
---|
779 | int m = ncols(A); |
---|
780 | int mr= n; //max. rang |
---|
781 | matrix P[n][n] = unitmat(n); |
---|
782 | matrix U[n][n] = P; |
---|
783 | |
---|
784 | if(!const_mat(A)){ |
---|
785 | "// input is not a constant matrix"; |
---|
786 | return(Z); |
---|
787 | } |
---|
788 | |
---|
789 | if(n>m){mr=m;} //max. rang |
---|
790 | |
---|
791 | for(i=1;i<=mr;i=i+1){ |
---|
792 | if((i+k)>m){break}; |
---|
793 | |
---|
794 | //Test: Diagonalelement=0 |
---|
795 | if(A[i,i+k]==0){ |
---|
796 | jp=i;pivo=0; |
---|
797 | for(j=i+1;j<=n;j=j+1){ |
---|
798 | c=absValue(A[j,i+k]); |
---|
799 | if(pivo<c){ pivo=c;jp=j;} |
---|
800 | } |
---|
801 | if(jp != i){ //Zeilentausch |
---|
802 | for(j=1;j<=m;j=j+1){ //Zeilentausch in A (und U) (i-te mit jp-ter) |
---|
803 | c=A[i,j]; |
---|
804 | A[i,j]=A[jp,j]; |
---|
805 | A[jp,j]=c; |
---|
806 | } |
---|
807 | for(j=1;j<=n;j=j+1){ //Zeilentausch in P |
---|
808 | c=P[i,j]; |
---|
809 | P[i,j]=P[jp,j]; |
---|
810 | P[jp,j]=c; |
---|
811 | } |
---|
812 | } |
---|
813 | if(pivo==0){k++;continue;} //eine von selbst auftauchende Stufe ! |
---|
814 | } //i sollte im naechsten Lauf nicht erhoeht sein |
---|
815 | |
---|
816 | //Eliminationsschritt |
---|
817 | for(j=i+1;j<=n;j=j+1){ |
---|
818 | c=A[j,i+k]/A[i,i+k]; |
---|
819 | for(l=i+k+1;l<=m;l=l+1){ |
---|
820 | A[j,l]=A[j,l]-A[i,l]*c; |
---|
821 | } |
---|
822 | A[j,i+k]=0; // nur wichtig falls k>0 ist |
---|
823 | A[j,i]=c; // bildet U |
---|
824 | } |
---|
825 | rang=i; |
---|
826 | } |
---|
827 | |
---|
828 | for(i=1;i<=mr;i=i+1){ |
---|
829 | for(j=i+1;j<=n;j=j+1){ |
---|
830 | U[j,i]=A[j,i]; |
---|
831 | A[j,i]=0; |
---|
832 | } |
---|
833 | } |
---|
834 | |
---|
835 | Z=insert(Z,rang); |
---|
836 | Z=insert(Z,A); |
---|
837 | Z=insert(Z,U); |
---|
838 | Z=insert(Z,P); |
---|
839 | |
---|
840 | return(Z); |
---|
841 | } |
---|
842 | example |
---|
843 | { "EXAMPLE";echo=2; |
---|
844 | ring r=0,(x),dp; |
---|
845 | matrix A[5][4]=1,3,-1,4,2,5,-1,3,1,3,-1,4,0,4,-3,1,-3,1,-5,-2; |
---|
846 | print(A); |
---|
847 | list Z=gaussred(A); //construct P,U,S s.t. P*A=U*S |
---|
848 | print(Z[1]); //P |
---|
849 | print(Z[2]); //U |
---|
850 | print(Z[3]); //S |
---|
851 | print(Z[4]); //rank |
---|
852 | print(Z[1]*A); //P*A |
---|
853 | print(Z[2]*Z[3]); //U*S |
---|
854 | } |
---|
855 | |
---|
856 | ////////////////////////////////////////////////////////////////////////////// |
---|
857 | proc gaussred_pivot(matrix A) |
---|
858 | "USAGE: gaussred_pivot(A); A any constant matrix |
---|
859 | RETURN: list Z: Z[1]=P , Z[2]=U , Z[3]=S , Z[4]=rank(A) |
---|
860 | gives n row reduced matrix S, a permutation matrix P and a |
---|
861 | normalized lower triangular matrix U, with P*A=U*S |
---|
862 | NOTE: with row pivoting |
---|
863 | EXAMPLE: example gaussred_pivot; shows an example" |
---|
864 | { |
---|
865 | int i,j,l,k,jp,rang; |
---|
866 | poly c,pivo; |
---|
867 | list Z; |
---|
868 | int n=nrows(A); |
---|
869 | int m=ncols(A); |
---|
870 | int mr=n; //max. rang |
---|
871 | matrix P[n][n]=unitmat(n); |
---|
872 | matrix U[n][n]=P; |
---|
873 | |
---|
874 | if(!const_mat(A)){ |
---|
875 | "// input is not a constant matrix"; |
---|
876 | return(Z); |
---|
877 | } |
---|
878 | |
---|
879 | if(n>m){mr=m;} //max. rang |
---|
880 | |
---|
881 | for(i=1;i<=mr;i=i+1){ |
---|
882 | if((i+k)>m){break}; |
---|
883 | |
---|
884 | //Pivotisierung |
---|
885 | pivo=absValue(A[i,i+k]);jp=i; |
---|
886 | for(j=i+1;j<=n;j=j+1){ |
---|
887 | c=absValue(A[j,i+k]); |
---|
888 | if(pivo<c){ pivo=c;jp=j;} |
---|
889 | } |
---|
890 | if(jp != i){ //Zeilentausch |
---|
891 | for(j=1;j<=m;j=j+1){ //Zeilentausch in A (und U) (i-te mit jp-ter) |
---|
892 | c=A[i,j]; |
---|
893 | A[i,j]=A[jp,j]; |
---|
894 | A[jp,j]=c; |
---|
895 | } |
---|
896 | for(j=1;j<=n;j=j+1){ //Zeilentausch in P |
---|
897 | c=P[i,j]; |
---|
898 | P[i,j]=P[jp,j]; |
---|
899 | P[jp,j]=c; |
---|
900 | } |
---|
901 | } |
---|
902 | if(pivo==0){k++;continue;} //eine von selbst auftauchende Stufe ! |
---|
903 | //i sollte im naechsten Lauf nicht erhoeht sein |
---|
904 | //Eliminationsschritt |
---|
905 | for(j=i+1;j<=n;j=j+1){ |
---|
906 | c=A[j,i+k]/A[i,i+k]; |
---|
907 | for(l=i+k+1;l<=m;l=l+1){ |
---|
908 | A[j,l]=A[j,l]-A[i,l]*c; |
---|
909 | } |
---|
910 | A[j,i+k]=0; // nur wichtig falls k>0 ist |
---|
911 | A[j,i]=c; // bildet U |
---|
912 | } |
---|
913 | rang=i; |
---|
914 | } |
---|
915 | |
---|
916 | for(i=1;i<=mr;i=i+1){ |
---|
917 | for(j=i+1;j<=n;j=j+1){ |
---|
918 | U[j,i]=A[j,i]; |
---|
919 | A[j,i]=0; |
---|
920 | } |
---|
921 | } |
---|
922 | |
---|
923 | Z=insert(Z,rang); |
---|
924 | Z=insert(Z,A); |
---|
925 | Z=insert(Z,U); |
---|
926 | Z=insert(Z,P); |
---|
927 | |
---|
928 | return(Z); |
---|
929 | } |
---|
930 | example |
---|
931 | { "EXAMPLE";echo=2; |
---|
932 | ring r=0,(x),dp; |
---|
933 | matrix A[5][4] = 1, 3,-1,4, |
---|
934 | 2, 5,-1,3, |
---|
935 | 1, 3,-1,4, |
---|
936 | 0, 4,-3,1, |
---|
937 | -3,1,-5,-2; |
---|
938 | list Z=gaussred_pivot(A); //construct P,U,S s.t. P*A=U*S |
---|
939 | print(Z[1]); //P |
---|
940 | print(Z[2]); //U |
---|
941 | print(Z[3]); //S |
---|
942 | print(Z[4]); //rank |
---|
943 | print(Z[1]*A); //P*A |
---|
944 | print(Z[2]*Z[3]); //U*S |
---|
945 | } |
---|
946 | |
---|
947 | ////////////////////////////////////////////////////////////////////////////// |
---|
948 | proc gauss_nf(matrix A) |
---|
949 | "USAGE: gauss_nf(A); A any constant matrix |
---|
950 | RETURN: matrix; gauss normal form of A (uses gaussred) |
---|
951 | EXAMPLE: example gauss_nf; shows an example" |
---|
952 | { |
---|
953 | list Z; |
---|
954 | if(!const_mat(A)){ |
---|
955 | "// input is not a constant matrix"; |
---|
956 | return(A); |
---|
957 | } |
---|
958 | Z = gaussred(A); |
---|
959 | return(Z[3]); |
---|
960 | } |
---|
961 | example |
---|
962 | { "EXAMPLE";echo=2; |
---|
963 | ring r = 0,(x),dp; |
---|
964 | matrix A[4][4] = 1,4,4,7,2,5,5,4,4,1,1,3,0,2,2,7; |
---|
965 | print(gauss_nf(A)); |
---|
966 | } |
---|
967 | |
---|
968 | ////////////////////////////////////////////////////////////////////////////// |
---|
969 | proc mat_rk(matrix A) |
---|
970 | "USAGE: mat_rk(A); A any constant matrix |
---|
971 | RETURN: int, rank of A |
---|
972 | EXAMPLE: example mat_rk; shows an example" |
---|
973 | { |
---|
974 | list Z; |
---|
975 | if(!const_mat(A)){ |
---|
976 | "// input is not a constant matrix"; |
---|
977 | return(-1); |
---|
978 | } |
---|
979 | Z = gaussred(A); |
---|
980 | return(Z[4]); |
---|
981 | } |
---|
982 | example |
---|
983 | { "EXAMPLE";echo=2; |
---|
984 | ring r = 0,(x),dp; |
---|
985 | matrix A[4][4] = 1,4,4,7,2,5,5,4,4,1,1,3,0,2,2,7; |
---|
986 | mat_rk(A); |
---|
987 | } |
---|
988 | |
---|
989 | ////////////////////////////////////////////////////////////////////////////// |
---|
990 | proc U_D_O(matrix A) |
---|
991 | "USAGE: U_D_O(A); constant invertible matrix A |
---|
992 | RETURN: list Z: Z[1]=P , Z[2]=U , Z[3]=D , Z[4]=O |
---|
993 | gives a permutation matrix P, |
---|
994 | a normalized lower triangular matrix U , |
---|
995 | a diagonal matrix D, and |
---|
996 | a normalized upper triangular matrix O |
---|
997 | with P*A=U*D*O |
---|
998 | NOTE: Z[1]=-1 means that A is not regular (proc uses gaussred) |
---|
999 | EXAMPLE: example U_D_O; shows an example" |
---|
1000 | { |
---|
1001 | int i,j; |
---|
1002 | list Z,L; |
---|
1003 | int n=nrows(A); |
---|
1004 | matrix O[n][n]=unitmat(n); |
---|
1005 | matrix D[n][n]; |
---|
1006 | |
---|
1007 | if (ncols(A)!=n){ |
---|
1008 | "// input is not a square matrix"; |
---|
1009 | return(Z); |
---|
1010 | } |
---|
1011 | if(!const_mat(A)){ |
---|
1012 | "// input is not a constant matrix"; |
---|
1013 | return(Z); |
---|
1014 | } |
---|
1015 | |
---|
1016 | L=gaussred(A); |
---|
1017 | |
---|
1018 | if(L[4]!=n){ |
---|
1019 | "// input is not an invertible matrix"; |
---|
1020 | Z=insert(Z,-1); //hint for calling procedures |
---|
1021 | return(Z); |
---|
1022 | } |
---|
1023 | |
---|
1024 | D=L[3]; |
---|
1025 | |
---|
1026 | for(i=1; i<=n; i++){ |
---|
1027 | for(j=i+1; j<=n; j++){ |
---|
1028 | O[i,j] = D[i,j]/D[i,i]; |
---|
1029 | D[i,j] = 0; |
---|
1030 | } |
---|
1031 | } |
---|
1032 | |
---|
1033 | Z=insert(Z,O); |
---|
1034 | Z=insert(Z,D); |
---|
1035 | Z=insert(Z,L[2]); |
---|
1036 | Z=insert(Z,L[1]); |
---|
1037 | return(Z); |
---|
1038 | } |
---|
1039 | example |
---|
1040 | { "EXAMPLE";echo=2; |
---|
1041 | ring r = 0,(x),dp; |
---|
1042 | matrix A[5][5] = 10, 4, 0, -9, 8, |
---|
1043 | -3, 6, -6, -4, 9, |
---|
1044 | 0, 3, -1, -9, -8, |
---|
1045 | -4,-2, -6, -10,10, |
---|
1046 | -9, 5, -1, -6, 5; |
---|
1047 | list Z = U_D_O(A); //construct P,U,D,O s.t. P*A=U*D*O |
---|
1048 | print(Z[1]); //P |
---|
1049 | print(Z[2]); //U |
---|
1050 | print(Z[3]); //D |
---|
1051 | print(Z[4]); //O |
---|
1052 | print(Z[1]*A); //P*A |
---|
1053 | print(Z[2]*Z[3]*Z[4]); //U*D*O |
---|
1054 | } |
---|
1055 | |
---|
1056 | ////////////////////////////////////////////////////////////////////////////// |
---|
1057 | proc pos_def(matrix A) |
---|
1058 | "USAGE: pos_def(A); A = constant, symmetric square matrix |
---|
1059 | RETURN: int: |
---|
1060 | 1 if A is positive definit , |
---|
1061 | 0 if not, |
---|
1062 | -1 if unknown |
---|
1063 | EXAMPLE: example pos_def; shows an example" |
---|
1064 | { |
---|
1065 | int j; |
---|
1066 | list Z; |
---|
1067 | int n = nrows(A); |
---|
1068 | matrix H[n][n]; |
---|
1069 | |
---|
1070 | if (ncols(A)!=n){ |
---|
1071 | "// input is not a square matrix"; |
---|
1072 | return(0); |
---|
1073 | } |
---|
1074 | if(!const_mat(A)){ |
---|
1075 | "// input is not a constant matrix"; |
---|
1076 | return(-1); |
---|
1077 | } |
---|
1078 | if(deg(std(A-transpose(A))[1])!=-1){ |
---|
1079 | "// input is not a hermitian (symmetric) matrix"; |
---|
1080 | return(-1); |
---|
1081 | } |
---|
1082 | |
---|
1083 | Z=U_D_O(A); |
---|
1084 | |
---|
1085 | if(Z[1]==-1){ |
---|
1086 | return(0); |
---|
1087 | } //A not regular, therefore not pos. definit |
---|
1088 | |
---|
1089 | H=Z[1]; |
---|
1090 | //es fand Zeilentausch statt: also nicht positiv definit |
---|
1091 | if(deg(std(H-unitmat(n))[1])!=-1){ |
---|
1092 | return(0); |
---|
1093 | } |
---|
1094 | |
---|
1095 | H=Z[3]; |
---|
1096 | |
---|
1097 | for(j=1;j<=n;j=j+1){ |
---|
1098 | if(H[j,j]<=0){ |
---|
1099 | return(0); |
---|
1100 | } //eigenvalue<=0, not pos.definit |
---|
1101 | } |
---|
1102 | |
---|
1103 | return(1); //positiv definit; |
---|
1104 | } |
---|
1105 | example |
---|
1106 | { "EXAMPLE"; echo=2; |
---|
1107 | ring r = 0,(x),dp; |
---|
1108 | matrix A[5][5] = 20, 4, 0, -9, 8, |
---|
1109 | 4, 12, -6, -4, 9, |
---|
1110 | 0, -6, -2, -9, -8, |
---|
1111 | -9, -4, -9, -20, 10, |
---|
1112 | 8, 9, -8, 10, 10; |
---|
1113 | pos_def(A); |
---|
1114 | matrix B[3][3] = 3, 2, 0, |
---|
1115 | 2, 12, 4, |
---|
1116 | 0, 4, 2; |
---|
1117 | pos_def(B); |
---|
1118 | } |
---|
1119 | |
---|
1120 | ////////////////////////////////////////////////////////////////////////////// |
---|
1121 | proc linsolve(matrix A, matrix b) |
---|
1122 | "USAGE: linsolve(A,b); A a constant nxm-matrix, b a constant nx1-matrix |
---|
1123 | RETURN: a 1xm matrix X, solution of inhomogeneous linear system A*X = b |
---|
1124 | return the 0-matrix if system is not solvable |
---|
1125 | NOTE: uses gaussred |
---|
1126 | EXAMPLE: example linsolve; shows an example" |
---|
1127 | { |
---|
1128 | int i,j,k,rc,r; |
---|
1129 | poly c; |
---|
1130 | list Z; |
---|
1131 | int n = nrows(A); |
---|
1132 | int m = ncols(A); |
---|
1133 | int n_b= nrows(b); |
---|
1134 | matrix Ab[n][m+1]; |
---|
1135 | matrix X[m][1]; |
---|
1136 | |
---|
1137 | if(ncols(b)!=1){ |
---|
1138 | "// right hand side b is not a nx1 matrix"; |
---|
1139 | return(X); |
---|
1140 | } |
---|
1141 | |
---|
1142 | if(!const_mat(A)){ |
---|
1143 | "// input hand is not a constant matrix"; |
---|
1144 | return(X); |
---|
1145 | } |
---|
1146 | |
---|
1147 | if(n_b>n){ |
---|
1148 | for(i=n; i<=n_b; i++){ |
---|
1149 | if(b[i,1]!=0){ |
---|
1150 | "// right hand side b not in Image(A)"; |
---|
1151 | return X; |
---|
1152 | } |
---|
1153 | } |
---|
1154 | } |
---|
1155 | |
---|
1156 | if(n_b<n){ |
---|
1157 | matrix copy[n_b][1]=b; |
---|
1158 | matrix b[n][1]=0; |
---|
1159 | for(i=1;i<=n_b;i=i+1){ |
---|
1160 | b[i,1]=copy[i,1]; |
---|
1161 | } |
---|
1162 | } |
---|
1163 | |
---|
1164 | r=mat_rk(A); |
---|
1165 | |
---|
1166 | //1. b constant vector |
---|
1167 | if(const_mat(b)){ |
---|
1168 | //extend A with b |
---|
1169 | for(i=1; i<=n; i++){ |
---|
1170 | for(j=1; j<=m; j++){ |
---|
1171 | Ab[i,j]=A[i,j]; |
---|
1172 | } |
---|
1173 | Ab[i,m+1]=b[i,1]; |
---|
1174 | } |
---|
1175 | |
---|
1176 | //Gauss reduction |
---|
1177 | Z = gaussred(Ab); |
---|
1178 | Ab = Z[3]; //normal form |
---|
1179 | rc = Z[4]; //rank(Ab) |
---|
1180 | //print(Ab); |
---|
1181 | |
---|
1182 | if(r<rc){ |
---|
1183 | "// no solution"; |
---|
1184 | return(X); |
---|
1185 | } |
---|
1186 | k=m; |
---|
1187 | for(i=r;i>=1;i=i-1){ |
---|
1188 | |
---|
1189 | j=1; |
---|
1190 | while(Ab[i,j]==0){j=j+1;}// suche Ecke |
---|
1191 | |
---|
1192 | for(;k>j;k=k-1){ X[k]=0;}//springe zur Ecke |
---|
1193 | |
---|
1194 | |
---|
1195 | c=Ab[i,m+1]; //i-te Komponene von b |
---|
1196 | for(j=m;j>k;j=j-1){ |
---|
1197 | c=c-X[j,1]*Ab[i,j]; |
---|
1198 | } |
---|
1199 | if(Ab[i,k]==0){ |
---|
1200 | X[k,1]=1; //willkuerlich |
---|
1201 | } |
---|
1202 | else{ |
---|
1203 | X[k,1]=c/Ab[i,k]; |
---|
1204 | } |
---|
1205 | k=k-1; |
---|
1206 | if(k==0){break;} |
---|
1207 | } |
---|
1208 | |
---|
1209 | |
---|
1210 | }//endif (const b) |
---|
1211 | else{ //b not constant |
---|
1212 | "// !not implemented!"; |
---|
1213 | |
---|
1214 | } |
---|
1215 | |
---|
1216 | return(X); |
---|
1217 | } |
---|
1218 | example |
---|
1219 | { "EXAMPLE";echo=2; |
---|
1220 | ring r=0,(x),dp; |
---|
1221 | matrix A[3][2] = -4,-6, |
---|
1222 | 2, 3, |
---|
1223 | -5, 7; |
---|
1224 | matrix b[3][1] = 10, |
---|
1225 | -5, |
---|
1226 | 2; |
---|
1227 | matrix X = linsolve(A,b); |
---|
1228 | print(X); |
---|
1229 | print(A*X); |
---|
1230 | } |
---|
1231 | ////////////////////////////////////////////////////////////////////////////// |
---|
1232 | |
---|
1233 | /////////////////////////////////////////////////////////////////////////////// |
---|
1234 | // PROCEDURES for Jordan normal form |
---|
1235 | // AUTHOR: Mathias Schulze, email: mschulze@mathematik.uni-kl.de |
---|
1236 | /////////////////////////////////////////////////////////////////////////////// |
---|
1237 | |
---|
1238 | static proc rowcolswap(matrix M,int i,int j) |
---|
1239 | { |
---|
1240 | if(i==j) |
---|
1241 | { |
---|
1242 | return(M); |
---|
1243 | } |
---|
1244 | poly p; |
---|
1245 | for(int k=1;k<=nrows(M);k++) |
---|
1246 | { |
---|
1247 | p=M[i,k]; |
---|
1248 | M[i,k]=M[j,k]; |
---|
1249 | M[j,k]=p; |
---|
1250 | } |
---|
1251 | for(k=1;k<=ncols(M);k++) |
---|
1252 | { |
---|
1253 | p=M[k,i]; |
---|
1254 | M[k,i]=M[k,j]; |
---|
1255 | M[k,j]=p; |
---|
1256 | } |
---|
1257 | return(M); |
---|
1258 | } |
---|
1259 | ////////////////////////////////////////////////////////////////////////////// |
---|
1260 | |
---|
1261 | static proc rowelim(matrix M,int i,int j,int k) |
---|
1262 | { |
---|
1263 | if(jet(M[i,k],0)==0||jet(M[j,k],0)==0) |
---|
1264 | { |
---|
1265 | return(M); |
---|
1266 | } |
---|
1267 | number n=number(jet(M[i,k],0))/number(jet(M[j,k],0)); |
---|
1268 | for(int l=1;l<=ncols(M);l++) |
---|
1269 | { |
---|
1270 | M[i,l]=M[i,l]-n*M[j,l]; |
---|
1271 | } |
---|
1272 | for(l=1;l<=nrows(M);l++) |
---|
1273 | { |
---|
1274 | M[l,j]=M[l,j]+n*M[l,i]; |
---|
1275 | } |
---|
1276 | return(M); |
---|
1277 | } |
---|
1278 | /////////////////////////////////////////////////////////////////////////////// |
---|
1279 | |
---|
1280 | static proc colelim(matrix M,int i,int j,int k) |
---|
1281 | { |
---|
1282 | if(jet(M[k,i],0)==0||jet(M[k,j],0)==0) |
---|
1283 | { |
---|
1284 | return(M); |
---|
1285 | } |
---|
1286 | number n=number(jet(M[k,i],0))/number(jet(M[k,j],0)); |
---|
1287 | for(int l=1;l<=nrows(M);l++) |
---|
1288 | { |
---|
1289 | M[l,i]=M[l,i]-n*M[l,j]; |
---|
1290 | } |
---|
1291 | for(l=1;l<=ncols(M);l++) |
---|
1292 | { |
---|
1293 | M[j,l]=M[j,l]+n*M[i,l]; |
---|
1294 | } |
---|
1295 | return(M); |
---|
1296 | } |
---|
1297 | /////////////////////////////////////////////////////////////////////////////// |
---|
1298 | |
---|
1299 | proc hessenberg(matrix M) |
---|
1300 | "USAGE: hessenberg(M); matrix M |
---|
1301 | ASSUME: M constant square matrix |
---|
1302 | RETURN: matrix H; Hessenberg form of M |
---|
1303 | EXAMPLE: example hessenberg; shows examples |
---|
1304 | " |
---|
1305 | { |
---|
1306 | if(system("with","eigenval")) |
---|
1307 | { |
---|
1308 | return(system("hessenberg",M)); |
---|
1309 | } |
---|
1310 | |
---|
1311 | int n=ncols(M); |
---|
1312 | int i,j; |
---|
1313 | for(int k=1;k<n-1;k++) |
---|
1314 | { |
---|
1315 | j=k+1; |
---|
1316 | while(j<n&&jet(M[j,k],0)==0) |
---|
1317 | { |
---|
1318 | j++; |
---|
1319 | } |
---|
1320 | if(jet(M[j,k],0)!=0) |
---|
1321 | { |
---|
1322 | M=rowcolswap(M,j,k+1); |
---|
1323 | for(i=j+1;i<=n;i++) |
---|
1324 | { |
---|
1325 | M=rowelim(M,i,k+1,k); |
---|
1326 | } |
---|
1327 | } |
---|
1328 | } |
---|
1329 | return(M); |
---|
1330 | } |
---|
1331 | example |
---|
1332 | { "EXAMPLE:"; echo=2; |
---|
1333 | ring R=0,x,dp; |
---|
1334 | matrix M[3][3]=3,2,1,0,2,1,0,0,3; |
---|
1335 | print(M); |
---|
1336 | print(hessenberg(M)); |
---|
1337 | } |
---|
1338 | /////////////////////////////////////////////////////////////////////////////// |
---|
1339 | |
---|
1340 | proc eigenvals(matrix M) |
---|
1341 | "USAGE: eigenvals(M); matrix M |
---|
1342 | ASSUME: eigenvalues of M in basefield |
---|
1343 | RETURN: |
---|
1344 | @format |
---|
1345 | list l; |
---|
1346 | ideal l[1]; |
---|
1347 | number l[1][i]; i-th eigenvalue of M |
---|
1348 | intvec l[2]; |
---|
1349 | int l[2][i]; multiplicity of i-th eigenvalue of M |
---|
1350 | @end format |
---|
1351 | EXAMPLE: example eigenvals; shows examples |
---|
1352 | " |
---|
1353 | { |
---|
1354 | if(system("with","eigenval")) |
---|
1355 | { |
---|
1356 | return(system("eigenvals",jet(M,0))); |
---|
1357 | } |
---|
1358 | |
---|
1359 | M=jet(hessenberg(M),0); |
---|
1360 | int n=ncols(M); |
---|
1361 | int k; |
---|
1362 | ideal e; |
---|
1363 | intvec m; |
---|
1364 | number e0; |
---|
1365 | intvec v; |
---|
1366 | list l; |
---|
1367 | int i,j; |
---|
1368 | j=1; |
---|
1369 | while(j<=n) |
---|
1370 | { |
---|
1371 | v=j; |
---|
1372 | j++; |
---|
1373 | if(j<=n) |
---|
1374 | { |
---|
1375 | while(j<n&&M[j,j-1]!=0) |
---|
1376 | { |
---|
1377 | v=v,j; |
---|
1378 | j++; |
---|
1379 | } |
---|
1380 | if(M[j,j-1]!=0) |
---|
1381 | { |
---|
1382 | v=v,j; |
---|
1383 | j++; |
---|
1384 | } |
---|
1385 | } |
---|
1386 | if(size(v)==1) |
---|
1387 | { |
---|
1388 | k++; |
---|
1389 | e[k]=M[v,v]; |
---|
1390 | m[k]=1; |
---|
1391 | } |
---|
1392 | else |
---|
1393 | { |
---|
1394 | l=factorize(det(submat(M,v,v)-var(1))); |
---|
1395 | for(i=size(l[1]);i>=1;i--) |
---|
1396 | { |
---|
1397 | e0=number(jet(l[1][i]/var(1),0)); |
---|
1398 | if(e0!=0) |
---|
1399 | { |
---|
1400 | k++; |
---|
1401 | e[k]=(e0*var(1)-l[1][i])/e0; |
---|
1402 | m[k]=l[2][i]; |
---|
1403 | } |
---|
1404 | } |
---|
1405 | } |
---|
1406 | } |
---|
1407 | return(spnf(list(e,m))); |
---|
1408 | } |
---|
1409 | example |
---|
1410 | { "EXAMPLE:"; echo=2; |
---|
1411 | ring R=0,x,dp; |
---|
1412 | matrix M[3][3]=3,2,1,0,2,1,0,0,3; |
---|
1413 | print(M); |
---|
1414 | eigenvals(M); |
---|
1415 | } |
---|
1416 | /////////////////////////////////////////////////////////////////////////////// |
---|
1417 | |
---|
1418 | proc minipoly(matrix M,list #) |
---|
1419 | "USAGE: minpoly(M); matrix M |
---|
1420 | ASSUME: eigenvalues of M in basefield |
---|
1421 | RETURN: |
---|
1422 | @format |
---|
1423 | list l; minimal polynomial of M |
---|
1424 | ideal l[1]; |
---|
1425 | number l[1][i]; i-th root of minimal polynomial of M |
---|
1426 | intvec l[2]; |
---|
1427 | int l[2][i]; multiplicity of i-th root of minimal polynomial of M |
---|
1428 | @end format |
---|
1429 | EXAMPLE: example minipoly; shows examples |
---|
1430 | " |
---|
1431 | { |
---|
1432 | if(nrows(M)==0) |
---|
1433 | { |
---|
1434 | ERROR("non empty expected"); |
---|
1435 | } |
---|
1436 | if(ncols(M)!=nrows(M)) |
---|
1437 | { |
---|
1438 | ERROR("square matrix expected"); |
---|
1439 | } |
---|
1440 | |
---|
1441 | M=jet(M,0); |
---|
1442 | |
---|
1443 | if(size(#)==0) |
---|
1444 | { |
---|
1445 | #=eigenvals(M); |
---|
1446 | } |
---|
1447 | def e0,m0=#[1..2]; |
---|
1448 | |
---|
1449 | intvec m1; |
---|
1450 | matrix N0,N1; |
---|
1451 | for(int i=1;i<=ncols(e0);i++) |
---|
1452 | { |
---|
1453 | m1[i]=1; |
---|
1454 | N0=M-e0[i]; |
---|
1455 | N1=N0; |
---|
1456 | while(size(syz(N1))<m0[i]) |
---|
1457 | { |
---|
1458 | m1[i]=m1[i]+1; |
---|
1459 | N1=N1*N0; |
---|
1460 | } |
---|
1461 | } |
---|
1462 | |
---|
1463 | return(list(e0,m1)); |
---|
1464 | } |
---|
1465 | example |
---|
1466 | { "EXAMPLE:"; echo=2; |
---|
1467 | ring R=0,x,dp; |
---|
1468 | matrix M[3][3]=3,2,1,0,2,1,0,0,3; |
---|
1469 | print(M); |
---|
1470 | minipoly(M); |
---|
1471 | } |
---|
1472 | /////////////////////////////////////////////////////////////////////////////// |
---|
1473 | |
---|
1474 | proc spnf(list #) |
---|
1475 | "USAGE: spnf(list(a[,m])); ideal a, intvec m |
---|
1476 | ASSUME: ncols(a)==size(m) |
---|
1477 | RETURN: order a[i] with multiplicity m[i] lexicographically |
---|
1478 | EXAMPLE: example spnf; shows examples |
---|
1479 | " |
---|
1480 | { |
---|
1481 | list sp=#; |
---|
1482 | ideal a=sp[1]; |
---|
1483 | int n=ncols(a); |
---|
1484 | intvec m; |
---|
1485 | list V; |
---|
1486 | module v; |
---|
1487 | int i,j; |
---|
1488 | for(i=2;i<=size(sp);i++) |
---|
1489 | { |
---|
1490 | if(typeof(sp[i])=="intvec") |
---|
1491 | { |
---|
1492 | m=sp[i]; |
---|
1493 | } |
---|
1494 | if(typeof(sp[i])=="module") |
---|
1495 | { |
---|
1496 | v=sp[i]; |
---|
1497 | for(j=n;j>=1;j--) |
---|
1498 | { |
---|
1499 | V[j]=module(v[j]); |
---|
1500 | } |
---|
1501 | } |
---|
1502 | if(typeof(sp[i])=="list") |
---|
1503 | { |
---|
1504 | V=sp[i]; |
---|
1505 | } |
---|
1506 | } |
---|
1507 | if(m==0) |
---|
1508 | { |
---|
1509 | for(i=n;i>=1;i--) |
---|
1510 | { |
---|
1511 | m[i]=1; |
---|
1512 | } |
---|
1513 | } |
---|
1514 | |
---|
1515 | int k; |
---|
1516 | ideal a0; |
---|
1517 | intvec m0; |
---|
1518 | list V0; |
---|
1519 | number a1; |
---|
1520 | int m1; |
---|
1521 | for(i=n;i>=1;i--) |
---|
1522 | { |
---|
1523 | if(m[i]!=0) |
---|
1524 | { |
---|
1525 | for(j=i-1;j>=1;j--) |
---|
1526 | { |
---|
1527 | if(m[j]!=0) |
---|
1528 | { |
---|
1529 | if(number(a[i])>number(a[j])) |
---|
1530 | { |
---|
1531 | a1=number(a[i]); |
---|
1532 | a[i]=a[j]; |
---|
1533 | a[j]=a1; |
---|
1534 | m1=m[i]; |
---|
1535 | m[i]=m[j]; |
---|
1536 | m[j]=m1; |
---|
1537 | if(size(V)>0) |
---|
1538 | { |
---|
1539 | v=V[i]; |
---|
1540 | V[i]=V[j]; |
---|
1541 | V[j]=v; |
---|
1542 | } |
---|
1543 | } |
---|
1544 | if(number(a[i])==number(a[j])) |
---|
1545 | { |
---|
1546 | m[i]=m[i]+m[j]; |
---|
1547 | m[j]=0; |
---|
1548 | if(size(V)>0) |
---|
1549 | { |
---|
1550 | V[i]=V[i]+V[j]; |
---|
1551 | } |
---|
1552 | } |
---|
1553 | } |
---|
1554 | } |
---|
1555 | k++; |
---|
1556 | a0[k]=a[i]; |
---|
1557 | m0[k]=m[i]; |
---|
1558 | if(size(V)>0) |
---|
1559 | { |
---|
1560 | V0[k]=V[i]; |
---|
1561 | } |
---|
1562 | } |
---|
1563 | } |
---|
1564 | |
---|
1565 | if(size(V0)>0) |
---|
1566 | { |
---|
1567 | n=size(V0); |
---|
1568 | module U=std(V0[n]); |
---|
1569 | for(i=n-1;i>=1;i--) |
---|
1570 | { |
---|
1571 | V0[i]=simplify(reduce(V0[i],U),1); |
---|
1572 | if(i>=2) |
---|
1573 | { |
---|
1574 | U=std(U+V0[i]); |
---|
1575 | } |
---|
1576 | } |
---|
1577 | } |
---|
1578 | |
---|
1579 | if(k>0) |
---|
1580 | { |
---|
1581 | sp=a0,m0; |
---|
1582 | if(size(V0)>0) |
---|
1583 | { |
---|
1584 | sp[3]=V0; |
---|
1585 | } |
---|
1586 | } |
---|
1587 | return(sp); |
---|
1588 | } |
---|
1589 | example |
---|
1590 | { "EXAMPLE:"; echo=2; |
---|
1591 | ring R=0,(x,y),ds; |
---|
1592 | list sp=list(ideal(-1/2,-3/10,-3/10,-1/10,-1/10,0,1/10,1/10,3/10,3/10,1/2)); |
---|
1593 | spprint(spnf(sp)); |
---|
1594 | } |
---|
1595 | /////////////////////////////////////////////////////////////////////////////// |
---|
1596 | |
---|
1597 | proc spprint(list sp) |
---|
1598 | "USAGE: spprint(sp); list sp |
---|
1599 | RETURN: string s; spectrum sp |
---|
1600 | EXAMPLE: example spprint; shows examples |
---|
1601 | " |
---|
1602 | { |
---|
1603 | string s; |
---|
1604 | for(int i=1;i<size(sp[2]);i++) |
---|
1605 | { |
---|
1606 | s=s+"("+string(sp[1][i])+","+string(sp[2][i])+"),"; |
---|
1607 | } |
---|
1608 | s=s+"("+string(sp[1][i])+","+string(sp[2][i])+")"; |
---|
1609 | return(s); |
---|
1610 | } |
---|
1611 | example |
---|
1612 | { "EXAMPLE:"; echo=2; |
---|
1613 | ring R=0,(x,y),ds; |
---|
1614 | list sp=list(ideal(-1/2,-3/10,-1/10,0,1/10,3/10,1/2),intvec(1,2,2,1,2,2,1)); |
---|
1615 | spprint(sp); |
---|
1616 | } |
---|
1617 | /////////////////////////////////////////////////////////////////////////////// |
---|
1618 | |
---|
1619 | proc jordan(matrix M,list #) |
---|
1620 | "USAGE: jordan(M); matrix M |
---|
1621 | ASSUME: eigenvalues of M in basefield |
---|
1622 | RETURN: |
---|
1623 | @format |
---|
1624 | list l; Jordan data of M |
---|
1625 | ideal l[1]; |
---|
1626 | number l[1][i]; eigenvalue of i-th Jordan block of M |
---|
1627 | intvec l[2]; |
---|
1628 | int l[2][i]; size of i-th Jordan block of M |
---|
1629 | intvec l[3]; |
---|
1630 | int l[3][i]; multiplicity of i-th Jordan block of M |
---|
1631 | @end format |
---|
1632 | EXAMPLE: example jordan; shows examples |
---|
1633 | " |
---|
1634 | { |
---|
1635 | if(nrows(M)==0) |
---|
1636 | { |
---|
1637 | ERROR("non empty expected"); |
---|
1638 | } |
---|
1639 | if(ncols(M)!=nrows(M)) |
---|
1640 | { |
---|
1641 | ERROR("square matrix expected"); |
---|
1642 | } |
---|
1643 | |
---|
1644 | M=jet(M,0); |
---|
1645 | |
---|
1646 | if(size(#)==0) |
---|
1647 | { |
---|
1648 | #=eigenvals(M); |
---|
1649 | } |
---|
1650 | def e0,m0=#[1..2]; |
---|
1651 | |
---|
1652 | int i; |
---|
1653 | for(i=1;i<=ncols(e0);i++) |
---|
1654 | { |
---|
1655 | if(deg(e0[i])>0) |
---|
1656 | { |
---|
1657 | |
---|
1658 | ERROR("eigenvalues in coefficient field expected"); |
---|
1659 | return(list()); |
---|
1660 | } |
---|
1661 | } |
---|
1662 | |
---|
1663 | int j,k; |
---|
1664 | matrix N0,N1; |
---|
1665 | module K0; |
---|
1666 | list K; |
---|
1667 | ideal e; |
---|
1668 | intvec s,m; |
---|
1669 | |
---|
1670 | for(i=1;i<=ncols(e0);i++) |
---|
1671 | { |
---|
1672 | N0=M-e0[i]*matrix(freemodule(ncols(M))); |
---|
1673 | |
---|
1674 | N1=N0; |
---|
1675 | K0=0; |
---|
1676 | K=module(); |
---|
1677 | while(size(K0)<m0[i]) |
---|
1678 | { |
---|
1679 | K0=syz(N1); |
---|
1680 | K=K+list(K0); |
---|
1681 | N1=N1*N0; |
---|
1682 | } |
---|
1683 | |
---|
1684 | for(j=2;j<size(K);j++) |
---|
1685 | { |
---|
1686 | if(2*size(K[j])-size(K[j-1])-size(K[j+1])>0) |
---|
1687 | { |
---|
1688 | k++; |
---|
1689 | e[k]=e0[i]; |
---|
1690 | s[k]=j-1; |
---|
1691 | m[k]=2*size(K[j])-size(K[j-1])-size(K[j+1]); |
---|
1692 | } |
---|
1693 | } |
---|
1694 | if(size(K[j])-size(K[j-1])>0) |
---|
1695 | { |
---|
1696 | k++; |
---|
1697 | e[k]=e0[i]; |
---|
1698 | s[k]=j-1; |
---|
1699 | m[k]=size(K[j])-size(K[j-1]); |
---|
1700 | } |
---|
1701 | } |
---|
1702 | |
---|
1703 | return(list(e,s,m)); |
---|
1704 | } |
---|
1705 | example |
---|
1706 | { "EXAMPLE:"; echo=2; |
---|
1707 | ring R=0,x,dp; |
---|
1708 | matrix M[3][3]=3,2,1,0,2,1,0,0,3; |
---|
1709 | print(M); |
---|
1710 | jordan(M); |
---|
1711 | } |
---|
1712 | /////////////////////////////////////////////////////////////////////////////// |
---|
1713 | |
---|
1714 | proc jordanbasis(matrix M,list #) |
---|
1715 | "USAGE: jordanbasis(M); matrix M |
---|
1716 | ASSUME: eigenvalues of M in basefield |
---|
1717 | RETURN: |
---|
1718 | @format |
---|
1719 | list l: |
---|
1720 | module l[1]; inverse(l[1])*M*l[1] in Jordan normal form |
---|
1721 | intvec l[2]; |
---|
1722 | int l[2][i]; weight filtration index of l[1][i] |
---|
1723 | @end format |
---|
1724 | EXAMPLE: example jordanbasis; shows examples |
---|
1725 | " |
---|
1726 | { |
---|
1727 | if(nrows(M)==0) |
---|
1728 | { |
---|
1729 | ERROR("non empty matrix expected"); |
---|
1730 | } |
---|
1731 | if(ncols(M)!=nrows(M)) |
---|
1732 | { |
---|
1733 | ERROR("square matrix expected"); |
---|
1734 | } |
---|
1735 | |
---|
1736 | M=jet(M,0); |
---|
1737 | |
---|
1738 | if(size(#)==0) |
---|
1739 | { |
---|
1740 | #=eigenvals(M); |
---|
1741 | } |
---|
1742 | def e,m=#[1..2]; |
---|
1743 | |
---|
1744 | for(int i=1;i<=ncols(e);i++) |
---|
1745 | { |
---|
1746 | if(deg(e[i])>0) |
---|
1747 | { |
---|
1748 | ERROR("eigenvalues in coefficient field expected"); |
---|
1749 | return(freemodule(ncols(M))); |
---|
1750 | } |
---|
1751 | } |
---|
1752 | |
---|
1753 | int j,k,l,n; |
---|
1754 | matrix N0,N1; |
---|
1755 | module K0,K1; |
---|
1756 | list K; |
---|
1757 | matrix u[ncols(M)][1]; |
---|
1758 | module U; |
---|
1759 | intvec w; |
---|
1760 | |
---|
1761 | for(i=1;i<=ncols(e);i++) |
---|
1762 | { |
---|
1763 | N0=M-e[i]*matrix(freemodule(ncols(M))); |
---|
1764 | |
---|
1765 | N1=N0; |
---|
1766 | K0=0; |
---|
1767 | K=list(); |
---|
1768 | while(size(K0)<m[i]) |
---|
1769 | { |
---|
1770 | K0=syz(N1); |
---|
1771 | K=K+list(K0); |
---|
1772 | N1=N1*N0; |
---|
1773 | } |
---|
1774 | |
---|
1775 | K1=0; |
---|
1776 | for(j=1;j<size(K);j++) |
---|
1777 | { |
---|
1778 | K0=K[j]; |
---|
1779 | K[j]=interred(reduce(K[j],std(K1+module(N0*K[j+1])))); |
---|
1780 | K1=K0; |
---|
1781 | } |
---|
1782 | K[j]=interred(reduce(K[j],std(K1))); |
---|
1783 | |
---|
1784 | for(l=size(K);l>=1;l--) |
---|
1785 | { |
---|
1786 | for(k=size(K[l]);k>0;k--) |
---|
1787 | { |
---|
1788 | u=K[l][k]; |
---|
1789 | for(j=l;j>=1;j--) |
---|
1790 | { |
---|
1791 | U=U+module(u); |
---|
1792 | n++; |
---|
1793 | w[n]=2*j-l-1; |
---|
1794 | u=N0*u; |
---|
1795 | } |
---|
1796 | } |
---|
1797 | } |
---|
1798 | } |
---|
1799 | |
---|
1800 | return(list(U,w)); |
---|
1801 | } |
---|
1802 | example |
---|
1803 | { "EXAMPLE:"; echo=2; |
---|
1804 | ring R=0,x,dp; |
---|
1805 | matrix M[3][3]=3,2,1,0,2,1,0,0,3; |
---|
1806 | print(M); |
---|
1807 | list l=jordanbasis(M); |
---|
1808 | print(l[1]); |
---|
1809 | print(l[2]); |
---|
1810 | print(inverse(l[1])*M*l[1]); |
---|
1811 | } |
---|
1812 | /////////////////////////////////////////////////////////////////////////////// |
---|
1813 | |
---|
1814 | proc jordanmatrix(list jd) |
---|
1815 | "USAGE: jordanmatrix(list(e,s,m)); ideal e, intvec s, intvec m |
---|
1816 | ASSUME: ncols(e)==size(s)==size(m) |
---|
1817 | RETURN: |
---|
1818 | @format |
---|
1819 | matrix J; Jordan matrix with list(e,s,m)==jordan(J) |
---|
1820 | @end format |
---|
1821 | EXAMPLE: example jordanmatrix; shows examples |
---|
1822 | " |
---|
1823 | { |
---|
1824 | ideal e=jd[1]; |
---|
1825 | intvec s=jd[2]; |
---|
1826 | intvec m=jd[3]; |
---|
1827 | if(ncols(e)!=size(s)||ncols(e)!=size(m)) |
---|
1828 | { |
---|
1829 | ERROR("arguments of equal size expected"); |
---|
1830 | } |
---|
1831 | |
---|
1832 | int i,j,k,l; |
---|
1833 | int n=int((transpose(matrix(s))*matrix(m))[1,1]); |
---|
1834 | matrix J[n][n]; |
---|
1835 | for(k=1;k<=ncols(e);k++) |
---|
1836 | { |
---|
1837 | for(l=1;l<=m[k];l++) |
---|
1838 | { |
---|
1839 | j++; |
---|
1840 | J[j,j]=e[k]; |
---|
1841 | for(i=s[k];i>=2;i--) |
---|
1842 | { |
---|
1843 | J[j+1,j]=1; |
---|
1844 | j++; |
---|
1845 | J[j,j]=e[k]; |
---|
1846 | } |
---|
1847 | } |
---|
1848 | } |
---|
1849 | |
---|
1850 | return(J); |
---|
1851 | } |
---|
1852 | example |
---|
1853 | { "EXAMPLE:"; echo=2; |
---|
1854 | ring R=0,x,dp; |
---|
1855 | ideal e=ideal(2,3); |
---|
1856 | intvec s=1,2; |
---|
1857 | intvec m=1,1; |
---|
1858 | print(jordanmatrix(list(e,s,m))); |
---|
1859 | } |
---|
1860 | /////////////////////////////////////////////////////////////////////////////// |
---|
1861 | |
---|
1862 | proc jordannf(matrix M,list #) |
---|
1863 | "USAGE: jordannf(M); matrix M |
---|
1864 | ASSUME: eigenvalues of M in basefield |
---|
1865 | RETURN: matrix J; Jordan normal form of M |
---|
1866 | EXAMPLE: example jordannf; shows examples |
---|
1867 | " |
---|
1868 | { |
---|
1869 | return(jordanmatrix(jordan(M,#))); |
---|
1870 | } |
---|
1871 | example |
---|
1872 | { "EXAMPLE:"; echo=2; |
---|
1873 | ring R=0,x,dp; |
---|
1874 | matrix M[3][3]=3,2,1,0,2,1,0,0,3; |
---|
1875 | print(M); |
---|
1876 | print(jordannf(M)); |
---|
1877 | } |
---|
1878 | |
---|
1879 | /////////////////////////////////////////////////////////////////////////////// |
---|
1880 | |
---|
1881 | /* |
---|
1882 | /////////////////////////////////////////////////////////////////////////////// |
---|
1883 | // Auskommentierte zusaetzliche Beispiele |
---|
1884 | // |
---|
1885 | /////////////////////////////////////////////////////////////////////////////// |
---|
1886 | // Singular for ix86-Linux version 1-3-10 (2000121517) Dec 15 2000 17:55:12 |
---|
1887 | // Rechnungen auf AMD700 mit 632 MB |
---|
1888 | |
---|
1889 | LIB "linalg.lib"; |
---|
1890 | |
---|
1891 | 1. Sparse integer Matrizen |
---|
1892 | -------------------------- |
---|
1893 | ring r1=0,(x),dp; |
---|
1894 | system("--random", 12345678); |
---|
1895 | int n = 70; |
---|
1896 | matrix m = sparsemat(n,n,50,100); |
---|
1897 | option(prot,mem); |
---|
1898 | |
---|
1899 | int t=timer; |
---|
1900 | matrix im = inverse(m,1)[1]; |
---|
1901 | timer-t; |
---|
1902 | print(im*m); |
---|
1903 | //list l0 = watchdog(100,"inverse("+"m"+",3)"); |
---|
1904 | //bricht bei 100 sec ab und gibt l0[1]: string Killed zurueck |
---|
1905 | |
---|
1906 | //inverse(m,1): std 5sec 5,5 MB |
---|
1907 | //inverse(m,2): interred 12sec |
---|
1908 | //inverse(m,2): lift nach 180 sec 13MB abgebrochen |
---|
1909 | //n=60: linalgorig: 3 linalg: 5 |
---|
1910 | //n=70: linalgorig: 6,7 linalg: 11,12 |
---|
1911 | // aber linalgorig rechnet falsch! |
---|
1912 | |
---|
1913 | 2. Sparse poly Matrizen |
---|
1914 | ----------------------- |
---|
1915 | ring r=(0),(a,b,c),dp; |
---|
1916 | system("--random", 12345678); |
---|
1917 | int n=6; |
---|
1918 | matrix m = sparsematrix(n,n,2,0,50,50,9); //matrix of polys of deg <=2 |
---|
1919 | option(prot,mem); |
---|
1920 | |
---|
1921 | int t=timer; |
---|
1922 | matrix im = inverse(m); |
---|
1923 | timer-t; |
---|
1924 | print(im*m); |
---|
1925 | //inverse(m,1): std 0sec 1MB |
---|
1926 | //inverse(m,2): interred 0sec 1MB |
---|
1927 | //inverse(m,2): lift nach 2000 sec 33MB abgebrochen |
---|
1928 | |
---|
1929 | 3. Sparse Matrizen mit Parametern |
---|
1930 | --------------------------------- |
---|
1931 | //liborig rechnet hier falsch! |
---|
1932 | ring r=(0),(a,b),dp; |
---|
1933 | system("--random", 12345678); |
---|
1934 | int n=7; |
---|
1935 | matrix m = sparsematrix(n,n,1,0,40,50,9); |
---|
1936 | ring r1 = (0,a,b),(x),dp; |
---|
1937 | matrix m = imap(r,m); |
---|
1938 | option(prot,mem); |
---|
1939 | |
---|
1940 | int t=timer; |
---|
1941 | matrix im = inverse(m); |
---|
1942 | timer-t; |
---|
1943 | print(im*m); |
---|
1944 | //inverse(m)=inverse(m,3):15 sec inverse(m,1)=1sec inverse(m,2):>120sec |
---|
1945 | //Bei Parametern vergeht die Zeit beim Normieren! |
---|
1946 | |
---|
1947 | 3. Sparse Matrizen mit Variablen und Parametern |
---|
1948 | ----------------------------------------------- |
---|
1949 | ring r=(0),(a,b),dp; |
---|
1950 | system("--random", 12345678); |
---|
1951 | int n=6; |
---|
1952 | matrix m = sparsematrix(n,n,1,0,35,50,9); |
---|
1953 | ring r1 = (0,a),(b),dp; |
---|
1954 | matrix m = imap(r,m); |
---|
1955 | option(prot,mem); |
---|
1956 | |
---|
1957 | int t=timer; |
---|
1958 | matrix im = inverse(m,3); |
---|
1959 | timer-t; |
---|
1960 | print(im*m); |
---|
1961 | //n=7: inverse(m,3):lange sec inverse(m,1)=1sec inverse(m,2):1sec |
---|
1962 | |
---|
1963 | 4. Ueber Polynomring invertierbare Matrizen |
---|
1964 | ------------------------------------------- |
---|
1965 | LIB"random.lib"; LIB"linalg.lib"; |
---|
1966 | system("--random", 12345678); |
---|
1967 | int n =3; |
---|
1968 | ring r= 0,(x,y,z),(C,dp); |
---|
1969 | matrix A=triagmatrix(n,n,1,0,0,50,2); |
---|
1970 | intmat B=sparsetriag(n,n,20,1); |
---|
1971 | matrix M = A*transpose(B); |
---|
1972 | M=M*transpose(M); |
---|
1973 | M[1,1..ncols(M)]=M[1,1..n]+xyz*M[n,1..ncols(M)]; |
---|
1974 | print(M); |
---|
1975 | //M hat det=1 nach Konstruktion |
---|
1976 | |
---|
1977 | int t=timer; |
---|
1978 | matrix iM=inverse(M); |
---|
1979 | timer-t; |
---|
1980 | print(iM*M); //test |
---|
1981 | |
---|
1982 | //ACHTUNG: Interred liefert i.A. keine Inverse, Gegenbeispiel z.B. |
---|
1983 | //mit n=3 |
---|
1984 | //eifacheres Gegenbeispiel: |
---|
1985 | matrix M = |
---|
1986 | 9yz+3y+3z+2, 9y2+6y+1, |
---|
1987 | 9xyz+3xy+3xz-9z2+2x-6z-1,9xy2+6xy-9yz+x-3y-3z |
---|
1988 | //det M=1, inverse(M,2); ->// ** matrix is not invertible |
---|
1989 | //lead(M); 9xyz*gen(2) 9xy2*gen(2) nicht teilbar! |
---|
1990 | |
---|
1991 | 5. charpoly: |
---|
1992 | ----------- |
---|
1993 | //ring rp=(0,A,B,C),(x),dp; |
---|
1994 | ring r=0,(A,B,C,x),dp; |
---|
1995 | matrix m[12][12]= |
---|
1996 | AC,BC,-3BC,0,-A2+B2,-3AC+1,B2, B2, 1, 0, -C2+1,0, |
---|
1997 | 1, 1, 2C, 0,0, B, -A, -4C, 2A+1,0, 0, 0, |
---|
1998 | 0, 0, 0, 1,0, 2C+1, -4C+1,-A, B+1, 0, B+1, 3B, |
---|
1999 | AB,B2,0, 1,0, 1, 0, 1, A, 0, 1, B+1, |
---|
2000 | 1, 0, 1, 0,0, 1, 0, -C2, 0, 1, 0, 1, |
---|
2001 | 0, 0, 2, 1,2A, 1, 0, 0, 0, 0, 1, 1, |
---|
2002 | 0, 1, 0, 1,1, 2, A, 3B+1,1, B2,1, 1, |
---|
2003 | 0, 1, 0, 1,1, 1, 1, 1, 2, 0, 0, 0, |
---|
2004 | 1, 0, 1, 0,0, 0, 1, 0, 1, 1, 0, 3, |
---|
2005 | 1, 3B,B2+1,0,0, 1, 0, 1, 0, 0, 1, 0, |
---|
2006 | 0, 0, 1, 0,0, 0, 0, 1, 0, 0, 0, 0, |
---|
2007 | 0, 1, 0, 1,1, 3, 3B+1, 0, 1, 1, 1, 0; |
---|
2008 | option(prot,mem); |
---|
2009 | |
---|
2010 | int t=timer; |
---|
2011 | poly q=charpoly(m,"x"); //1sec, charpoly_B 1sec, 16MB |
---|
2012 | timer-t; |
---|
2013 | //1sec, charpoly_B 1sec, 16MB (gleich in r und rp) |
---|
2014 | |
---|
2015 | */ |
---|