Chandra
Math / Linear Algebra

Linear Algebra

1D Vector: A single value (a scalar).

2D Vector: A coordinate pair [x,y][x, y] used to position a pixel on a screen.

Ax=bAx = b

Left Hand Side

The Left Hand Side (Ax\mathbf{A}\mathbf{x}) represents the transformation. It’s the combination of your variables (x1,x2,x_1, x_2, \dots) and your coefficients. In geometry, this side describes the “span” or the space you are working in [4.1].

Right Hand Side

The Right Hand Side (b\mathbf{b}) represents the target. It is a fixed vector or constant. To find a solution, the target vector must land somewhere within the space described by the LHS [4.1].


Linear Combination

Scalar Multiplication

v=[23]\vec{v} = \begin{bmatrix} 2 \\ 3 \end{bmatrix} and a scalar c=2c = 2.

The scalar multiplication cvc\vec{v} would be:

2×[23]=[2×22×3]=[46]2 \times \begin{bmatrix} 2 \\ 3 \end{bmatrix} = \begin{bmatrix} 2 \times 2 \\ 2 \times 3 \end{bmatrix} = \begin{bmatrix} 4 \\ 6 \end{bmatrix}


Vector Addition

a=[46]\vec{a} = \begin{bmatrix} 4 \\ 6 \end{bmatrix} and b=[12]\vec{b} = \begin{bmatrix} 1 \\ -2 \end{bmatrix}

The vector addition a+b\vec{a} + \vec{b} would be:

[46]+[12]=[4+16+(2)]=[54]\begin{bmatrix} 4 \\ 6 \end{bmatrix} + \begin{bmatrix} 1 \\ -2 \end{bmatrix} = \begin{bmatrix} 4 + 1 \\ 6 + (-2) \end{bmatrix} = \begin{bmatrix} 5 \\ 4 \end{bmatrix}

Dot Product

dot product is a single number that reveals the relationship between two vectors.

A=[3,4]\vec{A} = [3, 4] and B=[1,2]\vec{B} = [1, 2]

(3×1)+(4×2)=3+8=11(3 \times 1) + (4 \times 2) = 3 + 8 = \mathbf{11}

The number 11. It is no longer a point on a graph; it is a “score.”

This tells you how intense or strong the data is. example: In music: It’s the Volume. A loud song has a long vector; a quiet song has a short vector.


Schwarz Inequality

vwvw|v \cdot w| \leq \|v\| \|w\|


the length is v=x2+y2\|\vec{v}\| = \sqrt{x^2 + y^2}

Length of A\vec{A} is 5 (because 32+42=5\sqrt{3^2 + 4^2} = 5). The Length of B\vec{B} is 2.23.

vw=5×2.23=11.15\|v\| \|w\| = 5 \times 2.23 = \mathbf{11.15}. (Maximum possible dot product you could get if two specific vectors were perfectly aligned)


Cosine Rule (The Angle)

Similarity=Actual Score (Dot Product)Potential Score (Product of Lengths)\text{Similarity} = \frac{\text{Actual Score (Dot Product)}}{\text{Potential Score (Product of Lengths)}}

11.011.150.986\frac{11.0}{11.15} \approx \mathbf{0.986}

If a “Loud” song and a “Quiet” song are both Heavy Metal, they will point in the same direction (small angle).

  • cos(θ)=0.986\cos(\theta) = 0.986.
  • cos1(0.986)\cos^{-1}(0.986) 9.6\approx 9.6^\circ
If Similarity (Cosine) is…The Angle (θ) is…What it means
1.00Perfectly identical direction.
0.70745°Halfway between same and different.
0.0090°Completely different (Perpendicular).

Unit Vector

we often want to keep the direction of a vector but change its length to 1. This is called a Unit Vector (u\mathbf{u}).

u=vv\mathbf{u} = \frac{\mathbf{v}}{\|\mathbf{v}\|}

A=[3,4]\vec{A} = [3, 4] and its length is 55.

u=[3/5,4/5]=[0.6,0.8]\mathbf{u} = [3/5, 4/5] = [0.6, 0.8].

This “normalizes” data so you can compare different vectors fairly, regardless of how big the numbers started.


Triangle Inequality

It states that the shortest distance between two points is a straight line.

v+wv+w\|v + w\| \leq \|v\| + \|w\|

Example

  • Home: (0,0)(0,0)
  • Grocery Store: Point (4,0)(4, 0)
  • Coffee Shop: Point (4,3)(4, 3)

First, you walk from Home to the Store, then from the Store to the Coffee Shop.

  1. Vector v\vec{v} (Home to Store): You move 4 units East.
    • v=[4,0]\vec{v} = [4, 0]
    • Length v=4\|v\| = 4
  2. Vector w\vec{w} (Store to Coffee): You move 3 units North.
    • w=[0,3]\vec{w} = [0, 3]
    • Length w=3\|w\| = 3

Total Distance of Path B: 4+3=74 + 3 = \mathbf{7}

If you had walked straight from Home to the Coffee Shop, you are looking for the length of the vector v+w\vec{v} + \vec{w}.

  • Combined Vector: [4+0,0+3]=[4,3][4+0, 0+3] = [4, 3]
  • Length v+w\|v+w\|: Using Pythagoras: 42+32=16+9=25=5\sqrt{4^2 + 3^2} = \sqrt{16 + 9} = \sqrt{25} = \mathbf{5}

Now we plug these numbers into the inequality:

v+wv+w\|v + w\| \leq \|v\| + \|w\|

54+3\mathbf{5} \leq \mathbf{4 + 3}

57\mathbf{5} \leq \mathbf{7}

The inequality is True. The straight line (55) is shorter than the two-step path (77).

If you then walk from the Coffee Shop back to Home, you walk another 55 units.

  • Total Trip with the Store stop: 4+3+5=124 + 3 + 5 = \mathbf{12} units.
  • Total Trip without the Store stop: 5+5=105 + 5 = \mathbf{10} units.

Matrix

If Matrix AA is (m×n)(m \times n) and Matrix BB is (n×p)(n \times p), the resulting Matrix CC will be (m×p)(m \times p).

A=[123456],B=[789101112]A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}, \quad B = \begin{bmatrix} 7 & 8 \\ 9 & 10 \\ 11 & 12 \end{bmatrix}

Top-Left Entry (Row 1 of A ×\times Column 1 of B)

(1×7)+(2×9)+(3×11)=7+18+33=58(1 \times 7) + (2 \times 9) + (3 \times 11) = 7 + 18 + 33 = \mathbf{58}

Top-Right Entry (Row 1 of A ×\times Column 2 of B):

(1×8)+(2×10)+(3×12)=8+20+36=64(1 \times 8) + (2 \times 10) + (3 \times 12) = 8 + 20 + 36 = \mathbf{64}

Bottom-Left Entry (Row 2 of A ×\times Column 1 of B):

(4×7)+(5×9)+(6×11)=28+45+66=139(4 \times 7) + (5 \times 9) + (6 \times 11) = 28 + 45 + 66 = \mathbf{139}

Bottom-Right Entry (Row 2 of A ×\times Column 2 of B):

(4×8)+(5×10)+(6×12)=32+50+72=154(4 \times 8) + (5 \times 10) + (6 \times 12) = 32 + 50 + 72 = \mathbf{154}

C=[5864139154]C = \begin{bmatrix} 58 & 64 \\ 139 & 154 \end{bmatrix}

Linear Equations

linear equations in matrix form, Ax=bAx = b

  • Matrix AA (Coefficient Matrix): Contains only the numbers (coefficients) in front of the variables.
  • Vector xx (Variable Vector): A column vector of the unknowns (e.g., x,y,zx, y, z).
  • Vector bb (Constant Vector): A column vector of the answers on the right side of the equals sign.

2x+3y=82x + 3y = 8

5xy=15x - y = 1

To write this in Ax=bAx = b form:

[2351][xy]=[81]\begin{bmatrix} 2 & 3 \\ 5 & -1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 8 \\ 1 \end{bmatrix}


Matrix Inner Product

is a way to take two matrices of the same size and produce a single scalar value. Any valid matrix inner product must satisfy four rules:

  • Symmetry: A,B=B,A\langle A, B \rangle = \langle B, A \rangle
  • Linearity: cA,B=cA,B\langle cA, B \rangle = c \langle A, B \rangle
  • Additivity: A+B,C=A,C+B,C\langle A + B, C \rangle = \langle A, C \rangle + \langle B, C \rangle
  • Positivity: A,A0\langle A, A \rangle \ge 0, and it only equals 0 if AA is a zero matrix.

The Frobenius Inner Product

For two real-valued matrices AA and BB of size m×nm \times n, the inner product (denoted as A,BF\langle A, B \rangle_F) is calculated by multiplying corresponding elements and summing them up.

There are three ways to write the same operation:

  1. Element-wise Sum: A,B=i,jAijBij\langle A, B \rangle = \sum_{i,j} A_{ij} B_{ij}
  2. Using Trace: A,B=tr(ATB)\langle A, B \rangle = \text{tr}(A^T B)
  3. If you “flatten” both matrices into long vectors, their inner product is exactly the same as the standard vector dot product.

Just like the vector dot product tells us about the “relationship” between two arrows in space, the matrix inner product tells us about the relationship between two data structure:

  • Measuring Similarity: It tells us how “aligned” two matrices are. If the inner product is high, the matrices are similar; if it’s zero, the matrices are orthogonal.

  • Defining “Length” (Norm): The inner product of a matrix with itself gives the square of its “size,” known as the Frobenius Norm:

    AF=A,A\|A\|_F = \sqrt{\langle A, A \rangle}

  • Projection: In machine learning and signal processing, we use inner products to “project” a data matrix onto a set of basis matrices (like in SVD or JPEG compression).

  • Optimization: Many loss functions in deep learning (like the cost of weights) are calculated using these types of inner products.

Suppose we have two 2×22 \times 2 matrices:

A=[1234],B=[5678]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \quad B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}

The inner product is:

A,B=(1×5)+(2×6)+(3×7)+(4×8)\langle A, B \rangle = (1 \times 5) + (2 \times 6) + (3 \times 7) + (4 \times 8)

A,B=5+12+21+32=70\langle A, B \rangle = 5 + 12 + 21 + 32 = \mathbf{70}

Law Of Operations

OperationLawFormula
MultiplicationNot CommutativeABBAAB \neq BA
MultiplicationAssociativeA(BC)=(AB)CA(BC) = (AB)C
TransposeProduct Law(AB)T=BTAT(AB)^T = B^T A^T
InverseProduct Law(AB)1=B1A1(AB)^{-1} = B^{-1} A^{-1}
ScalarDistributivec(A+B)=cA+cBc(A + B) = cA + cB

Identity Matrix

I=[100010001]I = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}

Permutation Matrix

A permutation matrix PP of size n×nn \times n has exactly one entry of 1 in each row and each column, with all other entries being 0.

For example, a 3×33 \times 3 permutation matrix might look like this:

P=[010100001]P = \begin{bmatrix} 0 & 1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{bmatrix}

Matrix Elimination

simplifying a complex fraction. make the matrix clean

Cost Of Elimination

PhaseOperation Count (Approx)Complexity
Forward Elimination23n3\frac{2}{3}n^3O(n3)O(n^3)
Back-Substitutionn2n^2O(n2)O(n^2)
Total Solve Cost23n3+n2\frac{2}{3}n^3 + n^2O(n3)O(n^3)

Augmented Matrix

Often, when solving these equations by hand (using a method like Gaussian Elimination), we use an Augmented Matrix.

[238511]\left[ \begin{array}{cc|c} 2 & 3 & 8 \\ 5 & -1 & 1 \end{array} \right]

To solve the equation we use

Gaussian Elimination

This method uses “row operations” to simplify the matrix into a form where we can read the answers.

We combine Matrix AA and Vector bb into one:

[1253411]\left[ \begin{array}{cc|c} 1 & 2 & 5 \\ 3 & 4 & 11 \end{array} \right]

We want to eliminate the 33 in the second row. We can do this by: Row 2 \to Row 2 (3×- (3 \times Row 1).

(33×1)=0(3 - 3 \times 1) = 0

(43×2)=2(4 - 3 \times 2) = -2

(113×5)=4(11 - 3 \times 5) = -4

New Matrix:

[125024]\left[ \begin{array}{cc|c} 1 & 2 & 5 \\ 0 & -2 & -4 \end{array} \right]

Back-Substitution

Now we turn the rows back into equations

  1. Bottom row: 2y=4    y=2-2y = -4 \implies \mathbf{y = 2}
  2. Top row: x+2y=5x + 2y = 5
  3. Substitute y: x+2(2)=5    x+4=5    x=1x + 2(2) = 5 \implies x + 4 = 5 \implies \mathbf{x = 1}

Solution: x=1,y=2x = 1, y = 2

Matrix Inverse

inverse matrix is the mathematical “undo” button. Think of a matrix AA as a function that transforms data (moving a character in a game, encrypting a message, or blurring an image), the inverse A1A^{-1} is the function that reverses that transformation exactly.

(x=A1bx = A^{-1}b)

When you multiply a matrix by its inverse, you get the Identity Matrix.

AA1=IA \cdot A^{-1} = I

This method is like basic algebra. If ax=bax = b, then x=b/ax = b/a. In matrices, we multiply by the inverse instead of dividing.

A=[1234],x=[xy],b=[511]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \quad x = \begin{bmatrix} x \\ y \end{bmatrix}, \quad b = \begin{bmatrix} 5 \\ 11 \end{bmatrix}

For a 2×22 \times 2 matrix [abcd]\begin{bmatrix} a & b \\ c & d \end{bmatrix}, the inverse is 1adbc[dbca]\frac{1}{ad-bc} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}.

Determinant: (1×4)(2×3)=46=2(1 \times 4) - (2 \times 3) = 4 - 6 = \mathbf{-2}

Swap and Negate: Swap 1 and 4, make 2 and 3 negative [4231]\to \begin{bmatrix} 4 & -2 \\ -3 & 1 \end{bmatrix}

Multiply by 1/Det: A1=12[4231]=[211.50.5]A^{-1} = -\frac{1}{2} \begin{bmatrix} 4 & -2 \\ -3 & 1 \end{bmatrix} = \begin{bmatrix} -2 & 1 \\ 1.5 & -0.5 \end{bmatrix}

[xy]=[211.50.5][511]=[(2×5)+(1×11)(1.5×5)+(0.5×11)]=[12]\begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} -2 & 1 \\ 1.5 & -0.5 \end{bmatrix} \begin{bmatrix} 5 \\ 11 \end{bmatrix} = \begin{bmatrix} (-2 \times 5) + (1 \times 11) \\ (1.5 \times 5) + (-0.5 \times 11) \end{bmatrix} = \mathbf{\begin{bmatrix} 1 \\ 2 \end{bmatrix}}

Solution: x=1,y=2x = 1, y = 2


Factorization

LU Decomposition (A=LUA = LU)

This is the matrix version of Gaussian Elimination. L stands for Lower Triangular, and U stands for Upper Triangular.

A=[23815]A = \begin{bmatrix} 2 & 3 \\ 8 & 15 \end{bmatrix}

Identify the Pivot. The first pivot is 22 (at position Row 1, Column 1).

Eliminate the value below the pivot.

We need to turn that 88 into a 00. We do this by subtracting a multiple of Row 1 from Row 2.

  • Multiplier (l21l_{21}): 8/2=48 / 2 = 4.
  • Operation: R2R2(4×R1)R_2 \to R_2 - (4 \times R_1)

U=[238(4×2)15(4×3)]=[2303]U = \begin{bmatrix} 2 & 3 \\ 8 - (4 \times 2) & 15 - (4 \times 3) \end{bmatrix} = \begin{bmatrix} 2 & 3 \\ 0 & 3 \end{bmatrix}

The LL matrix is the “memory” of the elimination. Its job is to store the multipliers you used.

  1. Start with the Identity: LL always starts as an Identity Matrix (1s on the diagonal, 0s elsewhere).

    [10?1]\begin{bmatrix} 1 & 0 \\ ? & 1 \end{bmatrix}

  2. Insert the Multiplier: In Phase 1, we used the multiplier 4 to eliminate the value in Row 2, Column 1. We place that 4 in the exact same spot in LL.

    L=[1041]L = \begin{bmatrix} 1 & 0 \\ \mathbf{4} & 1 \end{bmatrix}

Why is it 0 in the top right? Because we never use Row 2 to eliminate Row 1. We only work “downward,” so only the “lower” half of the matrix gets values

Instead of solving Ax=bAx = b, which is slow, we solve two tiny, easy problems.

Imagine b=[731]b = \begin{bmatrix} 7 \\ 31 \end{bmatrix}. We want to find xx in LUx=bLUx = b.

Step 1: Solve Ly=bLy = b (Forward Substitution)

[1041][y1y2]=[731]\begin{bmatrix} 1 & 0 \\ 4 & 1 \end{bmatrix} \begin{bmatrix} y_1 \\ y_2 \end{bmatrix} = \begin{bmatrix} 7 \\ 31 \end{bmatrix}

  • Row 1: 1y1=7    y1=71y_1 = 7 \implies \mathbf{y_1 = 7}
  • Row 2: 4y1+1y2=31    4(7)+y2=31    28+y2=31    y2=34y_1 + 1y_2 = 31 \implies 4(7) + y_2 = 31 \implies 28 + y_2 = 31 \implies \mathbf{y_2 = 3}

Step 2: Solve Ux=yUx = y (Back Substitution)

[2303][x1x2]=[73]\begin{bmatrix} 2 & 3 \\ 0 & 3 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} = \begin{bmatrix} 7 \\ 3 \end{bmatrix}

  • Row 2: 3x2=3    x2=13x_2 = 3 \implies \mathbf{x_2 = 1}
  • Row 1: 2x1+3x2=7    2x1+3(1)=7    2x1=4    x1=22x_1 + 3x_2 = 7 \implies 2x_1 + 3(1) = 7 \implies 2x_1 = 4 \implies \mathbf{x_1 = 2}

Final Answer: x=[21]x = \begin{bmatrix} 2 \\ 1 \end{bmatrix}.


QR Decomposition (A=QRA = QR)

This breaks a matrix into an Orthogonal matrix (QQ) and an Upper Triangular matrix (RR). Computers love orthogonal matrices because they don’t lose precision during calculations. This is the standard way to solve Least Squares problems (finding the best-fit line in Data Science).

A=[1110]A = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}

  1. Find QQ: We use the Gram-Schmidt process to make the columns of AA orthonormal.

    After normalizing, we get Q=[1/21/21/21/2]Q = \begin{bmatrix} 1/\sqrt{2} & 1/\sqrt{2} \\ 1/\sqrt{2} & -1/\sqrt{2} \end{bmatrix}

  2. Find RR: RR is calculated as QTAQ^T A.

    R=[21/201/2]R = \begin{bmatrix} \sqrt{2} & 1/\sqrt{2} \\ 0 & 1/\sqrt{2} \end{bmatrix}


Rank, Nullspace, and Columnspace

Nullspace

The Nullspace is the “Garbage Can” of a matrix. It consists of all the input vectors that the matrix “squashes” into zero.

Ax=0Ax = 0

If xx is in the nullspace, multiplying it by AA completely destroys its information, turning it into a vector of zeros

If the Nullspace is empty (only contains the zero vector): The matrix is “perfect.” Every unique input gives a unique output. You can reverse the process (Invert the matrix).

If the Nullspace has “stuff” in it: You have a problem. Multiple different inputs can produce the same output.

Matrix Rank

rank of a matrix is a single number that tells you how much “real” information is inside that matrix.

  • Linearly Independent: This means a row is “unique.” You cannot create it by adding or scaling the other rows.
  • Linearly Dependent: This means a row is a “copycat.” For example, if Row 2 is just Row 1 multiplied by 10, Row 2 is dependent and doesn’t count toward the rank.

Look at this matrix:

A=[1224]A = \begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix}

  • Row 1: [1,2][1, 2]
  • Row 2: [2,4][2, 4]

Notice that Row 2 is just 2×2 \times Row 1. It adds no new information to the system. Because there is only one unique row, the Rank = 1.

Span, Subspace, Columnspace — A Unified Example

We will use 3D vectors where the numbers represent the amount of Cyan, Magenta, and Yellow (C,M,YC, M, Y).

A=[100011000]A = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 1 \\ 0 & 0 & 0 \end{bmatrix}

Row 1: Cyan Slot

Row 2: Yellow Slot

Row 3: Magenta Slot

The Span & Column Space

Col 1 (Cyan): [100]\begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix}

Col 2 (Yellow A): [010]\begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix}

Col 3 (Yellow B): [010]\begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix} (Oops! A duplicate of Col 2)

The Column Space is the Span of these three vectors. It represents every color this printer can make. Since we only have Cyan and Yellow ingredients, the span is “Every mix of Cyan and Yellow.”

The Vector Subspace (The “Result”)

Because we have no Magenta ingredient (the middle row of our result will always be 0 for the first and third components in a different setup, or specifically here, the third row is always 0), we are stuck in a 2D Subspace.

We can make [110]\begin{bmatrix} 1 \\ 1 \\ 0 \end{bmatrix} (Green).

We can never make [001]\begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix} (Pure Magenta) because no combination of our columns can put a number in that bottom slot.

The Rank (The “Unique Info”)

Look at the columns again.

  • Column 1 is unique.
  • Column 2 is unique.
  • Column 3 is just a copy of Column 2.

Since there are only 2 independent directions, the Rank = 2. Even though we have 3 cartridges, the printer only “sees” 2 dimensions of color.

The Nullspace (The “Waste”)

The Nullspace is the set of instructions (x1,x2,x3)(x_1, x_2, x_3) that tells the printer to use ink, but results in zero color on the page (Ax=0Ax = 0).

Since Column 2 and Column 3 are identical, watch what happens if we use this instruction:

x=[011]x = \begin{bmatrix} 0 \\ 1 \\ -1 \end{bmatrix}

This tells the printer: “Use 0 Cyan, 1 unit of Yellow A, and -1 unit of Yellow B.”

1[010]1[010]=[000]1\begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix} - 1\begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ 0 \end{bmatrix}

The ink cancels out! This vector [011]\begin{bmatrix} 0 \\ 1 \\ -1 \end{bmatrix} is in the Nullspace. It represents a redundant command.


Singular Value Decomposition (A=UΣVTA = U\Sigma V^T)

SVD is a way of breaking down a complex table of data (a matrix) into its most basic, essential building blocks.

  • UU (The “Who/What”): It identifies the categories or “latent features” in your data. (e.g., In a movie database, it might find categories like “Sci-Fi fans” or “Rom-Com fans”).
  • Σ\Sigma (The “How Much”): It tells you the importance of each category. The first value is always the biggest “story” in your data; the last values are usually just random noise.
  • VTV^T (The “Connection”): It shows how the original items in your data relate to those new categories. (e.g., Which specific movies belong to the “Sci-Fi” category).

main purpose is to squint data, see hidden pattern, denoising