Up Next Prev PrevTail Tail

### 16.62 SPARSE: Sparse Matrix Calculations

Author: Stephen Scowcroft

#### 16.62.1 Introduction

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.

#### 16.62.2 Sparse Matrix Calculations

To extend the the syntax to this class of calculations we need to add an expression type sparse.

##### 16.62.2.1 Sparse Variables

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).

##### 16.62.2.2 Assigning Sparse Matrix Elements

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

~The matrix is dense, contains only zeros~

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.

##### 16.62.2.3 Evaluating Sparse Matrix Elements

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.

#### 16.62.3 Sparse Matrix Expressions

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).

#### 16.62.4 Operators with Sparse Matrix Arguments

The operators in the Sparse Matix Package are the same as those in the Matrix Packge with the exception that the NULLSPACE operator is not defined. See section Operators with Matrix Arguments (14.4) for more details.

##### 16.62.4.1 Examples

In the examples the matrix will be

=

det ppp;

135

trace ppp;

18

rank ppp;

4

spmateigen(ppp,eta);

{{eta - 1,1,

spm(1,1) := arbcomplex(4)\$
},

{eta - 3,1,

spm(2,1) := arbcomplex(5)\$
},

{eta - 5,1,

spm(3,1) := arbcomplex(6)\$
},

{eta - 9,1,

spm(4,1) := arbcomplex(7)\$
}}

#### 16.62.5 The Linear Algebra Package for Sparse Matrices

This package is an extension of the Linear Algebra Package for REDUCE described in section 16.37. These functions are described alphabetically in section 16.62.6. They can be classified into four sections(n.b: the numbers after the dots signify the function label in section 6).

##### 16.62.5.1 Basic matrix handling

 spadd_columns … 16.62.6.1 spadd_rows … 16.62.6.2 spadd_to_columns … 16.62.6.3 spadd_to_rows … 16.62.6.4 spaugment_columns … 16.62.6.5 spchar_poly … 16.62.6.9 spcol_dim … 16.62.6.12 spcopy_into … 16.62.6.14 spdiagonal … 16.62.6.15 spextend … 16.62.6.16 spfind_companion … 16.62.6.17 spget_columns … 16.62.6.18 spget_rows … 16.62.6.19 sphermitian_tp … 16.62.6.21 spmatrix_augment … 16.62.6.27 spmatrix_stack … 16.62.6.29 spminor … 16.62.6.30 spmult_columns … 16.62.6.31 spmult_rows … 16.62.6.32 sppivot … 16.62.6.33 spremove_columns … 16.62.6.35 spremove_rows … 16.62.6.36 sprow_dim … 16.62.6.37 sprows_pivot … 16.62.6.38 spstack_rows … 16.62.6.41 spsub_matrix … 16.62.6.42 spswap_columns … 16.62.6.44 spswap_entries … 16.62.6.45 spswap_rows … 16.62.6.46

##### 16.62.5.2 Constructors

Functions that create sparse matrices.

 spband_matrix … 16.62.6.6 spblock_matrix … 16.62.6.7 spchar_matrix … 16.62.6.11 spcoeff_matrix … 16.62.6.11 spcompanion … 16.62.6.13 sphessian … 16.62.6.22 spjacobian … 16.62.6.23 spjordan_block … 16.62.6.24 spmake_identity … 16.62.6.26

##### 16.62.5.3 High level algorithms

 spchar_poly … 16.62.6.9 spcholesky … 16.62.6.10 spgram_schmidt … 16.62.6.20 splu_decom … 16.62.6.25 sppseudo_inverse … 16.62.6.34 spsvd … 16.62.6.43

##### 16.62.5.4 Predicates

 matrixp … 16.62.6.28 sparsematp … 16.62.6.39 squarep … 16.62.6.40 symmetricp … 16.62.6.47

##### Note on examples:

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.

##### Notation

Throughout is used to indicate the identity matrix and T to indicate the transpose of the matrix .

#### 16.62.6 Available Functions

Syntax:

 :- a sparse matrix. c1,c2 :- positive integers. expr :- a scalar expression.
Synopsis:

spadd_columns replaces column c2 of by
expr*column(,c1) + column(,c2).
Examples:
Related functions:

Syntax:

 :- a sparse matrix. column_list :- a positive integer or a list of positive integers. expr :- a scalar expression.
Synopsis:

Examples:
Related functions:

##### 16.62.6.5 spaugment_columns, spstack_rows
Syntax:

spaugment_columns(,column_list);
 :- a sparse matrix. column_list :- either a positive integer or a list of positive integers.
Synopsis:

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 .

Examples:
 spaugment_columns(,{1, 2}) = spstack_rows(,{1, 3}) =
Related functions:

spget_columns, spget_rows, spsub_matrix.

##### 16.62.6.6 spband_matrix
Syntax:

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.
Synopsis:

spband_matrix creates a sparse square matrix of dimension square_size.
Examples:

spband_matrix({x,y,z}, 6) =

Related functions:

spdiagonal.

##### 16.62.6.7 spblock_matrix
Syntax:

spblock_matrix(r,c,matrix_list);
 r,c :- positive integers. matrix_list :- a list of matrices of either sparse or matrix type.
Synopsis:

spblock_matrix creates a sparse matrix that consists of r by c matrices filled from the matrix_list row wise.
Examples:
 = , = , = spblock_matrix(2, 3,{,,,,,}) =

##### 16.62.6.8 spchar_matrix
Syntax:

spchar_matrix();
 :- a square sparse matrix. λ :- a symbol or algebraic expression.
Synopsis:

spchar_matrix creates the characteristic matrix of .

This is = λ *-.

Examples:

spchar_matrix(,x) =

Related functions:

spchar_poly.

##### 16.62.6.9 spchar_poly
Syntax:

spchar_poly();
 :- a sparse square matrix. λ :- a symbol or algebraic expression.
Synopsis:

spchar_poly finds the characteristic polynomial of .

This is the determinant of λ *-.

Examples:

spchar_poly(,x) = x3 - 15 * x2 - 59 * x - 45
Related functions:

spchar_matrix.

##### 16.62.6.10 spcholesky
Syntax:

spcholesky();
 :- a positive definite sparse matrix containing numeric entries.
Synopsis:

spcholesky computes the cholesky decomposition of .

It returns {,} where is a lower matrix, is an upper matrix,
= , and = T .

Examples:

 = cholesky() =
Related functions:

splu_decom.

##### 16.62.6.11 spcoeff_matrix
Syntax:

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.
Synopsis:

spcoeff_matrix creates the coefficient matrix of the linear equations.

It returns {,,} such that = .

Examples:
 spcoeff_matrix({y - 20 * w = 10,y - z = 20,y + 4 + 3 * z,w + x + 50}) =

##### 16.62.6.12 spcol_dim, sprow_dim
Syntax:

column_dim();
 :- a sparse matrix.
Synopsis:

spcol_dim finds the column dimension of .
sprow_dim finds the row dimension of .
Examples:

spcol_dim() = 3

##### 16.62.6.13 spcompanion
Syntax:

spcompanion(poly,x);
 poly :- a monic univariate polynomial in x. x :- the variable.
Synopsis:

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 = 1n, (i,i - 1) = 1 for i = 2n and the rest are 0.

Examples:

spcompanion(x4 + 17 * x3 - 9 * x2 + 11,x) =

Related functions:

spfind_companion.

##### 16.62.6.14 spcopy_into
Syntax:

spcopy_into(,,r,c);
 , :- matrices of type sparse or matrix. r,c :- positive integers.
Synopsis:

spcopy_into copies matrix into with (1,1) at (r,c).
Examples:

 = spcopy_into(,, 1, 2) =
Related functions:

spaugment_columns, spextend, spmatrix_augment, spmatrix_stack, spstack_rows, spsub_matrix.

##### 16.62.6.15 spdiagonal
Syntax:

spdiagonal({mat1,mat2, …,matn});32
 mat1,mat2, …,matn :- each can be either a scalar expr or a square matrix of sparse or matrix type.
Synopsis:

spdiagonal creates a sparse matrix that contains the input on the diagonal.
Examples:

 = spdiagonal({,x,}) =
Related functions:

spjordan_block.

##### 16.62.6.16 spextend
Syntax:

spextend(,r,c,expr);
 :- a sparse matrix. r,c :- positive integers. expr :- algebraic expression or symbol.
Synopsis:

spextend returns a copy of that has been extended by r rows and c columns. The new entries are made equal to expr.
Examples:

spextend(, 1, 2, 0) =

Related functions:

spcopy_into, spmatrix_augment, spmatrix_stack, spremove_columns, spremove_rows.

##### 16.62.6.17 spfind_companion
Syntax:

spfind_companion(,x);
 :- a sparse matrix. x :- the variable.
Synopsis:

Given a sparse companion matrix, spfind_companion finds the polynomial from which it was made.
Examples:
 = spfind_companion(,x) = x4 + 17 * x3 - 9 * x2 + 11
Related functions:

spcompanion.

##### 16.62.6.18 spget_columns, spget_rows
Syntax:

spget_columns(,column_list);
 :- a sparse matrix. c :- either a positive integer or a list of positive integers.
Synopsis:

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 .

Examples:
 spget_columns(,{1, 3}) = spget_rows(, 2) =
Related functions:

spaugment_columns, spstack_rows, spsub_matrix.

##### 16.62.6.19 spget_rows

See: spget_columns.

##### 16.62.6.20 spgram_schmidt
Syntax:

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;
Synopsis:

spgram_schmidt performs the gram_schmidt orthonormalisation on the input vectors.

It returns a list of orthogonal normalised vectors.

Examples:

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}}

##### 16.62.6.21 sphermitian_tp
Syntax:

sphermitian_tp();
 :- a sparse matrix.
Synopsis:

sphermitian_tp computes the hermitian transpose of .
Examples:

 = sphermitian_tp( ) =
Related functions:

tp33 .

##### 16.62.6.22 sphessian
Syntax:

sphessian(expr,variable_list);
 expr :- a scalar expression. variable_list :- either a single variable or a list of variables.
Synopsis:

sphessian computes the hessian matrix of expr w.r.t. the variables in variable_list.
Examples:

sphessian(x * y * z + x2,{w,x,y,z}) =

##### 16.62.6.23 spjacobian
Syntax:

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.
Synopsis:

spjacobian computes the jacobian matrix of expr_list w.r.t. variable_list.
Examples:

 spjacobian({x4,x * y2,x * y * z3},{w,x,y,z}) =
Related functions:

sphessian, df34 .

##### 16.62.6.24 spjordan_block
Syntax:

spjordan_block(expr,square_size);
 expr :- an algebraic expression or symbol. square_size :- a positive integer.
Synopsis:

spjordan_block computes the square jordan block matrix of dimension square_size.
Examples:

spjordan_block(x,5) =

Related functions:

spdiagonal, spcompanion.

##### 16.62.6.25 splu_decom
Syntax:

splu_decom();
 :- a sparse matrix containing either numeric entries or imaginary entries with numeric coefficients.
Synopsis:

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).

Examples:

=

 lu := splu_decom() =

 first lu * second lu = convert(,third lu) =
Related functions:

spcholesky.

##### 16.62.6.26 spmake_identity
Syntax:

spmake_identity(square_size);
 square_size :- a positive integer.
Synopsis:

spmake_identity creates the identity matrix of dimension square_size.
Examples:

spmake_identity(4) =

Related functions:

spdiagonal.

##### 16.62.6.27 spmatrix_augment, spmatrix_stack
Syntax:

spmatrix_augment({mat1,mat2, …,matn});35
 mat1,mat2, …,matn :- matrices.
Synopsis:

spmatrix_augment joins the matrices in matrix_list together horizontally.

spmatrix_stack joins the matrices in matrix_list together vertically.

Examples:

 spmatrix_augment({,}) = spmatrix_stack({,}) =
Related functions:

spaugment_columns, spstack_rows, spsub_matrix.

##### 16.62.6.28 matrixp
Syntax:

matrixp(test_input);
 test_input :- anything you like.
Synopsis:

matrixp is a boolean function that returns t if the input is a matrix of type sparse or matrix and nil otherwise.
Examples:

matrixp() = t

matrixp(doodlesackbanana) = nil

Related functions:

squarep, symmetricp, sparsematp.

##### 16.62.6.29 spmatrix_stack

See: spmatrix_augment.

##### 16.62.6.30 spminor
Syntax:

spminor(,r,c);
 :- a sparse matrix. r,c :- positive integers.
Synopsis:

spminor computes the (r,c)’th minor of .
Examples:

spminor(, 1, 3) =

Related functions:

spremove_columns, spremove_rows.

##### 16.62.6.31 spmult_columns, spmult_rows
Syntax:

spmult_columns(,column_list,expr);
 :- a sparse matrix. column_list :- a positive integer or a list of positive integers. expr :- an algebraic expression.
Synopsis:

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 .

Examples:

 spmult_columns(,{1, 3},x) = spmult_rows(, 2, 10) =
Related functions:

##### 16.62.6.32 spmult_rows

See: spmult_columns.

##### 16.62.6.33 sppivot
Syntax:

sppivot(,r,c);
 :- a sparse matrix. r,c :- positive integers such that (r,c) neq 0.
Synopsis:

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.

Related functions:

sprows_pivot.

##### 16.62.6.34 sppseudo_inverse
Syntax:

sppseudo_inverse();
 :- a sparse matrix containing only real numeric entries.
Synopsis:

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 - b2, and which, in addition, has minimum 2 (euclidean) Norm, x2. This x is b.

Examples:

 = sppseudo_inverse() =
Related functions:

spsvd.

##### 16.62.6.35 spremove_columns, spremove_rows
Syntax:

spremove_columns(,column_list);
 :- a sparse matrix. column_list :- either a positive integer or a list of positive integers.
Synopsis:

spremove_columns removes the columns specified in column_list from .

spremove_rows performs the same task on the rows of .

Examples:

 spremove_columns(, 2) = spremove_rows(,{1, 3}) =
Related functions:

spminor.

##### 16.62.6.36 spremove_rows

See: spremove_columns.

##### 16.62.6.37 sprow_dim

See: spcolumn_dim.

##### 16.62.6.38 sprows_pivot
Syntax:

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.
Synopsis:

sprows_pivot performs the same task as sppivot but applies the pivot only to the rows specified in row_list.
Related functions:

sppivot.

##### 16.62.6.39 sparsematp
Syntax:

sparsematp();
 :- a matrix.
Synopsis:

sparsematp is a boolean function that returns t if the matrix is declared sparse and nil otherwise.
Examples:

 := mat((1,2,3),(4,5,6),(7,8,9)); sparsematp() = t sparsematp() = nil
Related functions:

matrixp, symmetricp, squarep.

##### 16.62.6.40 squarep
Syntax:

squarep();
 :- a matrix.
Synopsis:

squarep is a boolean function that returns t if the matrix is square and nil otherwise.
Examples:

 = squarep() = t squarep() = nil
Related functions:

matrixp, symmetricp, sparsematp.

##### 16.62.6.41 spstack_rows

See: spaugment_columns.

##### 16.62.6.42 spsub_matrix
Syntax:

spsub_matrix(,row_list,column_list);
 :- a sparse matrix. row_list, column_list :- either a positive integer or a list of positive integers.
Synopsis:

spsub_matrix produces the matrix consisting of the intersection of the rows specified in row_list and the columns specified in column_list.
Examples:

spsub_matrix(,{1, 3},{2, 3}) =

Related functions:

spaugment_columns, spstack_rows.

##### 16.62.6.43 spsvd (singular value decomposition)
Syntax:

spsvd();
 :- a sparse matrix containing only real numeric entries.
Synopsis:

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.

Examples:

 = svd() =

##### 16.62.6.44 spswap_columns, spswap_rows
Syntax:

spswap_columns(,c1,c2);
 :- a sparse matrix. c1,c1 :- positive integers.
Synopsis:

spswap_columns swaps column c1 of with column c2.

spswap_rows performs the same task on 2 rows of .

Examples:

spswap_columns(, 2, 3) =

Related functions:

spswap_entries.

##### 16.62.6.45 swap_entries
Syntax:

spswap_entries(,{r1,c1},{r2,c2});
 :- a sparse matrix. r1,c1,r2,c2 :- positive integers.
Synopsis:

spswap_entries swaps (r1,c1) with (r2,c2).
Examples:

spswap_entries(,{1, 1},{3, 3}) =

Related functions:

spswap_columns, spswap_rows.

##### 16.62.6.46 spswap_rows

See: spswap_columns.

##### 16.62.6.47 symmetricp
Syntax:

symmetricp();
 :- a matrix.
Synopsis:

symmetricp is a boolean function that returns t if the matrix is symmetric and nil otherwise.
Examples:

 = symmetricp() = nil symmetricp() = t
Related functions:

matrixp, squarep, sparsematp.

#### 16.62.7 Fast Linear Algebra

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.

#### 16.62.8 Acknowledgments

This package is an extention of the code from the Linear Algebra Package for REDUCE by Matt Rebbeck (cf. section 16.37).

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.

#### Bibliography

[1]   Matt Rebbeck: A Linear Algebra Package for REDUCE, ZIB , Berlin. (1994)

[2]   Anthony C. Hearn: REDUCE User’s Manual 3.6. RAND (1995)

[3]   J. H. Wilkinson & C. Reinsch: Linear Algebra (volume II). Springer-Verlag (1971)

[4]   Karin Gatermann: Symmetry: A REDUCE package for the computation of linear representations of groups. ZIB, Berlin. (1992)

 Up Next Prev PrevTail Front