The Museum of HP Calculators

Dot Product and Cross Product 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

-3 programs are listed hereafter:

1°)     "DOT" computes the dot-product of 2  n-dimensional vectors ( and their norms and angle too )
2°) a) "CROSS" ------ the cross-product of  2  three-dimensional vectors
2°) b) "U*V" calculates the nxn coefficients of the Tensor P = [Pi,j]  which generalizes the cross-product in n-dimensional spaces.

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

1°) Dot Product

-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
•   Rbb' = y1 , •   Rbb'+1 = y2 , .......... , •   Ree' = yn                  before executing  "DOT" )                 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  ""                     ( TEXT 0  or another NOP instruction like LBL 00 ... )
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 )

Note:   The dot product is commutative:   V.U = U.V

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  ""                         ( TEXT 0 or another NOP instruction:  LBL 00 ... )
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

b) 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 = x1 ,  •   Rbb+1 = x2 , .......... ,  •   Ree = xn        (  These 2n registers are to be initialized
•   Rbb' = y1 , •   Rbb'+1 = y2 , .......... , •   Ree' = yn           before executing  "U*V" )     ee - bb = ee' - bb' = n - 1

and 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      ( synthetic )
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 )
and if we choose register R12 as the first register of 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        in registers      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 )
and if we choose register R12 as the first register of 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          in registers       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)