Determinant

# Determinants for the HP-41

Overview

1°) Determinants of order 3
2°) Determinants of order 4
3°) Determinants of order 5  ( X-Functions Module required )
4°) Determinants of order n  ( n < 18 )

a) Program#1
b) Program#2

-Determinants of order n may be computed by the programs "LS" or "LS2" which are listed in "Linear and non-linear systems for the HP-41"
-However, all the following programs are faster and give more accurate results.

( For n = 2 , writing a subroutine like  LBL "D2"  RCL 01  RCL 04  *  RCL 02  RCL 03  *  -  END  would be probably wasteful )

1°) Deteminants of order 3

Data Registers:         R00 = unused                  ( Registers R01 thru R09 are to be initialized before executing "D3" )

•  R01 = a11   •  R04 = a12   •  R07 = a13
•  R02 = a21   •  R05 = a22   •  R08 = a23
•  R03 = a31   •  R06 = a32   •  R09 = a33
Flags: /
Subroutines: /

 01  LBL "D3" 02  RCL 05  03  RCL 09 04  * 05  RCL 06 06  RCL 08 07  * 08  - 09  RCL 01  10  * 11  RCL 02  12  RCL 09 13  * 14  RCL 03 15  RCL 08  16  * 17  - 18  RCL 04  19  * 20  - 21  RCL 02 22  RCL 06  23  * 24  RCL 03 25  RCL 05  26  * 27  - 28  RCL 07 29  * 30  + 31  END

( 38 bytes / SIZE 010 )

 STACK INPUT OUTPUT X / Deteminant

Example:      Calculate

|  4  9  2  |      Store these      R01  R04  R07
D =  |  3  5  7  |       9 numbers      R02  R05  R08      respectively
|  8  1  6  |           into            R03  R06  R09

and   XEQ "D3"  >>>>   Det = 360    ( in 1 second )

2°) Deteminants of order 4

-Determinants of order 4 are computed by a sum of products of determinants of order 2.

Data Registers:         R00 = determinant at the end                 ( Registers R01 thru R16 are to be initialized before executing "D4" )

•  R01 = a11   •  R05 = a12   •  R09 = a13   •  R13 = a14
•  R02 = a21   •  R06 = a22   •  R10 = a23   •  R14 = a24
•  R03 = a31   •  R07 = a32   •  R11 = a33   •  R15 = a34
•  R04 = a41   •  R08 = a42   •  R12 = a43   •  R16 = a44
Flags: /
Subroutines: /

 01  LBL "D4" 02  RCL 02  03  RCL 07 04  * 05  RCL 03 06  RCL 06 07  * 08  - 09  RCL 09 10  RCL 16 11  * 12  RCL 12 13  RCL 13 14  * 15  - 16  * 17  STO 00 18  RCL 01 19  RCL 07 20  * 21  RCL 03 22  RCL 05  23  * 24  - 25  RCL 10  26  RCL 16 27  * 28  RCL 12 29  RCL 14 30  * 31  - 32  * 33  ST- 00 34  RCL 01 35  RCL 06 36  * 37  RCL 02 38  RCL 05 39  * 40  - 41  RCL 11 42  RCL 16  43  * 44  RCL 12 45  RCL 15  46  * 47  - 48  * 49  ST+ 00 50  RCL 01 51  RCL 08 52  * 53  RCL 04 54  RCL 05 55  * 56  - 57  RCL 10 58  RCL 15 59  * 60  RCL 11 61  RCL 14  62  * 63  - 64  * 65  ST+ 00 66  RCL 02  67  RCL 08 68  * 69  RCL 04 70  RCL 06 71  * 72  - 73  RCL 09 74  RCL 15 75  * 76  RCL 11 77  RCL 13 78  * 79  - 80  * 81  ST- 00 82  RCL 03 83  RCL 08  84  * 85  RCL 04 86  RCL 07  87  * 88  - 89  RCL 09 90  RCL 14 91  * 92  RCL 10 93  RCL 13 94  * 95  - 96  * 97  ST+ 00 98  RCL 00 99  END

( 114 bytes / SIZE 017 )

 STACK INPUT OUTPUT X / Deteminant

Example:    Calculate

|   1    8   13  12  |        Store these         R01   R05   R09   R13
D =  |  14  11   2    7   |       16 numbers         R02   R06   R10   R14      respectively
|   4    5   16   9   |             into               R03   R07   R11   R15
|  15  10   3    6   |                                  R04   R08   R12   R16

XEQ "D4"  >>>>  Det =  0   ( in 3 seconds )

3°) Deteminants of order 5

-Determinants of order 5 are computed by a sum of products of determinants of order 2 by determinants of order 3.
-Registers R00 to R25 are temporarily disturbed during the calculations, but their contents are finally restored.

Data Registers:         R00 = det A   at the end                         ( Registers R01 thru R25 are to be initialized before executing "D5" )

•  R01 = a11   •  R06 = a12   •  R11 = a13   •  R16 = a14   •  R21 = a15
•  R02 = a21   •  R07 = a22   •  R12 = a23   •  R17 = a24   •  R22 = a25
•  R03 = a31   •  R08 = a32   •  R13 = a33   •  R18 = a34   •  R23 = a35
•  R04 = a41   •  R09 = a42   •  R14 = a43   •  R19 = a44   •  R24 = a45
•  R05 = a51   •  R10 = a52   •  R15 = a53   •  R20 = a54   •  R25 = a55
Flags: /
Subroutines: /

 01  LBL "D5" 02  CLX 03  STO 00      04  10.005 05  XEQ 01 06  ST- 00 07  5.005 08  XEQ 01  09  ST+ 00 10  5 11  XEQ 01 12  ST- 00 13  CLX 14  XEQ 01 15  ST+ 00 16  .005 17  XEQ 01 18  ST- 00 19  5.005 20  XEQ 01 21  ST+ 00 22  10.005 23  XEQ 01  24  ST- 00 25  CLX 26  XEQ 01  27  ST+ 00 28  5 29  XEQ 01      30  ST- 00 31  10 32  XEQ 01 33  ST+ 00 34  RCL 00 35  RTN 36  LBL 01 37  1.016005 38  + 39  REGSWAP 40  RCL 07      41  RCL 13  42  * 43  RCL 08  44  RCL 12      45  * 46  - 47  RCL 01 48  * 49  RCL 06 50  RCL 13 51  * 52  RCL 08 53  RCL 11 54  * 55  - 56  RCL 02  57  * 58  - 59  RCL 06      60  RCL 12 61  * 62  RCL 07 63  RCL 11 64  * 65  - 66  RCL 03 67  * 68  + 69  RCL 19 70  RCL 25  71  * 72  RCL 20      73  RCL 24 74  * 75  - 76  * 77  END

( 148 bytes / SIZE 026 )

 STACK INPUT OUTPUT X / Deteminant

Example:    Calculate

|   1    7   13   19   25  |                    R01   R06   R11   R16   R21
|  14  20  21    2     8   |                    R02   R07   R12   R17   R22
D =  |  22   3    9    15   16  |          =        R03   R08   R13   R18   R23       respectively
|  10  11  17   23    4   |                    R04   R09   R14   R19   R24
|  18  24   5     6    12  |                    R05   R10   R15   R20   R25

XEQ "D5"  >>>>  Det =  -4680000   ( in 19 seconds )

Notes:

-When these first 3 programs stop, registers R01 thru Rn2 are unchanged.
-All the examples are determinants of magic squares.
( those of "D4" & "D5" are panmagic squares )

4°) Deteminants of order n  ( n < 18 )

a) Program#1

-This program tries to triangularize the matrix A by Gaussian elimination.
-The following listing is very similar to "LS2" ( cf "Linear and non-linear Systems for the HP-41" )
-When a pivot equals zero, 0 is stored in register R00 and the algorithm stops.

Data Registers:         R00 = det A at the end                           ( Registers R01 thru Rn2 are to be initialized before executing "DET" )

•  R01 = a11   •  Rn+1 = a12  .....................  •  Rn2-n+1 = a1n
•  R02 = a21   •  Rn+2 = a22  .....................  •  Rn2-n+2 = a2n
........................................................................................

•  Rnn = an1   •  R2n = an2    .....................   •  Rn2 = ann

Flags:    CF 00 = a pivot p is regarded as zero if  | p | < 10-7  ( line 60 )                 CF 01 = partial pivoting
SF 00 =  ---------------------------- if    p = 0                                        SF 01 = no pivoting

Subroutines: /

-Line 60,  E-7 may be replaced by another "small" number to identify the "tiny" elements
-Delete lines 82 to 84 if you want to zero the sub-diagonal elements.
-Line 103 is a three-byte GTO 01

 01  LBL "DET"    02  STO Y   03  .1   04  %   05  +   06  STO N   07  FRC   08  STO O    09  *   10  1   11  STO 00          12  ST+ O   13  LASTX   14  %   15  +   16  +   17  LBL 01   18  STO M   19  FS? 01   20  GTO 04   21  INT   22  RCL O 23  FRC   24  +   25  ENTER^   26  ENTER^   27  CLX   28  LBL 02   29  RCL IND Z   30  ABS   31  X>Y?   32  STO Z   33  X>Y?   34  +   35  RDN   36  ISG Z   37  GTO 02   38  RCL M   39  ENTER^   40  FRC   41  R^   42  INT   43  +   44  X=Y? 45  GTO 04   46  LBL 03   47  RCL IND X   48  X<> IND Z   49  STO IND Y   50  ISG Y   51  RDN   52  ISG Y   53  GTO 03   54  RCL 00   55  CHS   56  STO 00          57  LBL 04   58  CLX   59  FC? 00   60   E-7   61  RCL IND M   62  ST* 00   63  ABS   64  X<=Y?   65  GTO 07   66  ISG O 67  X<0?   68  GTO 08   69  RCL O   70  STO P   71  LBL 05   72  RCL M   73  ENTER^   74  FRC   75  RCL P   76  INT   77  +   78  RCL IND X   79  RCL IND Z   80  /   81  SIGN   82  ISG Y   83  CLX   84  ISG Z   85  LBL 06   86  CLX   87  RCL IND Z   88  LASTX 89  *   90  ST- IND Y    91  ISG Y   92  CLX   93  ISG Z   94  GTO 06   95  ISG P   96  GTO 05   97  RCL N   98  ST+ O   99  SIGN 100  RCL M 101  + 102  ISG X 103  GTO 01 104  LBL 07 105  CLX 106  STO 00 107  LBL 08 108  RCL 00 109  CLA 110  END

( 168 bytes / SIZE n2+1 )

 STACK INPUT OUTPUT X n Deteminant

where n is the order of the square matrix.

Example:    Calculate

|  4  9  2  |                  R01  R04  R07
D =  |  3  5  7  |      into      R02  R05  R08      respectively
|  8  1  6  |                  R03  R06  R09

3   XEQ "DET"  >>>>   Det = 360    ( in 9 seconds )

-The triangularized matrix is now

8     1     6
0   8.5   -1                however,          R02             do not contain 0
0     0   90/17            registers           R03   R06

-If you want to zero these sub-diagonal elements, delete lines 82-83-84  ( but it will slow execution )

Notes:

-"DET" is approximately 35% faster than "LS2"
-Execution time t depends of course on the number of rows exchanges when pivoting,

if n =  7  ,  t  is of the order of  1 minute
if n = 17 ,  t  is of the order of 11 minutes

-If you prefer to choose the "small" number p that identifies tiny elements by yourself:

-Replace register M by register Q
-Replace lines 58 to 60 by  RCL M
-Replace line 02 by STO M   X<> Y

-In this case, the inputs must be:

 STACK INPUTS OUTPUTS Y n / X p det A

where n is the order of the matrix,
and  p is the "small" number you have choosen.

b) Program#2

-This variant is very similar to "LS1" ( cf "Linear and non-linear Systems for the HP-41" )
-You can store the coefficients of the matrix from Rbb to Ree provided  bb > 00

Data Registers:         R00 = det A at the end                           ( Registers Rbb thru Rbb-1+n2 are to be initialized before executing "DET" )

•  Rbb   = a11  ....................   •  Ree   = ann

Flags: /
Subroutines: /

 01 LBL "DET"  02 ENTER  03 INT  04 LASTX  05 FRC  06 ISG X  07 INT  08 STO O         09 RCL Y  10 +  11 DSE X  12  E3  13 /  14 +  15 X<> O  16 .1  17 %  18 +  19 STO M  20 SIGN  21 STO 00  22 X<>Y 23 STO P  24 STO N  25 LBL 11  26 RCL N  27 INT  28 RCL O  29 FRC  30 +  31 ENTER  32 ENTER  33 CLX  34 LBL 08  35 RCL IND Z   36 ABS  37 X>Y?  38 STO Z  39 X>Y?  40 +  41 RDN  42 ISG Z  43 GTO 08  44 RCL N 45 ENTER  46 FRC  47 R^  48 INT  49 +  50 X=Y?  51 GTO 00  52 LBL 09  53 RCL IND X  54 X<> IND Z  55 STO IND Y  56 ISG Y  57 RDN  58 ISG Y  59 GTO 09  60 RCL 00  61 CHS  62 STO 00  63 LBL 00  64 RCL IND N  65 ST* 00  66 X=0? 67 GTO 00  68 LBL 12  69 RCL N  70 ENTER  71 FRC  72 RCL O  73 INT  74 +  75 X=Y?  76 GTO 14  77 RCL IND X  78 RCL IND Z  79 /  80 SIGN  81 ISG Y  82 LBL 13  83 ISG Z  84 FS? 30  85 GTO 14  86 CLX  87 RCL IND Z  88 LASTX 89 *  90 ST- IND Y  91 ISG Y  92 CLX  93 GTO 13  94 LBL 14  95 ISG O  96 GTO 12  97 RCL M  98 FRC  99 ST+ O 100 SIGN 101 ST+ N 102 ST+ O 103 ISG N 104 GTO 11 105 LBL 00 106 X<> P 107 SIGN 108 RCL 00 109 CLA 110 END

( 168 bytes / SIZE ??? )

 STACK INPUT OUTPUT X bbb.eeenn Deteminant L / bbb.eeenn

where n is the order of the square matrix and  bbb > 000

Example:    Calculate the following determinant assuming the coefficients are stored in R11 to R19

|  4  9  2  |                  R11  R14  R17
D =  |  3  5  7  |      into      R12  R15  R18      respectively
|  8  1  6  |                  R13  R16  R19

11.01903   XEQ "DET"  >>>>   Det = 360    ( in 10 seconds )

Note:

-This program does not check that the control number is valid.