Skip to content

Matrix Operations and Linear Transformations

πŸš€ Matrix Operations: Transforming Data

A matrix A∈RmΓ—n\mathbf{A} \in \mathbb{R}^{m \times n} is a 2D array of scalars. Beyond being a simple data structure, a matrix represents a Linear Transformation from an nn-dimensional space to an mm-dimensional space.


🟒 Level 1: Core Operations

1. Matrix Multiplication (C=AB\mathbf{C} = \mathbf{A}\mathbf{B})

Matrix multiplication is not element-wise. Instead, the entry CijC_{ij} is the dot product of the ii-th row of A\mathbf{A} and the jj-th column of B\mathbf{B}: Cij=βˆ‘k=1nAikBkjC_{ij} = \sum_{k=1}^n A_{ik} B_{kj}

import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

# Matrix Multiplication (Dot Product)
C = np.dot(A, B)  # or A @ B
print(f"Matrix Product:\n{C}")

# Element-wise (Hadamard) Product
C_element = A * B
print(f"Element-wise Product:\n{C_element}")

2. Transpose and Special Matrices

  • Transpose (AT\mathbf{A}^T): Formed by swapping rows and columns (Aijβ†’AjiA_{ij} \to A_{ji}).
  • Symmetric Matrix: A square matrix where A=AT\mathbf{A} = \mathbf{A}^T.
  • Identity Matrix (I\mathbf{I}): A square matrix with 1s on the diagonal and 0s elsewhere. AI=A\mathbf{A}\mathbf{I} = \mathbf{A}.
  • Orthogonal Matrix: A square matrix where ATA=I\mathbf{A}^T\mathbf{A} = \mathbf{I}. Its columns are orthonormal.

🟑 Level 2: Rank and Invertibility

3. Matrix Rank

The rank of a matrix is the number of linearly independent rows or columns. It represents the dimension of the output space after the transformation.

  • Full Rank: A matrix has full rank if its rank equals the smaller of its dimensions.

4. Determinant (det⁑(A)\det(\mathbf{A}))

The determinant is a scalar value that represents the β€œvolume scaling factor” of the linear transformation.

  • If det⁑(A)=0\det(\mathbf{A}) = 0, the transformation collapses the space into a lower dimension.
  • If det⁑(A)β‰ 0\det(\mathbf{A}) \neq 0, the matrix is invertible.

5. Matrix Inverse (Aβˆ’1\mathbf{A}^{-1})

For a square, non-singular matrix, the inverse satisfies AAβˆ’1=I\mathbf{A}\mathbf{A}^{-1} = \mathbf{I}. It β€œundoes” the transformation performed by A\mathbf{A}.

# Calculating Determinant and Inverse
A = np.array([[1, 2], [3, 4]])

det_A = np.linalg.det(A)
inv_A = np.linalg.inv(A)

print(f"Determinant: {det_A}")
print(f"Inverse:\n{inv_A}")

πŸ”΄ Level 3: Advanced Concepts

6. The Moore-Penrose Pseudoinverse (A+\mathbf{A}^+)

When a matrix is not square or is singular, we use the pseudoinverse for solving linear systems: A+=(ATA)βˆ’1AT\mathbf{A}^+ = (\mathbf{A}^T \mathbf{A})^{-1} \mathbf{A}^T ML Application: This is the mathematical foundation for finding the optimal weights in Ordinary Least Squares (OLS) linear regression.

7. Matrix Trace

The trace is the sum of the diagonal elements of a square matrix: tr(A)=βˆ‘Aii\text{tr}(\mathbf{A}) = \sum A_{ii}. It is invariant under cyclic permutations and basis changes.