Matrix Operations
Learn fundamental matrix operations: addition, multiplication, and scalar multiplication. Understand how matrices work and their essential properties.
What are Matrices?
A matrix is a rectangular array of numbers arranged in rows and columns. Matrices are fundamental in linear algebra and used extensively in computer graphics, machine learning, physics, and engineering.
Example Matrix (2×2):
This is a 2×2 matrix (2 rows, 2 columns)
Interactive Operations
Experiment with matrix addition, multiplication, and scalar multiplication. Change the values and operation to see results in real-time.
Calculation Steps:
Matrix addition: Add corresponding elements
Understanding the Operations
Matrix Addition
Add corresponding elements element-wise. Both matrices must have the same dimensions. Simple and intuitive: A + B means add each element in A to the corresponding element in B.
Matrix Multiplication
More complex: multiply rows of first matrix by columns of second matrix using dot product. For A×B, the number of columns in A must equal the number of rows in B.
Important: Matrix multiplication is NOT commutative! A×B ≠ B×A in general.
Scalar Multiplication
Multiply every element in the matrix by a single number (scalar). This scales the entire matrix uniformly.
Implementation
import numpy as np
# Matrix Operations in Linear Algebra
# Define matrices
A = np.array([
[2, 1],
[3, 4]
])
B = np.array([
[1, 2],
[2, 1]
])
print("Matrix A:")
print(A)
print("\nMatrix B:")
print(B)
# ==========================================
# 1. MATRIX ADDITION
# ==========================================
# Add corresponding elements: C[i,j] = A[i,j] + B[i,j]
C_add = A + B
print("\n=== Matrix Addition: A + B ===")
print(C_add)
# Manual calculation
C_manual = np.zeros_like(A)
for i in range(A.shape[0]):
for j in range(A.shape[1]):
C_manual[i, j] = A[i, j] + B[i, j]
print("Manual calculation:", C_manual)
# ==========================================
# 2. MATRIX MULTIPLICATION
# ==========================================
# Row × Column dot product
C_mult = A @ B # or np.dot(A, B) or A.dot(B)
print("\n=== Matrix Multiplication: A × B ===")
print(C_mult)
# Manual calculation (for 2x2)
C_manual_mult = np.array([
[A[0,0]*B[0,0] + A[0,1]*B[1,0], A[0,0]*B[0,1] + A[0,1]*B[1,1]],
[A[1,0]*B[0,0] + A[1,1]*B[1,0], A[1,0]*B[0,1] + A[1,1]*B[1,1]]
])
print("Manual calculation:")
print(C_manual_mult)
# Note: Matrix multiplication is NOT commutative
print("\nA × B:")
print(A @ B)
print("B × A:")
print(B @ A)
print("A × B == B × A?", np.array_equal(A @ B, B @ A))
# ==========================================
# 3. SCALAR MULTIPLICATION
# ==========================================
# Multiply each element by scalar
k = 2
C_scalar = k * A
print(f"\n=== Scalar Multiplication: {k} × A ===")
print(C_scalar)
# ==========================================
# 4. MATRIX PROPERTIES
# ==========================================
# Identity matrix
I = np.eye(2)
print("\n=== Identity Matrix ===")
print(I)
print("A × I =")
print(A @ I)
print("I × A =")
print(I @ A)
# Zero matrix
Z = np.zeros((2, 2))
print("\n=== Zero Matrix ===")
print("A + 0 =")
print(A + Z)
# Transpose
print("\n=== Transpose ===")
print("A^T =")
print(A.T)
# Determinant (2x2: ad - bc)
det_A = np.linalg.det(A)
det_B = np.linalg.det(B)
print(f"\ndet(A) = {det_A}")
print(f"det(B) = {det_B}")
print(f"det(A × B) = {np.linalg.det(A @ B)}")
print(f"det(A) × det(B) = {det_A * det_B}")Real-World Applications
Computer Graphics
3D transformations (rotation, scaling, translation) use matrix multiplication
Machine Learning
Neural networks use matrix operations for forward/backward propagation
Image Processing
Images are matrices; filters and transformations use matrix operations
Systems of Equations
Linear systems Ax = b solved using matrix operations
Related Topics
DeterminantsComing Soon
Calculate matrix determinants and their properties
Matrix InverseComing Soon
Finding inverse matrices and solving equations
Eigenvalues & EigenvectorsComing Soon
Understanding matrix transformations
Linear TransformationsComing Soon
Visualize how matrices transform space