HP Forums
Adjoint matrix (my function): ok with numbers but not symbolics - 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: Adjoint matrix (my function): ok with numbers but not symbolics (/thread-9469.html)



Adjoint matrix (my function): ok with numbers but not symbolics - salvomic - 11-10-2017 04:04 PM

hi,
in the code in this program the function adj(m) works well with numbers like
Code:
adj([1,2],[3,4])
but returns a "bad argument type" error with symbolic values like
Code:
adj([a,b],[c,d])
.
Why?

The need to have this function is because the new adjoint_matrix() in the CAS is, as it must be, more detailed, and it returns characteristic polynomial of A and the comatrix o A-xI, i.e.
Code:

adjoint_matrix([1,2],[4,3]);
[[1, -a-d, a*d-b*c], [ [[1],[0]], [[-d,b],[c,-a]] ] ]
that's correct, but isn't the simple desired form (only TRN(cofactors(mat)) ):
Code:
[[d, -b],[-c,a]]

Any help, please, using (or not) the new adjoint_matrix()?

Salvo


RE: Adjoint matrix (my function): ok with numbers but not symbolics - Carlos295pz - 11-10-2017 04:38 PM

It is true! was there a change in interactivity HP PPL - CAS?


RE: Adjoint matrix (my function): ok with numbers but not symbolics - Tim Wessman - 11-10-2017 04:41 PM

There should not have been, so this is interesting and exactly why we wanted a more public beta to test a wider range of user programs.

To be clear, you are calling your function in the CAS where previously it worked fine? Or are you calling it from Home?

Please provide exact steps that DID work in prior versions and now no longer work. Thank you!


RE: Adjoint matrix (my function): ok with numbers but not symbolics - salvomic - 11-10-2017 04:57 PM

(11-10-2017 04:41 PM)Tim Wessman Wrote:  There should not have been, so this is interesting and exactly why we wanted a more public beta to test a wider range of user programs.

To be clear, you are calling your function in the CAS where previously it worked fine? Or are you calling it from Home?

Please provide exact steps that DID work in prior versions and now no longer work. Thank you!

I called my adj() in CAS with this code:
Code:
adj([[a,b],[c,d]])
that should return
\[ \begin{Vmatrix}
d & -b \\
-c & a
\end{Vmatrix} \]
and instead return an error.
If I try
Code:
adj([[1,2],[4,3]])
I get correctly
\[
\begin{Vmatrix}
3 & -2 \\
-4 & 1
\end{Vmatrix}
\]

In my program adj(m) recall the internal cofactors(m) that use MAKEMAT and an internal function, minor(mat, r,c) that simply use delrows() and delcols() to delete row r and col c and pass the argument, and so on.
A simple code that, if I well remember worked well with numbers and with letters first (however I can be wrong, I don't remember if in older version it worked also with letters, I would like to understand why the littoral matrix is taken as it wouldn't a matrix and why then there is the "bad argument type")...

Also in iOS app I get the error. So I wonder: maybe it's not a real bug but only a problem with types of vars?

Simply, I thought that passing a matrix (m) it were indifferent if the matrix had numbers or letters (for symbolic calc) in its elements...

EDIT: also with adj([[1, SIN(x)],[2,3]]) I get "Error: Bad argument type"
\[
\begin{Vmatrix}
1 & sin(x) \\
2 & 3
\end{Vmatrix}
\]


RE: Adjoint matrix (my function): ok with numbers but not symbolics - salvomic - 11-10-2017 05:05 PM

Here there is the code, if someone would like to collaborate to find the error in it, that prevent the use of a letters instead of numbers in the matrix:

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 r 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;

The problem is in input, I suppose:
type([1,2],[4,3]) -> DOM_LIST
TYPE([1,2],[4,3]) -> 4
(matrix)
but
type([1,sin(x)],[2,3]) -> DOM_LIST
TYPE([1,sin(x)],[2,3]) -> 6
(list)
...


RE: Adjoint matrix (my function): ok with numbers but not symbolics - Tim Wessman - 11-10-2017 06:47 PM

(11-10-2017 04:57 PM)salvomic Wrote:  I called my adj() in CAS with this code:
Code:
adj([[a,b],[c,d]])
that should return

Well, that is the issue. Did it work previously and something has changed? Or is this just new code not working like you'd want.

One is a bug and one is an enhancement/feature request. Smile


RE: Adjoint matrix (my function): ok with numbers but not symbolics - salvomic - 11-10-2017 06:53 PM

(11-10-2017 06:47 PM)Tim Wessman Wrote:  Well, that is the issue. Did it work previously and something has changed? Or is this just new code not working like you'd want.

One is a bug and one is an enhancement/feature request. Smile

Tim, I'm not requesting an enhancement, hi :-)
I made the code in 2015 and then it worked, I believe also with the symbolic matrices...
Now that code (I haven't changed nothing) distinguishes between matrices and lists and refuses to treat the second ones.
I do *hope* it isn't a bug Smile


RE: Adjoint matrix (my function): ok with numbers but not symbolics - parisse - 11-10-2017 06:55 PM

When you compute the det of a minor, it will return a symbolic expression, can you really store that in a Home local variable?
Why don't you write a CAS program ?
What about defining adj(m):=adjoint_matrix(m)[2,size(m)]?


RE: Adjoint matrix (my function): ok with numbers but not symbolics - salvomic - 11-10-2017 07:07 PM

(11-10-2017 06:55 PM)parisse Wrote:  When you compute the det of a minor, it will return a symbolic expression, can you really store that in a Home local variable?
you are right, I didn't think it when I wrote the program.
Quote:Why don't you write a CAS program ?
What about defining adj(m):=adjoint_matrix(m)[2,size(m)]?

oh, well (it is not a bug, Tim) Smile
In 2015 we hadn't adjoint_matrix()...

Thank you! Now is more and more simple. I'm rewriting the program as you suggest.
Only a little difference: adjoint_matrix([[1,2],[4,3]])[2,2] returns [[-3,2],[4,-1]] and my adj([[1,2],[4,3]]) returns [[3,-2],[-4,1]], that's the first result multiplied by -1 ...
Also with [[a,b],[c,d]] I get the difference * -1...


RE: Adjoint matrix (my function): ok with numbers but not symbolics - parisse - 11-10-2017 07:09 PM

Then adj(m):=adjoint_matrix(m)[2,size(m)]*(-1)^(size(m)-1) should do the job.


RE: Adjoint matrix (my function): ok with numbers but not symbolics - salvomic - 11-10-2017 07:12 PM

(11-10-2017 07:09 PM)parisse Wrote:  Then adj(m):=adjoint_matrix(m)[2,size(m)]*(-1)^(size(m)-1) should do the job.

ok!
I'll rewrite the whole program for the new FW as a CAS function...

EDIT
This code is all CAS and works after the beta 2017 November 8.
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