Debian Codegraph @

texmf-dist/doc/latex/tkz-elements/TKZdoc-elements-classes-matrices.tex

raw
1 \newpage\section{Class \tkzClass{matrix}}
2
3 The variable \tkzVar{matrix}{M} holds a table used to store matrices. It is optional, and you are free to choose the variable name. However, using \code{M} is a recommended convention for clarity and consistency. If you use a custom variable (e.g., Matrices), you must initialize it manually.\\
4 The \code{init\_elements()} function reinitializes the \code{M} table if used.
5
6 \vspace{1em}
7
8 The \code{matrix} class is currently experimental, and its attribute and method names have not yet been finalized, indicating that this class is still evolving. Certain connections have been made with other classes, such as the \code{point} class. Additionally, a new attribute, \code{mtx}, has been included, associating a column matrix with the point, where the elements correspond to the point's coordinates in the original base. Similarly, an attribute has been added to the \code{vector} class, where \code{mtx} represents a column matrix consisting of the two affixes that compose the vector.
9
10 This \code{matrix} class has been created to avoid the need for an external library, and has been adapted to plane transformations. It allows you to use complex numbers.
11
12 \lefthand\ To display matrices, you'll need to load the \tkzNamePack{amsmath} package.
13
14 {\color{red}\lefthand\ } While some methods are valid for any matrix size, the majority are reserved for square matrices of order 2 and 3.
15
16
17 \subsection{Matrix creation}
18 \label{sub:matrix_creation}
19
20 The creation of a matrix is the result of numerous possibilities. Let's take a look at the different cases
21
22 The first one is to use an array of arrays, that is, a table wherein each element is another table. For instance, you can create a matrix of zeros with dimensions N by M with the following code:
23
24
25 \begin{itemize}
26
27 \item The first method is: [\ref{ssub:method_new}]. This function is the most important, as it's the one that creates an object. The other functions create specific objects and always use this function.
28
29 The \tkzClass{matrix} class represents a $2\times2$ matrix defined by four values.
30
31 \begin{mybox}
32 \begin{verbatim}
33 M = matrix:new(1, 0, 0, 1) -- identity matrix
34 \end{verbatim}
35 \end{mybox}
36
37 \texttt{Short form: }
38
39 A more concise form is available:
40
41 \begin{mybox}
42 \begin{verbatim}
43 M = matrix(1, 0, 0, 1)
44 \end{verbatim}
45 \end{mybox}
46
47
48 \begin{mybox}
49 | M.new = matrix({ { a, b }, { c, d } }) | \\
50 a, b, c, et d being real or complex numbers.
51 \end{mybox}
52
53 \begin{minipage}{.3\textwidth}
54 \directlua{
55 init_elements()
56 local a, b, c, d = 1, 2, 3, 4
57 M.new = matrix({ { a, b }, { c, d } })
58 tex.print('M = ') M.new:print()}
59 \end{minipage}
60 \begin{minipage}{.6\textwidth}
61 \begin{tkzexample}[code only]
62 \directlua{
63 init_elements()
64 local a, b, c, d = 1, 2, 3, 4
65 M.new = matrix({ { a, b }, { c, d } })
66 tex.print('M = ') M.new:print()}
67 \end{tkzexample}
68 \end{minipage}
69
70
71 \item With the function \code{create}, you get a matrix whose coefficients are all zero, with a number of columns and rows of your choice. [\ref{ssub:function_matrix_create_n_m}]
72
73 \begin{mybox}
74 |M.cr = matrix.create(4,5)|
75 \end{mybox}
76
77 \begin{tkzexample}[latex=.5\textwidth]
78 \directlua{
79 init_elements()
80 M.cr = matrix.create(4, 5)
81 tex.print('M = ') M.cr:print()}
82 \end{tkzexample}
83
84
85 \item The identity matrix of size
86 {$\displaystyle n$} is the {$\displaystyle n\times n$} square matrix with ones on the main diagonal and zeros elsewhere. See [\ref{ssub:method_identity}]
87
88 \begin{mybox}
89 |M.I = matrix.identity(3)|
90 \end{mybox}
91
92
93 \begin{tkzexample}[latex=.5\textwidth]
94 \directlua{
95 init_elements()
96 M.I = matrix.identity(3)
97 tex.print('$I_3 = $') M.I:print()}
98 \end{tkzexample}
99
100
101 \item It is also possible to obtain a square matrix with: [\ref{ssub:method_square}]
102 \begin{mybox}
103 |M.sq = matrix.square (2,a,b,c,d)|
104 \end{mybox}
105
106 \begin{tkzexample}[latex=.5\textwidth]
107 \directlua{
108 init_elements()
109 local a, b, c, d = 1, 2, 3, 4
110 M.sq = matrix.square(2, a, b, c, d)
111 tex.print('M = ') M.sq:print()}
112 \end{tkzexample}
113
114
115 \item In the case of a column vector: [\ref{ssub:method_vector}]
116
117 \begin{mybox}
118 | M.V = matrix.vector(1, 2, 3)|
119 also possible |M.V = matrix.column(1, 2, 3)|
120 \end{mybox}
121
122
123 \begin{tkzexample}[latex=.5\textwidth]
124 \directlua{
125 init_elements()
126 M.V = matrix.vector(1, 2, 3)
127 tex.print('V = ') M.V:print()}
128 \end{tkzexample}
129
130
131 \item In the case of a row vector: [\ref{ssub:function_row_vector}]
132
133 \begin{mybox}
134 | M.V = matrix.row_vector(1, 2, 3)|
135 \end{mybox}
136
137 \begin{tkzexample}[latex=.5\textwidth]
138 \directlua{
139 init_elements()
140 M.V = matrix.row_vector(1, 2, 3)
141 tex.print('V = ') M.V:print()}
142 \end{tkzexample}
143
144
145 \item Matrix associated with a point
146
147 |M.p = matrix({ { p.re }, { p.im } })|
148
149 \item Matrix associated with a vector
150
151 It's a column matrix made up of the affixes of the two points defining the vector.
152
153 | local M.v = matrix{ { za }, { zb } }|
154
155 \begin{tkzexample}[latex=.5\textwidth]
156 \directlua{
157 z.A = point(1, 2)
158 z.B = point(3, 4)
159 V.u = vector(z.A, z.B)
160 V.u.mtx:print()}
161 \end{tkzexample}
162
163
164
165 \item Homogeneous transformation matrix [\ref{ssub:method_htm}]
166
167 The objective is to generate a matrix with homogeneous coordinates capable of transforming a coordinate system through rotation, translation, and scaling. To achieve this, it is necessary to define both the rotation angle, the coordinates of the new origin ans the scaling factors.
168
169 \begin{tkzexample}[latex=.35\textwidth]
170 \directlua{
171 init_elements()
172 M.h = matrix.htm(math.pi / 3, 1, 2, 2, 1)
173 tex.print('H = ') M.h:print()}
174 \end{tkzexample}
175
176 \end{itemize}
177
178 \subsection{Method \tkzMeth{matrix}{print()}}
179 \label{sub:display_a_matrix_method_print}
180
181
182 This method (See \ref{ssub:method_print}) is necessary to control the results, so here are a few explanations on how to use it. It can be used on real or complex matrices, square or not. A few options allow you to format the results. You need to load the \tkzNamePack{amsmath} package to use the "print" method. Without this package, it is possible to display the contents of the matrix without formatting with |print_array (M)|
183
184 \begin{tkzexample}[latex=.5\textwidth]
185 \directlua{
186 init_elements()
187 M.new = matrix { { 1, -1}, { 2, 0 } }
188 M.new:print()}
189 \end{tkzexample}
190
191 \subsection{Attibutes of a matrix}
192 \label{sub:attibutes_of_a_matrix}
193
194 \begin{center}
195 \bgroup
196 \catcode`_=12
197 \small
198 \captionof{table}{Matrix attributes.}\label{matrix:attributes}
199 \begin{tabular}{ll}
200 \toprule
201 \textbf{Attributes} & \texttt{Reference} \\
202 \midrule
203 \tkzAttr{matrix}{set} & [\ref{sub:attribute_set}]\\
204 \tkzAttr{matrix}{rows} & [\ref{ssub:attributes_matrix_rows_and_cols}] \\
205 \tkzAttr{matrix}{cols} & [\ref{ssub:attributes_matrix_rows_and_cols}] \\
206 \tkzAttr{matrix}{type} & \\
207 \tkzAttr{matrix}{det} & [\ref{ssub:attributes_matrix_det}]\\
208 \bottomrule %
209 \end{tabular}
210 \egroup
211 \end{center}
212
213
214
215 \subsubsection{Attribute \tkzAttr{matrix}{type}}
216 \label{ssub:attribute_iattr_matrix_type}
217 \begin{mybox}
218 |M.new = matrix{ { 1, 1}, { 0, 2} } |
219 |A = { { 1, 1 }, { 0, 2 } } |
220 \end{mybox}
221
222 \code{M} is a matrix (and therefore a table) whereas A is a table. Thus \code{M.type} gives \code{'matrix'} and \code{A.type = nil}. \code{type(A) or type(M) = table}.
223
224 \subsubsection{Attribute \tkzAttr{matrix}{set}}
225 \label{sub:attribute_set}
226 A simple array such as |{{1,2},{2,-1}}| is often considered a \code{matrix}. In \tkzNamePack{tkz-elements}, we'll consider |M.new| defined by
227
228 |matrix({ { 1, 1 }, { 0, 2 } })|
229
230 as a matrix and |M.new.set| as an array (|M.new.set = { { 1, 1 }, {0, 2 } }|).
231
232 You can access a particular element of the matrix, for example: |M.new.set[2][1]| gives \tkzUseLua{M.new.set[2][2]}.
233
234 |\tkzUseLua{M.new.set[2][1]}| is the expression that displays $2$.
235
236 \subsubsection{Attributes \tkzAttr{matrix}{rows} and \tkzAttr{matrix}{cols}}
237 \label{ssub:attributes_matrix_rows_and_cols}
238
239 The number of rows is accessed with |M.n.rows| and the number of columns with |M.n.cols|, here's an example:
240
241 \vspace{.5em}
242 \begin{minipage}{.5\textwidth}
243 \begin{verbatim}
244 \directlua{
245 init_elements()
246 M.n = matrix({ { 1, 2, 3 }, { 4, 5, 6 } })
247 M.n:print()
248 tex.print("Rows: "..M.n.rows)
249 tex.print("Cols: "..M.n.cols)}
250 \end{verbatim}
251 \end{minipage}
252 \begin{minipage}{.5\textwidth}
253 \directlua{
254 init_elements()
255 M.n = matrix({ { 1, 2, 3 }, { 4, 5, 6 } })
256 M.n:print()
257 tex.print("Rows: "..M.n.rows)
258 tex.print("Cols: "..M.n.cols)}
259 \end{minipage}
260
261 \subsubsection{Attributes \tkzAttr{matrix}{det} }
262 \label{ssub:attributes_matrix_det}
263 Give the determinant of the matrix if it is square, otherwise it is \code{nil}. The coefficients of the matrix can be complex numbers.
264
265 \vspace{.5em}
266 \begin{minipage}{.6\textwidth}
267 \begin{verbatim}
268 \directlua{
269 init_elements()
270 M.s = matrix.square(3, 1, 1, 0, 2, -1, -2, 1, -1, 2)
271 M.s:print()
272 tex.print ('\\\\')
273 tex.print ("Its determinant is: " .. M.s.det)
274 }
275 \end{verbatim}
276 \end{minipage}
277 \begin{minipage}{.4\textwidth}
278 \directlua{
279 init_elements()
280 M.s = matrix.square(3, 1, 1, 0, 2, -1, -2, 1, -1, 2)
281 M.s:print()
282 tex.print('\\\\')
283 tex.print("Its determinant is: "..M.s.det)
284 tex.print('\\\\')
285 local a = point(1, -2)
286 local b = point(0, 1)
287 local c = point(1, 1)
288 local d = point(1, -1)
289 M.A = matrix({ { a, b }, {c, d } })
290 tex.print ("Its determinant is: "..tostring(M.A.det))}
291 \end{minipage}
292
293 \subsection{Metamethods for the matrices}
294
295 Conditions on matrices must be valid for certain operations to be possible.
296
297 \begin{center}
298 \bgroup
299 \catcode`_=12
300 \small
301 \captionof{table}{Matrix metamethods.}\label{matrix:metamethods}
302 \begin{tabular}{ll}
303 \toprule
304 \textbf{Metamethods} & \textbf{Refrence} \\
305 \midrule
306 \tkzMeta{matrix}{add(M1,M2)} & See [\ref{ssub:addition_of_matrices}] \\
307 \tkzMeta{matrix}{sub(M1,M2)} & See [\ref{ssub:addition_of_matrices}] \\
308 \tkzMeta{matrix}{unm(M} & |- M| \\
309 \tkzMeta{matrix}{mul(M1,M2)} & [\ref{ssub:multiplication_of_matrices}] \\
310 \tkzMeta{matrix}{pow(M,n)} & [\ref{ssub:multiplication_of_matrices}] \\
311 \tkzMeta{matrix}{tostring(M,n)} & displays the matrix \\
312 \tkzMeta{matrix}{eq(M1,M2)} & true or false \\
313 \bottomrule
314 \end{tabular}
315 \egroup
316 \end{center}
317
318
319
320 \subsubsection{Addition and subtraction of matrices}
321 \label{ssub:addition_of_matrices}
322 To simplify the entries, I've used a few functions to simplify the displays.
323
324 \vspace{.5em}
325 \begin{minipage}{.6\textwidth}
326 \begin{verbatim}
327 \directlua{
328 init_elements()
329 M.A = matrix({ { 1, 2 }, { 2 , -1 } })
330 M.B = matrix({ { -1, 0}, { 1, 3 } })
331 S = M.A + M.B
332 D = M.A - M.B
333 dsp(M.A,'A')
334 nl() nl()
335 dsp(M.B,'B')
336 nl() nl()
337 dsp(M.S,'S') sym(" = ")
338 dsp(M.A) sym(' + ') dsp(M.B)
339 nl() nl()
340 dsp(M.D,'D') sym(" = ")
341 dsp(M.A) sym(' - ') dsp(M.B)
342 }
343 \end{verbatim}
344 \end{minipage}
345 \begin{minipage}{.4\textwidth}
346 \directlua{
347 init_elements()
348
349 local function dsp (M,name)
350 if name then
351 tex.print(name..' = ')print_matrix(M)
352 else
353 print_matrix(M)
354 end
355 end
356
357 local function sym(s)
358 tex.print(' '..s..' ')
359 end
360
361 local function nl()
362 tex.print('\\\\')
363 end
364
365 M.A = matrix({ { 1, 2 }, { 2 , -1 } })
366 M.B = matrix({ { -1, 0}, { 1, 3 } })
367 M.S = M.A + M.B
368 M.D = M.A - M.B
369 dsp(M.A,'A')
370 nl() nl()
371 dsp(M.B,'B')
372 nl() nl()
373 dsp(M.S,'S') sym(" = ") dsp(M.A) sym(' + ') dsp(M.B)
374 nl() nl()
375 dsp(M.D,'D') sym(" = ") dsp(M.A) sym(' - ') dsp(M.B)}
376 \end{minipage}
377
378 \subsubsection{Multiplication and power of matrices}
379 \label{ssub:multiplication_of_matrices}
380 To simplify the entries, I've used a few functions. You can find their definitions in the sources section of this documentation. n integer > or < 0 or |'T'|
381
382 \begin{minipage}{.5\textwidth}
383 \begin{verbatim}
384 \directlua{
385 init_elements()
386 M.A = matrix({ { 1, 2 }, { 2 ,-1 } })
387 M.B = matrix({ { -1, 0 }, { 1, 3 } })
388 M.P = M.A * M.B
389 M.I = M.A ^ -1
390 M.C = M.A ^ 3
391 M.K = 2 * M.A}
392 \end{verbatim}
393 \end{minipage}
394 \begin{minipage}{.5\textwidth}
395 \directlua{
396 init_elements()
397
398 local function dsp (M,name)
399 if name then
400 tex.print(name..' = ')print_matrix(M)
401 else print_matrix(M) end
402 end
403
404 local function sym (s)
405 tex.print(' '..s..' ')
406 end
407
408 local function nl ()
409 tex.print('\\\\')
410 end
411
412 M.A = matrix({ { 1, 2 }, { 2 ,-1 } })
413 M.B = matrix({ { -1, 0 }, { 1, 3 } })
414 M.P = M.A * M.B
415 M.I = M.A ^ -1
416 M.C = M.A ^ 3
417 M.K = 2 * M.A
418 dsp(M.P,'P') sym(" = ") dsp(M.A) sym(' * ') dsp(M.B)
419 nl() nl()
420 dsp(M.A ^ -1,'$A^{-1}$')
421 nl() nl()
422 dsp(M.K,'K')}
423 \end{minipage}
424
425
426 \subsubsection{Metamethod \code{eq}}
427 \label{ssub:metamthod_eq}
428
429 Tests whether two matrices are equal. The result is \textbf{true} or \textbf{false}.
430
431 \subsection{Methods of the class matrix}
432
433 \begin{center}
434 \bgroup
435 \catcode`_=12
436 \small
437 \captionof{table}{Matrix functions and methods.}\label{matrix:methods}
438 \begin{tabular}{lll}
439 \toprule
440 \textbf{Functions} & \texttt{Reference} & \\
441 \midrule
442 \tkzFct{matrix}{new(...)}
443 & See [\ref{ssub:method_new}; \ref{sub:matrix_creation}] & \\
444 \tkzFct{matrix}{matrix.square()}
445 & [\ref{ssub:method_square}] & \\
446 \tkzFct{matrix}{matrix.vector()}
447 & [\ref{ssub:method_vector}] & \\
448 \tkzFct{matrix}{matrix.row\_vector()}
449 & [\ref{ssub:function_row_vector}] & \\
450 \tkzFct{matrix}{matrix.create()}
451 & & \\
452 \tkzFct{matrix}{matrix.identity()}
453 & [\ref{ssub:method_identity}] & \\
454 \tkzFct{matrix}{matrix.htm()}
455 & [\ref{ssub:method_htm}] & \\
456 \midrule
457 \textbf{Methods} & \texttt{Reference} & \\
458 \midrule
459 \tkzMeth{matrix}{print(s,n)}
460 & & \\
461 \tkzMeth{matrix}{htm\_apply(...)}
462 & [\ref{ssub:method_code_htm__apply}] & \\
463 \tkzMeth{matrix}{get\_htm\_point()}
464 & [\ref{ssub:method_get_htm_point}] & \\
465 \tkzMeth{matrix}{get()}
466 & [\ref{ssub:get_an_element_of_a_matrix}] & \\
467 \tkzMeth{matrix}{inverse()}
468 & [\ref{ssub:inverse_matrix}] & \\
469 \tkzMeth{matrix}{adjugate()}
470 & [\ref{ssub:method_adjugate}] & \\
471 \tkzMeth{matrix}{transpose()}
472 & [\ref{ssub:transpose_matrix}] & \\
473 \tkzMeth{matrix}{is\_diagonal()}
474 & [\ref{ssub:method_is_diagonal}] & \\
475 \tkzMeth{matrix}{is\_orthogonal()}
476 & [\ref{ssub:method_is_orthogonal}] & \\
477 \tkzMeth{matrix}{homogenization()}
478 & [\ref{ssub:method_homogenization}] & \\
479 \tkzMeth{matrix}{gauss\_jordan()}
480 & [\ref{ssub:method_matrix_gauss_jordan}] & \\
481 \tkzMeth{matrix}{rank()}
482 & [\ref{ssub:method_matrix_rank}] & \\
483 \tkzMeth{matrix}{augment\_right(B)}
484 & [\ref{ssub:matrix-augmentation-submatrix}] & \\
485 \tkzMeth{matrix}{submatrix(r1,r2,c1,c2)}
486 & [\ref{ssub:matrix-augmentation-submatrix}] & \\
487 \bottomrule
488 \end{tabular}
489 \egroup
490 \end{center}
491
492
493
494
495 \subsubsection{Method \tkzMeth{matrix}{print}}
496 \label{ssub:method_print}
497
498 With the \tkzNamePack{amsmath} package loaded, this method can be used. By default, the \tkzEnv{latex}{bmatrix} environment is selected, although you can choose from \tkzEnv{latex}{matrix}, \tkzEnv{latex}{pmatrix}, \tkzEnv{latex}{Bmatrix}, "vmatrix", "Vmatrix". Another option lets you set the number of digits after the decimal point. The "tkz\_dc" global variable is used to set the number of decimal places. Here's an example:
499
500 \vspace{.5em}
501 \begin{verbatim}
502 \directlua{
503 init_elements()
504 M.n = matrix({ { math.sqrt(2), math.sqrt(3) }, { math.sqrt(4), math.sqrt(5) } })
505 M.n:print('pmatrix')}
506 \end{verbatim}
507
508 \directlua{
509 init_elements()
510 M.n = matrix({ { math.sqrt(2), math.sqrt(3) }, { math.sqrt(4), math.sqrt(5) } })
511 tkz_dc = 3
512 M.n:print('pmatrix')
513 }
514
515
516 \vspace{.5em}
517 You can also display the matrix as a simple array using the |print_array (M)| function. see the next example.
518
519 In the case of a square matrix, it is possible to transmit a list of values whose first element is the order of the matrix.
520
521 \vspace{.5em}
522 \begin{minipage}{.5\textwidth}
523 \begin{verbatim}
524 \directlua{
525 init_elements()
526 M.s = matrix.square(2, 1, 0, 0, 2)
527 M.s:print()}
528 \end{verbatim}
529 \end{minipage}
530 \begin{minipage}{.5\textwidth}
531 \directlua{
532 init_elements()
533 M.s = matrix.square(2,1,0,0,2)
534 M.s:print()}
535 \end{minipage}
536
537 \subsubsection{Function \tkzFct{matrix}{new}}
538 \label{ssub:method_new}
539
540 This is the main method for creating a matrix. Here's an example of a 2x3 matrix with complex coefficients:
541
542 \vspace{.5em}
543 \begin{minipage}{.5\textwidth}
544 \begin{verbatim}
545 \directlua{
546 init_elements()
547 a = point(1, 0)
548 b = point(1, 1)
549 c = point(-1, 1)
550 d = point(0, 1)
551 e = point(1, -1)
552 f = point(0, -1)
553 M.n = matrix({ { a, b, c }, { d, e, f } })
554 M.n:print()}
555 \end{verbatim}
556 \end{minipage}
557 \begin{minipage}{.5\textwidth}
558 \directlua{
559 init_elements()
560 a = point(1, 0)
561 b = point(1, 1)
562 c = point(-1, 1)
563 d = point(0, 1)
564 e = point(1, -1)
565 f = point(0, -1)
566 M.n = matrix({ { a, b, c }, { d, e, f } })
567 M.n:print()}
568 \end{minipage}
569
570 \subsubsection{Function \tkzFct{matrix}{matrix.vector}}
571 \label{ssub:method_vector}
572
573 The special case of a column matrix, frequently used to represent a vector, can be treated as follows:
574
575 \vspace{.5em}
576 \begin{minipage}{.5\textwidth}
577 \begin{verbatim}
578 \directlua{
579 init_elements()
580 M.v = matrix.vector(1, 2, 3)
581 M.v:print()}
582 \end{verbatim}
583 \end{minipage}
584 \begin{minipage}{.5\textwidth}
585 \directlua{
586 init_elements()
587 M.v = matrix.vector(1, 2, 3)
588 M.v:print()}
589 \end{minipage}
590
591 \subsubsection{Function \tkzFct{matrix}{matrix.row\_vector}}
592 \label{ssub:function_row_vector}
593
594 \begin{mybox}
595 \code{ M.rv = matrix.row\_vector (1, 2, 3)}
596
597 m.rv = \directlua{matrix.row_vector (1, 2, 3):print()}
598 \end{mybox}
599
600 \subsubsection{Function \tkzFct{matrix}{matrix.create(n,m)}}
601 \label{ssub:function_matrix_create_n_m}
602
603 \begin{mybox}
604 \code{ M.c = matrix.create (2, 3)}
605
606 M.c = \directlua{matrix.create (2, 3):print()}
607 \end{mybox}
608
609 \subsubsection{Function \tkzFct{matrix}{matrix.square}(liste)}
610 \label{ssub:method_square}
611
612 We have already seen this method in the presentation of matrices. We first need to give the order of the matrix, then the coefficients, row by row.
613
614 \begin{minipage}{.5\textwidth}
615 \begin{verbatim}
616 \directlua{
617 init_elements()
618 M.s = matrix.square(2, 2, 3, -5, 4)
619 M.s:print()}
620 \end{verbatim}
621 \end{minipage}
622 \begin{minipage}{.5\textwidth}
623 \directlua{
624 init_elements()
625 M.s = matrix.square(2, 2, 3, -5, 4)
626 M.s:print()}
627 \end{minipage}
628
629 \subsubsection{Function \tkzFct{matrix}{matrix.identity}}
630 \label{ssub:method_identity}
631
632 Creating the identity matrix order 3
633
634
635 \begin{minipage}{.5\textwidth}
636 \begin{verbatim}
637 \directlua{
638 init_elements()
639 M.Id_3 = matrix.identity(3)
640 M.Id_3:print()}
641 \end{verbatim}
642 \end{minipage}
643 \begin{minipage}{.5\textwidth}
644 \directlua{
645 init_elements()
646 M.Id_3 = matrix.identity(3)
647 M.Id_3:print()}
648 \end{minipage}
649
650 \subsubsection{Method \tkzMeth{matrix}{is\_orthogonal}}
651 \label{ssub:method_is_orthogonal}
652
653 The method returns \code{true} if the matrix is orthogonal and \code{false} otherwise.
654
655 \begin{verbatim}
656 \directlua{
657 init_elements()
658 local cos = math.cos
659 local sin = math.sin
660 local pi = math.pi
661 M.A = matrix({ { cos(pi / 6), -sin(pi / 6) }, { sin(pi / 6), cos(pi / 6) } })
662 M.A:print()
663 bool = M.A:is_orthogonal()
664 tex.print("\\\\")
665 if bool then
666 tex.print("The matrix is orthogonal")
667 else
668 tex.print("The matrix is not orthogonal")
669 end
670 tex.print("\\\\")
671 tex.print("Test: $M.A^T = M.A^{-1} ?$")
672 print_matrix(transposeMatrix(M.A))
673 tex.print("=")
674 inv_matrix(M.A):print()}
675 \end{verbatim}
676
677 \directlua{
678 init_elements()
679 local cos = math.cos
680 local sin = math.sin
681 local pi = math.pi
682 M.A = matrix({{cos(pi / 6), -sin(pi / 6)}, {sin(pi / 6), cos(pi / 6)}})
683 M.A:print()
684 bool = M.A:is_orthogonal()
685 tex.print("\\\\")
686 if bool then
687 tex.print("The matrix is orthogonal")
688 else
689 tex.print("The matrix is not orthogonal")
690 end
691 tex.print("\\\\")
692 tex.print("Test: $M.A^T = M.A^{-1} ?$")
693 print_matrix(transposeMatrix(M.A))
694 tex.print("=")
695 inv_matrix(M.A):print()}
696
697 \subsubsection{Method \tkzMeth{matrix}{is\_diagonal}}
698 \label{ssub:method_is_diagonal}
699
700 The method returns \code{true} if the matrix is diagonal and \code{false} otherwise.
701
702 \subsubsection{Function \tkzFct{matrix}{print\_array}}
703 \label{ssub:display_a_table_or_array_function_code_print_array}
704
705 We'll need to display results, so let's look at the different ways of displaying them, and distinguish the differences between arrays and matrices.
706
707 Below, $A$ is an array. It can be displayed as a simple array or as a matrix, but we can't use the attributes and |A:print()| is not possible because $A$ is not an object of the class \code{matrix}. If you want to display an array like a matrix you can use the function |print_matrix| (see the next example).
708
709 \vspace{.5em}
710 \begin{minipage}{.5\textwidth}
711 \begin{verbatim}
712 \directlua{
713 init_elements()
714 A = { { 1, 2 }, { 1, -1 } }
715 tex.print("A = ")
716 print_array(A)
717 tex.print(" or ")
718 print_matrix(A)
719 M.A = matrix({ { 1, 1 }, { 0, 2 } })
720 tex.print("\\\\")
721 tex.print("M = ")
722 M.A:print()}
723 \end{verbatim}
724 \end{minipage}
725 \begin{minipage}{.5\textwidth}
726 \directlua{
727 init_elements()
728 A = { { 1, 2 }, { 1, -1 } }
729 tex.print("A = ")
730 print_array(A)
731 tex.print(" or ")
732 print_matrix(A)
733 M.A = matrix({ { 1, 1 }, { 0, 2 } })
734 tex.print("\\\\")
735 tex.print("M = ")
736 M.A:print()}
737 \end{minipage}
738
739 \subsubsection{Method \tkzMeth{matrix}{get}}
740 \label{ssub:get_an_element_of_a_matrix}
741 Get an element of a matrix.
742
743
744 \begin{minipage}{.5\textwidth}
745 \begin{verbatim}
746 \directlua{
747 init_elements()
748 M.n = matrix{ { 1, 2 }, { 2, -1 } }
749 S = M.n:get(1, 1) + M.n:get(2, 2)
750 tex.print(S)}
751 \end{verbatim}
752 \end{minipage}
753 \begin{minipage}{.5\textwidth}
754 \directlua{
755 init_elements()
756 M.n = matrix{ { 1, 2 }, { 2, -1 } }
757 S = M.n:get(1, 1) + M.n:get(2, 2)
758 tex.print(S)}
759 \end{minipage}
760
761 \subsubsection{Method \tkzMeth{matrix}{inverse}}
762 \label{ssub:inverse_matrix}
763
764 \begin{minipage}{.6\textwidth}
765 \begin{verbatim}
766 \directlua{
767 init_elements()
768 M.A = matrix({ { 1, 2 }, { 2, -1 } })
769 tex.print("Inverse of $A = $")
770 M.B = M.A:inverse()
771 M.B:print()}
772 \end{verbatim}
773 \end{minipage}
774 \begin{minipage}{.4\textwidth}
775 \directlua{
776 init_elements()
777 M.A = matrix({ { 1, 2 }, { 2, -1 } })
778 tex.print("Inverse of $A = $")
779 M.B = M.A:inverse()
780 M.B:print()}
781 \end{minipage}
782
783 \subsubsection{Inverse matrix with power syntax}
784 \label{ssub:inverse_matrix_with_power_syntax}
785
786 \begin{minipage}{.6\textwidth}
787 \begin{verbatim}
788 \directlua{
789 init_elements()
790 M.n = matrix({ { 1, 0, 1 }, { 1, 2, 1 }, { 0, -1, 2 } })
791 tex.print("$M = $") print_matrix (M.n)
792 tex.print('\\\\')
793 tex.print("Inverse of $M = M^{-1}$")
794 tex.print('\\\\','=') print_matrix(M.n ^ -1)}
795 \end{verbatim}
796 \end{minipage}
797 \begin{minipage}{.4\textwidth}
798 \directlua{
799 init_elements()
800 M.n = matrix({ { 1, 0, 1 }, { 1, 2, 1 }, { 0, -1, 2 } })
801 tex.print("$M = $") print_matrix (M.n)
802 tex.print('\\\\')
803 tex.print("$M = M^{-1} = $")
804 print_matrix(M.n ^ -1)}
805 \end{minipage}
806
807 \subsubsection{Method \tkzMeth{matrix}{transpose}}
808 \label{ssub:transpose_matrix}
809
810 A transposed matrix can be accessed with |A: transpose ()| or with |A^{'T'}|.
811
812 \vspace{.5em}
813 \begin{minipage}{.6\textwidth}
814 \begin{verbatim}
815 \directlua{
816 init_elements()
817 M.A = matrix({ { 1, 2 }, { 2, -1 } })
818 M.AT = M.A:transpose()
819 tex.print("$A^{'T'} = $")
820 M.AT:print()}
821 \end{verbatim}
822 \end{minipage}
823 \begin{minipage}{.4\textwidth}
824 \directlua{
825 init_elements()
826 M.A = matrix({ { 1, 2 }, { 2, -1 } })
827 M.AT = M.A:transpose()
828 tex.print("$A^{'T'} = $")
829 M.AT:print()}
830 \end{minipage}
831
832 \vspace{.5em}
833 Remark: |(A ^'T')^'T' = A|
834
835 \subsubsection{Method \tkzMeth{matrix}{adjugate}}
836 \label{ssub:method_adjugate}
837
838 \begin{minipage}{.6\textwidth}
839 \begin{verbatim}
840 \directlua{
841 init_elements()
842 M.N = matrix({ {1, 0, 3}, {2, 1, 0},
843 {-1, 2, 0} })
844 tex.print('N = ') print_matrix(M.N)
845 tex.print('\\\\')
846 M.N.a = M.N:adjugate()
847 M.N.i = M.N * M.N.a
848 tex.print('adj(M) = ') M.N.a:print()
849 tex.print('\\\\')
850 tex.print('N $\\times$ adj(N) = ')
851 print_matrix(M.N.i)
852 tex.print('\\\\')
853 tex.print('det(N) = ')
854 tex.print(M.N.det)}
855 \end{verbatim}
856 \end{minipage}
857 \begin{minipage}{.4\textwidth}
858 \directlua{
859 init_elements()
860 M.N = matrix({ {1, 0, 3}, {2, 1, 0}, {-1, 2, 0} })
861 tex.print('N = ') print_matrix(M.N)
862 tex.print('\\\\')
863 M.N.a = M.N:adjugate()
864 M.N.i = M.N * M.N.a
865 tex.print('adj(M) = ') M.N.a:print()
866 tex.print('\\\\')
867 tex.print('N $\\times$ adj(N) = ') print_matrix(M.N.i)
868 tex.print('\\\\')
869 tex.print('det(N) = ') tex.print(M.N.det)}
870 \end{minipage}
871 \newpage
872
873
874 \subsubsection{Method \tkzMeth{matrix}{diagonalize}}
875 \label{ssub:diagonalization}
876
877 For the moment, this method only concerns matrices of order 2.
878
879 \begin{minipage}{.5\textwidth}
880 \begin{verbatim}
881 \directlua{
882 init_elements()
883 M.A = matrix({ { 5, -3 }, { 6, -4 } })
884 tex.print("A = ")
885 M.A:print()
886 M.D, M.P = M.A:diagonalize()
887 tex.print("D = ")
888 M.D:print()
889 tex.print("P = ")
890 M.P:print()
891 M.R = M.P ^ -1 * M.A * M.P
892 tex.print("\\\\")
893 tex.print("Test: $D = P^{-1}AP = $ ")
894 M.R:print()
895 tex.print("\\\\")
896 tex.print("Verification: $P^{-1}P = $ ")
897 M.T = M.P ^ -1 * M.P
898 M.T:print()}
899 \end{verbatim}
900 \end{minipage}
901 \begin{minipage}{.5\textwidth}
902 \directlua{
903 init_elements()
904 M.A = matrix({ { 5, -3 }, { 6, -4 } })
905 tex.print("A = ")
906 M.A:print()
907 M.D, M.P = M.A:diagonalize()
908 tex.print("D = ")
909 M.D:print()
910 tex.print("P = ")
911 M.P:print()
912 M.R = M.P ^ -1 * M.A * M.P
913 tex.print("\\\\")
914 tex.print("Test: $D = P^{-1}AP = $ ")
915 M.R:print()
916 tex.print("\\\\")
917 tex.print("Verification: $P^{-1}P = $ ")
918 M.T = M.P ^ -1 * M.P
919 M.T:print()}
920 \end{minipage}
921
922 \subsubsection{Method \tkzMeth{matrix}{homogenization}}
923 \label{ssub:method_homogenization}
924
925 The goal of \code{homogenization} is to to be able to use a homogeneous transformation matrix
926
927 Let's take a point $A$ such that |z.A = point(2,-1)|. In order to apply a \code{htm} matrix, we need to perform a few operations on this point. The first is to determine the vector (matrix) associated with the point. This is straightforward, since there's a point attribute called \code{mtx} which gives this vector:
928
929 \begin{mybox}
930 z.A = point(2,0)\\
931 M.V = z.A.mtx:homogenization()
932 \end{mybox}
933 which gives:
934
935 \begin{minipage}{.5\textwidth}
936 \begin{verbatim}
937 \directlua{
938 init_elements()
939 pi = math.pi
940 M.h = matrix.htm(pi / 4, 3, 1)
941 z.A = point(2, 0)
942 M.V = z.A.mtx:homogenization()
943 z.A.mtx:print()
944 tex.print("then after homogenization: ")
945 M.V:print()}
946 \end{verbatim}
947 \end{minipage}
948 \begin{minipage}{.5\textwidth}
949 \directlua{
950 init_elements()
951 pi = math.pi
952 M.h = matrix.htm(pi / 4, 3, 1)
953 z.A = point(2, 0)
954 M.V = z.A.mtx:homogenization()
955 z.A.mtx:print()
956 tex.print("then after homogenization: ")
957 M.V:print()}
958 \end{minipage}
959
960 \subsubsection{Function \tkzFct{matrix}{matrix.htm}}
961 \label{ssub:method_htm}
962 Homogeneous transformation matrix.
963
964 There are several ways of using this transformation. First, we need to create a matrix that can associate a rotation with a translation.
965
966 The main method is to create the matrix:
967
968 \begin{mybox}
969 pi = math.pi\\
970 M.h = matrix.htm(pi / 4, 3, 1)
971 \end{mybox}
972
973 A 3x3 matrix is created which combines a $\pi/4$ rotation and a $\overrightarrow{t}=(3,1)$ translation.
974
975 \directlua{
976 init_elements()
977 pi = math.pi
978 M.h = matrix.htm(pi / 4, 3, 1)
979 M.h:print()
980 }
981
982
983 Now we can apply the matrix M. Let $A$ be the point defined here: \ref{ssub:method_homogenization}. By homogenization, we obtain the column matrix $V$.
984
985
986 \begin{mybox}
987 M.W = M.A * M.V
988 \end{mybox}
989
990 \directlua{
991 init_elements()
992 pi = math.pi
993 M.h = matrix.htm(pi / 4 , 3 , 1)
994 M.h:print()
995 z.A = point(2,0)
996 M.V = z.A.mtx:homogenization()
997 M.V:print() tex.print('=')
998 M.W = M.h * M.V
999 M.W:print()
1000 }
1001
1002 All that remains is to extract the coordinates of the new point.
1003
1004 \subsubsection{Method \tkzMeth{matrix}{get\_htm\_point}}
1005 \label{ssub:method_get_htm_point}
1006
1007 In the previous section, we obtained the $W$ matrix. Now we need to obtain the point it defines.
1008
1009 The method \code{get\_htm\_point} extracts a point from a vector obtained after applying a \code{htm} matrix.
1010
1011 \begin{minipage}{.5\textwidth}
1012 \begin{verbatim}
1013 \directlua{
1014 init_elements()
1015 pi = math.pi
1016 M.h = matrix.htm(pi / 4 , 3 , 1)
1017 z.A = point(2,0)
1018 M.V = z.A.mtx:homogenization()
1019 M.W = M.h * M.V
1020 M.W:print()
1021 z.P = get_htm_point(M.W)
1022 tex.print("The affix of $P$ is: ")
1023 tex.print(tkz.display(z.P))}
1024 \end{verbatim}
1025 \end{minipage}
1026 \begin{minipage}{.5\textwidth}
1027 \directlua{
1028 init_elements()
1029 pi = math.pi
1030 M.h = matrix.htm(pi / 4 , 3 , 1)
1031 z.A = point(2,0)
1032 M.V = z.A.mtx:homogenization()
1033 M.W = M.h * M.V
1034 M.W:print()
1035 z.P = get_htm_point(M.W)
1036 tex.print("The affix of $P$ is: ")
1037 tex.print(tkz.display(z.P))}
1038 \end{minipage}
1039
1040 \subsubsection{Method \tkzMeth{matrix}{htm\_apply}}
1041 \label{ssub:method_code_htm__apply}
1042 The above operations can be simplified by using the \code{htm\_apply} method directly at point $A$.
1043
1044 \begin{mybox}
1045 |z.Ap = M: htm_apply (z.A)|\\
1046 % display (z.Ap)
1047 \end{mybox}
1048
1049 Then the method \code{htm\_apply} transforms a point, a list of points or an object.
1050
1051 \directlua{
1052 init_elements()
1053 pi = math.pi
1054 M.h = matrix.htm(pi / 4, 3, 1)
1055 z.O = point(0, 0)
1056 z.I = point(1, 0)
1057 z.J = point(0, 1)
1058 z.A = point(2, 0)
1059 z.B = point(1, 2)
1060 L.AB = line(z.A, z.B)
1061 z.Op, z.Ip, z.Jp = M.h:htm_apply(z.O, z.I, z.J)
1062 L.ApBp = M.h:htm_apply(L.AB)
1063 z.Ap = L.ApBp.pa
1064 z.Bp = L.ApBp.pb
1065 z.K = point(2, 2)
1066 T.IJK = triangle(z.I, z.J, z.K)
1067 Tp = M.h:htm_apply(T.IJK)
1068 z.Kp = Tp.pc}
1069
1070 \begin{minipage}{.6\textwidth}
1071 \begin{verbatim}
1072 \directlua{
1073 init_elements()
1074 pi = math.pi
1075 M.h = matrix.htm(pi / 4, 3, 1)
1076 z.O = point(0, 0)
1077 z.I = point(1, 0)
1078 z.J = point(0, 1)
1079 z.A = point(2, 0)
1080 z.B = point(1, 2)
1081 L.AB = line(z.A, z.B)
1082 z.Op, z.Ip, z.Jp = M.h:htm_apply(z.O, z.I, z.J)
1083 L.ApBp = M.h:htm_apply(L.AB)
1084 z.Ap = L.ApBp.pa
1085 z.Bp = L.ApBp.pb
1086 z.K = point(2, 2)
1087 T.IJK = triangle(z.I, z.J, z.K)
1088 Tp = M.h:htm_apply(T.IJK)
1089 z.Kp = Tp.pc}
1090 \end{verbatim}
1091 \end{minipage}
1092 \begin{minipage}{.4\textwidth}
1093 \begin{tikzpicture}[gridded]
1094 \tkzGetNodes
1095 \tkzDrawPoints(O,O',A,B,A',B',K,K')
1096 \tkzLabelPoints(O,O',A,B,A',B',I,J,I',J',K,K')
1097 \tkzDrawSegments[->](O,I O,J O',I' O',J')
1098 \tkzDrawLines (A,B A',B')
1099 \tkzDrawPolygons[red](I,J,K I',J',K')
1100 \end{tikzpicture}
1101 \end{minipage}
1102
1103 \vspace{.5 em}
1104
1105 New cartesian coordinates system:
1106
1107 \vspace{.5 em}
1108 \begin{minipage}{.5\textwidth}
1109 \begin{verbatim}
1110 \directlua{
1111 init_elements()
1112 pi = math.pi
1113 tp = tex.print
1114 nl = "\\\\"
1115 a = point(1, 0)
1116 b = point(0, 1)
1117 M.R = matrix.htm(pi / 5, 2, 1)
1118 M.R:print()
1119 tp(nl)
1120 M.v = matrix.vector(1, 2)
1121 M.v:print()
1122 M.v.h = M.v:homogenization()
1123 M.v.h:print()
1124 tp(nl)
1125 M.V = M.R * M.v.h
1126 M.V:print()
1127 z.N = get_htm_point(M.V)
1128 tex.print(tkz.display(z.N))}
1129 \end{verbatim}
1130 \end{minipage}
1131 \begin{minipage}{.5\textwidth}
1132 \directlua{
1133 init_elements()
1134 pi = math.pi
1135 tp = tex.print
1136 nl = "\\\\"
1137 a = point(1, 0)
1138 b = point(0, 1)
1139 M.R = matrix.htm(pi / 5, 2, 1)
1140 M.R:print()
1141 tp(nl)
1142 M.v = matrix.vector(1, 2)
1143 M.v:print()
1144 M.v.h = M.v:homogenization()
1145 M.v.h:print()
1146 tp(nl)
1147 M.V = M.R * M.v.h
1148 M.V:print()
1149 z.N = get_htm_point(M.V)
1150 tex.print(tkz.display(z.N))}
1151 \end{minipage}
1152
1153 \subsubsection{Method \tkzMeth{matrix}{gauss\_jordan()}}
1154 \label{ssub:method_matrix_gauss_jordan}
1155
1156
1157 This method applies the Gauss–Jordan elimination algorithm to the matrix and returns its reduced row echelon form (RREF).
1158
1159 \medskip
1160 The Gauss–Jordan algorithm transforms a matrix into an equivalent matrix by a sequence of elementary row operations:
1161 \begin{itemize}
1162 \item swapping two rows;
1163 \item multiplying a row by a non-zero scalar;
1164 \item adding a multiple of one row to another.
1165 \end{itemize}
1166 The resulting matrix satisfies the following properties:
1167 \begin{itemize}
1168 \item each leading entry of a non-zero row is equal to $1$;
1169 \item each leading $1$ is the only non-zero entry in its column;
1170 \item all zero rows, if any, are at the bottom of the matrix.
1171 \end{itemize}
1172
1173 \medskip
1174 \texttt{Return value: }
1175
1176 The method returns a new matrix object corresponding to the reduced row echelon form of the original matrix.
1177 The original matrix is not modified.
1178
1179 \medskip
1180 \texttt{Numerical considerations: }
1181
1182 Since computations are performed using floating-point arithmetic, a numerical tolerance is used internally to detect zero pivots.
1183 Very small values (typically below a fixed threshold) are treated as zero.
1184
1185 \medskip
1186 \texttt{Typical uses: }
1187
1188 \begin{itemize}
1189 \item solving linear systems;
1190 \item computing the rank of a matrix;
1191 \item testing linear independence of vectors;
1192 \item computing the inverse of a square matrix (when it exists).
1193 \end{itemize}
1194
1195 \texttt{Example with matrix 2x2: }
1196
1197 \begin{tkzexample}[latex=.5\textwidth]
1198 \directlua{
1199 M.A = matrix({{2,1},{5,3}})
1200 M.Ainv = M.A:augment_right(matrix.identity(2))
1201 :gauss_jordan()
1202 :submatrix(1, 2, 3, 4)
1203
1204 M.Ainv:print()
1205 }
1206 \end{tkzexample}
1207
1208 \texttt{Example with matrix 3x3: }
1209
1210 \begin{tkzexample}[latex=.5\textwidth]
1211 \directlua{
1212 % Matrix 3x3
1213 M.A = matrix({
1214 {2, 1, 1},
1215 {1, 3, 2},
1216 {1, 0, 0}
1217 })
1218
1219 % Inverse via augmentation + Gauss–Jordan
1220 M.Ainv = M.A
1221 :augment_right(matrix.identity(3))
1222 :gauss_jordan()
1223 :submatrix(1, 3, 4, 6)
1224
1225 % Print the inverse
1226 M.Ainv:print()
1227 }
1228 \end{tkzexample}
1229
1230
1231
1232
1233 \subsubsection{Method \tkzMeth{matrix}{rank()}}
1234 \label{ssub:method_matrix_rank}
1235
1236 This method returns the rank of the matrix, that is, the dimension of the vector space spanned by its rows (or equivalently, by its columns).
1237
1238 \medskip
1239 The rank is computed using the Gauss–Jordan elimination process: it is equal to the number of non-zero rows in the reduced row echelon form of the matrix.
1240
1241 \medskip
1242 \texttt{Return value: }
1243 The method returns a non-negative integer equal to the rank of the matrix.
1244
1245 \medskip
1246 \texttt{Remarks: }
1247 \begin{itemize}
1248 \item The rank is always less than or equal to the minimum of the number of rows and columns.
1249 \item A matrix has full rank if its rank is equal to this minimum.
1250 \item A square matrix is invertible if and only if its rank is maximal.
1251 \end{itemize}
1252
1253 \medskip
1254 \texttt{Numerical stability: }
1255 As for \tkzMeth{matrix}{gauss\_jordan()}, a numerical tolerance is used to decide whether a row should be considered zero.
1256
1257 \medskip
1258 \noindent
1259 \textbf{Note.}
1260 The method \tkzMeth{matrix}{rank()} internally relies on the Gauss–Jordan reduction, but provides a direct and convenient access to the rank without exposing the intermediate reduced matrix.
1261
1262 \texttt{Example 1: }
1263
1264 \begin{tkzexample}[latex=.5\textwidth]
1265 \directlua{
1266 M.A = matrix({
1267 {2, 1, 1},
1268 {1, 3, 2},
1269 {1, 0, 0}
1270 })
1271
1272 tex.print("rank(A) = ", M.A:rank())
1273 }
1274 \end{tkzexample}
1275
1276 \texttt{Example 2: }
1277
1278 \begin{tkzexample}[latex=.5\textwidth]
1279 \directlua{
1280 M.B = matrix({
1281 {1, 2, 3},
1282 {2, 4, 6},
1283 {1, 1, 1}
1284 })
1285
1286 tex.print("rank(B) = ", M.B:rank())
1287 }
1288 \end{tkzexample}
1289
1290 \texttt{Example 3: }
1291
1292 \begin{tkzexample}[latex=.5\textwidth]
1293
1294 \directlua{
1295 M.C = matrix({
1296 {1, 2, 3},
1297 {2, 4, 6},
1298 {1, 1, 1}
1299 })
1300
1301 M.R = M.C:gauss_jordan()
1302
1303 tex.print("rank(C) = ", M.C:rank())
1304 }
1305
1306 \end{tkzexample}
1307
1308
1309 \subsubsection{Augmented matrices and submatrices}
1310 \label{ssub:matrix-augmentation-submatrix}
1311
1312 The inversion of a square matrix and the resolution of linear systems are both
1313 based on the same fundamental idea: the manipulation of an \emph{augmented matrix}
1314 followed by a block extraction after a Gauss--Jordan reduction.
1315
1316 \medskip
1317
1318 \paragraph{Augmented matrix.}
1319 Let $A$ be an $m\times n$ matrix and $B$ an $m\times p$ matrix.
1320 The augmented matrix $[A \mid B]$ is obtained by appending the columns of $B$
1321 to the right of $A$:
1322
1323 \[
1324 [A \mid B] =
1325 \begin{pmatrix}
1326 A_{11} & \cdots & A_{1n} & B_{11} & \cdots & B_{1p} \\
1327 \vdots & & \vdots & \vdots & & \vdots \\
1328 A_{m1} & \cdots & A_{mn} & B_{m1} & \cdots & B_{mp}
1329 \end{pmatrix}.
1330 \]
1331
1332 In the \texttt{matrix} class, this operation is performed by the method
1333 \texttt{augment\_right}.
1334
1335 \medskip
1336
1337 \paragraph{Example (matrix inversion).}
1338 Let
1339 \[
1340 A =
1341 \begin{pmatrix}
1342 2 & 1\\
1343 5 & 3
1344 \end{pmatrix}.
1345 \]
1346 The augmented matrix $[A \mid I]$ is
1347
1348 \[
1349 \begin{pmatrix}
1350 2 & 1 & 1 & 0\\
1351 5 & 3 & 0 & 1
1352 \end{pmatrix}.
1353 \]
1354
1355 Applying the Gauss--Jordan algorithm yields
1356
1357 \[
1358 \begin{pmatrix}
1359 1 & 0 & 3 & -1\\
1360 0 & 1 & -5 & 2
1361 \end{pmatrix}
1362 =
1363 [I \mid A^{-1}],
1364 \]
1365 from which the inverse matrix can be read directly.
1366
1367 \medskip
1368
1369 \paragraph{Submatrix extraction.}
1370 The method \texttt{submatrix(r1, r2, c1, c2)} extracts a rectangular block from a
1371 matrix, defined by:
1372
1373 \begin{itemize}
1374 \item rows from \texttt{r1} to \texttt{r2},
1375 \item columns from \texttt{c1} to \texttt{c2}.
1376 \end{itemize}
1377
1378 All indices start at~$1$.
1379
1380 \medskip
1381
1382 \paragraph{Meaning of \texttt{submatrix(1,2,3,4)}.}
1383 In the previous example, the reduced augmented matrix has:
1384 \begin{itemize}
1385 \item $2$ rows,
1386 \item $4$ columns.
1387 \end{itemize}
1388
1389 The inverse matrix occupies:
1390 \begin{itemize}
1391 \item rows $1$ to $2$,
1392 \item columns $3$ to $4$.
1393 \end{itemize}
1394
1395 Thus the extraction
1396 \[
1397 \texttt{submatrix(1,2,3,4)}
1398 \]
1399 returns exactly the block
1400 \[
1401 A^{-1} =
1402 \begin{pmatrix}
1403 3 & -1\\
1404 -5 & 2
1405 \end{pmatrix}.
1406 \]
1407
1408 \medskip
1409
1410 \paragraph{General rule.}
1411 For a square matrix of order $n$, the inverse matrix is obtained by
1412 \[
1413 [A \mid I_n]
1414 \;\xrightarrow{\text{Gauss--Jordan}}\;
1415 [I_n \mid A^{-1}],
1416 \]
1417 and extracted using
1418 \[
1419 \texttt{submatrix(1, n, n+1, 2n)}.
1420 \]
1421
1422 \medskip
1423
1424 \paragraph{Summary.}
1425 \begin{itemize}
1426 \item \texttt{augment\_right} constructs an augmented matrix,
1427 \item \texttt{gauss\_jordan} performs the row reduction,
1428 \item \texttt{submatrix} extracts the desired result block.
1429 \end{itemize}
1430
1431 \endinput