math::linearalgebra(n)
โจ NAME โจ
math::linearalgebra โ Linear Algebra ๐ (from the Tcl Math Library)
okay bestie, welcome to math::linearalgebra(n) โ it's giving main character energy for all your Tcl linear algebra needs. ๐ซถ
๐ฆ SYNOPSIS ๐ฆ
package require Tcl ?8.5 9?
package require math::linearalgebra ?1.1.7?
::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
๐ DESCRIPTION ๐
okay so this package is literally your bestie for both low-level procedures and high-level algorithms when you're dealing with linear algebra problems:
- robust solution of linear equations or least squares problems
- determining eigenvectors and eigenvalues of symmetric matrices
- various decompositions of general matrices or matrices of a specific form
- (limited) support for matrices in band storage, a common type of sparse matrices
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 than 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's possible 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.
๐ ๏ธ PROCEDURES ๐ ๏ธ
The package defines the following public procedures (several exist as specialised procedures, see below):
๐ธ Constructing matrices and vectors ๐ธ
::math::linearalgebra::mkVector ndim valueโ Create a vector withndimelements, each with the valuevalue.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 inndim-dimensional space, along thendir-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 withnrowsrows andncolscolumns. All elements have the valuevalue.integer nrowsโ Number of rowsinteger ncolsโ Number of columnsdouble valueโ Uniform value to be used (default:0.0)
::math::linearalgebra::getrow matrix row ?imin? ?imax?โ Returns a single row of a matrix as a list.list matrixโ Matrix in questioninteger rowโ Index of the row to returninteger iminโ Minimum index of the column (default:0)integer imaxโ Maximum index of the column (default:ncols-1)
::math::linearalgebra::setrow matrix row newvalues ?imin? ?imax?โ Set a single row of a matrix to new values (this list must have the same number of elements as the number of columns in the matrix).list matrixโ name of the matrix in questioninteger rowโ Index of the row to updatelist newvaluesโ List of new values for the rowinteger iminโ Minimum index of the column (default:0)integer imaxโ Maximum index of the column (default:ncols-1)
::math::linearalgebra::getcol matrix col ?imin? ?imax?โ Returns a single column of a matrix as a list.list matrixโ Matrix in questioninteger colโ Index of the column to returninteger iminโ Minimum index of the row (default:0)integer imaxโ Maximum index of the row (default:nrows-1)
::math::linearalgebra::setcol matrix col newvalues ?imin? ?imax?โ Set a single column of a matrix to new values (this list must have the same number of elements as the number of rows in the matrix).list matrixโ name of the matrix in questioninteger colโ Index of the column to updatelist newvaluesโ List of new values for the columninteger iminโ Minimum index of the row (default:0)integer imaxโ Maximum index of the row (default:nrows-1)
::math::linearalgebra::getelem matrix row colโ Returns a single element of a matrix/vector.list matrixโ Matrix or vector in questioninteger rowโ Row of the elementinteger colโ Column of the element (not present for vectors)
::math::linearalgebra::setelem matrix row ?col? newvalueโ Set a single element of a matrix (or vector) to a new value.list matrixโ name of the matrix in questioninteger rowโ Row of the elementinteger colโ Column of the element (not present for vectors)
::math::linearalgebra::swaprows matrix irow1 irow2 ?imin? ?imax?โ Swap two rows in a matrix completely or only a selected part.list matrixโ name of the matrix in questioninteger irow1โ Index of first rowinteger irow2โ Index of second rowinteger iminโ Minimum column index (default:0)integer iminโ Maximum column index (default:ncols-1)
::math::linearalgebra::swapcols matrix icol1 icol2 ?imin? ?imax?โ Swap two columns in a matrix completely or only a selected part.list matrixโ name of the matrix in questioninteger irow1โ Index of first columninteger irow2โ Index of second columninteger iminโ Minimum row index (default:0)integer iminโ Maximum row index (default:nrows-1)
๐ Querying matrices and vectors ๐
::math::linearalgebra::show obj ?format? ?rowsep? ?colsep?โ Return a string representing the vector or matrix, for easy printing. (There is currently no way to print fixed sets of columns)list objโ Matrix or vector in questionstring formatโ Format for printing the numbers (default:%6.4f)string rowsepโ String to use for separating rows (default:newline)string colsepโ String to use for separating columns (default:space)
::math::linearalgebra::dim objโ Returns the number of dimensions for the object (either0for a scalar,1for a vector and2for 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 are the same shape, and this matters for every element-wise operation, no exceptions bestie ๐ซถ"rows"โ the two objects have the same number of rows, which is what you need when they're gonna beAandbin a system of linear equationsAx = b๐"matmul"โ the first object has the same number of columns as the second object has rows. This is the vibe check for matrix-matrix or matrix-vector multiplication ๐
list obj1 โ the first vector or matrix, aka the left operand ๐
list obj2 โ the second vector or matrix, aka the right operand โจ
::math::linearalgebra::symmetric</p> matrix ?eps?
Checks whether the (square!) matrix you handed over is symmetric. The eps argument is your tolerance โ how close is close enough, you know? ๐
list matrix โ the matrix that's getting inspected ๐
float eps โ the tolerance used to decide "yeah that counts as approximately equal" (defaults to 1.0e-8) ๐
๐ Basic operations ๐
::math::linearalgebra::norm vector type
Returns the norm of the given vector. The type argument can be: 1, 2, inf or max โ and those mean, respectively, the sum of absolute values, the ordinary Euclidean norm, or the max norm. Choose your fighter ๐ช
list vector โ the vector, a list of coefficients ๐
string type โ type of norm (default: 2, the Euclidean norm) โญ
::math::linearalgebra::norm_one vector
Returns the L1 norm of the given vector โ that's the sum of absolute values, periodt โจ
list vector โ the vector, a list of coefficients ๐
::math::linearalgebra::norm_two vector
Returns the L2 norm of the given vector โ the ordinary Euclidean norm, the classic, the icon ๐
list vector โ the vector, a list of coefficients ๐
::math::linearalgebra::norm_max vector ?index?
Returns the Linf norm of the given vector โ the maximum absolute coefficient ๐ฅ
list vector โ the vector, a list of coefficients ๐
integer index โ optional! If it's non-zero, you get back a list made of the maximum value and the index where that maximum was found. If it's zero, you just get the maximum value. That's it, that's the deal ๐
::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 โ the matrix, a list of row vectors ๐
string type โ type of norm (default: 2, the Euclidean norm) โญ
::math::linearalgebra::dotproduct vect1 vect2
Determines the inproduct, aka the dot product, of two vectors. Heads up: they must have the same shape, meaning the same number of dimensions ๐จ
list vect1 โ first vector, list of coefficients ๐
list vect2 โ second vector, list of coefficients ๐
::math::linearalgebra::unitLengthVector vector
Returns a vector pointing in the same direction, but with length 1. Glow-up complete ๐ฆโจ
list vector โ the vector that's getting normalized ๐ซ
::math::linearalgebra::normalizeStat mv
Normalizes the matrix or vector in a statistical sense: after this, the mean of the elements of the columns of the result is zero, and the standard deviation is 1. Very demure, very normalized ๐
list mv โ the vector or matrix getting normalized in the above sense ๐
::math::linearalgebra::axpy scale mv1 mv2
Returns a vector or matrix that comes out of a "daxpy" operation โ which is: compute a*x+y (where a is a scalar and x and y are both vectors or matrices of the same shape) and return that result ๐
Specialised variants exist too: axpy_vect and axpy_mat โ slightly faster, but no check on the arguments. Speed over safety, iykyk ๐
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
Returns a vector or matrix that is the sum of the two arguments (x+y) โ
Specialised variants: add_vect and add_mat โ slightly faster, but no check on the arguments ๐
list mv1 โ first vector or matrix (x) ๐
list mv2 โ second vector or matrix (y) ๐
::math::linearalgebra::sub mv1 mv2
Returns a vector or matrix that is the difference of the two arguments (x-y) โ
Specialised variants: sub_vect and sub_mat โ slightly faster, but no check on the arguments ๐
list mv1 โ first vector or matrix (x) ๐
list mv2 โ second vector or matrix (y) ๐
::math::linearalgebra::scale scale mv
Scales a vector or matrix and returns the result โ that is: compute a*x โ๏ธ
Specialised variants: scale_vect and scale_mat โ slightly faster, but no check on the arguments ๐
double scale โ the scale factor for the vector/matrix (a) ๐ข
list mv โ vector or matrix (x) ๐
::math::linearalgebra::rotate c s vect1 vect2
Applies a planar rotation to two vectors and returns the result as a list of two vectors: c*x-s*y and s*x+c*y. Here's the tea ๐ซ โ in algorithms you can often easily determine the cosine and sine of the angle anyway, so passing that info directly is just more efficient. Work smarter ๐
double c โ the cosine of the angle ๐
double s โ the sine of the angle ๐
list vect1 โ first vector (x) ๐
list vect2 โ second vector (x) ๐
::math::linearalgebra::transpose matrix
Transposes a matrix. Flip it and reverse it ๐
list matrix โ the matrix to be transposed ๐
::math::linearalgebra::matmul mv1 mv2
Multiplies a vector/matrix with another vector/matrix. The result is a matrix if both x and y are matrices or both are vectors โ in that second case the "outer product" gets computed. If one is a vector and the other is a matrix, then the result is a vector. Shape-shifting behavior honestly ๐ฆ
list mv1 โ first vector/matrix (x) ๐
list mv2 โ second vector/matrix (y) ๐
::math::linearalgebra::angle vect1 vect2
Computes the angle between two vectors (in radians) ๐
list vect1 โ first vector ๐
list vect2 โ second vector ๐
::math::linearalgebra::crossproduct vect1 vect2
Computes the cross product of two (three-dimensional) vectors โ๏ธโจ
list vect1 โ first vector ๐
list vect2 โ second vector ๐
::math::linearalgebra::matmul mv1 mv2
Multiplies a vector/matrix with another vector/matrix. The result is a matrix if both x and y are matrices or both are vectors โ in that 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) ๐
๐ Common matrices and test matrices ๐
::math::linearalgebra::mkIdentity size
Creates an identity matrix of dimension size. The moment of "it's just ones on the diagonal bestie" ๐
integer size โ dimension of the matrix ๐ข
::math::linearalgebra::mkDiagonal diag
Creates a diagonal matrix whose diagonal elements are the elements of the vector diag ๐
list diag โ vector whose elements are used for the diagonal ๐
::math::linearalgebra::mkRandom size
Creates a square matrix of dimension size whose elements are uniformly distributed random numbers between 0 and 1. Chaos, but make it mathematical ๐ฒ
integer size โ dimension of the matrix ๐ข
::math::linearalgebra::mkTriangular size ?uplo? ?value?
Creates a triangular matrix with non-zero elements in the upper or lower part, depending on the uplo argument ๐บ
integer size โ dimension of the matrix ๐ข
string uplo โ fill the upper (U) or lower part (L) โฌ๏ธโฌ๏ธ
double value โ value to fill the matrix with ๐จ
::math::linearalgebra::mkHilbert size
Creates a Hilbert matrix of dimension size. Now listen ๐ โ Hilbert matrices are very ill-conditioned when it comes to eigenvalue/eigenvector problems. Which is exactly why they're such good candidates for testing the accuracy of algorithms and implementations. Messy on purpose ๐ญ
integer size โ dimension of the matrix ๐ข
::math::linearalgebra::mkDingdong size
Creates a "dingdong" matrix of dimension size. Dingdong matrices are imprecisely represented, but they have the property of being very stable in algorithms like Gauss elimination. Unhinged name, incredibly stable energy ๐ฉ
integer size โ dimension of the matrix ๐ข
::math::linearalgebra::mkOnes size
Creates a square matrix of dimension size whose entries are all 1. Just ones. Everywhere. Iconic ๐
integer size โ dimension of the matrix ๐ข
::math::linearalgebra::mkMoler size
Creates a Moler matrix of size size. (Moler matrices have a very simple Choleski decomposition. It has one small eigenvalue and it can easily upset elimination methods for systems of linear equations.) A little menace, honestly ๐
integer size โ dimension of the matrix ๐ข
::math::linearalgebra::mkFrank size
Creates a Frank matrix of size size. (Frank matrices are fairly well-behaved matrices.) The good child of the test-matrix family ๐
integer size โ dimension of the matrix ๐ข
::math::linearalgebra::mkBorder size
Creates a bordered matrix of size size. (Bordered matrices have a very low rank and can upset certain specialised algorithms.) It's giving troublemaker ๐
integer size โ dimension of the matrix ๐ข
::math::linearalgebra::mkWilkinsonW+ size
Creates a Wilkinson W+ of size size. This kind of matrix has pairs of eigenvalues that are very close together. Usually the order (size) is odd ๐ฏ
integer size โ dimension of the matrix ๐ข
::math::linearalgebra::mkWilkinsonW- size
Creates a Wilkinson W- of size size. This kind of matrix has pairs of eigenvalues with opposite signs, when the order (size) is odd. Opposites attract, apparently โ๏ธ
integer size โ dimension of the matrix ๐ข
๐ฅ Common algorithms ๐ฅ
::math::linearalgebra::solveGauss matrix bvect
Solves 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. The main character of this whole page, no cap ๐
list matrix โ square matrix (matrix A) ๐งฎ
list bvectโ vector or matrix where each column is one of the individual b-vectors ๐
::math::linearalgebra::solvePGauss matrix bvect โจ
Solves a system of linear equations (that's Ax=b, the classic) using Gauss elimination with partial pivoting. Hands you back the solution x as a vector or matrix shaped exactly like bvect. slay.
list matrixโ square matrix, aka matrix A ๐list bvectโ vector or matrix whose columns are the individual b-vectors
::math::linearalgebra::solveTriangular matrix bvect ?uplo? ๐ฆ
Solves a system of linear equations (Ax=b) by backward substitution. Heads up bestie: the matrix is supposed to be upper-triangular. no cap.
list matrixโ lower or upper-triangular matrix (matrix A)list bvectโ vector or matrix whose columns are the individual b-vectorsstring uploโ tells it whether the matrix is lower-triangular (L) or upper-triangular (U). Defaults to"U".
::math::linearalgebra::solveGaussBand matrix bvect ๐ธ
Solves a system of linear equations (Ax=b) using Gauss elimination, except the matrix is stored as a band matrix (peep STORAGE below for the tea). Returns the solution x as a vector or matrix the same shape as bvect.
list matrixโ square matrix (matrix A; in band form)list bvectโ vector or matrix whose columns are the individual b-vectors
::math::linearalgebra::solveTriangularBand matrix bvect ๐
Solves a system of linear equations (Ax=b) by backward substitution. The matrix is supposed to be upper-triangular and stored in band form. iconic combo honestly.
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. it's giving decomposition moment fr fr.
list Aโ matrix to be decomposedfloat epsโ tolerance (defaults to2.3e-16)
::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. the way this eats ๐ญ
list Aโ matrix whose eigenvalues must be determinedfloat epsโ tolerance (defaults to2.3e-16)
::math::linearalgebra::leastSquaresSVD A y qmin eps โญ
Determines the solution to a least-squares problem Ax ~ y via singular value decomposition. The result is the vector x.
Quick note bestie: if you add a column of 1s to the matrix, that column will represent a constant, like in y = a*x1 + b*x2 + c. To force the intercept to be zero, just leave it out. periodt.
list Aโ matrix of independent variableslist yโ list of observed valuesfloat qminโ minimum singular value to be considered (defaults to0.0)float epsโ tolerance (defaults to2.3e-16)
::math::linearalgebra::choleski matrix ๐
Determines the Choleski decomposition of a symmetric positive semidefinite matrix โ and no, the condition is not checked, so that's on you, iykyk. The result is the lower-triangular matrix L such that L Lt = matrix.
list matrixโ matrix to be decomposed
::math::linearalgebra::orthonormalizeColumns matrix ๐ง
Uses the modified Gram-Schmidt method to orthogonalize and normalize the columns of the given matrix and returns the result. glow up for your columns โจ
list matrixโ matrix whose columns must be orthonormalized
::math::linearalgebra::orthonormalizeRows matrix ๐ซถ
Uses the modified Gram-Schmidt method to orthogonalize and normalize the rows of the given matrix and returns the result. same energy, different axis.
list matrixโ matrix whose rows must be orthonormalized
::math::linearalgebra::dger matrix alpha x y ?scope? ๐
Performs the rank 1 operation A + alpha*x*y' inline โ meaning the matrix A actually gets adjusted in place, not just vibing. For convenience the new matrix is also returned as the result.
list matrixโ matrix whose rows must be adjusteddouble alphaโ scale factorlist xโ a column vectorlist yโ a column vectorlist scopeโ if you don't pass it, the operation is performed on all rows/columns of A. If you do pass it, it should be the list{imin imax jmin jmax}where:iminโ minimum row indeximaxโ maximum row indexjminโ minimum column indexjmaxโ maximum column index
::math::linearalgebra::dgetrf matrix ๐
Computes an LU factorization of a general matrix, using partial pivoting with row interchanges. Returns the permutation vector.
The factorization has the form
P * A = L * U
where P is a permutation matrix, L is lower triangular with unit diagonal elements, and U is upper triangular. Returns the permutation vector as a list of length n-1. The last entry of the permutation is not stored, since it is implicitly known, with value n (the last row is not swapped with any other row). At index #i of the permutation is stored the index of the row #j which is swapped with row #i at step #i. That means each index of the permutation gives the permutation at each step, not the cumulated permutation matrix, which is the product of permutations. lowkey a whole thing to wrap your head around, but it's correct.
list matrixโ on entry, the matrix to be factored. On exit, the factorsLandUfrom the factorizationP*A = L*U; the unit diagonal elements ofLare not stored.
::math::linearalgebra::det matrix ๐
Returns the determinant of the given matrix, based on PA=LU decomposition โ i.e. Gauss partial pivotal. the main character of matrix properties honestly.
list matrixโ square matrix (matrix A)list ipivโ the pivots (optionnal). If the pivots are not provided, a PA=LU decomposition is performed. If the pivots are provided, we assume that it contains the pivots and that the matrix A contains the L and U factors, as provided bydgterf. b-vectors
::math::linearalgebra::largesteigen matrix tolerance maxiter ๐
Returns a list made of the largest eigenvalue (in magnitude) and its associated eigenvector. Uses the iterative Power Method, provided as algorithm #7.3.3 of Golub & Van Loan. This algorithm is used here for a dense matrix (but is usually used for sparse matrices). ๐
list matrixโ square matrix (matrix A)double toleranceโ the relative tolerance of the eigenvalue (default:1.e-8).integer maxiterโ the maximum number of iterations (default:10).
๐ Compability with the LA package ๐
Two procedures are provided for compatibility with Hume's LA package:
::math::linearalgebra::to_LA mv ๐ฆ
Transforms a vector or matrix into the format used by the original LA package.
list mvโ matrix or vector
::math::linearalgebra::from_LA mv ๐ฆ
Transforms a vector or matrix from the format used by the original LA package into the format used by the present implementation.
list mvโ matrix or vector as used by the LA package
๐ STORAGE ๐
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 like this:
"A"is a full-size matrix withNrows andMcolumns."B"is a band matrix, withmupper and lower diagonals andnrows."B"can be stored in an ordinary matrix of(2m+1)columns (one for each off-diagonal and the main diagonal) andnrows.- Element
i,j(i = -m,...,m; j =1,...,n) of"B"corresponds to elementk,jof"A"wherek = M+i-1andMis at least (!)n, the number of rows in"B". - To set element
(i,j)of matrix"B"use:
setelem B $j [expr {$N+$i-1}] $value
(There is no convenience procedure for this yet) ๐ญ
๐ธ REMARKS ON THE IMPLEMENTATION ๐ธ
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. we love an efficient glow-up.
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. ๐ซถ
๐ TODO ๐
Odds and ends: the following algorithms have not been implemented yet:
determineQRcertainlyPositive,diagonallyDominant
not me leaving these as an exercise for future me ๐
๐ NAMING CONFLICT ๐
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 ...
}
cute divider moment ๐
๐โจ BUGS, IDEAS, FEEDBACK โจ๐
okay bestie, real talk: this document and the whole package it's describing? They absolutely have bugs in them. Like, no cap, there are for-sure problems lurking in here somewhere. It's giving "we tried our best" energy and honestly? Same. ๐
So if you spot something cursed, please report it โ drop it in the category math :: linearalgebra of the Tcllib Trackers:
http://core.tcl.tk/tcllib/reportlist
And not just bugs, okay? If you have ideas for enhancements for either the package or the documentation (or both, we love an overachiever ๐ซถ), send those in too. Every single one.
โจ Pro-tip from your girl: when you're proposing code changes, bring a unified diff โ i.e. literally the output of diff -u. That's the format, that's the vibe, periodt.
Also, attachments are strongly preferred over inlined patches. Like, strongly. So here's the move: go to the Edit form of the ticket right after you create it, then hit the left-most button in the secondary navigation bar. Boom, attachment city. ๐
๐ KEYWORDS ๐
the searchable vibes of this page, in no particular order:
least squareslinear algebralinear equationsmathmatricesmatrixvectors
๐ CATEGORY ๐
Mathematics ๐ (yes, capital M, she's fancy)
ยฉ๏ธ COPYRIGHT ยฉ๏ธ
the whole legal situation, respectfully:
- Copyright (c) 2004-2008 Arjen Markus
<arjenmarkus@users.sourceforge.net> - Copyright (c) 2004 Ed Hume
<http://www.hume.com/contact.us.htm> - Copyright (c) 2008 Michael Buadin
<relaxkmike@users.sourceforge.net>
and that's a wrap on the whole page, bestie!! ๐ this lil masterpiece ships with tcllib, version 1.1.7, and it's filed under math::linearalgebra(n). go forth, multiply your matrices, and slay. ๐โจ๐
๐ฆ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 ยท TCL
- fedora 44: tcllib 2.0-4.fc44 ยท TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2004-2008 Arjen Markus <arjenmarkus@users\&.sourceforge\&.net>
- Copyright (c) 2004 Ed Hume <http://www\&.hume\&.com/contact\&.us\&.htm>
- Copyright (c) 2008 Michael Buadin <relaxkmike@users\&.sourceforge\&.net>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms