Showing posts with label tensor product. Show all posts
Showing posts with label tensor product. Show all posts

Saturday, October 21, 2023

HP 15C: Vector Operations

HP 15C:   Vector Operations


Calculators covered:


* HP 15C

* HP 15C Limited Edition

* HP 15C Collector's Edition

* Apps, Swiss Micros DM 15



HP 15C:   Dot Product, Tensor Product, and Angle Between Vectors



Set vectors A and B as 3 x 1 vectors (3 rows, 1 column)


3 ENTER 1 dim A

3 ENTER 1 dim B


Dot Product  (LBL D)  


Dot product:  A ⋅ B = A^T B 

The result is a single number.


Code: 

001 : 42,21,14 : LBL D

002 : 45,16,11 : RCL MATRIX A

003 : 42,16, 4 : MATRIX 4

004 : 45, 16, 12 : RCL MATRIX B

005 : 42, 26, 13 : RESULT C

006 : 20 : ×

007 : 45,16,11 : RCL MATRIX A

008 : 42,16, 4 : MATRIX 4

009 : 45,13 : RCL C

010 : 43,32 : RTN


Tensor Product (LBL E)


Tensor product:  A ⊗ B = A B^T

The result is a 3 x 3 matrix


Code: 

011 : 42,21,15 : LBL E

012 : 45,16,11 : RCL MATRIX A

013 : 45,16,12 : RCL MATRIX B

014 : 42,16, 4 : MATRIX 4

015 : 42,26,13 : RESULT C

016 : 20 : ×

017 : 45,16,12 : RCL MATRIX B

018 : 41,16, 4 : MATRIX 4

019 : 42,16, 1 : MATRIX 1

020 : 45,16,13 : RCL MATRIX C

021 : 43, 32 : RTN


Angle Between Vectors (LBL 0)


θ = arccos( (A ⋅ B) ÷ (||A|| ||B||) )


Code:

022 : 42,21, 0 : LBL 0

023 : 32,14 : GSB D

024 : 45,16,11 : RCL MATRIX A

025 : 42,16, 8 : MATRIX 8

026 : 45,16,12 : RCL MATRIX B

027 : 42,16, 8 : MATRIX 8

028 : 20 : ×

029 : 10 : ÷

030 : 43,24 : COS^-1

031 : 43,32 : RTN


Notes:  


MATRIX 4:  Transposes a matrix, which it is stored in the original matrix slot.  


MATRIX 8:  The 2-norm of a matrix.   It is calculated by taking a square root of the sum of the square of each element.  √(Σ( A_r,c ^ 2,  r = 1 to n and c = 1 to m))


For the dot and tensor products, I transpose the appropriate matrix back to the 3 x 1 form after the calculation.



Example


A = [ [ 4 ], [ 5 ], [ -2 ] ]

B = [ [ -3 ], [ 9 ], [ 8 ] ]


Dot Product:  GTO D R/S

Result:  17


Tensor Product:   GTO E R/S

Result:

[ [ -12, 36, 32 ], [ -15, 45, 40 ], [ 6, -18, -16 ] ]


Angle Between Vectors:  GTO 0 R/S

Result:  78.2166°



Eddie


All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Saturday, July 8, 2023

HP Prime and Python (Casio fx-9750GIII): Direct Sum and Tensor Product

HP Prime and Python (Casio fx-9750GIII):  Direct Sum and Tensor Product



This blog is about two operations in tensor algebra.  



DIRSUM:  Direct Sum


DIRSUM is the direct sum of two tensors, specifically column vectors.   The direct sum, symbolized by ⊕ (circle with a plus symbol in it), stacks column vectors on top of each other.  The order of the two vectors matters.


Example:


V1 = [ [ 2 ] [ 3 ] ]

V2 = [ [ 5 ] [ 6 ] [ 8 ] ]


V1 ⊕ V2 = [ [ 2 ] [ 3 ] [ 5 ] [ 6 ] [ 8 ] ]


V2 ⊕ V1 = [ [ 5 ] [ 6 ] [ 8 ] [ 2 ] [ 3 ] ]


The dimension of the direct sum is the sum of the dimensions of the vectors.



TENSOR:  Tensor Product 


The tensor product, also known as the outer product multiplies the numbers from V1 to each element V2 in order.   The tensor product can be represented in a column vector or a matrix.  





 If V1 and V2 are matrices, the outer product is calculated as:


V1 ⊗ V2 = V1 × V2ᵀ = V1 × transpose(V2)


Example:


V1 = [ [ 2 ] [ 3 ] ]

V2 = [ [ 5 ] [ 6 ] [ 8 ] ]


V1 ⊕ V2 = [ [ 10, 12, 16 ] [ 15, 18, 24 ] ]


V2 ⊕ V1 = [ [ 10, 15 ] [ 12, 18 ] [ 16, 24 ] ]


The tensor product is not commutative, order matters.  




HP Prime Programs:  DIRSUM and TENSOR


Note: DIRSUM and TENSOR accepts matrices or vectors in the form of column matrices.  


EXPORT DIRSUM(v,w)

BEGIN

// direct sum of 2 vectors

// 2023-04-30 EWS

LOCAL lv,lw,lc;

lv:=mat2list(v); 

lw:=mat2list(w);

lc:=CONCAT(lv,lw);

RETURN list2mat(lc,1);

END;


EXPORT TENSOR(v,w)

BEGIN

// tensor product of 2 vectors or matrices

// 2023-04-23 EWS

RETURN v*TRN(w);

END;





Python:  tensor.py


In this file, the tensor and dirsum functions are meant to be used with vectors only.  No outside libraries are required. 


# tensor file


# outer product

def tensor(x,y):

  t=[]

  lx=len(x)

  ly=len(y)

  for i in range(lx):

    c=[]

    for k in range(ly):

      w=x[i]*y[k]

      c.append(w)

    t.append(c)  

  return t


# direct sum

def dirsum(x,y):  

  return x+y


Source:


Bradley, Tai-Danae.  "The Tensor Products, Demystified"  math3ma.com   November 18, 2018.  https://www.math3ma.com/blog/the-tensor-product-demystified  Accessed April 24, 2023.




Eddie 


All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Monday, May 15, 2023

TI-84 Plus CE and TI-83 Premium CE: Vectors Program

TI-84 Plus CE and TI-83 Premium CE:  Vectors Program










The program VECTORS is made for the TI-84 Plus CE and TI-83 Premium CE.  


Introduction


The program VECTORS calculates the following of the three dimensional vectors [A] and [B]:


* Dot product of [A] and [B]

* Euclidean norm of vectors [A] and [B]

* Angle between vectors [A] and [B] in degrees

* Cross product of [A] × [B]

* Tensor product of [A] ⊗ [B]


The program uses the system matrices [A] and [B] and formats them as 3 rows, 1 column matrices.   The program changes the angle mode to Degrees.


After entering each vector, the program gives you an option to normalize the vectors.  Since the cross and tensor products do not follow the commutative property, there is an option to switch the elements of both vectors.  


Depending on the calculator used, results will be shown in either exact format (TI-83 Premium CE) or approximate format (TI-84 CE).  



Calculations Used 


In this section, each of the vectors will be followed by the program code used.  


Let [A] and [B] be the column vectors:

[A] = [[a1][a2][a3]]

[B] = [[b1][b2][b3]]


Dot Product: 

[A] • [B] = a1 * b1 + a2 * b2 + a3 * b3 = [A]ᵀ [B]




Euclidean Norm:

||[A]||₂ = √(a1² + a2² + a3²) = √([A]ᵀ [A])

||[B]||₂ = √(b1² + b2² + b3²) = √([B]ᵀ [B])





Angle Between Two Vectors:

θ = arccos(([A] • [B]) ÷ (||[A]||₂ * ||[B]||₂))




Cross Product:

[A] × [B] = [C] where:

c1 = a2 * b3 – a3 * b2

c2 = -a1 * b3 + a3 * b1

c3 = a1 * b2 – a2 * b1

The result is a 3 x 1 matrix. 




Tensor Product:

[A] ⊗ [B] = [A] [B]ᵀ

The result is a 3 x 3 matrix. 





Download 



You can download the program and the user guide here:

https://drive.google.com/file/d/1R1sHGfr8dKQDkH87QDE0HgMDWxeLNHPN/view?usp=share_link



Eddie 



All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


First Look: HP 16C Collector's Edition

 First Look: HP 16C Collector's Edition I just got the HP 16C Collector's Edition.   This is the famous HP 16C that specializes in c...