Sparse matrices


A sparse matrix is a matrix which number of non-zero terms is very small compared to its size. It presents some great advantages, like reducing the memory space required to store it and reducing the global number of arithmetic operations used for computations of the product matrix-vector for instance. Nowdays, many numerical and efficient algorithms are based on sparse linear algebra (PDE, Graph theory,..., see [Saad96]). In such applications it is not rare to treat (sparse) matrices of size 104 × 104 or even 105 × 105, which could not be managed as dense matrices.



1  Implementation

linalg/MatSparse.H

template< class R>  struct MatSparse
Sparse matrices.
{
  R rep;

  typedef typename R::size_type  size_type;
  typedef typename R::value_type value_type;
  typedef MatSparse          self_t;

  MatSparse() {}
  MatSparse(const R  & r): rep(r)               {}
  MatSparse(int m, int n, int nz) : rep(n,m,nz) {}
  MatSparse(int m, int n, int nz, value_type* t, size_type* idx): 
    rep(m,n,nz,t,idx) {}

  MatSparse(const char * str): rep(str)         {}
  MatSparse(const self_t & M): rep(M.rep)       {}
  template 
  MatSparse(const VAL & M) {assign(*this,M.rep);} 

  self_t &  operator=(const self_t & M)
    {rep=M.rep; return *this;} 
  template
  self_t & operator=(const VAL & M) {assign(*this,M.rep); return *this;} 

  self_t & transpose () {rep.transpose(); return *this;}

  size_type nbrow() const {return rep.nbrow();}
  size_type nbcol() const {return rep.nbcol();}
  size_type nzero() const {return rep.nzero();}

  value_type operator() (size_type i, size_type j) const {return rep(i,j);} 

  self_t & operator*=(const value_type & c);
  self_t & operator/=(const value_type & c);
};


template< class R>  inline void WriteToHSL(const MatSparse< R>  & M, const char *file_to_write)
Write a Sparse Matrix M in Harwell-Boeing Format in the file file_to_write.

template< class R>  inline void ReadFromHSL(MatSparse< R>  & M, const char *file_to_write)
Read a Sparse Matrix M in Harwell-Boeing Format from the file file_to_write.

template< class R>  ostream & operator< < (ostream & os, const MatSparse< R>  & p)
Output operator for standard matrices.

linalg/MatSparse.C


2  Containers for sparse matrices

The container R should provide the following methods or definitions:
 typedef typename R::size_type;
 typedef typename R::value_type;

 size_type R::nbrow();
 size_type R::nbcol();
 size_type R::nbnz();

       value_typeR::operator()(size_type,size_type);
 const value_typeR::operator()(size_type,size_type) const;

 void   R::transpose();

3  A standard container for sparse matrices

John-John Ili & Cédric Peyruqeou

linalg/sparse2d.H

template< class C>  struct sparse2d: public array1d< C> 




4  Container for the connection with SuperLU

Ph. Trebuchet

linalg/superlu.H

template< class C>  struct superlu : public SuperMatrix
Container for the SuperLU package [SuperLU97], which will be called to solve a linear system by LU-factorisation, as illustrated in the following example:
<B><PRE>
   MatSparse$<$ superlu$<$ double$>$  A(...) 
   VectStd$<$ array1d$<$ double$>$  V(...), W 
   W=Solve(A,V,LU()) 
</PRE></B>
{
  typedef unsigned int  size_type;
  typedef double        value_type;
  superlu();
  superlu(const int nrow, const int ncol);
  superlu(const int nrow, const int ncol, const int nz);
  superlu(size_type nrow, size_type ncol, size_type nz,
	  value_type* coeffs, size_type *idx);

  ~superlu();

  double & operator() (const int & i,const int & j,const double &d) ;
  double & operator() (const int &i ,const int &j);
  double   operator() (const int &i ,const int &j) const ;
  
  inline void destroystore();
  size_type nbrow() const {return nrow;}
  size_type nbcol() const {return ncol;}
  void reserve(size_type, size_type, size_type);
};