Matrix Multiplication
Matrix Multiplication
Definition
Core Statement
Matrix Multiplication is a binary operation that takes a pair of matrices and produces another matrix. If
Purpose
- Linear Transformations: Rotating, scaling, or shearing space (Computer Graphics, Data Preprocessing).
- System of Equations: Representing
efficiently. - Neural Networks: The "Forward Pass" is essentially a series of matrix multiplications (Weights
Inputs). - Covariance Calculations:
constructs correlations.
The Rules
1. Dimension Compatibility
To multiply
- Columns of
Must Equal Rows of . - If inner dimensions don't match, multiplication is undefined.
2. The Dot Product Operation
For element
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:
(Quantities) is : (Prices) is :
Calculation (
- Person A:
. - Person B:
.
Result:
Person A spends $4, Person B spends $1.
Geometric Intuition
Matrix multiplication
- Transformation: Columns of
specify where the basis vectors ( ) allow the input vectors ( ) to land. - Composition: Doing transformation
followed by transformation .
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
- Non-Commutative: In general,
. Order matters! (Rotation then Shear Shear then Rotation). - Element-wise confusion: Matrix multiplication is NOT just multiplying corresponding elements (that is the Hadamard Product).
- Broadcasting: In Python/NumPy,
A * Boften does element-wise multiplication. You usually wantA @ Bornp.dot().
Related Concepts
- Eigenvalues & Eigenvectors - What vectors stay the same after multiplication?
- Principal Component Analysis (PCA) - Uses correlation matrix (
). - Multiple Linear Regression - Solution involves
.