The Museum of HP Calculators


Determinants for the HP-41

This program is Copyright © 2006 by Jean-Marc Baillard and is used here by permission.

This program is supplied without representation or warranty of any kind. Jean-Marc Baillard and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.


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 )
 

-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

                                            |  4  9  2  |                                                 R01  R04  R07
Example:    Calculate  D =  |  3  5  7  |     Store these 9 numbers into    R02  R05  R08      respectively
                                            |  8  1  6  |                                                 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

                                            |   1    8   13  12  |                                                         R01   R05   R09   R13
Example:    Calculate  D =  |  14  11   2    7   |       Store these 16 numbers into        R02   R06   R10   R14      respectively
                                            |   4    5   16   9   |                                                         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

                                            |   1    7   13   19   25  |                                                         R01   R06   R11   R16   R21
                                            |  14  20  21    2     8   |                                                         R02   R07   R12   R17   R22
Example:    Calculate  D =  |  22   3    9    15   16  |       Store these 25 numbers into        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 )
 

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

  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                          ( or another "small" number to identify the "tiny" elements )
  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                       ( synthetic )
  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                        Delete lines 82 to 84 if you want to zero the sub-diagonal elements.
  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                     ( a three-byte GTO )
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.

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

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

                                                      8     1     6
-The triangularized matrix is now     0   8.5   -1                however, registers  R02             do not contain 0
                                                      0     0   90/17                                         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,
  but   if n = 7 ,  t  is of the order of 1 minute
        if n = 17 ,  ------------------ 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.
 

Go back to the HP-41 software library
Go back to the general software library
Go back to the main exhibit hall