Adjoint matrix (my function): ok with numbers but not symbolics
11-10-2017, 04:04 PM (This post was last modified: 11-10-2017 04:05 PM by salvomic.)
Post: #1
 salvomic Senior Member Posts: 1,364 Joined: Jan 2015
Adjoint matrix (my function): ok with numbers but not symbolics
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]]

Salvo

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
11-10-2017, 04:38 PM
Post: #2
 Carlos295pz Member Posts: 260 Joined: Sep 2015
RE: Adjoint matrix (my function): ok with numbers but not symbolics
It is true! was there a change in interactivity HP PPL - CAS?

Viga C | TD | FB
11-10-2017, 04:41 PM
Post: #3
 Tim Wessman Senior Member Posts: 1,992 Joined: Dec 2013
RE: Adjoint matrix (my function): ok with numbers but not symbolics
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!

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
11-10-2017, 04:57 PM (This post was last modified: 11-10-2017 05:41 PM by salvomic.)
Post: #4
 salvomic Senior Member Posts: 1,364 Joined: Jan 2015
RE: Adjoint matrix (my function): ok with numbers but not symbolics
(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}$
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...

$\begin{Vmatrix} 1 & sin(x) \\ 2 & 3 \end{Vmatrix}$

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
11-10-2017, 05:05 PM (This post was last modified: 11-10-2017 05:39 PM by salvomic.)
Post: #5
 salvomic Senior Member Posts: 1,364 Joined: Jan 2015
RE: Adjoint matrix (my function): ok with numbers but not symbolics
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)
...

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
11-10-2017, 06:47 PM
Post: #6
 Tim Wessman Senior Member Posts: 1,992 Joined: Dec 2013
RE: Adjoint matrix (my function): ok with numbers but not symbolics
(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.

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
11-10-2017, 06:53 PM
Post: #7
 salvomic Senior Member Posts: 1,364 Joined: Jan 2015
RE: Adjoint matrix (my function): ok with numbers but not symbolics
(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.

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

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
11-10-2017, 06:55 PM
Post: #8
 parisse Senior Member Posts: 910 Joined: Dec 2013
RE: Adjoint matrix (my function): ok with numbers but not symbolics
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 ?
11-10-2017, 07:07 PM
Post: #9
 salvomic Senior Member Posts: 1,364 Joined: Jan 2015
RE: Adjoint matrix (my function): ok with numbers but not symbolics
(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 ?

oh, well (it is not a bug, Tim)

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...

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
11-10-2017, 07:09 PM
Post: #10
 parisse Senior Member Posts: 910 Joined: Dec 2013
RE: Adjoint matrix (my function): ok with numbers but not symbolics
11-10-2017, 07:12 PM (This post was last modified: 11-10-2017 09:36 PM by salvomic.)
Post: #11
 salvomic Senior Member Posts: 1,364 Joined: Jan 2015
RE: Adjoint matrix (my function): ok with numbers but not symbolics

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.
 // 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