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.