1
2[//000000001]: # (math::linearalgebra \- Tcl Math Library)
3[//000000002]: # (Generated from file 'linalg\.man' by tcllib/doctools with format 'markdown')
4[//000000003]: # (Copyright &copy; 2004\-2008 Arjen Markus <arjenmarkus@users\.sourceforge\.net>)
5[//000000004]: # (Copyright &copy; 2004 Ed Hume <http://www\.hume\.com/contact\.us\.htm>)
6[//000000005]: # (Copyright &copy; 2008 Michael Buadin <relaxkmike@users\.sourceforge\.net>)
7[//000000006]: # (math::linearalgebra\(n\) 1\.1\.5 tcllib "Tcl Math Library")
8
9<hr> [ <a href="../../../../toc.md">Main Table Of Contents</a> &#124; <a
10href="../../../toc.md">Table Of Contents</a> &#124; <a
11href="../../../../index.md">Keyword Index</a> &#124; <a
12href="../../../../toc0.md">Categories</a> &#124; <a
13href="../../../../toc1.md">Modules</a> &#124; <a
14href="../../../../toc2.md">Applications</a> ] <hr>
15
16# NAME
17
18math::linearalgebra \- Linear Algebra
19
20# <a name='toc'></a>Table Of Contents
21
22  - [Table Of Contents](#toc)
23
24  - [Synopsis](#synopsis)
25
26  - [Description](#section1)
27
28  - [PROCEDURES](#section2)
29
30  - [STORAGE](#section3)
31
32  - [REMARKS ON THE IMPLEMENTATION](#section4)
33
34  - [TODO](#section5)
35
36  - [NAMING CONFLICT](#section6)
37
38  - [Bugs, Ideas, Feedback](#section7)
39
40  - [Keywords](#keywords)
41
42  - [Category](#category)
43
44  - [Copyright](#copyright)
45
46# <a name='synopsis'></a>SYNOPSIS
47
48package require Tcl ?8\.4?
49package require math::linearalgebra ?1\.1\.5?
50
51[__::math::linearalgebra::mkVector__ *ndim* *value*](#1)
52[__::math::linearalgebra::mkUnitVector__ *ndim* *ndir*](#2)
53[__::math::linearalgebra::mkMatrix__ *nrows* *ncols* *value*](#3)
54[__::math::linearalgebra::getrow__ *matrix* *row* ?imin? ?imax?](#4)
55[__::math::linearalgebra::setrow__ *matrix* *row* *newvalues* ?imin? ?imax?](#5)
56[__::math::linearalgebra::getcol__ *matrix* *col* ?imin? ?imax?](#6)
57[__::math::linearalgebra::setcol__ *matrix* *col* *newvalues* ?imin? ?imax?](#7)
58[__::math::linearalgebra::getelem__ *matrix* *row* *col*](#8)
59[__::math::linearalgebra::setelem__ *matrix* *row* ?col? *newvalue*](#9)
60[__::math::linearalgebra::swaprows__ *matrix* *irow1* *irow2* ?imin? ?imax?](#10)
61[__::math::linearalgebra::swapcols__ *matrix* *icol1* *icol2* ?imin? ?imax?](#11)
62[__::math::linearalgebra::show__ *obj* ?format? ?rowsep? ?colsep?](#12)
63[__::math::linearalgebra::dim__ *obj*](#13)
64[__::math::linearalgebra::shape__ *obj*](#14)
65[__::math::linearalgebra::conforming__ *type* *obj1* *obj2*](#15)
66[__::math::linearalgebra::symmetric__ *matrix* ?eps?](#16)
67[__::math::linearalgebra::norm__ *vector* *type*](#17)
68[__::math::linearalgebra::norm\_one__ *vector*](#18)
69[__::math::linearalgebra::norm\_two__ *vector*](#19)
70[__::math::linearalgebra::norm\_max__ *vector* ?index?](#20)
71[__::math::linearalgebra::normMatrix__ *matrix* *type*](#21)
72[__::math::linearalgebra::dotproduct__ *vect1* *vect2*](#22)
73[__::math::linearalgebra::unitLengthVector__ *vector*](#23)
74[__::math::linearalgebra::normalizeStat__ *mv*](#24)
75[__::math::linearalgebra::axpy__ *scale* *mv1* *mv2*](#25)
76[__::math::linearalgebra::add__ *mv1* *mv2*](#26)
77[__::math::linearalgebra::sub__ *mv1* *mv2*](#27)
78[__::math::linearalgebra::scale__ *scale* *mv*](#28)
79[__::math::linearalgebra::rotate__ *c* *s* *vect1* *vect2*](#29)
80[__::math::linearalgebra::transpose__ *matrix*](#30)
81[__::math::linearalgebra::matmul__ *mv1* *mv2*](#31)
82[__::math::linearalgebra::angle__ *vect1* *vect2*](#32)
83[__::math::linearalgebra::crossproduct__ *vect1* *vect2*](#33)
84[__::math::linearalgebra::matmul__ *mv1* *mv2*](#34)
85[__::math::linearalgebra::mkIdentity__ *size*](#35)
86[__::math::linearalgebra::mkDiagonal__ *diag*](#36)
87[__::math::linearalgebra::mkRandom__ *size*](#37)
88[__::math::linearalgebra::mkTriangular__ *size* ?uplo? ?value?](#38)
89[__::math::linearalgebra::mkHilbert__ *size*](#39)
90[__::math::linearalgebra::mkDingdong__ *size*](#40)
91[__::math::linearalgebra::mkOnes__ *size*](#41)
92[__::math::linearalgebra::mkMoler__ *size*](#42)
93[__::math::linearalgebra::mkFrank__ *size*](#43)
94[__::math::linearalgebra::mkBorder__ *size*](#44)
95[__::math::linearalgebra::mkWilkinsonW\+__ *size*](#45)
96[__::math::linearalgebra::mkWilkinsonW\-__ *size*](#46)
97[__::math::linearalgebra::solveGauss__ *matrix* *bvect*](#47)
98[__::math::linearalgebra::solvePGauss__ *matrix* *bvect*](#48)
99[__::math::linearalgebra::solveTriangular__ *matrix* *bvect* ?uplo?](#49)
100[__::math::linearalgebra::solveGaussBand__ *matrix* *bvect*](#50)
101[__::math::linearalgebra::solveTriangularBand__ *matrix* *bvect*](#51)
102[__::math::linearalgebra::determineSVD__ *A* *eps*](#52)
103[__::math::linearalgebra::eigenvectorsSVD__ *A* *eps*](#53)
104[__::math::linearalgebra::leastSquaresSVD__ *A* *y* *qmin* *eps*](#54)
105[__::math::linearalgebra::choleski__ *matrix*](#55)
106[__::math::linearalgebra::orthonormalizeColumns__ *matrix*](#56)
107[__::math::linearalgebra::orthonormalizeRows__ *matrix*](#57)
108[__::math::linearalgebra::dger__ *matrix* *alpha* *x* *y* ?scope?](#58)
109[__::math::linearalgebra::dgetrf__ *matrix*](#59)
110[__::math::linearalgebra::det__ *matrix*](#60)
111[__::math::linearalgebra::largesteigen__ *matrix* *tolerance* *maxiter*](#61)
112[__::math::linearalgebra::to\_LA__ *mv*](#62)
113[__::math::linearalgebra::from\_LA__ *mv*](#63)
114
115# <a name='description'></a>DESCRIPTION
116
117This package offers both low\-level procedures and high\-level algorithms to deal
118with linear algebra problems:
119
120  - robust solution of linear equations or least squares problems
121
122  - determining eigenvectors and eigenvalues of symmetric matrices
123
124  - various decompositions of general matrices or matrices of a specific form
125
126  - \(limited\) support for matrices in band storage, a common type of sparse
127    matrices
128
129It arose as a re\-implementation of Hume's LA package and the desire to offer
130low\-level procedures as found in the well\-known BLAS library\. Matrices are
131implemented as lists of lists rather linear lists with reserved elements, as in
132the original LA package, as it was found that such an implementation is actually
133faster\.
134
135It is advisable, however, to use the procedures that are offered, such as
136*setrow* and *getrow*, rather than rely on this representation explicitly:
137that way it is to switch to a possibly even faster compiled implementation that
138supports the same API\.
139
140*Note:* When using this package in combination with Tk, there may be a naming
141conflict, as both this package and Tk define a command *scale*\. See the
142[NAMING CONFLICT](#section6) section below\.
143
144# <a name='section2'></a>PROCEDURES
145
146The package defines the following public procedures \(several exist as
147specialised procedures, see below\):
148
149*Constructing matrices and vectors*
150
151  - <a name='1'></a>__::math::linearalgebra::mkVector__ *ndim* *value*
152
153    Create a vector with ndim elements, each with the value *value*\.
154
155      * integer *ndim*
156
157        Dimension of the vector \(number of components\)
158
159      * double *value*
160
161        Uniform value to be used \(default: 0\.0\)
162
163  - <a name='2'></a>__::math::linearalgebra::mkUnitVector__ *ndim* *ndir*
164
165    Create a unit vector in *ndim*\-dimensional space, along the *ndir*\-th
166    direction\.
167
168      * integer *ndim*
169
170        Dimension of the vector \(number of components\)
171
172      * integer *ndir*
173
174        Direction \(0, \.\.\., ndim\-1\)
175
176  - <a name='3'></a>__::math::linearalgebra::mkMatrix__ *nrows* *ncols* *value*
177
178    Create a matrix with *nrows* rows and *ncols* columns\. All elements have
179    the value *value*\.
180
181      * integer *nrows*
182
183        Number of rows
184
185      * integer *ncols*
186
187        Number of columns
188
189      * double *value*
190
191        Uniform value to be used \(default: 0\.0\)
192
193  - <a name='4'></a>__::math::linearalgebra::getrow__ *matrix* *row* ?imin? ?imax?
194
195    Returns a single row of a matrix as a list
196
197      * list *matrix*
198
199        Matrix in question
200
201      * integer *row*
202
203        Index of the row to return
204
205      * integer *imin*
206
207        Minimum index of the column \(default: 0\)
208
209      * integer *imax*
210
211        Maximum index of the column \(default: ncols\-1\)
212
213  - <a name='5'></a>__::math::linearalgebra::setrow__ *matrix* *row* *newvalues* ?imin? ?imax?
214
215    Set a single row of a matrix to new values \(this list must have the same
216    number of elements as the number of *columns* in the matrix\)
217
218      * list *matrix*
219
220        *name* of the matrix in question
221
222      * integer *row*
223
224        Index of the row to update
225
226      * list *newvalues*
227
228        List of new values for the row
229
230      * integer *imin*
231
232        Minimum index of the column \(default: 0\)
233
234      * integer *imax*
235
236        Maximum index of the column \(default: ncols\-1\)
237
238  - <a name='6'></a>__::math::linearalgebra::getcol__ *matrix* *col* ?imin? ?imax?
239
240    Returns a single column of a matrix as a list
241
242      * list *matrix*
243
244        Matrix in question
245
246      * integer *col*
247
248        Index of the column to return
249
250      * integer *imin*
251
252        Minimum index of the row \(default: 0\)
253
254      * integer *imax*
255
256        Maximum index of the row \(default: nrows\-1\)
257
258  - <a name='7'></a>__::math::linearalgebra::setcol__ *matrix* *col* *newvalues* ?imin? ?imax?
259
260    Set a single column of a matrix to new values \(this list must have the same
261    number of elements as the number of *rows* in the matrix\)
262
263      * list *matrix*
264
265        *name* of the matrix in question
266
267      * integer *col*
268
269        Index of the column to update
270
271      * list *newvalues*
272
273        List of new values for the column
274
275      * integer *imin*
276
277        Minimum index of the row \(default: 0\)
278
279      * integer *imax*
280
281        Maximum index of the row \(default: nrows\-1\)
282
283  - <a name='8'></a>__::math::linearalgebra::getelem__ *matrix* *row* *col*
284
285    Returns a single element of a matrix/vector
286
287      * list *matrix*
288
289        Matrix or vector in question
290
291      * integer *row*
292
293        Row of the element
294
295      * integer *col*
296
297        Column of the element \(not present for vectors\)
298
299  - <a name='9'></a>__::math::linearalgebra::setelem__ *matrix* *row* ?col? *newvalue*
300
301    Set a single element of a matrix \(or vector\) to a new value
302
303      * list *matrix*
304
305        *name* of the matrix in question
306
307      * integer *row*
308
309        Row of the element
310
311      * integer *col*
312
313        Column of the element \(not present for vectors\)
314
315  - <a name='10'></a>__::math::linearalgebra::swaprows__ *matrix* *irow1* *irow2* ?imin? ?imax?
316
317    Swap two rows in a matrix completely or only a selected part
318
319      * list *matrix*
320
321        *name* of the matrix in question
322
323      * integer *irow1*
324
325        Index of first row
326
327      * integer *irow2*
328
329        Index of second row
330
331      * integer *imin*
332
333        Minimum column index \(default: 0\)
334
335      * integer *imin*
336
337        Maximum column index \(default: ncols\-1\)
338
339  - <a name='11'></a>__::math::linearalgebra::swapcols__ *matrix* *icol1* *icol2* ?imin? ?imax?
340
341    Swap two columns in a matrix completely or only a selected part
342
343      * list *matrix*
344
345        *name* of the matrix in question
346
347      * integer *irow1*
348
349        Index of first column
350
351      * integer *irow2*
352
353        Index of second column
354
355      * integer *imin*
356
357        Minimum row index \(default: 0\)
358
359      * integer *imin*
360
361        Maximum row index \(default: nrows\-1\)
362
363*Querying matrices and vectors*
364
365  - <a name='12'></a>__::math::linearalgebra::show__ *obj* ?format? ?rowsep? ?colsep?
366
367    Return a string representing the vector or matrix, for easy printing\. \(There
368    is currently no way to print fixed sets of columns\)
369
370      * list *obj*
371
372        Matrix or vector in question
373
374      * string *format*
375
376        Format for printing the numbers \(default: %6\.4f\)
377
378      * string *rowsep*
379
380        String to use for separating rows \(default: newline\)
381
382      * string *colsep*
383
384        String to use for separating columns \(default: space\)
385
386  - <a name='13'></a>__::math::linearalgebra::dim__ *obj*
387
388    Returns the number of dimensions for the object \(either 0 for a scalar, 1
389    for a vector and 2 for a matrix\)
390
391      * any *obj*
392
393        Scalar, vector, or matrix
394
395  - <a name='14'></a>__::math::linearalgebra::shape__ *obj*
396
397    Returns the number of elements in each dimension for the object \(either an
398    empty list for a scalar, a single number for a vector and a list of the
399    number of rows and columns for a matrix\)
400
401      * any *obj*
402
403        Scalar, vector, or matrix
404
405  - <a name='15'></a>__::math::linearalgebra::conforming__ *type* *obj1* *obj2*
406
407    Checks if two objects \(vector or matrix\) have conforming shapes, that is if
408    they can be applied in an operation like addition or matrix multiplication\.
409
410      * string *type*
411
412        Type of check:
413
414          + "shape" \- the two objects have the same shape \(for all element\-wise
415            operations\)
416
417          + "rows" \- the two objects have the same number of rows \(for use as A
418            and b in a system of linear equations *Ax = b*
419
420          + "matmul" \- the first object has the same number of columns as the
421            number of rows of the second object\. Useful for matrix\-matrix or
422            matrix\-vector multiplication\.
423
424      * list *obj1*
425
426        First vector or matrix \(left operand\)
427
428      * list *obj2*
429
430        Second vector or matrix \(right operand\)
431
432  - <a name='16'></a>__::math::linearalgebra::symmetric__ *matrix* ?eps?
433
434    Checks if the given \(square\) matrix is symmetric\. The argument eps is the
435    tolerance\.
436
437      * list *matrix*
438
439        Matrix to be inspected
440
441      * float *eps*
442
443        Tolerance for determining approximate equality \(defaults to 1\.0e\-8\)
444
445*Basic operations*
446
447  - <a name='17'></a>__::math::linearalgebra::norm__ *vector* *type*
448
449    Returns the norm of the given vector\. The type argument can be: 1, 2, inf or
450    max, respectively the sum of absolute values, the ordinary Euclidean norm or
451    the max norm\.
452
453      * list *vector*
454
455        Vector, list of coefficients
456
457      * string *type*
458
459        Type of norm \(default: 2, the Euclidean norm\)
460
461  - <a name='18'></a>__::math::linearalgebra::norm\_one__ *vector*
462
463    Returns the L1 norm of the given vector, the sum of absolute values
464
465      * list *vector*
466
467        Vector, list of coefficients
468
469  - <a name='19'></a>__::math::linearalgebra::norm\_two__ *vector*
470
471    Returns the L2 norm of the given vector, the ordinary Euclidean norm
472
473      * list *vector*
474
475        Vector, list of coefficients
476
477  - <a name='20'></a>__::math::linearalgebra::norm\_max__ *vector* ?index?
478
479    Returns the Linf norm of the given vector, the maximum absolute coefficient
480
481      * list *vector*
482
483        Vector, list of coefficients
484
485      * integer *index*
486
487        \(optional\) if non zero, returns a list made of the maximum value and the
488        index where that maximum was found\. if zero, returns the maximum value\.
489
490  - <a name='21'></a>__::math::linearalgebra::normMatrix__ *matrix* *type*
491
492    Returns the norm of the given matrix\. The type argument can be: 1, 2, inf or
493    max, respectively the sum of absolute values, the ordinary Euclidean norm or
494    the max norm\.
495
496      * list *matrix*
497
498        Matrix, list of row vectors
499
500      * string *type*
501
502        Type of norm \(default: 2, the Euclidean norm\)
503
504  - <a name='22'></a>__::math::linearalgebra::dotproduct__ *vect1* *vect2*
505
506    Determine the inproduct or dot product of two vectors\. These must have the
507    same shape \(number of dimensions\)
508
509      * list *vect1*
510
511        First vector, list of coefficients
512
513      * list *vect2*
514
515        Second vector, list of coefficients
516
517  - <a name='23'></a>__::math::linearalgebra::unitLengthVector__ *vector*
518
519    Return a vector in the same direction with length 1\.
520
521      * list *vector*
522
523        Vector to be normalized
524
525  - <a name='24'></a>__::math::linearalgebra::normalizeStat__ *mv*
526
527    Normalize the matrix or vector in a statistical sense: the mean of the
528    elements of the columns of the result is zero and the standard deviation is
529    1\.
530
531      * list *mv*
532
533        Vector or matrix to be normalized in the above sense
534
535  - <a name='25'></a>__::math::linearalgebra::axpy__ *scale* *mv1* *mv2*
536
537    Return a vector or matrix that results from a "daxpy" operation, that is:
538    compute a\*x\+y \(a a scalar and x and y both vectors or matrices of the same
539    shape\) and return the result\.
540
541    Specialised variants are: axpy\_vect and axpy\_mat \(slightly faster, but no
542    check on the arguments\)
543
544      * double *scale*
545
546        The scale factor for the first vector/matrix \(a\)
547
548      * list *mv1*
549
550        First vector or matrix \(x\)
551
552      * list *mv2*
553
554        Second vector or matrix \(y\)
555
556  - <a name='26'></a>__::math::linearalgebra::add__ *mv1* *mv2*
557
558    Return a vector or matrix that is the sum of the two arguments \(x\+y\)
559
560    Specialised variants are: add\_vect and add\_mat \(slightly faster, but no
561    check on the arguments\)
562
563      * list *mv1*
564
565        First vector or matrix \(x\)
566
567      * list *mv2*
568
569        Second vector or matrix \(y\)
570
571  - <a name='27'></a>__::math::linearalgebra::sub__ *mv1* *mv2*
572
573    Return a vector or matrix that is the difference of the two arguments \(x\-y\)
574
575    Specialised variants are: sub\_vect and sub\_mat \(slightly faster, but no
576    check on the arguments\)
577
578      * list *mv1*
579
580        First vector or matrix \(x\)
581
582      * list *mv2*
583
584        Second vector or matrix \(y\)
585
586  - <a name='28'></a>__::math::linearalgebra::scale__ *scale* *mv*
587
588    Scale a vector or matrix and return the result, that is: compute a\*x\.
589
590    Specialised variants are: scale\_vect and scale\_mat \(slightly faster, but no
591    check on the arguments\)
592
593      * double *scale*
594
595        The scale factor for the vector/matrix \(a\)
596
597      * list *mv*
598
599        Vector or matrix \(x\)
600
601  - <a name='29'></a>__::math::linearalgebra::rotate__ *c* *s* *vect1* *vect2*
602
603    Apply a planar rotation to two vectors and return the result as a list of
604    two vectors: c\*x\-s\*y and s\*x\+c\*y\. In algorithms you can often easily
605    determine the cosine and sine of the angle, so it is more efficient to pass
606    that information directly\.
607
608      * double *c*
609
610        The cosine of the angle
611
612      * double *s*
613
614        The sine of the angle
615
616      * list *vect1*
617
618        First vector \(x\)
619
620      * list *vect2*
621
622        Seocnd vector \(x\)
623
624  - <a name='30'></a>__::math::linearalgebra::transpose__ *matrix*
625
626    Transpose a matrix
627
628      * list *matrix*
629
630        Matrix to be transposed
631
632  - <a name='31'></a>__::math::linearalgebra::matmul__ *mv1* *mv2*
633
634    Multiply a vector/matrix with another vector/matrix\. The result is a matrix,
635    if both x and y are matrices or both are vectors, in which case the "outer
636    product" is computed\. If one is a vector and the other is a matrix, then the
637    result is a vector\.
638
639      * list *mv1*
640
641        First vector/matrix \(x\)
642
643      * list *mv2*
644
645        Second vector/matrix \(y\)
646
647  - <a name='32'></a>__::math::linearalgebra::angle__ *vect1* *vect2*
648
649    Compute the angle between two vectors \(in radians\)
650
651      * list *vect1*
652
653        First vector
654
655      * list *vect2*
656
657        Second vector
658
659  - <a name='33'></a>__::math::linearalgebra::crossproduct__ *vect1* *vect2*
660
661    Compute the cross product of two \(three\-dimensional\) vectors
662
663      * list *vect1*
664
665        First vector
666
667      * list *vect2*
668
669        Second vector
670
671  - <a name='34'></a>__::math::linearalgebra::matmul__ *mv1* *mv2*
672
673    Multiply a vector/matrix with another vector/matrix\. The result is a matrix,
674    if both x and y are matrices or both are vectors, in which case the "outer
675    product" is computed\. If one is a vector and the other is a matrix, then the
676    result is a vector\.
677
678      * list *mv1*
679
680        First vector/matrix \(x\)
681
682      * list *mv2*
683
684        Second vector/matrix \(y\)
685
686*Common matrices and test matrices*
687
688  - <a name='35'></a>__::math::linearalgebra::mkIdentity__ *size*
689
690    Create an identity matrix of dimension *size*\.
691
692      * integer *size*
693
694        Dimension of the matrix
695
696  - <a name='36'></a>__::math::linearalgebra::mkDiagonal__ *diag*
697
698    Create a diagonal matrix whose diagonal elements are the elements of the
699    vector *diag*\.
700
701      * list *diag*
702
703        Vector whose elements are used for the diagonal
704
705  - <a name='37'></a>__::math::linearalgebra::mkRandom__ *size*
706
707    Create a square matrix whose elements are uniformly distributed random
708    numbers between 0 and 1 of dimension *size*\.
709
710      * integer *size*
711
712        Dimension of the matrix
713
714  - <a name='38'></a>__::math::linearalgebra::mkTriangular__ *size* ?uplo? ?value?
715
716    Create a triangular matrix with non\-zero elements in the upper or lower
717    part, depending on argument *uplo*\.
718
719      * integer *size*
720
721        Dimension of the matrix
722
723      * string *uplo*
724
725        Fill the upper \(U\) or lower part \(L\)
726
727      * double *value*
728
729        Value to fill the matrix with
730
731  - <a name='39'></a>__::math::linearalgebra::mkHilbert__ *size*
732
733    Create a Hilbert matrix of dimension *size*\. Hilbert matrices are very
734    ill\-conditioned with respect to eigenvalue/eigenvector problems\. Therefore
735    they are good candidates for testing the accuracy of algorithms and
736    implementations\.
737
738      * integer *size*
739
740        Dimension of the matrix
741
742  - <a name='40'></a>__::math::linearalgebra::mkDingdong__ *size*
743
744    Create a "dingdong" matrix of dimension *size*\. Dingdong matrices are
745    imprecisely represented, but have the property of being very stable in such
746    algorithms as Gauss elimination\.
747
748      * integer *size*
749
750        Dimension of the matrix
751
752  - <a name='41'></a>__::math::linearalgebra::mkOnes__ *size*
753
754    Create a square matrix of dimension *size* whose entries are all 1\.
755
756      * integer *size*
757
758        Dimension of the matrix
759
760  - <a name='42'></a>__::math::linearalgebra::mkMoler__ *size*
761
762    Create a Moler matrix of size *size*\. \(Moler matrices have a very simple
763    Choleski decomposition\. It has one small eigenvalue and it can easily upset
764    elimination methods for systems of linear equations\.\)
765
766      * integer *size*
767
768        Dimension of the matrix
769
770  - <a name='43'></a>__::math::linearalgebra::mkFrank__ *size*
771
772    Create a Frank matrix of size *size*\. \(Frank matrices are fairly
773    well\-behaved matrices\)
774
775      * integer *size*
776
777        Dimension of the matrix
778
779  - <a name='44'></a>__::math::linearalgebra::mkBorder__ *size*
780
781    Create a bordered matrix of size *size*\. \(Bordered matrices have a very
782    low rank and can upset certain specialised algorithms\.\)
783
784      * integer *size*
785
786        Dimension of the matrix
787
788  - <a name='45'></a>__::math::linearalgebra::mkWilkinsonW\+__ *size*
789
790    Create a Wilkinson W\+ of size *size*\. This kind of matrix has pairs of
791    eigenvalues that are very close together\. Usually the order \(size\) is odd\.
792
793      * integer *size*
794
795        Dimension of the matrix
796
797  - <a name='46'></a>__::math::linearalgebra::mkWilkinsonW\-__ *size*
798
799    Create a Wilkinson W\- of size *size*\. This kind of matrix has pairs of
800    eigenvalues with opposite signs, when the order \(size\) is odd\.
801
802      * integer *size*
803
804        Dimension of the matrix
805
806*Common algorithms*
807
808  - <a name='47'></a>__::math::linearalgebra::solveGauss__ *matrix* *bvect*
809
810    Solve a system of linear equations \(Ax=b\) using Gauss elimination\. Returns
811    the solution \(x\) as a vector or matrix of the same shape as bvect\.
812
813      * list *matrix*
814
815        Square matrix \(matrix A\)
816
817      * list *bvect*
818
819        Vector or matrix whose columns are the individual b\-vectors
820
821  - <a name='48'></a>__::math::linearalgebra::solvePGauss__ *matrix* *bvect*
822
823    Solve a system of linear equations \(Ax=b\) using Gauss elimination with
824    partial pivoting\. Returns the solution \(x\) as a vector or matrix of the same
825    shape as bvect\.
826
827      * list *matrix*
828
829        Square matrix \(matrix A\)
830
831      * list *bvect*
832
833        Vector or matrix whose columns are the individual b\-vectors
834
835  - <a name='49'></a>__::math::linearalgebra::solveTriangular__ *matrix* *bvect* ?uplo?
836
837    Solve a system of linear equations \(Ax=b\) by backward substitution\. The
838    matrix is supposed to be upper\-triangular\.
839
840      * list *matrix*
841
842        Lower or upper\-triangular matrix \(matrix A\)
843
844      * list *bvect*
845
846        Vector or matrix whose columns are the individual b\-vectors
847
848      * string *uplo*
849
850        Indicates whether the matrix is lower\-triangular \(L\) or upper\-triangular
851        \(U\)\. Defaults to "U"\.
852
853  - <a name='50'></a>__::math::linearalgebra::solveGaussBand__ *matrix* *bvect*
854
855    Solve a system of linear equations \(Ax=b\) using Gauss elimination, where the
856    matrix is stored as a band matrix \(*cf\.* [STORAGE](#section3)\)\.
857    Returns the solution \(x\) as a vector or matrix of the same shape as bvect\.
858
859      * list *matrix*
860
861        Square matrix \(matrix A; in band form\)
862
863      * list *bvect*
864
865        Vector or matrix whose columns are the individual b\-vectors
866
867  - <a name='51'></a>__::math::linearalgebra::solveTriangularBand__ *matrix* *bvect*
868
869    Solve a system of linear equations \(Ax=b\) by backward substitution\. The
870    matrix is supposed to be upper\-triangular and stored in band form\.
871
872      * list *matrix*
873
874        Upper\-triangular matrix \(matrix A\)
875
876      * list *bvect*
877
878        Vector or matrix whose columns are the individual b\-vectors
879
880  - <a name='52'></a>__::math::linearalgebra::determineSVD__ *A* *eps*
881
882    Determines the Singular Value Decomposition of a matrix: A = U S Vtrans\.
883    Returns a list with the matrix U, the vector of singular values S and the
884    matrix V\.
885
886      * list *A*
887
888        Matrix to be decomposed
889
890      * float *eps*
891
892        Tolerance \(defaults to 2\.3e\-16\)
893
894  - <a name='53'></a>__::math::linearalgebra::eigenvectorsSVD__ *A* *eps*
895
896    Determines the eigenvectors and eigenvalues of a real *symmetric* matrix,
897    using SVD\. Returns a list with the matrix of normalized eigenvectors and
898    their eigenvalues\.
899
900      * list *A*
901
902        Matrix whose eigenvalues must be determined
903
904      * float *eps*
905
906        Tolerance \(defaults to 2\.3e\-16\)
907
908  - <a name='54'></a>__::math::linearalgebra::leastSquaresSVD__ *A* *y* *qmin* *eps*
909
910    Determines the solution to a least\-sqaures problem Ax ~ y via singular value
911    decomposition\. The result is the vector x\.
912
913    Note that if you add a column of 1s to the matrix, then this column will
914    represent a constant like in: y = a\*x1 \+ b\*x2 \+ c\. To force the intercept to
915    be zero, simply leave it out\.
916
917      * list *A*
918
919        Matrix of independent variables
920
921      * list *y*
922
923        List of observed values
924
925      * float *qmin*
926
927        Minimum singular value to be considered \(defaults to 0\.0\)
928
929      * float *eps*
930
931        Tolerance \(defaults to 2\.3e\-16\)
932
933  - <a name='55'></a>__::math::linearalgebra::choleski__ *matrix*
934
935    Determine the Choleski decomposition of a symmetric positive semidefinite
936    matrix \(this condition is not checked\!\)\. The result is the lower\-triangular
937    matrix L such that L Lt = matrix\.
938
939      * list *matrix*
940
941        Matrix to be decomposed
942
943  - <a name='56'></a>__::math::linearalgebra::orthonormalizeColumns__ *matrix*
944
945    Use the modified Gram\-Schmidt method to orthogonalize and normalize the
946    *columns* of the given matrix and return the result\.
947
948      * list *matrix*
949
950        Matrix whose columns must be orthonormalized
951
952  - <a name='57'></a>__::math::linearalgebra::orthonormalizeRows__ *matrix*
953
954    Use the modified Gram\-Schmidt method to orthogonalize and normalize the
955    *rows* of the given matrix and return the result\.
956
957      * list *matrix*
958
959        Matrix whose rows must be orthonormalized
960
961  - <a name='58'></a>__::math::linearalgebra::dger__ *matrix* *alpha* *x* *y* ?scope?
962
963    Perform the rank 1 operation A \+ alpha\*x\*y' inline \(that is: the matrix A is
964    adjusted\)\. For convenience the new matrix is also returned as the result\.
965
966      * list *matrix*
967
968        Matrix whose rows must be adjusted
969
970      * double *alpha*
971
972        Scale factor
973
974      * list *x*
975
976        A column vector
977
978      * list *y*
979
980        A column vector
981
982      * list *scope*
983
984        If not provided, the operation is performed on all rows/columns of A if
985        provided, it is expected to be the list \{imin imax jmin jmax\} where:
986
987          + *imin* Minimum row index
988
989          + *imax* Maximum row index
990
991          + *jmin* Minimum column index
992
993          + *jmax* Maximum column index
994
995  - <a name='59'></a>__::math::linearalgebra::dgetrf__ *matrix*
996
997    Computes an LU factorization of a general matrix, using partial, pivoting
998    with row interchanges\. Returns the permutation vector\.
999
1000    The factorization has the form
1001
1002        P * A = L * U
1003
1004    where P is a permutation matrix, L is lower triangular with unit diagonal
1005    elements, and U is upper triangular\. Returns the permutation vector, as a
1006    list of length n\-1\. The last entry of the permutation is not stored, since
1007    it is implicitely known, with value n \(the last row is not swapped with any
1008    other row\)\. At index \#i of the permutation is stored the index of the row \#j
1009    which is swapped with row \#i at step \#i\. That means that each index of the
1010    permutation gives the permutation at each step, not the cumulated
1011    permutation matrix, which is the product of permutations\.
1012
1013      * list *matrix*
1014
1015        On entry, the matrix to be factored\. On exit, the factors L and U from
1016        the factorization P\*A = L\*U; the unit diagonal elements of L are not
1017        stored\.
1018
1019  - <a name='60'></a>__::math::linearalgebra::det__ *matrix*
1020
1021    Returns the determinant of the given matrix, based on PA=LU decomposition,
1022    i\.e\. Gauss partial pivotal\.
1023
1024      * list *matrix*
1025
1026        Square matrix \(matrix A\)
1027
1028      * list *ipiv*
1029
1030        The pivots \(optionnal\)\. If the pivots are not provided, a PA=LU
1031        decomposition is performed\. If the pivots are provided, we assume that
1032        it contains the pivots and that the matrix A contains the L and U
1033        factors, as provided by dgterf\. b\-vectors
1034
1035  - <a name='61'></a>__::math::linearalgebra::largesteigen__ *matrix* *tolerance* *maxiter*
1036
1037    Returns a list made of the largest eigenvalue \(in magnitude\) and associated
1038    eigenvector\. Uses iterative Power Method as provided as algorithm \#7\.3\.3 of
1039    Golub & Van Loan\. This algorithm is used here for a dense matrix \(but is
1040    usually used for sparse matrices\)\.
1041
1042      * list *matrix*
1043
1044        Square matrix \(matrix A\)
1045
1046      * double *tolerance*
1047
1048        The relative tolerance of the eigenvalue \(default:1\.e\-8\)\.
1049
1050      * integer *maxiter*
1051
1052        The maximum number of iterations \(default:10\)\.
1053
1054*Compability with the LA package* Two procedures are provided for
1055compatibility with Hume's LA package:
1056
1057  - <a name='62'></a>__::math::linearalgebra::to\_LA__ *mv*
1058
1059    Transforms a vector or matrix into the format used by the original LA
1060    package\.
1061
1062      * list *mv*
1063
1064        Matrix or vector
1065
1066  - <a name='63'></a>__::math::linearalgebra::from\_LA__ *mv*
1067
1068    Transforms a vector or matrix from the format used by the original LA
1069    package into the format used by the present implementation\.
1070
1071      * list *mv*
1072
1073        Matrix or vector as used by the LA package
1074
1075# <a name='section3'></a>STORAGE
1076
1077While most procedures assume that the matrices are given in full form, the
1078procedures *solveGaussBand* and *solveTriangularBand* assume that the
1079matrices are stored as *band matrices*\. This common type of "sparse" matrices
1080is related to ordinary matrices as follows:
1081
1082  - "A" is a full\-size matrix with N rows and M columns\.
1083
1084  - "B" is a band matrix, with m upper and lower diagonals and n rows\.
1085
1086  - "B" can be stored in an ordinary matrix of \(2m\+1\) columns \(one for each
1087    off\-diagonal and the main diagonal\) and n rows\.
1088
1089  - Element i,j \(i = \-m,\.\.\.,m; j =1,\.\.\.,n\) of "B" corresponds to element k,j of
1090    "A" where k = M\+i\-1 and M is at least \(\!\) n, the number of rows in "B"\.
1091
1092  - To set element \(i,j\) of matrix "B" use:
1093
1094        setelem B $j [expr {$N+$i-1}] $value
1095
1096\(There is no convenience procedure for this yet\)
1097
1098# <a name='section4'></a>REMARKS ON THE IMPLEMENTATION
1099
1100There is a difference between the original LA package by Hume and the current
1101implementation\. Whereas the LA package uses a linear list, the current package
1102uses lists of lists to represent matrices\. It turns out that with this
1103representation, the algorithms are faster and easier to implement\.
1104
1105The LA package was used as a model and in fact the implementation of, for
1106instance, the SVD algorithm was taken from that package\. The set of procedures
1107was expanded using ideas from the well\-known BLAS library and some algorithms
1108were updated from the second edition of J\.C\. Nash's book, Compact Numerical
1109Methods for Computers, \(Adam Hilger, 1990\) that inspired the LA package\.
1110
1111Two procedures are provided to make the transition between the two
1112implementations easier: *to\_LA* and *from\_LA*\. They are described above\.
1113
1114# <a name='section5'></a>TODO
1115
1116Odds and ends: the following algorithms have not been implemented yet:
1117
1118  - determineQR
1119
1120  - certainlyPositive, diagonallyDominant
1121
1122# <a name='section6'></a>NAMING CONFLICT
1123
1124If you load this package in a Tk\-enabled shell like wish, then the command
1125
1126    namespace import ::math::linearalgebra
1127
1128results in an error message about "scale"\. This is due to the fact that Tk
1129defines all its commands in the global namespace\. The solution is to import the
1130linear algebra commands in a namespace that is not the global one:
1131
1132    package require math::linearalgebra
1133    namespace eval compute {
1134        namespace import ::math::linearalgebra::*
1135        ... use the linear algebra version of scale ...
1136    }
1137
1138To use Tk's scale command in that same namespace you can rename it:
1139
1140    namespace eval compute {
1141        rename ::scale scaleTk
1142        scaleTk .scale ...
1143    }
1144
1145# <a name='section7'></a>Bugs, Ideas, Feedback
1146
1147This document, and the package it describes, will undoubtedly contain bugs and
1148other problems\. Please report such in the category *math :: linearalgebra* of
1149the [Tcllib Trackers](http://core\.tcl\.tk/tcllib/reportlist)\. Please also
1150report any ideas for enhancements you may have for either package and/or
1151documentation\.
1152
1153When proposing code changes, please provide *unified diffs*, i\.e the output of
1154__diff \-u__\.
1155
1156Note further that *attachments* are strongly preferred over inlined patches\.
1157Attachments can be made by going to the __Edit__ form of the ticket
1158immediately after its creation, and then using the left\-most button in the
1159secondary navigation bar\.
1160
1161# <a name='keywords'></a>KEYWORDS
1162
1163[least squares](\.\./\.\./\.\./\.\./index\.md\#least\_squares), [linear
1164algebra](\.\./\.\./\.\./\.\./index\.md\#linear\_algebra), [linear
1165equations](\.\./\.\./\.\./\.\./index\.md\#linear\_equations),
1166[math](\.\./\.\./\.\./\.\./index\.md\#math),
1167[matrices](\.\./\.\./\.\./\.\./index\.md\#matrices),
1168[matrix](\.\./\.\./\.\./\.\./index\.md\#matrix),
1169[vectors](\.\./\.\./\.\./\.\./index\.md\#vectors)
1170
1171# <a name='category'></a>CATEGORY
1172
1173Mathematics
1174
1175# <a name='copyright'></a>COPYRIGHT
1176
1177Copyright &copy; 2004\-2008 Arjen Markus <arjenmarkus@users\.sourceforge\.net>
1178Copyright &copy; 2004 Ed Hume <http://www\.hume\.com/contact\.us\.htm>
1179Copyright &copy; 2008 Michael Buadin <relaxkmike@users\.sourceforge\.net>
1180