Post Reply 
HP-41 Complex Determinants w/ SandMatrix and 41Z
06-11-2018, 08:51 AM (This post was last modified: 06-14-2018 12:35 AM by Ángel Martin.)
Post: #1
HP-41 Complex Determinants w/ SandMatrix and 41Z
Complex Determinants with the SandMatrix and 41Z Modules

The programs below are a first-pass successful attempt at calculating Complex Matrix determinants up to order 4.

The Complex Matrix is to be stored using the SandMatrix convention - which is identical to the HP-41 Advantage's. With this convention each complex number is represented by four elements in the complex matrix - refer to the manuals for details.
Code:
[[  Re(aij)  -Im(aij) ]
 [  Im(aij)   Re(aij) ]]

The SandMatrix comes well-equipped with routines to calculate the TRACE and integer powers of a matrix (M^2 and MPWR), therefore it lends itself rather nicely to the direct formulas using those elements, as described at:

https://en.wikipedia.org/wiki/Determinant


The program includes 4 sections:

1. A Complex Matrix trace routine, "CMTR"
2. Determinant of a Complex 2x2 Matrix, "CMDT2"
3. Determinant of a Complex 3x3 Matrix, "CMDT3"
4. Determinant of a Complex 4x4 Matrix, "CMDT4"

The last three can be grouped into a single one, using the matrix DIMension as criteria to branch to the appropriate section - but I left it as-is for easier readability. Also the code can be optimized putting repeated lines in independent subroutines; yet ditto as before.

Only CMDT4 is commented, as it's a superset of the other two and can be used as a reference. Here's the program listing:

Code:
01  LBL "CMTR"    63  2    
02  XEQ 01        64  /    
03  ZAVIEW        65  MNAME?    
04  RTN           66  RTN    
05  LBL "CMDT2"   67  LBL "CMDT4"    
06  ASTO 02       68  ASTO 02    
07  XEQ 01        69  XEQ 01     tr(A) = 3+i
08  Z^2           70  ZRPL^      fills Z-stack
09  ZENTER^       71  "|-,#"    
10  "|-,#"        72  MAT=       copy to scratch
11  MAT=          73  "#"        scratch matrix
12  "#"           74  3    
13  M^2           75  MPWR       cube power
14  XEQ 01        76  XEQ 01     tr(A^3) = -75+50i
15  Z-            77  Z*         tr(A).tr(A^3)
16  2             78  8    
17  GTO 02        79  ST* Z    
18  LBL "CMDT3"   80  *          8.tr(A).tr(A^3)
19  ASTO 02       81  Z<>W       tr(A)
20  XEQ 01        82  Z^2        tr(A)^2
21  Z^3           83  6    
22  LASTZ         84  ST* Z    
23  ZENTER^       85  *          6. tr(A)^2 
24  "|-,#"        86  ZENTER^    
25  MAT=          87  CLA    
26  "#"           88  ARCL 02    
27  M^2           89  "|-,#"    
28  XEQ 01        90  MAT=    
29  Z*            91  "#"        scratch matrix
30  3             92  M^2        squared
31  ST* Z         93  XEQ 01     tr(A^2) = 2 -4i
32  *             94  Z*         6. tr(A)^2 . tr(A^2)
33  Z-            95  LASTZ      TR(A^2)
34  ZENTER^       96  ZRDN       puts it in level "V"
35  CLA           97  Z-         8.tr(A).tr(A^3) - 6. tr(A)^2 . tr(A^2)
36  ARCL 02       98  Z<>W       tr(A)
37  "|-,#"        99  Z^2    
38  MAT=          100  Z^2       tr(A)^4
39  "#"           101  Z+        tr(A)^4 - 6. tr(A)^2 . tr(A^2) + 8.tr(A).tr(A^3)
40  3             102  ZRUP      tr(A^2)
41  MPWR          103  Z^2       tr(A^2)^2
42  XEQ 01        104  3    
43  2             105  ST* Z    
44  ST* Z         106  *    
45  *             107  Z+        3.tr(A^2)^2 + tr(A)^4 - 6. tr(A)^2 . tr(A^2) + 8.tr(A).tr(A^3)
46  Z+            108  ZENTER^    
47  6             109  M^2       fourth power
48  GTO 02        110  XEQ 01    tr(A^4) = -160(1-i)
49  LBL 01        111  6    
50  ,             112  ST* Z    
51  MSIJA         113  *         6.tr(A^4)
52  I+            114  Z-        3.tr(A^2)^2 + tr(A)^4 - 6. tr(A)^2 . tr(A^2) + 8.tr(A).tr(A^3) - 6.r(A^4)
53  MRC+          115  24    
54  LBL 00        116  LBL 02    
55  I+            117  ST/ Z    
56  J+            118   /    
57  J+            119  ZAVIEW    
58  MRC+          120  "#"    
59  +             121  PURFL    
60  FC? 09        122  CLA    
61  GTO 00        123  ARCL 02    
62  MTRACE        124  END

The complex matrix won't be altered in any way, as all operations are made on a scratch copy. It can be stored in X-Mem, CL_Y-Mem, or standard Data registers area. The easiest way to enter the matrix is by using the CMEDIT routine - which expects the matrix name in ALPHA. It expects the matrix already created, using 2n x 2n as dimension - with "n" being the order.

If you place it in the standard registers area, be aware that data registers R00, R01 are used by the routine MPWR for scratch. Additionally, data register R02 is used to store the Matrix Name (thus can't exceed 6 characters).

As you can see there are numerous 41Z functions - used for the complex arithmetic using the Complex Stack. This has the additional advantage that doesn't require additional data registers, be that standard or CL Y-RAM.

Examples.-

Calculate the determinant of the 4x4 Complex Matrix:

Code:
[[ 1+i   2+2i  3+3i   4+4i ]
 [  0     1   -3-3i  -4-4i ]
 [ -1+i  1-i    1      i   ]
 [  -i  -1+1    1      0   ]]

Solution: det = -62-8i

https://www.wolframalpha.com/input/?i=de...,+0%7D%7D)

The program is slow in non-turbo settings- there are lots of moving pars behind the scenes, despite the straight-forward program listing.
Using TURBO_50 the 4x4 determinant is obtained in 5 seconds approx.

The accuracy for integer matrices holds up nicely, giving exact integer real and imaginary parts in the solution.

Feel free to add your comments and suggestions for improvement.

Cheers,
ÁM

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
HP-41 Complex Determinants w/ SandMatrix and 41Z - Ángel Martin - 06-11-2018 08:51 AM



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