HP Forums
Cofactors and adjoint matrix - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Cofactors and adjoint matrix (/thread-4076.html)



Cofactors and adjoint matrix - salvomic - 06-04-2015 08:06 PM

hi,
this code doesn't run:
Code:

minor();

EXPORT cofactors(m)
BEGIN
local tempmat, cofact, 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(tempmat, j, k);
      tempmat(j,k):= cofact;
    END; // inner for
  END; //for
  RETURN tempmat;
END;

minor(mat, r,c)
BEGIN
  mat:= delrows(mat,r);
  mat:= delcols(mat,c);
  RETURN mat;
END;

EXPORT adj(m)
BEGIN
local ad;
  ad:= cofactors(m);
RETURN TRN(ad);
END;

Is it not possible to have a matrix as item od another matrix?

I think the problem is in the line < tempmat(j,k):= cofact; >, in fact...
Any help?

Salvo


RE: Cofactors and adjoint matrix - salvomic - 06-04-2015 09:00 PM

ok, found!

this should run.
It returns both cofactors and adj (adjoint, transpose of cofactors):

Code:

minor();

EXPORT cofactors(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)
BEGIN
  mat:= delrows(mat,r);
  mat:= delcols(mat,c);
  RETURN mat;
END;

EXPORT adj(m)
BEGIN
local ad;
  ad:= cofactors(m);
RETURN TRN(ad);
END;