SparseMatrix Class Reference

#include <SparseMatrix.hpp>

Inheritance diagram for SparseMatrix:

Inheritance graph
[legend]
Collaboration diagram for SparseMatrix:

Collaboration graph
[legend]

List of all members.

Public Types

enum  Type { doubleHashedMatrix, sparseMatrix, unAssembled }

Public Member Functions

void reset ()
void getDiagonal (BaseVector &X) const
 used to get the diagonal of the Matrix and stores it in the vector X.
void timesX (const BaseVector &X, BaseVector &Z) const
 Computes z = A*x.
const real_t & operator() (const size_t &i, const size_t &j) const
 Read-only access to the SparseElem (i, j) of the matrix.
const SparseLineline (const size_t i) const
 Read-only access to the SparseLine i.
real_t & operator() (const size_t &i, const size_t &j)
 Access to the SparseElem (i, j) of the matrix.
const Vector< real_t > & operator* (const Vector< real_t > &V) const
 matrix vector product.
void transposedTimesX (const BaseVector &X, BaseVector &Z) const
 matrix vector product.
void copyProfile (const SparseMatrix &A)
 SparseMatrix (const DoubleHashedMatrix &A)
 SparseMatrix ()
 ~SparseMatrix ()
 Destructor.
const size_t & size () const
const BaseMatrix::Typetype () const

Protected Attributes

BaseMatrix::Type __type
size_t __size

Private Attributes

SparseLine__line
 SparseLines list.
const real_t __zero
Vector< real_t > __Au


Detailed Description

This class defines a sparse matrix.

Todo:
rework the impementation and add other types of matrices, and specializations such as symetric, ...
Author:
Stéphane Del Pino.

Definition at line 186 of file SparseMatrix.hpp.


Member Enumeration Documentation

enum BaseMatrix::Type [inherited]

Enumerator:
doubleHashedMatrix 
sparseMatrix 
unAssembled 

Definition at line 30 of file BaseMatrix.hpp.

00030             {
00031     doubleHashedMatrix,
00032     sparseMatrix,
00033 #ifdef    HAVE_PETSC
00034     petscMatrix,
00035 #endif // HAVE_PETSC
00036     unAssembled
00037   };


Constructor & Destructor Documentation

SparseMatrix::SparseMatrix ( const DoubleHashedMatrix A  )  [inline]

Definition at line 335 of file SparseMatrix.hpp.

References __line, BaseMatrix::__size, DoubleHashedMatrix::beginOfLine(), DoubleHashedMatrix::endOfLine(), DoubleHashedMatrix::numberOfLineNonNull(), and SparseLine::resize().

00336     : BaseMatrix(BaseMatrix::sparseMatrix, A.numberOfLines()),
00337       __zero(0),
00338       __Au(A.numberOfLines())
00339   {
00340     __line = new SparseLine[__size];
00341     for (size_t i=0; i<__size; ++i) {
00342       __line[i].resize(A.numberOfLineNonNull(i));
00343 
00344       for (DoubleHashedMatrix::const_iterator browsLine = A.beginOfLine(i);
00345            browsLine != A.endOfLine(i); ++browsLine) {
00346         (*this)(i,browsLine.first()) = browsLine.second();
00347       }
00348     }    
00349   };

Here is the call graph for this function:

SparseMatrix::SparseMatrix (  )  [inline]

Definition at line 351 of file SparseMatrix.hpp.

00352     : BaseMatrix(BaseMatrix::sparseMatrix),
00353       __zero(0)
00354   {
00355     ;
00356   };

SparseMatrix::~SparseMatrix (  )  [inline]

Destructor.

Definition at line 359 of file SparseMatrix.hpp.

References __line.

00360   {
00361      delete[] __line;
00362   }


Member Function Documentation

void SparseMatrix::reset (  )  [inline, virtual]

Sets the matrix to zero

Implements BaseMatrix.

Definition at line 201 of file SparseMatrix.hpp.

References __line, BaseMatrix::__size, and SparseLine::size().

00202   {
00203     for (size_t i=0; i<__size; ++i) {
00204       SparseLine& SL = __line[i];
00205       for (size_t j=0; j<SL.size(); ++j) {
00206         SL[j].value() = 0;
00207       }
00208     }
00209   }

Here is the call graph for this function:

void SparseMatrix::getDiagonal ( BaseVector X  )  const [inline, virtual]

used to get the diagonal of the Matrix and stores it in the vector X.

Implements BaseMatrix.

Definition at line 211 of file SparseMatrix.hpp.

References BaseMatrix::size().

00212   {
00213     Vector<real_t>& x = dynamic_cast<Vector<real_t>&>(X);
00214     for (size_t i=0; i<this->size();++i)
00215       x[i] = (*this)(i,i);
00216   }

Here is the call graph for this function:

void SparseMatrix::timesX ( const BaseVector X,
BaseVector Z 
) const [inline, virtual]

Computes z = A*x.

Implements BaseMatrix.

Definition at line 218 of file SparseMatrix.hpp.

00220   {
00221     const Vector<real_t>& x = dynamic_cast<const Vector<real_t>&>(X);
00222     Vector<real_t>& z = dynamic_cast<Vector<real_t>&>(Z);
00223     z = (*this)*x;
00224   }

const real_t& SparseMatrix::operator() ( const size_t &  i,
const size_t &  j 
) const [inline]

Read-only access to the SparseElem (i, j) of the matrix.

Definition at line 227 of file SparseMatrix.hpp.

References __line, __zero, BaseMatrix::size(), and SparseLine::size().

00229   {
00230     SparseLine& SL = __line[i];
00231     size_t icol;
00232 
00233     for (icol=0; icol<__line[i].size(); ++icol) {
00234       const int& column = SL[icol].column();
00235       if (column == (int)j)
00236         break;
00237       else
00238         if (column == -1) {
00239           break;
00240         }
00241     }
00242     if (icol == __line[i].size()) {             // means the element was not found
00243       return __zero;
00244     }
00245     return (SL[icol].value());
00246   }

Here is the call graph for this function:

const SparseLine& SparseMatrix::line ( const size_t  i  )  const [inline]

Read-only access to the SparseLine i.

Definition at line 249 of file SparseMatrix.hpp.

References __line, BaseMatrix::__size, and ASSERT.

Referenced by IncompleteCholeskiFactorization::computes(), copyProfile(), and IncompleteCholeskiFactorization::initializes().

00250   {
00251     ASSERT(i<__size);
00252     return __line[i];
00253   }

real_t& SparseMatrix::operator() ( const size_t &  i,
const size_t &  j 
) [inline]

Access to the SparseElem (i, j) of the matrix.

Definition at line 256 of file SparseMatrix.hpp.

References __line, BaseMatrix::size(), SparseLine::size(), stringify(), and ErrorHandler::unexpected.

00258   {
00259     SparseLine& SL = __line[i];
00260     size_t icol;
00261 
00262     for (icol=0; icol<__line[i].size(); ++icol) {
00263       const int& column = SL[icol].column();
00264       if (column == (int)j)
00265         break;
00266       else
00267         if (column == -1) {
00268           SL[icol].column() = j;
00269           break;
00270         }
00271     }
00272     if (icol == __line[i].size()) {             // means the element was not found
00273       std::string errorMsg
00274         = "cannot access element ("+stringify(i)+","+stringify(j)+")\n";
00275 
00276       throw ErrorHandler(__FILE__,__LINE__,
00277                          errorMsg,
00278                          ErrorHandler::unexpected);
00279     }
00280     return (SL[icol].value());
00281   }

Here is the call graph for this function:

const Vector<real_t>& SparseMatrix::operator* ( const Vector< real_t > &  V  )  const [inline]

matrix vector product.

Definition at line 284 of file SparseMatrix.hpp.

References __Au, __line, BaseMatrix::__size, and SparseLine::size().

00285   {
00286     __Au = 0;
00287 
00288     for (size_t i=0; i<__size; ++i) {
00289       const SparseLine& SL = __line[i];
00290       for (size_t j=0; j<SL.size(); ++j) {
00291         const int& k = SL[j].column();
00292         if (k!=-1)
00293           __Au[i] += SL[j].value()*V[k];
00294         else
00295           break;
00296       }
00297     }
00298     return __Au;
00299   }

Here is the call graph for this function:

void SparseMatrix::transposedTimesX ( const BaseVector X,
BaseVector Z 
) const [inline, virtual]

matrix vector product.

Implements BaseMatrix.

Definition at line 302 of file SparseMatrix.hpp.

References __line, BaseMatrix::__size, ASSERT, SparseLine::size(), BaseMatrix::size(), and Vector< T >::size().

00303   {
00304     const Vector<real_t>& x = dynamic_cast<const Vector<real_t>&>(X);
00305     Vector<real_t>& z = dynamic_cast<Vector<real_t>&>(Z);
00306     ASSERT(x.size()==z.size());
00307     ASSERT(this->size()==x.size());
00308     z = 0;
00309 
00310     for (size_t i=0; i<__size; ++i) {
00311       const SparseLine& SL = __line[i];
00312       for (size_t j=0; j<SL.size(); ++j) {
00313         const int& k = SL[j].column();
00314         if (k!=-1)
00315           z[k] += SL[j].value()*x[i];
00316         else
00317           break;
00318       }
00319     }
00320   }

Here is the call graph for this function:

void SparseMatrix::copyProfile ( const SparseMatrix A  )  [inline]

Definition at line 322 of file SparseMatrix.hpp.

References __Au, __line, BaseMatrix::__size, line(), SparseLine::resize(), Vector< T >::resize(), SparseLine::size(), and BaseMatrix::size().

00323   {
00324     __size = A.size();
00325     __Au.resize(__size);
00326 
00327     __line = new SparseLine[__size];
00328     for (size_t i=0; i<__size; ++i) {
00329       const SparseLine& Aline = A.line(i);
00330       const size_t s = Aline.size();
00331       __line[i].resize(s);
00332     }
00333   }

Here is the call graph for this function:

const size_t& BaseMatrix::size (  )  const [inline, inherited]

const BaseMatrix::Type& BaseMatrix::type (  )  const [inline, inherited]


Member Data Documentation

SparseLines list.

Definition at line 191 of file SparseMatrix.hpp.

Referenced by copyProfile(), line(), operator()(), operator*(), reset(), SparseMatrix(), transposedTimesX(), and ~SparseMatrix().

const real_t SparseMatrix::__zero [private]

Definition at line 193 of file SparseMatrix.hpp.

Referenced by operator()().

Vector<real_t> SparseMatrix::__Au [mutable, private]

Contains the result of Matrix Vector Product, this trick avoids the cost of memory management during conjugate gradient and same methods.

Definition at line 198 of file SparseMatrix.hpp.

Referenced by copyProfile(), and operator*().

BaseMatrix::Type BaseMatrix::__type [protected, inherited]

Definition at line 40 of file BaseMatrix.hpp.

Referenced by BaseMatrix::type().

size_t BaseMatrix::__size [protected, inherited]


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

Generated on Wed Nov 19 00:14:26 2008 for FreeFEM3D (aka ff3d) by  doxygen 1.5.6