2 * Introduction to linearalgebra::
3 * Functions and Variables for linearalgebra::
6 @c -----------------------------------------------------------------------------
7 @node Introduction to linearalgebra, Functions and Variables for linearalgebra, Package linearalgebra, Package linearalgebra
8 @section Introduction to linearalgebra
9 @c -----------------------------------------------------------------------------
11 @code{linearalgebra} is a collection of functions for linear algebra.
16 @c M : matrix ([1, 2], [1, 2]);
19 @c ptriangularize (M - z*ident(2), z);
20 @c M : matrix ([1, 2, 3], [4, 5, 6], [7, 8, 9]) - z*ident(3);
21 @c MM : ptriangularize (M, z);
23 @c tellrat (MM [3, 3]);
26 @c M : matrix ([1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],
29 @c apply ('orthogonal_complement, args (nullspace (transpose (M))));
32 (%i1) M : matrix ([1, 2], [1, 2]);
42 (%i3) columnspace (M);
46 (%i4) ptriangularize (M - z*ident(2), z);
51 (%i5) M : matrix ([1, 2, 3], [4, 5, 6], [7, 8, 9]) - z*ident(3);
57 (%i6) MM : ptriangularize (M, z);
62 [ 0 -- - -- + ----- + --- ]
67 [ 0 0 ----- - ------ - ----- ]
69 (%i7) algebraic : true;
71 (%i8) tellrat (MM [3, 3]);
73 (%o8) [z - 15 z - 18 z]
74 (%i9) MM : ratsimp (MM);
78 (%o9) [ 66 7 z - 102 z - 132 ]
79 [ 0 -- - ------------------ ]
83 (%i10) nullspace (MM);
95 (%i11) M : matrix ([1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],
104 (%i12) columnspace (M);
108 (%o12) span([ ], [ ])
112 (%i13) apply ('orthogonal_complement, args (nullspace (transpose (M))));
116 (%o13) span([ ], [ ])
122 @opencatbox{Categories:}
123 @category{Linear algebra}
124 @category{Share packages}
125 @category{Package linearalgebra}
128 @c -----------------------------------------------------------------------------
130 @node Functions and Variables for linearalgebra, , Introduction to linearalgebra, Package linearalgebra
131 @section Functions and Variables for linearalgebra
132 @c -----------------------------------------------------------------------------
134 @c -----------------------------------------------------------------------------
136 @deffn {Function} addmatrices (@var{f}, @var{M_1}, @dots{}, @var{M_n})
138 @c REWORD -- THE RESULT IS NOT GENERALLY THE SUM OF M_1, ..., M_N
139 Using the function @var{f} as the addition function, return the sum of the
140 matrices @var{M_1}, @dots{}, @var{M_n}. The function @var{f} must accept any
141 number of arguments (a Maxima nary function).
146 @c m1 : matrix([1,2],[3,4])$
147 @c m2 : matrix([7,8],[9,10])$
148 @c addmatrices('max,m1,m2);
149 @c addmatrices('max,m1,m2,5*m1);
152 (%i1) m1 : matrix([1,2],[3,4])$
153 (%i2) m2 : matrix([7,8],[9,10])$
154 (%i3) addmatrices('max,m1,m2);
155 (%o3) matrix([7,8],[9,10])
156 (%i4) addmatrices('max,m1,m2,5*m1);
157 (%o4) matrix([7,10],[15,20])
160 @opencatbox{Categories:}
161 @category{Package linearalgebra}
165 @c -----------------------------------------------------------------------------
166 @anchor{blockmatrixp}
167 @deffn {Function} blockmatrixp (@var{M})
169 Return true if and only if @var{M} is a matrix and every entry of
172 @opencatbox{Categories:}
173 @category{Package linearalgebra}
174 @category{Predicate functions}
178 @c -----------------------------------------------------------------------------
180 @deffn {Function} columnop (@var{M}, @var{i}, @var{j}, @var{theta})
182 If @var{M} is a matrix, return the matrix that results from doing the column
183 operation @code{C_i <- C_i - @var{theta} * C_j}. If @var{M} doesn't have a row
184 @var{i} or @var{j}, signal an error.
186 @opencatbox{Categories:}
187 @category{Package linearalgebra}
191 @c -----------------------------------------------------------------------------
193 @deffn {Function} columnswap (@var{M}, @var{i}, @var{j})
195 If @var{M} is a matrix, swap columns @var{i} and @var{j}. If @var{M} doesn't
196 have a column @var{i} or @var{j}, signal an error.
198 @opencatbox{Categories:}
199 @category{Package linearalgebra}
203 @c -----------------------------------------------------------------------------
205 @deffn {Function} columnspace (@var{M})
207 If @var{M} is a matrix, return @code{span (v_1, ..., v_n)}, where the set
208 @code{@{v_1, ..., v_n@}} is a basis for the column space of @var{M}. The span
209 of the empty set is @code{@{0@}}. Thus, when the column space has only
210 one member, return @code{span ()}.
212 @opencatbox{Categories:}
213 @category{Package linearalgebra}
217 @c -----------------------------------------------------------------------------
219 @deffn {Function} cholesky @
220 @fname{cholesky} (@var{M}) @
221 @fname{cholesky} (@var{M}, @var{field})
223 Return the Cholesky factorization of the matrix selfadjoint (or hermitian)
224 matrix @var{M}. The second argument defaults to 'generalring.' For a
225 description of the possible values for @var{field}, see @code{lu_factor}.
227 @opencatbox{Categories:}
228 @category{Matrix decompositions}
229 @category{Package linearalgebra}
233 @c -----------------------------------------------------------------------------
235 @deffn {Function} ctranspose (@var{M})
237 Return the complex conjugate transpose of the matrix @var{M}. The function
238 @code{ctranspose} uses @code{matrix_element_transpose} to transpose each matrix
241 @opencatbox{Categories:}
242 @category{Package linearalgebra}
246 @c -----------------------------------------------------------------------------
248 @deffn {Function} diag_matrix (@var{d_1}, @var{d_2}, @dots{}, @var{d_n})
250 Return a diagonal matrix with diagonal entries @var{d_1}, @var{d_2}, @dots{},
251 @var{d_n}. When the diagonal entries are matrices, the zero entries of the
252 returned matrix are zero matrices of the appropriate size; for example:
255 @c diag_matrix(diag_matrix(1,2),diag_matrix(3,4));
259 (%i1) diag_matrix(diag_matrix(1,2),diag_matrix(3,4));
268 (%i2) diag_matrix(p,q);
275 @opencatbox{Categories:}
276 @category{Package linearalgebra}
280 @c -----------------------------------------------------------------------------
282 @deffn {Function} dotproduct (@var{u}, @var{v})
284 Return the dotproduct of vectors @var{u} and @var{v}. This is the same as
285 @code{conjugate (transpose (@var{u})) . @var{v}}. The arguments @var{u} and
286 @var{v} must be column vectors.
288 @opencatbox{Categories:}
289 @category{Package linearalgebra}
293 @c -----------------------------------------------------------------------------
294 @anchor{eigens_by_jacobi}
295 @deffn {Function} eigens_by_jacobi @
296 @fname{eigens_by_jacobi} (@var{A}) @
297 @fname{eigens_by_jacobi} (@var{A}, @var{field_type})
299 Computes the eigenvalues and eigenvectors of @var{A} by the method of Jacobi
300 rotations. @var{A} must be a symmetric matrix (but it need not be positive
301 definite nor positive semidefinite). @var{field_type} indicates the
302 computational field, either @code{floatfield} or @code{bigfloatfield}.
303 If @var{field_type} is not specified, it defaults to @code{floatfield}.
305 The elements of @var{A} must be numbers or expressions which evaluate to numbers
306 via @code{float} or @code{bfloat} (depending on @var{field_type}).
311 @c S : matrix ([1/sqrt(2), 1/sqrt(2)], [- 1/sqrt(2), 1/sqrt(2)]);
312 @c L : matrix ([sqrt(3), 0], [0, sqrt(5)]);
313 @c M : S . L . transpose (S);
314 @c eigens_by_jacobi (M);
315 @c float ([[sqrt(3), sqrt(5)], S]);
316 @c eigens_by_jacobi (M, bigfloatfield);
319 (%i1) S: matrix([1/sqrt(2), 1/sqrt(2)],[-1/sqrt(2), 1/sqrt(2)]);
325 [ - ------- ------- ]
327 (%i2) L : matrix ([sqrt(3), 0], [0, sqrt(5)]);
331 (%i3) M : S . L . transpose (S);
332 [ sqrt(5) sqrt(3) sqrt(5) sqrt(3) ]
333 [ ------- + ------- ------- - ------- ]
336 [ sqrt(5) sqrt(3) sqrt(5) sqrt(3) ]
337 [ ------- - ------- ------- + ------- ]
339 (%i4) eigens_by_jacobi (M);
340 The largest percent change was 0.1454972243679
341 The largest percent change was 0.0
343 number of rotations: 1
344 (%o4) [[1.732050807568877, 2.23606797749979],
345 [ 0.70710678118655 0.70710678118655 ]
347 [ - 0.70710678118655 0.70710678118655 ]
348 (%i5) float ([[sqrt(3), sqrt(5)], S]);
349 (%o5) [[1.732050807568877, 2.23606797749979],
350 [ 0.70710678118655 0.70710678118655 ]
352 [ - 0.70710678118655 0.70710678118655 ]
353 (%i6) eigens_by_jacobi (M, bigfloatfield);
354 The largest percent change was 1.454972243679028b-1
355 The largest percent change was 0.0b0
357 number of rotations: 1
358 (%o6) [[1.732050807568877b0, 2.23606797749979b0],
359 [ 7.071067811865475b-1 7.071067811865475b-1 ]
361 [ - 7.071067811865475b-1 7.071067811865475b-1 ]
364 @opencatbox{Categories:}
365 @category{Matrix decompositions}
366 @category{Package linearalgebra}
370 @c -----------------------------------------------------------------------------
371 @anchor{get_lu_factors}
372 @deffn {Function} get_lu_factors (@var{x})
374 When @code{@var{x} = lu_factor (@var{A})}, then @code{get_lu_factors} returns a
375 list of the form @code{[P, L, U]}, where @var{P} is a permutation matrix,
376 @var{L} is lower triangular with ones on the diagonal, and @var{U} is upper
377 triangular, and @code{@var{A} = @var{P} @var{L} @var{U}}.
379 @opencatbox{Categories:}
380 @category{Package linearalgebra}
384 @c -----------------------------------------------------------------------------
386 @deffn {Function} hankel @
387 @fname{hankel} (@var{col}) @
388 @fname{hankel} (@var{col}, @var{row})
390 Return a Hankel matrix @var{H}. The first column of @var{H} is @var{col};
391 except for the first entry, the last row of @var{H} is @var{row}. The
392 default for @var{row} is the zero vector with the same length as @var{col}.
394 @opencatbox{Categories:}
395 @category{Package linearalgebra}
399 @c -----------------------------------------------------------------------------
401 @deffn {Function} hessian (@var{f}, @var{x})
403 Returns the Hessian matrix of @var{f} with respect to the list of variables
404 @var{x}. The @code{(i, j)}-th element of the Hessian matrix is
405 @code{diff(@var{f}, @var{x}[i], 1, @var{x}[j], 1)}.
410 @c hessian (x * sin (y), [x, y]);
411 @c depends (F, [a, b]);
412 @c hessian (F, [a, b]);
415 (%i1) hessian (x * sin (y), [x, y]);
418 [ cos(y) - x sin(y) ]
419 (%i2) depends (F, [a, b]);
421 (%i3) hessian (F, [a, b]);
435 @opencatbox{Categories:}
436 @category{Differential calculus}
437 @category{Package linearalgebra}
441 @c -----------------------------------------------------------------------------
442 @anchor{hilbert_matrix}
443 @deffn {Function} hilbert_matrix (@var{n})
445 Return the @var{n} by @var{n} Hilbert matrix. When @var{n} isn't a positive
446 integer, signal an error.
448 @opencatbox{Categories:}
449 @category{Package linearalgebra}
453 @c -----------------------------------------------------------------------------
455 @deffn {Function} identfor @
456 @fname{identfor} (@var{M}) @
457 @fname{identfor} (@var{M}, @var{fld})
459 Return an identity matrix that has the same shape as the matrix
460 @var{M}. The diagonal entries of the identity matrix are the
461 multiplicative identity of the field @var{fld}; the default for
462 @var{fld} is @var{generalring}.
464 The first argument @var{M} should be a square matrix or a non-matrix. When
465 @var{M} is a matrix, each entry of @var{M} can be a square matrix -- thus
466 @var{M} can be a blocked Maxima matrix. The matrix can be blocked to any
469 See also @mref{zerofor}
471 @opencatbox{Categories:}
472 @category{Package linearalgebra}
476 @c -----------------------------------------------------------------------------
477 @anchor{invert_by_lu}
478 @deffn {Function} invert_by_lu (@var{M}, @var{(rng generalring)})
480 Invert a matrix @var{M} by using the LU factorization. The LU factorization
481 is done using the ring @var{rng}.
483 @opencatbox{Categories:}
484 @category{Package linearalgebra}
488 @c -----------------------------------------------------------------------------
490 @deffn {Function} jacobian (@var{f}, @var{x})
492 Returns the Jacobian matrix of the list of functions @var{f} with respect to
493 the list of variables @var{x}. The @code{(i, j)}-th element of the Jacobian
494 matrix is @code{diff(@var{f}[i], @var{x}[j])}.
499 @c jacobian ([sin (u - v), sin (u * v)], [u, v]);
500 @c depends ([F, G], [y, z]);
501 @c jacobian ([F, G], [y, z]);
504 (%i1) jacobian ([sin (u - v), sin (u * v)], [u, v]);
505 [ cos(v - u) - cos(v - u) ]
507 [ v cos(u v) u cos(u v) ]
508 (%i2) depends ([F, G], [y, z]);
509 (%o2) [F(y, z), G(y, z)]
510 (%i3) jacobian ([F, G], [y, z]);
520 @opencatbox{Categories:}
521 @category{Differential calculus}
522 @category{Package linearalgebra}
526 @c -----------------------------------------------------------------------------
527 @anchor{kronecker_product}
528 @deffn {Function} kronecker_product (@var{A}, @var{B})
530 Return the Kronecker product of the matrices @var{A} and @var{B}.
532 @opencatbox{Categories:}
533 @category{Package linearalgebra}
537 @c -----------------------------------------------------------------------------
538 @anchor{locate_matrix_entry}
539 @deffn {Function} locate_matrix_entry (@var{M}, @var{r_1}, @var{c_1}, @var{r_2}, @var{c_2}, @var{f}, @var{rel})
541 The first argument must be a matrix; the arguments
542 @var{r_1} through @var{c_2} determine a sub-matrix of @var{M} that consists of
543 rows @var{r_1} through @var{r_2} and columns @var{c_1} through @var{c_2}.
545 Find an entry in the sub-matrix @var{M} that satisfies some property.
548 (1) @code{@var{rel} = 'bool} and @var{f} a predicate:
550 Scan the sub-matrix from left to right then top to bottom,
551 and return the index of the first entry that satisfies the
552 predicate @var{f}. If no matrix entry satisfies @var{f}, return @code{false}.
554 (2) @code{@var{rel} = 'max} and @var{f} real-valued:
556 Scan the sub-matrix looking for an entry that maximizes @var{f}.
557 Return the index of a maximizing entry.
559 (3) @code{@var{rel} = 'min} and @var{f} real-valued:
561 Scan the sub-matrix looking for an entry that minimizes @var{f}.
562 Return the index of a minimizing entry.
564 @opencatbox{Categories:}
565 @category{Package linearalgebra}
569 @c -----------------------------------------------------------------------------
571 @deffn {Function} lu_backsub (@var{M}, @var{b})
573 When @code{@var{M} = lu_factor (@var{A}, @var{field})},
574 then @code{lu_backsub (@var{M}, @var{b})} solves the linear
575 system @code{@var{A} @var{x} = @var{b}}.
577 The @var{n} by @var{m} matrix @code{@var{b}}, with @var{n} the number of
578 rows of the matrix @code{@var{A}}, contains one right hand side per column. If
579 there is only one right hand side then @code{@var{b}} must be a @var{n} by 1
582 Each column of the matrix @code{@var{x}=lu_backsub (@var{M}, @var{b})} is the
583 solution corresponding to the respective column of @code{@var{b}}.
588 @c A : matrix ([1 - z, 3], [3, 8 - z]);
589 @c M : lu_factor (A,generalring);
590 @c b : matrix([a],[c]);
591 @c x : lu_backsub(M,b);
592 @c ratsimp(A . x - b);
593 @c B : matrix([a,d],[c,f]);
594 @c x : lu_backsub(M,B);
595 @c ratsimp(A . x - B);
598 (%i1) A : matrix ([1 - z, 3], [3, 8 - z]);
602 (%i2) M : lu_factor (A,generalring);
605 (%o2) [[ 3 9 ], [1, 2], generalring]
606 [ ----- (- z) - ----- + 8 ]
608 (%i3) b : matrix([a],[c]);
612 (%i4) x : lu_backsub(M,b);
616 [ a - ----------------- ]
618 [ (- z) - ----- + 8 ]
620 [ --------------------- ]
626 [ ----------------- ]
628 [ (- z) - ----- + 8 ]
630 (%i5) ratsimp(A . x - b);
634 (%i6) B : matrix([a,d],[c,f]);
638 (%i7) x : lu_backsub(M,B);
640 [ 3 (c - -----) 3 (f - -----) ]
642 [ a - ----------------- d - ----------------- ]
644 [ (- z) - ----- + 8 (- z) - ----- + 8 ]
646 [ --------------------- --------------------- ]
647 (%o7) [ 1 - z 1 - z ]
650 [ c - ----- f - ----- ]
652 [ ----------------- ----------------- ]
654 [ (- z) - ----- + 8 (- z) - ----- + 8 ]
656 (%i8) ratsimp(A . x - B);
662 @opencatbox{Categories:}
663 @category{Package linearalgebra}
667 @c -----------------------------------------------------------------------------
669 @deffn {Function} lu_factor (@var{M}, @var{field})
671 Return a list of the form @code{[@var{LU}, @var{perm}, @var{fld}]}, or
672 @code{[@var{LU}, @var{perm}, @var{fld}, @var{lower-cnd} @var{upper-cnd}]}, where
674 (1) The matrix @var{LU} contains the factorization of @var{M} in a packed form.
675 Packed form means three things: First, the rows of @var{LU} are permuted
676 according to the list @var{perm}. If, for example, @var{perm} is the list
677 @code{[3,2,1]}, the actual first row of the @var{LU} factorization is the
678 third row of the matrix @var{LU}. Second, the lower triangular factor of
679 m is the lower triangular part of @var{LU} with the diagonal entries
680 replaced by all ones. Third, the upper triangular factor of @var{M} is the
681 upper triangular part of @var{LU}.
683 (2) When the field is either @code{floatfield} or @code{complexfield}, the
684 numbers @var{lower-cnd} and @var{upper-cnd} are lower and upper bounds for
685 the infinity norm condition number of @var{M}. For all fields, the
686 condition number might not be estimated; for such fields, @code{lu_factor}
687 returns a two item list. Both the lower and upper bounds can differ from
688 their true values by arbitrarily large factors. (See also @mref{mat_cond}.)
690 The argument @var{M} must be a square matrix.
692 The optional argument @var{fld} must be a symbol that determines a ring or
693 field. The pre-defined fields and rings are:
695 (a) @code{generalring} -- the ring of Maxima expressions,
697 (b) @code{floatfield} -- the field of floating point numbers of the
700 (c) @code{complexfield} -- the field of complex floating point numbers of
703 (d) @code{crering} -- the ring of Maxima CRE expressions,
705 (e) @code{rationalfield} -- the field of rational numbers,
707 (f) @code{runningerror} -- track the all floating point rounding errors,
709 (g) @code{noncommutingring} -- the ring of Maxima expressions where
710 multiplication is the non-commutative dot
713 When the field is @code{floatfield}, @code{complexfield}, or
714 @code{runningerror}, the algorithm uses partial pivoting; for all
715 other fields, rows are switched only when needed to avoid a zero
718 Floating point addition arithmetic isn't associative, so the meaning
719 of 'field' differs from the mathematical definition.
721 A member of the field @code{runningerror} is a two member Maxima list
722 of the form @code{[x,n]},where @var{x} is a floating point number and
723 @code{n} is an integer. The relative difference between the 'true'
724 value of @code{x} and @code{x} is approximately bounded by the machine
725 epsilon times @code{n}. The running error bound drops some terms that
726 of the order the square of the machine epsilon.
728 There is no user-interface for defining a new field. A user that is
729 familiar with Common Lisp should be able to define a new field. To do
730 this, a user must define functions for the arithmetic operations and
731 functions for converting from the field representation to Maxima and
732 back. Additionally, for ordered fields (where partial pivoting will be
733 used), a user must define functions for the magnitude and for
734 comparing field members. After that all that remains is to define a
735 Common Lisp structure @code{mring}. The file @code{mring} has many
738 To compute the factorization, the first task is to convert each matrix
739 entry to a member of the indicated field. When conversion isn't
740 possible, the factorization halts with an error message. Members of
741 the field needn't be Maxima expressions. Members of the
742 @code{complexfield}, for example, are Common Lisp complex numbers. Thus
743 after computing the factorization, the matrix entries must be
744 converted to Maxima expressions.
746 See also @mref{get_lu_factors}.
751 @c w[i,j] := random (1.0) + %i * random (1.0);
753 @c M : genmatrix (w, 100, 100)$
754 @c lu_factor (M, complexfield)$
755 @c lu_factor (M, generalring)$
757 @c M : matrix ([1 - z, 3], [3, 8 - z]);
758 @c lu_factor (M, generalring);
759 @c get_lu_factors (%);
760 @c %[1] . %[2] . %[3];
763 (%i1) w[i,j] := random (1.0) + %i * random (1.0);
764 (%o1) w := random(1.) + %i random(1.)
766 (%i2) showtime : true$
767 Evaluation took 0.00 seconds (0.00 elapsed)
768 (%i3) M : genmatrix (w, 100, 100)$
769 Evaluation took 7.40 seconds (8.23 elapsed)
770 (%i4) lu_factor (M, complexfield)$
771 Evaluation took 28.71 seconds (35.00 elapsed)
772 (%i5) lu_factor (M, generalring)$
773 Evaluation took 109.24 seconds (152.10 elapsed)
774 (%i6) showtime : false$
776 (%i7) M : matrix ([1 - z, 3], [3, 8 - z]);
780 (%i8) lu_factor (M, generalring);
783 (%o8) [[ 3 9 ], [1, 2], generalring]
784 [ ----- - z - ----- + 8 ]
786 (%i9) get_lu_factors (%);
789 (%o9) [[ ], [ 3 ], [ 9 ]]
790 [ 0 1 ] [ ----- 1 ] [ 0 - z - ----- + 8 ]
792 (%i10) %[1] . %[2] . %[3];
798 @opencatbox{Categories:}
799 @category{Matrix decompositions}
800 @category{Package linearalgebra}
804 @c -----------------------------------------------------------------------------
806 @deffn {Function} mat_cond @
807 @fname{mat_cond} (@var{M}, 1) @
808 @fname{mat_cond} (@var{M}, inf)
810 Return the @var{p}-norm matrix condition number of the matrix
811 @var{m}. The allowed values for @var{p} are 1 and @var{inf}. This
812 function uses the LU factorization to invert the matrix @var{m}. Thus
813 the running time for @code{mat_cond} is proportional to the cube of
814 the matrix size; @code{lu_factor} determines lower and upper bounds
815 for the infinity norm condition number in time proportional to the
816 square of the matrix size.
818 @opencatbox{Categories:}
819 @category{Package linearalgebra}
823 @c -----------------------------------------------------------------------------
825 @deffn {Function} mat_norm @
826 @fname{mat_norm} (@var{M}, 1) @
827 @fname{mat_norm} (@var{M}, inf) @
828 @fname{mat_norm} (@var{M}, frobenius)
830 Return the matrix @var{p}-norm of the matrix @var{M}. The allowed values for
831 @var{p} are 1, @code{inf}, and @code{frobenius} (the Frobenius matrix norm).
832 The matrix @var{M} should be an unblocked matrix.
834 @opencatbox{Categories:}
835 @category{Package linearalgebra}
839 @c -----------------------------------------------------------------------------
840 @anchor{linearalgebra_matrixp}
841 @deffn {Function} matrixp @
842 @fname{matrixp} (@var{e}, @var{p}) @
843 @fname{matrixp} (@var{e})
845 Given an optional argument @var{p}, return @code{true} if @var{e} is
846 a matrix and @var{p} evaluates to @code{true} for every matrix element.
847 When @code{matrixp} is not given an optional argument, return @code{true}
848 if @code{e} is a matrix. In all other cases, return @code{false}.
850 See also @mref{blockmatrixp}
852 @opencatbox{Categories:}
853 @category{Package linearalgebra}
854 @category{Predicate functions}
858 @c -----------------------------------------------------------------------------
860 @deffn {Function} matrix_size (@var{M})
862 Return a two member list that gives the number of rows and columns, respectively
863 of the matrix @var{M}.
865 @opencatbox{Categories:}
866 @category{Package linearalgebra}
870 @c -----------------------------------------------------------------------------
871 @anchor{mat_fullunblocker}
872 @deffn {Function} mat_fullunblocker (@var{M})
874 If @var{M} is a block matrix, unblock the matrix to all levels. If @var{M} is
875 a matrix, return @var{M}; otherwise, signal an error.
877 @opencatbox{Categories:}
878 @category{Package linearalgebra}
882 @c -----------------------------------------------------------------------------
884 @deffn {Function} mat_trace (@var{M})
886 Return the trace of the matrix @var{M}. If @var{M} isn't a matrix, return a
887 noun form. When @var{M} is a block matrix, @code{mat_trace(M)} returns
888 the same value as does @code{mat_trace(mat_unblocker(m))}.
890 @opencatbox{Categories:}
891 @category{Package linearalgebra}
895 @c -----------------------------------------------------------------------------
896 @anchor{mat_unblocker}
897 @deffn {Function} mat_unblocker (@var{M})
899 If @var{M} is a block matrix, unblock @var{M} one level. If @var{M} is a
900 matrix, @code{mat_unblocker (M)} returns @var{M}; otherwise, signal an error.
902 Thus if each entry of @var{M} is matrix, @code{mat_unblocker (M)} returns an
903 unblocked matrix, but if each entry of @var{M} is a block matrix,
904 @code{mat_unblocker (M)} returns a block matrix with one less level of blocking.
906 If you use block matrices, most likely you'll want to set
907 @code{matrix_element_mult} to @code{"."} and @code{matrix_element_transpose} to
908 @code{'transpose}. See also @mref{mat_fullunblocker}.
913 @c A : matrix ([1, 2], [3, 4]);
914 @c B : matrix ([7, 8], [9, 10]);
916 @c mat_unblocker (%);
919 (%i1) A : matrix ([1, 2], [3, 4]);
923 (%i2) B : matrix ([7, 8], [9, 10]);
927 (%i3) matrix ([A, B]);
933 (%i4) mat_unblocker (%);
939 @opencatbox{Categories:}
940 @category{Package linearalgebra}
944 @c -----------------------------------------------------------------------------
946 @deffn {Function} nullspace (@var{M})
948 If @var{M} is a matrix, return @code{span (v_1, ..., v_n)}, where the set
949 @code{@{v_1, ..., v_n@}} is a basis for the nullspace of @var{M}. The span of
950 the empty set is @code{@{0@}}. Thus, when the nullspace has only one member,
951 return @code{span ()}.
953 @opencatbox{Categories:}
954 @category{Package linearalgebra}
958 @c -----------------------------------------------------------------------------
960 @deffn {Function} nullity (@var{M})
962 If @var{M} is a matrix, return the dimension of the nullspace of @var{M}.
964 @opencatbox{Categories:}
965 @category{Package linearalgebra}
969 @c -----------------------------------------------------------------------------
970 @anchor{orthogonal_complement}
971 @deffn {Function} orthogonal_complement (@var{v_1}, @dots{}, @var{v_n})
973 Return @code{span (u_1, ..., u_m)}, where the set @code{@{u_1, ..., u_m@}} is a
974 basis for the orthogonal complement of the set @code{(v_1, ..., v_n)}.
976 Each vector @var{v_1} through @var{v_n} must be a column vector.
978 @opencatbox{Categories:}
979 @category{Package linearalgebra}
983 @c -----------------------------------------------------------------------------
984 @anchor{polytocompanion}
985 @deffn {Function} polytocompanion (@var{p}, @var{x})
987 If @var{p} is a polynomial in @var{x}, return the companion matrix of @var{p}.
988 For a monic polynomial @var{p} of degree @var{n}, we have
989 @code{@var{p} = (-1)^@var{n} charpoly (polytocompanion (@var{p}, @var{x}))}.
991 When @var{p} isn't a polynomial in @var{x}, signal an error.
993 @opencatbox{Categories:}
994 @category{Package linearalgebra}
998 @c -----------------------------------------------------------------------------
999 @anchor{ptringularize}
1000 @deffn {Function} ptriangularize (@var{M}, @var{v})
1002 If @var{M} is a matrix with each entry a polynomial in @var{v}, return
1003 a matrix @var{M2} such that
1005 (1) @var{M2} is upper triangular,
1007 (2) @code{@var{M2} = @var{E_n} ... @var{E_1} @var{M}},
1008 where @var{E_1} through @var{E_n} are elementary matrices
1009 whose entries are polynomials in @var{v},
1011 (3) @code{|det (@var{M})| = |det (@var{M2})|},
1013 Note: This function doesn't check that every entry is a polynomial in @var{v}.
1015 @opencatbox{Categories:}
1016 @category{Package linearalgebra}
1020 @c -----------------------------------------------------------------------------
1022 @deffn {Function} rowop (@var{M}, @var{i}, @var{j}, @var{theta})
1024 If @var{M} is a matrix, return the matrix that results from doing the
1025 row operation @code{R_i <- R_i - theta * R_j}. If @var{M} doesn't have a row
1026 @var{i} or @var{j}, signal an error.
1028 @opencatbox{Categories:}
1029 @category{Package linearalgebra}
1034 @anchor{linalg_rank}
1035 @deffn {Function} linalg_rank (@var{M})
1037 Return the rank of the matrix @var{M}. This function is equivalent to
1038 function @mref{rank}, but it uses a different algorithm: it finds the
1039 @mref{columnspace} of the matrix and counts its elements, since the rank
1040 of a matrix is the dimension of its column space.
1043 @c linalg_rank(matrix([1,2],[2,4]));
1044 @c linalg_rank(matrix([1,b],[c,d]));
1048 (%i1) linalg_rank(matrix([1,2],[2,4]));
1052 (%i2) linalg_rank(matrix([1,b],[c,d]));
1057 @opencatbox{Categories:}
1058 @category{Package linearalgebra}
1062 @c -----------------------------------------------------------------------------
1064 @deffn {Function} rowswap (@var{M}, @var{i}, @var{j})
1066 If @var{M} is a matrix, swap rows @var{i} and @var{j}. If @var{M} doesn't
1067 have a row @var{i} or @var{j}, signal an error.
1069 @opencatbox{Categories:}
1070 @category{Package linearalgebra}
1074 @c -----------------------------------------------------------------------------
1076 @deffn {Function} toeplitz @
1077 @fname{toeplitz} (@var{col}) @
1078 @fname{toeplitz} (@var{col}, @var{row})
1080 Return a Toeplitz matrix @var{T}. The first first column of @var{T} is
1081 @var{col}; except for the first entry, the first row of @var{T} is @var{row}.
1082 The default for @var{row} is complex conjugate of @var{col}. Example:
1085 @c toeplitz([1,2,3],[x,y,z]);
1086 @c toeplitz([1,1+%i]);
1089 (%i1) toeplitz([1,2,3],[x,y,z]);
1097 (%i2) toeplitz([1,1+%i]);
1104 @opencatbox{Categories:}
1105 @category{Package linearalgebra}
1109 @c -----------------------------------------------------------------------------
1110 @anchor{vandermonde_matrix}
1111 @deffn {Function} vandermonde_matrix ([@var{x_1}, ..., @var{x_n}])
1113 Return a @var{n} by @var{n} matrix whose @var{i}-th row is
1114 @code{[1, @var{x_i}, @var{x_i}^2, ... @var{x_i}^(@var{n}-1)]}.
1116 @opencatbox{Categories:}
1117 @category{Package linearalgebra}
1121 @c -----------------------------------------------------------------------------
1123 @deffn {Function} zerofor @
1124 @fname{zerofor} (@var{M}) @
1125 @fname{zerofor} (@var{M}, @var{fld})
1127 Return a zero matrix that has the same shape as the matrix
1128 @var{M}. Every entry of the zero matrix is the
1129 additive identity of the field @var{fld}; the default for
1130 @var{fld} is @var{generalring}.
1132 The first argument @var{M} should be a square matrix or a
1133 non-matrix. When @var{M} is a matrix, each entry of @var{M} can be a
1134 square matrix -- thus @var{M} can be a blocked Maxima matrix. The
1135 matrix can be blocked to any (finite) depth.
1137 See also @mref{identfor}
1139 @opencatbox{Categories:}
1140 @category{Package linearalgebra}
1144 @c -----------------------------------------------------------------------------
1145 @anchor{zeromatrixp}
1146 @deffn {Function} zeromatrixp (@var{M})
1148 If @var{M} is not a block matrix, return @code{true} if
1149 @code{is (equal (@var{e}, 0))} is true for each element @var{e} of the matrix
1150 @var{M}. If @var{M} is a block matrix, return @code{true} if @code{zeromatrixp}
1151 evaluates to @code{true} for each element of @var{e}.
1153 @opencatbox{Categories:}
1154 @category{Package linearalgebra}
1155 @category{Predicate functions}