Matrix Multiplication

Matrix Multiplication

Definition

Core Statement

Matrix Multiplication is a binary operation that takes a pair of matrices and produces another matrix. If A is an m×n matrix and B is an n×p matrix, their product C=AB is an m×p matrix where each element cij is the dot product of the i-th row of A and the j-th column of B.


Purpose

  1. Linear Transformations: Rotating, scaling, or shearing space (Computer Graphics, Data Preprocessing).
  2. System of Equations: Representing Ax=b efficiently.
  3. Neural Networks: The "Forward Pass" is essentially a series of matrix multiplications (Weights × Inputs).
  4. Covariance Calculations: XTX constructs correlations.

The Rules

1. Dimension Compatibility

To multiply A×B:

2. The Dot Product Operation

For element cij:

cij=Rowi(A)Colj(B)=k=1naikbkj

Worked Example: Shopping Bill

Problem

Prices (Matrix P): Apple=$1, Banana=$0.5.
Quantities (Matrix Q):

  • Person A: 2 Apples, 4 Bananas.
  • Person B: 1 Apple, 0 Bananas.

Calculate Total Cost for each.

Setup:

Calculation (Q×P):

Result:

[41]

Person A spends $4, Person B spends $1.


Geometric Intuition

Matrix multiplication AB can be seen as:

  1. Transformation: Columns of A specify where the basis vectors (i^,j^) allow the input vectors (B) to land.
  2. Composition: Doing transformation B followed by transformation A.

Python Implementation

import numpy as np

# Define A (2x3)
A = np.array([[1, 2, 3],
              [4, 5, 6]])

# Define B (3x2)
B = np.array([[7, 8],
              [9, 1],
              [2, 3]])

# Multiply
C = np.dot(A, B)
# OR ideal modern syntax:
C_modern = A @ B

print(f"Result (2x2):\n{C_modern}")

Limitations & Pitfalls

Pitfalls

  1. Non-Commutative: In general, ABBA. Order matters! (Rotation then Shear Shear then Rotation).
  2. Element-wise confusion: Matrix multiplication is NOT just multiplying corresponding elements (that is the Hadamard Product).
  3. Broadcasting: In Python/NumPy, A * B often does element-wise multiplication. You usually want A @ B or np.dot().