Gauss-Jordan Elimination with HP 42S

The integrated solver for linear systems in the HP 42S is very powerful for a non-graphical calculator. However, the solver is not able to solve systems with free variables and can therefore not be used to row reduce a matrix into its reduced echelon form. I wrote a program that will do the Gauss-Jordan elimination on any augmented matrix.

I used the undocumented [MAX] and [MIN] function that was discovered by Joseph K. Horn. In the program below you can see a part about correction of the precision error of the HP 42S. The error appears when doing division and multiplication with sub matrices. The error can be verified on your calculator by calculating 62 - 6.

The program takes an existing matrix in the X register and row reduces it to the reduced echelon form. The matrix remains in the X register. The execution may take some time, depending on the size of the matrix.

Below the program sources, comments are on the right side:

    01 LBL "RREF"
    02 MAT?
    03 STO "MATA"
    04 INDEX "MATA"
    05 I-
    06 RCLIJ
    07 STO 06       // # of columns
    08 X<>Y
    09 STO 07       // # of rows
    10 I+
    11 1

    12 LBL 00       // search pivot
    13 STO 01	    //pivot row

    14 LBL 01
    15 [MAX]
    16 XO?
    17  GTO 02
    18 J+
    19 FC? 76
    20  GTO 01
    21 GTO 05

    22 LBL 02       // found pivot
    23 ABS
    24 [MIN]
    25 ABS
    26 X<>Y
    27 R↓
    28 XY?
    29  R↑
    30 R↑
    31 RCL 01
    32 R<>R
    33 RCLEL        // precision error check
    34 ABS
    35 1E-11
    36 XO?
    37  GTO 22
    38 0
    39 STOEL
    40 GTO 01
    41 LBL 22       // end of precision error
    42 RCLIJ
    43 RCL 06
    44 X<>Y
    45 -
    46 1
    47 +
    48 STO 02
    49 1
    50 X<>Y
    51 GETM
    52 RCLEL
    53 ÷
    54 STO "MATB"
    55 PUTM

    56 LBL 03       // row reduce
    57 I+
    58 FS? 76
    59  GTO 04
    60 RCLEL
    61 X=0?
    62  GTO 03
    63 1
    64 RCL 02
    65 GETM
    66 X<>Y
    67 ÷
    68 RCL "MATB"
    69 -
    70 PUTM
    71 GTO 03

    72 LBL 04       // seek to new pivot line
    73 FS? 77
    74  GTO 05
    75 RCLIJ
    76 RCL 01
    77 X<>Y
    78 STOIJ
    79 I+
    80 FS? 76
    81  GTO 05
    82 GTO 00

    83 LBL 05       // backward pivoting
    84 RCL 01
    85 1
    86 STOIJ
    87 LBL 55
    88 RCLEL
    89 X≠0?
    90  GTO 56
    91 J+
    92 FC? 76
    93  GTO 55
    94 J-
    95 I-
    96 FS? 76
    97  GTO 99
    98 RCLIJ
    99 X<>Y
   100 1
   101 STOIJ
   102 GTO 55
   103 LBL 56
   104 RCLIJ
   105 X<>Y
   106 STO 01
   107 X<>Y
   108 RCL 06
   109 X<>Y
   110 -
   111 1
   112 +
   113 STO 02
   114 1
   115 X<>Y
   116 GETM
   117 STO "MATB"

   118 LBL 06
   119 I-
   120 FS? 76
   121  GTO 07
   122 RCLEL
   123 X=0?
   124  GTO 06
   125 1
   126 RCL 02
   127 GETM
   128 X<>Y
   129 RCL "MATB"
   130 x
   131 -
   132 PUTM
   133 GTO 06
   
   134 LBL 07
   135 RCLIJ
   136 RCL 01
   137 X<>Y
   138 STOIJ
   139 I-
   140 FS? 76
   141  GTO 99
   142 RCLIJ
   143 X<>Y
   144 STO 01
   145 GTO 05
   146 LBL 99
   147 RCL "MATA"
   148 .END.
    


If there are any bugs or questions, contact me at
Valentin Koch