Post Reply 
[VA] SRC #015 - HP-15C & clones: Big NxN Matrix Inverse & Determinant
09-01-2023, 03:25 PM
Post: #14
RE: [VA] SRC #015 - HP-15C & clones: Big NxN Matrix Inverse & Determinant
The following routines will solve a real system up to 11x11 (GSB B) or a complex system up to 6x6 (GSB C).

Subroutine B will of course be able to solve a 5x5 complex system the usual way, ie by
transforming it into a 10x10 real system, but a 12x12 real system takes too much space.
Making use of the special structure of a complex linear equation, however, it is possible to go up to a 6x6 using routine C.
Solving 12x12 and 13x13 real systems remains possible, with my previous routine (in this thread) that requires a special setup.
In contrast, these new routines do all the partitioning and re-assembling for you.
Both routines solve A*C=B, so input MATRIX A and B, perform either GSB B or GSB C, and the result will be in MATRIX C.
Additionally, the complex routine expects the matrices to be in c-form, so with real and complex parts alternating:

a11r a11c a12r a12c ...
a21r a21c a22r a22c ...

with suffix -'r' being the real part and -'c' the complex part of a number. See the Owner's handbook pg 160 'Calculations with Complex Matrices' for more information on the various formats of complex matrices and how to switch between them.
The result C will also be in that format.
The drawback of this ease of use is the size of 157 bytes - but you may leave off either routine B or C, depending on your needs.

Overview:

routine B: 45 lines, 58 bytes
solves a real system A*C=B of order n>8
If A is nxn and B is nxm, it uses n*(2*n+m-8) registers, eg solving a single 11x11 system needs 11*15=165 registers

routine C: 46 lines, 59 bytes
solves a complex system A*C=B.
If A is nx2n and B is nx2m, it uses n*(4*n+3*m) registers eg a 6x6 system with 1 right hand side would need 6*27=162 registers

subroutines needed for both routines (35 lines, 40 bytes):

Subroutine 1
splits a matrix (in stack register X) in a top and bottom part; the row size of the top part is in stack register Y.
The matrix itself will be redimensioned to hold the top part only; the bottom part will be in MATRIX C.

Subroutine 2
joins MATRIX C (top) with the matrix in stack register X (bottom). The result will be in MATRIX C.

Subroutine 3
moves elements from the matrix in I to MATRIX C. Used by both subroutine 1 and 2.

6x6 complex example

A
(2,5) (6,5) (1,9) (3,5) (5,6) (1,4)
(6,7) (5,1) (0,5) (4,8) (0,5) (4,4)
(9,5) (4,7) (2,7) (2,3) (3,0) (8,5)
(9,6) (2,8) (1,0) (7,9) (4,3) (9,4)
(6,2) (8,0) (0,3) (2,4) (7,5) (9,4)
(0,7) (4,6) (4,0) (5,3) (4,8) (8,4)

f USER to turn USER mode ON
f MATRIX 0
6 ENTER 12 f DIM A
f MATRIX 1
2 STO A
5 STO A
6 STO A
5 STO A
...
8 STO A
4 STO A

6 ENTER 2 f DIM B
1 STO B

GSB C (or just 'C' in USER mode)

RCL C -7.3378-03
RCL C 2.4292-02
..

to get

C
(-7.3378-03, 2.4292-02)
( 1.9606-02,-4.7971-02)
( 3.3200-02,-3.5196-02)
(-6.0637-03,-4.2733-02)
( 7.6352-02,-1.4428-02)
(-8.7514-02, 3.6901-02)

157 bytes, 126 lines:

@ solve A*C=B
@ 157 bytes
@ input A and B, result in C=inv(A)*B
@
@ complex solve A*C=B
@ all in c-form
@ up to order 6
@                    matrix:    A       B       C       D       E
001 LBL C               @      -----------------------------------
002 RCL MATRIX A        @       ABc     XYc
003 Py,x
004 RCL DIM A           
005 RCL MATRIX B        @       A       X
006 Py,x                @       B       Y
007 GSB 1               @
008 STO MATRIX E        @       A       X       Y               Y
009 RCL DIM A           @       B
010 RCL MATRIX A
011 GSB 1
012 STO MATRIX D        @       A       X       B       B       Y
013 MATRIX 4
014 CHS
015 RCL MATRIX B
016 MATRIX 4
017 GSB 2
018 MATRIX 4            @       A       X      -BX      B       Y
019 RCL MATRIX A
020 STO MATRIX B
021 RESULT C
022 /
023 STO MATRIX A        @       CX      A       CX      B       Y
024 RCL DIM D
025 RCL MATRIX A
026 MATRIX 4
027 GSB 1
028 RCL MATRIX A
029 MATRIX 4            @       C       A       Xt      B       Y
030 RCL MATRIX D
031 RCL MATRIX C
032 MATRIX 4            @                       X
033 RESULT E
034 MATRIX 6            @                                      Y-BX
035 RCL MATRIX D
036 RCL MATRIX A
037 RESULT B
038 MATRIX 6            @              A-BC
039 RESULT E
040 /                   @                                      Y/A
041 RESULT C
042 MATRIX 6            @                      X-CY
043 RCL MATRIX E
044 GSB 2
045 Cy,x                @                      XYc
046 RTN

@ real solve A*C=B
@                    matrix:    A       B       C       D       E
047 LBL B               @      -----------------------------------
048 8                   @       AB      X
049 RCL MATRIX B        @       CD      Y
050 GSB 1
051 STO MATRIX E        @       AB      X       Y               Y
052 8                   @       CD
053 RCL MATRIX A
054 GSB 1
055 STO MATRIX D        @       AB      X       CD      CD      Y
056 8
057 RCL MATRIX A
058 MATRIX 4
059 GSB 1
060 MATRIX 4
061 RCL MATRIX A
062 MATRIX 4            @       A       X       B       CD      Y
063 RESULT C
064 /
065 RCL MATRIX B
066 RCL MATRIX A
067 RESULT B            @               -1      -1
068 /                   @              A .X    A .B     CD      Y
069 RCL MATRIX C
070 STO MATRIX A        @       B       X       B       CD      Y
071 8
072 RCL MATRIX D
073 MATRIX 4
074 GSB 1               @                                t
075 MATRIX 4            @       B       X       D       C       Y
076 RCL MATRIX A        @ prepare line 089 ;-)
077 RCL MATRIX D
078 MATRIX 4            @                               C
079 RCL MATRIX B
080 RESULT E
081 MATRIX 6            @                                      Y-C.X
082 RCL MATRIX D
083 RCL MATRIX A
084 RESULT C
085 MATRIX 6            @                      D-C.B
086 RESULT E
087 /
088 RESULT B
089 MATRIX 6            @              X-B.Y
090 STO MATRIX C
091 RCL MATRIX E

@ combine C=X and Z=Y to
@ C= | X |
@ | Y |
@ In: X: Z=Y
@ Z is any matrix but C
@ Out: C= | X |
@ | Y |
@

092 LBL 2
093 STO I
094 RCL DIM C
095 X<>Y
096 CHS
097 MATRIX 1
098 STO- 0
099 X<>Y

100 LBL 0
101 RCL DIM I
102 X<>Y
103 R^
104 -
105 X<>Y
106 DIM C

@ core subroutine
@ copy I -> C

107 LBL 3
108 RCL 0
109 LASTX
110 +
111 RCL 1
112 RCL g (i)
113uSTO C @ enter in USER mode!
114 GTO 3
115 RCL MATRIX C
116 RTN

@ split Z=| X |
@ | Y |
@ In:
@ Y: rX (#rows of X)
@ X: matrix Z=XY
@ Z is anything but C
@ Out:
@ X: MAT C
@ Z=X and C=Y

117 LBL 1
118 STO I
119 MATRIX 1
120 GSB 0
121 RCL DIM I
122 LASTX
123 X<>Y
124 DIM I
125 RCL MATRIX C
126 RTN


Hope you like it,
Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: [VA] SRC #015 - HP-15C & clones: Big NxN Matrix Inverse & Determinant - Werner - 09-01-2023 03:25 PM



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