ConicBundle
Private Member Functions | Private Attributes | Friends | List of all members
CH_Matrix_Classes::Sparsemat Class Reference

Matrix class of sparse matrices with real values of type Real More...

#include <sparsmat.hxx>

Inheritance diagram for CH_Matrix_Classes::Sparsemat:
CH_Matrix_Classes::Memarrayuser

Public Member Functions

Constructors, Destructor, and Initialization (Members)
 Sparsemat ()
 empty matrix
 
 Sparsemat (const Sparsemat &A, Real d=1.)
 copy constructor, *this=d*A, abs(values)<tol are removed from the support
 
 Sparsemat (Integer nr, Integer nc)
 initialize to zero-matrix of size nr*nc
 
 Sparsemat (Integer nr, Integer nc, Integer nz, const Integer *ini, const Integer *inj, const Real *va)
 initialize to size nr*nc and nz nonzeros so that this(ini[i],inj[i])=val[i] for i=0,..,nz-1; multiple elements are summed up.
 
 Sparsemat (Integer nr, Integer nc, Integer nz, const Indexmatrix &ini, const Indexmatrix &inj, const Matrix &va)
 initialize to size nr*nc and nz nonzeros so that this(ini(i),inj(i))=val(i) for i=0,..,nz-1; multiple elements are summed up.
 
void set_init (bool)
 after external initialization, call matrix.set_init(true) (not needed if CONICBUNDLE_DEBUG is undefined)
 
bool get_init () const
 returns true if the matrix has been declared initialized (not needed if CONICBUNDLE_DEBUG is undefined)
 
Sparsematinit (const Sparsemat &A, Real d=1.)
 initialize to *this=A*d
 
Sparsematinit (const Matrix &A, Real d=1.)
 initialize to *this=A*d, abs(values)<tol are removed from the support
 
Sparsematinit (const Indexmatrix &A, Real d=1.)
 initialize to *this=A*d, zeros are removed from the support
 
Sparsematinit (const Symmatrix &, Real d=1.)
 initialize to *this=A*d, abs(values)<tol are removed from the support
 
Sparsematinit (const Sparsesym &, Real d=1.)
 initialize to *this=A*d
 
Sparsematinit (Integer nr, Integer nc)
 initialize to zero-matrix of size nr*nc
 
Sparsematinit (Integer nr, Integer nc, Integer nz, const Integer *ini, const Integer *inj, const Real *va)
 initialize to size nr*nc and nz nonzeros so that this(ini[i],inj[i])=val[i] for i=0,..,nz-1; multiple elements are summed up.
 
Sparsematinit (Integer nr, Integer nc, Integer nz, const Indexmatrix &ini, const Indexmatrix &inj, const Matrix &va)
 initialize to size nr*nc and nz nonzeros so that this(ini(i),inj(i))=val(i) for i=0,..,nz-1; multiple elements are summed up.
 
void set_tol (Real t)
 set tolerance for recognizing zero values to t
 
Conversions from other Matrix Classes (Members)
 Sparsemat (const Matrix &A, Real d=1.)
 initialize to *this=d*A, abs(values)<tol are removed from the support
 
 Sparsemat (const Indexmatrix &A, Real d=1.)
 initialize to *this=d*A, zeros are removed from the support
 
 Sparsemat (const Symmatrix &A, Real d=1.)
 initialize to *this=d*A, abs(values)<tol are removed from the support
 
 Sparsemat (const Sparsesym &A, Real d=1.)
 initialize to *this=d*A
 
Size and Type Information (Members)
void dim (Integer &in_nr, Integer &in_nc) const
 returns the number of rows in _nr and the number of columns in _nc
 
Integer dim () const
 returns the dimension rows * columns when the matrix is regarded as a vector
 
Integer rowdim () const
 returns the row dimension
 
Integer coldim () const
 returns the column dimension
 
Integer nonzeros () const
 returns the number of nonzeros
 
Integer col_nonzeros (Integer i, Integer *startind=0) const
 returns the number of nonzeros in column i; if nonzeros>0 and startind!=0 then the index of the first nonzero in colindex/colval is stored there
 
Integer row_nonzeros (Integer i, Integer *startind=0) const
 returns the number of nonzeros in row i; if nonzeros>0 and startind!=0 then the index of the first nonzero in rowindex/rowval is stored there
 
Mtype get_mtype () const
 returns the type of the matrix, MTmatrix
 
Indexing and Submatrices (Members)
Real operator() (Integer i, Integer j) const
 returns value of element (i,j) of the matrix (rowindex i, columnindex j)
 
Real operator() (Integer i) const
 returns value of element (i) of the matrix if regarded as vector of stacked columns [element (irowdim, i/rowdim)]
 
Real operator[] (Integer i) const
 returns value of element (i) of the matrix if regarded as vector of stacked columns [element (irowdim, i/rowdim)]
 
Sparsemat col (Integer i) const
 returns column i copied to a new sparse matrix
 
Sparsemat row (Integer i) const
 returns row i copied to a new sparse matrix
 
Sparsemat cols (const Indexmatrix &ind) const
 returns a sparse matrix of size this->rowdim() x vec.dim(), with column i a copy of column vec(i) of *this
 
Sparsemat rows (const Indexmatrix &ind) const
 returns a sparse matrix of size vec.dim() x this->rowdim(), with row i a copy of row vec(i) of *this
 
Sparsematdelete_rows (const Indexmatrix &ind)
 all rows indexed by vector ind are deleted, no row should appear twice in ind, remaining rows are moved up keeping their order, returns *this
 
Sparsematdelete_cols (const Indexmatrix &ind)
 all colmuns indexed by vector ind are deleted, no column should appear twice in ind, remaining columns are moved up keeping their order, returns *this
 
Sparsematinsert_row (Integer i, const Sparsemat &v)
 insert the row vector v before row i, 0<=i<= row dimension, for i==row dimension the row is appended below; appending to a 0x0 matrix is allowed, returns *this
 
Sparsematinsert_col (Integer i, const Sparsemat &v)
 insert a column before column i, 0<=i<= column dimension, for i==column dimension the column is appended at the right; appending to a 0x0 matrix is allowed, returns *this
 
Sparsematconcat_right (const Sparsemat &A)
 concats sparse matrix A to the right of *this, A or *this may be the 0x0 matrix initally, returns *this
 
Sparsematconcat_below (const Sparsemat &A)
 concats sparse matrix A to the bottom of *this, A or *this may be the 0x0 matrix initally, returns *this
 
const Indexmatrixget_colinfo () const
 returns information on nonzero columns, k by 3, listing: index, %nonzeros, first index in colindex/colval
 
const Indexmatrixget_colindex () const
 returns the index vector of the column representation holding the row index for each element
 
const Matrixget_colval () const
 returns the value vector of the column representation holding the value for each element
 
const Indexmatrixget_rowinfo () const
 returns information on nonzero rows, k by 3, listing: index, %nonzeros, first index in rowindex/rowval
 
const Indexmatrixget_rowindex () const
 returns the index vector of the row representation holding the column index for each element
 
const Matrixget_rowval () const
 returns the value vector of the row representation holding the value for each element
 
void get_edge_rep (Indexmatrix &I, Indexmatrix &J, Matrix &val) const
 stores the nz nonzero values of *this in I,J,val so that this(I(i),J(i))=val(i) for i=0,...,nz-1 and dim(I)=dim(J)=dim(val)=nz (ordered as in row representation)
 
int get_edge (Integer i, Integer &indi, Integer &indj, Real &val) const
 stores element i of the get_edge_rep() function (ordered as in row representation); returns 1 if i is out of range, 0 otherwise.
 
int contains_support (const Sparsemat &A) const
 returns 1 if A is of the same dimension and the support of A is contained in the support of *this, 0 otherwise
 
BLAS-like Routines (Members)
Sparsematxeya (const Sparsemat &A, Real d=1.)
 sets *this=d*A and returns *this
 
Sparsematxeya (const Matrix &A, Real d=1.)
 sets *this=d*A removing abs(values)<tol; returns *this
 
Sparsematxeya (const Indexmatrix &A, Real d=1.)
 sets *this=d*A removing zeros; returns *this
 
Usual Arithmetic Operators (Members)
Sparsematoperator= (const Sparsemat &A)
 
Sparsematoperator+= (const Sparsemat &A)
 
Sparsematoperator-= (const Sparsemat &A)
 
Sparsemat operator- () const
 
Sparsematoperator*= (Real d)
 
Sparsematoperator/= (Real d)
 
Sparsematoperator= (const Matrix &A)
 sets *this=A removing abs(values)<tol; returns *this
 
Sparsematoperator= (const Indexmatrix &A)
 sets *this=A removing zeros; returns *this
 
Sparsemattranspose ()
 transposes itself (swaps row and column representations, thus cheap)
 
Connections to other Classes (Members)
Sparsematxeya (const Sparsesym &A, Real d=1.)
 sets *this=A*d, abs(values)<tol are removed from the support, and returns *this,
 
Sparsematoperator= (const Symmatrix &A)
 sets *this=A, abs(values)<tol are removed from the support, returns *this
 
Sparsematoperator= (const Sparsesym &A)
 sets *this=A and returns *this
 
Numerical Methods (Members)
Sparsematscale_rows (const Matrix &vec)
 scales each row i of (this) by vec(i), i.e., (*this)=diag(vec)(*this), and returns (*this)
 
Sparsematscale_cols (const Matrix &vec)
 scales each column i of (*this) by vec(i), i.e., (*this)=(*this)*diag(vec), and returns (*this)
 
Input, Output (Members)
void display (std::ostream &out, int precision=0, int width=0, int screenwidth=0) const
 displays a matrix in a pretty way for bounded screen widths; for variables of value zero default values are used. More...
 

Private Member Functions

void init_to_zero ()
 initialize the matrix to a 0x0 matrix without storage
 
Integer find_column (Integer i) const
 given column index i, find the corresponding rowindex of colinfo or return -1 if the column is empty
 
Integer find_row (Integer i) const
 given row index i, find the corresponding rowindex of rowinfo or return -1 if the row is empty
 

Private Attributes

Mtype mtype
 used for MatrixError templates (runtime type information was not yet existing)
 
Integer nr
 number of rows
 
Integer nc
 number of columns
 
Indexmatrix colinfo
 k by 3, for nonzero columns: index, # nonzeros, first index in colindex/colval
 
Indexmatrix colindex
 gives the rowindex of the element at position i, (sorted increasingly per column)
 
Matrix colval
 gives the value of the element at position i
 
Indexmatrix rowinfo
 k by 3, for nonzero rows: index, # nonzeros, first index in colindex/colval
 
Indexmatrix rowindex
 gives the column index of the element at position i, (sorted increasingly per row)
 
Matrix rowval
 gives the value of the element at position i
 
Real tol
 >0, if abs(value)<tol, then value is taken to be zero
 
bool is_init
 flag whether memory is initialized, it is only used if CONICBUNDLE_DEBUG is defined
 

Friends

class Indexmatrix
 
class Matrix
 
class Symmatrix
 
class Sparsesym
 
Indexing and Submatrices (Friends)
Sparsemat concat_right (const Sparsemat &A, const Sparsemat &B)
 returns a new sparse matrix [A, B], i.e., it concats matrices A and B rowwise; A or B may be a 0x0 matrix
 
Sparsemat concat_below (const Sparsemat &A, const Sparsemat &B)
 returns a new sparse matrix [A; B], i.e., it concats matrices A and B columnwise; A or B may be a 0x0 matrix
 
void swap (Sparsemat &A, Sparsemat &B)
 swap the content of the two sparse matrices A and B (involves no copying)
 
BLAS-like Routines (Friends)
Sparsematxbpeya (Sparsemat &x, const Sparsemat &y, Real alpha, Real beta, int ytrans)
 returns x= alpha*y+beta*x, where y may be transposed (ytrans=1); if beta==0. then x is initialized to the correct size
 
Matrixgenmult (const Sparsemat &A, const Matrix &B, Matrix &C, Real alpha, Real beta, int atrans, int btrans)
 returns C=beta*C+alpha*A*B, where A and B may be transposed; C must not be equal to A and B; if beta==0. then C is initialized to the correct size
 
Matrixgenmult (const Sparsemat &A, const Matrix &B, Integer colB, Matrix &C, Integer colC, Real alpha, Real beta, int atrans, int btrans)
 returns C.col(colC)=beta*C.col(colC)+alpha*A*B.col(colB), where A and B may be transposed first; C must not be equal to A and B; if beta==0. then C is initialized, but the size of C must be correct already
 
Matrixgenmult (const Matrix &A, const Sparsemat &B, Matrix &C, Real alpha, Real beta, int atrans, int btrans)
 returns C=beta*C+alpha*A*B, where A and B may be transposed; C must not be equal to A and B; if beta==0. then C is initialized to the correct size
 
Matrixgenmult (const Sparsemat &A, const Sparsemat &B, Matrix &C, Real alpha, Real beta, int atrans, int btrans)
 returns C=beta*C+alpha*A*B, where A and B may be transposed; C must not be equal to A and B; if beta==0. then C is initialized to the correct size
 
Usual Arithmetic Operators (Friends)
Sparsemat operator* (const Sparsemat &A, const Sparsemat &B)
 returns a Sparsemat equal to A*B
 
Sparsemat operator+ (const Sparsemat &A, const Sparsemat &B)
 returns a Sparsemat equal to A+B
 
Sparsemat operator- (const Sparsemat &A, const Sparsemat &B)
 returns a Sparsemat equal to A-B
 
Sparsemat operator* (const Sparsemat &A, Real d)
 returns a Sparsemat equal to A*d
 
Sparsemat operator* (Real d, const Sparsemat &A)
 returns a Sparsemat equal to d*A
 
Sparsemat operator/ (const Sparsemat &A, Real d)
 ATTENTION: d is NOT checked for 0.
 
Matrix operator* (const Sparsemat &A, const Matrix &B)
 returns a Matrix equal to A*B
 
Matrix operator* (const Matrix &A, const Sparsemat &B)
 returns a Matrix equal to A*B
 
Matrix operator+ (const Sparsemat &A, const Matrix &B)
 returns a Matrix equal to A+B
 
Matrix operator+ (const Matrix &A, const Sparsemat &B)
 returns a Matrix equal to A+B
 
Matrix operator- (const Sparsemat &A, const Matrix &B)
 returns a Matrix equal to A-B
 
Matrix operator- (const Matrix &A, const Sparsemat &B)
 returns a Matrix equal to A-B
 
Sparsemat transpose (const Sparsemat &A)
 returns a Sparsemat that is the transpose of A
 
Connections to other Classes (Friends)
Matrixgenmult (const Symmatrix &A, const Sparsemat &B, Matrix &C, Real alpha, Real beta, int btrans)
 returns C=beta*C+alpha*A*B, where B may be transposed; if beta==0. then C is initialized to the correct size
 
Matrixgenmult (const Sparsemat &A, const Symmatrix &B, Matrix &C, Real alpha, Real beta, int atrans)
 returns C=beta*C+alpha*A*B, where A may be transposed; if beta==0. then C is initialized to the correct size
 
Matrixgenmult (const Sparsesym &A, const Sparsemat &B, Matrix &C, Real alpha, Real beta, int btrans)
 returns C=beta*C+alpha*A*B, where B may be transposed; if beta==0. then C is initialized to the correct size
 
Matrixgenmult (const Sparsemat &A, const Sparsesym &B, Matrix &C, Real alpha, Real beta, int atrans)
 returns C=beta*C+alpha*A*B, where A may be transposed; if beta==0. then C is initialized to the correct size
 
Symmatrixrankadd (const Sparsemat &A, Symmatrix &C, Real alpha, Real beta, int trans)
 returns C=beta*C+alpha* A*A^T, where A may be transposed; if beta==0. then C is initialized to the correct size
 
Symmatrixscaledrankadd (const Sparsemat &A, const Matrix &D, Symmatrix &C, Real alpha, Real beta, int trans)
 returns C=beta*C+alpha* A*D*A^T, where D is a vector representing a diagonal matrix and A may be transposed; if beta==0. then C is initialized to the correct size
 
Symmatrixrank2add (const Sparsemat &A, const Matrix &B, Symmatrix &C, Real alpha, Real beta, int trans)
 returns C=beta*C+alpha*(A*B^T+B*A^T)/2 [or for transposed (A^T*B+B^T*A)/2]. If beta==0. then C is initiliazed to the correct size.
 
Matrix operator* (const Symmatrix &A, const Sparsemat &B)
 returns a Matrix that equals A*B
 
Matrix operator* (const Sparsemat &A, const Symmatrix &B)
 returns a Matrix that equals A*B
 
Elementwise Operations (Friends)
Sparsemat abs (const Sparsemat &A)
 returns a Sparsmat with elements abs((*this)(i,j)) for all i,j
 
Numerical Methods (Friends)
Real trace (const Sparsemat &A)
 returns the sum of the diagonal elements A(i,i) over all i
 
Real ip (const Sparsemat &A, const Sparsemat &B)
 returns the usual inner product of A and B, i.e., the sum of A(i,j)*B(i,j) over all i,j
 
Real ip (const Sparsemat &A, const Matrix &B)
 returns the usual inner product of A and B, i.e., the sum of A(i,j)*B(i,j) over all i,j
 
Real ip (const Matrix &A, const Sparsemat &B)
 returns the usual inner product of A and B, i.e., the sum of A(i,j)*B(i,j) over all i,j
 
Real colip (const Sparsemat &A, Integer j, const Matrix *scaling)
 returns the squared Frobenius norm of col i of A, i.e., the sum of A(i,j)*A(i,j) over all i with possibly (if scaling!=0) each term i multiplied by (*scaling)(i)
 
Real rowip (const Sparsemat &A, Integer i, const Matrix *scaling)
 returns the squared Frobenius norm of row i of A, i.e., the sum of A(i,j)*A(i,j) over all j with possibly (if scaling!=0) each term j multiplied by (*scaling)(j)
 
Sparsemat colsip (const Sparsemat &A, const Matrix *scaling)
 returns the column vector of the squared Frobenius norm of all columns j of A, i.e., the sum of A(i,j)*A(i,j) over all i for each j with possibly (if scaling~=0) each term i multiplied by (*scaling)(i)
 
Sparsemat rowsip (const Sparsemat &A, const Matrix *scaling)
 returns the row vector of the squared Frobenius norm of all rows i of A, i.e., the sum of A(i,j)*A(i,j) over all j for each i with possibly (if scaling~=0) each term j multiplied by (*scaling)(j)
 
Real norm2 (const Sparsemat &A)
 returns the Frobenius norm of A, i.e., the square root of the sum of A(i,j)*A(i,j) over all i,j
 
Matrix sumrows (const Sparsemat &A)
 returns a row vector holding the sum over all rows, i.e., (1 1 ... 1)*A
 
Matrix sumcols (const Sparsemat &A)
 returns a column vector holding the sum over all columns, i.e., A*(1 1 ... 1)^T
 
Real sum (const Sparsemat &A)
 returns the sum over all elements of A, i.e., (1 1 ... 1)*A*(1 1 ... 1)^T
 
Equal (Members)
int equal (const Sparsemat &A, const Sparsemat &B, Real eqtol)
 returns 1 if both matrices are identical, 0 otherwise
 
Input, Output (Friends)
std::ostream & operator<< (std::ostream &o, const Sparsemat &v)
 output format: nr nc nz \n i1 j1 val1\n i2 j2 val2\n ... inz jnz valnz\n
 
std::istream & operator>> (std::istream &i, Sparsemat &v)
 input format: nr nc nz \n i1 j1 val1\n i2 j2 val2\n ... inz jnz valnz\n
 

Additional Inherited Members

- Protected Member Functions inherited from CH_Matrix_Classes::Memarrayuser
 Memarrayuser ()
 if memarray is NULL, then a new Memarray is generated. In any case the number of users of the Memarray is incremented
 
virtual ~Memarrayuser ()
 the number of users is decremented and the Memarray memory manager is destructed, if the number is zero.
 
- Static Protected Attributes inherited from CH_Matrix_Classes::Memarrayuser
static Memarraymemarray
 pointer to common memory manager for all Memarrayusers, instantiated in memarray.cxx
 

Detailed Description

Matrix class of sparse matrices with real values of type Real

Any matrix element can be indexed by (i,j) or directly by the one dimensional index (i+j*nr). The latter view directly corresponds to the vec() operator often used in the linear algebra literature, i.e., the matrix is transformed to a vector by stacking the columns on top of each other.

Internally a sparse matrix of size nr x nc is automatically stored in sparse columnwise as well as rowwise format by means of additional objects of type Indexmatrix and Matrix. The purpose of this somewhat complicated format is to allow fast multiplication from left and right without increasing the amount of information stored by more than a constant factor times the number of nonzero elements.

We now explain the columnwise format (the rowwise is structured in the same manner). It is given by the matrices colinfo, colval, and colindex.

Suppose that 0<=k<=nc columns of the matrix contain nonzero elements, then the Indexmatrix colinfo is a k by 3 matrix with one row for each nonzero column and the elements of row k give

Matrix colval is a vector with number of elements equal to the number of nonzeros in the entire matrix. It stores the values of the nonzero elements in columnwise and then rowwise order, i.e., nonzero element (i1,j1) appears before a different nonzero element (i2,j2) iff ((j1<j2)||((j1==j2)&&(i1<i2)).

Indexmatrix colindex is of the same size as colval and stores the corresponding row indices in the corresponding position.

Finding an element (i,j) is thus done as follows: First find the column index j in the first column of colinfo by binary search yielding a colinfo-rowindex k if successful; then find the row index i within colindex[colinfo(k,2)]...colindex[colinfo(k,2)+colinfo(k,1)-1] again by binary search.

Member Function Documentation

◆ display()

void CH_Matrix_Classes::Sparsemat::display ( std::ostream &  out,
int  precision = 0,
int  width = 0,
int  screenwidth = 0 
) const

displays a matrix in a pretty way for bounded screen widths; for variables of value zero default values are used.

Parameters
outoutput stream
precisionnumber of most significant digits, default=4
widthfield width, default = precision+6
screenwidthmaximum number of characters in one output line, default = 80

Referenced by ConicBundle::CMgramsparse::display(), ConicBundle::CMlowrankss::display(), ConicBundle::CMlowranksd::display(), ConicBundle::CMgramsparse_withoutdiag::display(), and get_rowval().


The documentation for this class was generated from the following files: