# hp41programs

dot&cross Dot Product and Cross Product for the HP-41

Overview

1°)  Dot Product

1-a)  Focal Program
1-b)  M-Code Routine

2°)  Cross-Product

2-a)  3D-Vectors
2-b)  7D-Vectors
2-c)  n-dimensional Vectors

>>> The latest addition is paragraph 2-b)

-Synthetic registers M N O P may be replaced by any unused data registers.

1°) Dot Product

a) Focal Program

-The following program calculates the dot-product U.V of 2 vectors  U(x1,x2,....,xn)  and  V(y1,y2,....,yn) ,
the norms  || U || , || V ||  and the angle µ defined by these vectors  ( between 0 and 180° )

Formula:         cos µ = U.V / ( || U || . || V || )

Data Registers:    •   Rbb = x1 ,  •   Rbb+1 = x2 , .......... ,  •   Ree = xn     ( These 2n registers are to be initialized before executing  "DOT" )
•   Rbb' = y1 , •   Rbb'+1 = y2 , .......... , •   Ree' = yn                                 ee - bb = ee' - bb' = n - 1
Flags: /
Subroutines: /

-If you use registers  R01 and R02 instead of registers N and O, replace line 02 by  0  STO 01  STO 02  RDN

 01  LBL "DOT" 02  CLA 03  STO M 04  CLX 05  LBL 01  06  RCL IND M 07  X^2 08  ST+ N 09  X<> L 10  RCL IND Z  11  X^2 12  ST+ O  13  X<> L 14  * 15  + 16  ISG Y 17  "" 18  ISG M 19  GTO 01        20  STO Y 21  RCL O 22  SQRT 23  ST/ Z 24  RCL N         25  SQRT 26  ST/ T 27  R^ 28  ACOS 29  X<> T          30  CLA 31  END

( 55 bytes )

 STACK INPUTS OUTPUTS T / µ Z / || U || Y bbb.eee(U) || V || X bbb.eee(V) U.V L / cos µ

Example:      U(2,3,7,1)   V(3,1,4,6)

-For instance,

2  STO 01   3  STO 02   7  STO 03   1  STO 04    ( control number = 1.004 )
3  STO 06   1  STO 07   4  STO 08   6  STO 09    ( control number = 6.009 )

1.004  ENTER^
6.009  XEQ "DOT"  >>>>     U.V = 43
RDN   || V || = 7.874007874
RDN   || U || = 7.937253933
RDN      µ    = 46.52626239°   ( if the HP-41 is set in DEG mode )

b)  M-Code Routine

-"DOTN"  takes the control numbers of 2 vectors  bbb.eee(ii)1 and bbb.eee(ii)2 in registers Y and X  and returns the dot-product in X-register
-bbb.eee(ii)1 is unchanged in Y-register, bbb.eee(ii)2 is saved in L-register
-The "alpha" register is unused but synthetic register Q is altered.

-If  ii = 01 , it may be replaced by 00

Warning:

-This routine does not check for alpha data.

08E  "N"
014  "T"
00F  "O"
004  "D"
248   SETF 9
128   WRIT 4(L)
2A0  SETDEC
084   CLRF 5                   C
0ED  ?NCXQ                   =
064   193B                      frc(C)
226   C=C+1 S&X           C
226   C=C+1 S&X           =
226   C=C+1 S&X       1000 C
070   N=C ALL
013   JNC +02
3B3  JNC -10d=-0Ah
088   SETF 5                    C
0ED  ?NCXQ                   =
064   193B                     int(C)
260   SETHEX
38D  ?NCXQ
008   02E3
106   A=C S&X
03C  RCR 3
146   A=A+C S&X
130   LDI S&X
200   200h                          this value is for an HP-41CV , CX or C with a Quad memory module or 4 memory modules
306   ?A<C S&X               if you have an HP-41C without memory modules, replace 200 by 100
381   ?NC GO
00A   02E0                        if register Ree1 or Ree2 doesn't exist, the routine stops after displaying "NONEXISTENT"
0A6   A<>C S&X
268   WRIT 9(Q)
0B0   C=N ALL
2A0   SETDEC
084   CLRF 5                   C
0ED  ?NCXQ                   =
064   193B                      frc(C)
226   C=C+1 S&X          C=
226   C=C+1 S&X        100 C
260   SETHEX
38D  ?NCXQ
008   02E3
2E6   ?C#0 S&X
017   JC+02
226   C=C+1 S&X
106   A=C S&X
1BC  RCR 11
0A6  A<>C S&X
268   WRIT 9(Q)
2A0  SETDEC
088   SETF 5                    C
0ED  ?NCXQ                   =
064   193B                     int(C)
260   SETHEX
38D  ?NCXQ
008   02E3
106   A=C S&X
03C  RCR 3
146   A=A+C S&X
1BC  RCR 11
0A6  A<>C S&X
24C  ?FSET 9
043   JNC+ 08
244   CLRF 9
10E   A=C ALL
0AE  A<>C ALL
0E8   WRIT 3(X)
0AE  A<>C ALL
22B  JNC -59d=-3Bh
268   WRIT 9(Q)
070   N=C ALL
04E   C=0 ALL
0E8   WRIT 3(X)
106   A=C S&X
17C  RCR 6
0A6  A<>C S&X
306   ?A<C S&X
360   ?C RTN                               the routine stops when the last register of one vector has been used.
270   RAMSLCT
0EE  C<>B ALL
0B0  C=N ALL
106   A=C S&X
17C  RCR 6
0A6  A<>C S&X
306   ?A<C S&X
360   ?C RTN                               the routine stops when the last register of one vector has been used.
270   RAMSLCT
10E   A=C ALL
046   C=0 S&X
270   RAMSLCT
2A0  SETDEC
0EE  C<>B ALL
135   ?NCXQ                                C=
060   184D                                  A*C
10E   A=C ALL
01D  ?NCXQ                                C=
060   1807                                   A+C
0E8   WRIT 3(X)
260   SETHEX
0B0   C=N ALL
106   A=C S&X
03C  RCR 3
146   A=A+C S&X
1BC  RCR 11
0A6  A<>C S&X
070   N=C ALL
106   A=C S&X
03C  RCR 3
146   A=A+C S&X
1BC  RCR 11
0A6  A<>C S&X
268   WRIT 9(Q)
2AB  JNC -43d=-2Bh

-The dot product of two vectors of 100 components is returned in 6.5 seconds.

2°) Cross Product

a) 3-Dimensional Vectors

-The cross product of 2 vectors  U(x,y,z) and V(x',y',z')  is a vector defined by  UxV ( yz'-y'z , x'z-xz' , xy'-x'y )
-Since the stack has less than 6 registers, we have to store x , y , z  into 3 data registers.

Data Registers:     •  Rbb = x   •  Rbb+1 = y  •  Rbb+2 = z      ( These 3 registers are to be initialized before executing "CROSS" )
Flags: /
Subroutines: /

 01  LBL "CROSS" 02  STO M  03  RDN 04  STO N 05  X<>Y 06  STO O 07  RCL IND Z 08  ST* Z 09  * 10  ISG Z 11  ""  12  RCL IND Z     13  ST* O 14  X<> M 15  ST* M 16  X<> M  17  ST- Z 18  ISG T 19  CLX 20  CLX 21  RCL IND T 22  ST* N 23  RCL M  24  * 25  X<>Y 26  - 27  RCL O            28  RCL N 29  - 30  CLA                31  END

( 59 bytes )

 STACK INPUTS OUTPUTS T bbb.eee(U) / Z z' z" Y y' y" X x' x"

Example:        U(2,3,7)   V(3,1,4)     Compute the cross product  UxV

-For instance,   2  STO 01   3  STO 02   7  STO 03    ( control number = 1.003 )

1.003  ENTER^
4      ENTER^
1      ENTER^
3      XEQ "CROSS"   >>>>    5
RDN   13
RDN   -7      whence    UxV(5,13,-7)

Notes:

-In fact, this program will work as well if you place bbb in T register ( instead of  bbb.eee )
-You may also store  x , y , z  in non-contiguous registers like  R01  R03  R05  but in this case, place  1.00502 ( or 1.00002 ) in T

-The cross product is anticommutative:    VxU = -UxV

-Alternatively, you can also use the following program that is essentially similar to the M-Code routine CROSS
-It requires that you store  x'  y'  z'  into synthetic registers  M  N  O
( assign STO M   STO N  STO O  to 3 keys or use it in a program )

Data Registers: /
Flags: /
Subroutines: /

 01  LBL "CROSS"  02  STO P  03  RCL N  04  *  05  X<>Y  06  STO Q  07  RCL M  08  *  09  -  10  X<>Y  11  RCL M  12  *  13  RCL O  14  ST* P  15  ST* Q  16  X<> P  17  -  18  RCL N  19  R^  20  *  21  ST- Q  22  X<> Q  23  END

( 45 bytes / SIZE 000 )

 STACK INPUTS OUTPUTS T bbb.eee(U) / Z z' z" Y y' y" X x' x"

Example:        U(2,3,7)   V(3,1,4)     Compute the cross product  UxV

-For instance,   2  STO M   3  STO N   7  STO O

4      ENTER^
1      ENTER^
3      XEQ "CROSS"   >>>>    5
RDN   13
RDN   -7      whence    UxV(5,13,-7)

Notes:

-The 3 components  x'  y'  z'  are still in  M  N  O  at the end.
-If you don't need to save these values, add  CLA  before the end.

b) 7-Dimensional Vectors

- U(x1,x2,....,x7)   and  V(y1,y2,....,y7)  are two 7D-vectors.

-"U*V7" calculates the cross product  W(z1,z2,....,z7) by the formula given   here

-You store the components of U in R01 thru R07 and the components of V in R08 thru R14.
-"U*V7" stores the result in R15 thru R21

Data Registers:              R00 = temp          ( Registers R01 thru R14 are to be initialized before executing "U*V7" )

•  R01 = x1           •  R08 = y1                                                    R15 = z1
•  R02 = x2           •  R09 = y2                                                    R16 = z2
.............               ............          When the program stops:        ...........

•  R07 = x7           •  R14 = y7                                                    R21 = z7
Flags: /
Subroutines: /

-Synthetic register M may be replaced by  R22

 01  LBL "U*V7"  02  15.021  03  STO 00  04  7  05  STO M  06  LBL 01  07  RCL 02         08  RCL 11  09  *  10  RCL 04  11  RCL 09 12  *  13  -  14  RCL 03  15  RCL 14  16  *  17  +  18  RCL 07         19  RCL 10  20  *  21  -  22  RCL 05 23  RCL 13  24  *  25  +  26  RCL 06  27  RCL 12  28  *  29  -  30  STO IND 00  31  RCL 07         32  X<> 06  33  X<> 05 34  X<> 04  35  X<> 03  36  X<> 02  37  X<> 01  38  STO 07  39  RCL 14  40  X<> 13         41  X<> 12  42  X<> 11  43  X<> 10  44  X<> 09 45  X<> 08  46  STO 14  47  ISG 00  48  CLX  49  DSE M  50  GTO 01    51  RCL 00         52  7  53  -  54  END

( 85 bytes / SIZE 022 )

 STACK INPUT OUTPUT X / 15.021

Example:    U( 6  2  4  7  10  13  12 )    V( 4  6  3  2  5  7  8 )

-Store these 14 numbers in R01 thru R14

XEQ "U*V7"  >>>>   15.021                          ---Execution time = 10s---

-And you get in R15 thru R21

W( -37  26  73  59  -40  33  -47 )

Notes:

-Registers R01 to R14 are modified during execution, but their contents is restored at the end.
-If n > 3 & n # 7 , the cross-product cannot be defined as a vector:

c) n-Dimensional Vectors

- U(x1,x2,....,xn)   and  V(y1,y2,....,yn)  are 2 vectors.

- In n-dimensional spaces, the cross product is actually an antisymmetric Tensor  P = [Pi,j]   where  Pi,j = xi.yj - xj.yi         i , j = 1 , 2 , ...... , n
-"U*V" computes and stores the  n2  components of this tensor   ( in column order )

Data Registers:               R00 = n                              ( Rbb thru Ree & Rbb' thru Ree' are to be initialized before executing  "U*V" )

•   Rbb = x1 ,   •   Rbb+1 = x2 , .......... ,  •   Ree = xn                     ee - bb = ee' - bb' = n - 1
•   Rbb' = y1 ,  •   Rbb'+1 = y2 , .......... , •   Ree' = yn

>>>>  When the program stops,    Rbb" to Ree" contain the n2 coefficients of the product  ( in column order )                 ee" - bb" = n2 - 1
Flags: /
Subroutines: /

 01  LBL "U*V" 02  STO M  03  RDN 04  STO O 05  X<>Y 06  STO N        07  INT 08  CHS 09  LASTX 10  FRC 11   E3 12  * 13  + 14  1 15  + 16  STO 00 17  X^2 18  1 19  - 20  .1 21  % 22  STO P  23  LBL 01 24  RCL P 25  INT 26  STO Y 27  RCL 00        28  ST/ Z 29  MOD 30  X<>Y 31  INT 32  RCL X 33  RCL O 34  + 35  RDN 36  RCL IND T 37  RCL N  38  R^ 39  ST+ Y 40  R^ 41  R^ 42  RCL IND T 43  * 44  RCL O        45  ST+ T 46  CLX 47  RCL IND T 48  RCL N 49  ST+ T 50  CLX 51  RCL IND T 52  * 53  - 54  STO IND M 55  ISG M 56  CLX 57  ISG P 58  GTO 01 59  RCL N  60  RCL M 61  ENTER^ 62  DSE X 63   E3 64  / 65  + 66  RCL 00 67  X^2 68  - 69  RCL 00         70   E5 71  / 72  + 73  RCL O  74  X<>Y 75  CLA 76  END

( 115 bytes )

 STACK INPUTS OUTPUTS Z bbb.eee(U) bbb.eee(U) Y bbb.eee(V) bbb.eee(V) X bbb(P) bbb.eee,nn(P)

Example1:     U(2,3,7,1)   V(3,1,4,6)     Calculate the 16 coefficients of  P = UxV

-For instance,

2  STO 01   3  STO 02   7  STO 03   1  STO 04    ( control number = 1.004 )
3  STO 06   1  STO 07   4  STO 08   6  STO 09    ( control number = 6.009 )

-If we choose register R12 as the first register for the product:

1.004  ENTER^
6.009  ENTER^
12     XEQ "U*V"   >>>>   12.02704   ( in 17 seconds )   and we get:

0   -7   -13   9                    R12   R16   R20   R24
7    0     5    17        =        R13   R17   R21   R25      respectively.
13  -5    0    38                   R14   R18   R22   R26
-9  -17 -38   0                    R15   R19   R23   R27

-There are only  n(n-1)/2  independent coefficients  ( 6 in this example ) and the diagonal elements are always zeros.

Example2:     U(2,3,7)   V(3,1,4)     Compute   UxV

2  STO 01   3  STO 02   7  STO 03      ( control number = 1.003 )
3  STO 06   1  STO 07   4  STO 08      ( control number = 6.008 )

-If we choose again register R12 as the first register for the product:

1.003  ENTER^
6.008  ENTER^
12        R/S     >>>>   12.02003   ( in 10s ) , we get:

0   -7  -13                  R12   R15   R18
7    0     5         =       R13   R16   R19     respectively.
13  -5    0                   R14   R17   R20

Note:

-If  n = 3 , there are only 3 independent coefficients and p23 ,  p31 ,  p12   ( in this order )
form the components of the usual cross product   ( which is actually a pseudo-vector )
-In this example, we recognize  UxV = (5,13,-7)