oomph::CRMatrix< T > Class Template Reference

#include <matrices.h>

+ Inheritance diagram for oomph::CRMatrix< T >:

Public Member Functions

 CRMatrix ()
 Default constructor. More...
 
 CRMatrix (const Vector< T > &value, const Vector< int > &column_index_, const Vector< int > &row_start_, const unsigned long &n, const unsigned long &m)
 
 CRMatrix (const CRMatrix &source_matrix)
 Copy constructor. More...
 
void operator= (const CRMatrix &)=delete
 Broken assignment operator. More...
 
virtual ~CRMatrix ()
 Destructor, delete any allocated memory. More...
 
T get_entry (const unsigned long &i, const unsigned long &j) const
 
Tentry (const unsigned long &i, const unsigned long &j)
 The read-write access function is deliberately broken. More...
 
introw_start ()
 Access to C-style row_start array. More...
 
const introw_start () const
 Access to C-style row_start array (const version) More...
 
intcolumn_index ()
 Access to C-style column index array. More...
 
const intcolumn_index () const
 Access to C-style column index array (const version) More...
 
void output_bottom_right_zero_helper (std::ostream &outfile) const
 
void sparse_indexed_output_helper (std::ostream &outfile) const
 
void clean_up_memory ()
 Wipe matrix data and set all values to 0. More...
 
void build (const Vector< T > &value, const Vector< int > &column_index, const Vector< int > &row_start, const unsigned long &n, const unsigned long &m)
 
void build_without_copy (T *value, int *column_index, int *row_start, const unsigned long &nnz, const unsigned long &n, const unsigned long &m)
 
- Public Member Functions inherited from oomph::SparseMatrix< T, CRMatrix< T > >
 SparseMatrix ()
 Default constructor. More...
 
 SparseMatrix (const SparseMatrix &source_matrix)
 Copy constructor. More...
 
void operator= (const SparseMatrix &)=delete
 Broken assignment operator. More...
 
virtual ~SparseMatrix ()
 Destructor, delete the memory associated with the values. More...
 
Tvalue ()
 Access to C-style value array. More...
 
const Tvalue () const
 Access to C-style value array (const version) More...
 
unsigned long nrow () const
 Return the number of rows of the matrix. More...
 
unsigned long ncol () const
 Return the number of columns of the matrix. More...
 
unsigned long nnz () const
 Return the number of nonzero entries. More...
 
- Public Member Functions inherited from oomph::Matrix< T, MATRIX_TYPE >
 Matrix ()
 (Empty) constructor More...
 
 Matrix (const Matrix &matrix)=delete
 Broken copy constructor. More...
 
void operator= (const Matrix &)=delete
 Broken assignment operator. More...
 
virtual ~Matrix ()
 Virtual (empty) destructor. More...
 
T operator() (const unsigned long &i, const unsigned long &j) const
 
Toperator() (const unsigned long &i, const unsigned long &j)
 
virtual void output (std::ostream &outfile) const
 
void sparse_indexed_output (std::ostream &outfile, const unsigned &precision=0, const bool &output_bottom_right_zero=false) const
 
void sparse_indexed_output (std::string filename, const unsigned &precision=0, const bool &output_bottom_right_zero=false) const
 

Protected Attributes

intColumn_index
 Column index. More...
 
intRow_start
 Start index for row. More...
 
- Protected Attributes inherited from oomph::SparseMatrix< T, CRMatrix< T > >
TValue
 Internal representation of the matrix values, a pointer. More...
 
unsigned long N
 Number of rows. More...
 
unsigned long M
 Number of columns. More...
 
unsigned long Nnz
 Number of non-zero values (i.e. size of Value array) More...
 

Additional Inherited Members

- Protected Member Functions inherited from oomph::Matrix< T, MATRIX_TYPE >
void range_check (const unsigned long &i, const unsigned long &j) const
 
- Static Protected Attributes inherited from oomph::SparseMatrix< T, CRMatrix< T > >
static T Zero
 Dummy zero. More...
 

Detailed Description

template<class T>
class oomph::CRMatrix< T >

A class for compressed row matrices, a sparse storage format Once again the recursive template trick is used to inform that base class that is should use the access functions provided in the CRMatrix class.

Constructor & Destructor Documentation

◆ CRMatrix() [1/3]

template<class T >
oomph::CRMatrix< T >::CRMatrix ( )
inline

Default constructor.

685  : SparseMatrix<T, CRMatrix<T>>()
686  {
687  Column_index = 0;
688  Row_start = 0;
689  }
int * Row_start
Start index for row.
Definition: matrices.h:873
int * Column_index
Column index.
Definition: matrices.h:870

References oomph::CRMatrix< T >::Column_index, and oomph::CRMatrix< T >::Row_start.

◆ CRMatrix() [2/3]

template<class T >
oomph::CRMatrix< T >::CRMatrix ( const Vector< T > &  value,
const Vector< int > &  column_index_,
const Vector< int > &  row_start_,
const unsigned long &  n,
const unsigned long &  m 
)
inline

Constructor: Pass vector of values, vector of column indices, vector of row starts and number of rows and columns Number of nonzero entries is read off from value, so make sure the vector has been shrunk to its correct length.

702  : SparseMatrix<T, CRMatrix<T>>()
703  {
704  Column_index = 0;
705  Row_start = 0;
706  build(value, column_index_, row_start_, n, m);
707  }
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
void build(const Vector< T > &value, const Vector< int > &column_index, const Vector< int > &row_start, const unsigned long &n, const unsigned long &m)
Definition: matrices.h:3404
T * value()
Access to C-style value array.
Definition: matrices.h:616
int * m
Definition: level2_cplx_impl.h:294

References oomph::CRMatrix< T >::build(), oomph::CRMatrix< T >::Column_index, m, n, oomph::CRMatrix< T >::Row_start, and oomph::SparseMatrix< T, CRMatrix< T > >::value().

◆ CRMatrix() [3/3]

template<class T >
oomph::CRMatrix< T >::CRMatrix ( const CRMatrix< T > &  source_matrix)
inline

Copy constructor.

711  : SparseMatrix<T, CRMatrix<T>>(source_matrix)
712  {
713  // NNz, N and M are set the the copy constructor of the SparseMatrix
714  // called above
715  // Column indices stored in C-style array
716  Column_index = new int[this->Nnz];
717 
718  // Assign:
719  for (unsigned long i = 0; i < this->Nnz; i++)
720  {
721  Column_index[i] = source_matrix.column_index()[i];
722  }
723 
724  // Row start:
725  Row_start = new int[this->N + 1];
726 
727  // Assign:
728  for (unsigned long i = 0; i <= this->N; i++)
729  {
730  Row_start[i] = source_matrix.row_start()[i];
731  }
732  }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
unsigned long Nnz
Number of non-zero values (i.e. size of Value array)
Definition: matrices.h:574
unsigned long N
Number of rows.
Definition: matrices.h:568

References oomph::CRMatrix< T >::column_index(), oomph::CRMatrix< T >::Column_index, i, oomph::SparseMatrix< T, CRMatrix< T > >::N, oomph::SparseMatrix< T, CRMatrix< T > >::Nnz, oomph::CRMatrix< T >::row_start(), and oomph::CRMatrix< T >::Row_start.

◆ ~CRMatrix()

template<class T >
virtual oomph::CRMatrix< T >::~CRMatrix ( )
inlinevirtual

Destructor, delete any allocated memory.

739  {
740  delete[] Column_index;
741  Column_index = 0;
742  delete[] Row_start;
743  Row_start = 0;
744  }

References oomph::CRMatrix< T >::Column_index, and oomph::CRMatrix< T >::Row_start.

Member Function Documentation

◆ build()

template<class T >
void oomph::CRMatrix< T >::build ( const Vector< T > &  value,
const Vector< int > &  column_index_,
const Vector< int > &  row_start_,
const unsigned long &  n,
const unsigned long &  m 
)

Build matrix from compressed representation. Number of nonzero entries is read off from value, so make sure the vector has been shrunk to its correct length. This matrix forms the storage for CRDoubleMatrices which are distributable. The argument n should be the number of local rows. The argument m is the number of columns

Build matrix from compressed representation. Number of nonzero entries is read off from value, so make sure the vector has been shrunk to its correct length. The optional final parameter specifies the number of columns. If it is not specified the matrix is assumed to be quadratic.

3409  {
3410 #ifdef PARANOID
3411  if (value.size() != column_index_.size())
3412  {
3413  std::ostringstream error_message;
3414  error_message << "Must have the same number of values and column indices,"
3415  << "we have " << value.size() << " values and "
3416  << column_index_.size() << " column inidicies."
3417  << std::endl;
3418  throw OomphLibError(
3419  error_message.str(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
3420  }
3421 #endif
3422  // Number of nonzero entries
3423  this->Nnz = value.size();
3424 
3425  // Number of rows
3426  this->N = n;
3427 
3428  // Number of columns
3429  this->M = m;
3430 
3431  // We need to delete any previously allocated storage
3432  if (this->Value != 0)
3433  {
3434  delete[] this->Value;
3435  }
3436  if (this->Column_index != 0)
3437  {
3438  delete[] this->Column_index;
3439  }
3440  if (this->Row_start != 0)
3441  {
3442  delete[] this->Row_start;
3443  }
3444 
3445  // Values stored in C-style array
3446  this->Value = new T[this->Nnz];
3447 
3448  // Column indices stored in C-style array
3449  this->Column_index = new int[this->Nnz];
3450 
3451  // Assign:
3452  for (unsigned long i = 0; i < this->Nnz; i++)
3453  {
3454  this->Value[i] = value[i];
3455  this->Column_index[i] = column_index_[i];
3456  }
3457 
3458  // Row start:
3459  // Find the size and allocate
3460  unsigned long n_row_start = row_start_.size();
3461  this->Row_start = new int[n_row_start];
3462 
3463  // Assign:
3464  for (unsigned long i = 0; i < n_row_start; i++)
3465  {
3466  this->Row_start[i] = row_start_[i];
3467  }
3468  }
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
T * Value
Internal representation of the matrix values, a pointer.
Definition: matrices.h:565
#define OOMPH_EXCEPTION_LOCATION
Definition: oomph_definitions.h:61
#define OOMPH_CURRENT_FUNCTION
Definition: oomph_definitions.h:86

References i, m, n, N, OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION, and Eigen::value.

Referenced by oomph::CRDoubleMatrix::build(), oomph::CRDoubleMatrix::CRDoubleMatrix(), and oomph::CRMatrix< T >::CRMatrix().

◆ build_without_copy()

template<class T >
void oomph::CRMatrix< T >::build_without_copy ( T value,
int column_index_,
int row_start_,
const unsigned long &  nnz,
const unsigned long &  n,
const unsigned long &  m 
)

Function to build matrix from pointers to arrays which hold the row starts, column indices and non-zero values. The final two arguments are the number of rows and columns. Note that, as the name suggests, this function does not make a copy of the data pointed to by the first three arguments!

Function to build a CRMatrix from pointers to arrays which hold the row starts, column indices and non-zero values Note that, as the name suggests, this function does not make a copy of the data pointed to by the first three arguments!

3360  {
3361  // Number of nonzero entries
3362  this->Nnz = nnz;
3363 
3364  // Number of rows
3365  this->N = n;
3366 
3367  // Number of columns
3368  this->M = m;
3369 
3370  // delete any previously allocated storage
3371  if (this->Value != 0)
3372  {
3373  delete[] this->Value;
3374  }
3375  if (this->Column_index != 0)
3376  {
3377  delete[] this->Column_index;
3378  }
3379  if (this->Row_start != 0)
3380  {
3381  delete[] this->Row_start;
3382  }
3383 
3384  // set Value
3385  this->Value = value;
3386 
3387  // set Column_index
3388  this->Column_index = column_index_;
3389 
3390  // set Row_start
3391  this->Row_start = row_start_;
3392  }
unsigned long nnz() const
Return the number of nonzero entries.
Definition: matrices.h:640

References m, n, N, and Eigen::value.

Referenced by oomph::CRDoubleMatrix::build_without_copy().

◆ clean_up_memory()

template<class T >
void oomph::CRMatrix< T >::clean_up_memory

Wipe matrix data and set all values to 0.

////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////

3324  {
3325  // delete any previously allocated storage
3326  if (this->Value != 0)
3327  {
3328  delete[] this->Value;
3329  this->Value = 0;
3330  }
3331  if (this->Column_index != 0)
3332  {
3333  delete[] this->Column_index;
3334  this->Column_index = 0;
3335  }
3336  if (this->Row_start != 0)
3337  {
3338  delete[] this->Row_start;
3339  this->Row_start = 0;
3340  }
3341  this->Nnz = 0;
3342  this->N = 0;
3343  this->M = 0;
3344  }

References N.

Referenced by oomph::CRDoubleMatrix::build(), oomph::CRDoubleMatrix::build_without_copy(), and oomph::CRDoubleMatrix::clear().

◆ column_index() [1/2]

◆ column_index() [2/2]

template<class T >
const int* oomph::CRMatrix< T >::column_index ( ) const
inline

Access to C-style column index array (const version)

804  {
805  return Column_index;
806  }

References oomph::CRMatrix< T >::Column_index.

◆ entry()

template<class T >
T& oomph::CRMatrix< T >::entry ( const unsigned long &  i,
const unsigned long &  j 
)
inline

The read-write access function is deliberately broken.

765  {
766  std::string error_string =
767  "Non-const access not provided for the CRMatrix<T> class\n";
768  error_string +=
769  "It is not possible to use round-bracket access: M(i,j)\n";
770  error_string += "if M is not declared as const.\n";
771  error_string += "The solution (albeit ugly) is to create const reference "
772  "to the matrix\n";
773  error_string += " const CRMatrix<T>& read_M = M;\n";
774  error_string += "Then read_M(i,j) is permitted\n";
775 
776  throw OomphLibError(
778 
779  // Dummy return
780  T dummy;
781  return dummy;
782  }
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286

References OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION, and oomph::Global_string_for_annotation::string().

◆ get_entry()

template<class T >
T oomph::CRMatrix< T >::get_entry ( const unsigned long &  i,
const unsigned long &  j 
) const
inline

Access function that will be called by the read-only round-bracket operator (const)

749  {
750 #ifdef RANGE_CHECKING
751  this->range_check(i, j);
752 #endif
753  for (long k = Row_start[i]; k < Row_start[i + 1]; k++)
754  {
755  if (unsigned(Column_index[k]) == j)
756  {
757  return this->Value[k];
758  }
759  }
760  return this->Zero;
761  }
void range_check(const unsigned long &i, const unsigned long &j) const
Definition: matrices.h:78
static T Zero
Dummy zero.
Definition: matrices.h:577
char char char int int * k
Definition: level2_impl.h:374
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References oomph::CRMatrix< T >::Column_index, i, j, k, oomph::Matrix< T, MATRIX_TYPE >::range_check(), oomph::CRMatrix< T >::Row_start, oomph::SparseMatrix< T, CRMatrix< T > >::Value, and oomph::SparseMatrix< T, CRMatrix< T > >::Zero.

Referenced by oomph::CRDoubleMatrix::diagonal_entries(), and oomph::CRDoubleMatrix::operator()().

◆ operator=()

template<class T >
void oomph::CRMatrix< T >::operator= ( const CRMatrix< T > &  )
delete

Broken assignment operator.

◆ output_bottom_right_zero_helper()

template<class T >
void oomph::CRMatrix< T >::output_bottom_right_zero_helper ( std::ostream &  outfile) const
inlinevirtual

Output the "bottom right" entry regardless of it being zero or not (this allows automatic detection of matrix size in e.g. matlab, python).

Reimplemented from oomph::SparseMatrix< T, CRMatrix< T > >.

812  {
813  int last_row_local = this->N - 1;
814  int last_col = this->M - 1;
815 
816  // Use this strange thingy because of the CRTP discussed above.
817  T last_value = this->operator()(last_row_local, last_col);
818 
819  if (last_value == T(0))
820  {
821  outfile << last_row_local << " " << last_col << " " << T(0)
822  << std::endl;
823  }
824  }
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
T operator()(const unsigned long &i, const unsigned long &j) const
Definition: matrices.h:128

References oomph::SparseMatrix< T, CRMatrix< T > >::N, and oomph::Matrix< T, MATRIX_TYPE >::operator()().

Referenced by oomph::CRDoubleMatrix::output_bottom_right_zero_helper().

◆ row_start() [1/2]

◆ row_start() [2/2]

template<class T >
const int* oomph::CRMatrix< T >::row_start ( ) const
inline

Access to C-style row_start array (const version)

792  {
793  return Row_start;
794  }

References oomph::CRMatrix< T >::Row_start.

◆ sparse_indexed_output_helper()

template<class T >
void oomph::CRMatrix< T >::sparse_indexed_output_helper ( std::ostream &  outfile) const
inlinevirtual

Indexed output function to print a matrix to the stream outfile as i,j,a(i,j) for a(i,j)!=0 only.

Reimplemented from oomph::SparseMatrix< T, CRMatrix< T > >.

829  {
830  for (unsigned long i = 0; i < this->N; i++)
831  {
832  for (long j = Row_start[i]; j < Row_start[i + 1]; j++)
833  {
834  outfile << i << " " << Column_index[j] << " " << this->Value[j]
835  << std::endl;
836  }
837  }
838  }

References oomph::CRMatrix< T >::Column_index, i, j, oomph::SparseMatrix< T, CRMatrix< T > >::N, oomph::CRMatrix< T >::Row_start, and oomph::SparseMatrix< T, CRMatrix< T > >::Value.

Referenced by oomph::CRDoubleMatrix::sparse_indexed_output_helper().

Member Data Documentation

◆ Column_index

◆ Row_start


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