HP Forums
Matrices: adjoint and cofactors - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP Prime Software Library (/forum-15.html)
+--- Thread: Matrices: adjoint and cofactors (/thread-4119.html)



Matrices: adjoint and cofactors - salvomic - 06-09-2015 09:07 PM

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