source: git/Singular/LIB/linalg.lib @ 7a7499

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