Ravelin
SparseMatrixN.h
1 class SPARSEMATRIXN
3 {
4  public:
5  enum StorageType { eCSR, eCSC };
6 
8  SPARSEMATRIXN(StorageType s);
9  SPARSEMATRIXN(StorageType s, unsigned m, unsigned n, const std::map<std::pair<unsigned, unsigned>, REAL>& values);
10  SPARSEMATRIXN(StorageType s, unsigned m, unsigned n, boost::shared_array<unsigned> ptr, boost::shared_array<unsigned> indices, boost::shared_array<REAL> data);
11  SPARSEMATRIXN(const MATRIXN& m, REAL tol=EPS);
12  SPARSEMATRIXN(StorageType s, const MATRIXN& m, REAL tol=EPS);
13  REAL norm_inf() const;
14  static SPARSEMATRIXN identity(unsigned n);
15  static SPARSEMATRIXN identity(StorageType stype, unsigned n);
16  VECTORN& mult(const VECTORN& x, VECTORN& result) const;
17  VECTORN& transpose_mult(const VECTORN& x, VECTORN& result) const;
18  MATRIXN& mult(const MATRIXN& m, MATRIXN& result) const;
19  MATRIXN& mult_transpose(const MATRIXN& m, MATRIXN& result) const;
20  MATRIXN& transpose_mult(const MATRIXN& m, MATRIXN& result) const;
21  MATRIXN& transpose_mult_transpose(const MATRIXN& m, MATRIXN& result) const;
22  unsigned rows() const { return _rows; }
23  unsigned columns() const { return _columns; }
24  SPARSEMATRIXN get_sub_mat(unsigned rstart, unsigned rend, unsigned cstart, unsigned cend) const;
25  SPARSEVECTORN& get_row(unsigned i, SPARSEVECTORN& row) const;
26  SPARSEVECTORN& get_column(unsigned i, SPARSEVECTORN& column) const;
27  VECTORN& get_row(unsigned i, VECTORN& row) const;
28  VECTORN& get_column(unsigned i, VECTORN& column) const;
29  const unsigned* get_indices() const { return _indices.get(); }
30  const unsigned* get_ptr() const { return _ptr.get(); }
31  const REAL* get_data() const { return _data.get(); }
32  void set_row(unsigned i, const VECTORN& v);
33  void set_column(unsigned i, const VECTORN& v);
37  SPARSEMATRIXN& operator*=(REAL scalar);
39  static SPARSEMATRIXN& outer_square(const VECTORN& g, SPARSEMATRIXN& result);
40  static SPARSEMATRIXN& outer_square(const SPARSEVECTORN& v, SPARSEMATRIXN& result);
41  MATRIXN& to_dense(MATRIXN& m) const;
42  void set_capacities(unsigned nnz_capacity, unsigned ptr_capacity, bool preserve);
43  void get_values(std::map<std::pair<unsigned, unsigned>, REAL>& values) const;
44 
46  StorageType get_storage_type() const { return _stype; }
47 
49  unsigned* get_indices() { return _indices.get(); }
50 
52  /*
53  * Element j denotes the location in the nonzero data and column (row, if
54  CSC) indices that starts row (column, if CSC) j. This array has rows()+1
55  (columns()+1, if CSC) entries and the final element in the array is
56  equal to get_nnz().
57  */
58  unsigned* get_ptr() { return _ptr.get(); }
59 
61  REAL* get_data() { return _data.get(); }
62 
64  unsigned get_nnz() const { return _nnz; }
65 
66  protected:
67  SPARSEMATRIXN(StorageType stype, unsigned m, unsigned n);
68 
69  boost::shared_array<unsigned> _indices; // column (row) indices of nonzeros
70  boost::shared_array<unsigned> _ptr; // starting indices for row (col) i
71  boost::shared_array<REAL> _data; // actual data (nonzeros)
72  unsigned _nnz; // the number of nonzeros
73  unsigned _rows;
74  unsigned _columns;
75  unsigned _nnz_capacity; // the nnz capacity
76  unsigned _ptr_capacity; // the row capacity
77  StorageType _stype; // the storage capacity
78 
79  private:
80  void set(unsigned rows, unsigned columns, const std::map<std::pair<unsigned, unsigned>, REAL>& values);
81 }; // end class
82 
83 std::ostream& operator<<(std::ostream& out, const SPARSEMATRIXN& s);
84 
SPARSEMATRIXN & operator-=(const SPARSEMATRIXN &m)
Subtracts a sparse matrix from this one – attempts to do it in place.
Definition: SparseMatrixN.cpp:1152
SPARSEVECTORN & get_row(unsigned i, SPARSEVECTORN &row) const
Gets a row of the sparse matrix as a sparse vector.
Definition: SparseMatrixN.cpp:655
VECTORN & mult(const VECTORN &x, VECTORN &result) const
Multiplies this sparse matrix by a dense vector.
Definition: SparseMatrixN.cpp:946
SPARSEVECTORN & get_column(unsigned i, SPARSEVECTORN &column) const
Gets a column of the sparse matrix as a sparse vector.
Definition: SparseMatrixN.cpp:542
VECTORN & transpose_mult(const VECTORN &x, VECTORN &result) const
Multiplies the transpose of this sparse matrix by a dense vector.
Definition: SparseMatrixN.cpp:986
SPARSEMATRIXN & operator*=(REAL scalar)
Multiplies a sparse matrix by a scalar.
Definition: SparseMatrixN.cpp:1277
void set_column(unsigned i, const VECTORN &v)
Sets the column with the particular index.
Definition: SparseMatrixN.cpp:285
MATRIXN & to_dense(MATRIXN &m) const
Gets a dense matrix from this sparse matrix.
Definition: SparseMatrixN.cpp:1130
static SPARSEMATRIXN & outer_square(const VECTORN &g, SPARSEMATRIXN &result)
Calculates the outer product of a vector with itself and stores the result in a sparse matrix...
Definition: SparseMatrixN.cpp:1360
MATRIXN & mult_transpose(const MATRIXN &m, MATRIXN &result) const
Multiplies this matrix by the transpose of a dense matrix.
Definition: SparseMatrixN.cpp:1051
void set_capacities(unsigned nnz_capacity, unsigned ptr_capacity, bool preserve)
Sets the capacities of the arrays.
Definition: SparseMatrixN.cpp:864
SPARSEMATRIXN & operator=(const SPARSEMATRIXN &m)
Copies a sparse matrix to this.
Definition: SparseMatrixN.cpp:1228
A generic, possibly non-square matrix.
Definition: MatrixN.h:18
A sparse vector represented in 'CSR' format.
Definition: SparseVectorN.h:6
A sparse matrix.
Definition: SparseMatrixN.h:2
SPARSEMATRIXN get_sub_mat(unsigned rstart, unsigned rend, unsigned cstart, unsigned cend) const
Gets a submatrix of the sparse matrix.
Definition: SparseMatrixN.cpp:767
StorageType get_storage_type() const
Gets the storage type.
Definition: SparseMatrixN.h:46
REAL norm_inf() const
Computes the infinity norm of this sparse matrix.
Definition: SparseMatrixN.cpp:181
static SPARSEMATRIXN identity(unsigned n)
Sets up an identity matrix from a sparse matrix.
Definition: SparseMatrixN.cpp:190
A generic N-dimensional floating point vector.
Definition: VectorN.h:16
SPARSEMATRIXN & negate()
Negates this sparse matrix.
Definition: SparseMatrixN.cpp:1284
SPARSEMATRIXN & operator+=(const SPARSEMATRIXN &m)
Adds a sparse matrix to this one – attempts to do it in place.
Definition: SparseMatrixN.cpp:1190
unsigned * get_ptr()
Gets the row (column, if CSC) pointers.
Definition: SparseMatrixN.h:58
MATRIXN & transpose_mult_transpose(const MATRIXN &m, MATRIXN &result) const
Multiplies the transpose of this sparse matrix by the transpose of a dense matrix.
Definition: SparseMatrixN.cpp:1092
REAL * get_data()
Gets the array of nonzeros (sized get_nnz())
Definition: SparseMatrixN.h:61
void set_row(unsigned i, const VECTORN &v)
Sets the row with the particular index.
Definition: SparseMatrixN.cpp:369
unsigned * get_indices()
Gets the column (row, if CSC) indices of the nonzeros (sized get_nnz()
Definition: SparseMatrixN.h:49
unsigned get_nnz() const
Gets the number of nonzeros.
Definition: SparseMatrixN.h:64