The Museum of HP Calculators

# Hypermatrix and Magic Cubes for the HP-41

This program is Copyright © 2004 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

-An hypermatrix  [ ai,j,k ] ( 1<= i <= n ; 1 <= j <= m ; 1 <= k <= p )  is actually equivalent to a p-list of n x m  matrices.
-Two programs are listed herafter:

1°) "HYPMAT" creates an   n x m x p hypermatrix provided its elements verify  ai,j,k = f(i,j,k)  where  f is a known function.
2°) "T" allows to compute the elements of a p x p x p magic cube, where p is a prime > 2  ( and p < 999 )

-If p = 3 or 5  the cube is an Andrews magic cube ( the sums equal the magic constant p(p3+1)  in the 3 directions and the 4 space diagonals )
-If p = 7         the cube is perfect pandiagonal ( all the orthogonal sections are panmagic squares )
-If p = 11 , 13 , ... ,  we have a panmagic cube ( all the orthogonal or diagonal sections - broken or unbroken - are magic squares )

1°) Creating an Hypermatrix

-The elements of the hypermatrix  [ ai,j,k ] are stored as shown below.
-If n.m.p > 318 , replace line 42 by VIEW X and the coefficients will be displayed ( from the last to the first )

Data Registers:           •  R00 =  n.mmm,ppp = n + m/1,000 + p/1,000,000   ( this register is to be initialized before executing "HYPMAT" )

and when the program stops:

R01 = a1,1,1        Rn+1 = a1,2,1   .......................................     Rnm-n+1 = a1,m,1
R02 = a2,1,1        Rn+2 = a2,2,1   .......................................     Rnm-n+2 = a2,m,1
..................................................................................................................
Rn   = an,1,1        R2n  =  an,2,1   ........................................    Rnm = an,m,1

Rnm+1 = a1,1,2        Rnm+n+1 = a1,2,2   .......................................     R2nm-n+1 = a1,m,2
Rnm+2 = a2,1,2        Rnm+n+2 = a2,2,2   .......................................     R2nm-n+2 = a2,m,2
..................................................................................................................................
Rnm+n = an,1,2        Rnm+2n  =  an,2,2   ........................................    R2nm = an,m,2

..........................................................................................................
..........................................................................................................
..........................................................................................................

Rnm(p-1)+1 = a1,1,p        Rnm(p-1)+n+1 = a1,2,p   .......................................     Rnmp-n+1 = a1,m,p
Rnm(p-1)+2 = a2,1,p        Rnm(p-1)+n+2 = a2,2,p   .......................................     Rnmp-n+2 = a2,m,p
....................................................................................................................................................
Rnm(p-1)+n = an,1,p        Rnm(p-1)+2n  =  an,2,p   ........................................    Rnmp = an,m,p

Flag: /                                                                                                           Z = k
Subroutine:  a program called "T" ( global label )  changing the stack from    Y = j      to    X' = ai,j,k  without disturbing synthetic register M
X = i

-In other words, it must calculate   ai,j,k  = f(i,j,k)  assuming  i , j , k are in registers X , Y , Z ( respectively ) upon entry.

01  LBL "HYPMAT"
02  RCL 00
03  INT
04  LASTX
05  FRC
06   E3
07  *
08  INT
09  *
10  STO 01
11  RCL 00
12   E6
13  ST* Y
14  SQRT
15  MOD
16  *
17  STO M
18  LBL 01
19  RCL M
20  1
21  -
22  STO Y
23  RCL 01
24  /
25  INT
26  RCL Y
27  RCL 01
28  MOD
29  RCL 00
30  INT
31  /
32  INT
33  R^
34  RCL 00
35  INT
36  MOD
37  1
38  ST+ T
39  ST+ Z
40  +
41  XEQ "T"
42  STO IND M
43  DSE M
44  GTO 01
45  CLA                                this line is superfluous if the subroutine doesn't disturb registers N & O.
46  END

( 71 bytes / SIZE nmp+1 )

Example:   n = 4 ; m = 5 ; p = 3  and   ai,j,k = ( i2 + j3 + k4 ) modulo (pi)

-Load this short routine in program memory:

LBL "T"
X^2
X<>Y
ENTER^
X^2
*
+
X<>Y
X^2
X^2
+
PI
MOD
RTN

-Then   4.005003   STO 00  XEQ "HYPMAT"
yields  ( in 75 seconds )         R01 = 3 ; R02 = 2.8584 ; R03 = 1.5752 ; ..... ; R21 = 2.2920 ; .....; R41 = 1.3186 ; ..... ; R60 = 2.0885

2°) Magic Cubes ( of order p , assuming p is a prime and p > 2 )

-The following program calculates the elements of a magic cube   ai,j,k  assuming  i , j , k are in registers X , Y , Z ( respectively ) upon entry.

Data Registers:           •  R00 = p.ppp,ppp    ( R00 is to be initialized )
R01 to Rp3 = the coefficents of the magic cube
Flags: /
Subroutines: /

01  LBL "T"
02  1
03  ST- T
04  ST- Z
05  -
06  STO N
07  RDN
08  STO O
09  RCL 00
10  INT
11  7
12  -
13  X<0?
14  GTO 01
15  X=0?
16  GTO 02
17  X<> T
18  ST+ X
19  +
20  ST+ X
21  RCL N
22  +
23  RCL X
24  RCL Z
25  +
26  RCL X
27  RCL O
28  +
29  GTO 03
30  LBL 01
31  X<> T
32  +
33  RCL N
34  ST- Y
35  ST+ X
36  RCL Y
37  +
38  RCL O
39  ST+ X
40  CHS
41  X<>Y
42  ST+ Y
43  R^
44  ST+ X
45  -
46  GTO 03
47  LBL 02
48  CLX
49  RCL N
50  +
51  ST+ X
52  +
53  CHS
54  RCL N
55  CHS
56  X<>Y
57  ST+ Y
58  RCL O
59  CHS
60  X<>Y
61  ST+ Y
62  R^
63  ST+ X
64  -
65  RCL N
66  +
67  LBL 03
68  RCL 00
69  INT
70  MOD
71  X<>Y
72  LASTX
73  ST* Z
74  MOD
75  +
76  X<>Y
77  RCL 00
78  INT
79  ST* Z
80  MOD
81  +
82  1
83  +
84  END

( 125 bytes )

Example:      3.003003  STO 00  XEQ "HYPMAT"  stores the following Andrews magic cube

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

-As you can check, the sums equal 42 in the  x- , y- , z- directions
and the space diagonals 1 + 14 + 27 = 26 + 14 + 2 = 10 + 14 + 18 = 24 + 14 + 4 = 42

Notes:

-If p = 7 , 11 , 13 , 17 , .... , replace line 42 by VIEW X in the "HYPMAT" listing to see the successive coefficients of the magic cube.
-A perfect magic cube of order 5 has been discovered last year, but this program cannot calculate its elements.