| Up | Next | Prev | PrevTail | Tail |
A very powerful feature of REDUCE is the ease with which matrix calculations can be performed. This package extends the available matrix feature to enable calculations with sparse matrices. This package also provides a selection of functions that are useful in the world of linear algebra with respect to sparse matrices.
The package is loaded by: load_package sparse;
To extend the the syntax to this class of calculations we need to add an expression type sparse.
An identifier may be declared a sparse variable by the declaration SPARSE. The size of the sparse matrix must be declared explicitly in the matrix declaration. For example,
sparse aa(10,1),bb(200,200);
declares aa to be a 10 x 1 (column) sparse matrix and y to be a 200 x 200 sparse matrix. The declaration sparse is similar to the declaration matrix. Once a symbol is declared to name a sparse matrix, it can not also be used to name an array, operator, procedure, or used as an ordinary variable. For more information see the Matrix Variables section (14.2).
Once a matix has been declared a sparse matrix all elements of the matrix are initialized to 0. Thus when a sparse matrix is initially referred to the message
"Empty matrix"
is returned. When printing out a matrix only the non-zero elements are printed. This is due to the fact that only the non-zero elements of the matrix are stored. To assign the elements of the declared matrix we use the following syntax. Assuming aa and bb have been declared as spasre matrices, we simply write,
aa(1,1):=10; bb(100,150):=a;
etc. This then sets the element in the first row and first column to 10, or the element in the 100th row and 150th column to a.
Once an element of a sparse matrix has been assingned, it may be referred to in standard array element notation. Thus aa(2,1) refers to the element in the second row and first column of the sparse matrix aa.
These follow the normal rules of matrix algebra. Sums and products must be of compatible size; otherwise an error will result during evaluation. Similarly, only square matrices may be raised to a power. A negative power is computed as the inverse of the matrix raised to the corresponding positive power. For more information and the syntax for matrix algebra see the Matrix Expressions section (14.3).
The operators in the Sparse Matrix Package are the same as those in the Matrix Package with the exception that the nullspace operator is not defined. See section Operators with Matrix Arguments (14.4) for more details.
In the examples the matrix 𝒜𝒜 will be
det aa;
135
trace aa;
18
rank aa;
4
spmateigen(aa,eta);
{{eta - 1,1,
spm(1,1) := arbcomplex(1)$
},
{eta - 3,1,
spm(2,1) := arbcomplex(2)$
},
{eta - 5,1,
spm(3,1) := arbcomplex(3)$
},
{eta - 9,1,
spm(4,1) := arbcomplex(4)$
}}
This package is an extension of the Linear Algebra Package for REDUCE described in section 20.32. These functions are described alphabetically in section 20.52.6. They can be classified into four sections(n.b: the numbers after the dots signify the function label in section 6).
| spadd_columns | … | 20.52.6.1 | spadd_rows | … | 20.52.6.2 |
| spadd_to_columns | … | 20.52.6.3 | spadd_to_rows | … | 20.52.6.4 |
| spaugment_columns | … | 20.52.6.5 | spchar_poly | … | 20.52.6.9 |
| spcol_dim | … | 20.52.6.12 | spcopy_into | … | 20.52.6.14 |
| spdiagonal | … | 20.52.6.15 | spextend | … | 20.52.6.16 |
| spfind_companion | … | 20.52.6.17 | spget_columns | … | 20.52.6.18 |
| spget_rows | … | 20.52.6.19 | sphermitian_tp | … | 20.52.6.21 |
| spmatrix_augment | … | 20.52.6.27 | spmatrix_stack | … | 20.52.6.29 |
| spminor | … | 20.52.6.30 | spmult_columns | … | 20.52.6.31 |
| spmult_rows | … | 20.52.6.32 | sppivot | … | 20.52.6.33 |
| spremove_columns | … | 20.52.6.35 | spremove_rows | … | 20.52.6.36 |
| sprow_dim | … | 20.52.6.37 | sprows_pivot | … | 20.52.6.38 |
| spstack_rows | … | 20.52.6.41 | spsub_matrix | … | 20.52.6.42 |
| spswap_columns | … | 20.52.6.44 | spswap_entries | … | 20.52.6.45 |
| spswap_rows | … | 20.52.6.46 |
Functions that create sparse matrices.
| spband_matrix | … | 20.52.6.6 | spblock_matrix | … | 20.52.6.7 |
| spchar_matrix | … | 20.52.6.11 | spcoeff_matrix | … | 20.52.6.11 |
| spcompanion | … | 20.52.6.13 | sphessian | … | 20.52.6.22 |
| spjacobian | … | 20.52.6.23 | spjordan_block | … | 20.52.6.24 |
| spmake_identity | … | 20.52.6.26 |
| spchar_poly | … | 20.52.6.9 | spcholesky | … | 20.52.6.10 |
| spgram_schmidt | … | 20.52.6.20 | splu_decom | … | 20.52.6.25 |
| sppseudo_inverse | … | 20.52.6.34 | spsvd | … | 20.52.6.43 |
| matrixp | … | 20.52.6.28 | sparsematp | … | 20.52.6.39 |
| squarep | … | 20.52.6.40 | symmetricp | … | 20.52.6.47 |
In the examples the matrix 𝒜 will be
Unfortunately, due to restrictions of size, it is not practical to use “large” sparse matrices in the examples. As a result the examples shown may appear trivial, but they give an idea of how the functions work.
Throughout ℐ is used to indicate the identity matrix and 𝒜T to indicate the transpose of the matrix 𝒜.
spadd_columns(𝒜,c1,c2,expr);
| 𝒜 | :- | a sparse matrix. |
| c1,c2 | :- | positive integers. |
| expr | :- | a scalar expression. |
spadd_columns replaces column c2 of 𝒜 by
expr∗column(𝒜,c1) + column(𝒜,c2).
add_rows performs the equivalent task on the rows of 𝒜.
spadd_columns
spadd_rows
spadd_to_columns, spadd_to_rows, spmult_columns,
spmult_rows.
See: spadd_columns.
spadd_to_columns(𝒜,column_list,expr);
| 𝒜 | :- | a sparse matrix. |
| column_list | :- | a positive integer or a list of positive integers. |
| expr | :- | a scalar expression. |
spadd_to_columns adds expr to each column specified in column_list of
𝒜.
spadd_to_rows performs the equivalent task on the rows of 𝒜.
spadd_to_columns
spadd_to_rows
spadd_columns, spadd_rows, spmult_rows, spmult_columns.
See: spadd_to_columns.
spaugment_columns(𝒜,column_list);
| 𝒜 | :- | a sparse matrix. |
| column_list | :- | either a positive integer or a list of positive integers. |
spaugment_columns gets hold of the columns of 𝒜 specified in column_list
and sticks them together.
spstack_rows performs the same task on rows of 𝒜.
spaugment_columns
spstack_rows
spget_columns, spget_rows, spsub_matrix.
spband_matrix(expr_list,square_size);
| expr_list | :- | either a single scalar expression or a list of an odd number of scalar expressions. |
| square_size | :- | a positive integer. |
spband_matrix creates a sparse square matrix of dimension square_size.
spband_matrix
spdiagonal.
spblock_matrix(r,c,matrix_list);
| r,c | :- | positive integers. |
| matrix_list | :- | a list of matrices of either sparse or matrix type. |
spblock_matrix creates a sparse matrix that consists of r by c matrices filled
from the matrix_list row wise.

spblock_matrix
spchar_matrix(𝒜,λ);
| 𝒜 | :- | a square sparse matrix. |
| λ | :- | a symbol or algebraic expression. |
spchar_matrix creates the characteristic matrix 𝒞 of 𝒜.
This is 𝒞 = λ ∗ℐ−𝒜.
spchar_matrix
spchar_poly.
spchar_poly(𝒜,λ);
| 𝒜 | :- | a sparse square matrix. |
| λ | :- | a symbol or algebraic expression. |
spchar_poly finds the characteristic polynomial of 𝒜.
This is the determinant of λ ∗ℐ−𝒜.
spchar_poly(𝒜,x) = x3 − 15 ∗x2 − 59 ∗x − 45
spchar_matrix.
spcholesky(𝒜);
| 𝒜 | :- | a positive definite sparse matrix containing numeric entries. |
spcholesky computes the cholesky decomposition of 𝒜.
It returns {ℒ,𝒰} where ℒ is a lower matrix, 𝒰 is an upper matrix,
𝒜 = ℒ𝒰, and 𝒰 = ℒT .

cholesky
splu_decom.
spcoeff_matrix({lin_eqn1,lin_eqn2, …,lin_eqnn});
| lin_eqn1,lin_eqn2, …,lin_eqnn | :- | linear equations. Can be of the form equation = number or just equation which is equivalent to equation = 0. |
spcoeff_matrix creates the coefficient matrix 𝒞 of the linear equations.
It returns {𝒞,𝒳,ℬ} such that 𝒞𝒳 = ℬ.
spcoeff_matrix

column_dim(𝒜);
| 𝒜 | :- | a sparse matrix. |
spcol_dim finds the column dimension of 𝒜.
sprow_dim finds the row dimension of 𝒜.
spcol_dim(𝒜) = 3
spcompanion(poly,x);
| poly | :- | a monic univariate polynomial in x. |
| x | :- | the variable. |
spcompanion creates the companion matrix 𝒞 of poly.
This is the square matrix of dimension n, where n is the degree of poly w.r.t. x. The entries of 𝒞 are: 𝒞(i,n) = −coeffn(poly,x,i − 1) for i = 1…n, 𝒞(i,i − 1) = 1 for i = 2…n and the rest are 0.
spcompanion
spfind_companion.
spcopy_into(𝒜,ℬ,r,c);
| 𝒜,ℬ | :- | matrices of type sparse or matrix. |
| r,c | :- | positive integers. |
spcopy_into copies matrix 𝒜 into ℬ with 𝒜(1,1) at ℬ(r,c).

spcopy_into
spaugment_columns, spextend, spmatrix_augment, spmatrix_stack,
spstack_rows, spsub_matrix.
spdiagonal({mat1,mat2,
…,matn});45
| mat1,mat2, …,matn | :- | each can be either a scalar expr or a square matrix of sparse or matrix type. |
spdiagonal creates a sparse matrix that contains the input on the diagonal.

spdiagonal
spjordan_block.
spextend(𝒜,r,c,expr);
| 𝒜 | :- | a sparse matrix. |
| r,c | :- | positive integers. |
| expr | :- | algebraic expression or symbol. |
spextend returns a copy of 𝒜 that has been extended by r rows and c columns.
The new entries are made equal to expr.
spextend
spcopy_into, spmatrix_augment, spmatrix_stack,
spremove_columns, spremove_rows.
spfind_companion(𝒜,x);
| 𝒜 | :- | a sparse matrix. |
| x | :- | the variable. |
Given a sparse companion matrix, spfind_companion finds the polynomial
from which it was made.

spfind_companion
spcompanion.
spget_columns(𝒜,column_list);
| 𝒜 | :- | a sparse matrix. |
| c | :- | either a positive integer or a list of positive integers. |
spget_columns removes the columns of 𝒜 specified in column_list and returns
them as a list of column matrices.
spget_rows performs the same task on the rows of 𝒜.
spget_columns
spget_rows
spaugment_columns, spstack_rows, spsub_matrix.
spgram_schmidt({vec1,vec2, …,vecn});
| vec1,vec2, …,vecn | :- | linearly independent vectors. Each vector must be written as a list of predefined sparse (column) matrices, eg: sparse a(4,1);, a(1,1):=1; |
spgram_schmidt performs the gram_schmidt orthonormalisation on the input
vectors.
It returns a list of orthogonal normalised vectors.
Suppose a,b,c,d correspond to sparse matrices representing the following lists:
{{1,0,0,0},{1,1,0,0},{1,1,1,0},{1,1,1,1}}.
spgram_schmidt({{a},{b},{c},{d}}) =
{{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}}
sphermitian_tp(𝒜);
| 𝒜 | :- | a sparse matrix. |
sphermitian_tp computes the hermitian transpose of 𝒜.

sphermitian_tp
tp46 .
sphessian(expr,variable_list);
| expr | :- | a scalar expression. |
| variable_list | :- | either a single variable or a list of variables. |
sphessian computes the hessian matrix of expr w.r.t. the variables in
variable_list.
sphessian
spjacobian(expr_list,variable_list);
| expr_list | :- | either a single algebraic expression or a list of algebraic expressions. |
| variable_list | :- | either a single variable or a list of variables. |
spjacobian computes the jacobian matrix of expr_list w.r.t. variable_list.
spjacobian
sphessian, df47 .
spjordan_block(expr,square_size);
| expr | :- | an algebraic expression or symbol. |
| square_size | :- | a positive integer. |
spjordan_block computes the square jordan block matrix 𝒥 of dimension
square_size.
spjordan_block(x,5)
spdiagonal, spcompanion.
splu_decom(𝒜);
| 𝒜 | :- | a sparse matrix containing either numeric entries or imaginary entries with numeric coefficients. |
splu_decom performs LU decomposition on 𝒜, ie: it returns {ℒ,𝒰}
where ℒ is a lower diagonal matrix, 𝒰 an upper diagonal matrix and
𝒜 = ℒ𝒰.
Caution: The algorithm used can swap the rows of 𝒜 during the calculation. This means that ℒ𝒰 does not equal 𝒜 but a row equivalent of it. Due to this, splu_decom returns {ℒ,𝒰,vec}. The call spconvert(𝒜,vec) will return the sparse matrix that has been decomposed, ie: ℒ𝒰 = spconvert(𝒜,vec).
lu := splu_decom

spcholesky.
spmake_identity(square_size);
| square_size | :- | a positive integer. |
spmake_identity creates the identity matrix of dimension square_size.
spmake_identity
spdiagonal.
spmatrix_augment({mat1,mat2,
…,matn});48
| mat1,mat2, …,matn | :- | matrices. |
spmatrix_augment joins the matrices in matrix_list together horizontally.
spmatrix_stack joins the matrices in matrix_list together vertically.
spmatrix_augment
spmatrix_stack
spaugment_columns, spstack_rows, spsub_matrix.
matrixp(test_input);
| test_input | :- | anything you like. |
matrixp is a boolean function that returns t if the input is a matrix of type sparse
or matrix and nil otherwise.
matrixp(𝒜) = t
matrixp(doodlesackbanana) = nil
squarep, symmetricp, sparsematp.
spminor(𝒜,r,c);
| 𝒜 | :- | a sparse matrix. |
| r,c | :- | positive integers. |
spminor computes the (r,c)’th minor of 𝒜.
spminor
spremove_columns, spremove_rows.
spmult_columns(𝒜,column_list,expr);
| 𝒜 | :- | a sparse matrix. |
| column_list | :- | a positive integer or a list of positive integers. |
| expr | :- | an algebraic expression. |
spmult_columns returns a copy of 𝒜 in which the columns specified in
column_list have been multiplied by expr.
spmult_rows performs the same task on the rows of 𝒜.
spmult_columns
spmult_rows
spadd_to_columns, spadd_to_rows.
See: spmult_columns.
sppivot(𝒜,r,c);
| 𝒜 | :- | a sparse matrix. |
| r,c | :- | positive integers such that 𝒜(r,c) neq 0. |
sppivot pivots 𝒜 about it’s (r,c)’th entry.
To do this, multiples of the r’th row are added to every other row in the matrix.
This means that the c’th column will be 0 except for the (r,c)’th entry.
sprows_pivot.
sppseudo_inverse(𝒜);
| 𝒜 | :- | a sparse matrix containing only real numeric entries. |
sppseudo_inverse, also known as the Moore-Penrose inverse, computes the
pseudo inverse of 𝒜.
Given the singular value decomposition of 𝒜, i.e: 𝒜 = 𝒰Σ𝒱T , then the pseudo inverse 𝒜† is defined by 𝒜† = 𝒱Σ†𝒰T . For the diagonal matrix Σ, the pseudoinverse Σ† is computed by taking the reciprocal of only the nonzero diagonal elements.
If 𝒜 is square and non-singular, then 𝒜† = 𝒜. In general, however, 𝒜𝒜†𝒜 = 𝒜, and 𝒜†𝒜𝒜† = 𝒜†.
Perhaps more importantly, 𝒜† solves the following least-squares problem: given a rectangular matrix 𝒜 and a vector b, find the x minimizing ∥𝒜x −b∥2, and which, in addition, has minimum ℓ2 (euclidean) Norm, ∥x∥2. This x is 𝒜†b.

sppseudo_inverse
spsvd.
spremove_columns(𝒜,column_list);
| 𝒜 | :- | a sparse matrix. |
| column_list | :- | either a positive integer or a list of positive integers. |
spremove_columns removes the columns specified in column_list from
𝒜.
spremove_rows performs the same task on the rows of 𝒜.
spremove_columns
spremove_rows
spminor.
See: spremove_columns.
See: spcolumn_dim.
sprows_pivot(𝒜,r,c,{row_list});
| 𝒜 | :- | a sparse matrix. |
| r,c | :- | positive integers such that 𝒜(r,c) neq 0. |
| row_list | :- | positive integer or a list of positive integers. |
sprows_pivot performs the same task as sppivot but applies the pivot only
to the rows specified in row_list.
sppivot.
sparsematp(𝒜);
| 𝒜 | :- | a matrix. |
sparsematp is a boolean function that returns t if the matrix is declared sparse
and nil otherwise.

sparsematp
sparsematp
matrixp, symmetricp, squarep.
squarep(𝒜);
| 𝒜 | :- | a matrix. |
squarep is a boolean function that returns t if the matrix is square and nil
otherwise.

squarep
squarep
matrixp, symmetricp, sparsematp.
See: spaugment_columns.
spsub_matrix(𝒜,row_list,column_list);
| 𝒜 | :- | a sparse matrix. |
| row_list, column_list | :- | either a positive integer or a list of positive integers. |
spsub_matrix produces the matrix consisting of the intersection of the rows
specified in row_list and the columns specified in column_list.
spsub_matrix
spaugment_columns, spstack_rows.
spsvd(𝒜);
| 𝒜 | :- | a sparse matrix containing only real numeric entries. |
spsvd computes the singular value decomposition of 𝒜.
If A is an m ×n real matrix of (column) rank r, svd returns the 3-element list {𝒰,Σ,𝒱} where 𝒜 = 𝒰Σ𝒱T .
Let k = min(m,n). Then U is m ×k, V is n ×k, and and Σ = diag(σ1,…,σk), where σi ≥ 0 are the singular values of 𝒜; only r of these are non-zero. The singular values are the non-negative square roots of the eigenvalues of 𝒜T 𝒜.
𝒰 and 𝒱 are such that 𝒰𝒰T = 𝒱𝒱T = 𝒱T 𝒱 = ℐk.
Note: there are a number of different definitions of SVD in the literature, in some of which Σ is square and U and V rectangular, as here, but in others U and V are square, and Σ is rectangular.


spswap_columns(𝒜,c1,c2);
| 𝒜 | :- | a sparse matrix. |
| c1,c1 | :- | positive integers. |
spswap_columns swaps column c1 of 𝒜 with column c2.
spswap_rows performs the same task on 2 rows of 𝒜.
spswap_columns
spswap_entries.
spswap_entries(𝒜,{r1,c1},{r2,c2});
| 𝒜 | :- | a sparse matrix. |
| r1,c1,r2,c2 | :- | positive integers. |
spswap_entries swaps 𝒜(r1,c1) with 𝒜(r2,c2).
spswap_entries
spswap_columns, spswap_rows.
symmetricp(𝒜);
| 𝒜 | :- | a matrix. |
symmetricp is a boolean function that returns t if the matrix is symmetric and
nil otherwise.

symmetricp
symmetricp
matrixp, squarep, sparsematp.
By turning the fast_la switch on, the speed of the following functions will be increased:
| spadd_columns | spadd_rows | spaugment_columns | spcol_dim |
| spcopy_into | spmake_identity | spmatrix_augment | spmatrix_stack |
| spminor | spmult_column | spmult_row | sppivot |
| spremove_columns | spremove_rows | sprows_pivot | squarep |
| spstack_rows | spsub_matrix | spswap_columns | spswap_entries |
| spswap_rows | symmetricp |
The increase in speed will be insignificant unless you are making a significant number(i.e: thousands) of calls. When using this switch, error checking is minimised. This means that illegal input may give strange error messages. Beware.
This package is an extention of the code from the Linear Algebra Package for REDUCE by Matt Rebbeck (cf. section 20.32).
The algorithms for spcholesky, splu_decom, and spsvd are taken from the book Linear Algebra – J.H. Wilkinson & C. Reinsch[3].
The spgram_schmidt code comes from Karin Gatermann’s Symmetry package[4] for REDUCE.
| Up | Next | Prev | PrevTail | Front |