-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2867 lines (2608 loc) · 147 KB
/
index.html
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>
<!-- 2019-08-28 Wed 18:04 -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Elinear: linear algebra basics in Clojure</title>
<meta name="generator" content="Org mode">
<meta name="author" content="George Kontsevich">
<meta name="description" content="Linear algebra system in ELisp from the basics"
>
<link rel="stylesheet" type="text/css" href="../web/worg.css" />
<link rel="shortcut icon" href="../web/panda.svg" type="image/x-icon">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
displayAlign: "center",
displayIndent: "0em",
"HTML-CSS": { scale: 100,
linebreaks: { automatic: "false" },
webFont: "TeX"
},
SVG: {scale: 100,
linebreaks: { automatic: "false" },
font: "TeX"},
NativeMML: {scale: 100},
TeX: { equationNumbers: {autoNumber: "AMS"},
MultLineWidth: "85%",
TagSide: "right",
TagIndent: ".8em"
}
});
</script>
<script type="text/javascript"
src="../MathJax/MathJax.js?config=TeX-AMS_CHTML"></script>
</head>
<body>
<div id="org-div-home-and-up">
<a accesskey="h" href=".."> UP </a>
|
<a accesskey="H" href=".."> HOME </a>
</div><div id="content">
<h1 class="title">Elinear: linear algebra basics in Clojure</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgc1f3607">Preface</a></li>
<li><a href="#org8a93bb2">Systems of linear equations</a>
<ul>
<li><a href="#org42f889f">A farming problem</a></li>
</ul>
</li>
<li><a href="#org8c2eb22">Matices as representations of linear systems</a>
<ul>
<li><a href="#org18f9659">The Matrix in the computer</a>
<ul>
<li><a href="#org4216a33">Some helpers</a></li>
</ul>
</li>
<li><a href="#org8190b1e">Transposition: Getting the other equivalent matrix</a></li>
</ul>
</li>
<li><a href="#orgbf09bea">Representing the whole system of equations</a>
<ul>
<li><a href="#org69bd649">Matrix Multiplication</a>
<ul>
<li><a href="#org6122d7b">Inner Product</a></li>
<li><a href="#org7e3057d">Submatrices</a></li>
<li><a href="#orgf081d3b">Matrix Product</a></li>
<li><a href="#org68d3443">Matrix Conformability</a></li>
<li><a href="#org0152e5d">Addendum: Scalar Product</a></li>
</ul>
</li>
<li><a href="#org7ce0c81">A system of equations as matrix product</a>
<ul>
<li><a href="#org4d1956a">The mirror universe</a></li>
</ul>
</li>
<li><a href="#orgf5c2234">Chaining problems through matrix composition</a>
<ul>
<li><a href="#org44d1529">Taxing our farmers</a></li>
<li><a href="#org2da90ba">EXAMPLE: Geometrical transformations</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org7df7c9a">Equivalent matrices</a>
<ul>
<li><a href="#orgea3d75b">Identity Matrix</a></li>
<li><a href="#org7000662">Unit Column/Rows</a></li>
<li><a href="#org02c1f79">Addition</a></li>
<li><a href="#org2fa5457">Elementary Matrices</a>
<ul>
<li><a href="#org4cfd0db">Type I - Row/Column Interchange</a></li>
<li><a href="#org1c3a0b4">Type II - Row/Column Multiple</a></li>
<li><a href="#org52bfbda">Type III - Row/Column Addition</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org66c6817">The LU Decomposition</a>
<ul>
<li><a href="#orgca0294c">Gaussian elimination in matrix form</a>
<ul>
<li><a href="#org886b21d">Elementary Lower Triangular Matrics</a></li>
<li><a href="#orgec68ac9">Building the <b>L</b> Matrix</a></li>
<li><a href="#org3709d27">Partial Pivoting</a></li>
<li><a href="#org304cb08">Extracting the pivots</a></li>
</ul>
</li>
<li><a href="#org9db8819">Using the LU</a>
<ul>
<li><a href="#org9e7536d">Solving for x in Ax=b</a></li>
<li><a href="#orga61e74c">The LDU Decomposition</a></li>
<li><a href="#org2b4f662">The Cholesky Decomposition</a></li>
<li><a href="#org52fddae">Solving for A<sup>-1</sup></a></li>
<li><a href="#org352a5fa">Least Squares</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org6fe1db3">The QR Decomposition</a>
<ul>
<li><a href="#org0fd1556">The Gram-Schmidt procedure</a>
<ul>
<li><a href="#orgb5750a5">The Base case</a></li>
<li><a href="#orgfd31a2a">Recursive Step</a></li>
</ul>
</li>
<li><a href="#org77cab40">Decomposing</a></li>
<li><a href="#org5a3d0ad">The Householder reduction</a>
<ul>
<li><a href="#org1406244">elementary reflector</a></li>
<li><a href="#orgcbb562a">elementary coordinate reflector</a></li>
<li><a href="#org49e5026">The QR decomposition - part 2</a></li>
</ul>
</li>
<li><a href="#org0e3219c">Givens reduction</a></li>
<li><a href="#orgbd50524">Least Squares again</a></li>
</ul>
</li>
<li><a href="#org7588cdc"><span class="todo TODO">TODO</span> s</a></li>
<li><a href="#org5795772">End</a></li>
<li><a href="#org32f96c8">Notes from the Author</a></li>
</ul>
</div>
</div>
<div id="outline-container-orgc1f3607" class="outline-2">
<h2 id="orgc1f3607">Preface</h2>
<div class="outline-text-2" id="text-orgc1f3607">
<p>
This text is primarily my personal notes on linear algebra as I go through <a href="https://www.matrixanalysis.com">Matrix Analysis & Applied Linear Algebra</a>. At the same time this document is a literate program that can be executed in Emacs so the text will be slowly building up a linear algebra library of sorts. This will often not match the order things are presented in the book. There is no emphasis on performance - just on clarity, extensability and correctness when possible. This is purely (self)educational with my primary motivation being to help me better understand what I learn (through having to explain it) and to sanity check with actual programs. Things that are adequately explained in the book will not be repeated here.
</p>
<p>
This is my first program in Elisp, so if you see any issues, please leave a note in the <a href="https://github.com/geokon-gh/linearsystems/issues">issues</a> tab of <a href="https://github.com/geokon-gh/linearsystems/">the repository</a>. There you can also find the original org-mode file and the generated elisp files - both of which have additional unit-tests ommited from this webpage.
</p>
<p>
This is very much a work in progress and will change often…
</p>
</div>
</div>
<div id="outline-container-org8a93bb2" class="outline-2">
<h2 id="org8a93bb2">Systems of linear equations</h2>
<div class="outline-text-2" id="text-org8a93bb2">
<p>
The book's opening problem from ancient China of calculating the price of bushels of crop serves as a good example of a linear problem. I've simplified the problem a bit for clarity - but I will expand on it and refer back to it extensively:
</p>
</div>
<div id="outline-container-org42f889f" class="outline-3">
<h3 id="org42f889f">A farming problem</h3>
<div class="outline-text-3" id="text-org42f889f">
<blockquote>
<p>
You have a 3 fruit farms in a region of ancient China. In a given year:
</p>
<p>
<b>Given 1:</b><br>
Farm 1 produces 3 tons of apples 2 ton of oranges and 1 ton of lemons<br>
Farm 2 produces 2 tons of apples 3 tons of oranges and 1 ton of lemons<br>
Farm 3 produces 1 ton of apples 2 tons of oranges and 3 tons of lemons<br>
</p>
<p>
<b>Given 2:</b><br>
Farm 1 sold its fruit for 39 yuan<br>
Farm 2 sold its fruit for 34 yuan<br>
Farm 3 sold its fruit for 26 yuan<br>
</p>
<p>
What is the price of the a ton of apples/oranges/lemons?
</p>
</blockquote>
<p>
This is a familiar problem that can be restated as a system of linear equations
</p>
\begin{equation}
\begin{split}
3x+2y+z = 39\\
2x+3y+z = 34\\
x+ 2y + 3z = 26
\end{split}
\end{equation}
<p>
Where <code>x</code>, <code>y</code> and <code>z</code> represent <code>apples</code> <code>oranges</code> and <code>lemons</code> respectively
</p>
<p>
We know how to solve this system by manipulating the equations, solving for a variable and then back-substituting the results.
</p>
<p>
It's not accident I split up the problem into two sets of <b>Givens</b>. It's important to note that the problem actually has two distinct and independent parts. There is the farm/crop <b>linear system</b> (<code>Given 1</code>), and then there is the <b>constraint</b> of the profits of each farm (<code>Given 2</code>)
</p>
<p>
We are looking for the input fruit-prices that will yield the given profits for each farm
</p>
</div>
</div>
</div>
<div id="outline-container-org8c2eb22" class="outline-2">
<h2 id="org8c2eb22">Matices as representations of linear systems</h2>
<div class="outline-text-2" id="text-org8c2eb22">
<p>
The <b>linear system</b> can be represented with a matrix
</p>
\begin{bmatrix}
3 & 2 & 1\\
2 & 3 & 1\\
1 & 2 & 3\\
\end{bmatrix}
<p>
or flipped::
</p>
\begin{bmatrix}
3 & 2 & 1\\
2 & 3 & 2\\
1 & 1 & 3\\
\end{bmatrix}
<p>
We prefer the first representation, but both ways work as long as you remember what each row and column represents
</p>
</div>
<div id="outline-container-org18f9659" class="outline-3">
<h3 id="org18f9659">The Matrix in the computer</h3>
<div class="outline-text-3" id="text-org18f9659">
<p>
Once we've chosen a layout the easiest way to store the matrix in the computer is to remember 3 values: <code>number-of-rows</code> <code>number-of-columns</code> <code>data</code>
</p>
<p>
The <code>data</code> value will be a long list of size <code>num-row * num-col</code> that contains all the values of the matrix; row after row. So given a list <code>data</code> and a pair of sizes we simply build the matrix into a list of these three values:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-from-data-list</span> (number-of-rows number-of-columns data-list<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Builds a matrix from a data list"</span>
(list
number-of-rows
number-of-columns
data-list<span style="color: #999999;">))</span>
</pre>
</div>
</div>
<div id="outline-container-org4216a33" class="outline-4">
<h4 id="org4216a33">Some helpers</h4>
<div class="outline-text-4" id="text-org4216a33">
<p>
With a couple of helper function we can get back these 3 fields. This will improve the readability of the code as we go along
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-rows</span> (matrix<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Get the number of rows"</span>
(nth 0 matrix<span style="color: #999999;">))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-columns</span> (matrix<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Get the number of columns"</span>
(nth 1 matrix<span style="color: #999999;">))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-data</span> (matrix<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Get the data list from the matrix"</span>
(nth 2 matrix<span style="color: #999999;">))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-get-value</span> (matrix row column<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Get the scalar value at position ROW COLUMN (ZERO indexed) from MATRIX"</span>
(nth
(+
column
(*
row
(matrix-columns matrix<span style="color: #999999;">)))</span>
(matrix-data matrix<span style="color: #999999;">)))</span>
</pre>
</div>
<blockquote>
<p>
<code>nth</code> gets the nth element of the list
</p>
</blockquote>
<p>
For debugging and looking at results we also need to be able to print out the matrix for inspection
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-data-get-first-n-values</span> (data n<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Given a list of values, get the first n in a string"</span>
(<span style="color: #0000FF;">if</span> (zerop n<span style="color: #999999;">)</span>
<span style="color: #008000;">""</span> <span style="color: #8D8D84;">;</span><span style="color: #8D8D84; font-style: italic;">base case</span>
(concat
(number-to-string (car data<span style="color: #999999;">))</span>
<span style="color: #008000;">" "</span>
(matrix-data-get-first-n-values (cdr data) (1- n))))) <span style="color: #8D8D84;">;</span><span style="color: #8D8D84; font-style: italic;">iterative step</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-data-print</span> (number-of-rows number-of-columns data<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Print out the data list gives the dimension of the original matrix"</span>
(<span style="color: #0000FF;">if</span> (zerop number-of-rows<span style="color: #999999;">)</span>
<span style="color: #008000;">""</span> <span style="color: #8D8D84;">;</span><span style="color: #8D8D84; font-style: italic;">base case</span>
(concat
(matrix-data-get-first-n-values data number-of-columns<span style="color: #999999;">)</span>
<span style="color: #008000;">"\n"</span>
(matrix-data-print <span style="color: #8D8D84;">;</span><span style="color: #8D8D84; font-style: italic;">iterative step</span>
(1- number-of-rows<span style="color: #999999;">)</span>
number-of-columns
(nthcdr number-of-columns data <span style="color: #999999;">)))))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-print</span> (matrix<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Print out the matrix"</span>
(concat <span style="color: #008000;">"\n"</span> (matrix-data-print
(matrix-rows matrix<span style="color: #999999;">)</span>
(matrix-columns matrix<span style="color: #999999;">)</span>
(matrix-data matrix<span style="color: #999999;">))))</span>
<span style="color: #8D8D84;">; </span><span style="color: #8D8D84; font-style: italic;">ex: (message (matrix-print (matrix-from-data-list 2 2 '(1 2 3 4))))</span>
</pre>
</div>
<blockquote>
<p>
<code>zerop</code> tests if the value is zero
</p>
</blockquote>
<blockquote>
<p>
<code>()</code> with a quote is the <i>empty-list</i>
</p>
</blockquote>
<blockquote>
<p>
<code>cons</code> attaches the first argument to the second argument (which is normally a list)
</p>
</blockquote>
<blockquote>
<p>
<code>cdr</code> returns the list without the first element
</p>
</blockquote>
</div>
</div>
</div>
<div id="outline-container-org8190b1e" class="outline-3">
<h3 id="org8190b1e">Transposition: Getting the other equivalent matrix</h3>
<div class="outline-text-3" id="text-org8190b1e">
<p>
Since we have two equivalent matrices that represent our linear system we need a mechanism to go from one to the other. This method is the matrix transpose which flips the matrix along the diagonal. The text goes into depth on the properties of the matrix transpose, but in short, as long as you take the transpose of both sides of your equations equivalances will be preserved.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-transpose</span> (matrix<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Get the transpose of a matrix"</span>
(<span style="color: #0000FF;">if</span> (equal (matrix-columns matrix) 1<span style="color: #999999;">)</span>
(matrix-from-data-list
1
(matrix-rows matrix<span style="color: #999999;">)</span>
(matrix-data matrix<span style="color: #999999;">))</span>
(matrix-append
(matrix-from-data-list
1
(matrix-rows matrix<span style="color: #999999;">)</span>
(matrix-data (matrix-get-column matrix 0<span style="color: #999999;">)))</span>
(matrix-transpose
(matrix-submatrix
matrix
0
1
(matrix-rows matrix<span style="color: #999999;">)</span>
(matrix-columns matrix<span style="color: #999999;">))))))</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orgbf09bea" class="outline-2">
<h2 id="orgbf09bea">Representing the whole system of equations</h2>
<div class="outline-text-2" id="text-orgbf09bea">
<p>
Now that we can represent the fruit/profits system we want a mechanism to represent the whole system of equations so that given a constraint, we can solve for a solution.
</p>
</div>
<div id="outline-container-org69bd649" class="outline-3">
<h3 id="org69bd649">Matrix Multiplication</h3>
<div class="outline-text-3" id="text-org69bd649">
<p>
This is done notationally with matrix multiplication. The notation allows us to keep the two <b>Givens</b> separated and allows us to visually chain linear systems together. As a shorthand, we write the product of two matrices <code>A</code> and <code>B</code> as <code>AB = C</code>, with the order of <code>A</code> and <code>B</code> being important. For every value (at a given row and column position) in the resulting matrix <code>C</code> we take the equivalent row in <code>A</code> and multiply it by its equivalent column in <code>B</code>. From this we can conclude that <code>C</code> will have as many rows as <code>A</code> and as many column as <code>B</code>
</p>
<p>
Multiplying a row times a column is called an <code>inner product</code>
</p>
</div>
<div id="outline-container-org6122d7b" class="outline-4">
<h4 id="org6122d7b">Inner Product</h4>
<div class="outline-text-4" id="text-org6122d7b">
<p>
The <code>inner-product</code> is defined as the sum of the product of every pair of equivalent elements in the two vectors. The sum will naturally return one scalar value. This operation only makes sense if both the row and column have the same number of values.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-inner-product-data</span> (row-data column-data<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Multiply a row times a column and returns a scalar. If they're empty you will get zero"</span>
(reduce
'+
(for-each-pair
row-data
column-data
'*<span style="color: #999999;">)))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-inner-product</span> (row column<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Multiply a row times a column and returns a scalar. If they're empty you will get zero"</span>
(matrix-inner-product-data
(matrix-data row<span style="color: #999999;">)</span>
(matrix-data column<span style="color: #999999;">)))</span>
</pre>
</div>
<blockquote>
<p>
<code>reduce</code> works down the list elements-by-element applying the operator on each cumulative result
</p>
</blockquote>
</div>
</div>
<div id="outline-container-org7e3057d" class="outline-4">
<h4 id="org7e3057d">Submatrices</h4>
<div class="outline-text-4" id="text-org7e3057d">
<p>
To get rows and columns (and other submatrices) we need a few more helper functions
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-extract-subrow</span> (matrix row start-column end-column<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Get part of a row of a matrix and generate a row matrix from it. START-COLUMN is inclusive, END-COLUMN is exclusive"</span>
(<span style="color: #0000FF;">let</span>
((number-of-columns-on-input (matrix-columns matrix<span style="color: #999999;">))</span>
(number-of-columns-on-output (-
end-column
start-column<span style="color: #999999;">)))</span>
(matrix-from-data-list
1
number-of-columns-on-output
(subseq
(matrix-data matrix<span style="color: #999999;">)</span>
(+ (* row number-of-columns-on-input) start-column<span style="color: #999999;">)</span>
(+ (* row number-of-columns-on-input) end-column<span style="color: #999999;">)))))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-append</span> (matrix1 matrix2<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Append one matrix (set of linear equations) to another"</span>
(<span style="color: #0000FF;">if</span> (null matrix2<span style="color: #999999;">)</span>
matrix1
(matrix-from-data-list
(+
(matrix-rows matrix2<span style="color: #999999;">)</span>
(matrix-rows matrix1<span style="color: #999999;">))</span>
(matrix-columns matrix1<span style="color: #999999;">)</span>
(append
(matrix-data matrix1<span style="color: #999999;">)</span>
(matrix-data matrix2<span style="color: #999999;">)))))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-submatrix</span> (matrix start-row start-column end-row end-column<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Get a submatrix. start-row/column are inclusive. end-row/column are exclusive"</span>
(<span style="color: #0000FF;">if</span> (equal start-row end-row<span style="color: #999999;">)</span>
'(<span style="color: #999999;">)</span>
(matrix-append
(matrix-extract-subrow matrix start-row start-column end-column<span style="color: #999999;">)</span>
(matrix-submatrix
matrix
(1+ start-row<span style="color: #999999;">)</span>
start-column
end-row
end-column<span style="color: #999999;">))))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-get-row</span> (matrix row<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Get a row from a matrix. Index starts are ZERO"</span>
(matrix-extract-subrow
matrix
row
0
(matrix-columns matrix<span style="color: #999999;">)))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-get-column</span> (matrix column<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Get a column from a matrix. Index starts are ZERO"</span>
(matrix-submatrix
matrix
0
column
(nth 0 matrix<span style="color: #999999;">)</span>
(1+ column<span style="color: #999999;">)))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgf081d3b" class="outline-4">
<h4 id="orgf081d3b">Matrix Product</h4>
<div class="outline-text-4" id="text-orgf081d3b">
<p>
Now we have all the tools we need to write down the algorithm for calculating the matrix product. First we write a function to calculate the product for one value at a given position
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-product-one-value</span> (matrix1 matrix2 row column<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Calculate one value in the resulting matrix of the product of two matrices"</span>
(matrix-inner-product
(matrix-get-row matrix1 row <span style="color: #999999;">)</span>
(matrix-get-column matrix2 column<span style="color: #999999;">)))</span>
</pre>
</div>
<p>
And then we recursively apply it to construct the resulting matrix
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-product</span> (matrix1 matrix2<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Multiply two matrices"</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-product-rec</span> (matrix1 matrix2 row column<span style="color: #999999;">)</span>
<span style="color: #036A07;">"A recursive helper function that builds the matrix multiplication's data vector"</span>
(<span style="color: #0000FF;">if</span> (equal (matrix-rows matrix1) row<span style="color: #999999;">)</span>
'(<span style="color: #999999;">)</span>
(<span style="color: #0000FF;">if</span> (equal (matrix-columns matrix2) column<span style="color: #999999;">)</span>
(matrix-product-rec
matrix1
matrix2
(1+ row<span style="color: #999999;">)</span>
0<span style="color: #999999;">)</span>
(cons
(matrix-product-one-value
matrix1
matrix2
row column<span style="color: #999999;">)</span>
(matrix-product-rec
matrix1
matrix2
row
(1+ column<span style="color: #999999;">))))))</span>
(matrix-from-data-list
(matrix-rows matrix1<span style="color: #999999;">)</span>
(matrix-columns matrix2<span style="color: #999999;">)</span>
(matrix-product-rec
matrix1
matrix2
0
0<span style="color: #999999;">)))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org68d3443" class="outline-4">
<h4 id="org68d3443">Matrix Conformability</h4>
<div class="outline-text-4" id="text-org68d3443">
<p>
You will notice that the algorithm won't make sense if the number of columns of <code>A</code> doesn't match the number of rows of <code>B</code>. When the values match the matrices are called <b>conformable</b>. When they <i>don't</i> match you will see that inner product isn't defined and therefore neither is the product.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-conformable?</span> (matrix1 matrix2<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Check that two matrices can be multiplied"</span>
(equal
(matrix-columns matrix1<span style="color: #999999;">)</span>
(matrix-rows matrix2<span style="color: #999999;">)))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org0152e5d" class="outline-4">
<h4 id="org0152e5d">Addendum: Scalar Product</h4>
<div class="outline-text-4" id="text-org0152e5d">
<p>
An additional form of matrix multiplication is between a matrix and a scalar. Here we simply multiply each element of the matrix times the scalar to construct the resulting matrix. The order of multiplication is not important -> <b>αA=Aα</b>
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-scalar-product</span> (matrix scalar<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Multiple the matrix by a scalar. ie. multiply each value by the scalar"</span>
(matrix-from-data-list
(matrix-rows matrix<span style="color: #999999;">)</span>
(matrix-columns matrix<span style="color: #999999;">)</span>
(mapcar
(<span style="color: #0000FF;">lambda</span> (x)
(* scalar x<span style="color: #999999;">))</span>
(matrix-data matrix<span style="color: #999999;">))))</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org7ce0c81" class="outline-3">
<h3 id="org7ce0c81">A system of equations as matrix product</h3>
<div class="outline-text-3" id="text-org7ce0c81">
<p>
Now that we have all our tools we can write down a matrix product that will mimic our system of equation.
</p>
\begin{equation}
\begin{bmatrix}
3 & 2 & 1\\
2 & 3 & 1\\
1 & 2 & 3\\
\end{bmatrix}
\begin{bmatrix}
x\\
y\\
z\\
\end{bmatrix}
=
\begin{bmatrix}
39\\
34\\
26\\
\end{bmatrix}
\end{equation}
<p>
Going through our algorithm manually we see that the resulting matrix is:
</p>
\begin{equation}
\begin{bmatrix}
3x + 2y + z\\
2x + 3y + z\\
x + 2y + 3z\\
\end{bmatrix}
=
\begin{bmatrix}
39\\
34\\
26\\
\end{bmatrix}
\end{equation}
</div>
<div id="outline-container-org4d1956a" class="outline-4">
<h4 id="org4d1956a">The mirror universe</h4>
<div class="outline-text-4" id="text-org4d1956a">
<p>
Now I said that flipped matrix was also a valid representation. We can confirm this by taking the transpose of both sides
</p>
\begin{equation}
\begin{bmatrix}
x & y & z\\
\end{bmatrix}
\begin{bmatrix}
3 & 2 & 1\\
2 & 3 & 2\\
1 & 1 & 3\\
\end{bmatrix}
=
\begin{bmatrix}
39 & 34 & 26\\
\end{bmatrix}
\end{equation}
<p>
It yields another matrix product that mimics the equations, however you'll see in the textbook that we always prefer the first notation.
</p>
</div>
</div>
</div>
<div id="outline-container-orgf5c2234" class="outline-3">
<h3 id="orgf5c2234">Chaining problems through matrix composition</h3>
<div class="outline-text-3" id="text-orgf5c2234">
<p>
The real power of matrix multiplication is in its ability to chain systems together through <b>linear composition</b>
</p>
<p>
If we are given a new problem that take the output of our first system and produces a new output - composition gives us a mechanism to combine the systems into one.
</p>
</div>
<div id="outline-container-org44d1529" class="outline-4">
<h4 id="org44d1529">Taxing our farmers</h4>
<div class="outline-text-4" id="text-org44d1529">
<p>
Say the imperial palace has a system for collecting taxes
</p>
<blockquote>
<p>
<b>Given</b>:<br>
The farms have to pay a percentage of their income to different regional governements. The breakdown is as follows:<br>
The town taxes Farm 1 at 5%, Farm 2 at 3%, Farm 3 at 7%<br>
The province taxes all Farm 1 at 2% Farm 2 at 4%, Farm 3 at 2%<br>
The palace taxes all farms at 7%
</p>
</blockquote>
<p>
Now, given the income of each farm <b>i</b> we can build a new matrix <b>B</b> and calculate the tax revenue of each government - <b>t</b>.<br>
</p>
\begin{equation}
Bi=t
\end{equation}
<p>
From the previous problem we know that the income of each farm was already a system of equation with the price of fruit as input <b>f</b><br>
</p>
\begin{equation}
Af=i
\end{equation}
<p>
So we just plug one into the other and get<br>
</p>
\begin{equation}
B(Af)=t
\end{equation}
<p>
and compose a new equation that given the price of fruit gives us the regional tax revenue. By carrying out the product we can generate one linear system<br>
</p>
\begin{equation}
(BA)f=t\\
\end{equation}
<p>
Where if <b>BA=C</b> the final composed system is:
</p>
\begin{equation}
Cf=t
\end{equation}
<p>
Note that the rows of <b>BA</b> are the combination of the rows of <b>A</b> and the columns of <b>BA</b> are the combination of the columns of <b>B</b> - at the same time! (see page 98)
</p>
</div>
</div>
<div id="outline-container-org2da90ba" class="outline-4">
<h4 id="org2da90ba">EXAMPLE: Geometrical transformations</h4>
<div class="outline-text-4" id="text-org2da90ba">
<p>
A very simple example are the linear systems that takes coordinates <i>x y</i> and do transformations on them
</p>
<p>
<b>Rotation</b>
</p>
\begin{equation}
\begin{bmatrix}
\cos \theta & -\sin \theta \\
\sin \theta & \cos \theta \\
\end{bmatrix}
\begin{bmatrix}
x \\
y \\
\end{bmatrix}
=
\begin{bmatrix}
x_{rotated}\\
y_{rotated}\\
\end{bmatrix}
\end{equation}
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-rotate-2D</span> (radians<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Generate a matrix that will rotates a [x y] column vector by RADIANS"</span>
(matrix-from-data-list
2
2
(list
(cos radians<span style="color: #999999;">)</span>
(- (sin radians<span style="color: #999999;">))</span>
(sin radians<span style="color: #999999;">)</span>
(cos radians<span style="color: #999999;">))))</span>
</pre>
</div>
<p>
<b>Reflection about X-Axis</b>
</p>
\begin{equation}
\begin{bmatrix}
1 & 0 \\
0 & -1\\
\end{bmatrix}
\begin{bmatrix}
x \\
y \\
\end{bmatrix}
=
\begin{bmatrix}
x_{reflected}\\
y_{reflected}\\
\end{bmatrix}
\end{equation}
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-reflect-around-x-2D</span> (<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Generate a matrix that will reflect a [x y] column vector around the x axis"</span>
(matrix-from-data-list
2
2
'(1 0 0 -1<span style="color: #999999;">)))</span>
</pre>
</div>
<p>
<b>Projection on line</b>
</p>
\begin{equation}
\begin{bmatrix}
1/2 & 1/2 \\
1/2 & 1/2\\
\end{bmatrix}
\begin{bmatrix}
x \\
y \\
\end{bmatrix}
=
\begin{bmatrix}
x_{projected}\\
y_{projected}\\
\end{bmatrix}
\end{equation}
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-project-on-x=y-diagonal-2D</span> (<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Generate a matrix that projects a point ([x y] column vector) onto a line (defined w/ a unit-vector)"</span>
(matrix-from-data-list
2
2
'(0.5 0.5 0.5 0.5<span style="color: #999999;">)))</span>
</pre>
</div>
<p>
So given a point <i>[x y]</i> (represented by the column vector <b>v</b>) we can use these 3 transformation matrices to move it around our 2D space. We simple write a chain of transformations <b>T</b> and multiply them times the given vector <b>T<sub>1</sub>T<sub>2</sub>T<sub>3</sub>v=v<sub>new</sub></b>. These transformation matrices can then be multiplied together into one that will carry out the transformation in one matrix product. <b>T<sub>1</sub>T<sub>2</sub>T<sub>3</sub>=T<sub>total</sub></b> => <b>T<sub>total</sub>v=v<sub>new</sub></b>
</p>
</div>
</div>
</div>
</div>
<div id="outline-container-org7df7c9a" class="outline-2">
<h2 id="org7df7c9a">Equivalent matrices</h2>
<div class="outline-text-2" id="text-org7df7c9a">
<p>
Now thanks to matrix multiplication we can represent linear systems and we can chain them together. The next step is extending multiplication to represent general manipulations of matrices.
</p>
</div>
<div id="outline-container-orgea3d75b" class="outline-3">
<h3 id="orgea3d75b">Identity Matrix</h3>
<div class="outline-text-3" id="text-orgea3d75b">
<p>
For any matrix <b>A</b>, the identity matrix <b>I</b> is such that <b>A*I</b> = <b>A</b> = <b>I*A</b>. Given the dimensions, <b>I</b> has to be a square matrix. It will have <b>1</b>'s on the diagonal (ie. where <code>row==column</code>) and zeroes everywhere else. We build it recursively:
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-identity</span> (rank<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Build an identity matrix of the given size/rank"</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-build-identity-rec</span> (rank row column<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Helper function that build the data vector of the identity matrix"</span>
(<span style="color: #0000FF;">if</span> (equal column rank) <span style="color: #8D8D84;">; </span><span style="color: #8D8D84; font-style: italic;">time to build next row</span>
(<span style="color: #0000FF;">if</span> (equal row (1- rank<span style="color: #999999;">))</span>
'() <span style="color: #8D8D84;">; </span><span style="color: #8D8D84; font-style: italic;">we're done</span>
(matrix-build-identity-rec
rank
(1+ row<span style="color: #999999;">)</span>
0<span style="color: #999999;">))</span>
(<span style="color: #0000FF;">if</span> (equal row column<span style="color: #999999;">)</span>
(cons
1.0
(matrix-build-identity-rec
rank
row
(1+ column<span style="color: #999999;">)))</span>
(cons
0.0
(matrix-build-identity-rec
rank
row
(1+ column<span style="color: #999999;">))))))</span>
(matrix-from-data-list rank rank (matrix-build-identity-rec rank 0 0 <span style="color: #999999;">)))</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org7000662" class="outline-3">
<h3 id="org7000662">Unit Column/Rows</h3>
<div class="outline-text-3" id="text-org7000662">
<p>
Each column of the <b>identity matrix</b> is a unit column (denoted as <b>e<sub><i>j</i></sub></b>). It contains a <b>1</b> in a given postion (here: <i>j</i>) and <b>0s</b> everwhere else. Its transpose is naturally called the <b>unit row</b><br>
<b>Ae<sub><i>j</i></sub></b> = the <i>j</i> column of A<br>
<b>e<sub><i>i</i></sub><sup>T</sup>A</b> = the <i>i</i> row of A<br>
<b>e<sub><i>i</i></sub><sup>T</sup>Ae<sub><i>j</i></sub></b> = gets the [ <i>i</i>, <i>j</i> ] element in A
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-unit-rowcol-data</span> (index size<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Create a data-list for a matrix row/column. INDEX (starts at ZERO) matches the row or column where you want a 1. SIZE is the overall size of the vector"</span>
(<span style="color: #0000FF;">if</span> (zerop size<span style="color: #999999;">)</span>
'(<span style="color: #999999;">)</span>
(<span style="color: #0000FF;">if</span> (zerop index<span style="color: #999999;">)</span>
(cons
1.0
(matrix-unit-rowcol-data
(1- index<span style="color: #999999;">)</span>
(1- size<span style="color: #999999;">)))</span>
(cons
0.0
(matrix-unit-rowcol-data
(1- index<span style="color: #999999;">)</span>
(1- size<span style="color: #999999;">))))))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-unit-column</span> (row size<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Build a unit column. ROW is where you want the 1 to be placed (ZERO indexed). SIZE is the overall length"</span>
(matrix-from-data-list
size
1
(matrix-unit-rowcol-data
row
size<span style="color: #999999;">)))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-unit-row</span> (column size<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Build a unit column. COLUMN is where you want the 1 to be placed (ZERO indexed). SIZE is the overall length"</span>
(matrix-from-data-list
1
size
(matrix-unit-rowcol-data
column
size<span style="color: #999999;">)))</span>
</pre>
</div>
<blockquote>
<p>
Here I'm just trying out a new notation. With <code>letrec</code> we can hide the recursive helper function inside the function that uses it.
</p>
</blockquote>
</div>
</div>
<div id="outline-container-org02c1f79" class="outline-3">
<h3 id="org02c1f79">Addition</h3>
<div class="outline-text-3" id="text-org02c1f79">
<p>
As a tool in building new matrices, we need a way to easily add two matrices, ie. add their values one to one. Matrices that are added need to have the same size.
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-equal-size-p</span> (matrix1 matrix2<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Check if 2 matrices are the same size"</span>
(<span style="color: #0000FF;">and</span>
(equal
(matrix-rows matrix1<span style="color: #999999;">)</span>
(matrix-rows matrix2<span style="color: #999999;">))</span>
(equal
(matrix-columns matrix1<span style="color: #999999;">)</span>
(matrix-columns matrix2<span style="color: #999999;">))))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">for-each-pair</span> (list1 list2 operator<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Go through 2 lists applying an operator on each pair of elements"</span>
(<span style="color: #0000FF;">if</span> (null list1<span style="color: #999999;">)</span>
'(<span style="color: #999999;">)</span>
(cons
(funcall operator (car list1) (car list2<span style="color: #999999;">))</span>
(for-each-pair (cdr list1) (cdr list2) operator<span style="color: #999999;">))))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-add</span> (matrix1 matrix2<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Add to matrices together"</span>
(<span style="color: #0000FF;">if</span> (matrix-equal-size-p matrix1 matrix2<span style="color: #999999;">)</span>
(matrix-from-data-list
(matrix-rows matrix1<span style="color: #999999;">)</span>
(matrix-columns matrix1<span style="color: #999999;">)</span>
(for-each-pair
(matrix-data matrix1<span style="color: #999999;">)</span>
(matrix-data matrix2<span style="color: #999999;">)</span>
'+<span style="color: #999999;">))))</span>
(<span style="color: #0000FF;">defun</span> <span style="color: #006699;">matrix-subtract</span> (matrix1 matrix2<span style="color: #999999;">)</span>
<span style="color: #036A07;">"Subtract MATRIX2 from MATRIX1"</span>
(<span style="color: #0000FF;">if</span> (matrix-equal-size-p matrix1 matrix2<span style="color: #999999;">)</span>
(matrix-from-data-list
(matrix-rows matrix1<span style="color: #999999;">)</span>
(matrix-columns matrix1<span style="color: #999999;">)</span>