Symbolic Tensor Notebook

[1]:
import sympy as sp
from IPython.display import Markdown


from mechpy.core.symbolic.tensor import (
    SymbolicTensor,
    SymbolicThreeByThreeTensor,
    SymbolicSymmetricThreeByThreeTensor,
    SymbolicSixBySixTensor,
)

Symbolic Tensor

[2]:
a, b, c, d, e, f, g, h, i = sp.symbols("a b c d e f g h i")

Init Method

[3]:
data = sp.ImmutableDenseNDimArray([[a, b], [c, d]])
A = SymbolicTensor(data)
display(A.data)
$\displaystyle \left[\begin{matrix}a & b\\c & d\end{matrix}\right]$

From List method

Symbolic Tensor

[4]:
A = SymbolicTensor.from_list([a, b, c, d], shape=(2, 2))
display(A.data)
$\displaystyle \left[\begin{matrix}a & b\\c & d\end{matrix}\right]$
[5]:
A = SymbolicTensor.from_list([a, b, c, d, e, f, g, h, i], shape=(3, 3))
display(A.data)
$\displaystyle \left[\begin{matrix}a & b & c\\d & e & f\\g & h & i\end{matrix}\right]$

Symbolic 3x3 Tensor

[6]:
A = SymbolicThreeByThreeTensor.from_list([a, b, c, d, e, f, g, h, i])
display(A.data)
$\displaystyle \left[\begin{matrix}a & b & c\\d & e & f\\g & h & i\end{matrix}\right]$

Symbolic Symmetric 3x3 Tensor

[7]:
list_notation_standard = [a, b, c, b, d, e, c, e, f]
list_notation_voigt = [a, f, e, f, b, d, e, d, c]
List 1

Default notation

[8]:
A = SymbolicSymmetricThreeByThreeTensor.from_list(list_notation_standard)
display(A.__class__)
display(A.data)
display(A.notation)
mechpy.core.symbolic.tensor.SymbolicSymmetricThreeByThreeTensor
$\displaystyle \left[\begin{matrix}a & b & c & d & e & f\end{matrix}\right]$
'standard'
[9]:
B = A.to_general()
display(B.data)
$\displaystyle \left[\begin{matrix}a & b & c\\b & d & e\\c & e & f\end{matrix}\right]$

Notation 2

[10]:
A = SymbolicSymmetricThreeByThreeTensor.from_list(list_notation_voigt, notation="voigt")
display(A.data)
display(A.notation)
$\displaystyle \left[\begin{matrix}a & b & c & d & e & f\end{matrix}\right]$
'voigt'
[11]:
B = A.to_general()
display(B.data)
$\displaystyle \left[\begin{matrix}a & f & e\\f & b & d\\e & d & c\end{matrix}\right]$
List 2

Default notation

[12]:
A = SymbolicSymmetricThreeByThreeTensor.from_list(list_notation_voigt)
display(A.data)
display(A.notation)
$\displaystyle \left[\begin{matrix}a & f & e & b & d & c\end{matrix}\right]$
'standard'
[13]:
B = A.to_general()
display(B.data)
$\displaystyle \left[\begin{matrix}a & f & e\\f & b & d\\e & d & c\end{matrix}\right]$

Notation 2

[14]:
A = SymbolicSymmetricThreeByThreeTensor.from_list(list_notation_voigt, notation="voigt")
display(A.data)
display(A.notation)
$\displaystyle \left[\begin{matrix}a & b & c & d & e & f\end{matrix}\right]$
'voigt'
[15]:
B = A.to_general()
display(B.data)
$\displaystyle \left[\begin{matrix}a & f & e\\f & b & d\\e & d & c\end{matrix}\right]$

list of 6 elements

default notation

[16]:
A = SymbolicSymmetricThreeByThreeTensor.from_list([a, b, c, d, e, f])
display(A.data)
display(A.notation)
$\displaystyle \left[\begin{matrix}a & b & c & d & e & f\end{matrix}\right]$
'standard'
[17]:
B = A.to_general()
display(B.data)
$\displaystyle \left[\begin{matrix}a & b & c\\b & d & e\\c & e & f\end{matrix}\right]$

Notation 2

[18]:
A = SymbolicSymmetricThreeByThreeTensor.from_list([a, b, c, d, e, f], notation="voigt")
display(A.data)
display(A.notation)
$\displaystyle \left[\begin{matrix}a & b & c & d & e & f\end{matrix}\right]$
'voigt'
[19]:
B = A.to_general()
display(B.data)
$\displaystyle \left[\begin{matrix}a & f & e\\f & b & d\\e & d & c\end{matrix}\right]$

Symbolic 6x6 Tensor

List 36 elements

[20]:
data_list = list(range(1,37))
A = SymbolicSixBySixTensor.from_list(data_list)
display(A.data)
$\displaystyle \left[\begin{matrix}1 & 2 & 3 & 4 & 5 & 6\\7 & 8 & 9 & 10 & 11 & 12\\13 & 14 & 15 & 16 & 17 & 18\\19 & 20 & 21 & 22 & 23 & 24\\25 & 26 & 27 & 28 & 29 & 30\\31 & 32 & 33 & 34 & 35 & 36\end{matrix}\right]$

List 6x6 elemnts

[21]:
data_list = [[i * 6 + j + 1 for j in range(6)] for i in range(6)]
A = SymbolicSixBySixTensor.from_list(data_list)
display(A.data)
$\displaystyle \left[\begin{matrix}1 & 2 & 3 & 4 & 5 & 6\\7 & 8 & 9 & 10 & 11 & 12\\13 & 14 & 15 & 16 & 17 & 18\\19 & 20 & 21 & 22 & 23 & 24\\25 & 26 & 27 & 28 & 29 & 30\\31 & 32 & 33 & 34 & 35 & 36\end{matrix}\right]$

Create Method

This method is a simple way to create a tensor.

[22]:
A = SymbolicTensor.create(shape=(2, 2), name="x")
display(A.data)
$\displaystyle \left[\begin{matrix}x_{11} & x_{12}\\x_{21} & x_{22}\end{matrix}\right]$
[23]:
A = SymbolicThreeByThreeTensor.create(name="X")
display(A.data)
$\displaystyle \left[\begin{matrix}X_{11} & X_{12} & X_{13}\\X_{21} & X_{22} & X_{23}\\X_{31} & X_{32} & X_{33}\end{matrix}\right]$

Custom name

[24]:
A = SymbolicSixBySixTensor.create(name="a")
display(A.data)
display(A.name)
$\displaystyle \left[\begin{matrix}a_{11} & a_{12} & a_{13} & a_{14} & a_{15} & a_{16}\\a_{21} & a_{22} & a_{23} & a_{24} & a_{25} & a_{26}\\a_{31} & a_{32} & a_{33} & a_{34} & a_{35} & a_{36}\\a_{41} & a_{42} & a_{43} & a_{44} & a_{45} & a_{46}\\a_{51} & a_{52} & a_{53} & a_{54} & a_{55} & a_{56}\\a_{61} & a_{62} & a_{63} & a_{64} & a_{65} & a_{66}\end{matrix}\right]$
'a'
[25]:
A = SymbolicSymmetricThreeByThreeTensor.create(name="x")
display(A.data)
display(A.name)
display(A.notation)
display(A.to_general().data)

$\displaystyle \left[\begin{matrix}x_{1} & x_{2} & x_{3} & x_{4} & x_{5} & x_{6}\end{matrix}\right]$
'x'
'standard'
$\displaystyle \left[\begin{matrix}x_{1} & x_{2} & x_{3}\\x_{2} & x_{4} & x_{5}\\x_{3} & x_{5} & x_{6}\end{matrix}\right]$
[26]:
A = SymbolicSymmetricThreeByThreeTensor.create(name="x", notation="voigt")
display(A.data)
display(A.name)
display(A.notation)
display(A.to_general().data)
$\displaystyle \left[\begin{matrix}x_{11} & x_{22} & x_{33} & x_{23} & x_{13} & x_{12}\end{matrix}\right]$
'x'
'voigt'
$\displaystyle \left[\begin{matrix}x_{11} & x_{12} & x_{13}\\x_{12} & x_{22} & x_{23}\\x_{13} & x_{23} & x_{33}\end{matrix}\right]$
[27]:
A = SymbolicTensor.from_list([a, 0, 0, b], shape=(2, 2))
B = SymbolicTensor.from_list([1, c, 1, d], shape=(2, 2))
C = A @ B
display(C.data)
$\displaystyle \left[\begin{matrix}a & a c\\b & b d\end{matrix}\right]$

Converting

Tensor to 3x3

[28]:
A = SymbolicTensor.from_list([a, b, c, b, d, e, c, e, f], shape=(3,3))
display(type(A))
B = A.to_3x3()
display(type(B))
mechpy.core.symbolic.tensor.SymbolicTensor
mechpy.core.symbolic.tensor.SymbolicThreeByThreeTensor

General to Symmetric 3x3

[29]:
A = SymbolicTensor.from_list([a, b, c, b, d, e, c, e, f], shape=(3,3))
display(type(A))
display(A.is_symmetric())
B = A.to_sym_3x3()
display(type(B))
display(B.data)
mechpy.core.symbolic.tensor.SymbolicTensor
True
mechpy.core.symbolic.tensor.SymbolicSymmetricThreeByThreeTensor
$\displaystyle \left[\begin{matrix}a & b & c & d & e & f\end{matrix}\right]$

3x3 to Symmetric

[30]:
A = SymbolicThreeByThreeTensor.from_list([a, b, c, b, d, e, c, e, f])
display(type(A))
display(A.data)
display(A.is_symmetric())
B = A.to_symmetric()
display(type(B))
display(B.data)
mechpy.core.symbolic.tensor.SymbolicThreeByThreeTensor
$\displaystyle \left[\begin{matrix}a & b & c\\b & d & e\\c & e & f\end{matrix}\right]$
True
mechpy.core.symbolic.tensor.SymbolicSymmetricThreeByThreeTensor
$\displaystyle \left[\begin{matrix}a & b & c & d & e & f\end{matrix}\right]$

Converting to general

[31]:
A = SymbolicSymmetricThreeByThreeTensor.from_list([a, b, c, d, e, f])
display(type(A))
display(A.data)
mechpy.core.symbolic.tensor.SymbolicSymmetricThreeByThreeTensor
$\displaystyle \left[\begin{matrix}a & b & c & d & e & f\end{matrix}\right]$
[32]:
B = A.to_general()
display(type(B))
display(B.data)
mechpy.core.symbolic.tensor.SymbolicThreeByThreeTensor
$\displaystyle \left[\begin{matrix}a & b & c\\b & d & e\\c & e & f\end{matrix}\right]$

Multiplication

Getting Tensor Components

[33]:
A = SymbolicTensor.from_list([a, b, c, d], shape=(2, 2))
display(A.data)
$\displaystyle \left[\begin{matrix}a & b\\c & d\end{matrix}\right]$
[34]:
display([[A[0, 0], A[0, 1]], [A[1, 0], A[1, 1]]])
[[a, b], [c, d]]
[35]:
display([[A[0][0], A[0][1]], [A[1][0], A[1][1]]])
[[a, b], [c, d]]

Eigenvalues and Eigenvectors

[36]:
A = SymbolicTensor.from_list([[a, 0], [0, b]], shape=(2, 2))
eigenvectors = A.eigenvectors()
display(eigenvectors)
[(a,
  1,
  [Matrix([
   [1],
   [0]])]),
 (b,
  1,
  [Matrix([
   [0],
   [1]])])]
[37]:
A = SymbolicTensor.from_list([[a, 0, 0], [0, a, 0], [0, 0, b]], shape=(3, 3))
eigenvectors = A.eigenvectors()
display(eigenvectors)
[(a,
  2,
  [Matrix([
   [1],
   [0],
   [0]]),
   Matrix([
   [0],
   [1],
   [0]])]),
 (b,
  1,
  [Matrix([
   [0],
   [0],
   [1]])])]
[38]:
A = SymbolicTensor.from_list([[4, -2, 0], [3, 1, -4], [0, 0, 8]], shape=(3, 3))
eigenvectors = A.eigenvectors()
display(*[_[2][0] for _ in eigenvectors])
$\displaystyle \left[\begin{matrix}\frac{4}{17}\\- \frac{8}{17}\\1\end{matrix}\right]$
$\displaystyle \left[\begin{matrix}\frac{1}{2} - \frac{\sqrt{15} i}{6}\\1\\0\end{matrix}\right]$
$\displaystyle \left[\begin{matrix}\frac{1}{2} + \frac{\sqrt{15} i}{6}\\1\\0\end{matrix}\right]$
[39]:
A = SymbolicTensor.from_list([[a, 0, c], [a, b, b], [a, 0, b]], shape=(3, 3))
eigenvectors = A.eigenvectors()
for eig in eigenvectors:
    eig_val = eig[0]
    multiplicity = eig[1]
    eig_vects = eig[2][0]
    display(eig_val)
    display(multiplicity)
    display(eig_vects)
$\displaystyle b$
1
$\displaystyle \left[\begin{matrix}0\\1\\0\end{matrix}\right]$
$\displaystyle \frac{a}{2} + \frac{b}{2} - \frac{\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}$
1
$\displaystyle \left[\begin{matrix}- \frac{b}{a} + \frac{\frac{a}{2} + \frac{b}{2} - \frac{\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}}{a}\\\frac{- b + c}{c} + \frac{b \left(\frac{a}{2} + \frac{b}{2} - \frac{\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}\right)}{a c}\\1\end{matrix}\right]$
$\displaystyle \frac{a}{2} + \frac{b}{2} + \frac{\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}$
1
$\displaystyle \left[\begin{matrix}- \frac{b}{a} + \frac{\frac{a}{2} + \frac{b}{2} + \frac{\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}}{a}\\\frac{- b + c}{c} + \frac{b \left(\frac{a}{2} + \frac{b}{2} + \frac{\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}\right)}{a c}\\1\end{matrix}\right]$
[40]:
P, D = A.diagonalize()
display(P)
display(D)
$\displaystyle \left[\begin{matrix}0 & \frac{a - b - \sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2 a} & \frac{a - b + \sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2 a}\\1 & \frac{a \left(- b + c\right) + \frac{b \left(a + b - \sqrt{a^{2} - 2 a b + 4 a c + b^{2}}\right)}{2}}{a c} & \frac{a \left(- b + c\right) + \frac{b \left(a + b + \sqrt{a^{2} - 2 a b + 4 a c + b^{2}}\right)}{2}}{a c}\\0 & 1 & 1\end{matrix}\right]$
$\displaystyle \left[\begin{matrix}b & 0 & 0\\0 & \frac{a}{2} + \frac{b}{2} - \frac{\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2} & 0\\0 & 0 & \frac{a}{2} + \frac{b}{2} + \frac{\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}\end{matrix}\right]$
[41]:
B = SymbolicSymmetricThreeByThreeTensor.from_list([a, b, b, 0, c, 0])
display(B.to_general().data)
$\displaystyle \left[\begin{matrix}a & b & b\\b & 0 & c\\b & c & 0\end{matrix}\right]$
[42]:
P, D = B.diagonalize()
display(P)
display(D)
$\displaystyle \left[\begin{matrix}0 & \frac{a - c - \sqrt{a^{2} - 2 a c + 8 b^{2} + c^{2}}}{2 b} & \frac{a - c + \sqrt{a^{2} - 2 a c + 8 b^{2} + c^{2}}}{2 b}\\-1 & 1 & 1\\1 & 1 & 1\end{matrix}\right]$
$\displaystyle \left[\begin{matrix}- c & 0 & 0\\0 & \frac{a}{2} + \frac{c}{2} - \frac{\sqrt{a^{2} - 2 a c + 8 b^{2} + c^{2}}}{2} & 0\\0 & 0 & \frac{a}{2} + \frac{c}{2} + \frac{\sqrt{a^{2} - 2 a c + 8 b^{2} + c^{2}}}{2}\end{matrix}\right]$