API

check

template<typename T>
void castor::check(smatrix<T> &As)

Check sparse matrix data.

check(As) removes all zeros values and associated indices and verify if linear indices are well sorted in ascending order.

smatrix<> Xs(range(0,12),eye(1,12),3,4);
disp(Xs);
check(Xs);
disp(Xs);

See Class matrix.

clear

template<typename T>
void castor::clear(smatrix<T> &As)

Clear sparse matrix to free memory.

clear(As) removes all values and indices of sparse matrix As and fix size to 0x0. clear should be used to free memory without deleting sparse matrix object.

smatrix<> X = {{1,2,3},{4,5,6}};
disp(Xs);
clear(Xs);
disp(Xs);

See Class matrix.

disp

template<typename T>
void castor::disp(smatrix<T> const &As, int info = 2, std::ostream &flux = std::cout, std::size_t r = 3)

Display sparse matrix.

disp(spones(3,4));
disp(spones(3,4),2);
disp(spones(3,4),2,std::cout);
disp(spones(3,4),2,std::cout,12);

See operator<<.

find

template<typename T>
auto castor::find(smatrix<T> const &As)

Find bi-linear indices of nonzero elements.

(I,J,V) = find(As) returns the bi-linear indices corresponding to the nonzero entries of the array As. As may be a logical expression.

Use index(As) or values(As) to find respectively linear index or values.

smatrix<> As = eye(3,4);
smatrix<std::size_t> Ls = find(A);
disp(Ls);

See index, values.

full

template<typename T>
matrix<T> castor::full(smatrix<T> const &As)

Dense conversion.

A = full(As) convert sparse matrix to dense matrix.

smatrix<> As = speye(3,4);
matrix<>  A  = full(As);
disp(A);

See sparse.

index

template<typename T>
matrix<std::size_t> castor::index(smatrix<T> const &As)

Copy indices in a matrix.

I = index(As) returns matrix with all linear indices.

smatrix<> As = {{1,2,3},{4,5,6}};
matrix<std::size_t> val = index(As);
disp(val);

mtimes

template<typename R, typename S>
auto castor::mtimes(matrix<R> const &A, smatrix<S> const &Bs)
template<typename R, typename S>
auto castor::mtimes(smatrix<R> const &As, matrix<S> const &B)

Matrix multiply.

mtimes(As,B) is the matrix product of As (sparse) and B (dense), the number of columns of As must equal the number of rows of B.

mtimes(A,Bs) is the matrix product of A (dense) and Bs (sparse), the number of columns of A must equal the number of rows of Bs.

mtimes(As,Bs) is the matrix product of A (sparse) and Bs (sparse), the number of columns of As must equal the number of rows of Bs. This is a naive implementation.

smatrix<> As = speye(3);
matrix<>  B  = ones(3,1);
matrix<>  C  = mtimes(As,B);
disp(C);

See tgemm, kron.

nnz

template<typename T>
std::size_t castor::nnz(smatrix<T> const &As)

Number of nonzero matrix elements.

nz = nnz(As) is the number of nonzero elements in As.

smatrix<>   As = speye(3,4);
std::size_t nz = nnz(As);
disp(nz);

See find, size.

numel

template<typename T>
std::size_t castor::numel(smatrix<T> const &As)

Number of elements in a sparse array (including zeros).

N = numel(As) returns the number of elements N of sparse array As (including zeros), equivalent to prod(size(As)).

smatrix<>   As = speye(3,4);
std::size_t n  = numel(As);
disp(n);

See size, nnz.

reshape

template<typename T>
smatrix<T> castor::reshape(smatrix<T> const &As, std::size_t m, std::size_t n)

Reshape sparse matrix.

reshape(As,M,N) returns the M-by-N sparse matrix whose elements are taken from A. An error results if A does not have M*N elements.

smatrix<> As = {{1,2,3},{4,5,6}};
smatrix<> Bs = reshape(A,3,2);
disp(Bs);

See transpose.

size

template<typename T>
std::size_t castor::size(smatrix<T> const &As, int dim)
template<typename T>
matrix<std::size_t> castor::size(smatrix<T> const &As)

Size of sparse matrix.

S = size(As) for m-by-n sparse matrix As returns the two-element vector [m,n] containing the number of rows and columns in the matrix.

S = size(As,dim) returns the lengths of the specified dimensions dim.

smatrix<> As = speye(3,4);
disp(size(As));
disp(size(As,1));
disp(size(As,2));

See numel, nnz.

sparse

template<typename T>
smatrix<T> castor::sparse(matrix<std::size_t> const &L, matrix<T> const &V, std::size_t m, std::size_t n)
template<typename T>
smatrix<T> castor::sparse(matrix<std::size_t> const &I, matrix<std::size_t> const &J, matrix<T> const &V)
template<typename T>
smatrix<T> castor::sparse(matrix<std::size_t> const &I, matrix<std::size_t> const &J, matrix<T> const &V, std::size_t m, std::size_t n)
template<typename T>
smatrix<T> castor::sparse(matrix<T> const &A)

Sparse conversion.

As = sparse(A) convert dense to sparse matrix.

S = sparse(L,V,m,n) uses vectors L and V to generate an m-by-n sparse matrix S, such that S(L(k)) = V(k). Vectors L and V are all the same length. Any elements of V that have duplicate values of L are added together.

S = sparse(I,J,V,m,n) uses vectors I, J, and V to generate an m-by-n sparse matrix S, such that S(I(k),J(k)) = V(k). Vectors I, J, and V are all the same length. Any elements of V that have duplicate values of I and J are added together.

S = sparse(I,J,V) use m = max(I)+1 and n = max(J)+1.

matrix<>  A  = eye(3,4);
smatrix<> As = sparse(A);
disp(As);

See full.

speye

template<typename T = double>
smatrix<T> castor::speye(matrix<std::size_t> const &S)
template<typename T = double>
smatrix<T> castor::speye(std::size_t m, long n = -1)

Identity sparse matrix.

speye(N) is the N-by-N identity sparse matrix.

speye(M,N) or speye({M,N}) is an M-by-N sparse matrix with 1’s on the diagonal and zeros elsewhere.

smatrix<> As = speye(2,3);
disp(As);

See spzeros, spones, sprand, eye.

spones

template<typename T = double>
smatrix<T> castor::spones(matrix<std::size_t> const &S)
template<typename T = double>
smatrix<T> castor::spones(std::size_t m, long n = -1)

Ones sparse matrix.

spones(N) is an N-by-N sparse matrix of ones.

spones(M,N) or spones({M,N}) is an M-by-N sparse matrix of ones.

matrix<> As = spones(2,3);
disp(As);

See spzeros, speye, sprand, ones.

sprand

template<typename T = double>
smatrix<T> castor::sprand(matrix<std::size_t> const &S, bool seed = false)
template<typename T = double>
smatrix<T> castor::sprand(std::size_t m, long n = -1, bool seed = false)

Sparse matrix of uniformly distributed pseudorandom numbers.

sprand(N) returns an N-by-N sparse matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval (0,1).

sprand(M,N) or sprand({M,N}) returns an M-by-N sparse matrix.

sprand(M,N,true) use standard time to initialize seed.

smatrix<> As = sprand(2,3);
smatrix<> Bs = sprand(2,3,true);
disp(As);
disp(Bs);

See spzeros, speye, spones, rand.

spzeros

template<typename T = double>
smatrix<T> castor::spzeros(std::size_t m, long n = -1)

Zeros sparse matrix.

spzeros(N) is an N-by-N sparse matrix of zeros.

spzeros(M,N) or spzeros({M,N}) is an M-by-N sparse matrix of zeros.

matrix<> As = spzeros(2,3);
disp(As);

See spones, speye, sprand, zeros.

transpose

template<typename T>
smatrix<T> castor::transpose(smatrix<T> const &As)

Non-conjugate transpose.

transpose(A) is called for the syntax A^t.

smatrix<> As = {{1,2,3},{4,5,6}};
smatrix<> At = transpose(As);
disp(At);

See reshape.

values

template<typename T>
matrix<T> castor::values(smatrix<T> const &As)

Copy non-zeros values in a matrix.

V = values(As) returns matrix with all non zeros values of sparse matrix As.

smatrix<> As = {{1,2,3},{4,5,6}};
matrix<double> val = values(As);
disp(val);

See index, find.

operator<<

template<typename T>
std::ostream &castor::operator<<(std::ostream &flux, smatrix<T> const &As)

Flux operator.

Insert sparse matrix As inside a stream flux respecting standard C++ conventions.

smatrix<> As = speye(3,4);
std::cout << As << std::endl;

See disp.

operator+

template<typename R, typename S>
auto castor::operator+(smatrix<R> const &As, S b)
template<typename R, typename S>
auto castor::operator+(R a, smatrix<S> const &Bs)
template<typename R, typename S>
auto castor::operator+(smatrix<R> const &As, matrix<S> const &B)
template<typename R, typename S>
auto castor::operator+(matrix<R> const &A, smatrix<S> const &Bs)
template<typename R, typename S>
auto castor::operator+(smatrix<R> const &As, smatrix<S> const &Bs)

Plus.

As+Bs adds sparse matrices As and Bs. As and Bs must have compatible sizes. In the simplest cases, they can be the same size or one can be a scalar. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are the same.

smatrix<> As = {{1,2,3},{4,5,6}};
smatrix<> Bs = {{6,5,4},{3,2,1}};
smatrix<> Cs = As+Bs;
smatrix<> Ds = As+1;
disp(Cs);
disp(Ds);

operator-

template<typename R, typename S>
auto castor::operator-(smatrix<R> const &As, S b)
template<typename R, typename S>
auto castor::operator-(R a, smatrix<S> const &Bs)
template<typename R, typename S>
auto castor::operator-(smatrix<R> const &As, matrix<S> const &B)
template<typename R, typename S>
auto castor::operator-(matrix<R> const &A, smatrix<S> const &Bs)
template<typename R, typename S>
auto castor::operator-(smatrix<R> const &As, smatrix<S> const &Bs)
template<typename T>
auto castor::operator-(smatrix<T> const &As)

Unary or binary minus.

-As negates the elements of As.

smatrix<> As = {{1,2,3},{4,5,6}};
smatrix<> Bs = -As;
disp(Bs);

As-Bs substracts sparse matrices As and Bs. As and Bs must have compatible sizes. In the simplest cases, they can be the same size or one can be a scalar. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are the same.

smatrix<> As = {{1,2,3},{4,5,6}};
smatrix<> Bs = {{6,5,4},{3,2,1}};
smatrix<> Cs = As-Bs;
smatrix<> Ds = As-1;
disp(Cs);
disp(Ds);

operator*

template<typename R, typename S>
inline auto castor::operator*(smatrix<R> const &As, S Bs)
template<typename R, typename S>
inline auto castor::operator*(R As, smatrix<S> const &Bs)
template<typename R, typename S>
auto castor::operator*(smatrix<R> const &As, matrix<S> const &B)
template<typename R, typename S>
auto castor::operator*(matrix<R> const &A, smatrix<S> const &Bs)
template<typename R, typename S>
auto castor::operator*(smatrix<R> const &As, smatrix<S> const &Bs)

Multiply.

As*Bs multiply sparse matrices As and Bs. As and Bs must have compatible sizes. In the simplest cases, they can be the same size or one can be a scalar. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are the same.

smatrix<> As = {{1,2,3},{4,5,6}};
smatrix<> Bs = {{6,5,4},{3,2,1}};
smatrix<> Cs = As*Bs;
smatrix<> Ds = As*2;
disp(Cs);
disp(Ds);

operator/

template<typename R, typename S>
inline auto castor::operator/(smatrix<R> const &As, S Bs)
template<typename R, typename S>
inline auto castor::operator/(matrix<R> const &As, smatrix<S> const &Bs)
template<typename R, typename S>
inline auto castor::operator/(smatrix<R> const &As, matrix<S> const &Bs)
template<typename R, typename S>
inline auto castor::operator/(R As, smatrix<S> const &Bs)
template<typename R, typename S>
auto castor::operator/(smatrix<R> const &As, smatrix<S> const &Bs)

Divide.

As/Bs divide sparse matrices As and Bs. As and Bs must have compatible sizes. In the simplest cases, they can be the same size or one can be a scalar. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are the same.

smatrix<> As = {{1,2,3},{4,5,6}};
smatrix<> Bs = {{6,5,4},{3,2,1}};
smatrix<> Cs = As/Bs;
smatrix<> Ds = As/2;
disp(Cs);
disp(Ds);