Skip to content

Vector & Matrix

Install packages

1
2
3
4
!uv pip install -q \
    numpy==2.3.0 \
    sympy==1.14.0 \
    latexify-py==0.4.4

Import packages

1
2
3
4
import latexify
import numpy as np
from IPython.display import Math
from sympy import Matrix, latex

In linear algebra, vectors are columns not rows.

Math(latex(Matrix([1, 2, 3, 4, 5])))

\(\displaystyle \left[\begin{matrix}1\\2\\3\\4\\5\end{matrix}\right]\)

Sum of two vectors

first_vector_to_sum = Matrix([1, 2, 3])
second_vector_to_sum = Matrix([4, 5, 6])

sum_of_two_vectors = first_vector_to_sum + second_vector_to_sum

Math(
    latex(first_vector_to_sum)
    + " + "
    + latex(second_vector_to_sum)
    + " = "
    + latex(sum_of_two_vectors)
)

\(\displaystyle \left[\begin{matrix}1\\2\\3\end{matrix}\right] + \left[\begin{matrix}4\\5\\6\end{matrix}\right] = \left[\begin{matrix}5\\7\\9\end{matrix}\right]\)

Multiplication of of two vectors

first_vector_to_multiply = Matrix([1, 2, 3])
second_vector_to_multiply = Matrix([4, 5, 6])

multiplication_of_two_vectors = first_vector_to_multiply.multiply_elementwise(
    second_vector_to_multiply
)

Math(
    latex(first_vector_to_multiply)
    + " \\odot "
    + latex(second_vector_to_multiply)
    + " = "
    + latex(multiplication_of_two_vectors)
)

\(\displaystyle \left[\begin{matrix}1\\2\\3\end{matrix}\right] \odot \left[\begin{matrix}4\\5\\6\end{matrix}\right] = \left[\begin{matrix}4\\10\\18\end{matrix}\right]\)

The dot product of two vectors

first_vector_to_multiply = Matrix([1, 2, 3])
second_vector_to_multiply = Matrix([4, 5, 6])

multiplication_of_two_vectors = first_vector_to_multiply.dot(
    second_vector_to_multiply
)

Math(
    latex(first_vector_to_multiply)
    + " \\cdot "
    + latex(second_vector_to_multiply)
    + " = "
    + latex(multiplication_of_two_vectors)
)

\(\displaystyle \left[\begin{matrix}1\\2\\3\end{matrix}\right] \cdot \left[\begin{matrix}4\\5\\6\end{matrix}\right] = 32\)

Matrix-vector multiplication

first_matrix_to_multiply = Matrix([[1, 2, 3], [4, 5, 6]])
first_vector_to_multiply = Matrix([1, 2, 3])
first_matrix_to_multiply_representation

multiplication_of_two_matrices = (
    first_matrix_to_multiply * first_vector_to_multiply
)

Math(
    latex(first_matrix_to_multiply)
    + " \\cdot "
    + latex(first_vector_to_multiply)
    + " = "
    + latex(multiplication_of_two_matrices)
)

\(\displaystyle \left[\begin{matrix}1 & 2 & 3\\4 & 5 & 6\end{matrix}\right] \cdot \left[\begin{matrix}1\\2\\3\end{matrix}\right] = \left[\begin{matrix}14\\32\end{matrix}\right]\)

Operation for the first row

\(1\cdot 1 + 2\cdot 2 + 3\cdot 3 = 14\)

Operation for the second row

\(4\cdot 1 + 5\cdot 2 + 6\cdot 3 = 32\)

Matrix-matrix multiplication

first_matrix_to_multiply = Matrix([[1, 2, 3], [4, 5, 6]])
second_matrix_to_multiply = Matrix([[7, 8], [9, 10], [11, 12]])

multiplication_of_two_matrices = (
    first_matrix_to_multiply * second_matrix_to_multiply
)

Math(
    latex(first_matrix_to_multiply)
    + " \\cdot "
    + latex(second_matrix_to_multiply)
    + " = "
    + latex(multiplication_of_two_matrices)
)

\(\displaystyle \left[\begin{matrix}1 & 2 & 3\\4 & 5 & 6\end{matrix}\right] \cdot \left[\begin{matrix}7 & 8\\9 & 10\\11 & 12\end{matrix}\right] = \left[\begin{matrix}58 & 64\\139 & 154\end{matrix}\right]\)

Entry (1,1): first row × first column

\(1\cdot 7 \;+\; 2\cdot 9 \;+\; 3\cdot 11 \;=\; 58\)

Entry (1,2): first row × second column

\(1\cdot 8 \;+\; 2\cdot 10 \;+\; 3\cdot 12 \;=\; 64\)

Entry (2,1): second row × first column

\(4\cdot 7 \;+\; 5\cdot 9 \;+\; 6\cdot 11 \;=\; 139\)

Entry (2,2): second row × second column

\(4\cdot 8 \;+\; 5\cdot 10 \;+\; 6\cdot 12 \;=\; 154\)

Identity Matrix

Identity matrix is like the number 1 in multiplication. Any matrix multiplied by the identity matrix results in the same matrix.

1
2
3
identity_matrix = Matrix(np.eye(3))

Math(latex(identity_matrix))

\(\displaystyle \left[\begin{matrix}1.0 & 0.0 & 0.0\\0.0 & 1.0 & 0.0\\0.0 & 0.0 & 1.0\end{matrix}\right]\)

vector_to_multiply_with_identity_matrix = Matrix([5, 10, 15])
identity_matrix_to_multiply_with_vector = Matrix(np.eye(3))

multiplication_with_identity_matrix = (
    identity_matrix_to_multiply_with_vector
    * vector_to_multiply_with_identity_matrix
)

Math(
    latex(identity_matrix_to_multiply_with_vector)
    + " \\cdot "
    + latex(vector_to_multiply_with_identity_matrix)
    + " = "
    + latex(multiplication_with_identity_matrix)
)

\(\displaystyle \left[\begin{matrix}1.0 & 0.0 & 0.0\\0.0 & 1.0 & 0.0\\0.0 & 0.0 & 1.0\end{matrix}\right] \cdot \left[\begin{matrix}5\\10\\15\end{matrix}\right] = \left[\begin{matrix}5.0\\10.0\\15.0\end{matrix}\right]\)

Matrix inverse

square_matrix_to_compute_inverse = Matrix([[1, 2, 2], [3, 5, 6], [7, 8, 9]])

inverse_of_square_matrix = square_matrix_to_compute_inverse.inv()

square_matrix_multiplication_with_its_inverse = (
    square_matrix_to_compute_inverse * inverse_of_square_matrix
)

Math(
    latex(square_matrix_to_compute_inverse)
    + "^{-1} = "
    + latex(inverse_of_square_matrix)
    + " \\quad \\Rightarrow \\quad "
    + latex(square_matrix_to_compute_inverse)
    + " \\cdot "
    + latex(inverse_of_square_matrix)
    + " = "
    + latex(square_matrix_multiplication_with_its_inverse)
)

\(\displaystyle \left[\begin{matrix}1 & 2 & 2\\3 & 5 & 6\\7 & 8 & 9\end{matrix}\right]^{-1} = \left[\begin{matrix}- \frac{3}{5} & - \frac{2}{5} & \frac{2}{5}\\3 & -1 & 0\\- \frac{11}{5} & \frac{6}{5} & - \frac{1}{5}\end{matrix}\right] \quad \Rightarrow \quad \left[\begin{matrix}1 & 2 & 2\\3 & 5 & 6\\7 & 8 & 9\end{matrix}\right] \cdot \left[\begin{matrix}- \frac{3}{5} & - \frac{2}{5} & \frac{2}{5}\\3 & -1 & 0\\- \frac{11}{5} & \frac{6}{5} & - \frac{1}{5}\end{matrix}\right] = \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]\)