1\chapter{LINALG: Linear algebra package}
2\label{LINALG}
3\typeout{{LINALG: Linear algebra package}}
4
5{\footnotesize
6\begin{center}
7Matt Rebbeck \\
8Konrad--Zuse--Zentrum f\"ur Informationstechnik Berlin \\
9Takustra\"se 7 \\
10D--14195 Berlin--Dahlem, Germany \\[0.05in]
11\end{center}
12}
13\ttindex{LINALG}
14
15\section{Introduction}
16
17This package provides a selection of functions that are useful
18in the world of linear algebra.  They can be classified into four
19sections:
20
21\subsection{Basic matrix handling}
22
23\begin{center}
24\begin{tabular}{l l l l}
25add\_columns\ttindex{ADD\_COLUMNS}           &
26add\_rows\ttindex{ADD\_ROWS}                 &
27add\_to\_columns\ttindex{ADD\_TO\_COLUMNS}   &
28add\_to\_rows\ttindex{ADD\_TO\_ROWS}         \\
29augment\_columns\ttindex{AUGMENT\_COLUMNS}   &
30char\_poly\ttindex{CHAR\_POLY}               &
31column\_dim\ttindex{COLUMN\_DIM}             &
32copy\_into\ttindex{COPY\_INTO}               \\
33diagonal\ttindex{DIAGONAL}                   &
34extend\ttindex{EXTEND}                       &
35find\_companion\ttindex{FIND\_COMPANION}     &
36get\_columns\ttindex{GET\_COLUMNS}           \\
37get\_rows\ttindex{GET\_ROWS}                 &
38hermitian\_tp\ttindex{HERMITIAN\_TP}         &
39matrix\_augment\ttindex{MATRIX\_AUGMENT}     &
40matrix\_stack\ttindex{MATRIX\_STACK}         \\
41minor\ttindex{MINOR}                         &
42mult\_columns\ttindex{MULT\_COLUMNS}         &
43mult\_rows\ttindex{MULT\_ROWS}               &
44pivot\ttindex{PIVOT}                         \\
45remove\_columns\ttindex{REMOVE\_COLUMNS}     &
46remove\_rows\ttindex{REMOVE\_ROWS}           &
47row\_dim\ttindex{ROW\_DIM}                   &
48rows\_pivot\ttindex{ROWS\_PIVOT}             \\
49stack\_rows\ttindex{STACK\_ROWS}             &
50sub\_matrix\ttindex{SUB\_MATRIX}             &
51swap\_columns\ttindex{SWAP\_COLUMNS}         &
52swap\_entries\ttindex{SWAP\_ENTRIES}         \\
53swap\_rows\ttindex{SWAP\_ROWS}         & & &
54\end{tabular}
55\end{center}
56
57\subsection{Constructors}
58
59Functions that create matrices.
60
61\begin{center}
62\begin{tabular}{l l l l}
63band\_matrix\ttindex{BAND\_MATRIX}        &
64block\_matrix\ttindex{BLOCK\_MATRIX}       &
65char\_matrix\ttindex{CHAR\_MATRIX}        &
66coeff\_matrix\ttindex{COEFF\_MATRIX}       \\
67companion\ttindex{COMPANION}           &
68hessian\ttindex{HESSIAN}             &
69hilbert\ttindex{HILBERT}             &
70jacobian\ttindex{JACOBIAN}            \\
71jordan\_block\ttindex{JORDAN\_BLOCK}       &
72make\_identity\ttindex{MAKE\_IDENTITY}      &
73random\_matrix\ttindex{RANDOM\_MATRIX}      &
74toeplitz\ttindex{TOEPLITZ}            \\
75vandermonde\ttindex{VANDERMONDE}         &
76Kronecker\_Product\ttindex{KRONECKER\_PRODUCT}  &
77\end{tabular}
78\end{center}
79
80\subsection{High level algorithms}
81
82\begin{center}
83\begin{tabular}{l l l l}
84char\_poly\ttindex{CHAR\_POLY}        &
85cholesky\ttindex{CHOLESKY}          &
86gram\_schmidt\ttindex{GRAM\_SCHMIDT}     &
87lu\_decom\ttindex{LU\_DECOM}         \\
88pseudo\_inverse\ttindex{PSEUDO\_INVERSE}   &
89simplex\ttindex{SIMPLEX}           &
90svd\ttindex{SVD}               &
91triang\_adjoint\ttindex{TRIANG\_ADJOINT} \\
92\end{tabular}
93\end{center}
94
95\vspace*{5mm}
96There is a separate {\small NORMFORM} package (chapter~\ref{NORMFORM})
97for computing the matrix normal forms smithex, smithex\_int,
98frobenius, ratjordan, jordansymbolic and jordan in \REDUCE.
99
100\subsection{Predicates}
101
102\begin{center}
103\begin{tabular}{l l l}
104matrixp\ttindex{MATRIXP}      &
105squarep\ttindex{SQUAREP}      &
106symmetricp\ttindex{SYMMETRICP}
107\end{tabular}
108\end{center}
109
110\section{Explanations}
111
112In the examples the matrix ${\cal A}$ will be
113
114\begin{flushleft}
115\begin{math}
116{\cal A} = \left( \begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9
117\end{array} \right)
118\end{math}
119\end{flushleft}
120
121Throughout ${\cal I}$ is used to indicate the identity matrix and
122${\cal A}^T$ to indicate the transpose of the matrix ${\cal A}$.
123
124Many of the functions have a fairly obvious meaning.  Others need a
125little explanation.
126
127\section{Basic matrix handling}
128
129The functions \f{ADD\_COLUMNS}\ttindex{ADD\_COLUMNS} and \f{ADD\_ROWS}
130provide basic operations between rows and columns.  The form is
131
132\noindent {\tt add\_columns(${\cal A}$,c1,c2,expr);}
133
134and it replaces column c2 of the matix by expr $*$ column(${\cal
135A}$,c1) $+$ column(${\cal A}$,c2).
136
137\f{ADD\_TO\_COLUMNS}\ttindex{ADD\_TO\_COLUMNS} and
138\f{ADD\_TO\_ROWS}\ttindex{ADD\_TO\_ROWS} do a similar task, adding an
139expression to each of a number of columns (or rows) specified by a
140list.
141
142\begin{math}
143\begin{array}{ccc}
144{\tt add\_to\_columns}({\cal A},\{1,2\},10) & = &
145\left( \begin{array}{ccc} 11 & 12 & 3 \\ 14 & 15 & 6 \\ 17 & 18 & 9
146\end{array} \right)
147\end{array}
148\end{math}
149
150The functions \f{MULT\_COLUMNS}\ttindex{MULT\_COLUMNS} and
151\f{MULT\_ROW}\ttindex{MULT\_ROW} are equivalent to multiply columns
152and rows.
153
154
155\f{COLUMN\_DIM}\ttindex{COLUMN\_DIM} and
156\f{ROW\_DIM}\ttindex{ROW\_DIM} find the column dimension and row
157dimension of their argument.
158
159Parts of a matrix can be replaced from another by using
160\f{COPY\_INTO}\ttindex{COPY\_INTO}; the last two arguments are row and
161column counters for to where to copy the matrix.
162
163\begin{flushleft}
164\hspace*{0.175in}
165\begin{math}
166{\cal G} = \left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\
1670 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0
168\end{array} \right)
169\end{math}
170\end{flushleft}
171
172\begin{flushleft}
173\hspace*{0.1in}
174\begin{math}
175\begin{array}{ccc}
176{\tt copy\_into}({\cal A,G},1,2) & = &
177\left( \begin{array}{cccc} 0 & 1 & 2 & 3 \\ 0 & 4 & 5 & 6 \\ 0 & 7 & 8
178& 9 \\ 0 & 0 & 0 & 0
179\end{array} \right)
180\end{array}
181\end{math}
182\end{flushleft}
183
184A diagonal matrix can be created with \f{DIAGONAL}\ttindex{DIAGONAL}.
185The argument is a list of expressions of matrices which form the
186diagonal.
187
188An existing matrix can be extended; the call \f{EXTEND}(A,r,c,exp)\ttindex{EXTEND}
189returns the matrix A extended by r rows and c columns, with the new
190entries all exp.
191
192The function \f{GET\_COLUMNS}\ttindex{GET\_COLUMNS} extracts from a
193matrix a list of the specified columns as matrices.
194\f{GET\_ROWS}\ttindex{GET\_ROWS} does the equivalent for rows.
195
196\begin{flushleft}
197\hspace*{0.1in}
198\begin{math}
199\begin{array}{ccc}
200{\tt get\_columns}({\cal A},\{1,3\}) & = &
201\left\{
202        \left( \begin{array}{c} 1 \\ 4 \\ 7 \end{array} \right),
203        \left( \begin{array}{c} 3 \\ 6 \\ 9 \end{array} \right)
204\right\}
205\end{array}
206\end{math}
207\end{flushleft}
208
209The Hermitian transpose, that is a matrix in which the (i,$\,$j) entry is the conjugate of
210the (j,$\,$i) entry of the input is returned by \f{HERMITIAN\_TP}\ttindex{HERMITIAN\_TP}.
211
212\f{MATRIX\_AUGMENT}(\{mat$_{1}$,mat$_{2}$, \ldots ,mat$_{n}$\})\ttindex{MATRIX\_AUGMENT}
213produces a new matrix from the list joined as new columns.
214\ttindex{MATRIX\_STACK}\f{MATRIX\_STACK} joins a list of matrices by
215stacking them.
216
217\begin{flushleft}
218\hspace*{0.1in}
219\begin{math}
220\begin{array}{ccc}
221{\tt matrix\_stack}(\{{\cal A,A}\}) & = &
222        \left( \begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9
223\\ 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9
224 \end{array} \right)
225\end{array}
226\end{math}
227\end{flushleft}
228
229\f{MINOR}(A,r,c)\ttindex{MINOR} calculates the (r,c) minor of A.
230
231\f{PIVOT}\ttindex{PIVOT} pivots a matrix about its (r,c) entry.
232To do this, multiples of the $r^{th}$ row are added to every other row in
233the matrix.  This means that the $c^{th}$ column will be 0 except for
234the (r,c) entry.
235
236A variant on this operation is provided by
237\f{ROWS\_PIVOT}\ttindex{ROWS\_PIVOT}.  It applies the pivot only to the
238rows specified as the last argument.
239
240A sub matrix can be extracted, giving a list or the rows and columns
241to keep.
242
243\begin{flushleft}
244\hspace*{0.1in}
245\begin{math}
246\begin{array}{ccc}
247{\tt sub\_matrix}({\cal A},\{1,3\},\{2,3\}) & = &
248        \left( \begin{array}{cc} 2 & 3 \\ 8 & 9
249 \end{array} \right)
250\end{array}
251\end{math}
252\end{flushleft}
253
254The basic operation of swapping rows or columns is provided by
255\f{SWAP\_ROWS}\ttindex{SWAP\_ROWS} and
256\f{SWAP\_COLUMNS}\ttindex{SWAP\_COLUMNS}.  Individual entries can be
257swapped with \f{SWAP\_ENTRIES}\ttindex{SWAP\_ENTRIES}.
258\begin{flushleft}
259\hspace*{0.1in}
260\begin{math}
261\begin{array}{ccc}
262{\tt swap\_columns}({\cal A},2,3) & = &
263        \left( \begin{array}{ccc} 1 & 3 & 2 \\ 4 & 6 & 5 \\ 7 & 9 & 8
264 \end{array} \right)
265\end{array}
266\end{math}
267\end{flushleft}
268
269\begin{flushleft}
270\hspace*{0.1in}
271\begin{math}
272\begin{array}{ccc}
273{\tt swap\_entries}({\cal A},\{1,1\},\{3,3\}) & = &
274        \left( \begin{array}{ccc} 9 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 1
275 \end{array} \right)
276\end{array}
277\end{math}
278\end{flushleft}
279
280
281\section{Constructors}
282
283\f{AUGMENT\_COLUMNS}\ttindex{AUGMENT\_COLUMNS} allows just specified
284columns to be selected; \f{STACK\_ROWS}\ttindex{STACK\_ROWS} does
285a similar job for rows.
286
287\begin{math}
288\begin{array}{ccc}
289{\tt stack\_rows}({\cal A},\{1,3\}) & = &
290\left( \begin{array}{ccc} 1 & 2 & 3 \\ 7 & 8 & 9
291\end{array} \right)
292\end{array}
293\end{math}
294
295Rows or columns can be removed with
296\f{REMOVE\_COLUMNS}\ttindex{REMOVE\_COLUMNS} and
297\f{REMOVE\_ROWS}\ttindex{REMOVE\_ROWS}.
298
299\begin{flushleft}
300\hspace*{0.1in}
301\begin{math}
302\begin{array}{ccc}
303{\tt remove\_columns}({\cal A},2) & = &
304        \left( \begin{array}{cc} 1 & 3 \\ 4 & 6 \\ 7 & 9
305 \end{array} \right)
306\end{array}
307\end{math}
308\end{flushleft}
309
310
311{\tt BAND\_MATRIX}\ttindex{BAND\_MATRIX} creates a square matrix of
312dimension its second argument.  The diagonal consists of the middle
313expressions of the first argument, which is an expression list. The
314expressions to the left of this fill the required number of
315sub\_diagonals and the expressions to the right the super\_diagonals.
316
317\begin{math}
318\begin{array}{ccc}
319{\tt band\_matrix}(\{x,y,z\},6) & = &
320\left( \begin{array}{cccccc} y & z & 0 & 0 & 0 & 0 \\ x & y & z & 0 & 0
321& 0 \\ 0 & x & y & z & 0 & 0 \\ 0 & 0 & x & y & z & 0 \\ 0 & 0 & 0 & x &
322 y & z \\ 0 & 0 & 0 & 0 & x & y
323\end{array} \right)
324\end{array}
325\end{math}
326
327Related to the band matrix is a block matrix, which can be created by
328
329\noindent {\tt BLOCK\_MATRIX(r,c,matrix\_list)}.\ttindex{BLOCK\_MATRIX}
330
331The resulting matrix consists of r by c matrices filled from the
332matrix\_list row wise.
333
334\begin{flushleft}
335\hspace*{0.1in}
336\begin{math}
337\begin{array}{ccc}
338{\cal B} = \left( \begin{array}{cc} 1 & 0 \\ 0 & 1
339\end{array} \right), &
340{\cal C} = \left( \begin{array}{c} 5 \\ 5
341\end{array} \right), &
342{\cal D} = \left( \begin{array}{cc} 22 & 33 \\ 44 & 55
343\end{array} \right)
344\end{array}
345\end{math}
346\end{flushleft}
347
348\vspace*{0.175in}
349
350\begin{flushleft}
351\hspace*{0.1in}
352\begin{math}
353\begin{array}{ccc}
354{\tt block\_matrix}(2,3,\{{\cal B,C,D,D,C,B}\}) & = &
355\left( \begin{array}{ccccc} 1 & 0 & 5 & 22 & 33 \\ 0 & 1 & 5 & 44 & 55
356\\
35722 & 33 & 5 & 1 & 0 \\ 44 & 55 & 5 & 0 & 1
358\end{array} \right)
359\end{array}
360\end{math}
361\end{flushleft}
362
363Characteristic polynomials and characteristic matrices are created by
364the functions
365{\tt CHAR\_POLY}\ttindex{CHAR\_POLY} and
366\f{CHAR\_MATRIX}\ttindex{CHAR\_MATRIX}.
367
368A set of linear equations can be turned into the associated
369coefficient matrix and vector of unknowns and the righthandside.
370\f{COEFF\_MATRIX} returns a list \{${\cal C,X,B}$\} such that ${\cal
371CX} = {\cal B}$.
372
373\begin{math}
374\hspace*{0.175in}
375{\tt coeff\_matrix}(\{x+y+4*z=10,y+x-z=20,x+y+4\}) =
376\end{math}
377
378\vspace*{0.1in}
379
380\begin{flushleft}
381\hspace*{0.175in}
382\begin{math}
383\left\{ \left( \begin{array}{ccc} 4 & 1 & 1 \\ -1 & 1 & 1 \\ 0 & 1 & 1
384\end{array} \right), \left( \begin{array}{c} z \\ y \\ x \end{array}
385\right), \left( \begin{array}{c} 10 \\ 20 \\ -4
386\end{array} \right) \right\}
387\end{math}
388\end{flushleft}
389
390\f{COMPANION}(poly,x)  creates the companion matrix ${\cal C}$ of a
391polynomial.  That is the square matrix of dimension n, where n is the
392degree of polynomial with respect to x, and the entries of ${\cal C}$ are:
393${\cal C}$(i,n) = -coeffn(poly,x,i-1) for i = 1 \ldots n, ${\cal
394C}$(i,i-1) = 1 for i = 2 \ldots n and the rest are 0.
395
396\begin{flushleft}
397\hspace*{0.1in}
398\begin{math}
399\begin{array}{ccc}
400{\tt companion}(x^4+17*x^3-9*x^2+11,x) & = &
401\left( \begin{array}{cccc} 0 & 0 & 0 & -11 \\ 1 & 0 & 0 & 0 \\
4020 & 1 & 0 & 9 \\ 0 & 0 & 1 & -17
403\end{array} \right)
404\end{array}
405\end{math}
406\end{flushleft}
407
408The polynomial associated with a companion matrix can be recovered by
409calling \f{FIND\_COMPANION}\ttindex{FIND\_COMPANION}.
410
411\f{HESSIAN}(expr, var\_list)\ttindex{HESSIAN} calculates the Hessian
412matrix of the expressions with respect to the variables in the list,
413or the single variable.  That is the matrix with the (i,$\,$j) element
414the $j^{th}$ derivative of the expressions with respect to the
415$i^{th}$ variable.
416
417\begin{flushleft}
418\hspace*{0.1in}
419\begin{math}
420\begin{array}{ccc}
421{\tt hessian}(x*y*z+x^2,\{w,x,y,z\}) & = &
422\left( \begin{array}{cccc} 0 & 0 & 0 & 0 \\ 0 & 2 & z & y \\ 0 & z & 0
423& x \\ 0 & y & x & 0
424\end{array} \right)
425\end{array}
426\end{math}
427\end{flushleft}
428
429Hilbert's matrix, that is where the (i,$\,$j) element is $1/(i+j-x)$
430is constructed by \f{HILBERT}(n,x)\ttindex{HILBERT}.
431
432The Jacobian of an expression list with respect to a variable list is
433calculated by
434\f{JACOBIAN}(expr\_list,variable\_list)\ttindex{JACOBIAN}.  This is a
435matrix whose (i,$\,$j) entry is df(expr\_list(i),variable\_list(j)).
436
437
438The square Jordan block matrix of dimension $n$ is calculated by the
439function \f{JORDAN\_BLOCK}(exp,n).\ttindex{JORDAN\_BLOCK}  The entries
440of the Jordan\_block matrix are ${\cal J}$(i,i) = expr for i=1 \ldots
441n, ${\cal J}$(i,i+1) = 1 for i=1 \ldots n-1, and all other entries are 0.
442
443\begin{flushleft}
444\hspace*{0.1in}
445\begin{math}
446\begin{array}{ccc}
447{\tt jordan\_block(x,5)} & = &
448\left( \begin{array}{ccccc} x & 1 & 0 & 0 & 0 \\ 0 & x & 1 & 0 & 0 \\ 0
449& 0 & x & 1 & 0 \\ 0 & 0 & 0 & x & 1 \\ 0 & 0 & 0 & 0 & x
450\end{array} \right)
451\end{array}
452\end{math}
453\end{flushleft}
454
455\f{MAKE\_IDENTITY}(n)\ttindex{MAKE\_IDENTITY} generates the $n \times
456n$ identity matrix.
457
458\f{RANDOM\_MATRIX}(r,c,limit)\ttindex{RANDOM\_MATRIX} generates and $r
459\times c$ matrix with random values limited by {\tt limit}.  The type
460of entries is controlled by a number of switches.
461
462\begin{description}
463\item[{\tt IMAGINARY}]\ttindex{IMAGINARY}
464If on then matrix entries are $x+i*y$ where $-limit < x,y < limit$.
465\item[{\tt NOT\_NEGATIVE}]\ttindex{NOT\_NEGATIVE}
466If on then $0 < entry < limit$. In the imaginary case we have $0 < x,y
467< limit$.
468\item[{\tt ONLY\_INTEGER}]\ttindex{ONLY\_INTEGER}
469If on then each entry is an integer.  In the imaginary case $x$ and $y$ are
470integers.  If off the values are rounded.
471\item[{\tt SYMMETRIC}]\ttindex{SYMMETRIC}
472If on then the matrix is symmetric.
473\item[{\tt UPPER\_MATRIX}]\ttindex{UPPER\_MATRIX}
474If on then the matrix is upper triangular.
475\item[{\tt LOWER\_MATRIX}]\ttindex{LOWER\_MATRIX}
476If on then the matrix is lower triangular.
477\end{description}
478
479\begin{flushleft}
480\hspace*{0.1in}
481\begin{math}
482\begin{array}{ccc}
483{\tt random\_matrix}(3,3,10) & = &
484        \left( \begin{array}{ccc} -4.729721 & 6.987047 & 7.521383 \\
485- 5.224177 & 5.797709 & - 4.321952 \\
486- 9.418455 & - 9.94318 & - 0.730980
487 \end{array} \right)
488\end{array}
489\end{math}
490\end{flushleft}
491
492\vspace*{0.2in}
493\hspace*{0.165in}
494{\tt on only\_integer, not\_negative, upper\_matrix, imaginary;}
495\begin{flushleft}
496%\hspace*{0.12in}
497\begin{math}
498\begin{array}{ccc}
499{\tt random\_matrix}(4,4,10) & = &
500\left( \begin{array}{cccc} 2*i+5 & 3*i+7 & 7*i+3 & 6 \\ 0 & 2*i+5 &
5015*i+1 & 2*i+1 \\ 0 & 0 & 8 & i \\ 0 & 0 & 0& 5*i+9
502\end{array} \right)
503\end{array}
504\end{math}
505\end{flushleft}
506
507{\tt TOEPLITZ}\ttindex{TOEPLITZ} creates the Toeplitz matrix from the
508given expression list.  This is a square symmetric matrix in which the
509first expression is placed on the diagonal  and the $i^{th}$
510expression is placed on the $(i-1)^{th}$ sub- and super-diagonals.
511It has dimension equal to the number of expressions.
512
513\begin{flushleft}
514\begin{math}
515\begin{array}{ccc}
516{\tt toeplitz}(\{w,x,y,z\}) & = &
517        \left( \begin{array}{cccc} w & x & y & z \\ x & w & x & y \\
518      y & x & w & x \\ z & y & x & w
519 \end{array} \right)
520\end{array}
521\end{math}
522\end{flushleft}
523
524\f{VANDERMONDE}\ttindex{VANDERMONDE} creates the Vandermonde matrix
525from the expression list; the square matrix in which the (i,$\,$j)
526entry is expr\_list(i) $^{(j-1)}$.
527
528\begin{flushleft}
529\hspace*{0.1in}
530\begin{math}
531\begin{array}{ccc}
532{\tt vandermonde}(\{x,2*y,3*z\}) & = &
533        \left( \begin{array}{ccc} 1 & x & x^2 \\ 1 & 2*y & 4*y^2 \\ 1
534& 3*z & 9*z^2
535 \end{array} \right)
536\end{array}
537\end{math}
538\end{flushleft}
539
540The direct product\index{direct product} (or tensor
541product\index{tensor product}) is created by the
542\f{KRONECKER\_PRODUCT}\ttindex{KRONECKER\_PRODUCT} function.
543
544\begin{verbatim}
545a1 := mat((1,2),(3,4),(5,6))$
546a2 := mat((1,1,1),(2,z,2),(3,3,3))$
547kronecker_product(a1,a2);
548\end{verbatim}
549\begin{flushleft}
550\hspace*{0.1in}
551\begin{math}
552\begin{array}{ccc}
553\left( \begin{array}{cccccc} 1 & 1 & 1 & 2 & 2 & 2 \\
5542 &  z & 2 & 4  &2*z &4 \\
5553 &  3 & 3 & 6  & 6  &6 \\
5563 &  3 & 3 & 4  & 4  &4 \\
5576 & 3*z& 6 & 8  &4*z &8 \\
5589 &  9 & 9 & 12 &12  &12\\
5595 &  5 & 5 & 6  & 6  &6 \\
56010 &5*z& 10& 12 &6*z &12 \\
56115 &15 & 15& 18 &18  &18 \end{array} \right)
562\end{array}
563\end{math}
564\end{flushleft}
565
566\section{Higher Algorithms}
567
568The Cholesky decomposition  of a matrix can be
569calculated with the function \f{CHOLESKY}.  It returns \{${\cal
570L,U}$\} where ${\cal L}$ is a lower matrix, ${\cal U}$ is an upper
571matrix, and ${\cal A} = {\cal LU}$, and ${\cal U} = {\cal L}^T$.
572
573Gram--Schmidt orthonormalisation can be calculated by
574\f{GRAM\_SCHMIDT}\ttindex{GRAM\_SCHMIDT}.  It accepts a list of
575linearly independent vectors, written as lists, and returns a list of
576orthogonal normalised vectors.
577
578\begin{verbatim}
579gram_schmidt({{1,0,0},{1,1,0},{1,1,1}});
580
581{{1,0,0},{0,1,0},{0,0,1}}
582
583gram_schmidt({{1,2},{3,4}});
584
585      1         2        2*sqrt(5)    - sqrt(5)
586{{---------,---------},{-----------,------------}}
587   sqrt(5)   sqrt(5)         5           5
588\end{verbatim}
589
590The LU decomposition of a real or imaginary matrix with numeric
591entries is performed by {\tt LU\_DECOM(${\cal A}$)}.\ttindex{LU\_DECOM}
592It returns \{${\cal L,U}$\} where ${\cal L}$ is a lower diagonal
593matrix, ${\cal U}$ an upper diagonal matrix and ${\cal A} = {\cal LU}$.
594
595Note: the algorithm used can swap the rows of ${\cal A}$ during
596the calculation.  This means that ${\cal LU}$ does not equal ${\cal
597A}$ but a row equivalent of it.  Due to this, {\tt lu\_decom} returns
598\{${\cal L,U}$,vec\}.  The call {\tt CONVERT(${\cal
599A}$,vec)}\ttindex{CONVERT} will return the matrix that has been
600decomposed, {\em i.e.\ } ${\cal LU} = $  {\tt convert(${\cal A}$,vec)}.
601
602\begin{flushleft}
603\hspace*{0.175in}
604\begin{math}
605{\cal K} = \left( \begin{array}{ccc} 1 & 3 & 5 \\ -4 & 3 & 7 \\ 8 & 6 &
6064
607\end{array} \right)
608\end{math}
609\end{flushleft}
610
611\begin{flushleft}
612%\hspace*{0.1in}
613\begin{math}
614\begin{array}{cccc}
615$%   {\tt lu} :=
616 {\tt lu\_decom}$({\cal K}) & = &
617\left\{
618        \left( \begin{array}{ccc} 8 & 0 & 0 \\ -4 & 6 & 0 \\ 1 & 2.25 &
6191.125 1 \end{array} \right),
620        \left( \begin{array}{ccc} 1 & 0.75 & 0.5 \\ 0 & 1 & 1.5 \\ 0 &
6210 & 1 \end{array} \right),
622        [\; 3 \; 2 \; 3 \; ]
623\right\}
624\end{array}
625\end{math}
626\end{flushleft}
627
628{\tt PSEUDO\_INVERSE}\ttindex{PSEUDO\_INVERSE}, also known as the
629Moore--Penrose inverse\index{Moore--Penrose inverse}, computes
630the pseudo inverse of ${\cal A}$.
631Given the singular value decomposition of ${\cal A}$, {\em i.e.\ }
632${\cal A} =  {\cal U} \sum {\cal V}^T$, then the pseudo inverse ${\cal
633A}^{-1}$ is defined by ${\cal A}^{-1} = {\cal V}^T \sum^{-1} {\cal U}$.
634
635Thus ${\cal A}$ $ * $ {\tt pseudo\_inverse}$({\cal A}) = {\cal I}$.
636
637\begin{flushleft}
638\hspace*{0.1in}
639\begin{math}
640\begin{array}{ccc}
641{\tt pseudo\_inverse}({\cal A}) & = &
642        \left( \begin{array}{cc} -0.2 & 0.1 \\ -0.05 & 0.05 \\ 0.1 & 0
643\\ 0.25 & -0.05
644 \end{array} \right)
645\end{array}
646\end{math}
647\end{flushleft}
648
649\label{simplex}
650The simplex linear programming algorithm\index{Simplex Algorithm} for
651maximising or minimising a function subject to lineal inequalities can
652be used with the function \f{SIMPLEX}\ttindex{SIMPLEX}.  It requires
653three arguments, the first indicates where the action is to maximising
654or minimising, the second is the test expressions, and the last is a
655list of linear inequalities.
656It returns \{optimal value,\{ values of variables at this optimal\}\}.
657The algorithm implies that all the variables are non-negative.
658
659\begin{addtolength}{\leftskip}{0.22in}
660%\begin{math}
661{\tt simplex($max,x+y,\{x>=10,y>=20,x+y<=25\}$);}
662%\end{math}
663
664{\tt ***** Error in simplex: Problem has no feasible solution.}
665
666\vspace*{0.2in}
667
668\parbox[t]{0.96\linewidth}{\tt simplex($max,10x+5y+5.5z,\{5x+3z<=200,
669x+0.1y+0.5z<=12$,\\
670\hspace*{0.55in} $0.1x+0.2y+0.3z<=9, 30x+10y+50z<=1500\}$);}
671
672\vspace*{0.1in}
673{\tt $\{525.0,\{x=40.0,y=25.0,z=0\}$\}}
674
675\end{addtolength}
676
677{\tt SVD}\ttindex{SVD} computes the singular value decomposition of
678${\cal A}$ with numeric entries.   It returns \{${\cal U},\sum,{\cal V}$\} where ${\cal A} = {\cal U}
679\sum {\cal V}^T$ and $\sum = diag(\sigma_{1}, \ldots ,\sigma_{n}). \;
680\sigma_{i}$ for $i= (1 \ldots n)$ are the singular values of ${\cal A}$.
681The singular values of ${\cal A}$ are the non-negative square roots of
682the eigenvalues of ${\cal A}^T {\cal A}$.
683
684${\cal U}$ and ${\cal V}$ are such that ${\cal UU}^T = {\cal VV}^T =
685{\cal V}^T {\cal V} = {\cal I}_n$.
686\begin{flushleft}
687\hspace*{0.175in}
688\begin{math}
689{\cal Q} = \left( \begin{array}{cc} 1 & 3 \\ -4 & 3
690\end{array} \right)
691\end{math}
692\end{flushleft}
693
694\begin{eqnarray}
695\hspace*{0.1in}
696{\tt svd({\cal Q})} & = &
697\left\{
698        \left( \begin{array}{cc} 0.289784 & 0.957092 \\ -0.957092 &
6990.289784 \end{array} \right), \left( \begin{array}{cc} 5.149162 & 0 \\
7000 & 2.913094 \end{array} \right), \right. \nonumber \\ & & \left. \: \;
701\, \left( \begin{array}{cc} -0.687215 & 0.726453 \\ -0.726453 &
702-0.687215 \end{array} \right)
703\right\} \nonumber
704\end{eqnarray}
705
706{\tt TRIANG\_ADJOINT}\ttindex{TRIANG\_ADJOINT} computes the trianglarizing adjoint of
707the given matrix. The triangularizing adjoint is a lower triangular matrix. The
708multiplication of the triangularizing adjoint with the given matrix results in an
709upper triangular matrix. The i-th entry in the diagonal of this matrix is the
710determinant of the principal i-th minor of the given matrix.
711
712\begin{flushleft}
713\hspace*{0.1in}
714\begin{math}
715\begin{array}{ccc}
716{\tt triang\_adjoint}({\cal A}) & = &
717        \left( \begin{array}{ccc} 1 & 0 & 0 \\ -4 & 1 & 0 \\ -3 & 6 & -3
718 \end{array} \right)
719\end{array}
720\end{math}
721\end{flushleft}
722
723The multiplication of this matrix with ${\cal A}$ results in an upper triangular matrix.
724
725\begin{flushleft}
726\hspace*{0.1in}
727\begin{math}
728\begin{array}{cccc}
729 \left( \begin{array}{ccc} 1 & 0 & 0 \\ -4 & 1 & 0 \\ -3 & 6 & -3
730 \end{array} \right) &
731 \left( \begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9
732 \end{array} \right)
733 & = &
734        \left( \begin{array}{ccc} 1 & 2 & 3 \\ 0 & -3 & -6 \\ 0 & 0 & 0
735 \end{array} \right)
736\end{array}
737\end{math}
738\end{flushleft}
739
740\section{Fast Linear Algebra}
741
742By turning the {\tt FAST\_LA}\ttindex{FAST\_LA} switch on, the speed
743of the following functions will be increased:
744
745\begin{tabular}{l l l l}
746   add\_columns    & add\_rows      & augment\_columns & column\_dim  \\
747   copy\_into      & make\_identity & matrix\_augment  & matrix\_stack\\
748   minor           & mult\_column   &  mult\_row       & pivot        \\
749   remove\_columns & remove\_rows   & rows\_pivot      & squarep      \\
750   stack\_rows     & sub\_matrix    & swap\_columns    & swap\_entries\\
751   swap\_rows      & symmetricp
752\end{tabular}
753
754The increase in speed will be insignificant unless you are making a
755thousands of calls.  When using this switch,
756error checking is minimised, and thus illegal input may give strange
757error messages.
758
759