{:check ["true"]}
import numpy as np
Let's create a $2\times 3$ matrix as 2D nd-array. $$ x = \left[\begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ \end{array}\right] $$
x = np.array([[1,2,3], [4,5,6]])
numpy
ndarray allows natural operations that are closely analogous to the mathematical notations.
Here is the element-wise addition matrices: $x + x$.
x + x
Here is element-wise inverse of the matrix $x$.
1 / x
Here is the element-wise product of matrices.
x * x
We can take the element-wise power of the matrix $x$.
x ** 2
Numpy comes with another function numpy.power
that performs
exponentiation.
np.power(x, 2)
Let's create another matrix, also $2\times 3$ in dimensions.
$$ y = x + 10 $$y = x + 10
y
We can perform element-wise multiplication between $x$ and $y$ because they have the same shape.
x * y
But we cannot quite multiple them as matrices because their shapes are not compatible.
x @ y
However, we can compute the transpose of $y$.
$y^T$ has a shape of $3\times 2$, which is compatible to be multiplied with $x$.
y.T
The result of $A_\mathrm{2\times 3}\cdot B_\mathrm{3\times 2} = C_\mathrm{2\times 2}$ is a square matrix of shape $2\times 2$.
x @ y.T
We can also multiply the transpose of $x$ with $y$.
$A_\mathrm{3\times 2}\cdot B_\mathrm{2\times 3} = C_\mathrm{3\times 3}$
x.T @ y