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);
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¶
mtimes¶
-
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);
nnz¶
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);
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>
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));
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¶
spones¶
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);
spzeros¶
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¶
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, 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 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>
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/(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>
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);