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
|
%D \module
%D [ file=m-matrix,
%D version=2014.11.04, % already a year older
%D title=\CONTEXT\ Extra Modules,
%D subtitle=Matrices,
%D author={Jeong Dalyoung \& Hans Hagen},
%D date=\currentdate,
%D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.
%D This code is based on a post by Dalyoung on the context list. After that
%D we turned it into a module and improved the code a bit. Feel free to ask
%D us for more. Once we're satisfied, a more general helper l-matrix could
%D be made. Dalyoung does the clever bits, and Hans only cleanes up and
%D optimizes a bit.
% \registerctxluafile{l-matrix}{} % not yet
\startmodule[matrix]
\startluacode
local settings_to_hash = utilities.parsers.settings_to_hash
local formatters = string.formatters
local copy = table.copy
local insert = table.insert
local remove = table.remove
local random = math.random
local context = context
local matrix = { }
moduledata.matrix = matrix
local f_matrix_slot = formatters["%s_{%s%s}"]
function matrix.symbolic(sym, x, y, nx ,ny) -- symMatrix("a", "m", "n")
local nx = nx or 2
local ny = ny or nx
local function filled(i,y)
local mrow = { }
for j=1,nx do
mrow[#mrow+1] = f_matrix_slot(sym,i,j)
end
mrow[#mrow+1] = "\\cdots"
mrow[#mrow+1] = f_matrix_slot(sym,i,y)
return mrow
end
local function dummy()
local mrow = { }
for j=1,nx do
mrow[#mrow+1] = "\\vdots"
end
mrow[#mrow+1] = "\\ddots"
mrow[#mrow+1] = "\\vdots"
return mrow
end
--
local mm = { }
for i=1,ny do
mm[i] = filled(i,y)
end
mm[#mm+1] = dummy()
mm[#mm+1] = filled(x,y)
return mm
end
-- todo: define a matrix at the tex end so that we have more control
-- local fences = {
-- parentheses = { left = "\\left(\\,", right = "\\,\\right)" },
-- brackets = { left = "\\left[\\,", right = "\\,\\right]" },
-- bars = { left = "\\left|\\,", right = "\\,\\right|" },
-- }
local fences = {
parentheses = { "matrix:parentheses" },
brackets = { "matrix:brackets" },
bars = { "matrix:bars" },
}
-- one can add more fences
fences.bar = fences.bars
fences.parenthesis = fences.parentheses
fences.bracket = fences.brackets
-- one can set the template
matrix.template = "%0.3F"
function matrix.typeset(m,options)
if type(m) == "table" then
local options = settings_to_hash(options or "")
local whatever = options.determinant == "yes" and fences.bars or fences.parentheses
if options.fences then
whatever = fences[options.fences] or whatever
elseif options.determinant then
-- whatever = fences.brackets
whatever = fences.bars
end
local template = options.template or matrix.template
if template == "yes" then
template = matrix.template
elseif template == "no" then
template = false
elseif tonumber(template) then
template = "%0." .. template .. "F"
end
context.startnamedmatrix(whatever)
if type(m[1]) ~= "table" then
m = { copy(m) }
end
for i=1, #m do
local mi = m[i]
for j=1,#mi do
context.NC()
local n = mi[j]
if template and tonumber(n) then
context(template,n)
else
context(mi[j])
end
end
context.NR()
end
context.stopnamedmatrix()
elseif m then
context(m)
end
end
function matrix.swaprows(t,i,j)
local ti = t[i]
if not ti then
return "error: no row"
end
local tj = t[j]
if not tj then
return "error: no row"
end
t[i], t[j] = tj, ti
return t
end
-- interchange two columns (i-th, j-th)
function matrix.swapcolumns(t, i, j)
local t1 = t[1]
if not t1 then
return "error: no rows"
end
local n = #t1
if i > n or j > n then
return "error: no column"
end
for k = 1, #t do
local tk = t[k]
tk[i], tk[j] = tk[j], tk[i]
end
return t
end
matrix.swapC = matrix.swapcolumns
matrix.swapR = matrix.swaprows
matrix.swap = matrix.swaprows
-- replace i-th row with factor * (i-th row)
function matrix.multiply(m,i,factor)
local mi = m[i]
for k=1,#mi do
mi[k] = factor * mi[k]
end
return m
end
-- scalar product "factor * m"
function matrix.scalar(m, factor)
for i=1,#m do
local mi = m[i]
for j=1,#mi do
mi[j] = factor * mi[j]
end
end
return m
end
-- replace i-th row with i-th row + factor * (j-th row)
function matrix.sumrow(m,i,j,factor)
local mi = m[i]
local mj = m[j]
for k=1,#mi do
mi[k] = mi[k] + factor * mj[k]
end
end
-- transpose of a matrix
function matrix.transpose(m)
local t = { }
for j=1,#m[1] do
local r = { }
for i=1,#m do
r[i] = m[i][j]
end
t[j] = r
end
return t
end
-- inner product of two vectors
function matrix.inner(u,v)
local nu = #u
if nu == 0 then
return 0
end
local nv = #v
if nv ~= nu then
return "error: size mismatch"
end
local result = 0
for i=1,nu do
result = result + u[i] * v[i]
end
return result
end
-- product of two matrices
function matrix.product(m1,m2)
if #m1[1] == #m2 then
local product = { }
for i=1,#m1 do
local m1i = m1[i]
local mrow = { }
for j=1,#m2[1] do
local temp = 0
for k=1,#m1[1] do
temp = temp + m1i[k] * m2[k][j]
end
mrow[j] = temp
end
product[i] = mrow
end
return product
else
return "error: size mismatch"
end
end
local function uppertri(m,sign)
local temp = copy(m)
for i=1,#temp-1 do
local pivot = temp[i][i]
if pivot == 0 then
local pRow = i +1
while temp[pRow][i] == 0 do
pRow = pRow + 1
if pRow > #temp then -- if there is no nonzero number
return temp
end
end
temp[i], temp[pRow] = temp[pRow], temp[i]
if sign then
sign = -sign
end
end
local mi = temp[i]
for k=i+1, #temp do
local factor = -temp[k][i]/mi[i]
local mk = temp[k]
for l=i,#mk do
mk[l] = mk[l] + factor * mi[l]
end
end
end
if sign then
return temp, sign
else
return temp
end
end
matrix.uppertri = uppertri
local function determinant(m)
if #m == #m[1] then
local d = 1
local t, s = uppertri(m,1)
for i=1,#t do
d = d * t[i][i]
end
return s*d
else
return "error: not a square matrix" -- not context(..)
end
end
matrix.determinant = determinant
local function rowechelon(m,r)
local temp = copy(m)
local pRow = 1
local pCol = 1
while pRow <= #temp do
local pivot = temp[pRow][pCol]
if pivot == 0 then
local i = pRow
local n = #temp
while temp[i][pCol] == 0 do
i = i + 1
if i > n then
-- no nonzero number in a column
pCol = pCol + 1
if pCol > #temp[pRow] then
-- there is no nonzero number in a row
return temp
end
i = pRow
end
end
temp[pRow], temp[i] = temp[i], temp[pRow]
end
local row = temp[pRow]
pivot = row[pCol]
for l=pCol,#row do
row[l] = row[l]/pivot
end
if r == 1 then
-- make the "reduced row echelon form"
local row = temp[pRow]
for k=1,pRow-1 do
local current = temp[k]
local factor = -current[pCol]
local mk = current
for l=pCol,#mk do
mk[l] = mk[l] + factor * row[l]
end
end
end
-- just make the row echelon form
local row = temp[pRow]
for k=pRow+1, #temp do
local current = temp[k]
local factor = -current[pCol]
local mk = current
for l=pCol,#mk do
mk[l] = mk[l] + factor * row[l]
end
end
pRow = pRow + 1
pCol = pCol + 1
if pRow > #temp or pCol > #temp[1] then
pRow = #temp + 1
end
end
return temp
end
matrix.rowechelon = rowechelon
matrix.rowEchelon = rowechelon
-- make matrices until its determinant is not 0
function matrix.make(m,n,low,high) -- m and n swapped
if not n then
n = 10
end
if not m then
m = 10
end
if not low then
low = 0
end
if not high then
high = 10
end
local t = { }
for i=1,m do
t[i] = { }
end
while true do
for i=1,m do
local ti = t[i]
for j=1,n do
ti[j] = random(low,high)
end
end
if n ~= m or determinant(t,1) ~= 0 then
return t
end
end
end
-- extract submatrix by using
local function submatrix(t,i,j)
local rows = #t
local columns = #t[1]
local sign = 1 -- not used
if i <= rows and j <= columns then
local c = copy(t)
remove(c,i)
for k=1,rows-1 do
remove(c[k],j)
end
return c
else
return "error: out of bound"
end
end
matrix.submatrix = submatrix
-- calculating determinant using Laplace Expansion
function matrix.laplace(t) -- not sure if this is the most effient but
local factors = { 1 } -- it's not used for number crunching anyway
local data = copy(t)
local det = 0
while #data > 0 do
local mat = { }
local siz = #data[1]
if siz == 0 then
return "error: no determinant"
elseif siz == 1 then
det = data[1][1]
return det
end
for i=1,siz do
mat[i] = data[1]
remove(data,1)
end
local factor = remove(factors,1)
local m1 = mat[1]
if siz == 2 then
local m2 = mat[2]
det = det + factor * (m1[1]*m2[2] - m1[2]*m2[1])
else
for j=1,#m1 do
local m1j = m1[j]
if m1j ~= 0 then
insert(factors, (-1)^(j+1) * factor * m1j)
local m = submatrix(mat,1,j)
for k, v in next, m do
insert(data,v)
end
end
end
end
end
return det
end
-- solve the linear equation m X = c
local function solve(m,c)
local n = #m
if n ~= #c then
-- return "error: size mismatch"
return nil
end
local newm = copy(m)
local temp = copy(c)
local solution = copy(c)
for i=1,n do
insert(newm[i],temp[i])
end
newm = uppertri(newm, 0)
for k = n,1,-1 do
local val = 0
local new = newm[k]
for j = k+1, n do
val = val + new[j] * solution[j]
end
if new[k] == 0 then
-- return "error: no unique solution"
return nil
else
solution[k] = (new[n+1] - val)/new[k]
end
end
return solution
end
matrix.solve = solve
-- find the inverse matrix of m
local function inverse(m)
local n = #m
local temp = copy(m)
if n ~= #m[1] then
return temp
end
for i=1,n do
for j=1,n do
insert(temp[i],j == i and 1 or 0)
end
end
temp = rowechelon(temp,1)
for i=1,n do
for j=1,n do
remove(temp[i], 1)
end
end
return temp
end
matrix.inverse = inverse
\stopluacode
\stopmodule
\unexpanded\def\ctxmodulematrix#1{\ctxlua{moduledata.matrix.#1}}
\continueifinputfile{m-matrix.mkiv}
\usemodule[m-matrix]
\usemodule[art-01]
\starttext
\startluacode
document.DemoMatrixA = {
{ 0, 2, 4, -4, 1 },
{ 0, 0, 2, 3, 4 },
{ 2, 2, -6, 2, 4 },
{ 2, 0, -6, 9, 7 },
{ 2, 3, 4, 5, 6 },
{ 6, 6, -6, 6, 6 },
}
document.DemoMatrixB = {
{ 0, 2, 4, -4, 1 },
{ 0, 0, 2, 3, 4 },
{ 2, 2, -6, 3, 4 },
{ 2, 0, -6, 9, 7 },
{ 2, 2, -6, 2, 4 },
}
document.DemoMatrixC = {
{ 3, 3, -1, 3 },
{ -1, 4, 1, 3 },
{ 5, 4, 0, 2 },
{ 2, 4, 0, -1 },
}
\stopluacode
\startbuffer[demo]
\typebuffer
\startalignment[middle]
\dontleavehmode\inlinebuffer
\stopalignment
\stopbuffer
\setuphead[section][before={\testpage[5]\blank[2*big]}]
\startsubject[title={A symbolic matrix}]
\startbuffer
\ctxmodulematrix{typeset(moduledata.matrix.symbolic("a", "m", "n"))}
\ctxmodulematrix{typeset(moduledata.matrix.symbolic("a", "m", "n", 4, 8))}
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Generate a new $m \times n$ matrix}]
\startbuffer
\startluacode
moduledata.matrix.typeset(moduledata.matrix.make(4,3, 0,5))
context.qquad()
moduledata.matrix.typeset(moduledata.matrix.make(5,5,-1,5))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Swap two rows (2 and 4)}]
\startbuffer
\startluacode
moduledata.matrix.typeset(document.DemoMatrixA)
context("$\\qquad \\Rightarrow \\qquad$")
moduledata.matrix.typeset(moduledata.matrix.swaprows(document.DemoMatrixA,2,4))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Swap two columns (2 and 4)}]
\startbuffer
\startluacode
moduledata.matrix.typeset(document.DemoMatrixA)
context("$\\qquad \\Rightarrow \\qquad$")
moduledata.matrix.typeset(moduledata.matrix.swapcolumns(document.DemoMatrixA,2, 4))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Multiply 3 to row 2($3 \times r_2$)}]
\startbuffer
\startluacode
moduledata.matrix.typeset(document.DemoMatrixA)
context("$\\qquad \\Rightarrow \\qquad$")
moduledata.matrix.typeset(moduledata.matrix.multiply(document.DemoMatrixA,2,3))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Add 4 times of row 3 to row 2($r_2 + 4 \times r_3$)}]
\startbuffer
\startluacode
moduledata.matrix.typeset(document.DemoMatrixA)
context("$\\qquad \\Rightarrow \\qquad$")
moduledata.matrix.sumrow(document.DemoMatrixA,2,3,4)
moduledata.matrix.typeset(document.DemoMatrixA)
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Transpose a matrix}]
\startbuffer
\startluacode
moduledata.matrix.typeset(document.DemoMatrixA)
context("$\\qquad \\Rightarrow \\qquad$")
moduledata.matrix.typeset(moduledata.matrix.transpose(document.DemoMatrixA))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={The inner product of two vectors}]
\startbuffer
\startluacode
context("$<1,2,3> \\cdot <3,1,2> \\ =\\ $ ")
context( moduledata.matrix.inner({ 1, 2, 3 }, { 3, 1, 2 }))
\stopluacode
\stopbuffer
\getbuffer[demo]
\startluacode
context("$<1,2,3> \\cdot <3,1,2, 4> \\ =\\ $ ")
context(moduledata.matrix.inner({ 1, 2, 3 }, { 3, 1, 2, 4 }))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={The product of two matrices}]
\startbuffer
\startluacode
context("$\\ $")
moduledata.matrix.typeset(document.DemoMatrixB)
context("$\\cdot$")
moduledata.matrix.typeset(document.DemoMatrixA)
context("$ = $")
moduledata.matrix.typeset(moduledata.matrix.product
(document.DemoMatrixB,document.DemoMatrixB))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={An Upper Triangular Matrix}]
\startbuffer
\startluacode
moduledata.matrix.typeset(document.DemoMatrixB)
context("$\\qquad \\Rightarrow \\qquad$")
moduledata.matrix.typeset(moduledata.matrix.uppertri(document.DemoMatrixB))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Determinant: using triangulation}]
\startbuffer
\startluacode
local m = {
{ 1, 2, 4 },
{ 0, 0, 2 },
{ 2, 2, -6 },
{ 2, 2, -6 },
}
moduledata.matrix.typeset(m, {fences="bars"})
context("$\\qquad = \\qquad$")
moduledata.matrix.determinant(m)
\stopluacode
\stopbuffer
\getbuffer[demo]
\startbuffer
\startluacode
moduledata.matrix.typeset(document.DemoMatrixC, { fences = "bars" })
context("$\\qquad = \\qquad$")
context(moduledata.matrix.determinant(document.DemoMatrixC))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Determinant: using Laplace Expansion}]
\startbuffer
\startluacode
moduledata.matrix.typeset(document.DemoMatrixC, { fences = "bars" })
context("$\\qquad = \\qquad$")
context(moduledata.matrix.laplace(document.DemoMatrixC))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Example of Laplace Expansion using submatrix function}]
\startbuffer
\startluacode
local m = {
{ 1, 5, 4, 2 },
{ 5, 2, 0, 4 },
{ 2, 2, 1, 1 },
{ 1, 0, 0, 5 },
}
local options = {fences = "bars"}
moduledata.matrix.typeset(m,options)
context("\\par $=$")
for j = 1, #m[1] do
local mm = moduledata.matrix.submatrix(m, 1, j)
local factor = (-1)^(1+j) *(m[1][j])
context("\\ ($%d$) \\cdot ", factor)
moduledata.matrix.typeset(mm, options)
if j < #m[1] then
context("\\ $+$ ")
end
end
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Row echelon form}]
\startbuffer
\startluacode
local m = {
{ 1, 3, -2, 0, 2, 0, 0 },
{ 2, 6, -5, -2, 4, -3, -1 },
{ 0, 0, 5, 10, 0, 15, 5 },
{ 2, 6, 0, 8, 4, 18, 6 },
}
moduledata.matrix.typeset(m)
context("$\\Rightarrow$")
moduledata.matrix.typeset(moduledata.matrix.rowechelon(m,1))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Solving linear equation}]
\startbuffer
\startluacode
local m = {
{ 1, 3, -2, 0 },
{ 2, 0, 1, 2 },
{ 6, -5, -2, 4 },
{ -3, -1, 5, 10 },
}
local c = { 5, 2, 6, 8 }
moduledata.matrix.typeset(moduledata.matrix.solve(m,c))
context.blank()
moduledata.matrix.typeset(moduledata.matrix.solve(m,c), { template = 6 })
context.blank()
moduledata.matrix.typeset(moduledata.matrix.solve(m,c), { template = "no" })
context.blank()
moduledata.matrix.typeset(moduledata.matrix.solve(m,c), { template = "%0.3f" })
context.blank()
moduledata.matrix.typeset(moduledata.matrix.solve(m,c), { template = "%0.4F" })
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\startsubject[title={Inverse matrix}]
\startbuffer
\startluacode
local m = {
{ 1, 1, 1 },
{ 0, 2, 3 },
{ 3, 2, 1 },
}
context("$A =\\quad$")
moduledata.matrix.typeset(m)
context("$\\qquad A^{-1} = \\quad$")
moduledata.matrix.typeset(moduledata.matrix.inverse(m))
context("\\blank\\ ")
moduledata.matrix.typeset(m)
context("$\\cdot$")
moduledata.matrix.typeset(moduledata.matrix.inverse(m))
context("$ = $")
moduledata.matrix.typeset(moduledata.matrix.product(m, moduledata.matrix.inverse(m)))
\stopluacode
\stopbuffer
\getbuffer[demo]
\stopsubject
\stoptext
|