Matrix for the HP-48S/SX/G/GX


Overview

 1°) Magic Squares & Cubes

  a)  Magic Squares
  b)  Magic Square ?
  c)  Panmagic Squares
  d)  Panmagic Square ?
  e)  Bimagic Squares
  f)  Magic Multiplication
  g) Magic & Panmagic Cubes
  h) Magic Cube ?
  i) Primality ?


 2°)  Knight's Tour
 3°)  Hartley Transform

   a)  Discrete Hartley Transform ( Dim1 )
   b)  
Discrete Hartley Transform ( Dim2 )
   c)  Discrete Hartley Transform ( Dim3 )
   d)  Hartley Transform with Filon Integration
   e)  Continous Data - Integration


 4°)  Eigenvalues & Eigenvectors - Jacobi's Method
 5°)  Linear Systems

   a)  Trangularization ( Householder Method )
   b)  Overdetermined Systems
   c)  Underdetermined Systems


 6°)  Linear Programming - Simplex Method
 7°)  Creating an Array
 8°)  Random Matrix
 9°)  Trace of a Square Matrix
10°) Rank of a Matrix
11°) Pseudo-Inverse ( Greville's Method )
12°) Power of a Square Matrix
13°) Matrix Square-Root
14°) Matrix N-th Root
15°) Exponential of a Square Matrix
16°) Logarithm of a Square Matrix
17°)  GETC
18°)  eps

 

1°)  Magic Squares & Cubes
 

     a) Magic Squares
 

-The elements of a normal magic square 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

 
  • With MAG, the element ai,j of a magic square of odd order n  is calculated by  ai,j = 1 + ( i + 2 j - 2 ) mod n + n [ ( i + j - 1 + Int(n/2) ) mod n ]

  • If n = 4.m , MAG  calculates  ai,j   

    via the following method:

-Let  x' = 0  if  1 <= x <= n/2
 and  x' = 1  if  n/2 < x <= n

-Let  x" = n + 1 - x

-If  ( i + j + i' + j' ) mod 2 = 1  then   ai,j =  n ( i - 1 ) + j
-If  ( i + j + i' + j' ) mod 2 = 0  then   ai,j =  n ( i" - 1 ) +  j"

  • And if n = 4m+2 , the formulas are more complicated:

  Let   t = n/2

    u = ( i' - j' ) mod t                  where           x' = min { x , n+1-x }
    v = 2 i" + j"                              and            x" = 0  if  0 < x <= n/2  &  x" = 1  if  n/2 < x <= n

  Then,  d(u,v) is defined by the table ( in the last 2 rows, x > 0 so that u > 2 ):
 
 

   d(u,0)    d(u,1)    d(u,2)    d(u,3)
   d(0,v)        3        1        2       0
   d(1,v)        1        3        0       2
   d(2,v)        0        1        3       2
d(2x+1,v)        0        1        2       3
d(2x+2,v)        3        2        1       0

 
   Finally,  ai,j =  t2 d(u,v)  +  t  [  ( i' - j' + (t-1)/2 )  mod t ]  +  [  ( i' + j' - (t+1)/2 )  mod t ] + 1
 

          STACK           INPUT         OUTPUT
           Level 1                n       Magic Square

Examples:

 
  •   n = 3        3   MAG    gives:

       8   1   6
       3   5   7
       4   9   2

-Magic Constant = 15

  •   n = 4        4   MAG    gives:
 
   16   02   03   13
   05   11   10   08
   09   07   06   12
   04   14   15   01

-The magic constant = 34

  •   n = 6        6   MAG    gives:

   31  02  18  36  11  13
   17  33  01  10  15  35
   03  16  32  14  34  12
   30  07  23  05  25  21
   08  24  28  19  06  26
   22  29  09  27  20  04

-The magic constant = 111


       b) Magic Square  ?


- MGS?  tests if the matrix in level 1 is a magic square
-If the answer is "yes" it gives the magic constant
-Otherwise the HP48 displays "Error: Bad Argument Value"


          STACK           INPUT         OUTPUT
           Level 1               M       Magic Constant

  Where  M  is a square matrix

Example:

 
     [[  8   1   6 ]
      [  3   5   7 ]
      [  4   9   2 ]]        MGS?     ->     15


     c) Panmagic Squares


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

  •  The element ai,j of a panmagic square of order n where n is neither a mutiple of 2 nor a multiple of 3 is calculated by

       ai,j = 1 + [ ( i - 1 ) + 3 ( j - 1 ) ] mod n + n { [ ( i - 1 ) + 2 ( j - 1 ) ] mod n }

  •  If  n = 4.m ,  we calculate

    [ ( i - 1 ) + ( n/2 ) ( j - 1 ) ] mod n = x       if   x >= n/2   x' = 3n/2 - x - 1  &  if  x < n/2 ,  x' = x
    [ ( n/2 ) ( i - 1 ) + ( i - 1 ) ] mod n = y       if   y >= n/2   y' = 3n/2 - y - 1  &  if  y < n/2 ,  y' = y

-Then,  ai,j = 1 + n x' + y'


          STACK           INPUT         OUTPUT
           Level 1                n     Panmagic Square


Examples:

 
   •   n = 5

   5   PMAG  gives:

   01   14   22   10   18
   07   20   03   11   24
   13   21   09   17   05
   19   02   15   23   06
   25   08   16   04   12

-Magic Constant = 65

   •   n = 4

   4   PMAG  returns:

   01   14   04   15
   08   11   05   10
   13   02   16   03
   12   07   09   06

-The magic constant = 34

Note:

-There is no normal panmagic squares of order  2 , 6 , 10 , ....


       d) Panmagic Square  ?


- PMG?  tests if the matrix in level 1 is a panmagic square
-If the answer is "yes" it gives the magic constant
-Otherwise the HP48 displays "Error: Bad Argument Value"


          STACK           INPUT         OUTPUT
           Level 1               M       Magic Constant

  Where  M  is a square matrix

Example:

  [[ 01   14   04   15 ]
   [ 08   11   05   10 ]
   [ 13   02   16   03 ]
   [ 12   07   09   06 ]     PMG?    ->     34


     e) Bimagic Squares

 BMAG  constructs a bimagic square ( a magic square that remains magic after squaring all its elements ) of order p2 provided  p is a prime  p > 2

  If p > 3 , the bimagic square is also panmagic.

-The method uses 4 key-numbers:  r = ( a b c d )p  r' = ( a' b' c' d' )p  r" = ( a" b" c" d" )p  r''' = ( a''' b''' c''' d''' )p  witten in base p
-The element ai,j  is given by

   ai,j =  p3 [  { a [ ( i - 1 ) mod p ]  + a' int [ ( i - 1 ) / p ] + a" [ ( j - 1 ) mod p ] + a''' int [ ( j - 1 ) / p ] } mod p ]
        +  p2 [  { b [ ( i - 1 ) mod p ]  + b' int [ ( i - 1 ) / p ] + b" [ ( j - 1 ) mod p ] + b''' int [ ( j - 1 ) / p ] } mod p ]
        +  p  [  { c [ ( i - 1 ) mod p ]  + c' int [ ( i - 1 ) / p ] + c" [ ( j - 1 ) mod p ] + c''' int [ ( j - 1 ) / p ] } mod p ]
        +     [  { d [ ( i - 1 ) mod p ]   + d' int [ ( i - 1 ) / p ] + d" [ ( j - 1 ) mod p ] + d''' int [ ( j - 1 ) / p ] } mod p ] + 1
 

-This formula will give a bimagic square if the following determinant is different from 0  ( mod p )

      a   b   c   d
      a'  b'  c'  d'
     a"  b"  c"  d"
     a''' b''' c''' d'''

 and if   a b' - a' b # 0 ,  a c' - a' c # 0 , a d' - a' d # 0 , b c' - b' c # 0 , ...... , c d' - c' d # 0    ( mod p )
 and similar relations with the keys  r" & r'''

-The square will be panmagic if the keys

       r + r" &  r' + r'''  and
       r - r"  &  r' - r'''  satisfy the same similar relations too.

-In BMAG,  the 4 keys are:

  ( 2 1 0 1 )   ( 0 1 1 2 )  ( 1 0 2 1 )  ( 2 1 2 0 )    if p = 3
  ( 0 1 1 1 )   ( 1 0 1 2 )  ( 3 3 2 1 )  ( 2 1 0 1 )    if p > 3 , p # 11
  ( 0 1 1 1 )   ( 1 0 1 2 )  ( 3 3 2 0 )  ( 2 1 0 1 )    if p = 11

-But other choices are clearly posible.


          STACK           INPUT         OUTPUT
           Level 1                p     Bimagic Square

  where  p is a prime,  p > 2

Example:     p = 3    We'll have a bimagic square of order 9

   3   BMAG   returns:

   01   35   60   70   14   39   49   74   27
   65   18   40   53   78   19   05   30   61
   48   79   23   09   31   56   69   10   44               Magic Constant = 369
   15   37   71   75   25   50   36   58   02
   76   20   54   28   62   06   16   41   66               Magic Constant of the squared elements = 20049
   32   57   07   11   45   67   80   24   46
   26   51   73   59   03   34   38   72   13
   63   04   29   42   64   17   21   52   77
   43   68   12   22   47   81   55   08   33


     f) Magic Multiplication

 MxM   takes 2 magic squares and calculates a "product" of these matrices that is also a magic square.
 

  Formula:     A is an mxm matrix   B is an nxn matrix  then  C = A*B = [ Ci,j ]  is defined as

   Ci,j = n2 A + bi,j 1mxm          where       1mxm  is the mxm matrix whose all elements equal 1

-So,  Ci,j  is also an  mxm matrix  and  C is an mnxmn matrix built with n2 submatrices Ci,j



          STACK           INPUTS         OUTPUT
           Level 2
               A
              /
           Level 1                B            AxB

  Where   A & B  are 2 magic squares

Example:


-With   A  =

   01   14   04   15    
   08   11   05   10          
   13   02   16   03      
   12   07   09   06  

  and   B  =  

   01   14   22   10   18
   07   20   03   11   24
   13   21   09   17   05   
   19   02   15   23   06
   25   08   16   04   12

-The "product" C = AxB is the following 20x20 matrix:
 
 
  26  351  101  376   39  364  114  389   47  372  122  397   35  360  110  385   43  368  118  393
 201  276  126  251  214  289  139  264  222  297  147  272  210  285  135  260  218  293  143  268
 326   51  401   76  339   64  414   89  347   72  422   97  335   60  410   85  343   68  418   93
 301  176  226  151  314  189  239  164  322  197  247  172  310  185  235  160  318  193  243  168
  32  357  107  382   45  370  120  395   28  353  103  378   36  361  111  386   49  374  124  399
 207  282  132  257  220  295  145  270  203  278  128  253  211  286  136  261  224  299  149  274
 332   57  407   82  345   70  420   95  328   53  403   78  336   61  411   86  349   74  424   99
 307  182  232  157  320  195  245  170  303  178  228  153  311  186  236  161  324  199  249  174
  38  363  113  388   46  371  121  396   34  359  109  384   42  367  117  392   30  355  105  380
 213  288  138  263  221  296  146  271  209  284  134  259  217  292  142  267  205  280  130  255
 338   63  413   88  346   71  421   96  334   59  409   84  342   67  417   92  330   55  405   80
 313  188  238  163  321  196  246  171  309  184  234  159  317  192  242  167  305  180  230  155
  44  369  119  394   27  352  102  377   40  365  115  390   48  373  123  398   31  356  106  381
 219  294  144  269  202  277  127  252  215  290  140  265  223  298  148  273  206  281  131  256
 344   69  419   94  327   52  402   77  340   65  415   90  348   73  423   98  331   56  406   81
 319  194  244  169  302  177  227  152  315  190  240  165  323  198  248  173  306  181  231  156
  50  375  125  400   33  358  108  383   41  366  116  391   29  354  104  379   37  362  112  387
 225  300  150  275  208  283  133  258  216  291  141  266  204  279  129  254  212  287  137  262
 350   75  425  100  333   58  408   83  341   66  416   91  329   54  404   79  337   62  412   87
 325  200  250  175  308  183  233  158  316  191  241  166  304  179  229  154  312  187  237  162

 
-Here, the magic constant = 4510
-But if you subtract 25 to all these elements, you get a normal magic square, with all the integers between 1 and 400, magic constant = 4010

-Since A & B are panmagic squares,  C is a panmagic square too !


Note:

-With an HP48GX, execution time = 67 seconds


     g) Magic & PanMagic Cubes


-The following programs create normal magic cubes, i-e their elements are  1  2  3  ...............  n3

-An Andrews magic cube is a cubic array where the sums of the elements equal the magic constant n(n3+1)/2  in the 3 directions and the 4 space diagonals.
-The cube is perfect pandiagonal if all the orthogonal sections are panmagic squares.
-In a panmagic cube , all the orthogonal or diagonal sections - broken or unbroken - are panmagic squares.
 

 "MGCB" builds:

    Andrews magic cubes if n = 3 or 5 and - more generally - if n is odd.
    Perfect pandiagonal magic cube if n = 7
    Panmagic cubes if n is a prime > 7 

Formulae:

    ai,j,k =  n2  { [ a ( i - 1 ) + a' ( j - 1 ) + a" ( k - 1 ) ] mod n }
              +  n  { [ b ( i - 1 ) + b' ( j - 1 ) + b" ( k - 1 ) ] mod n }
               +  [  c ( i - 1 ) + c' ( j - 1 ) + c" ( k - 1 ) ] mod n + 1

    with

   ( a b c )   =  ( 1 -1  1 )
  ( a' b' c' )  =  ( -1  1  1 )      to create Andrews Magic Cubes of odd orders
 ( a" b" c" ) =  ( 1  1  -1 )

   ( a b c )   =  ( 6  5  4 )
  ( a' b' c' )  =  ( 5  4  5 )       if  n = 7
 ( a" b" c" ) =  ( 4  6  6 )

   ( a b c )   =  ( 1  1  1 )
  ( a' b' c' )  =  ( 3  2  2 )       if  n is a prime > 7
 ( a" b" c" ) =  ( 5  5  4 )


   •   n = 4.m    MGCB employs

-Let  x' = 0  if  1 <= x <= n/2
 and  x' = 1  if  n/2 < x <= n

-Let  x" = n + 1 - x

-If  ( i + j + k + i' + j' + k' ) mod 2 = 1  then   ai,j,k =  n2 ( i - 1 ) + n ( j - 1 ) + k
-If  ( i + j + k + i' + j' + k' ) mod 2 = 0  then   ai,j,k =  n2 ( i" - 1 ) + n ( j" - 1 ) + k"


   •   n = 4.m + 2 ,   MGCB employs the formula given in reference [4]



          STACK           INPUT         OUTPUT
           Level 1                n               L

  Where  L is  a list of n  square matrices of order n

Example:


   •   With n = 3   we get a list of 3 matrices:

    01  17  24
    23  03  16       15  19  08
    18  22  02       07  14  21      26  06  10
                           20  09  13      12  25  05
                                                 04  11  27

-The magic constant = 42 =  1+17+24 = 1+23+18 = .....  = 4+11+27 = 10+5+27

  = 1+14+27 = 18+14+10 = 24+14+4 =2+14+10  ( the 4 space diagonals )


     h) Magic Cube ?


  MGC?  tests if a list of n square matrices ( nxn ) corresponds to a magic cube
-If the answer is "yes" it gives the magic constant
-Otherwise the HP48 displays "Error: Bad Argument Value"


          STACK           INPUT         OUTPUT
           Level 1               L       Magic Constant

  Where  L is  a list of n  square matrices of order n

Example:


-With the list obtained in paragraph 1°)g) , we get  42


     i) Primality ?

 PR?  tests if a positive number N is a prime

 it returns 1 if  N is a prime
 --------- 0  if it's not


          STACK           INPUT         OUTPUT
           Level 1                N          1 or 0

Examples:


  107   PR?    ->    1
  143   PR?    ->    0

Notes:

-It's not a good program for large numbers ( very slow )
-But it's only used here by some of the programs above to choose the proper formulae corresponding to the order of the matrix
-So, it's enough well !


2°)  Knight's Tour


-The following program returns a sequence of Knight's moves on a  m x n  chessboard so that each square of the board is visited exactly once.
-You specify the starting square and your HP-48 gives a Knight's Tour in most cases ( it fails sometimes ).
-It uses Warnsdorff's method to find such a path


          STACK           INPUTS         OUTPUTS
           Level 2
           { i  j }
             M
           Level 1           { m  n }              N

  where   { i  j }  is the starting square                                M = m x n Matrix = a solution
   and    { m  n } is the dimension of the chessboard           N = number of visited squares

-The program fails if  N <  n m  ( in this case, M contains one or several zero(s) )

Example1:          m = 3 ,  n = 4  ,  starting square = { 1  1 }

   { 1  1 }   ENTER
   { 3  4 }   KNIGHT  returns  N = 12         ( in 41 seconds with an HP-48S/SX , in 27 seconds with an HP-48G/GX )

                   [ [  1   4   7  10 ]
   and   M =   [  8  11  2   5  ]
                     [  3   6   9  12 ] ]

Example2:          m = n = 8  - standard chessboard -  starting square = { 1  1 }

   { 1  1 }   ENTER
   { 3  4 }   KNIGHT  returns  N = 64         ( in 4mn57s with an HP-48S/SX , in 3mn17s with an HP-48G/GX )

                    [ [  1    26   15   24   29   34   13   32 ]
                      [ 16   23   28   37   14   31   64   35 ]
                      [ 27    2    25   30   61   36   33   12 ]
   and   M  =   [ 22   17   40   49   38   55   60   63 ]
                      [  3    48   21   56   41   62   11   54 ]
                      [ 18   45   42   39   50   53    8    59 ]
                      [ 43    4    47   20   57    6    51   10 ]
                      [ 46   19   44    5    52    9    58    7  ] ]

-If m = n = 8 , all the starting squares produce a complete tour, except  { i  j } = { 6  4 }
 

3°)  Hartley Transform  


     a) Discrete Hartley Transform ( Dim 1 )


-The DHT of a vector  [ a1 , a2 , ........ , an ]  is a vector   [ b1 , b2 , ........ , bn ]   where   bi =  SUMq=1,2,...,n   aq. cas  360°(q-1)(i-1)/n    with   cas µ = sin µ + cos µ


          STACK           INPUT         OUTPUT
           Level 1               V               V'


Example:


    [ 2  4  7  6 ]    DHT    [ 19  -7  -1  -3 ]


Notes:

 DHToDHT = n.Id
-So, if you modify DHT to divide the result by sqrt(n), the transformation becomes its own inverse.

-If all the elements are real, if  n  is an integer power of 2 and if you have an HP48G/GX,
  you can use the fast Fourier transform  FFT  to have a fast Hartley transform  FHT:   <<  FFT  RE  LASTARG  IM  -  >>


     b) Discrete Hartley Transform ( Dim 2 )


-The DHT may be generalized to  mxn matrices  [ ai,j ]  ( 1 <= i <= n ; 1 <= j <= m ).
-The result is also a  nxm matrix  [ bi,j ]   where  bi,j =  SUMq=1,2,...,m  ; r=1,2,....,n   aq,r. cas 360°[(q-1)(i-1)/m+(r-1)(j-1)/n]            ( cas = sin + cos )


          STACK           INPUT         OUTPUT
           Level 1               M               M'


Example:

               [ [  1  3  4  10  ]
 Let  V =   [  4  5  7  14  ]       DHT2   gives 
                 [  2  9  6  11  ] ]

                       [ [    76             -28         -28          8         ]
     DHT (A) =   [  -9.2679    5.9282    5.4641   -3.1962  ]        ( rounded to 4 decimals )
                         [ -12.7321  -7.9282   -1.4641    7.1962  ] ]

Notes:

-We have the property   DHToDHT = m.n.Id
-Applying the DHT2 twice yields the original matrix multiplied by m.n
-So, if you modify DHT2 to divide the result by sqrt(m.n), the transformation becomes its own inverse.
-If all the elements are real, if  n & m  are integers power of 2 and if you have an HP48G/GX,
  you can use the fast Fourier transform  FFT  to have a fast Hartley transform  FHT:   <<  FFT  RE  LASTARG  IM  -  >>


     c) Discrete Hartley Transform ( Dim 3 )


-In this case, the DHT of a  mxnxp  hypermatrix [ ai,j,k ]  ( 1 <= i <= m ; 1 <= j <= n ; 1 <= k <= p ) is a  mxnxp hypermatrix  [ bi,j,k ]

  where  bi,j,k =  SUMq=1,2,...,m  ; r=1,2,....,n ; s=1,2,....,p   aq,r,s. cas 360°[(q-1)(i-1)/m+(r-1)(j-1)/n+(s-1)(k-1)/p]         ( cas = sin + cos )



          STACK           INPUT         OUTPUT
           Level 1               L              L'

Where  L is a list of  p  matrices with  n rows & m columns

Example:

    Let L =  { [ [   1  25  40   5    2  ]                  
                       [   4  36  18  32  37 ]                 
                       [   9   8   39  20  33 ]
                       [ 16  23  21  10  31 ] ]               
 
                             [ [  31  10  21  23  16 ]
                               [  33  20  39   8    9  ]
                               [  37  32  18  36   4  ]
                               [   2    5   40  25   1  ] ] 

                                       [ [   0  16  23  21  10 ]
                                         [   1  25  40   5    2  ]
                                         [   4  36  18  32  37 ]
                                         [   9   8   39  20  33 ] ] }        DHT3     returns a list of three  4x5-matrices

-For instance, the (2;1)-element of the 3rd matrix became:  b2,1,3 = 67.0070415521

Notes:

-With an HP48GX, execution time = 8m34s (!)

-We have the property   DHToDHT = mnp.Id
-Applying DHT3 twice yields the original hypermatrix multiplied by mnp.
-So, if you modify DHT3 to divide the result by sqrt(m.n.p), the transformation becomes its own inverse.



     d) Hartley Transform with Filon Integration

-Actually, Hartley has defined the real transform  H(µ) =  (2.pi) -1/2  §-infinity+infinity  f(t) ( cos µ.t + sin µ.t ) dt      ( § = integral )
-This transform is strictly reciprocal.

-We can estimate this integrals by Filon's integration formula ( cf [9] & [11] )


      STACK        INPUTS      OUTPUTS
       Level 5
            /
            a
       Level 4             a             b
       Level 3             b             V
       Level 2             V             µ
       Level 1             µ           H(µ)

  Where  V = [ f(x0)  f(x1)  .......  f(x2n-1) f(x2n) ]      x0 = a     x2n = b

Example:

     We take  f(t) =  e -t/2  for  t >= 0  and  f(t) = 0  if  t < 0

- H(µ) may be expressed analytically and  H(µ) = 2(1+2µ)/(1+4µ2) / sqrt ( 2.Pi )

-We can choose a & b  so that f(x) is enough small to be neglected in the integral

-If we use f(0) , f(1) , .............. , f(16) to represent the function,

                     0     ENTER
                    16    ENTER
[ 1  0.60653...  0.36787....    ........................   0.000335... ]
                     2      HFIL                                                             ->      0.2366

 we obtain the following results ( rounded to 4 decimals )

 
 
        x       -1     -0.5    -0.25       0   0.2071       1       2
     HFIL   -0.1584   0.0013    0.3197    0.7979   0.9636    0.4797   0.2366
     exact   -0.1596       0    0.3192    0.7979   0.9631    0.4787   0.2347


-The graph of the Hartley transform looks like this:

                                                              H(x)
                                                                 |
                                                                 |  *                               H(x) is maximum for x = 0.2071
                                                                 |*   *
                                                                 |          *
                                                                 |                       *
                                                                 |                                                          *
  ---------------------------------------* -|----------------------------------------------- x
            *                                                   |
                                     *                 *       |
                                                                 |
 


     e) Continous Data - Integration


-Of course, we can also use the built-in integrator
-HRTL employs this method with the change of variable  t = Tan u


      STACK        INPUTS      OUTPUTS
       Level 5
            /
            a
       Level 4             a             b
       Level 3             b          'F(X)'
       Level 2          'F(X)'             µ
       Level 1             µ           H(µ)

Example:    The same one:    f(t) =  e -t/2  for  t >= 0  and  f(t) = 0  if  t < 0

                    4  FIX

                    0             ENTER
                   16            ENTER
              'EXP(-X/2)'  ENTER
                   2               HRTL     ->      0.2346

 
 we obtain the following results ( rounded to 4 decimals )

 
 
        x       -1     -0.5    -0.25       0   0.2071       1       2
     HFIL   -0.1597   0.0003    0.3190    0.7976   0.9635    0.4789   0.2346
     exact   -0.1596       0    0.3192    0.7979   0.9631    0.4787   0.2347


Notes:

-The precision is better in 5  FIX  ,  6 FIX  ....   but the execution will increase a lot.
-You can use  a = - E499 & b = + E499,  the change of variable  t = Tan u  will change them into ( about )  - Pi/2 & + Pi/2


4°)  Eigenvalues & Eigenvectors - Jacobi's Method

 JCB uses a variant of the Jacobi's algorithm:

*** The eigenvalues of a matrix A are the diagonal-elements of an upper triangular matrix T equal to the infinite product   ...Uk-1.Uk-1-1....U2-1.U1-1.A.U1.U2....Uk-1.Uk....
 where the Uk are unitary matrices. The eigenvectors are the columns of  U = U1.U2....Uk-1.Uk.... if A is Hermitian ( i-e if A equals its transconjugate )
 Actually, T is diagonal if A is Hermitian.

-"JCB" finds the greatest element ai j below the main diagonal
-Then,  U1  is determined so that it places a zero in position ( i , j )  in U1-1.A.U1

  U1  has the same elements as the Identity matrix, except that  ui i = uj j = x  and  ui j = y + i.z ,  uj i = -y + i.z

     with  y + i.z  = C.x  ,  x = ( 1 + | C |2 ) -1/2  and   C = 2.ai j / [ ai i - ai j + ( ( ai i - ai j )2 + 4 ai j aj i )1/2 ]

-The process is repeated until the greatest sub-diagonal element is smaller than eps

-The successive greatest | ai j |  ( i > j ) are displayed when the routine is running.

*** If the matrix is non-Hermitian, and if all the eigenvalues are distinct,  we define an upper triangular matrix W = [ wi j ]  by

   wi j = 0    if  i > j
   wi i = 1
   wi j = Sumk = i+1, i+2 , .... , j      ti k wk j / ( tj j - ti i )      if  i < j

-Then, the n columns of  U.W  are the eigenvectors corresponding to the n eigenvalues  t11 = k1 , ............... , tnn = kn



          STACK           INPUTS         OUTPUTS
           Level 2
               /
              M
           Level 1               A               V

   Where  A is a square matrix , V contains the eigenvalues,  the columns of M are the eigenvectors

Example1:
   Let's compute all the eigenvalues and the eigenvectors of the matrix

            1  2  4
    A =  2  7  3     JCB   returns    the 3-vector in level 1    [ -0.730676198691    4.91074121335   12.8199349853  ]
            4  3  9

-So, the eigenvalues are:

                 k1 = -0.730676198691
                 k2 =  4.91074121335
                 k3 = 12.8199349853

and the 3 corresponding eigenvectors are the 3 columns of the matrix in level 2:

             V1 (  0.930757325639 ; -0.104865823188 ; -0.350276975967 )
             V2 ( -0.101146468234 ; 0.846760700436 ; -0.522269765696 )
             V3   (  0.351369026422 ; 0.521535689482 ; 0.777521917338 )

-All the eigenvectors are unit vectors when the matrix A is symmetric ( or more generally hermitian )

Example2:

             1  2  4
     A =  4  3  5    JCB   gives the following results;
             7  4  7

         k1 =  -2.09209759346
         k2 =  0.185167648589
         k3 = 12.90692994 48

and the 3 corresponding eigenvectors:

          V1 ( 0.800454174265 ; -0.0416510801675 ; -0.597945066394 )
          V2 ( -0.0958180743763 ; 0.907396310041 ; -0.434179238312 ) 
          V3 ( 0.360058723032 ; 0.548018519593 ;  0.797789237995 ) 

-The eigenvectors are not unit vectors except the first one if the A is not hermitian ( or not symmetric if all the coefficients are real ).
-Divide V2 & V3 by  || V2 || & || V3 || respectively if you want unit eigenvectors.

Notes:
 
-If A is non-symmetric ( or not hermitian for complex matrices ), this program only works if all the eigenvalues are distinct:
-The eigenvalues are correctly computed, but not all the eigenvectors.
-The eigenvalues of a symmetric real matrix are always real.
-The eigenvalues of a hermitian matrix are always real too.


3°)  Linear Systems


     a)  Triangularization ( Householder Method )

-The Householder method uses orthogonal symmetries to gradually triangularize a matrix.

-If ( a1 , ...... , an ) is the first column of the matrix A,  the other columns  ( x1 , ...... , xn )  are replaced by  ( y1 , ...... , yn )  defined as

   ( y1 , ...... , yn ) = ( x1 , ...... , xn ) - C [ a1 + sgn(a1) ( a12 + ....... + an2 )1/2 , a2 , ...... , an ]

 where   C = [ SUMi=1,...,n ai xi + x1 sgn(a1) ( SUMi=1,...,n ai2 )1/2 ] / [ SUMi=1,...,n ai2 + a1 sgn(a1) ( SUMi=1,...,n ai2 )1/2 ]

-The first column becomes  ( A1 , 0 , .......... , 0 )   with   A1 = -sgn(a1) ( SUMi=1,...,n ai2 )1/2
-Then, the same process is repeated with the 2nd column, without taking into account the 1st column and the 1st raw, and so on...



          STACK           INPUT         OUTPUT
           Level 1               M              T

Where  M  is a matrix ( real or complex )  with at least 2 rows

Example:

              9  8  2  2                                   -12.2066    -9.1754    -5.2431    -6.5539
    M =   8  4  4  6    HHLD    returns             0           3.4369      3.4603     2.2886         ( rounded to 4 decimals )
              2  4  7  7                                          0               0          5.4347      6.3882


     b)  Overdetermined Systems


-An overdetermined system  A.X = B  has often no solution at all
-But the following programs calculate the least-squares solution:
-It minimizes || A.X - B ||

Formula:   X = ( tA.A ) -1 ( tA.B )


          STACK           INPUTS         OUTPUTS
           Level 2
              B
              /
           Level 1               A              X


Example:
 

     5x + y + z  =   8          
     4x - y + 2z =  13
       x + 2y - z =  -5
     7x - 4y + 5z =  32
     2x + 5y - 9z = -20

     [ 8  13  -5  32  -20 ]  ENTER
    [[ 5  1  1 ]
     [ 4 -1 2 ]
     [ 1  2 -1]
     [ 7 -4 5 ]
     [ 2  5 -9 ]]    SL↑   [ 2.07120743034  -3.20123839009   0.904024767798 ]

-So   x = 2.07120743034  ,  y = -3.201238389009  ,  z = 0.904024767798


     c)  Underdetermined Systems


 SL↓  computes the Moore-Penrose solution given by the formula:   X = tA ( A.tA ) -1 B


          STACK           INPUTS         OUTPUTS
           Level 2
              B
              /
           Level 1               A              X


Example:
 

      2x + 3y + 7z + 4t = 1    
      3x + 2y - 5z + 8t = 4    
      4x + 5y + 6z + t  = 7    

      [ 1  4  7 ]   ENTER
     [[ 2  3  7  4 ]
      [ 3  2 -5  8 ]
      [ 4  5  6  1 ]]     SL↓        ->      [ 1.09508816121  1.07577665827  -0.389378673387  -0.422963895887 ]   =  [ x  y  z  t  ]


Note:

-The program  ?SL↓   employs a random matrix
-If you inititialize   RDZ   with  1   (  1  RDZ  )    ?SL↓  gives the following solution with the system above

    [ 0.711745989365  1.45009901431  -0.448007476136  -0.409434172173 ]    ( very small differences in the last decimals with an HP48SX )


6°)  Linear Programming - Simplex Method

-The purpose is to find  n  non-negative real numbers:   x1 , ..... , xn   satisfying:

                          bi >= ai;1x1 + ....... + ai;n xn            i = 1 , ....... , m                              ( m inequations )           the bi  , bi'  may be positive or not !
                          bi'  =  ai';1x1 + ....... +ai';n xn            i' = 1 , ...... , m'                              ( m' equations )

and such that      F  =  c1x1 + ....... +  cnxn       is maximized.    ( to find the minimum of F, seek the maximum of  -F )
 

-Here, we first treat each equation by pivoting on the largest coefficient to develop a complete basic variable set

-Then, the dual simplex pivoting rule is used to transform all the negative bi into non-negative numbers ( if possible )

       •  pivot = ai,j < 0  with the largest  cj / ai,j

-Then, the primal simplex pivoting rule is applied to transform all the positive ci into non-positive numbers

       •  pivot = ai,j > 0  with the smallest  bi / ai,j

-Finally, the tableau is solved.


          STACK           INPUTS         OUTPUTS
           Level 3
               /
             M'
           Level 2
              M
            Max:  ...
           Level 1               n               V

  Where M is the matrix containing the system of inequations - equation(s) - maximum

Example:
 

Find  4 non-negative numbers  x, y, z and t satisfying:

             56 >=  x + 2y + 3z +4t
             41 >= 5x +  y - 3z - t
             61 >= 4x + 7y +2z - 3t  
               7 <=   x + y + z + t
             17 <= 2x - y + z + 3t
             25 = 2x + 3y - z + 2t 

such that F = 2x + 4y +3z + 5t is maximized.

1-Re-write the LP as follows:  the >= inequations first ( if any ), then the = equations ( if any )

             56 >=  x + 2y + 3z +4t
             41 >= 5x +  y - 3z - t
             61 >= 4x + 7y +2z - 3t
             -7 >=  -x  -  y  -  z  -  t                          the "<=" inequations must be multiplied by (-1) to become  ">=" inequations
           -17 >= -2x + y  -  z - 3t
             25 =  2x + 3y - z + 2t
 

2-Then, place the following matrix in the stack

          [[ 56   1   2   3   4  ]
           [ 41   5   1  -3  -1 ]      
           [ 61   4   7   2  -3 ]     
           [ -7  -1  -1  -1  -1 ]    
           [ -17 -2   1  -1  -3 ]          
           [ 25   2   3  -1   2 ]            
           [   0   2   4   3   5 ]]    ENTER   then the number of equation(s)  here  1

                      1                     SPLX     and the HP48  displays the successive values in the bottom left of the matrix, and finally:

             Level 3 = the transformed matrix M'
             Level 2 = MAX:   76
             Level 1 = [ 2  7  8  4  0  52  0  14  0 ]                   ( with very small roundoff-errors )


-Thus the maximum of F is 76 and it is achieved for x = 2, y = 7, z = 8, t = 4.

-We also get the 5 slack variables:  0  52  0  14  0

Notes:

-In the example above, execution time = 18s with an HP48SX, 12s with an HP48GX

-All the equations must be linearly independant.
-Especially, don't key in more equations ( = ) than unknowns.  

-SPLX calls  MIJ  as a subroutine  which uses Externals:
-MIJ  takes a real matrix A in level 3  and  2  indexes i & j in level  1 & 2 ( it checks the inputs are correct )
-The i-th row is divided by aij  and the other rows are modified to replace akj  by zero.

-For instance, with  i = 2 & j = 3 the matrix

  1   2   3   4                      -5   -7   0  -8
  4   6   2   8     becomes     2    3   1    4
  7   1   5   6                      -3  -14  0 -14


7°)  Creating an Array  


-To create an mxn matrix A ( or an n-vector V )  TAB  takes a program in level 2 that takes  m in level 2 & and n in level 1
 ( or simply n in level 1 )  and returns  aij  ( or vi )  in level 1
 and the size of the matrix ( or the vector ) in level 1


          STACK           INPUTS         OUTPUTS
           Level 2
           << f >>
               /
           Level 1      { m n )  or { n }            A or V


Example1:    Create a Pascal matrix of order 4

 [ai,j]  are defined by  ai,j = Ci+j-2i-1    where  Cnp = n! / ( p! ( n-p)! )  are the binomial coefficients.

   <<  2  -  OVER  +  SWAP  1  6  COMB  >>    ENTER
                                     { 5  5 }                              TAB         gives the following Pascal's matrix

    1   1   1   1   1  
    1   2   3   4   5
    1   3   6  10 15
    1   4  10 20 35
    1   5  15 35 70

Example2:

    <<  SQ  >>    ENTER   { 4 }     TAB    ->    [ 1   4   9  16 ]


8°)  Random Matrix  


 THZD  creates a random matrix whose elements are integers between 1 & 10


          STACK           INPUT         OUTPUT
           Level 1      { m n )  or { n }            A or V


Examples:  

  After  1   RDZ

  { 3 }   THZD   gives  [ 8   8  10 ]    and then,

   { 3  4 }   creates  

  [[ 3   9   1   6 ]
   [ 1   9   8   2 ]
   [ 1   1  10  6 ]]


9°)  Trace of a Square Matrix


-The trace of a square matrix equals the sum of its diagonal elements.


          STACK           INPUT         OUTPUT
           Level 1               A            tr(A)


Example:  

  [[ 3   9   1 ]
   [ 1   9   8 ]
   [ 1   1  10 ]]    TRACE     22

Notes:

  TRACE is called by  MGS?  PMG?  &  RANK
-This program is only useful with an HP48S/SX  since  TRACE is a built-in function of the HP48G/GX
-However, it works well - even without changing its name - with an HP48G/GX


10°)  Rank of a Matrix


 RANK employs the iterative method of Ben-Israel & Cohen.
 

Formula:

    B0   =  ( A AT )  /   Trace ( A AT )
   Bk+1 = 2 Bk - Bk2

  >>>     r(A) = Limk->infinity Tr ( Bk )
 

          STACK           INPUT         OUTPUT
           Level 1               A            r(A)


Example:  

        [[ 1  1  4  2 ]                    
         [ 0  1  2  3]           RANK    the HP48 displays the successive approximations and eventually returns  2
         [ 1  3  8  8 ]]     

  r(A) = 2


Note:

-This program is only useful with an HP48S/SX  since RANK is a built-in function of the HP48G/GX


11°)  Pseudo-Inverse ( Gréville's Method )


-If A is a nxm matrix ( n rows, m columns ), "GREV" computes the pseudoinverse A+ , also called Moore-Penrose inverse, of the matrix A

  A+ is the unique mxn matrix ( m rows, n columns ) such that:  A A+ A = A ,  A+ A A+  = A+ ,  A A+ and  A+ A  symmetric

-The method of Greville gradually builds the pseudo-inverse row by row:

-Let  ak = the k-th column of A
        Ak = the matrix built with the k first columns of A
       A+k  = the pseudoinverse at step k

-We start with  A+1 = ta1 / ( ta1 a1 )   if   a1 # 0
                  or  A+1 = ta1                   if   a1 = 0     and then,  for k = 2 , 3 , ..... , m

     Let    dk = A+k-1  ak
              ck = ak - Ak-1 dk
              bk = tck / ( tck ck )   if   ck # 0      or      bk = tdk A+k-1 / ( 1 +  tdk dk )   if  ck = 0

   >>>   A+k  = [ A+k-1 - dk bk ]           In other words,  A+k  is obtained by placing  the row   bk  under the matrix  A+k-1 - dk bk
                          [      bk      ]

-Finally,   A+ = A+m


          STACK           INPUT         OUTPUT
           Level 1               A              A+


Example:  

                 1  1  4  2          
      A =     0  1  2  3       GREV       gives
                 3  2  6  7           

                      -21/112   -85/112   43/112
      A+ =           7/112      23/112   -9/112             ( with some roundoff errors of course ).
                       49/112      1/112   -15/112
                      -35/112    29/112    13/112

-In this example,  A A+ = Id3


12°)  Power of a Square Matrix


-The program hereafter calculates Ap  where A is a square matrix and p is a positive integer   ( A0 = I = Identity Matrix )
-If p is a negative integer, compute the inverse A-1 first, and then (A-1) -p


          STACK           INPUTS         OUTPUTS
           Level 2
               A
              /
           Level 1                p              Ap

  Where  p  is an integer

Example:  With  p = 7  and  A  is the following matrix

          1  4  9    
  A =  3  5  7     ENTER    7     POWM    gives:
          2  1  8    

             7851276   8652584   31076204 
   A7 =   8911228  9823060   35267932  
             5829472  6422156   23076808  


13°)  Matrix Square-Root

-Given a non-singular nxn matrix A  ( det A # 0 ), "SQRTM" uses the iterations:

    M0 = A      Mk+1 = (1/2) [ I + ( Mk + Mk-1 ) / 2 ]      where  I  is the Identity matrix
    X0 = A      Xk+1 = Xk ( I + Mk-1 ) / 2

-If A has no negative real eigenvalue,  Xk quadratically tends to  A1/2  and  Mk tends to I  as k tends to infinity.


          STACK           INPUT         OUTPUT
           Level 1               A           sqrt(A)


Example:  

              4  2  3                                       1.79498101327      0.65636752920   0.54042525978
    A  =   3  2  5      SQRTM   gives         0.772143190575    1.06137417379   1.58298934032
              2  1  4                                       0.501888908675    0.23163462677   1.83360066840

 

14°)  Matrix Nth-Root

-The principal n-th root of a non-singular matrix A ( det A # 0 )  may be computed by the algorithm:

    M0 = A      Mk+1 = Mk [ ( 2.I + ( n - 2 ) Mk ) ( I + ( n - 1) Mk ) -1 ] p      where  I  is the Identity matrix
    X0 =  I       Xk+1  = Xk  ( 2.I + ( n - 2 ) Mk ) -1 ( I + ( n - 1) Mk )

    Mk  tends to  I        as k tends to infinity
    Xk   tends to A 1/p  as k tends to infinity

-The convergence is also quadratic if A has no negative real eigenvalue.


          STACK           INPUTS         OUTPUTS
           Level 2               A                 /
           Level 1
              n
             A1/n


Example:  

              4  2  3                                                                               1.43777141517     0.396760704684   0.231139048328
    A  =   3  2  5    ENTER   3     NROOT    gives     A^(1/3)    =    0.421053139813   0.977706013368   0.944423238828
              2  1  4                                                                               0.270151310348   0.119249480888   1.46942376378

15°) Exponential of a Square Matrix


-The exponential of a nxn matrix A is defined as

   exp(A) = I + A + A2/2! + A3/3! + ..... + Ak/k! + ....                ( I = the Unit matrix = the Identity matrix:  1 in the diagonal, 0 elsewhere )



          STACK           INPUT         OUTPUT
           Level 1               A           exp(A)


Example:      With

             1   2   3
    A =   0   1   2     EXPM   gives:
             1   3   2 

                     19.4582837522    63.1503050663   66.9878767362
    Exp(A) =   8.53464026568    32.2602441507   33.2790641362
                     16.6395320684    58.4532364702   61.7017366167

16°)  Logarithm of a Square Matrix


-If  || A - I || < 1 , the logarithm of a nxn matrix A is defined by the series expansion:

       Ln(A) = ( A - I ) - ( A - I )2/2 + ( A - I )3/3 -  ( A - I )4/4 + ......  


          STACK           INPUT         OUTPUT
           Level 1               A           Ln (A)


Example:      With

               1.2   0.1   0.3   
      A =   0.1   0.8   0.1       LNM     returns:
               0.1   0.2   0.9   

                   0.16708339584        0.0695779233559   0.287707998737  
    Ln(A) =  0.0977830050234   -0.240971673255     0.103424021357
                   0.0865009723563    0.23505312438      -0.131906635565

-In this example,   || A - I || = 0.5099...  < 1

Notes:

-If  || A - I || < 1   Exp(Ln(A)) = A
-If  || A || < Ln 2   Ln(Exp(A)) = A


17°)  GETC


 GETC  returns the j-th comlumn of a matrix


          STACK           INPUTS         OUTPUTS
           Level 2               A                 /
           Level 1
              j
              Cj


Example:  

                 1  1  4  2          
      A =     0  1  2  3      ENTER   4      gives      [ 2  3  7 ]
                 3  2  6  7   


18°)  eps


  'eps'  simply contains a small number  ( E-10 )  to stop the iterations in several programs above.




References:

 [1]   Pierre Tougne - "Pour la Science" - pages 121 to 127 - Issue#46 - Aout 1981  ( in French )
 [2]   http://mathworld.wolfram.com/MagicSquare.html
 [3]   http://mathworld.wolfram.com/MagicCube.html
 [4]   Marian Trenkler - "An Algorithm for making Magic Cubes"
 [5]  Yangkok Kim, Jaechil Yoo  - "An algorithm for constructing magic squares" - Discrete Applied Mathematics 156 (2008) 2804–2809
 [6]   http://mathworld.wolfram.com/KnightsTour.html
 [7]   Sam Ganzfried - "A Simple Algorithm for Knight's Tours"
 [8]   Ronald N. Bracewell - "The Fourier Transform and its Applications" - McGraw-Hill -  ISBN 0-07-116043-4
 [9]   Francis Scheid   "Numerical Analysis"       McGraw-Hill   ISBN 07-055197-9
[10]  Hossein Arsham - "Artificial-variable Free Solution Algorithms for LP Models"
[11]  Abramowitz and Stegun - "Handbook of Mathematical Functions" -  Dover Publications -  ISBN  0-486-61272-4