Post Reply 
Matrices: adjoint and cofactors
06-09-2015, 09:07 PM (This post was last modified: 11-17-2017 01:52 PM by salvomic.)
Post: #1
Matrices: adjoint and cofactors
hi,
a little program to get adjoint matrix, cofactor matrix and minor(s) of a given matrix

Salvo Micciché

EDIT 2017, November
After the release of beta 2017 November 8 (build >13005) the CAS has also another important command: adjoint_matrix(m). It returns for a matrix the characteristic polynomial and the adjoint matrix (see help in the Prime).
So the above code work in the previous version of the firmware and only with numerical entries (no literal symbolic values).

Code:

minor();

EXPORT cofactors(m)
// Cofactors of the matrix m
BEGIN
local tempmat, cofact, deter;
local r, c, j, k;
  r:=rowDim(m);
  c:=colDim(m);
  cofact:=  MAKEMAT(0,r,c);
  tempmat:= m;
  FOR j FROM 1 TO r DO
  FOR k FROM 1 to c DO
    cofact:= minor(m, j, k);
    deter:= ((-1)^(j+k)) * det(cofact);
    tempmat(j,k):= deter;
  END; // inner for
  END; //for
  RETURN tempmat;
END;

minor(mat, r,c)
// Returns the matrix without row c and col c
BEGIN
  mat:= delrows(mat,r);
  mat:= delcols(mat,c);
  RETURN mat;
END;

EXPORT adj(m)
// adjoint matrix (=transpose of cofactors(m))
BEGIN
local ad;
  ad:= cofactors(m);
RETURN TRN(ad);
END;

After the beta 2017 November 8 and the next firmwares there is a better, fast and simple code.
This code is all CAS and use the new adjoint_matrix() function to calculate:
adj(m) Adjoint matrix
cofactors(m) Cofactors matrix -> TRN(adj(m))
minor(m, r, c) a minor of the matrix m, suppressing row r and col c
Code:

// Cofactors matrix
// use programs adj(m) and minor(m,r,c)
#cas
cofactors(m) :=
BEGIN
RETURN TRN(adj(m));
END;
#end

#cas
// Adjoint of a matrix (transpose of cofactor mat)
// after beta 2017 Nov 8
adj(m) :=
BEGIN
RETURN adjoint_matrix(m)[2,size(m)]*(-1)^(size(m)-1);
END;
#end

#cas
// Minor of a matrix
// r row, c col to suppress
minor(m, r,c) :=
BEGIN
  m:= delrows(m,r);
  m:= delcols(m,c);
  RETURN m;
END;
#end

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)