Skip to content

Commit d124dc4

Browse files
authored
Merge pull request #1745 from maths/aug_improvement
Aug improvement
2 parents c16b364 + aa17a76 commit d124dc4

3 files changed

Lines changed: 37 additions & 20 deletions

File tree

doc/en/Topics/Linear_algebra/Matrix_library.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,15 @@ The last three predicates above will all accept an optional argument, `sp`, that
8383

8484
It is sometimes convenient to be able to display an augmented matrix. `matrix.mac` adds some limited support for this.
8585

86-
Perhaps you have a question in which students are asked to solve the matrix equation \(A\underline{\mathbf{x}} = \underline{\mathbf{b}}\) using Gaussian elimination and you wanted to display this problem as an augmented matrix. Then, with matrix `A` and right hand side vector (really a matrix) `b` already defined, you could use `aug(addcol(A,b))` to display
86+
Perhaps you have a question in which students are asked to solve the matrix equation \(A\underline{\mathbf{x}} = \underline{\mathbf{b}}\) using Gaussian elimination and you wanted to display this problem as an augmented matrix. Then, with matrix `A` and right hand side vector (really a matrix) `b` already defined, you could use `aug(A,b)` to display
8787

88-
\[{\left[\begin{array}{cc} 1 & 2 \\ 4 & 5 \end{array}\right|\left.\begin{array}{c} 3 \\ 6 \end{array}\right]}\]
88+
\[\left[\begin{array}{cc} 1 & 2 \\ 4 & 5 \end{array}\right|\left.\begin{array}{c} 3 \\ 6 \end{array}\right]\]
8989

90-
Really what is happening here is that `aug` is converting a matrix with concatenated columns `A` and `b` to an `aug_matrix`, which then displays as a matrix with its final column separated by a vertical bar. `aug_matrix` is an inert function that exists only in this library for display purposes. You can save this to a variable, perhaps `Ab`, but Maxima doesn't know that this is a matrix at all and so matrix operations won't work on it. To turn it back into a matrix, you can use `de_aug(Ab)`.
90+
Really what is happening here is that `aug` is converting a matrix with concatenated columns `A` and `b` to an `aug_matrix`, which then displays as a matrix with its final column separated by a vertical bar. `aug_matrix` is an inert function that exists only in this library for display purposes. You can save this to a variable, perhaps `Ab`, but Maxima doesn't know that this is a matrix at all and so matrix operations won't work on it. To turn it back into a matrix, you can use `de_aug(Ab)`. `de_aug` concatenates the input matrices instead of returning a list, so if you need more control you may prefer to programme your row operations separately and display `aug(A1,b1)`, `aug(A2,b2)` etc. manually.
91+
92+
This can generalise to as many inputs as needed. For instance, `aug(matrix([1,2,3],[4,5,6],[7,8,9]), matrix([1],[1],[1]), ident(3))` will display as
93+
94+
\[\left[\begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}\left|\begin{array}{c} 1 \\ 1 \\ 1 \end{array}\right.\left|\begin{array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array}\right.\right]\]
9195

9296
### Systems of equations
9397

stack/maxima/contrib/matrix.mac

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,43 @@ stack_linear_algebra_declare(true)$
2828
/*******************************************************************************/
2929
/* A convenience function for displaying a matrix as an augmented matrix */
3030
/*******************************************************************************/
31-
texput(aug_matrix, lambda([ex], block([M,ll,rr,m,n,A,b,simp],
32-
simp:true,
33-
M: apply(matrix,args(ex)),
34-
ll: lmxchar,
35-
if is(ll="[") then rr: "]"
36-
else if is(ll="(") then rr: ")"
37-
else if is(ll="") then (ll: ".", rr: ".")
38-
else if is(ll="{") then (ll: "\\{", rr: "\\}")
39-
else if is(ll="|") then rr: "|",
40-
[m, n]: matrix_size(M),
41-
A: submatrix(M,n),
42-
b: col(M,n),
43-
sconcat("\\left",ll,block([lmxchar],lmxchar:"",tex1(A)),"\\right|\\left.",block([lmxchar],lmxchar:"",tex1(b)),"\\right",rr)
44-
)));
31+
texput(aug_matrix, lambda([ex],
32+
block([exl:args(ex),ii,ss,ll:lmxchar,rr,lmxchar],
33+
if is(ll="[") then rr: "]"
34+
else if is(ll="(") then rr: ")"
35+
else if is(ll="") then (ll: ".", rr: ".")
36+
else if is(ll="{") then (ll: "\\{", rr: "\\}")
37+
else if is(ll="|") then rr: "|",
38+
lmxchar: "",
39+
ss: ["\\left",ll,tex1(exl[1])],
40+
for ii: 2 thru length(exl) do block(
41+
ii: ev(ii,simp),
42+
ss: append(ss, ["\\left|",tex1(exl[ii]),"\\right."])
43+
),
44+
ss: append(ss, ["\\right",rr]),
45+
return(simplode(ss))
46+
)
47+
));
4548

4649
/**
4750
* Converts a matrix to an aug_matrix
4851
* aug_matrix is an inert function that is used for displaying a matrix as an augmented matrix
4952
* To convert back, use de_aug
5053
*
51-
* @param[matrix] M The matrix you would like to display as an augmented matrix
54+
* @param[matrix] M The matrix or matrices you would like to display as an augmented matrix
5255
* @return[aug_matrix] An augmented matrix
5356
*/
54-
aug(M):= apply(aug_matrix,args(M));
57+
aug([M]):= if is(length(M)=1) then apply(aug_matrix,[submatrix(first(M),second(matrix_size(first(M)))),col(first(M),second(matrix_size(first(M))))]) else apply(aug_matrix,M);
5558

5659
/**
5760
* Converts an aug_matrix to a matrix
5861
* aug_matrix is an inert function that is used for displaying a matrix as an augmented matrix
62+
* Note: This always returns a single matrix constructed by concatenating the arguments of the original aug_matrix.
5963
*
6064
* @param[matrix] M The aug_matrix you would like to treat as a regular matrix
6165
* @return[aug_matrix] A matrix
6266
*/
63-
de_aug(M):= apply(matrix,args(M));
67+
de_aug(M):= apply(addcol, args(M));
6468

6569
/*********************************************************************************/
6670
/* Functions to extract parts of matrices */

stack/maxima/contrib/matrix_test.mac

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030

3131
s_test_case(aug(matrix([1,2,3,1],[4,5,6,1],[7,8,9,1])),aug_matrix([1,2,3,1],[4,5,6,1],[7,8,9,1]));
3232
s_test_case(de_aug(aug_matrix([1,2,3,1],[4,5,6,1],[7,8,9,1])),matrix([1,2,3,1],[4,5,6,1],[7,8,9,1]));
33+
s_test_case(de_aug(aug(ident(4),matrix([1],[2],[3],[4]),ident(4))),matrix([1,0,0,0,1,1,0,0,0],[0,1,0,0,2,0,1,0,0],[0,0,1,0,3,0,0,1,0],[0,0,0,1,4,0,0,0,1]));
34+
35+
lmxchar: "[";
36+
s_test_case(tex1(aug(matrix([1,2,3],[4,5,6],[7,8,9]),matrix([1],[1],[1]))),"\\left[\\begin{array}{ccc} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\\\ 7 & 8 & 9 \\end{array}\\left|\\begin{array}{c} 1 \\\\ 1 \\\\ 1 \\end{array}\\right.\\right]");
37+
s_test_case(tex1(aug(matrix([1,2,3,1],[4,5,6,1],[7,8,9,1]))),"\\left[\\begin{array}{ccc} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\\\ 7 & 8 & 9 \\end{array}\\left|\\begin{array}{c} 1 \\\\ 1 \\\\ 1 \\end{array}\\right.\\right]");
38+
s_test_case(tex1(aug(matrix([1,2,3],[4,5,6],[7,8,9]),matrix([1],[1],[1]),matrix([3,2,1,0],[6,5,4,3],[9,8,7,6]))),"\\left[\\begin{array}{ccc} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\\\ 7 & 8 & 9 \\end{array}\\left|\\begin{array}{c} 1 \\\\ 1 \\\\ 1 \\end{array}\\right.\\left|\\begin{array}{cccc} 3 & 2 & 1 & 0 \\\\ 6 & 5 & 4 & 3 \\\\ 9 & 8 & 7 & 6 \\end{array}\\right.\\right]");
39+
lmxchar: "{";
40+
s_test_case(tex1(aug(matrix([1,2,3],[4,5,6],[7,8,9]),matrix([1],[1],[1]))),"\\left\\{\\begin{array}{ccc} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\\\ 7 & 8 & 9 \\end{array}\\left|\\begin{array}{c} 1 \\\\ 1 \\\\ 1 \\end{array}\\right.\\right\\}");
41+
lmxchar: "[";
3342

3443
s_test_case(triu(matrix([1,2,3],[4,5,6],[7,8,9])),matrix([1,2,3],[0,5,6],[0,0,9]));
3544
s_test_case(triu(matrix([1,2,3],[4,5,6],[7,8,9],[10,11,12])),matrix([1,2,3],[0,5,6],[0,0,9],[0,0,0]));

0 commit comments

Comments
 (0)