```python
# N-dim arrays
# four classes are available: dense/sparse + mutable/immutable
from sympy import *
#from sympy import Array
init_printing(use_unicode=True)
```


```python
A = Array([[1,2],[3,4]])
```


```python
A
```


```python
B = Array(range(12), (3,4))
```


```python
B
```


```python
C = Array(range(12), (2,2,3))
```


```python
C
```


```python
# rank of the Array
A.rank()
```


```python
B.rank()
```


```python
C.rank()
```


```python
A.tomatrix()
```


```python
A.tolist()
```


```python
B.tomatrix()
```


```python
C.tolist()
```


```python
# no default functionality for converting higher-order tensors into matrices
C.tomatrix()
```


```python
# tensor products: higher-order tensors from lower-order tensors

# unit vectors
##############

i = Array([1,0,0])
j = Array([0,1,0])
k = Array([0,0,1])
```


```python
ii = tensorproduct(i,i)
```


```python
ii
```


```python
ij = tensorproduct(i,j)
```


```python
ij

```


```python
# similarly
# ii, ij, ik
# ji, jj, jk
# ki, kj, kk

```


```python
i.rank()
```


```python
ii.rank()
```


```python
I2 = Array([[1,0,0], [0,1,0], [0,0,1]])
```


```python
I2
```


```python
# fourth-order tensor
# I4_1_ijkl = delta_ij delta_kl
I4_1 = tensorproduct(I2, I2)
```


```python
I4_1
```


```python
Matrix(I4_1)
```


```python
Matrix(I4_1).reshape(9,9)
```


```python
# I4_2_ijkl = delta_ik delta_jl
I4_2 = eye(9)
```


```python
I4_2
```


```python
# tensor contraction
A = Array([[1,2,3],[4,5,6],[7,8,9]])
```


```python
A
```


```python
# the matrix trace is equivalent to contaction of second-order tensor along axes 1 and 2
# since the starting index in Python is zero.
trA = tensorcontraction(A, (0,1))
```


```python
trA
```


```python
# matrix product is the contaction of fourth order tensor along axes 2 and 3
# of the fourth-order tensor formed as the tensor product of the matrices as rank-2 tensor
D = Array([[1,2],[3,4]])
```


```python
DtD = tensorproduct(D,D)
```


```python
DtD
```


```python
DtD.rank()
```


```python
tensorcontraction(DtD,(1,2))
```


```python
D.tomatrix()*D.tomatrix()
```


```python
#
```


```python
#
```


```python
# indexed objects
# A[i,j]
# A is the IndexedBase
# i and j are indices

A = IndexedBase('A')
i,j = symbols('i j', cls=Idx)
```


```python
A[i,j]
```


```python
A[i,j].shape
```


```python
i = Idx('i', 3)
j = Idx('j', 3)
```


```python
A[i,j].shape
```


```python
A[i,j].ranges
```


```python
i.lower
```


```python
i.upper
```


```python
# index with unbounded upper limit
k = Idx('k', oo)
```


```python
k.lower
```


```python
k.upper
```


```python
#
```


```python
#
```


```python
# Matrix expressions
from sympy import MatrixSymbol, Matrix
```


```python
F = MatrixSymbol('F',3,3)
```


```python
F.shape
```


```python
F
```


```python
Matrix(F)
```


```python
A = MatrixSymbol('A',3,3)
B = MatrixSymbol('B',3,3)
C = MatrixSymbol('C',3,3)
```


```python
Matrix(C)
```


```python
# MatrixAddition
Matrix(MatAdd(A,B,C))
```


```python
MatMul(A,B,C)
```


```python
Matrix(MatMul(A,B,C))
```


```python
Matrix(hadamard_product(A,B))
```


```python
# matrix inverse
Ainv = Inverse(A)
```


```python
Ainv
```


```python
IA = Trace(A)
```


```python
IA
```


```python
# tensor calculs

i = Idx('i', 3)
j = Idx('j', 3)
k = Idx('k', 3)
l = Idx('l', 3)

```


```python
A=MatrixSymbol('A',3,3)
```


```python
A
```


```python
print(diff(F[i,j],F[i,j]))
```

    1



```python
print(diff(F[k,l],F[i,j]))
```

    KroneckerDelta(i, k)*KroneckerDelta(j, l)



```python
from sympy.concrete.delta import _simplify_delta

_simplify_delta(diff(F[k,l],F[i,j]))
```


```python
C = F.T*F
```


```python
print(diff(C[i,j],F[k,l]))
_simplify_delta(diff(C[i,j],F[k,l]))
```


```python
simplify(diff(C[i,j],F[k,l]))
```


```python
Determinant(A)
```


```python
LeviCivita(i,j,k)
```


```python
Finv=Inverse(F)
```


```python
diff(Determinant(F),F[k,l])
```


```python

```

8f77cb5c1602cc75c30813f22755c9641b320b84
ipynb
Jupyter Notebook
6-calculus-tensors.ipynb
chennachaos/SA2CTechChatSymPy
9f1dbb48655ff5f8bdd6b4ced48b58aed0ba5bf4
MIT
6-calculus-tensors.ipynb
chennachaos/SA2CTechChatSymPy
9f1dbb48655ff5f8bdd6b4ced48b58aed0ba5bf4
MIT
6-calculus-tensors.ipynb
chennachaos/SA2CTechChatSymPy
9f1dbb48655ff5f8bdd6b4ced48b58aed0ba5bf4
MIT
Qwen/Qwen-72B
1. YES
2. YES