Class smatrix¶
-
template<typename T = double>
class castor::smatrix¶ Array with element of type T (double by default), stored in sparse format, e.g. only non-zeros values are keeped in memory.
Values are stored in std::vector<T>, associated to linear indices in std::vector<std::size_t> of the same dimensions of the values.
smatrix<logical> s = true; smatrix<int> X = {1, 2, 3, 4}; smatrix<double> A = {{1, 2, 3, 4}, {5, 6, 7, 8}}; auto B = A;
Constructors
-
smatrix()¶
Builds an empty sparse matrix
smatrix<> As; disp(As);
-
smatrix(std::initializer_list<T> const &v)¶
Builds a row matrix from initializer list.
smatrix<float> As({0,1,2,M_PI}); disp(As);
-
smatrix(std::initializer_list<std::vector<T>> const &v)¶
Builds a matrix from nested initializer list.
smatrix<double> As({{0,1,2,M_PI},{4,5,6,7},{8,9,10,11}}); disp(As);
-
smatrix(std::size_t m, std::size_t n)¶
Builds a sparse matrix from dimensions
smatrix<> As(3,4); disp(As);
-
smatrix(std::size_t m, std::size_t n, std::size_t nnz)¶
Builds a sparse matrix from dimensions and prepare non zeros values
smatrix<> As(3,4,7); disp(As);
-
template<typename S>
smatrix(std::size_t m, std::size_t n, std::vector<std::size_t> const &L, std::vector<S> const &V)¶ Builds sparse matrix using linear indexing and values
smatrix<> As(2,3,{1,0,3,2,5,4},std::vector<double>({0.,0.,1.,2.,0.,3.})); disp(Bs);
-
template<typename S>
smatrix(std::size_t m, std::size_t n, std::vector<std::size_t> const &I, std::vector<std::size_t> const &J, std::vector<S> const &V)¶ Builds sparse matrix using bi-linear indexing and values
smatrix<> As(2,3,{0,0,1,1},{0,1,0,1},std::vector<double>({1,0,0,2})); disp(As);
Internal operators
-
template<typename S>
explicit operator S() const¶ Converts a (1x1) to fundamental type variable.
smatrix<int> A(3); a = double(A); disp(a);
-
smatrix<T> &operator+=(smatrix<T> const &A)¶
Performs addition assignment.
If A is fundamental type variable, add value of A to each element of the sparse matrix If A is a sparse matrix, add each element of A to each element of the sparse matrix, in this case matrix dimensions must agree.
smatrix<float> As = speye(3,4); As += 1; disp(As); As += spones(3,4); disp(As);
-
smatrix<T> &operator-=(smatrix<T> const &A)¶
Performs substraction assignment.
If As is fundamental type variable, substract value of As to each element of the sparse matrix If As is a sparse matrix, substract each element of As to each element of the sparse matrix, in this case matrix dimensions must agree.
smatrix<float> As = speye(3,4); As -= 1; disp(As); As -= spones(3,4); disp(As);
-
smatrix<T> &operator*=(smatrix<T> const &A)¶
Performs multiplication assignment.
If As is fundamental type variable, multiply value of As to each element of the sparse matrix If As is a sparse matrix, multiply each element of As to each element of the sparse matrix, in this case matrix dimensions must agree.
smatrix<float> As = speye(3,4); As *= 2; disp(As);
-
smatrix<T> &operator/=(smatrix<T> const &A)¶
Performs division assignment.
If As is fundamental type variable, divide value of As to each element of the sparse matrix If As is a sparse matrix, divide each element of As to each element of the sparse matrix, in this case matrix dimensions must agree.
smatrix<float> As = speye(3,4); As /= 2; disp(As);
-
const T &operator()(std::size_t l) const¶
Returns an element of the sparse matrix by giving the linear indexing of element (const accessor).
Remark : sparse matrix values are stored in matrix<T> object, associated to matrix<std:size_t> both for row and column indexing.
smatrix<float> As = speye(3,4); float coef = A(11); disp(coef);
WARNING : Sparse indexing is relatively slow, please prefer builders or constructors instead.
-
T &operator()(std::size_t l)¶
Returns an element of the sparse matrix by giving the linear indexing of element (non const accessor).
Remark : sparse matrix values are stored in matrix<T> object, associated to matrix<std:size_t> both for row and column indexing.
smatrix<float> As = speye(3,4); // access to last element As(11) = 10; disp(As(11));
WARNING : Sparse indexing is relatively slow, please prefer builders or constructors instead.
-
const scview<T> operator()(matrix<std::size_t> const &L) const¶
Returns a reference to a submatrix corresponding to linear indexing L (const accessor).
const smatrix<float> As = speye(3,4); // access to element equal to 1 auto Bs = As({0,5,10}); disp(eval(B));
-
sview<T> operator()(matrix<std::size_t> const &L)¶
Returns a reference to a submatrix corresponding to linear indexing L (non-const accessor).
smatrix<float> As = speye(3,4); // access to element equal to 1 auto Bs = As({0,5,10}); disp(eval(Bs));
-
const T &operator()(std::size_t i, std::size_t j) const¶
Returns an element of the sparse matrix by giving the bi-linear indexing of element (const accessor).
const smatrix<int> As = speye(3,4); int coef = As(2,3); disp(coef);
WARNING : Sparse indexing is relatively slow, please prefer builders or constructors instead.
-
T &operator()(std::size_t i, std::size_t j)¶
Returns an element of the sparse matrix by giving the bi-linear indexing of element (non-const accessor).
smatrix<int> As = speye(3,4); As(2,3) = 5; disp(As(2,3));
WARNING : Sparse indexing is relatively slow, please prefer builders or constructors instead.
-
const scview<T> operator()(matrix<std::size_t> const &I, matrix<std::size_t> const &J) const¶
Returns a reference to a submatrix corresponding to bilinear indexing I for rows and J for columns (const accessor).
smatrix<float> As = speye(3,4); auto Bs = As({0,2}, {0,2}); disp(eval(Bs));
-
sview<T> operator()(matrix<std::size_t> const &I, matrix<std::size_t> const &J)¶
Return a reference to a submatrix corresponding to bilinear indexing I for rows and J for columns (non-const accessor).
smatrix<float> As = speye(3,4); auto Bs = As({0,2}, {0,2}); disp(eval(Bs));
-
void check()¶
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);
-
void clear()¶
Removes all values of sparse matrix A and fix size to 0x0.
Clear should be used to free memory without deleting matrix object.
smatrix<> As = {{1,2,3},{4,5,6}}; disp(As); As.clear; disp(As);
-
void reshape(std::size_t m, std::size_t n)¶
Modifies the shape of the sparse matrix without changing its values.
smatrix<> As = speye(3,4); As.reshape(4,3); disp(As);
-
std::size_t const &ind(std::size_t k) const¶
Linear index of the k-th stored value
smatrix<> As = {{1,2,0},{0,5,6}}; disp(As.ind(0));
-
std::size_t nnz() const¶
Non zeros values stored
smatrix<> As = {{1,2,0},{0,5,6}}; disp(As.nnz());
-
std::size_t size(int dim = 0) const¶
If dim=0 (default value) returns the number elements of the sparse matrix else returns the lengths of the specified dimensions dim.
smatrix<> As = {{1,2,3},{4,5,6}}; disp(As.size(0)); disp(As.size(1)); disp(As.size(2));
-
smatrix()¶