hp41programs

Matrix Creating a Matrix for the HP-41
 

Overview
 

 1°)  Main Program
 2°)  Pascal's Matrices
 3°)  nxn Magic Squares ( where n is odd )
 4°)  nxn Panmagic Squares ( where n is neither a multiple of 2 nor a multiple of 3 )
 5°)  Random Matrices
 6°)  Random Symmetric Matrices
 
 

1°) Main program
 

-A real matrix [ai,j]    ( 1 <= i <= n ; 1 <= i <= m )  is an array of n.m real numbers

        a1,1 a1,2 ........... a1,m
        a2,1 a2,2 ........... a2,m
          .    .    ............   .                         ( ai,j   is in the i-th row of the j-th column ).
          .    .    ............   .
        an,1 an,2 ........... an,m

-n is the number of rows and m is the number of columns.
-"MATRIX"  computes and stores these elements into contiguous registers ( in column order )
  if they can be defined by  ai,j = f ( i ; j )  where f is a function of 2 variables.

-To create a matrix  [ai,j]  and store its elements into contiguous registers from Rbb to Ree   (  ee = bb + n.m -1 and  bb > 00 ):

    1°) Load the function f into program memory ( global label, 6 characters or less )
    2°) Store its name into register R00
    3°) Place the control number of the matrix  bbb.eeenn  in X-register  ( n = the number of rows ) and XEQ "MATRIX"
 

    a1,1 a1,2 ........... a1,m                                         Rbb        Rbb+n                             Rbb+n.m-n
    a2,1 a2,2 ........... a2,m                                         Rbb+1    Rbb+n+1                        Rbb+n.m-n+1
      .     .    ...........  .                will be in                  .......................................................................                       respectively.
      .     .    ...........  .
    an,1 an,2 ........... an,m                                        Rbb+n-1  Rbb+2n-1                      Rbb+n.m-1 = Ree
 
 

Data Registers:      •  R00 = function name          ( Register R00 is to be initialized before executing "MATRIX" )

                                  and the registers containing the matrix elements ( no other register is used )

Flags: /
Subroutine:      The function f  ( assuming i is in X-register and j is in Y-register upon entry )
 
        If needed, you have also 1 in L-register,
        n = the number of rows in N-register and
        k = the pointer value of the current element in M-register ( from 1 to n.m , counted in column order )

N.B:

-Synthetic registers M N O can be replaced by R01 R02 R03 but, in this case, delete line 31,
  replace line 02 with 0 STO 01 RDN in the following listing and choose bb > 03.
 
 

01  LBL "MATRIX"
02  CLA
03  ENTER^
04  FRC
05  ISG X
06  INT
07  STO N
08  1
09  -
10   E5
11  /
12  -
13  STO O             
14  LBL 01
15  RCL M
16  STO Y
17  RCL N
18  /
19  INT
20  X<>Y
21  RCL N             
22  MOD
23  1
24  ST+ Z
25  ST+ M
26  +
27  XEQ IND 00   
28  STO IND O
29  ISG O
30  GTO 01            
31  CLA
32  END

 
  ( 57 bytes )
 

Example:   Store the elements of the 3x4 matrix  [ai,j]  where  ai,j = i2 + j3  into registers R01 thru R12.

  01  LBL "T"      defines the function  f(i;j) = i2 + j3
  02  X^2
  03  X<>Y
  04  3
  05  Y^X
  06  +
  07  RTN

      "T"    ASTO 00
1.01203  XEQ "MATRIX"   and 13 seconds later,
 

      2   9   28  65                  R01  R04  R07  R10
      5  12  31  68        =        R02  R05  R08  R11       respectively
     10 17  36  73                  R03  R06  R09  R12
 

Note:

-If your matrix is actually a vector ( only one row )  you can key in  bbb.eee  instead of  bbb.eee01
 

2°) Pascal's Matrices
 

-These matrices [ai,j]  are defined by  ai,j = Ci+j-2i-1    where  Cnp = n! / ( p! ( n-p)! )  are the binomial coefficients.
-The following program calculates ( and stores ) these numbers without any roundoff error.
 
 

 01  LBL "PSL"
 02  SIGN
 03  X=Y?
 04  RTN
 05  LASTX
 06  X=Y?
 07  RTN
 08  CLX
 09  RCL O
 10  ST- Y
 11  RCL N
 12  -
 13  RCL IND Y
 14  RCL IND Y
 15  +
 16  END

 
  ( 29 bytes )
 

Example:    Create the 4x4 Pascal's matrix in register R11 thru R26

      "PSL"  ASTO 00
11.02604  XEQ "MATRIX" and 14 seconds later,

    1   1   1   1                   R11    R15   R19   R23
    1   2   3   4        =         R12    R16   R20   R24        respectively
    1   3   6  10                  R13    R17   R21   R25
    1   4  10 20                  R14    R18   R22   R26
 
 

3°) nxn Magic Squares ( where n is odd )
 

-The elements of this matrix are  1 , 2 , ............. , n2  and it has the following property:
 All its row sums, column sums and both diagonal sums are equal to the magic constant   M = n.(n2 + 1)/2
 
 

01  LBL "MAG"
02  RCL Y
03  X<>Y
04  -
05  ST+ Y
06  RCL N
07  DSE X
08  2
09  /
10  +
11  RCL N        
12  X<=Y?
13  ST- Y
14  X<>Y
15  X<0?
16  +
17  STO Y        
18  R^
19  RCL N        
20  ST* T
21  X<=Y?
22  ST- Y
23  X<>Y
24  X<=0?
25  +
26  R^
27  +
28  END         

 
  ( 46 bytes )
 

Example:     Create a 3x3 magic matrix in registers  R04 thru R12

  "MAG"  ASTO 00
4.01203  XEQ "MATRIX"  yields ( in 13 seconds )

   4    9    2            R04   R07   R10
   3    5    7     =     R05   R08   R11     respectively    ( all the rows, columns and main diagonals sum to 15 )
   8    1    6            R06   R09   R12

Note:   This program doesn't work if n is even
 

4°)  nxn Panmagic Squares ( where n is a neither a multiple of 2 nor a multiple of 3 )
 

-A panmagic square ( also called pandiagonal square ) is a magic square
 where all broken diagonal sums are also equal to the magic constant  M = n.(n2 + 1)/2
 
 

01  LBL "PMAG"
02  LASTX
03  ST- Z
04  -
05  STO Z
06  X<>Y
07  ENTER^
08  ST+ X          
09  ST+ Y
10  R^
11  ST+ Z
12  +
13  RCL N          
14  MOD
15  LASTX
16  *
17  X<>Y
18  LASTX         
19  MOD
20  +
21  1
22  +
23  END            

 
   ( 38 bytes )
 

Example:   Create a 5x5 panmagic matrix in registers R01 thru R25

 "PMAG"   ASTO 00
 1.02505    XEQ "MATRIX"   produces  ( in 30 seconds )

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

-Each row sum, column sum, main diagonal sum and "broken" diagonal sum is equal to 65
 ( for instance  19 + 8 + 22 + 11 +  5  = 65
                  ...   1  + 8 + 15 + 17 + 24 = 65  ... etc ... )

Notes:

1-If  n > 17 there are not enough registers to store nxn numbers, but if you replace line 28 of the "MATRIX" listing by VIEW X,
    the successive elements of the panmagic square will be displayed. ( and if you set flag F21 the program will stop at each VIEW X ).
2-If  31 < n < 100   add also CLX after line 29 and, for instance:

  "PMAG"  ASTO 00
  1.10097   XEQ "MATRIX"   will display     1  99  197  295  393  491  ........    which are the 9409 elements of a 97x97 pandiagonal square.
 

5°)  Random Matrices
 

-This short routine stores pseudo-random integers ( between 1 and N )
  into registers Rbb , ........ , Ree
-No other register is used.
 
 

 01  LBL "RANM"
 02  LBL 01
 03  R-D
 04  FRC
 05  RCL X
 06  X<> Z
 07  ST* Z
 08  X<> Z
 09  INT
 10  ISG X
 11  CLX
 12  STO IND T
 13  RDN
 14  ISG Z
 15  GTO 01
 16  END

 
  ( 33 bytes )
 
 

      STACK        INPUTS      OUTPUTS
           Z      bbb.eee(ii)             /
           Y            N            N
           X          seed             /

   where bbb.eee(ii) is the control number of the array

Example:    Store random integers between 1 and 12 into registers   R01 , R02 , ........ , R07  with seed = 1

  1.007  ENTER^
    12     ENTER^
     1      XEQ "RANM"  and in 3 seconds,   R01 = 4   R02 = 12   R03 = 4   R04 = 7   R05 = 12   R06 = 2   R07 = 5

Notes:

-Do not choose seed = PI
-The random number generator ( R-D  FRC )  may be replaced by a better one.
 

6°)  Random Symmetric Matrices
 

-To get a random symmetric matrix, we could create a random matrix A and add A to its transpose.
-"RANSM" directly stores a nxn symmetric matrix in registers  R01 thru  Rnn
-No other register is used.
 
 

01  LBL "RANSM"
02  STO N
03  RDN
04  STO O
05  CLX
06   E3
07  /
08  STO M
09  1
10  %
11  LASTX           
12  ST+ Z
13  +
14  LBL 02
15  RCL N
16  R-D
17  FRC
18  STO N
19  RCL O
20  *
21  INT
22  ISG X             
23  CLX
24  STO IND Y
25  STO IND Z
26  RDN
27  ISG X
28  CLX
29  ISG Y
30  GTO 02          
31  FRC
32  RCL M
33  ST+ Z
34  SIGN
35  ST+ M
36  +
37  RCL Y           
38  INT
39  +
40  ISG Y
41  GTO 02
42  CLA
43  END

 
  ( 73 bytes / SIZE n2+1 )
 
 

      STACK        INPUTS      OUTPUTS
           Z            n             /
           Y            N             /
           X          seed             /

 
Example:   Store a random symmetric matrix of order 5 - elements between 1 and 12 - in registers R01 thru R25 with seed = 2

   5    ENTER^
  12   ENTER^
   2    XEQ "RANSM"  it yields, in 10 seconds

   R01   R06   R11   R16   R21                8    11    3     3     6
   R02   R07   R12   R17   R22               11   12   12    7    12
   R03   R08   R13   R18   R23      =        3    12    9     6    10
   R04   R09   R14   R19   R24                3     7     6     5     5
   R05   R10   R15   R20   R25                6    12   10    5    10