REDUCE

16.60 SPARSE: Sparse Matrix Calculations

Author: Stephen Scowcroft

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

Loading the Package

The package is loaded by: load_package sparse;

16.60.2 Sparse Matrix Calculations

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

16.60.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 in The REDUCE User’s Manual[2].

16.60.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.60.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.60.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 in The REDUCE User’s Manual[2].

16.60.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 in The REDUCE User’s Manual[2] for more details.

16.60.4.1 Examples

In the examples the matrix AA will be

AA = (             )
   1  0  0  0
||  0  3  0  0 ||
(  0  0  5  0 )
   0  0  0  9

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.60.5 The Linear Algebra Package for Sparse Matrices

This package is an extension of the Linear Algebra Package for REDUCE.[1] These functions are described alphabetically in section 6 of this document and are labelled 6.1 to 6.47. They can be classified into four sections(n.b: the numbers after the dots signify the function label in section 6).

16.60.5.1 Basic matrix handling

spadd_columns 6.1 spadd_rows 6.2
spadd_to_columns 6.3 spadd_to_rows 6.4
spaugment_columns6.5 spchar_poly 6.9
spcol_dim 6.12spcopy_into 6.14
spdiagonal 6.15spextend 6.16
spfind_companion 6.17spget_columns 6.18
spget_rows 6.19sphermitian_tp 6.21
spmatrix_augment 6.27spmatrix_stack 6.29
spminor 6.30spmult_columns6.31
spmult_rows 6.32sppivot 6.33
spremove_columns 6.35spremove_rows 6.36
sprow_dim 6.37sprows_pivot 6.38
spstack_rows 6.41spsub_matrix 6.42
spswap_columns 6.44spswap_entries 6.45
spswap_rows 6.46

16.60.5.2 Constructors

Functions that create sparse matrices.

spband_matrix 6. 6 spblock_matrix6. 7
spchar_matrix 6. 8 spcoeff_matrix 6. 11
spcompanion 6. 13sphessian 6. 22
spjacobian 6. 23spjordan_block6. 24
spmake_identity6. 26

16.60.5.3 High level algorithms

spchar_poly 6.9 spcholesky 6.10
spgram_schmidt 6.20splu_decom6.25
sppseudo_inverse6.34svd 6.43

16.60.5.4 Predicates

matrixp6.28sparsematp 6.39
squarep 6.40symmetricp6.47

Note on examples:

In the examples the matrix A will be

A = (          )
   1  0  0
(  0  5  0 )
   0  0  9

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 I is used to indicate the identity matrix and AT to indicate the transpose of the matrix A.

16.60.6 Available Functions

16.60.6.1 spadd_columns, spadd_rows

  spadd_columns(A,c1,c2,expr);

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

Synopsis:

spadd_columns replaces column c2 of A by expr column(A,c1) + column(A,c2).

spadd_rows performs the equivalent task on the rows of A.

Examples:

spadd_columns(A, 1, 2,x)
=
(          )
   1  x  0
(  0  5  0 )
   0  0  9

spadd_rows(A, 2, 3, 5)
=
(            )
   1   0  0
(  0   5  0  )
   0  25  9

Related functions:

  spadd_to_columns, spadd_to_rows, spmult_columns, spmult_rows.

16.60.6.2 spadd_rows

  see: spadd_columns.

16.60.6.3 spadd_to_columns, spadd_to_rows

  spadd_to_columns(A,column_list,expr);

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

Synopsis:

spadd_to_columns adds expr to each column specified in column_list of A.

spadd_to_rows performs the equivalent task on the rows of A.

Examples:

 

spadd_to_columns(A,{1, 2}, 10)
=
(            )
   11  10  0
(  10  15  0 )
   10  10  9

 

spadd_to_rows(A, 2,x)
=
(                    )
    1      0      0
(  − x  − x + 5  − x )
    0      0      9

Related functions:

  spadd_columns, spadd_rows, spmult_rows, spmult_columns.

16.60.6.4 spadd_to_rows

  see: spadd_to_columns.

16.60.6.5 spaugment_columns, spstack_rows

  spaugment_columns(A,column_list);

A :-a sparse matrix.
column_list:-either a positive integer or a list of positive integers.

Synopsis:

spaugment_columns gets hold of the columns of A specified in column_list and sticks them together.

spstack_rows performs the same task on rows of A.

Examples:

spaugment_columns(A,{1, 2})
=
(       )
   1  0
(  0  5 )
   0  0

spstack_rows(A,{1, 3})
=
(          )
   1  0  0
   0  0  9

Related functions:

  spget_columns, spget_rows, spsub_matrix.

16.60.6.6 spband_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.

Synopsis:

spband_matrix creates a sparse square matrix of dimension square_size.

Examples:

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

Related functions:

  spdiagonal.

16.60.6.7 spblock_matrix

  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:

B = (       )
   1  0
   0  1,
C = (    )
   5
   0,
D = (        )
   22  0
   0   0

spblock_matrix(2, 3,{B,C,D,D,C,B})
=
(  1   0  5  22  0 )
|                  |
|  0   1  0  0   0 |
(  22  0  5  1   0 )
   0   0  0  0   1

16.60.6.8 spchar_matrix

  spchar_matrix(A);

A:-a square sparse matrix.
λ :-a symbol or algebraic expression.

Synopsis:

spchar_matrix creates the characteristic matrix C of A.

This is C = λ IA.

Examples:

spchar_matrix(A,x)
=
(                       )
   x − 1    0       0
(    0    x − 5     0   )
     0      0    x −  9

Related functions:

  spchar_poly.

16.60.6.9 spchar_poly

  spchar_poly(A);

A:-a sparse square matrix.
λ :-a symbol or algebraic expression.

Synopsis:

spchar_poly finds the characteristic polynomial of A.

This is the determinant of λ IA.

Examples:

  spchar_poly(A,x) = x3 15 x2 59 x 45

Related functions:

  spchar_matrix.

16.60.6.10 spcholesky

  spcholesky(A);

A:-a positive definite sparse matrix containing numeric entries.

Synopsis:

spcholesky computes the cholesky decomposition of A.

It returns {L,U} where L is a lower matrix, U is an upper matrix,
A = LU, and U = LT .

Examples:

  F = (          )
   1  0  0
(  0  5  0 )
   0  0  9

cholesky(F)
=
(                                 )
{ (  1   0   0 )   (  1   0   0 ) }
  (     √ --   )   (     √ --   )
(    0    5  0    ,   0    5  0   )
     0   0   3        0   0   3

Related functions:

  splu_decom.

16.60.6.11 spcoeff_matrix

  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.

Synopsis:

spcoeff_matrix creates the coefficient matrix C of the linear equations.

It returns {C,X,B} such that CX = B.

Examples:

  spcoeff_matrix({y20w = 10,yz = 20,y +4+3z,w +x+50}) =

  ( (                  )   (    )  (      ) )
||    1  − 20   0   0        y        10   ||
{ |  1    0   − 1  0 |   | w  |  |   20 | }
  |(                  |)  ,|(    |) ,|(      |)
||(    1    0    3   0        z       − 4   ||)
     0    1    0   1        x        50

16.60.6.12 spcol_dim, sprow_dim

  column_dim(A);

A:-a sparse matrix.

Synopsis:

  spcol_dim finds the column dimension of A.

  sprow_dim finds the row dimension of A.

Examples:

  spcol_dim(A) = 3

16.60.6.13 spcompanion

  spcompanion(poly,x);

poly:-a monic univariate polynomial in x.
x :-the variable.

Synopsis:

spcompanion creates the companion matrix C of poly.

This is the square matrix of dimension n, where n is the degree of poly w.r.t. x.

The entries of C are: C(i,n) = -coeffn(poly,x,i-1) for i = 1 …n, C(i,i-1) = 1 for i = 2 …n and the rest are 0.

Examples:

spcompanion(x4 + 17 x3 9 x2 + 11,x)
=
(                )
   0  0  0   − 11
||  1  0  0    0  ||
(  0  1  0    9  )
   0  0  1   − 17

Related functions:

  spfind_companion.

16.60.6.14 spcopy_into

  spcopy_into(A,B,r,c);

A,B:-matrices of type sparse or matrix.
r,c :-positive integers.

Synopsis:

  spcopy_into copies matrix A into B with A(1,1) at B(r,c).

Examples:

  G = (  0  0  0  0 )
|             |
|  0  0  0  0 |
(  0  0  0  0 )
   0  0  0  0

spcopy_into(A,G, 1, 2)
=
(  0  1  0  0 )
|             |
|  0  0  5  0 |
(  0  0  0  9 )
   0  0  0  0

Related functions:

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

16.60.6.15 spdiagonal

  spdiagonal({mat1,mat2, …,matn});29

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:

  H = (         )
   66  77
   88  99

spdiagonal({A,x,H})
=
(                      )
   1  0  0   0  0   0
|  0  5  0   0  0   0  |
||  0  0  9   0  0   0  ||
|  0  0  0  x   0   0  |
|(                      |)
   0  0  0   0  66  77
   0  0  0   0  88  99

Related functions:

  spjordan_block.

16.60.6.16 spextend

  spextend(A,r,c,expr);

A :-a sparse matrix.
r,c :-positive integers.
expr:-algebraic expression or symbol.

Synopsis:

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

Examples:

spextend(A, 1, 2, 0)
=
(  1  0  0  0  0 )
|                |
|  0  5  0  0  0 |
(  0  0  9  0  0 )
   0  0  0  0  0

Related functions:

spcopy_into, spmatrix_augment, spmatrix_stack, spremove_columns, spremove_rows.

16.60.6.17 spfind_companion

  spfind_companion(A,x);

A:-a sparse matrix.
x :-the variable.

Synopsis:

Given a sparse companion matrix, spfind_companion finds the polynomial from which it was made.

Examples:

  C = (  0  0  0  − 11 )
|                |
|  1  0  0   0   |
(  0  1  0   9   )
   0  0  1  − 17

  spfind_companion(C,x) = x4 + 17 x3 9 x2 + 11

Related functions:

  spcompanion.

16.60.6.18 spget_columns, spget_rows

  spget_columns(A,column_list);

A:-a sparse matrix.
c :-either a positive integer or a list of positive integers.

Synopsis:

spget_columns removes the columns of A specified in column_list and returns them as a list of column matrices.

  spget_rows performs the same task on the rows of A.

Examples:

spget_columns(A,{1, 3})
=
( (    )  (    ) )
{    1       0   }
  (  0 ) ,(  0 )
(    0       9   )

spget_rows(A, 2)
=
{(0   5  0  )}

Related functions:

  spaugment_columns, spstack_rows, spsub_matrix.

16.60.6.19 spget_rows

  see: spget_columns.

16.60.6.20 spgram_schmidt

  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.60.6.21 sphermitian_tp

  sphermitian_tp(A);

A:-a sparse matrix.

Synopsis:

sphermitian_tp computes the hermitian transpose of A.

Examples:

  J = (                     )
   i + 1  i + 2 i + 3
(    0      0     0   )
     0      i     0

sphermitian_tp(J )
=
(                )
   − i + 1 0   0
(  − i + 2 0  − i)
   − i + 3 0   0

Related functions:

  tp30 .

16.60.6.22 sphessian

  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})
=
(              )
   0  0  0  0
|  0  2  z  y  |
|(  0  z  0  x  |)

   0  y  x  0

16.60.6.23 spjacobian

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

  (          3                        )
   0  4 ∗2x      0           0
(  0    y     2 ∗ x ∗ y      0      )
   0  y ∗ z3   x ∗ z3  3 ∗ x ∗ y ∗ z2

Related functions:

  sphessian, df31 .

16.60.6.24 spjordan_block

  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 J of dimension square_size.

Examples:

spjordan_block(x,5)
=
(                 )
   x  1  0  0  0
||  0  x  1  0  0  ||
|  0  0  x  1  0  |
|(  0  0  0  x  1  |)

   0  0  0  0  x

Related functions:

  spdiagonal, spcompanion.

16.60.6.25 splu_decom

  splu_decom(A);

A:-a sparse matrix containing either numeric entries or imaginary entries with numeric coefficients.

Synopsis:

splu_decom performs LU decomposition on A, ie: it returns {L,U} where L is a lower diagonal matrix, U an upper diagonal matrix and A = LU.

caution:

The algorithm used can swap the rows of A during the calculation. This means that LU does not equal A but a row equivalent of it. Due to this, splu_decom returns {L,U,vec}. The call spconvert(A,vec) will return the sparse matrix that has been decomposed, ie: LU = spconvert(A,vec).

Examples:

  K = (          )
   1  0  0
(  0  5  0 )
   0  0  9

lu := splu_decom(K)
=
( (          )  (          )       )
{    1  0  0       1  0  0         }
  (  0  5  0 ) ,(  0  1  0 )  ,[123 ]
(    0  0  9       0  0  1         )

first lu * second lu
=
(          )
   1  0  0
(  0  5  0 )
   0  0  9

convert(K,third lu)
=
(          )
   1  0  0
(  0  5  0 )

   0  0  9

Related functions:

  spcholesky.

16.60.6.26 spmake_identity

  spmake_identity(square_size);

square_size:-a positive integer.

Synopsis:

  spmake_identity creates the identity matrix of dimension square_size.

Examples:

spmake_identity(4)
=
(             )
   1  0  0  0
||  0  1  0  0 ||
(  0  0  1  0 )
   0  0  0  1

Related functions:

  spdiagonal.

16.60.6.27 spmatrix_augment, spmatrix_stack

  spmatrix_augment({mat1,mat2, …,matn});32

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({A,A})
=
(                   )
   1  0  0  1  0  0
(  0  5  0  0  5  0 )

   0  0  9  0  0  9

spmatrix_stack({A,A})
=
(          )
   1  0  0
|  0  5  0 |
||  0  0  9 ||
|          |
|(  1  0  0 |)
   0  5  0
   0  0  9

Related functions:

  spaugment_columns, spstack_rows, spsub_matrix.

16.60.6.28 matrixp

  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(A) = t

  matrixp(doodlesackbanana) = nil

Related functions:

  squarep, symmetricp, sparsematp.

16.60.6.29 spmatrix_stack

  see: spmatrix_augment.

16.60.6.30 spminor

  spminor(A,r,c);

A:-a sparse matrix.
r,c:-positive integers.

Synopsis:

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

Examples:

spminor(A, 1, 3)
=
(       )
   0  5
   0  0

Related functions:

  spremove_columns, spremove_rows.

16.60.6.31 spmult_columns, spmult_rows

  spmult_columns(A,column_list,expr);

A :-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 A in which the columns specified in column_list have been multiplied by expr.

spmult_rows performs the same task on the rows of A.

Examples:

spmult_columns(A,{1, 3},x)
=
(              )
   x  0    0
(  0  5    0   )
   0  0  9 ∗ x

spmult_rows(A, 2, 10)
=
(           )
   1  0   0
(  0  50  0 )
   0  0   9

Related functions:

  spadd_to_columns, spadd_to_rows.