Operations#
import numpy as np
Tensordot#
np.tensordot
realises tensors product by specified axes.
Consider as targets for the example two-dimentional matrices:
\[\begin{split}
a = \left(\begin{array}{cc}
a_{11} = 1 & a_{12} = 2 \\
a_{21} = 3 & a_{22} = 4 \\
\end{array}\right)
\end{split}\]
\[\begin{split}
b = \left(\begin{array}{cc}
b_{11} = 5 & b_{12} = 6 \\
b_{21} = 7 & b_{22} = 8 \\
\end{array}\right)
\end{split}\]
Following cells multiplies matrices under consideration with all possible axes combinations specified.
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
\(D^a = \left\{0\right\}, D^b=\left\{0\right\}\)
\[\begin{split}
\left(\begin{array}{cc}
a_{11} \dot b_{11} + a_{21} \dot b_{21} = 26 & a_{11} \dot b_{12} + a_{21} \dot b_{22} = 30 \\
a_{12} \dot b_{11} + a_{22} \dot b_{21} = 38 & a_{12} \dot b_{12} + a_{22} \dot b_{22} = 44 \\
\end{array}\right)
\end{split}\]
np.tensordot(a, b, (0, 0))
array([[26, 30],
[38, 44]])
\(D^a = \left\{0\right\}, D^b=\left\{1\right\}\)
\[\begin{split}
\left(\begin{array}{cc}
a_{11} \dot b_{11} + a_{21} \dot b_{12} = 23 & a_{11} \dot b_{21} + a_{21} \dot b_{22} = 31 \\
a_{12} \dot b_{11} + a_{22} \dot b_{21} = 38 & a_{12} \dot b_{12} + a_{22} \dot b_{22} = 44 \\
\end{array}\right)
\end{split}\]
np.tensordot(a, b, (0, 1))
array([[23, 31],
[34, 46]])
\(D^a = \left\{1\right\}, D^b=\left\{0\right\}\)
\[\begin{split}
\left(\begin{array}{cc}
a_{11} \dot b_{11} + a_{12} \dot b_{21} = 19 & a_{11} \dot b_{12} + a_{12} \dot b_{22} = 22 \\
a_{21} \dot b_{11} + a_{22} \dot b_{21} = 43 & a_{21} \dot b_{11} + a_{22} \dot b_{22} = 50 \\
\end{array}\right)
\end{split}\]
np.tensordot(a, b, (1, 0))
array([[19, 22],
[43, 50]])
\(D^a = \left\{1\right\}, D^b=\left\{1\right\}\)
\[\begin{split}
\left(\begin{array}{cc}
a_{11} \dot b_{11} + a_{12} \dot b_{12} = 17 & a_{11} \dot b_{21} + a_{12} \dot b_{22} = 23 \\
a_{21} \dot b_{11} + a_{22} \dot b_{12} = 39 & a_{21} \dot b_{21} + a_{22} \dot b_{22} = 53 \\
\end{array}\right)
\end{split}\]
np.tensordot(a, b, (1, 1))
array([[17, 23],
[39, 53]])
\(D^a = \left\{0, 1\right\}, D^b=\left\{0, 1\right\}\)
\[
a_{11}b_{11} + a_{12}b_{12} + a_{21}b_{21} + a_{22}b_{22} = 70
\]
np.tensordot(a, b, ((0, 1), (0, 1)))
array(70)
\(D^a = \left\{ 1, 0 \right\}, D^b=\left\{ 0, 1\right\}\)
\[a_{11}b_{11} + a_{12}b_{21} + a_{21}b_{21} + a_{22}b_{22} = 69\]
np.tensordot(a, b, ((1, 0), (0, 1)))
array(69)