Tcl Library Source Code

Bounty program for improvements to Tcl and certain Tcl packages.

[ Main Table Of Contents | Table Of Contents | Keyword Index | Categories | Modules | Applications ]


math::linearalgebra - Linear Algebra

Table Of Contents


package require Tcl ?8.4?
package require math::linearalgebra ?1.1.5?

::math::linearalgebra::mkVector ndim value
::math::linearalgebra::mkUnitVector ndim ndir
::math::linearalgebra::mkMatrix nrows ncols value
::math::linearalgebra::getrow matrix row ?imin? ?imax?
::math::linearalgebra::setrow matrix row newvalues ?imin? ?imax?
::math::linearalgebra::getcol matrix col ?imin? ?imax?
::math::linearalgebra::setcol matrix col newvalues ?imin? ?imax?
::math::linearalgebra::getelem matrix row col
::math::linearalgebra::setelem matrix row ?col? newvalue
::math::linearalgebra::swaprows matrix irow1 irow2 ?imin? ?imax?
::math::linearalgebra::swapcols matrix icol1 icol2 ?imin? ?imax?
::math::linearalgebra::show obj ?format? ?rowsep? ?colsep?
::math::linearalgebra::dim obj
::math::linearalgebra::shape obj
::math::linearalgebra::conforming type obj1 obj2
::math::linearalgebra::symmetric matrix ?eps?
::math::linearalgebra::norm vector type
::math::linearalgebra::norm_one vector
::math::linearalgebra::norm_two vector
::math::linearalgebra::norm_max vector ?index?
::math::linearalgebra::normMatrix matrix type
::math::linearalgebra::dotproduct vect1 vect2
::math::linearalgebra::unitLengthVector vector
::math::linearalgebra::normalizeStat mv
::math::linearalgebra::axpy scale mv1 mv2
::math::linearalgebra::add mv1 mv2
::math::linearalgebra::sub mv1 mv2
::math::linearalgebra::scale scale mv
::math::linearalgebra::rotate c s vect1 vect2
::math::linearalgebra::transpose matrix
::math::linearalgebra::matmul mv1 mv2
::math::linearalgebra::angle vect1 vect2
::math::linearalgebra::crossproduct vect1 vect2
::math::linearalgebra::matmul mv1 mv2
::math::linearalgebra::mkIdentity size
::math::linearalgebra::mkDiagonal diag
::math::linearalgebra::mkRandom size
::math::linearalgebra::mkTriangular size ?uplo? ?value?
::math::linearalgebra::mkHilbert size
::math::linearalgebra::mkDingdong size
::math::linearalgebra::mkOnes size
::math::linearalgebra::mkMoler size
::math::linearalgebra::mkFrank size
::math::linearalgebra::mkBorder size
::math::linearalgebra::mkWilkinsonW+ size
::math::linearalgebra::mkWilkinsonW- size
::math::linearalgebra::solveGauss matrix bvect
::math::linearalgebra::solvePGauss matrix bvect
::math::linearalgebra::solveTriangular matrix bvect ?uplo?
::math::linearalgebra::solveGaussBand matrix bvect
::math::linearalgebra::solveTriangularBand matrix bvect
::math::linearalgebra::determineSVD A eps
::math::linearalgebra::eigenvectorsSVD A eps
::math::linearalgebra::leastSquaresSVD A y qmin eps
::math::linearalgebra::choleski matrix
::math::linearalgebra::orthonormalizeColumns matrix
::math::linearalgebra::orthonormalizeRows matrix
::math::linearalgebra::dger matrix alpha x y ?scope?
::math::linearalgebra::dgetrf matrix
::math::linearalgebra::det matrix
::math::linearalgebra::largesteigen matrix tolerance maxiter
::math::linearalgebra::to_LA mv
::math::linearalgebra::from_LA mv


This package offers both low-level procedures and high-level algorithms to deal with linear algebra problems:

It arose as a re-implementation of Hume's LA package and the desire to offer low-level procedures as found in the well-known BLAS library. Matrices are implemented as lists of lists rather linear lists with reserved elements, as in the original LA package, as it was found that such an implementation is actually faster.

It is advisable, however, to use the procedures that are offered, such as setrow and getrow, rather than rely on this representation explicitly: that way it is to switch to a possibly even faster compiled implementation that supports the same API.

Note: When using this package in combination with Tk, there may be a naming conflict, as both this package and Tk define a command scale. See the NAMING CONFLICT section below.


The package defines the following public procedures (several exist as specialised procedures, see below):

Constructing matrices and vectors

Querying matrices and vectors

Basic operations

Common matrices and test matrices

Common algorithms

Compability with the LA package Two procedures are provided for compatibility with Hume's LA package:


While most procedures assume that the matrices are given in full form, the procedures solveGaussBand and solveTriangularBand assume that the matrices are stored as band matrices. This common type of "sparse" matrices is related to ordinary matrices as follows:

(There is no convenience procedure for this yet)


There is a difference between the original LA package by Hume and the current implementation. Whereas the LA package uses a linear list, the current package uses lists of lists to represent matrices. It turns out that with this representation, the algorithms are faster and easier to implement.

The LA package was used as a model and in fact the implementation of, for instance, the SVD algorithm was taken from that package. The set of procedures was expanded using ideas from the well-known BLAS library and some algorithms were updated from the second edition of J.C. Nash's book, Compact Numerical Methods for Computers, (Adam Hilger, 1990) that inspired the LA package.

Two procedures are provided to make the transition between the two implementations easier: to_LA and from_LA. They are described above.


Odds and ends: the following algorithms have not been implemented yet:


If you load this package in a Tk-enabled shell like wish, then the command

namespace import ::math::linearalgebra

results in an error message about "scale". This is due to the fact that Tk defines all its commands in the global namespace. The solution is to import the linear algebra commands in a namespace that is not the global one:

package require math::linearalgebra
namespace eval compute {
    namespace import ::math::linearalgebra::*
    ... use the linear algebra version of scale ...

To use Tk's scale command in that same namespace you can rename it:

namespace eval compute {
    rename ::scale scaleTk
    scaleTk .scale ...

Bugs, Ideas, Feedback

This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category math :: linearalgebra of the Tcllib Trackers. Please also report any ideas for enhancements you may have for either package and/or documentation.

When proposing code changes, please provide unified diffs, i.e the output of diff -u.

Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.


least squares, linear algebra, linear equations, math, matrices, matrix, vectors




Copyright © 2004-2008 Arjen Markus
Copyright © 2004 Ed Hume http://www\.hume\.com/contact\.us\.htm
Copyright © 2008 Michael Buadin