matrix.tex 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. \section{Matrix Operations}
  2. \begin{Operator}{COFACTOR}
  3. \index{matrix}
  4. The operator \name{cofactor} returns the cofactor of the element in row
  5. \meta{row} and column \meta{column} of a \nameref{matrix}. Errors occur
  6. if \meta{row} or \meta{column} do not evaluate to integer expressions or if
  7. the matrix is not square.
  8. \begin{Syntax}
  9. \name{cofactor}\(\meta{matrix\_expression},\meta{row},\meta{column}\)
  10. % COFACTOR(EXPRN:matrix_expression,ROW:integer,COLUMN:integer):algebraic
  11. \end{Syntax}
  12. \begin{Examples}
  13. cofactor(mat((a,b,c),(d,e,f),(p,q,r)),2,2); & A*R - C*P \\
  14. cofactor(mat((a,b,c),(d,e,f)),1,1); & ***** non-square matrix
  15. \end{Examples}
  16. \end{Operator}
  17. \begin{Operator}{DET}
  18. \index{matrix}\index{determinant}
  19. The \name{det} operator returns the determinant of its
  20. (square \nameref{matrix}) argument.
  21. \begin{Syntax}
  22. \name{det}\(\meta{expression}\) or \name{det} \meta{expression}
  23. \end{Syntax}
  24. \meta{expression} must evaluate to a square matrix.
  25. \begin{Examples}
  26. matrix m,n; \\
  27. m := mat((a,b),(c,d)); & \begin{multilineoutput}{4cm}
  28. M(1,1) := A
  29. M(1,2) := B
  30. M(2,1) := C
  31. M(2,2) := D
  32. \end{multilineoutput}\\
  33. det m; & A*D - B*C \\
  34. n := mat((1,2),(1,2)); & \begin{multilineoutput}{4cm}
  35. N(1,1) := 1
  36. N(1,2) := 2
  37. N(2,1) := 1
  38. N(2,2) := 2
  39. \end{multilineoutput} \\
  40. det(n); & 0 \\
  41. det(5); & 5
  42. \end{Examples}
  43. \begin{Comments}
  44. Given a numerical argument, \name{det} returns the number. However, given a
  45. variable name that has not been declared of type matrix, or a non-square
  46. matrix, \name{det} returns an error message.
  47. \end{Comments}
  48. \end{Operator}
  49. \begin{Operator}{MAT}
  50. \index{matrix}
  51. The \name{mat} operator is used to represent a two-dimensional
  52. \nameref{matrix}.
  53. \begin{Syntax}
  54. \name{mat}\(\(\meta{expr}\{,\meta{expr}\}\optional\)%
  55. \{\(\meta{expr}\{\name{,}\meta{expr}\}\optional\)\}\optional\)
  56. \end{Syntax}
  57. \meta{expr} may be any valid REDUCE scalar expression.
  58. \begin{Examples}
  59. mat((1,2),(3,4)); &
  60. \begin{multilineoutput}{6cm}
  61. MAT(1,1) := 1
  62. MAT(2,3) := 2
  63. MAT(2,1) := 3
  64. MAT(2,2) := 4
  65. \end{multilineoutput}\\
  66. mat(2,1); & \begin{multilineoutput}{6cm}
  67. ***** Matrix mismatch
  68. Cont? (Y or N)
  69. \end{multilineoutput}\\
  70. matrix qt; \\
  71. qt := ws; & \begin{multilineoutput}{6cm}
  72. QT(1,1) := 1
  73. QT(1,2) := 2
  74. QT(2,1) := 3
  75. QT(2,2) := 4
  76. \end{multilineoutput}\\
  77. matrix a,b; \\
  78. a := mat((x),(y),(z)); & \begin{multilineoutput}{6cm}
  79. A(1,1) := X
  80. A(2,1) := Y
  81. A(3,1) := Z
  82. \end{multilineoutput}\\
  83. b := mat((sin x,cos x,1)); & \begin{multilineoutput}{6cm}
  84. B(1,1) := SIN(X)
  85. B(1,2) := COS(X)
  86. B(1,3) := 1
  87. \end{multilineoutput}
  88. \end{Examples}
  89. \begin{Comments}
  90. Matrices need not have a size declared (unlike arrays). \name{mat}
  91. redimensions a matrix variable as needed. It is necessary, of course,
  92. that all rows be the same length. An anonymous matrix, as shown in the
  93. first example, must be named before it can be referenced (note error
  94. message). When using \name{mat} to fill a \IFTEX{$1 \times n$}{1 x n}
  95. matrix, the row of values must be inside a second set of parentheses, to
  96. eliminate ambiguity.
  97. \end{Comments}
  98. \end{Operator}
  99. \begin{Operator}{MATEIGEN}
  100. \index{matrix}\index{eigenvalue}
  101. The \name{mateigen} operator calculates the eigenvalue equation and the
  102. corresponding eigenvectors of a \nameref{matrix}.
  103. \begin{Syntax}
  104. \name{mateigen}\(\meta{matrix-id},\meta{tag-id}\)
  105. \end{Syntax}
  106. \meta{matrix-id} must be a declared matrix of values, and \meta{tag-id} must be
  107. a legal REDUCE identifier.
  108. \begin{Examples}
  109. aa := mat((2,5),(1,0))\$ \\
  110. mateigen(aa,alpha); &
  111. \begin{multilineoutput}{2cm}
  112. \{\{ALPHA^{2} - 2*ALPHA - 5,
  113. 1,
  114. MAT(1,1) := \rfrac{5*ARBCOMPLEX(1)}{ALPHA - 2},
  115. MAT(2,1) := ARBCOMPLEX(1)
  116. \}\}
  117. \end{multilineoutput}\\
  118. charpoly := first first ws; & CHARPOLY := ALPHA^{2} - 2*ALPHA - 5 \\
  119. bb := mat((1,0,1),(1,1,0),(0,0,1))\$ \\
  120. mateigen(bb,lamb); &
  121. \begin{multilineoutput}{6cm}
  122. \{\{LAMB - 1,3,
  123. [ 0 ]
  124. [ARBCOMPLEX(2)]
  125. [ 0 ]
  126. \}\}
  127. \end{multilineoutput}
  128. \end{Examples}
  129. \begin{Comments}
  130. The \name{mateigen} operator returns a list of lists of three
  131. elements. The first element is a square free factor of the characteristic
  132. polynomial; the second element is its multiplicity; and the third element
  133. is the corresponding eigenvector. If the characteristic polynomial can be
  134. completely factored, the product of the first elements of all the sublists
  135. will produce the minimal polynomial. You can access the various parts of
  136. the answer with the usual list access operators.
  137. If the matrix is degenerate, more than one eigenvector can be produced for
  138. the same eigenvalue, as shown by more than one arbitrary variable in the
  139. eigenvector. The identification numbers of the arbitrary complex variables
  140. shown in the examples above may not be the same as yours. Note that since
  141. \name{lambda} is a reserved word in REDUCE, you cannot use it as a {\it
  142. tag-id} for this operator.
  143. \end{Comments}
  144. \end{Operator}
  145. \begin{Declaration}{MATRIX}
  146. Identifiers are declared to be of type \name{matrix}.
  147. \begin{Syntax}
  148. \name{matrix} \meta{identifier} \&option \(\meta{index},\meta{index}\)\\
  149. \{,\meta{identifier} \&option
  150. \(\meta{index},\meta{index}\)\}\optional
  151. \end{Syntax}
  152. \meta{identifier} must not be an already-defined operator or array or
  153. the name of a scalar variable. Dimensions are optional, and if used appear
  154. inside parentheses. \meta{index} must be a positive integer.
  155. \begin{Examples}
  156. matrix a,b(1,4),c(4,4); \\
  157. b(1,1); & 0 \\
  158. a(1,1); & ***** Matrix A not set \\
  159. a := mat((x0,y0),(x1,y1)); & \begin{multilineoutput}{6cm}
  160. A(1,1) := X0
  161. A(1,2) := Y0
  162. A(2,1) := X0
  163. A(2,2) := X1
  164. \end{multilineoutput}\\
  165. length a; & \{2,2\} \\
  166. b := a**2; & \begin{multilineoutput}{6cm}
  167. B(1,1) := X0^{2} + X1*Y0
  168. B(1,2) := Y0*(X0 + Y1)
  169. B(2,1) := X1*(X0 + Y1)
  170. B(2,2) := X1*Y0 + Y1^{2}
  171. \end{multilineoutput}
  172. \end{Examples}
  173. \begin{Comments}
  174. When a matrix variable has not been dimensioned, matrix elements cannot be
  175. referenced until the matrix is set by the \nameref{mat} operator. When a
  176. matrix is dimensioned in its declaration, matrix elements are set to 0.
  177. Matrix elements cannot stand for themselves. When you use \nameref{let} on
  178. a matrix element, there is no effect unless the element contains a
  179. constant, in which case an error message is returned. The same behavior
  180. occurs with \nameref{clear}. Do \meta{not} use \nameref{clear} to try to
  181. set a matrix element to 0. \nameref{let} statements can be applied to
  182. matrices as a whole, if the right-hand side of the expression is a matrix
  183. expression, and the left-hand side identifier has been declared to be a matrix.
  184. Arithmetical operators apply to matrices of the correct dimensions. The
  185. operators \name{+} and \name{-} can be used with matrices of the same
  186. dimensions. The operator \name{*} can be used to multiply
  187. \IFTEX{$m \times n$}{m x n} matrices by \IFTEX{$n \times p$}{n x p}
  188. matrices. Matrix multiplication is non-commutative. Scalars can also be
  189. multiplied with matrices, with the result that each element of the matrix
  190. is multiplied by the scalar. The operator \name{/} applied to two
  191. matrices computes the first matrix multiplied by the inverse of the
  192. second, if the inverse exists, and produces an error message otherwise.
  193. Matrices can be divided by scalars, which results in dividing each element
  194. of the matrix. Scalars can also be divided by matrices when the matrices
  195. are invertible, and the result is the multiplication of the scalar by the
  196. inverse of the matrix. Matrix inverses can by found by \name{1/A} or
  197. \name{/A}, where \name{A} is a matrix. Square matrices can be raised to
  198. positive integer powers, and also to negative integer powers if they are
  199. nonsingular.
  200. When a matrix variable is assigned to the results of a calculation, the
  201. matrix is redimensioned if necessary.
  202. \end{Comments}
  203. \end{Declaration}
  204. \begin{Operator}{NULLSPACE}
  205. \index{matrix}
  206. \begin{Syntax}
  207. \name{nullspace}(\meta{matrix\_expression})
  208. \end{Syntax}
  209. \meta{nullspace} calculates for its \nameref{matrix} argument,
  210. \name{a}, a list of
  211. linear independent vectors (a basis) whose linear combinations satisfy the
  212. equation $a x = 0$. The basis is provided in a form such that as many
  213. upper components as possible are isolated.
  214. \begin{Examples}
  215. nullspace mat((1,2,3,4),(5,6,7,8)); &
  216. \begin{multilineoutput}{6cm}
  217. \{
  218. [ 1 ]
  219. [ ]
  220. [ 0 ]
  221. [ ]
  222. [ - 3]
  223. [ ]
  224. [ 2 ]
  225. ,
  226. [ 0 ]
  227. [ ]
  228. [ 1 ]
  229. [ ]
  230. [ - 2]
  231. [ ]
  232. [ 1 ]
  233. \}
  234. \end{multilineoutput}
  235. \end{Examples}
  236. \begin{Comments}
  237. Note that with \name{b := nullspace a}, the expression \name{length b} is
  238. the {\em nullity\/} of A, and that \name{second length a - length b}
  239. calculates the {\em rank\/} of A. The rank of a matrix expression can
  240. also be found more directly by the \nameref{rank} operator.
  241. In addition to the REDUCE matrix form, \name{nullspace} accepts as input a
  242. matrix given as a \nameref{list} of lists, that is interpreted as a row matrix. If
  243. that form of input is chosen, the vectors in the result will be
  244. represented by lists as well. This additional input syntax facilitates
  245. the use of \name{nullspace} in applications different from classical linear
  246. algebra.
  247. \end{Comments}
  248. \end{Operator}
  249. \begin{Operator}{RANK}
  250. \index{matrix}
  251. \begin{Syntax}
  252. \name{rank}(\meta{matrix\_expression})
  253. \end{Syntax}
  254. \name{rank} calculates the rank of its matrix argument.
  255. \begin{Examples}
  256. rank mat((a,b,c),(d,e,f)); & 2
  257. \end{Examples}
  258. \begin{Comments}
  259. The argument to \name{rank} can also be a \nameref{list} of lists, interpreted
  260. either as a row matrix or a set of equations. If that form of input is
  261. chosen, the vectors in the result will be represented by lists as well.
  262. This additional input syntax facilitates the use of \name{rank} in
  263. applications different from classical linear algebra.
  264. \end{Comments}
  265. \end{Operator}
  266. \begin{Operator}{TP}
  267. \index{transpose}\index{matrix}
  268. The \name{tp} operator returns the transpose of its \nameref{matrix}
  269. argument.
  270. \begin{Syntax}
  271. \name{tp} \meta{identifier} or \name{tp}\(\meta{identifier}\)
  272. \end{Syntax}
  273. \meta{identifier} must be a matrix, which either has had its dimensions set
  274. in its declaration, or has had values put into it by \name{mat}.
  275. \begin{Examples}
  276. matrix m,n; \\
  277. m := mat((1,2,3),(4,5,6))$ \\
  278. n := tp m; &
  279. \begin{multilineoutput}{6cm}
  280. N(1,1) := 1
  281. N(1,2) := 4
  282. N(2,1) := 2
  283. N(2,2) := 5
  284. N(3,1) := 3
  285. N(3,2) := 6
  286. \end{multilineoutput}
  287. \end{Examples}
  288. \begin{Comments}
  289. In an assignment statement involving \name{tp}, the matrix identifier on the
  290. left-hand side is redimensioned to the correct size for the transpose.
  291. \end{Comments}
  292. \end{Operator}
  293. \begin{Operator}{TRACE}
  294. \index{matrix}
  295. The \name{trace} operator finds the trace of its \nameref{matrix} argument.
  296. \begin{Syntax}
  297. \name{trace}\(\meta{expression}\) or \name{trace} \meta{simple\_expression}
  298. \end{Syntax}
  299. \meta{expression} or \meta{simple\_expression} must evaluate to a square
  300. matrix.
  301. \begin{Examples}
  302. matrix a; \\
  303. a := mat((x1,y1),(x2,y2))\$ \\
  304. trace a; & X1 + Y2
  305. \end{Examples}
  306. \begin{Comments}
  307. The trace is the sum of the entries along the diagonal of a square matrix.
  308. Given a non-matrix expression, or a non-square matrix, \name{trace} returns
  309. an error message.
  310. \end{Comments}
  311. \end{Operator}