-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinear-algebra.html
More file actions
1353 lines (1170 loc) · 64.7 KB
/
linear-algebra.html
File metadata and controls
1353 lines (1170 loc) · 64.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Linear Algebra - Math from Zero to CS - Better Dev</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header class="topbar">
<button class="sidebar-toggle" aria-label="Open navigation" aria-expanded="false">
<span class="hamburger-icon"></span>
</button>
<a href="index.html" class="logo">Better Dev</a>
</header>
<div class="sidebar-backdrop" aria-hidden="true"></div>
<aside class="sidebar" aria-label="Site navigation">
<div class="sidebar-header">
<span class="sidebar-title">Navigation</span>
<button class="sidebar-close" aria-label="Close navigation">×</button>
</div>
<div class="sidebar-search">
<input type="text" class="sidebar-search-input" placeholder="Search topics..." aria-label="Search topics">
<div class="sidebar-search-results"></div>
</div>
<nav class="sidebar-nav">
<div class="sidebar-group">
<a href="index.html">Home</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Mathematics</div>
<a href="pre-algebra.html">Pre-Algebra</a>
<a href="algebra.html">Algebra</a>
<a href="sequences-series.html">Sequences & Series</a>
<a href="geometry.html">Geometry</a>
<a href="calculus.html">Calculus</a>
<a href="discrete-math.html">Discrete Math</a>
<a href="linear-algebra.html">Linear Algebra</a>
<a href="probability.html">Probability & Statistics</a>
<a href="binary-systems.html">Binary & Number Systems</a>
<a href="number-theory.html">Number Theory for CP</a>
<a href="computational-geometry.html">Computational Geometry</a>
<a href="game-theory.html">Game Theory</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Data Structures & Algorithms</div>
<a href="dsa-foundations.html">DSA Foundations</a>
<a href="arrays.html">Arrays & Strings</a>
<a href="stacks-queues.html">Stacks & Queues</a>
<a href="hashmaps.html">Hash Maps & Sets</a>
<a href="linked-lists.html">Linked Lists</a>
<a href="trees.html">Trees & BST</a>
<a href="graphs.html">Graphs</a>
<a href="sorting.html">Sorting & Searching</a>
<a href="patterns.html">LeetCode Patterns</a>
<a href="dp.html">Dynamic Programming</a>
<a href="advanced.html">Advanced Topics</a>
<a href="string-algorithms.html">String Algorithms</a>
<a href="advanced-graphs.html">Advanced Graphs</a>
<a href="advanced-dp.html">Advanced DP</a>
<a href="advanced-ds.html">Advanced Data Structures</a>
<a href="leetcode-650.html">The 650 Problems</a>
<a href="competitive-programming.html">CP Roadmap</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Languages & Systems</div>
<a href="cpp.html">C++</a>
<a href="golang.html">Go</a>
<a href="javascript.html">JavaScript Deep Dive</a>
<a href="typescript.html">TypeScript</a>
<a href="nodejs.html">Node.js Internals</a>
<a href="os.html">Operating Systems</a>
<a href="linux.html">Linux</a>
<a href="git.html">Git</a>
<a href="backend.html">Backend</a>
<a href="system-design.html">System Design</a>
<a href="networking.html">Networking</a>
<a href="cloud.html">Cloud & Infrastructure</a>
<a href="docker.html">Docker & Compose</a>
<a href="kubernetes.html">Kubernetes</a>
<a href="message-queues.html">Queues & Pub/Sub</a>
<a href="selfhosting.html">VPS & Self-Hosting</a>
<a href="databases.html">PostgreSQL & MySQL</a>
<a href="stripe.html">Stripe & Payments</a>
<a href="distributed-systems.html">Distributed Systems</a>
<a href="backend-engineering.html">Backend Engineering</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">JS/TS Ecosystem</div>
<a href="js-tooling.html">Tooling & Bundlers</a>
<a href="js-testing.html">Testing</a>
<a href="ts-projects.html">Building with TS</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">More</div>
<a href="seans-brain.html">Sean's Brain</a>
</div>
</nav>
</aside>
<div class="container">
<!-- Page Header -->
<div class="page-header">
<div class="breadcrumb"><a href="index.html">Home</a> › Linear Algebra</div>
<h1>Linear Algebra</h1>
<p>Vectors, matrices, and transformations -- the math that powers everything from 3D graphics to neural networks.</p>
</div>
<!-- Table of Contents -->
<div class="toc">
<h4>Table of Contents</h4>
<a href="#what-is-linear-algebra">1. What is Linear Algebra?</a>
<a href="#vectors">2. Vectors</a>
<a href="#matrices">3. Matrices</a>
<a href="#matrix-operations-cs">4. Matrix Operations for CS</a>
<a href="#determinant">5. Determinant</a>
<a href="#inverse-matrix">6. Inverse Matrix</a>
<a href="#eigenvalues-eigenvectors">7. Eigenvalues and Eigenvectors</a>
<a href="#linear-transformations">8. Linear Transformations</a>
<a href="#cs-applications">9. Practical CS Applications</a>
<a href="#practice-quiz">10. Practice Quiz</a>
</div>
<!-- =============================== -->
<!-- SECTION 1: What is Linear Algebra? -->
<!-- =============================== -->
<section id="what-is-linear-algebra">
<h2>1. What is Linear Algebra?</h2>
<p>
Linear algebra is the branch of mathematics that deals with <strong>vectors</strong> (ordered lists of numbers), <strong>matrices</strong> (grids of numbers), and <strong>linear transformations</strong> (functions that move, stretch, and rotate space). If regular algebra is about solving for one unknown number, linear algebra is about solving for many unknowns at once -- and understanding the structure of the space they live in.
</p>
<p>
Think of it this way: a single variable <code>x</code> is just a number on a number line. But in the real world, data almost never comes as a single number. An image is a grid of pixel values. A point in 3D space has three coordinates. A neural network has millions of parameters. Linear algebra gives you the tools to work with all of that at once, efficiently and elegantly.
</p>
<div class="tip-box">
<div class="label">Think of it like this</div>
<p>Regular algebra deals with single numbers. Linear algebra deals with <strong>lists and grids of numbers</strong>. That's it at its core. A vector is a list, a matrix is a grid, and the operations tell you how to combine and transform them.</p>
</div>
<h3>Why CS Needs Linear Algebra</h3>
<table>
<thead>
<tr>
<th>CS Field</th>
<th>How Linear Algebra is Used</th>
</tr>
</thead>
<tbody>
<tr>
<td>Machine Learning / AI</td>
<td>Neural networks are built from matrix multiplications. Training involves gradient vectors. Data lives in high-dimensional vector spaces.</td>
</tr>
<tr>
<td>Computer Graphics</td>
<td>Every rotation, scaling, and projection in 3D graphics is a matrix operation. GPUs are literally designed for matrix math.</td>
</tr>
<tr>
<td>Game Development</td>
<td>Character positions are vectors. Camera angles, physics simulations, and collision detection all use linear algebra.</td>
</tr>
<tr>
<td>Data Science</td>
<td>Datasets are matrices. PCA (dimensionality reduction) uses eigenvectors. Recommendation systems use matrix factorization.</td>
</tr>
<tr>
<td>Image Processing</td>
<td>Images are matrices of pixels. Filters (blur, sharpen, edge detect) are matrix operations called convolutions.</td>
</tr>
<tr>
<td>Cryptography</td>
<td>Many encryption schemes (like Hill cipher) rely on matrix operations and modular arithmetic.</td>
</tr>
</tbody>
</table>
<div class="warning-box">
<div class="label">Warning</div>
<p>Linear algebra has a reputation for being abstract and hard. That reputation comes from how it's traditionally taught -- theorem-proof style with no applications. On this page we focus on <strong>what things mean</strong> and <strong>how they're used in code</strong>. You will understand this.</p>
</div>
</section>
<!-- =============================== -->
<!-- SECTION 2: Vectors -->
<!-- =============================== -->
<section id="vectors">
<h2>2. Vectors</h2>
<p>
A <strong>vector</strong> is an ordered list of numbers. That's the whole definition. In 2D, a vector has two components. In 3D, three. In machine learning, vectors can have thousands or millions of components.
</p>
<div class="formula-box">
v = [v<sub>1</sub>, v<sub>2</sub>, v<sub>3</sub>] (a 3D vector)<br><br>
Examples:<br>
Position in 2D: p = [3, 5]<br>
RGB color: c = [255, 128, 0]<br>
Word embedding: w = [0.2, -0.5, 0.8, 0.1, ...]
</div>
<p>
Geometrically, you can think of a 2D or 3D vector as an arrow pointing from the origin to a point. The vector [3, 2] points 3 units right and 2 units up. But vectors don't have to be spatial -- they can represent anything: colors, audio samples, user preferences, word meanings.
</p>
<h3>Vector Addition and Subtraction</h3>
<p>
To add or subtract vectors, you just add or subtract their corresponding components. Both vectors must have the same number of components.
</p>
<div class="formula-box">
a + b = [a<sub>1</sub> + b<sub>1</sub>, a<sub>2</sub> + b<sub>2</sub>, a<sub>3</sub> + b<sub>3</sub>]<br>
a - b = [a<sub>1</sub> - b<sub>1</sub>, a<sub>2</sub> - b<sub>2</sub>, a<sub>3</sub> - b<sub>3</sub>]
</div>
<div class="example-box">
<div class="label">Example: Vector Addition</div>
<p><strong>Problem:</strong> Add vectors a = [2, 5, -1] and b = [3, -2, 4]</p>
<p>
a + b = [2+3, 5+(-2), -1+4]<br>
a + b = [5, 3, 3]
</p>
<p><strong>CS context:</strong> If a player moves [2, 5, -1] then [3, -2, 4], their total displacement is [5, 3, 3].</p>
</div>
<h3>Scalar Multiplication</h3>
<p>
A <strong>scalar</strong> is just a regular number (as opposed to a vector). Multiplying a vector by a scalar multiplies every component by that number.
</p>
<div class="formula-box">
k * v = [k * v<sub>1</sub>, k * v<sub>2</sub>, k * v<sub>3</sub>]
</div>
<div class="example-box">
<div class="label">Example: Scalar Multiplication</div>
<p><strong>Problem:</strong> Multiply v = [4, -2, 6] by scalar 3</p>
<p>
3 * v = [3*4, 3*(-2), 3*6]<br>
3 * v = [12, -6, 18]
</p>
<p><strong>CS context:</strong> Scaling a velocity vector by 3 makes the object move 3 times faster in the same direction.</p>
</div>
<h3>Magnitude (Length)</h3>
<p>
The <strong>magnitude</strong> (or length or norm) of a vector tells you how long it is. It's the distance from the origin to the point the vector represents.
</p>
<div class="formula-box">
||v|| = √(v<sub>1</sub>² + v<sub>2</sub>² + v<sub>3</sub>² + ...)
</div>
<div class="example-box">
<div class="label">Example: Vector Magnitude</div>
<p><strong>Problem:</strong> Find the magnitude of v = [3, 4]</p>
<p>
||v|| = √(3² + 4²)<br>
||v|| = √(9 + 16)<br>
||v|| = √25<br>
||v|| = 5
</p>
<p><strong>CS context:</strong> If v represents a velocity, then ||v|| = 5 is the speed (how fast, ignoring direction).</p>
</div>
<h3>Unit Vectors</h3>
<p>
A <strong>unit vector</strong> is a vector with magnitude 1. It represents a pure direction with no scaling. To make any vector into a unit vector, divide it by its magnitude.
</p>
<div class="formula-box">
v̂ = v / ||v||
</div>
<div class="example-box">
<div class="label">Example: Unit Vector</div>
<p><strong>Problem:</strong> Find the unit vector of v = [3, 4]</p>
<p>
||v|| = 5 (from previous example)<br>
v̂ = [3/5, 4/5] = [0.6, 0.8]
</p>
<p>Check: √(0.6² + 0.8²) = √(0.36 + 0.64) = √1 = 1</p>
</div>
<div class="tip-box">
<div class="label">CS Tip</div>
<p>Unit vectors come up constantly in game dev and graphics. When you want to move a character "toward the enemy" at a fixed speed, you compute the direction vector (enemy_pos - player_pos), normalize it to a unit vector, then multiply by the speed. Direction times speed equals velocity.</p>
</div>
<h3>Dot Product</h3>
<p>
The <strong>dot product</strong> (or scalar product) takes two vectors and returns a <strong>single number</strong>. You multiply corresponding components and add them up.
</p>
<div class="formula-box">
a · b = a<sub>1</sub>b<sub>1</sub> + a<sub>2</sub>b<sub>2</sub> + a<sub>3</sub>b<sub>3</sub> + ...
</div>
<div class="example-box">
<div class="label">Example: Dot Product</div>
<p><strong>Problem:</strong> Find the dot product of a = [2, 3, -1] and b = [4, -1, 5]</p>
<p>
a · b = (2)(4) + (3)(-1) + (-1)(5)<br>
a · b = 8 + (-3) + (-5)<br>
a · b = 0
</p>
<p><strong>Note:</strong> When the dot product is 0, the vectors are <strong>perpendicular</strong> (orthogonal).</p>
</div>
<h3>Geometric Meaning of Dot Product</h3>
<p>
The dot product tells you how much two vectors point in the same direction. It connects to the angle between them through this formula:
</p>
<div class="formula-box">
cos(θ) = (a · b) / (||a|| * ||b||)
</div>
<ul>
<li>If dot product > 0: vectors point roughly the <strong>same direction</strong> (angle < 90 degrees)</li>
<li>If dot product = 0: vectors are <strong>perpendicular</strong> (angle = 90 degrees)</li>
<li>If dot product < 0: vectors point in roughly <strong>opposite directions</strong> (angle > 90 degrees)</li>
</ul>
<div class="example-box">
<div class="label">Example: Angle Between Vectors</div>
<p><strong>Problem:</strong> Find the angle between a = [1, 0] and b = [1, 1]</p>
<p>
a · b = (1)(1) + (0)(1) = 1<br>
||a|| = √(1 + 0) = 1<br>
||b|| = √(1 + 1) = √2<br>
cos(θ) = 1 / (1 * √2) = 1/√2<br>
θ = 45 degrees
</p>
</div>
<div class="tip-box">
<div class="label">CS Tip</div>
<p>The dot product is everywhere in CS. In <strong>lighting calculations</strong>, you dot the surface normal with the light direction to figure out how bright a surface is. In <strong>recommendation engines</strong>, you dot a user preference vector with a product feature vector to predict how much they'll like it. In <strong>NLP</strong>, cosine similarity (which uses dot product) measures how similar two word embeddings are.</p>
</div>
<h3>Cross Product (3D Only)</h3>
<p>
The <strong>cross product</strong> takes two 3D vectors and returns a <strong>new vector</strong> that is perpendicular to both inputs. It's mainly used in 3D graphics for computing surface normals.
</p>
<div class="formula-box">
a × b = [<br>
a<sub>2</sub>b<sub>3</sub> - a<sub>3</sub>b<sub>2</sub>,<br>
a<sub>3</sub>b<sub>1</sub> - a<sub>1</sub>b<sub>3</sub>,<br>
a<sub>1</sub>b<sub>2</sub> - a<sub>2</sub>b<sub>1</sub><br>
]
</div>
<div class="example-box">
<div class="label">Example: Cross Product</div>
<p><strong>Problem:</strong> Find the cross product of a = [1, 0, 0] and b = [0, 1, 0]</p>
<p>
a × b = [(0)(0) - (0)(1), (0)(0) - (1)(0), (1)(1) - (0)(0)]<br>
a × b = [0, 0, 1]
</p>
<p><strong>CS context:</strong> The x-axis crossed with the y-axis gives the z-axis. This is how 3D coordinate systems are defined.</p>
</div>
<div class="warning-box">
<div class="label">Warning</div>
<p>The cross product is <strong>not commutative</strong>: a × b = -(b × a). The order matters -- it flips the direction of the result. Also, the cross product only works in 3D.</p>
</div>
</section>
<!-- =============================== -->
<!-- SECTION 3: Matrices -->
<!-- =============================== -->
<section id="matrices">
<h2>3. Matrices</h2>
<p>
A <strong>matrix</strong> is a rectangular grid of numbers arranged in rows and columns. Just as a vector is a list, a matrix is a table. We describe a matrix by its dimensions: an <strong>m × n</strong> matrix has <strong>m rows</strong> and <strong>n columns</strong>.
</p>
<div class="formula-box">
A (2×3 matrix) =<br><br>
| 1 2 3 |<br>
| 4 5 6 |<br><br>
2 rows, 3 columns
</div>
<p>
Matrices show up everywhere in CS: an image is a matrix of pixel values, a spreadsheet is a matrix, a neural network layer is defined by a weight matrix, and a 3D transformation is a 4×4 matrix.
</p>
<h3>Matrix Addition and Subtraction</h3>
<p>
Just like vectors, you add/subtract matrices element by element. Both matrices must have the <strong>same dimensions</strong>.
</p>
<div class="example-box">
<div class="label">Example: Matrix Addition</div>
<p><strong>Problem:</strong> Add matrices A and B</p>
<p>
A = | 1 2 | B = | 5 6 |<br>
| 3 4 | | 7 8 |<br><br>
A + B = | 1+5 2+6 | = | 6 8 |<br>
| 3+7 4+8 | | 10 12 |
</p>
</div>
<h3>Scalar Multiplication</h3>
<p>
Multiply every element in the matrix by the scalar.
</p>
<div class="example-box">
<div class="label">Example: Scalar Multiplication</div>
<p>
3 * | 1 2 | = | 3 6 |<br>
| 4 5 | | 12 15 |
</p>
</div>
<h3>Matrix Multiplication</h3>
<p>
This is the big one. Matrix multiplication is <strong>not</strong> element-wise. It uses a "row times column" pattern. Each entry in the result is the <strong>dot product</strong> of a row from the first matrix with a column from the second matrix.
</p>
<div class="formula-box">
If A is m×n and B is n×p, then AB is m×p<br><br>
(AB)<sub>ij</sub> = Row i of A · Column j of B<br><br>
Key requirement: number of columns in A must equal number of rows in B
</div>
<div class="warning-box">
<div class="label">Critical Rule</div>
<p>Matrix multiplication requires the <strong>inner dimensions to match</strong>. A (2×<strong>3</strong>) matrix can multiply a (<strong>3</strong>×4) matrix, giving a (2×4) result. But a (2×3) CANNOT multiply a (2×4) because 3 does not equal 2.</p>
</div>
<div class="example-box">
<div class="label">Example: Matrix Multiplication (Step by Step)</div>
<p><strong>Problem:</strong> Multiply A (2×3) by B (3×2)</p>
<p>
A = | 1 2 3 | B = | 7 8 |<br>
| 4 5 6 | | 9 10 |<br>
| 11 12 |<br><br>
Result will be 2×2.<br><br>
<strong>Position (1,1):</strong> Row 1 of A · Col 1 of B<br>
= (1)(7) + (2)(9) + (3)(11) = 7 + 18 + 33 = <strong>58</strong><br><br>
<strong>Position (1,2):</strong> Row 1 of A · Col 2 of B<br>
= (1)(8) + (2)(10) + (3)(12) = 8 + 20 + 36 = <strong>64</strong><br><br>
<strong>Position (2,1):</strong> Row 2 of A · Col 1 of B<br>
= (4)(7) + (5)(9) + (6)(11) = 28 + 45 + 66 = <strong>139</strong><br><br>
<strong>Position (2,2):</strong> Row 2 of A · Col 2 of B<br>
= (4)(8) + (5)(10) + (6)(12) = 32 + 50 + 72 = <strong>154</strong><br><br>
AB = | 58 64 |<br>
| 139 154 |
</p>
</div>
<div class="warning-box">
<div class="label">Warning: Not Commutative</div>
<p>Matrix multiplication is <strong>NOT commutative</strong>: AB does not equal BA. In fact, even if AB is defined, BA might not be (because the dimensions might not match the other way). This matters in graphics -- applying rotation then translation gives a different result than translation then rotation.</p>
</div>
<h3>Identity Matrix</h3>
<p>
The <strong>identity matrix</strong> (I) is the matrix equivalent of the number 1. It's a square matrix with 1s on the diagonal and 0s everywhere else. Multiplying any matrix by I leaves it unchanged.
</p>
<div class="formula-box">
I (3×3) =<br><br>
| 1 0 0 |<br>
| 0 1 0 |<br>
| 0 0 1 |<br><br>
A * I = A and I * A = A
</div>
<div class="tip-box">
<div class="label">CS Tip</div>
<p>In 3D graphics, the identity matrix represents "no transformation." When you reset a model's transform, you set it to the identity matrix. Every transformation starts from identity.</p>
</div>
<h3>Transpose</h3>
<p>
The <strong>transpose</strong> of a matrix (written A<sup>T</sup>) flips rows and columns. Row 1 becomes column 1, row 2 becomes column 2, and so on. An m×n matrix becomes n×m.
</p>
<div class="example-box">
<div class="label">Example: Transpose</div>
<p>
A = | 1 2 3 | A<sup>T</sup> = | 1 4 |<br>
| 4 5 6 | | 2 5 |<br>
| 3 6 |
</p>
<p>The 2×3 matrix became a 3×2 matrix.</p>
</div>
<div class="example-box">
<div class="label">Example: Complete Matrix Operations</div>
<p><strong>Problem:</strong> Given A = | 2 1 | and B = | 0 3 |, find 2A - B</p>
<p> | 3 4 | | 1 2 |</p>
<p>
<strong>Step 1:</strong> Compute 2A<br>
2A = | 4 2 |<br>
| 6 8 |<br><br>
<strong>Step 2:</strong> Subtract B<br>
2A - B = | 4-0 2-3 | = | 4 -1 |<br>
| 6-1 8-2 | | 5 6 |
</p>
</div>
</section>
<!-- =============================== -->
<!-- SECTION 4: Matrix Operations for CS -->
<!-- =============================== -->
<section id="matrix-operations-cs">
<h2>4. Matrix Operations for CS</h2>
<h3>Systems of Equations as Ax = b</h3>
<p>
One of the most powerful uses of matrices is representing and solving systems of linear equations. Instead of writing out multiple equations, you pack everything into a single matrix equation.
</p>
<div class="example-box">
<div class="label">Example: System to Matrix Form</div>
<p><strong>System of equations:</strong></p>
<p>
2x + 3y = 7<br>
4x - y = 1
</p>
<p><strong>Matrix form Ax = b:</strong></p>
<p>
| 2 3 | | x | | 7 |<br>
| 4 -1 | * | y | = | 1 |<br><br>
A = coefficient matrix, x = unknowns vector, b = constants vector
</p>
</div>
<div class="tip-box">
<div class="label">Why This Matters</div>
<p>This isn't just a notation trick. Phrasing problems as Ax = b lets you use matrix operations to solve them -- and computers are incredibly fast at matrix operations. This is how physics simulators solve thousands of equations simultaneously, and how machine learning models find optimal parameters.</p>
</div>
<h3>Gaussian Elimination (Row Reduction)</h3>
<p>
<strong>Gaussian elimination</strong> is a systematic method for solving systems of equations by transforming the matrix into a simpler form. You create an <strong>augmented matrix</strong> (A with b appended) and perform row operations until the solution is clear.
</p>
<p>The three allowed <strong>row operations</strong> are:</p>
<ol>
<li><strong>Swap</strong> two rows</li>
<li><strong>Multiply</strong> a row by a nonzero constant</li>
<li><strong>Add</strong> a multiple of one row to another row</li>
</ol>
<div class="example-box">
<div class="label">Example: Gaussian Elimination (Step by Step)</div>
<p><strong>Problem:</strong> Solve the system</p>
<p>
x + y + z = 6<br>
2x + 3y + z = 14<br>
x + y + 2z = 9
</p>
<p><strong>Step 1:</strong> Write the augmented matrix</p>
<p>
| 1 1 1 | 6 |<br>
| 2 3 1 | 14 |<br>
| 1 1 2 | 9 |
</p>
<p><strong>Step 2:</strong> R2 = R2 - 2*R1 (eliminate x from row 2)</p>
<p>
| 1 1 1 | 6 |<br>
| 0 1 -1 | 2 |<br>
| 1 1 2 | 9 |
</p>
<p><strong>Step 3:</strong> R3 = R3 - R1 (eliminate x from row 3)</p>
<p>
| 1 1 1 | 6 |<br>
| 0 1 -1 | 2 |<br>
| 0 0 1 | 3 |
</p>
<p><strong>Step 4:</strong> Back-substitute from the bottom</p>
<p>
Row 3: z = 3<br>
Row 2: y - z = 2 → y = 2 + 3 = 5<br>
Row 1: x + y + z = 6 → x = 6 - 5 - 3 = -2
</p>
<p><strong>Solution:</strong> x = -2, y = 5, z = 3</p>
<p><strong>Verify:</strong> (-2) + 5 + 3 = 6, 2(-2) + 3(5) + 3 = 14, (-2) + 5 + 2(3) = 9</p>
</div>
<h3>Row Echelon Form</h3>
<p>
The goal of Gaussian elimination is to reach <strong>row echelon form</strong>, where:
</p>
<ul>
<li>All zero rows are at the bottom</li>
<li>The first nonzero entry in each row (called the <strong>pivot</strong>) is to the right of the pivot in the row above</li>
<li>The matrix forms a "staircase" pattern</li>
</ul>
<div class="formula-box">
Row Echelon Form (staircase pattern):<br><br>
| <strong>1</strong> * * * |<br>
| 0 <strong>1</strong> * * |<br>
| 0 0 <strong>1</strong> * |<br>
| 0 0 0 0 |<br><br>
(* = any number, <strong>bold</strong> = pivots)
</div>
<div class="tip-box">
<div class="label">CS Tip</div>
<p>In practice, you rarely hand-compute Gaussian elimination. Libraries like NumPy handle it. But understanding the process is essential for debugging numerical issues, understanding computational complexity (it's O(n³)), and knowing when a system has no solution or infinite solutions.</p>
</div>
</section>
<!-- =============================== -->
<!-- SECTION 5: Determinant -->
<!-- =============================== -->
<section id="determinant">
<h2>5. Determinant</h2>
<p>
The <strong>determinant</strong> is a single number computed from a square matrix that tells you important things about the matrix. Think of it as a measure of how much the matrix "stretches" or "squishes" space.
</p>
<h3>2×2 Determinant</h3>
<div class="formula-box">
For A = | a b |<br>
| c d |<br><br>
det(A) = ad - bc
</div>
<div class="example-box">
<div class="label">Example: 2×2 Determinant</div>
<p><strong>Problem:</strong> Find det(A) where A = | 3 2 |</p>
<p> | 1 4 |</p>
<p>
det(A) = (3)(4) - (2)(1)<br>
det(A) = 12 - 2<br>
det(A) = <strong>10</strong>
</p>
</div>
<h3>What the Determinant Means</h3>
<p>
Geometrically, the determinant represents the <strong>scaling factor for area (2D) or volume (3D)</strong> when you apply the matrix as a transformation.
</p>
<ul>
<li><strong>|det| > 1:</strong> The transformation <strong>expands</strong> area/volume</li>
<li><strong>|det| = 1:</strong> Area/volume is <strong>preserved</strong> (like rotation)</li>
<li><strong>0 < |det| < 1:</strong> The transformation <strong>shrinks</strong> area/volume</li>
<li><strong>det = 0:</strong> The transformation <strong>collapses</strong> a dimension (the matrix is <strong>singular</strong>)</li>
<li><strong>det < 0:</strong> The transformation <strong>flips</strong> orientation (like a reflection)</li>
</ul>
<div class="example-box">
<div class="label">Example: Determinant of a Scaling Matrix</div>
<p>
Scaling by 2 in x and 3 in y:<br>
A = | 2 0 |<br>
| 0 3 |<br><br>
det(A) = (2)(3) - (0)(0) = 6
</p>
<p>A 1×1 unit square becomes a 2×3 rectangle. Area goes from 1 to 6. The determinant is 6.</p>
</div>
<div class="example-box">
<div class="label">Example: Singular Matrix (det = 0)</div>
<p>
A = | 2 4 |<br>
| 1 2 |<br><br>
det(A) = (2)(2) - (4)(1) = 4 - 4 = <strong>0</strong>
</p>
<p>This matrix has no inverse. Row 1 is just 2 times row 2 -- the rows are "linearly dependent." The transformation collapses 2D space into a line.</p>
</div>
<h3>3×3 Determinant</h3>
<p>
For larger matrices, you expand along a row or column (cofactor expansion). For a 3×3 matrix:
</p>
<div class="formula-box">
For A = | a b c |<br>
| d e f |<br>
| g h i |<br><br>
det(A) = a(ei - fh) - b(di - fg) + c(dh - eg)
</div>
<div class="example-box">
<div class="label">Example: 3×3 Determinant</div>
<p>
A = | 1 2 3 |<br>
| 4 5 6 |<br>
| 7 8 0 |<br><br>
det(A) = 1(5*0 - 6*8) - 2(4*0 - 6*7) + 3(4*8 - 5*7)<br>
= 1(0 - 48) - 2(0 - 42) + 3(32 - 35)<br>
= -48 + 84 + (-9)<br>
= <strong>27</strong>
</p>
</div>
<div class="warning-box">
<div class="label">Warning</div>
<p>Computing determinants by cofactor expansion gets extremely expensive for large matrices -- it's O(n!) in the naive approach. Real libraries use LU decomposition (based on Gaussian elimination) which is O(n³). You should understand what determinants mean, but let NumPy compute them.</p>
</div>
</section>
<!-- =============================== -->
<!-- SECTION 6: Inverse Matrix -->
<!-- =============================== -->
<section id="inverse-matrix">
<h2>6. Inverse Matrix</h2>
<p>
The <strong>inverse</strong> of a matrix A, written A<sup>-1</sup>, is the matrix that "undoes" A. When you multiply A by its inverse, you get the identity matrix.
</p>
<div class="formula-box">
A * A<sup>-1</sup> = I and A<sup>-1</sup> * A = I
</div>
<p>
Think of it like division for matrices. If multiplication by A transforms space in some way, multiplication by A<sup>-1</sup> reverses that transformation exactly.
</p>
<h3>When Does the Inverse Exist?</h3>
<p>
A matrix has an inverse <strong>if and only if</strong> its determinant is not zero. If det(A) = 0, the matrix is called <strong>singular</strong> and has no inverse. This makes intuitive sense: if A collapses a dimension (det = 0), you can't recover the lost information.
</p>
<h3>2×2 Inverse Formula</h3>
<div class="formula-box">
For A = | a b | with det(A) = ad - bc ≠ 0<br>
| c d |<br><br>
A<sup>-1</sup> = (1 / det(A)) * | d -b |<br>
| -c a |<br><br>
Steps: swap a and d, negate b and c, divide by determinant
</div>
<div class="example-box">
<div class="label">Example: Finding a 2×2 Inverse</div>
<p>
<strong>Problem:</strong> Find A<sup>-1</sup> where A = | 4 7 |<br>
| 2 6 |
</p>
<p>
<strong>Step 1:</strong> det(A) = (4)(6) - (7)(2) = 24 - 14 = 10<br><br>
<strong>Step 2:</strong> Apply formula<br>
A<sup>-1</sup> = (1/10) * | 6 -7 |<br>
| -2 4 |<br><br>
A<sup>-1</sup> = | 0.6 -0.7 |<br>
| -0.2 0.4 |
</p>
<p>
<strong>Verify:</strong> A * A<sup>-1</sup> should equal I<br>
| 4 7 | * | 0.6 -0.7 | = | 4(0.6)+7(-0.2) 4(-0.7)+7(0.4) |<br>
| 2 6 | | -0.2 0.4 | | 2(0.6)+6(-0.2) 2(-0.7)+6(0.4) |<br><br>
= | 2.4-1.4 -2.8+2.8 | = | 1 0 |<br>
| 1.2-1.2 -1.4+2.4 | | 0 1 |
</p>
</div>
<h3>Using the Inverse to Solve Systems</h3>
<p>
If you have the equation Ax = b and you know A<sup>-1</sup>, you can solve for x directly:
</p>
<div class="formula-box">
Ax = b<br>
A<sup>-1</sup>Ax = A<sup>-1</sup>b<br>
Ix = A<sup>-1</sup>b<br>
<strong>x = A<sup>-1</sup>b</strong>
</div>
<div class="example-box">
<div class="label">Example: Solving with Inverse</div>
<p><strong>Problem:</strong> Solve using the inverse from above</p>
<p>
4x + 7y = 5<br>
2x + 6y = 4
</p>
<p>
x = A<sup>-1</sup>b = | 0.6 -0.7 | * | 5 |<br>
| -0.2 0.4 | | 4 |<br><br>
x = | 0.6(5) + (-0.7)(4) | = | 3 - 2.8 | = | 0.2 |<br>
| -0.2(5) + 0.4(4) | | -1 + 1.6 | | 0.6 |
</p>
<p><strong>Solution:</strong> x = 0.2, y = 0.6</p>
</div>
<div class="tip-box">
<div class="label">CS Tip</div>
<p>In practice, solving Ax = b by computing A<sup>-1</sup> explicitly is <strong>inefficient and numerically unstable</strong>. Real code uses LU decomposition or other factorization methods. In NumPy, use <code>np.linalg.solve(A, b)</code> instead of <code>np.linalg.inv(A) @ b</code>. But the inverse concept is still essential for understanding the theory.</p>
</div>
</section>
<!-- =============================== -->
<!-- SECTION 7: Eigenvalues and Eigenvectors -->
<!-- =============================== -->
<section id="eigenvalues-eigenvectors">
<h2>7. Eigenvalues and Eigenvectors</h2>
<p>
This is the concept that makes most students panic, but the idea is surprisingly simple. When you multiply most vectors by a matrix, they change both direction and magnitude. But some special vectors only get <strong>scaled</strong> -- they keep pointing in the same direction. These are <strong>eigenvectors</strong>, and the scaling factor is the <strong>eigenvalue</strong>.
</p>
<div class="formula-box">
Av = λv<br><br>
A = the matrix<br>
v = eigenvector (direction doesn't change)<br>
λ = eigenvalue (the scaling factor)
</div>
<p>
In words: "When I apply transformation A to vector v, I get back the same vector v, just scaled by λ."
</p>
<h3>How to Find Eigenvalues</h3>
<p>
Starting from Av = λv, we rearrange:
</p>
<div class="formula-box">
Av = λv<br>
Av - λv = 0<br>
(A - λI)v = 0<br><br>
For nonzero v to exist: <strong>det(A - λI) = 0</strong><br><br>
This is called the <strong>characteristic equation</strong>.
Solving it gives you the eigenvalues (λ values).
</div>
<div class="example-box">
<div class="label">Example: Finding Eigenvalues and Eigenvectors</div>
<p><strong>Problem:</strong> Find eigenvalues and eigenvectors of</p>
<p>
A = | 4 1 |<br>
| 2 3 |
</p>
<p><strong>Step 1:</strong> Set up A - λI</p>
<p>
A - λI = | 4-λ 1 |<br>
| 2 3-λ |
</p>
<p><strong>Step 2:</strong> Set determinant to zero</p>
<p>
det(A - λI) = (4-λ)(3-λ) - (1)(2) = 0<br>
12 - 4λ - 3λ + λ² - 2 = 0<br>
λ² - 7λ + 10 = 0<br>
(λ - 5)(λ - 2) = 0
</p>
<p><strong>Eigenvalues:</strong> λ<sub>1</sub> = 5, λ<sub>2</sub> = 2</p>
<p><strong>Step 3:</strong> Find eigenvectors for each λ</p>
<p>
<strong>For λ<sub>1</sub> = 5:</strong> Solve (A - 5I)v = 0<br>
| -1 1 | * | v<sub>1</sub> | = | 0 |<br>
| 2 -2 | | v<sub>2</sub> | | 0 |<br><br>
-v<sub>1</sub> + v<sub>2</sub> = 0 → v<sub>2</sub> = v<sub>1</sub><br>
<strong>Eigenvector:</strong> v<sub>1</sub> = [1, 1] (or any scalar multiple)
</p>
<p>
<strong>For λ<sub>2</sub> = 2:</strong> Solve (A - 2I)v = 0<br>
| 2 1 | * | v<sub>1</sub> | = | 0 |<br>
| 2 1 | | v<sub>2</sub> | | 0 |<br><br>
2v<sub>1</sub> + v<sub>2</sub> = 0 → v<sub>2</sub> = -2v<sub>1</sub><br>
<strong>Eigenvector:</strong> v<sub>2</sub> = [1, -2] (or any scalar multiple)
</p>
<p><strong>Verification for λ<sub>1</sub> = 5, v = [1, 1]:</strong></p>
<p>
Av = | 4 1 | * | 1 | = | 5 | = 5 * | 1 | = λv
<br>
| 2 3 | | 1 | | 5 | | 1 |
</p>
</div>
<h3>Why CS Cares About Eigenvalues</h3>
<table>
<thead>
<tr>
<th>Application</th>
<th>How Eigenvalues/Eigenvectors Are Used</th>
</tr>
</thead>
<tbody>
<tr>
<td>Google PageRank</td>
<td>Web pages are nodes in a giant matrix. The principal eigenvector of the link matrix gives page importance rankings.</td>
</tr>
<tr>
<td>PCA (Data Science)</td>
<td>Eigenvectors of the covariance matrix point in the directions of greatest variance. This lets you reduce dimensionality while keeping the most important patterns.</td>
</tr>
<tr>
<td>Stability Analysis</td>
<td>If all eigenvalues of a system's matrix have magnitude < 1, the system is stable. Used in control systems and simulation.</td>
</tr>
<tr>
<td>Image Compression</td>
<td>SVD (closely related to eigendecomposition) lets you approximate images using only the most significant components.</td>
</tr>
<tr>
<td>Graph Algorithms</td>
<td>Eigenvalues of the adjacency matrix reveal graph properties like connectivity and clustering structure (spectral graph theory).</td>
</tr>
</tbody>
</table>
<div class="tip-box">
<div class="label">Intuition</div>
<p>Think of eigenvectors as the "natural axes" of a transformation. A matrix might do complicated things to most vectors, but along its eigenvectors, the action is simple: just stretching. Eigenvalues tell you how much stretching happens along each axis. This is why they simplify so many problems -- they reveal the matrix's fundamental behavior.</p>
</div>
</section>
<!-- =============================== -->
<!-- SECTION 8: Linear Transformations -->
<!-- =============================== -->
<section id="linear-transformations">
<h2>8. Linear Transformations</h2>
<p>
Every matrix represents a <strong>linear transformation</strong> -- a function that takes vectors in, moves/stretches/rotates them, and outputs new vectors. When you multiply a vector by a matrix, you're applying that transformation. This is the connection between abstract matrix math and real visual/spatial effects.
</p>
<div class="formula-box">
<strong>Linear Transformation — Formal Definition (The Two Axioms):</strong><br><br>
A function T is a linear transformation if and only if:<br><br>
1. <strong>Additivity:</strong> T(u + v) = T(u) + T(v) for all vectors u, v<br>
2. <strong>Homogeneity:</strong> T(cu) = c·T(u) for all vectors u and scalars c<br><br>
<span style="color:#555555;">If a transformation satisfies these two rules, it can always be represented as a matrix multiplication T(x) = Ax.</span>
</div>
<h3>Common 2D Transformation Matrices</h3>
<table>
<thead>
<tr>
<th>Transformation</th>
<th>Matrix</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr>
<td>Rotation by θ</td>
<td>| cosθ -sinθ |<br>| sinθ cosθ |</td>
<td>Rotates all points by angle θ around the origin</td>
</tr>
<tr>
<td>Scaling</td>
<td>| s<sub>x</sub> 0 |<br>| 0 s<sub>y</sub> |</td>
<td>Stretches x by s<sub>x</sub> and y by s<sub>y</sub></td>
</tr>
<tr>
<td>Reflection (x-axis)</td>
<td>| 1 0 |<br>| 0 -1 |</td>
<td>Flips vertically (negates y)</td>
</tr>
<tr>
<td>Reflection (y-axis)</td>
<td>| -1 0 |<br>| 0 1 |</td>
<td>Flips horizontally (negates x)</td>
</tr>
<tr>
<td>Shear (horizontal)</td>
<td>| 1 k |<br>| 0 1 |</td>