matrix.tex 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. \chapter{Matrix Calculations} \index{Matrix calculations}
  2. A very powerful feature of {\REDUCE} is the ease with which matrix
  3. calculations can be performed. To extend our syntax to this class of
  4. calculations we need to add another prefix operator, {\tt MAT},
  5. \ttindex{MAT} and a further
  6. variable and expression type as follows:
  7. \section{MAT Operator}\ttindex{MAT}
  8. This prefix operator is used to represent $n\times m$ matrices. {\tt
  9. MAT} has {\em n} arguments interpreted as rows of the matrix, each of
  10. which is a list of {\em m} expressions representing elements in that row.
  11. For example, the matrix
  12. \[ \left( \begin{array}{lcr} a & b & c \\ d & e & f \end{array} \right) \]
  13. would be written as {\tt mat((a,b,c),(d,e,f))}.
  14. Note that the single column matrix
  15. \[ \left( \begin{array}{c} x \\ y \end{array} \right) \]
  16. becomes {\tt mat((x),(y))}. The inside parentheses are required to
  17. distinguish it from the single row matrix
  18. \[ \left( \begin{array}{lr} x & y \end{array} \right) \]
  19. that would be written as {\tt mat((x,y))}.
  20. \section{Matrix Variables}
  21. An identifier may be declared a matrix variable by the declaration {\tt
  22. MATRIX}.\ttindex{MATRIX}
  23. The size of the matrix may be declared explicitly in the matrix
  24. declaration, or by default in assigning such a variable to a matrix
  25. expression. For example,
  26. \begin{verbatim}
  27. matrix x(2,1),y(3,4),z;
  28. \end{verbatim}
  29. declares {\tt X} to be a 2 x 1 (column) matrix, {\tt Y} to be a 3 x 4
  30. matrix and {\tt Z} a matrix whose size is to be declared later.
  31. Matrix declarations can appear anywhere in a program. Once a symbol is
  32. declared to name a matrix, it can not also be used to name an array,
  33. operator or a procedure, or used as an ordinary variable. It can however
  34. be redeclared to be a matrix, and its size may be changed at that time.
  35. Note however that matrices once declared are {\em global\/} in scope, and so
  36. can then be referenced anywhere in the program. In other words, a
  37. declaration within a block (or a procedure) does not limit the scope of
  38. the matrix to that block, nor does the matrix go away on exiting the block
  39. (use {\tt CLEAR} instead for this purpose). An element of a matrix is
  40. referred to in the expected manner; thus {\tt x(1,1)} gives the first
  41. element of the matrix {\tt X} defined above. References to elements of a
  42. matrix whose size has not yet been declared leads to an error. All
  43. elements of a matrix whose size is declared are initialized to 0. As a
  44. result, a matrix element has an {\em instant evaluation\/}\index{Instant
  45. evaluation} property and cannot stand for itself. If this is required,
  46. then an operator should be used to name the matrix elements as in:
  47. \begin{verbatim}
  48. matrix m; operator x; m := mat((x(1,1),x(1,2));
  49. \end{verbatim}
  50. \section{Matrix Expressions}
  51. These follow the normal rules of matrix algebra as defined by the
  52. following syntax:\ttindex{MAT}
  53. \begin{verbatim}
  54. <matrix expression> ::=
  55. MAT<matrix description>|<matrix variable>|
  56. <scalar expression>*<matrix expression>|
  57. <matrix expression>*<matrix expression>
  58. <matrix expression>+<matrix expression>|
  59. <matrix expression>^<integer>|
  60. <matrix expression>/<matrix expression>
  61. \end{verbatim}
  62. Sums and products of matrix expressions must be of compatible size;
  63. otherwise an error will result during their evaluation. Similarly, only
  64. square matrices may be raised to a power. A negative power is computed as
  65. the inverse of the matrix raised to the corresponding positive power.
  66. {\tt a/b} is interpreted as {\tt a*b\verb|^|(-1)}.
  67. {\it Examples:}
  68. Assuming {\tt X} and {\tt Y} have been declared as matrices, the following
  69. are matrix expressions
  70. \begin{verbatim}
  71. y
  72. y^2*x-3*y^(-2)*x
  73. y + mat((1,a),(b,c))/2
  74. \end{verbatim}
  75. The computation of the quotient of two matrices normally uses a two-step
  76. elimination method due to Bareiss. An alternative method using Cramer's
  77. method is also available. This is usually less efficient than the Bareiss
  78. method unless the matrices are large and dense, although we have no solid
  79. statistics on this as yet. To use Cramer's method instead, the switch
  80. {\tt CRAMER}\ttindex{CRAMER} should be turned on.
  81. \section{Operators with Matrix Arguments}
  82. The operator {\tt LENGTH}\ttindex{LENGTH} applied to a matrix returns a
  83. list of the number of rows and columns in the matrix. Other operators
  84. useful in matrix calculations are defined in the following subsections.
  85. Attention is also drawn to the LINALG
  86. \extendedmanual{(chapter~\ref{LINALG})} and NORMFORM
  87. \extendedmanual{(chapter~\ref{NORMFORM})} packages.
  88. \subsection{DET Operator}\ttindex{DET}
  89. Syntax:
  90. \begin{verbatim}
  91. DET(EXPRN:matrix_expression):algebraic.
  92. \end{verbatim}
  93. The operator {\tt DET} is used to represent the determinant of a square
  94. matrix expression. E.g.,
  95. \begin{verbatim}
  96. det(y^2)
  97. \end{verbatim}
  98. is a scalar expression whose value is the determinant of the square of the
  99. matrix {\tt Y}, and
  100. \begin{verbatim}
  101. det mat((a,b,c),(d,e,f),(g,h,j));
  102. \end{verbatim}
  103. is a scalar expression whose value is the determinant of the matrix
  104. \[ \left( \begin{array}{lcr} a & b & c \\ d & e & f \\ g & h & j
  105. \end{array} \right) \]
  106. Determinant expressions have the {\em instant evaluation\/} property.
  107. \index{Instant evaluation} In other words, the statement
  108. \begin{verbatim}
  109. let det mat((a,b),(c,d)) = 2;
  110. \end{verbatim}
  111. sets the {\em value\/} of the determinant to 2, and does not set up a rule
  112. for the determinant itself.
  113. \subsection{MATEIGEN Operator}\ttindex{MATEIGEN}
  114. Syntax:
  115. \begin{verbatim}
  116. MATEIGEN(EXPRN:matrix_expression,ID):list.
  117. \end{verbatim}
  118. {\tt MATEIGEN} calculates the eigenvalue equation and the corresponding
  119. eigenvectors of a matrix, using the variable {\tt ID} to denote the
  120. eigenvalue. A square free decomposition of the characteristic polynomial
  121. is carried out. The result is a list of lists of 3 elements, where the
  122. first element is a square free factor of the characteristic polynomial,
  123. the second its multiplicity and the third the corresponding eigenvector
  124. (as an {\em n} by 1 matrix). If the square free decomposition was
  125. successful, the product of the first elements in the lists is the minimal
  126. polynomial. In the case of degeneracy, several eigenvectors can exist for
  127. the same eigenvalue, which manifests itself in the appearance of more than
  128. one arbitrary variable in the eigenvector. To extract the various parts
  129. of the result use the operations defined on lists.
  130. {\it Example:}
  131. The command
  132. \begin{verbatim}
  133. mateigen(mat((2,-1,1),(0,1,1),(-1,1,1)),eta);
  134. \end{verbatim}
  135. gives the output
  136. \begin{verbatim}
  137. {{ETA - 1,2,
  138. [ARBCOMPLEX(1)]
  139. [ ]
  140. [ARBCOMPLEX(1)]
  141. [ ]
  142. [ 0 ]
  143. },
  144. {ETA - 2,1,
  145. [ 0 ]
  146. [ ]
  147. [ARBCOMPLEX(2)]
  148. [ ]
  149. [ARBCOMPLEX(2)]
  150. }}
  151. \end{verbatim}
  152. \subsection{TP Operator}\ttindex{TP}
  153. Syntax:
  154. \begin{verbatim}
  155. TP(EXPRN:matrix_expression):matrix.
  156. \end{verbatim}
  157. This operator takes a single matrix argument and returns its transpose.
  158. \subsection{Trace Operator}\ttindex{TRACE}
  159. Syntax:
  160. \begin{verbatim}
  161. TRACE(EXPRN:matrix_expression):algebraic.
  162. \end{verbatim}
  163. The operator {\tt TRACE} is used to represent the trace of a square matrix.
  164. \subsection{Matrix Cofactors}\ttindex{COFACTOR}
  165. Syntax:
  166. \begin{verbatim}
  167. COFACTOR(EXPRN:matrix_expression,ROW:integer,COLUMN:integer):
  168. algebraic
  169. \end{verbatim}
  170. The operator {\tt COFACTOR} returns the cofactor of the element in row
  171. {\tt ROW} and column {\tt COLUMN} of the matrix {\tt MATRIX}. Errors occur
  172. if {\tt ROW} or {\tt COLUMN} do not simplify to integer expressions or if
  173. {\tt MATRIX} is not square.
  174. \subsection{NULLSPACE Operator}\ttindex{NULLSPACE}
  175. Syntax:
  176. \begin{verbatim}
  177. NULLSPACE(EXPRN:matrix_expression):list
  178. \end{verbatim}
  179. {\tt NULLSPACE} calculates for a matrix {\tt A} a list of linear
  180. independent vectors (a basis) whose linear combinations satisfy the
  181. equation $A x = 0$. The basis is provided in a form such that as many
  182. upper components as possible are isolated.
  183. Note that with {\tt b := nullspace a} the expression {\tt length b} is the
  184. {\em nullity\/} of A, and that {\tt second length a - length b} calculates the
  185. {\em rank\/} of A. The rank of a matrix expression can also be found more
  186. directly by the {\tt RANK} operator described below.
  187. {\it Example:} The command
  188. \begin{verbatim}
  189. nullspace mat((1,2,3,4),(5,6,7,8));
  190. \end{verbatim}
  191. gives the output
  192. \begin{verbatim}
  193. {
  194. [ 1 ]
  195. [ ]
  196. [ 0 ]
  197. [ ]
  198. [ - 3]
  199. [ ]
  200. [ 2 ]
  201. ,
  202. [ 0 ]
  203. [ ]
  204. [ 1 ]
  205. [ ]
  206. [ - 2]
  207. [ ]
  208. [ 1 ]
  209. }
  210. \end{verbatim}
  211. In addition to the {\REDUCE} matrix form, {\tt NULLSPACE} accepts as input a
  212. matrix given as a list of lists, that is interpreted as a row matrix. If
  213. that form of input is chosen, the vectors in the result will be
  214. represented by lists as well. This additional input syntax facilitates
  215. the use of {\tt NULLSPACE} in applications different from classical linear
  216. algebra.
  217. \subsection{RANK Operator}\ttindex{RANK}
  218. Syntax:
  219. \begin{verbatim}
  220. RANK(EXPRN:matrix_expression):integer
  221. \end{verbatim}
  222. {\tt RANK} calculates the rank of its argument, that, like {\tt NULLSPACE}
  223. can either be a standard matrix expression, or a list of lists, that can
  224. be interpreted either as a row matrix or a set of equations.
  225. {\tt Example:}
  226. \begin{verbatim}
  227. rank mat((a,b,c),(d,e,f));
  228. \end{verbatim}
  229. returns the value 2.
  230. \section{Matrix Assignments} \index{Matrix assignment}
  231. Matrix expressions may appear in the right-hand side of assignment
  232. statements. If the left-hand side of the assignment, which must be a
  233. variable, has not already been declared a matrix, it is declared by default
  234. to the size of the right-hand side. The variable is then set to the value
  235. of the right-hand side.
  236. Such an assignment may be used very conveniently to find the solution of a
  237. set of linear equations. For example, to find the solution of the
  238. following set of equations
  239. \begin{verbatim}
  240. a11*x(1) + a12*x(2) = y1
  241. a21*x(1) + a22*x(2) = y2
  242. \end{verbatim}
  243. we simply write
  244. \begin{verbatim}
  245. x := 1/mat((a11,a12),(a21,a22))*mat((y1),(y2));
  246. \end{verbatim}
  247. \section{Evaluating Matrix Elements}
  248. Once an element of a matrix has been assigned, it may be referred to in
  249. standard array element notation. Thus {\tt y(2,1)} refers to the element
  250. in the second row and first column of the matrix {\tt Y}.