Feel++  0.91.4
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Friends
Feel::MatrixSparse< T > Class Template Reference

#include <matrixsparse.hpp>

Inheritance diagram for Feel::MatrixSparse< T >:
Feel::MatrixBlockBase< T > Feel::MatrixGmm< T, LayoutType > Feel::MatrixBlock< NR, NC, T >

List of all members.

Public Types

typedef T value_type
typedef type_traits< T >::real_type real_type
typedef GraphCSR graph_type
typedef boost::shared_ptr
< graph_type
graph_ptrtype
typedef Vector< T > vector_type
typedef boost::shared_ptr
< Vector< T > > 
vector_ptrtype

Public Member Functions

 MatrixSparse ()
 MatrixSparse (DataMap const &dmRow, DataMap const &dmCol, WorldComm const &worldComm=WorldComm())
virtual ~MatrixSparse ()
DataMap const & mapRow () const
DataMap const & mapCol () const
void setMapRow (DataMap const &d)
void setMapCol (DataMap const &d)
virtual bool isInitialized () const
virtual void updateSparsityPattern (const std::vector< std::vector< size_type > > &)
virtual void init (const size_type m, const size_type n, const size_type m_l, const size_type n_l, const size_type nnz=30, const size_type noz=10)=0
virtual void init (const size_type m, const size_type n, const size_type m_l, const size_type n_l, graph_ptrtype const &graph)=0
virtual void setIndexSplit (std::vector< std::vector< int > > const &_indexSplit)
std::vector< std::vector< int > > indexSplit () const
bool hasGraph () const
graph_ptrtype const & graph () const
void setGraph (graph_ptrtype const &graph)
void setMatrixProperties (size_type p)
bool isHermitian () const
bool isNonHermitian () const
bool isHermitianPositiveDefinite () const
bool isSingular () const
bool isPositiveDefinite () const
bool haveConsistentProperties () const
void checkProperties () const
WorldComm const & comm () const
virtual void clear ()=0
virtual void zero ()=0
virtual void zero (size_type start1, size_type size1, size_type start2, size_type size2)=0
virtual void close () const =0
virtual size_type size1 () const =0
virtual size_type size2 () const =0
virtual size_type rowStart () const =0
virtual size_type rowStop () const =0
virtual void set (const size_type i, const size_type j, const value_type &value)=0
virtual void add (const size_type i, const size_type j, const value_type &value)=0
virtual void addMatrix (const ublas::matrix< value_type > &dm, const std::vector< size_type > &rows, const std::vector< size_type > &cols)=0
virtual void addMatrix (int *rows, int nrows, int *cols, int ncols, value_type *data)=0
virtual void addMatrix (const ublas::matrix< value_type > &dm, const std::vector< size_type > &dof_indices)=0
virtual void addMatrix (const T, MatrixSparse< T > &)=0
void addMatrix (const T &s, boost::shared_ptr< MatrixSparse< T > > &m)
virtual void scale (const T)=0
void multVector (const Vector< T > &arg, Vector< T > &dest) const
void multVector (const boost::shared_ptr< Vector< T > > &arg, boost::shared_ptr< Vector< T > > &dest) const
void multAddVector (const Vector< T > &arg, Vector< T > &dest) const
virtual T operator() (const size_type i, const size_type j) const =0
virtual MatrixSparse< T > & operator= (MatrixSparse< value_type > const &M)=0
MatrixSparse< T > & operator= (boost::shared_ptr< MatrixSparse< value_type > > const &M)
virtual void diagonal (Vector< T > &dest) const =0
void diagonal (boost::shared_ptr< Vector< T > > &dest) const
virtual void transpose (MatrixSparse< value_type > &Mt) const =0
boost::shared_ptr
< MatrixSparse< T > > 
transpose () const
void transpose (boost::shared_ptr< MatrixSparse< value_type > > &Mt) const
virtual void symmetricPart (MatrixSparse< value_type > &Ms) const
void symmetricPart (boost::shared_ptr< MatrixSparse< value_type > > &Ms) const
virtual real_type energy (vector_type const &v, vector_type const &u, bool transpose=false) const =0
real_type energy (vector_ptrtype const &v, vector_ptrtype const &u, bool _transpose=false) const
virtual real_type l1Norm () const =0
virtual real_type linftyNorm () const =0
virtual bool closed () const =0
void print (std::ostream &os=std::cout) const
virtual void printPersonal (std::ostream &=std::cout) const
virtual void printMatlab (const std::string name="NULL") const
virtual void createSubmatrix (MatrixSparse< T > &submatrix, const std::vector< size_type > &rows, const std::vector< size_type > &cols) const
virtual void reinitSubmatrix (MatrixSparse< T > &submatrix, const std::vector< size_type > &rows, const std::vector< size_type > &cols) const
virtual void zeroRows (std::vector< int > const &rows, std::vector< value_type > const &values, Vector< value_type > &rhs, Context const &on_context)=0
virtual void updateBlockMat (boost::shared_ptr< MatrixSparse< T > > m, size_type start_i, size_type start_j)=0
void setInitialized (bool _init)

Protected Member Functions

virtual void _get_submatrix (MatrixSparse< T > &, const std::vector< size_type > &, const std::vector< size_type > &, const bool) const

Protected Attributes

WorldComm M_worldComm
 mpi communicator
bool _M_is_initialized
graph_ptrtype _M_graph
Context M_mprop
std::vector< std::vector< int > > M_IndexSplit
DataMap M_mapRow
DataMap M_mapCol

Friends

template<typename U >
std::ostream & operator<< (std::ostream &os, const MatrixSparse< U > &m)

Detailed Description

template<typename T>
class Feel::MatrixSparse< T >

Generic sparse matrix. This class contains pure virtual members that must be overloaded in derived classes. Using a derived class allows for uniform access to sparse matrices from various different solver packages in different formats.

Author:
Benjamin S. Kirk, 2003
Christophe Prud'homme, 2005

Constructor & Destructor Documentation

template<typename T >
Feel::MatrixSparse< T >::MatrixSparse ( )
inline

Constructor; initializes the matrix to be empty, without any structure, i.e. the matrix is not usable at all. This constructor is therefore only useful for matrices which are members of a class. All other matrices should be created at a point in the data flow where all necessary information is available.

You have to initialize the matrix before usage with init(...).

template<typename T >
Feel::MatrixSparse< T >::~MatrixSparse ( )
inlinevirtual

Destructor. Free all memory, but do not release the memory of the sparsity structure.


Member Function Documentation

template<typename T>
virtual void Feel::MatrixSparse< T >::_get_submatrix ( MatrixSparse< T > &  ,
const std::vector< size_type > &  ,
const std::vector< size_type > &  ,
const bool   
) const
inlineprotectedvirtual

Protected implementation of the create_submatrix and reinit_submatrix routines. Note that this function must be redefined in derived classes for it to work properly!

Referenced by Feel::MatrixBlockBase< T >::createSubmatrix(), Feel::MatrixSparse< T >::createSubmatrix(), Feel::MatrixBlockBase< T >::reinitSubmatrix(), and Feel::MatrixSparse< T >::reinitSubmatrix().

template<typename T>
virtual void Feel::MatrixSparse< T >::add ( const size_type  i,
const size_type  j,
const value_type &  value 
)
pure virtual

Add value to the element (i,j). Throws an error if the entry does not exist. Still, it is allowed to store zero values in non-existent fields.

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::addMatrix ( const ublas::matrix< value_type > &  dm,
const std::vector< size_type > &  rows,
const std::vector< size_type > &  cols 
)
pure virtual

Add the full matrix to the Sparse matrix. This is useful for adding an element matrix at assembly time

Implemented in Feel::MatrixBlockBase< T >.

Referenced by Feel::MatrixSparse< T >::addMatrix().

template<typename T>
virtual void Feel::MatrixSparse< T >::addMatrix ( int *  rows,
int  nrows,
int *  cols,
int  ncols,
value_type *  data 
)
pure virtual

Add the full matrix to the Sparse matrix. This is useful for adding an element matrix at assembly time

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::addMatrix ( const ublas::matrix< value_type > &  dm,
const std::vector< size_type > &  dof_indices 
)
pure virtual

Same, but assumes the row and column maps are the same. Thus the matrix dm must be square.

Implemented in Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::addMatrix ( const T  ,
MatrixSparse< T > &   
)
pure virtual

Add a Sparse matrix _X, scaled with _a, to this, stores the result in this: $\texttt{this} = \_a*\_X + \texttt{this} $.

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
void Feel::MatrixSparse< T >::addMatrix ( const T &  s,
boost::shared_ptr< MatrixSparse< T > > &  m 
)
inline

Add a Sparse matrix _X, scaled with _a, to this, stores the result in this: $\texttt{this} = \_a*\_X + \texttt{this} $.

References Feel::MatrixSparse< T >::addMatrix().

template<typename T>
virtual void Feel::MatrixSparse< T >::clear ( )
pure virtual

Release all memory and return to a state just like after having called the default constructor.

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::close ( ) const
pure virtual

Call the Sparse assemble routines. sends necessary messages to other processors

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual bool Feel::MatrixSparse< T >::closed ( ) const
pure virtual

see if Sparse matrix has been closed and fully assembled yet

Implemented in Feel::MatrixBlockBase< T >, and Feel::MatrixGmm< T, LayoutType >.

template<typename T>
WorldComm const& Feel::MatrixSparse< T >::comm ( ) const
inline
Returns:
the communicator

References Feel::MatrixSparse< T >::M_worldComm.

template<typename T>
virtual void Feel::MatrixSparse< T >::createSubmatrix ( MatrixSparse< T > &  submatrix,
const std::vector< size_type > &  rows,
const std::vector< size_type > &  cols 
) const
inlinevirtual

This function creates a matrix called "submatrix" which is defined by the row and column indices given in the "rows" and "cols" entries. Currently this operation is only defined for the PetscMatrix type.

Reimplemented in Feel::MatrixBlockBase< T >.

References Feel::MatrixSparse< T >::_get_submatrix().

template<typename T>
virtual void Feel::MatrixSparse< T >::diagonal ( Vector< T > &  dest) const
pure virtual

Copies the diagonal part of the matrix into dest.

Implemented in Feel::MatrixBlockBase< T >.

template<typename T>
void Feel::MatrixSparse< T >::diagonal ( boost::shared_ptr< Vector< T > > &  dest) const
inline

Copies the diagonal part of the matrix into dest.

References Feel::MatrixSparse< T >::diagonal().

Referenced by Feel::MatrixSparse< T >::diagonal().

template<typename T>
virtual real_type Feel::MatrixSparse< T >::energy ( vector_type const &  v,
vector_type const &  u,
bool  transpose = false 
) const
pure virtual

compute the A scalar product $v^T A u$

Parameters:
ua vector
va vector
transposetrue to compute $v^T A^T u$
Returns:
the energy $v^T A u$

Referenced by Feel::MatrixSparse< T >::energy().

template<typename T>
real_type Feel::MatrixSparse< T >::energy ( vector_ptrtype const &  v,
vector_ptrtype const &  u,
bool  _transpose = false 
) const
inline

Compute the scalar product $(Au, v)= v^T A u$

Parameters:
ua vector
va vector
transposetrue to compute $v^T A^T u$ instead, false otherwise
Returns:
the energy $v^T A u$

References Feel::MatrixSparse< T >::energy().

template<typename T>
graph_ptrtype const& Feel::MatrixSparse< T >::graph ( ) const
inline
Returns:
the graph associated to the sparse matrix

Referenced by Feel::MatrixSparse< T >::setGraph().

template<typename T>
bool Feel::MatrixSparse< T >::hasGraph ( ) const
inline
Returns:
true if matrix has a graph, false otherwise
template<typename T>
virtual void Feel::MatrixSparse< T >::init ( const size_type  m,
const size_type  n,
const size_type  m_l,
const size_type  n_l,
const size_type  nnz = 30,
const size_type  noz = 10 
)
pure virtual

Initialize a Sparse matrix that is of global dimension $ m \times n $ with local dimensions $ m_l \times n_l $. nnz is the number of on-processor nonzeros per row (defaults to 30). noz is the number of on-processor nonzeros per row (defaults to 10).

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::init ( const size_type  m,
const size_type  n,
const size_type  m_l,
const size_type  n_l,
graph_ptrtype const &  graph 
)
pure virtual

Initialize using sparsity structure computed by dof_map.

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
bool Feel::MatrixSparse< T >::isHermitian ( ) const
inline
Returns:
true if matrix is hermitian, false otherwise

References Feel::HERMITIAN.

template<typename T>
bool Feel::MatrixSparse< T >::isHermitianPositiveDefinite ( ) const
inline
Returns:
true if matrix is positive definite, false otherwise

References Feel::HERMITIAN.

template<typename T>
virtual bool Feel::MatrixSparse< T >::isInitialized ( ) const
inlinevirtual
Returns:
true if the matrix has been initialized, false otherwise.

Reimplemented in Feel::MatrixGmm< T, LayoutType >.

References Feel::MatrixSparse< T >::_M_is_initialized.

template<typename T>
bool Feel::MatrixSparse< T >::isNonHermitian ( ) const
inline
Returns:
true if matrix is non hermitian, false otherwise

References Feel::NON_HERMITIAN.

template<typename T>
bool Feel::MatrixSparse< T >::isPositiveDefinite ( ) const
inline
Returns:
true if matrix is singular, false otherwise
template<typename T>
bool Feel::MatrixSparse< T >::isSingular ( ) const
inline
Returns:
true if matrix is singular, false otherwise
template<typename T>
virtual real_type Feel::MatrixSparse< T >::l1Norm ( ) const
pure virtual

Return the l1-norm of the matrix, that is $|M|_1=max_{all columns j}\sum_{all rows i} |M_ij|$, (max. sum of columns).

This is the natural matrix norm that is compatible to the l1-norm for vectors, i.e. $|Mv|_1\leq |M|_1 |v|_1$.

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual real_type Feel::MatrixSparse< T >::linftyNorm ( ) const
pure virtual

Return the linfty-norm of the matrix, that is

$|M|_\infty=max_{all rows i}\sum_{all columns j} |M_ij|$,

(max. sum of rows). This is the natural matrix norm that is compatible to the linfty-norm of vectors, i.e. $|Mv|_\infty \leq |M|_\infty |v|_\infty$.

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
DataMap const& Feel::MatrixSparse< T >::mapCol ( ) const
inline

Return datamap for cols

template<typename T>
DataMap const& Feel::MatrixSparse< T >::mapRow ( ) const
inline

Return datamap for rows

References Feel::MatrixSparse< T >::M_mapRow.

template<typename T >
void Feel::MatrixSparse< T >::multAddVector ( const Vector< T > &  arg,
Vector< T > &  dest 
) const

Multiplies the matrix with arg and adds the result to dest.

References Feel::Vector< T >::addVector().

template<typename T >
void Feel::MatrixSparse< T >::multVector ( const Vector< T > &  arg,
Vector< T > &  dest 
) const

Multiplies the matrix with arg and stores the result in dest.

References Feel::Vector< T >::zero().

Referenced by Feel::MatrixSparse< T >::multVector().

template<typename T>
void Feel::MatrixSparse< T >::multVector ( const boost::shared_ptr< Vector< T > > &  arg,
boost::shared_ptr< Vector< T > > &  dest 
) const
inline

Multiplies the matrix with arg and stores the result in dest.

References Feel::MatrixSparse< T >::multVector().

template<typename T>
virtual T Feel::MatrixSparse< T >::operator() ( const size_type  i,
const size_type  j 
) const
pure virtual

Return the value of the entry (i,j). This may be an expensive operation and you should always take care where to call this function. In order to avoid abuse, this function throws an exception if the required element does not exist in the matrix.

In case you want a function that returns zero instead (for entries that are not in the sparsity pattern of the matrix), use the el function.

Implemented in Feel::MatrixBlockBase< T >, and Feel::MatrixGmm< T, LayoutType >.

template<typename T >
void Feel::MatrixSparse< T >::print ( std::ostream &  os = std::cout) const
inline

Print the contents of the matrix to the screen in a uniform style, regardless of matrix/solver package being used.

Reimplemented in Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::printMatlab ( const std::string  name = "NULL") const
inlinevirtual

Print the contents of the matrix in Matlab's sparse matrix format. Optionally prints the matrix to the file named name. If name is not specified it is dumped to the screen.

Reimplemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::printPersonal ( std::ostream &  = std::cout) const
inlinevirtual

Print the contents of the matrix to the screen in a package-personalized style, if available.

Reimplemented in Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::reinitSubmatrix ( MatrixSparse< T > &  submatrix,
const std::vector< size_type > &  rows,
const std::vector< size_type > &  cols 
) const
inlinevirtual

This function is similar to the one above, but it allows you to reuse the existing sparsity pattern of "submatrix" instead of reallocating it again. This should hopefully be more efficient if you are frequently extracting submatrices of the same size.

Reimplemented in Feel::MatrixBlockBase< T >.

References Feel::MatrixSparse< T >::_get_submatrix().

template<typename T>
virtual size_type Feel::MatrixSparse< T >::rowStart ( ) const
pure virtual

return row_start, the index of the first matrix row stored on this processor

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual size_type Feel::MatrixSparse< T >::rowStop ( ) const
pure virtual

return row_stop, the index of the last matrix row (+1) stored on this processor

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::set ( const size_type  i,
const size_type  j,
const value_type &  value 
)
pure virtual

Set the element (i,j) to value. Throws an error if the entry does not exist. Still, it is allowed to store zero values in non-existent fields.

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
void Feel::MatrixSparse< T >::setGraph ( graph_ptrtype const &  graph)
inline

set the graph associated to the sparse matrix

References Feel::MatrixSparse< T >::graph().

template<typename T>
void Feel::MatrixSparse< T >::setInitialized ( bool  _init)
inline

set initialized only for subclasses

References Feel::MatrixSparse< T >::_M_is_initialized.

template<typename T>
void Feel::MatrixSparse< T >::setMatrixProperties ( size_type  p)
inline

set matrix properties,

See also:
MatrixProperties
template<typename T>
virtual size_type Feel::MatrixSparse< T >::size1 ( ) const
pure virtual
Returns:
m, the row-dimension of the matrix where the marix is $ M \times N $.

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual size_type Feel::MatrixSparse< T >::size2 ( ) const
pure virtual
Returns:
n, the column-dimension of the matrix where the marix is $ M \times N $.

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::symmetricPart ( MatrixSparse< value_type > &  Ms) const
inlinevirtual

Returns the symmetric part of the matrix

Referenced by Feel::MatrixSparse< T >::symmetricPart().

template<typename T>
void Feel::MatrixSparse< T >::symmetricPart ( boost::shared_ptr< MatrixSparse< value_type > > &  Ms) const
inline

Returns the symmetric part of the matrix

References Feel::MatrixSparse< T >::symmetricPart().

template<typename T>
virtual void Feel::MatrixSparse< T >::transpose ( MatrixSparse< value_type > &  Mt) const
pure virtual

Returns the transpose of a matrix

Parameters:
Mtthe matrix transposed

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
boost::shared_ptr<MatrixSparse<T> > Feel::MatrixSparse< T >::transpose ( ) const
inline
Returns:
the transpose of the matrix

Referenced by Feel::MatrixSparse< T >::transpose().

template<typename T>
void Feel::MatrixSparse< T >::transpose ( boost::shared_ptr< MatrixSparse< value_type > > &  Mt) const
inline

Returns the transpose of a matrix

Parameters:
Mthe matrix to transpose
Mtthe matrix transposed

References Feel::MatrixSparse< T >::transpose().

template<typename T>
virtual void Feel::MatrixSparse< T >::updateBlockMat ( boost::shared_ptr< MatrixSparse< T > >  m,
size_type  start_i,
size_type  start_j 
)
pure virtual

update a block matrix

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::updateSparsityPattern ( const std::vector< std::vector< size_type > > &  )
inlinevirtual

Updates the matrix sparsity pattern. When your MatrixSparse<T> implementation does not need this data simply do not overload this method.

template<typename T>
virtual void Feel::MatrixSparse< T >::zero ( )
pure virtual

Set all entries to 0.

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::zero ( size_type  start1,
size_type  size1,
size_type  start2,
size_type  size2 
)
pure virtual

Set entries between to 0.

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.

template<typename T>
virtual void Feel::MatrixSparse< T >::zeroRows ( std::vector< int > const &  rows,
std::vector< value_type > const &  values,
Vector< value_type > &  rhs,
Context const &  on_context 
)
pure virtual

eliminate rows without change pattern, and put 1 on the diagonal entry

Warning:
if the matrix was symmetric before this operation, it won't be afterwards. So use the proper solver (nonsymmetric)

Implemented in Feel::MatrixGmm< T, LayoutType >, and Feel::MatrixBlockBase< T >.


Friends And Related Function Documentation

template<typename T>
template<typename U >
std::ostream& operator<< ( std::ostream &  os,
const MatrixSparse< U > &  m 
)
friend

Same as the print method above, but allows you to print to a stream in the standard syntax.

Reimplemented in Feel::MatrixBlockBase< T >.


Member Data Documentation

template<typename T>
bool Feel::MatrixSparse< T >::_M_is_initialized
protected

Flag indicating whether or not the matrix has been initialized.

Referenced by Feel::MatrixSparse< T >::isInitialized(), and Feel::MatrixSparse< T >::setInitialized().

template<typename T>
DataMap Feel::MatrixSparse< T >::M_mapRow
protected

data distribution map of the vector over the processors

Referenced by Feel::MatrixSparse< T >::mapRow().