MFEM v2.0
Public Member Functions | Private Member Functions | Private Attributes
SparseMatrix Class Reference

Data type sparse matrix. More...

#include <sparsemat.hpp>

Inheritance diagram for SparseMatrix:
Inheritance graph
[legend]
Collaboration diagram for SparseMatrix:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SparseMatrix (int nrows, int ncols=0)
 Creates sparse matrix.
 SparseMatrix (int *i, int *j, double *data, int m, int n)
int * GetI () const
 Return the array I.
int * GetJ () const
 Return the array J.
double * GetData () const
 Return element data.
int Width () const
 Return the number of columns.
int RowSize (int i)
 Returns the number of elements in row i.
virtual double & Elem (int i, int j)
 Returns reference to a_{ij}. Index i, j = 0 .. size-1.
virtual const double & Elem (int i, int j) const
 Returns constant reference to a_{ij}. Index i, j = 0 .. size-1.
double & operator() (int i, int j)
 Returns reference to A[i][j]. Index i, j = 0 .. size-1.
const double & operator() (int i, int j) const
 Returns reference to A[i][j]. Index i, j = 0 .. size-1.
virtual void Mult (const Vector &x, Vector &y) const
 Matrix vector multiplication.
void AddMult (const Vector &x, Vector &y, const double a=1.0) const
 y += A * x (default) or y += a * A * x
void MultTranspose (const Vector &x, Vector &y) const
 Multiply a vector with the transposed matrix. y = At * x.
void AddMultTranspose (const Vector &x, Vector &y, const double a=1.0) const
 y += At * x (default) or y += a * At * x
void PartMult (const Array< int > &rows, const Vector &x, Vector &y)
double InnerProduct (const Vector &x, const Vector &y) const
 Compute y^t A x.
void GetRowSums (Vector &x) const
virtual MatrixInverseInverse () const
 Returns a pointer to approximation of the matrix inverse.
void EliminateRow (int row, const double sol, Vector &rhs)
 Eliminates a column from the transpose matrix.
void EliminateRow (int row)
void EliminateCol (int col)
void EliminateCols (Array< int > &cols, Vector *x=NULL, Vector *b=NULL)
 Eliminate all columns 'i' for which cols[i] != 0.
void EliminateRowCol (int rc, const double sol, Vector &rhs, int d=0)
void EliminateRowColMultipleRHS (int rc, const Vector &sol, DenseMatrix &rhs, int d=0)
void EliminateRowCol (int rc, int d=0)
void EliminateRowCol (int rc, SparseMatrix &Ae, int d=0)
void SetDiagIdentity ()
 If a row contains only one diag entry of zero, set it to 1.
void EliminateZeroRows ()
 If a row contains only zeros, set its diagonal to 1.
void Gauss_Seidel_forw (const Vector &x, Vector &y) const
 Gauss-Seidel forward and backward iterations over a vector x.
void Gauss_Seidel_back (const Vector &x, Vector &y) const
double GetJacobiScaling () const
 Determine appropriate scaling for Jacobi iteration.
void Jacobi (const Vector &b, const Vector &x0, Vector &x1, double sc) const
void Jacobi2 (const Vector &b, const Vector &x0, Vector &x1, double sc=1.0) const
virtual void Finalize (int skip_zeros=1)
int Finalized ()
void GetBlocks (Array2D< SparseMatrix * > &blocks) const
void GetSubMatrix (const Array< int > &rows, const Array< int > &cols, DenseMatrix &subm)
void Set (const int i, const int j, const double a)
void Add (const int i, const int j, const double a)
void SetSubMatrix (const Array< int > &rows, const Array< int > &cols, const DenseMatrix &subm, int skip_zeros=1)
void SetSubMatrixTranspose (const Array< int > &rows, const Array< int > &cols, const DenseMatrix &subm, int skip_zeros=1)
void AddSubMatrix (const Array< int > &rows, const Array< int > &cols, const DenseMatrix &subm, int skip_zeros=1)
void AddRow (const int row, const Array< int > &cols, const Vector &srow)
void ScaleRow (const int row, const double scale)
SparseMatrixoperator+= (SparseMatrix &B)
SparseMatrixoperator= (double a)
void Print (ostream &out=cout, int width=4) const
 Prints matrix to stream out.
void PrintMatlab (ostream &out=cout) const
 Prints matrix in matlab format.
void PrintMM (ostream &out=cout) const
 Prints matrix in Matrix Market sparse format.
void PrintCSR (ostream &out) const
 Prints matrix to stream out in hypre_CSRMatrix format.
void PrintCSR2 (ostream &out) const
 Prints a sparse matrix to stream out in CSR format.
int Walk (int &i, int &j, double &a)
 Walks the sparse matrix.
double IsSymmetric () const
 Returns max_{i,j} |(i,j)-(j,i)| for a finalized matrix.
void Symmetrize ()
 (*this) = 1/2 ((*this) + (*this)^t)
int NumNonZeroElems () const
 Returns the number of the nonzero elements in the matrix.
double MaxNorm () const
int CountSmallElems (double tol)
 Count the number of entries with |a_ij| < tol.
void LoseData ()
 Call this if data has been stolen.
virtual ~SparseMatrix ()
 Destroys sparse matrix.

Private Member Functions

void SetColPtr (const int row)
void ClearColPtr ()
double & SearchRow (const int col)
void _Add_ (const int col, const double a)
void _Set_ (const int col, const double a)
double _Get_ (const int col)
double & SearchRow (const int row, const int col)
void _Add_ (const int row, const int col, const double a)
void _Set_ (const int row, const int col, const double a)

Private Attributes

int * I
int * J
int width
double * A
 The nonzero entries in the Sparse matrix with size I[size].
RowNode ** Rows
int current_row
union {
   int *   J
   RowNode **   Node
ColPtr

Detailed Description

Data type sparse matrix.

Definition at line 30 of file sparsemat.hpp.


Constructor & Destructor Documentation

SparseMatrix::SparseMatrix ( int  nrows,
int  ncols = 0 
) [explicit]

Creates sparse matrix.

Definition at line 21 of file sparsemat.cpp.

References A, ColPtr, I, J, Rows, and width.

Referenced by GetBlocks().

SparseMatrix::SparseMatrix ( int *  i,
int *  j,
double *  data,
int  m,
int  n 
) [inline]

Definition at line 70 of file sparsemat.hpp.

References ColPtr, and Rows.

SparseMatrix::~SparseMatrix ( ) [virtual]

Destroys sparse matrix.

Definition at line 1349 of file sparsemat.cpp.

References A, ColPtr, I, J, RowNode::Prev, Rows, and Operator::size.


Member Function Documentation

void SparseMatrix::_Add_ ( const int  col,
const double  a 
) [inline, private]

Definition at line 54 of file sparsemat.hpp.

References SearchRow().

Referenced by Add(), AddRow(), AddSubMatrix(), and operator+=().

void SparseMatrix::_Add_ ( const int  row,
const int  col,
const double  a 
) [inline, private]

Definition at line 61 of file sparsemat.hpp.

References SearchRow().

double SparseMatrix::_Get_ ( const int  col) [inline, private]

Definition at line 343 of file sparsemat.hpp.

References A, ColPtr, Rows, and RowNode::Value.

Referenced by GetSubMatrix().

void SparseMatrix::_Set_ ( const int  col,
const double  a 
) [inline, private]

Definition at line 56 of file sparsemat.hpp.

References SearchRow().

Referenced by Set(), SetSubMatrix(), and SetSubMatrixTranspose().

void SparseMatrix::_Set_ ( const int  row,
const int  col,
const double  a 
) [inline, private]

Definition at line 63 of file sparsemat.hpp.

References SearchRow().

void SparseMatrix::Add ( const int  i,
const int  j,
const double  a 
)

Definition at line 1056 of file sparsemat.cpp.

References _Add_(), A, mfem_error(), Operator::size, and width.

Referenced by EliminateRowCol().

void SparseMatrix::AddMult ( const Vector x,
Vector y,
const double  a = 1.0 
) const
void SparseMatrix::AddMultTranspose ( const Vector x,
Vector y,
const double  a = 1.0 
) const

y += At * x (default) or y += a * At * x

Definition at line 187 of file sparsemat.cpp.

References A, RowNode::Column, Vector::GetData(), I, J, mfem_error(), RowNode::Prev, Rows, Vector::Size(), Operator::size, RowNode::Value, and width.

Referenced by MultTranspose().

void SparseMatrix::AddRow ( const int  row,
const Array< int > &  cols,
const Vector srow 
)
void SparseMatrix::AddSubMatrix ( const Array< int > &  rows,
const Array< int > &  cols,
const DenseMatrix subm,
int  skip_zeros = 1 
)
void SparseMatrix::ClearColPtr ( ) [inline, private]
int SparseMatrix::CountSmallElems ( double  tol)

Count the number of entries with |a_ij| < tol.

Definition at line 447 of file sparsemat.cpp.

References A, I, RowNode::Prev, Rows, and Operator::size.

const double & SparseMatrix::Elem ( int  i,
int  j 
) const [virtual]

Returns constant reference to a_{ij}. Index i, j = 0 .. size-1.

Implements Matrix.

Definition at line 53 of file sparsemat.cpp.

References operator()().

double & SparseMatrix::Elem ( int  i,
int  j 
) [virtual]

Returns reference to a_{ij}. Index i, j = 0 .. size-1.

Implements Matrix.

Definition at line 48 of file sparsemat.cpp.

References operator()().

void SparseMatrix::EliminateCol ( int  col)

Definition at line 513 of file sparsemat.cpp.

References mfem_error(), RowNode::Prev, Rows, Operator::size, and RowNode::Value.

void SparseMatrix::EliminateCols ( Array< int > &  cols,
Vector x = NULL,
Vector b = NULL 
)

Eliminate all columns 'i' for which cols[i] != 0.

Definition at line 526 of file sparsemat.cpp.

References mfem_error(), RowNode::Prev, Rows, Operator::size, and RowNode::Value.

void SparseMatrix::EliminateRow ( int  row,
const double  sol,
Vector rhs 
)

Eliminates a column from the transpose matrix.

Definition at line 478 of file sparsemat.cpp.

References RowNode::Column, mfem_error(), RowNode::Prev, Rows, Operator::size, and RowNode::Value.

void SparseMatrix::EliminateRow ( int  row)

Definition at line 497 of file sparsemat.cpp.

References mfem_error(), RowNode::Prev, Rows, Operator::size, and RowNode::Value.

void SparseMatrix::EliminateRowCol ( int  rc,
int  d = 0 
)
void SparseMatrix::EliminateRowCol ( int  rc,
SparseMatrix Ae,
int  d = 0 
)

Definition at line 721 of file sparsemat.cpp.

References A, Add(), RowNode::Column, I, J, mfem_error(), RowNode::Prev, Rows, and RowNode::Value.

void SparseMatrix::EliminateRowCol ( int  rc,
const double  sol,
Vector rhs,
int  d = 0 
)

Eliminates the column 'rc' to the 'rhs', deletes the row 'rc' and replaces the element (rc,rc) with 1.0; assumes that element (i,rc) is assembled if and only if the element (rc,i) is assembled. If d != 0 then the element (rc,rc) remains the same.

Definition at line 543 of file sparsemat.cpp.

References A, I, J, mfem_error(), RowNode::Prev, Rows, and Operator::size.

void SparseMatrix::EliminateRowColMultipleRHS ( int  rc,
const Vector sol,
DenseMatrix rhs,
int  d = 0 
)

Like previous one, but multiple values for eliminated dofs are accepted, and accordingly multiple right-hand-sides are used.

Definition at line 609 of file sparsemat.cpp.

References A, I, J, mfem_error(), RowNode::Prev, Rows, Vector::Size(), Operator::size, and DenseMatrix::Width().

void SparseMatrix::EliminateZeroRows ( )

If a row contains only zeros, set its diagonal to 1.

Definition at line 795 of file sparsemat.cpp.

References A, I, J, and Operator::size.

void SparseMatrix::Finalize ( int  skip_zeros = 1) [virtual]

Finalize the matrix initialization. The function should be called only once, after the matrix has been initialized. Internally, this method converts the matrix from row-wise linked list format into CSR (compressed sparse row) format.

Reimplemented from Matrix.

Definition at line 276 of file sparsemat.cpp.

References A, ColPtr, RowNode::Column, I, J, RowNode::Prev, Rows, Operator::size, and RowNode::Value.

int SparseMatrix::Finalized ( ) [inline]

Definition at line 168 of file sparsemat.hpp.

References A.

void SparseMatrix::Gauss_Seidel_back ( const Vector x,
Vector y 
) const
void SparseMatrix::Gauss_Seidel_forw ( const Vector x,
Vector y 
) const

Gauss-Seidel forward and backward iterations over a vector x.

Definition at line 814 of file sparsemat.cpp.

References A, RowNode::Column, Vector::GetData(), I, J, mfem_error(), RowNode::Prev, Rows, Operator::size, and RowNode::Value.

void SparseMatrix::GetBlocks ( Array2D< SparseMatrix * > &  blocks) const

Split the matrix into M x N blocks of sparse matrices in CSR format. The 'blocks' array is M x N (i.e. M and N are determined by its dimensions) and its entries are overwritten by the new blocks.

Definition at line 323 of file sparsemat.cpp.

References A, I, J, mfem_error(), Array2D< T >::NumCols(), Array2D< T >::NumRows(), RowNode::Prev, Rows, Operator::size, SparseMatrix(), and width.

Referenced by MixedBilinearForm::GetBlocks().

double* SparseMatrix::GetData ( ) const [inline]

Return element data.

Definition at line 79 of file sparsemat.hpp.

References A.

Referenced by Mult(), SparseMatrixFunction(), and Transpose().

int* SparseMatrix::GetI ( ) const [inline]

Return the array I.

Definition at line 75 of file sparsemat.hpp.

References I.

Referenced by Mult(), and Transpose().

int* SparseMatrix::GetJ ( ) const [inline]

Return the array J.

Definition at line 77 of file sparsemat.hpp.

References J.

Referenced by Mult(), and Transpose().

double SparseMatrix::GetJacobiScaling ( ) const

Determine appropriate scaling for Jacobi iteration.

Definition at line 928 of file sparsemat.cpp.

References A, I, J, mfem_error(), norm(), and Operator::size.

void SparseMatrix::GetRowSums ( Vector x) const

Definition at line 261 of file sparsemat.cpp.

References A, I, RowNode::Prev, Rows, and Operator::size.

void SparseMatrix::GetSubMatrix ( const Array< int > &  rows,
const Array< int > &  cols,
DenseMatrix subm 
)
double SparseMatrix::InnerProduct ( const Vector x,
const Vector y 
) const

Compute y^t A x.

Definition at line 243 of file sparsemat.cpp.

References A, I, J, RowNode::Prev, Rows, and Operator::size.

Referenced by BilinearForm::FullInnerProduct(), and BilinearForm::InnerProduct().

MatrixInverse * SparseMatrix::Inverse ( ) const [virtual]

Returns a pointer to approximation of the matrix inverse.

Implements Matrix.

Definition at line 473 of file sparsemat.cpp.

double SparseMatrix::IsSymmetric ( ) const

Returns max_{i,j} |(i,j)-(j,i)| for a finalized matrix.

Definition at line 373 of file sparsemat.cpp.

References A, I, J, mfem_error(), and Operator::size.

void SparseMatrix::Jacobi ( const Vector b,
const Vector x0,
Vector x1,
double  sc 
) const

One scaled Jacobi iteration for the system A x = b. x1 = x0 + sc D^{-1} (b - A x0) where D is the diag of A.

Definition at line 956 of file sparsemat.cpp.

References A, I, J, mfem_error(), and Operator::size.

void SparseMatrix::Jacobi2 ( const Vector b,
const Vector x0,
Vector x1,
double  sc = 1.0 
) const

x1 = x0 + sc D^{-1} (b - A x0) where $ D_{ii} = \sum_j |A_{ij}| $.

Definition at line 980 of file sparsemat.cpp.

References A, I, J, mfem_error(), norm(), and Operator::size.

void SparseMatrix::LoseData ( ) [inline]

Call this if data has been stolen.

Definition at line 233 of file sparsemat.hpp.

References A, I, and J.

double SparseMatrix::MaxNorm ( ) const

Definition at line 428 of file sparsemat.cpp.

References A, I, RowNode::Prev, Rows, and Operator::size.

void SparseMatrix::Mult ( const Vector x,
Vector y 
) const [virtual]

Matrix vector multiplication.

Implements Operator.

Definition at line 99 of file sparsemat.cpp.

References AddMult().

Referenced by BilinearForm::FullMult().

void SparseMatrix::MultTranspose ( const Vector x,
Vector y 
) const [virtual]

Multiply a vector with the transposed matrix. y = At * x.

Reimplemented from Operator.

Definition at line 181 of file sparsemat.cpp.

References AddMultTranspose().

Referenced by Mesh::UpdateNodes().

int SparseMatrix::NumNonZeroElems ( ) const

Returns the number of the nonzero elements in the matrix.

Definition at line 410 of file sparsemat.cpp.

References A, I, RowNode::Prev, Rows, and Operator::size.

Referenced by PrintMM(), SparseMatrixFunction(), and Transpose().

double & SparseMatrix::operator() ( int  i,
int  j 
)

Returns reference to A[i][j]. Index i, j = 0 .. size-1.

Definition at line 58 of file sparsemat.cpp.

References A, I, J, mfem_error(), Operator::size, and width.

Referenced by Elem().

const double & SparseMatrix::operator() ( int  i,
int  j 
) const

Returns reference to A[i][j]. Index i, j = 0 .. size-1.

Definition at line 79 of file sparsemat.cpp.

References A, I, J, mfem_error(), Operator::size, and width.

SparseMatrix & SparseMatrix::operator+= ( SparseMatrix B)

Add a sparse matrix to "*this" sparse marix Both marices should not be finilized

Definition at line 1220 of file sparsemat.cpp.

References _Add_(), ClearColPtr(), RowNode::Column, mfem_error(), RowNode::Prev, Rows, SetColPtr(), Operator::size, RowNode::Value, and width.

SparseMatrix & SparseMatrix::operator= ( double  a)

Definition at line 1245 of file sparsemat.cpp.

References A, I, Rows, and Operator::size.

void SparseMatrix::PartMult ( const Array< int > &  rows,
const Vector x,
Vector y 
)

Definition at line 222 of file sparsemat.cpp.

References A, I, J, mfem_error(), and Array< T >::Size().

Referenced by BilinearForm::EliminateVDofsInRHS().

void SparseMatrix::Print ( ostream &  out = cout,
int  width = 4 
) const [virtual]

Prints matrix to stream out.

Reimplemented from Matrix.

Definition at line 1259 of file sparsemat.cpp.

References A, I, J, mfem_error(), and Operator::size.

void SparseMatrix::PrintCSR ( ostream &  out) const

Prints matrix to stream out in hypre_CSRMatrix format.

Definition at line 1310 of file sparsemat.cpp.

References A, I, J, mfem_error(), and Operator::size.

void SparseMatrix::PrintCSR2 ( ostream &  out) const

Prints a sparse matrix to stream out in CSR format.

Definition at line 1329 of file sparsemat.cpp.

References A, I, J, mfem_error(), Operator::size, and width.

void SparseMatrix::PrintMatlab ( ostream &  out = cout) const

Prints matrix in matlab format.

Definition at line 1280 of file sparsemat.cpp.

References A, I, J, and Operator::size.

void SparseMatrix::PrintMM ( ostream &  out = cout) const

Prints matrix in Matrix Market sparse format.

Definition at line 1293 of file sparsemat.cpp.

References A, I, J, NumNonZeroElems(), Operator::size, and width.

int SparseMatrix::RowSize ( int  i)

Returns the number of elements in row i.

Definition at line 35 of file sparsemat.cpp.

References I, RowNode::Prev, Rows, and RowNode::Value.

void SparseMatrix::ScaleRow ( const int  row,
const double  scale 
)

Definition at line 1198 of file sparsemat.cpp.

References A, I, and Rows.

double & SparseMatrix::SearchRow ( const int  row,
const int  col 
) [inline, private]

Definition at line 357 of file sparsemat.hpp.

References A, RowNode::Column, I, J, mfem_error(), RowNode::Prev, Rows, and RowNode::Value.

double & SparseMatrix::SearchRow ( const int  col) [inline, private]

Definition at line 315 of file sparsemat.hpp.

References A, ColPtr, RowNode::Column, current_row, mfem_error(), RowNode::Prev, Rows, and RowNode::Value.

Referenced by _Add_(), and _Set_().

void SparseMatrix::Set ( const int  i,
const int  j,
const double  a 
)

Definition at line 1037 of file sparsemat.cpp.

References _Set_(), A, mfem_error(), Operator::size, and width.

void SparseMatrix::SetColPtr ( const int  row) [inline, private]
void SparseMatrix::SetDiagIdentity ( )

If a row contains only one diag entry of zero, set it to 1.

Definition at line 788 of file sparsemat.cpp.

References A, I, and Operator::size.

void SparseMatrix::SetSubMatrix ( const Array< int > &  rows,
const Array< int > &  cols,
const DenseMatrix subm,
int  skip_zeros = 1 
)
void SparseMatrix::SetSubMatrixTranspose ( const Array< int > &  rows,
const Array< int > &  cols,
const DenseMatrix subm,
int  skip_zeros = 1 
)
void SparseMatrix::Symmetrize ( )

(*this) = 1/2 ((*this) + (*this)^t)

Definition at line 394 of file sparsemat.cpp.

References A, I, J, mfem_error(), and Operator::size.

int SparseMatrix::Walk ( int &  i,
int &  j,
double &  a 
)

Walks the sparse matrix.

int SparseMatrix::Width ( ) const [inline]

Return the number of columns.

Definition at line 81 of file sparsemat.hpp.

References width.

Referenced by Mult(), and Transpose().


Member Data Documentation

double* SparseMatrix::A [private]
union { ... } SparseMatrix::ColPtr [private]

Definition at line 44 of file sparsemat.hpp.

Referenced by ClearColPtr(), SearchRow(), and SetColPtr().

int* SparseMatrix::I [private]
int* SparseMatrix::J [private]

Definition at line 45 of file sparsemat.hpp.

int SparseMatrix::width [private]

The documentation for this class was generated from the following files:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines