ActiveTcl User Guide

[ Main table Of Contents | Tcllib Table Of Contents | Tcllib Index ]

math::linearalgebra(n) 1.0 "Math"

NAME

math::linearalgebra - Linear Algebra

TABLE OF CONTENTS

    TABLE OF CONTENTS
    SYNOPSIS
    DESCRIPTION
    PROCEDURES
    REMARKS ON THE IMPLEMENTATION
    KEYWORDS
    COPYRIGHT

SYNOPSIS

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

::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::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::mkVector ndim value
::math::linearalgebra::mkUnitVector ndim ndir
::math::linearalgebra::mkMatrix nrows ncols value
::math::linearalgebra::mkIdentity size
::math::linearalgebra::mkHilbert size
::math::linearalgebra::mkDingdong size
::math::linearalgebra::mkOnes size
::math::linearalgebra::solveGauss matrix bvect
::math::linearalgebra::solveTriangular matrix bvect
::math::linearalgebra::determineSVD A eps
::math::linearalgebra::eigenvectorsSVD A eps

DESCRIPTION

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.

PROCEDURES

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

::math::linearalgebra::dim obj
Returns the number of dimensions for the object (either 0 for a scalar, 1 for a vector and 2 for a matrix)

any obj
Scalar, vector, or matrix


::math::linearalgebra::shape obj
Returns the number of elements in each dimension for the object (either an empty list for a scalar, a single number for a vector and a list of the number of rows and columns for a matrix)

any obj
Scalar, vector, or matrix


::math::linearalgebra::conforming type obj1 obj2
Checks if two objects (vector or matrix) have conforming shapes, that is if they can be applied in an operation like addition or matrix multiplication.

string type
Type of check:

  • "shape" - the two objects have the same shape (for all element-wise operations)

  • "rows" - the two objects have the same number of rows (for use as A and b in a system of linear equations Ax = b

  • "matmul" - the first object has the same number of columns as the number of rows of the second object. Useful for matrix-matrix or matrix-vector multiplication.
list obj1
First vector or matrix (left operand)

list obj2
Second vector or matrix (right operand)


::math::linearalgebra::symmetric matrix ?eps?
Checks if the given (square) matrix is symmetric. The argument eps is the tolerance.

list matrix
Matrix to be inspected

float eps
Tolerance for determining approximate equality (defaults to 1.0e-8)


::math::linearalgebra::norm vector type
Returns the norm of the given vector. The type argument can be: 1, 2, inf or max, respectively the sum of absolute values, the ordinary Euclidean norm or the max norm.

list vector
Vector, list of coefficients

string type
Type of norm (default: 2, the Euclidean norm)


::math::linearalgebra::normMatrix matrix type
Returns the norm of the given matrix. The type argument can be: 1, 2, inf or max, respectively the sum of absolute values, the ordinary Euclidean norm or the max norm.

list matrix
Matrix, list of row vectors

string type
Type of norm (default: 2, the Euclidean norm)


::math::linearalgebra::dotproduct vect1 vect2
Determine the inproduct or dot product of two vectors. These must have the same shape (number of dimensions)

list vect1
First vector, list of coefficients

list vect2
Second vector, list of coefficients


::math::linearalgebra::unitLengthVector vector
Return a vector in the same direction with length 1.

list vector
Vector to be normalized


::math::linearalgebra::normalizeStat mv
Normalize the matrix or vector in a statistical sense: the mean of the elements of the columns of the result is zero and the standard deviation is 1.

list mv
Vector or matrix to be normalized in the above sense


::math::linearalgebra::axpy scale mv1 mv2
Return a vector or matrix that results from a "daxpy" operation, that is: compute a*x+y (a a scalar and x and y both vectors or matrices of the same shape) and return the result.

double scale
The scale factor for the first vector/matrix (a)

list mv1
First vector or matrix (x)

list mv2
Second vector or matrix (y)


::math::linearalgebra::add mv1 mv2
Return a vector or matrix that is the sum of the two arguments (x+y)

list mv1
First vector or matrix (x)

list mv2
Second vector or matrix (y)


::math::linearalgebra::sub mv1 mv2
Return a vector or matrix that is the difference of the two arguments (x-y)

list mv1
First vector or matrix (x)

list mv2
Second vector or matrix (y)


::math::linearalgebra::scale scale mv
Scale a vector or matrix and return the result, that is: compute a*x.

double scale
The scale factor for the vector/matrix (a)

list mv
Vector or matrix (x)


::math::linearalgebra::rotate c s vect1 vect2
Apply a planar rotation to two vectors and return the result as a list of two vectors: c*x-s*y and s*x+c*y. In algorithms you can often easily determine the cosine and sine of the angle, so it is more efficient to pass that information directly.

double c
The cosine of the angle

double s
The sine of the angle

list vect1
First vector (x)

list vect2
Seocnd vector (x)


::math::linearalgebra::transpose matrix
Transpose a matrix

list matrix
Matrix to be transposed


::math::linearalgebra::matmul mv1 mv2
Multiply a vector/matrix with another vector/matrix. The result is a matrix, if both x and y are matrices or both are vectors, in which case the "outer product" is computed. If one is a vector and the other is a matrix, then the result is a vector.

list mv1
First vector/matrix (x)

list mv2
Second vector/matrix (y)


::math::linearalgebra::mkVector ndim value
Create a vector with ndim elements, each with the value value.

integer ndim
Dimension of the vector (number of components)

double value
Uniform value to be used (default: 0.0)


::math::linearalgebra::mkUnitVector ndim ndir
Create a unit vector in ndim-dimensional space, along the ndir-th direction.

integer ndim
Dimension of the vector (number of components)

integer ndir
Direction (0, ..., ndim-1)


::math::linearalgebra::mkMatrix nrows ncols value
Create a matrix with nrows rows and ncols columns. All elements have the value value.

integer nrows
Number of rows

integer ncols
Number of columns

double value
Uniform value to be used (default: 0.0)


::math::linearalgebra::mkIdentity size
Create an identity matrix of dimension size.

integer size
Dimension of the matrix


::math::linearalgebra::mkHilbert size
Create a Hilbert matrix of dimension size. Hilbert matrices are very ill-conditioned with respect to eigenvalue/eigenvector problems. Therefore they are good candidates for testing the accuracy of algorithms and implementations.

integer size
Dimension of the matrix


::math::linearalgebra::mkDingdong size
Create a "dingdong" matrix of dimension size. Dingdong matrices are imprecisely represented, but have the property of being very stable in such algorithms as Gauss elimination.

integer size
Dimension of the matrix


::math::linearalgebra::mkOnes size
Create a sqaure matrix of dimension size whose entries are all 1.

integer size
Dimension of the matrix


::math::linearalgebra::solveGauss matrix bvect
Solve a system of linear equations (Ax=b) using Gauss elimination. Returns the solution (x) as a vector or matrix of the same shape as bvect.

list matrix
Square matrix (matrix A)

list bvect
Vector or matrix whose columns are the individual b-vectors


::math::linearalgebra::solveTriangular matrix bvect
Solve a system of linear equations (Ax=b) by backward substitution. The matrix is supposed to be upper-triangular.

list matrix
Upper-triangular matrix (matrix A)

list bvect
Vector or matrix whose columns are the individual b-vectors


::math::linearalgebra::determineSVD A eps
Determines the Singular Value Decomposition of a matrix: A = U S Vtrans. Returns a list with the matrix U, the vector of singular values S and the matrix V.

list A
Matrix to be decomposed

float eps
Tolerance (defaults to 2.3e-16) b-vectors


::math::linearalgebra::eigenvectorsSVD A eps
Determines the eigenvectors and eigenvalues of a real symmetric matrix, using SVD. Returns a list with the matrix of normalized eigenvectors and their eigenvalues.

list A
Matrix whose eigenvalues must be determined

float eps
Tolerance (defaults to 2.3e-16) b-vectors
TODO: all the other procedures and algorithms get/set procedures angle solveGauss, solveGaussBand determineQR, determineSVD, cholesky modified Gram-Schmidt leastSquaresSVD, leastSquares certainlyPositive, diagonallyDominant

REMARKS ON THE IMPLEMENTATION

TODO

KEYWORDS

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

COPYRIGHT

Copyright © 2004 Arjen Markus <arjenmarkus@users.sourceforge.net>
Copyright © 2004 Ed Hume<hume@hume.com>

Copyright © 2004 for compilation: ActiveState