oomph::Matrix< T, MATRIX_TYPE > Class Template Referenceabstract

#include <matrices.h>

+ Inheritance diagram for oomph::Matrix< T, MATRIX_TYPE >:

Public Member Functions

 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...
 
virtual unsigned long nrow () const =0
 Return the number of rows of the matrix. More...
 
virtual unsigned long ncol () const =0
 Return the number of columns of the matrix. 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
 
virtual void output_bottom_right_zero_helper (std::ostream &outfile) const =0
 
virtual void sparse_indexed_output_helper (std::ostream &outfile) const =0
 
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 Member Functions

void range_check (const unsigned long &i, const unsigned long &j) const
 

Detailed Description

template<class T, class MATRIX_TYPE>
class oomph::Matrix< T, MATRIX_TYPE >

Abstract base class for matrices, templated by the type of object that is stored in them and the type of matrix. The MATRIX_TYPE template argument is used as part of the Curiously Recurring Template Pattern, see http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern The pattern is used to force the inlining of the round bracket access functions by ensuring that they are NOT virtual functions.

Constructor & Destructor Documentation

◆ Matrix() [1/2]

template<class T , class MATRIX_TYPE >
oomph::Matrix< T, MATRIX_TYPE >::Matrix ( )
inline

(Empty) constructor

105 {}

◆ Matrix() [2/2]

template<class T , class MATRIX_TYPE >
oomph::Matrix< T, MATRIX_TYPE >::Matrix ( const Matrix< T, MATRIX_TYPE > &  matrix)
delete

Broken copy constructor.

◆ ~Matrix()

template<class T , class MATRIX_TYPE >
virtual oomph::Matrix< T, MATRIX_TYPE >::~Matrix ( )
inlinevirtual

Virtual (empty) destructor.

114 {}

Member Function Documentation

◆ ncol()

◆ nrow()

◆ operator()() [1/2]

template<class T , class MATRIX_TYPE >
T& oomph::Matrix< T, MATRIX_TYPE >::operator() ( const unsigned long &  i,
const unsigned long &  j 
)
inline

Round brackets to give access as a(i,j) for read-write access. The function uses the MATRIX_TYPE template parameter to call the entry() function which must be defined in all derived classes that are to be fully instantiated. If the particular Matrix does not allow write access, the function should break with an error message.

141  {
142  return static_cast<MATRIX_TYPE*>(this)->entry(i, j);
143  }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References i, and j.

◆ operator()() [2/2]

template<class T , class MATRIX_TYPE >
T oomph::Matrix< T, MATRIX_TYPE >::operator() ( const unsigned long &  i,
const unsigned long &  j 
) const
inline

Round brackets to give access as a(i,j) for read only (we're not providing a general interface for component-wise write access since not all matrix formats allow efficient direct access!) The function uses the MATRIX_TYPE template parameter to call the get_entry() function which must be defined in all derived classes that are to be fully instantiated.

129  {
130  return static_cast<MATRIX_TYPE const*>(this)->get_entry(i, j);
131  }

References i, and j.

Referenced by oomph::CRMatrix< T >::output_bottom_right_zero_helper(), and oomph::CCMatrix< T >::output_bottom_right_zero_helper().

◆ operator=()

template<class T , class MATRIX_TYPE >
void oomph::Matrix< T, MATRIX_TYPE >::operator= ( const Matrix< T, MATRIX_TYPE > &  )
delete

Broken assignment operator.

◆ output()

template<class T , class MATRIX_TYPE >
virtual void oomph::Matrix< T, MATRIX_TYPE >::output ( std::ostream &  outfile) const
inlinevirtual

Output function to print a matrix row-by-row, in the form a(0,0) a(0,1) ... a(1,0) a(1,1) ... ... to the stream outfile. Broken virtual since it might not be sensible to implement this for some sparse matrices.

Reimplemented in oomph::DenseMatrix< T >, oomph::DenseMatrix< oomph::MatrixVectorProduct * >, oomph::DenseMatrix< double >, oomph::DenseMatrix< int >, oomph::DenseMatrix< double * >, oomph::DenseMatrix< oomph::CRDoubleMatrix * >, oomph::DenseMatrix< std::complex< double > >, and oomph::DenseMatrix< bool >.

153  {
154  throw OomphLibError(
155  "Output function is not implemented for this matrix class",
158  }
#define OOMPH_EXCEPTION_LOCATION
Definition: oomph_definitions.h:61
#define OOMPH_CURRENT_FUNCTION
Definition: oomph_definitions.h:86

References OOMPH_CURRENT_FUNCTION, and OOMPH_EXCEPTION_LOCATION.

◆ output_bottom_right_zero_helper()

template<class T , class MATRIX_TYPE >
virtual void oomph::Matrix< T, MATRIX_TYPE >::output_bottom_right_zero_helper ( std::ostream &  outfile) const
pure virtual

Output the "bottom right" entry regardless of it being zero or not (this allows automatic detection of matrix size in e.g. matlab, python). This functionality was moved from the function sparse_indexed_output(...) because at the moment, generalisation of this functionality does not work in parallel. CRDoubleMatrix has an nrow() function but it should it should use nrow_local() - which is the N variable in the underlaying CRMatrix.

Implemented in oomph::SumOfMatrices, oomph::CCMatrix< T >, oomph::CCMatrix< double >, oomph::CCMatrix< std::complex< double > >, oomph::CRDoubleMatrix, oomph::CRMatrix< T >, oomph::CRMatrix< double >, oomph::CRMatrix< std::complex< double > >, oomph::SparseMatrix< T, MATRIX_TYPE >, oomph::SparseMatrix< T, CCMatrix< T > >, oomph::SparseMatrix< T, CRMatrix< T > >, oomph::DenseMatrix< T >, oomph::DenseMatrix< oomph::MatrixVectorProduct * >, oomph::DenseMatrix< double >, oomph::DenseMatrix< int >, oomph::DenseMatrix< double * >, oomph::DenseMatrix< oomph::CRDoubleMatrix * >, oomph::DenseMatrix< std::complex< double > >, and oomph::DenseMatrix< bool >.

Referenced by oomph::Matrix< T, MATRIX_TYPE >::sparse_indexed_output().

◆ range_check()

template<class T , class MATRIX_TYPE >
void oomph::Matrix< T, MATRIX_TYPE >::range_check ( const unsigned long &  i,
const unsigned long &  j 
) const
inlineprotected

Range check to catch when an index is out of bounds, if so, it issues a warning message and dies by throwing an OomphLibError

79  {
80  if (i >= nrow())
81  {
82  std::ostringstream error_message;
83  error_message << "Range Error: i=" << i << " is not in the range (0,"
84  << nrow() - 1 << ")." << std::endl;
85 
86  throw OomphLibError(error_message.str(),
89  }
90  else if (j >= ncol())
91  {
92  std::ostringstream error_message;
93  error_message << "Range Error: j=" << j << " is not in the range (0,"
94  << ncol() - 1 << ")." << std::endl;
95 
96  throw OomphLibError(error_message.str(),
99  }
100  }
virtual unsigned long nrow() const =0
Return the number of rows of the matrix.
virtual unsigned long ncol() const =0
Return the number of columns of the matrix.

References i, j, oomph::Matrix< T, MATRIX_TYPE >::ncol(), oomph::Matrix< T, MATRIX_TYPE >::nrow(), OOMPH_CURRENT_FUNCTION, and OOMPH_EXCEPTION_LOCATION.

Referenced by oomph::CRMatrix< T >::get_entry(), and oomph::CCMatrix< T >::get_entry().

◆ sparse_indexed_output() [1/2]

template<class T , class MATRIX_TYPE >
void oomph::Matrix< T, MATRIX_TYPE >::sparse_indexed_output ( std::ostream &  outfile,
const unsigned precision = 0,
const bool output_bottom_right_zero = false 
) const
inline

Indexed output function to print a matrix to the stream outfile as i,j,a(i,j) for a(i,j)!=0 only with specified precision (if precision=0 then nothing is changed). If optional boolean flag is set to true we also output the "bottom right" entry regardless of it being zero or not (this allows automatic detection of matrix size in e.g. matlab, python).

186  {
187  // Implemented as a wrapper around "sparse_indexed_output(std::ostream)"
188  // so that only one output helper function is needed in derived classes.
189 
190  // We can't have separate functions for only "output_bottom_right_zero"
191  // because people often write false as "0" and then C++ would pick the
192  // wrong function.
193 
194  // If requested set the new precision and store the previous value.
195  unsigned old_precision = 0;
196  if (precision != 0)
197  {
198  old_precision = outfile.precision();
199  outfile.precision(precision);
200  }
201 
202  // Output as normal using the helper function defined in each matrix class
204 
205  // If requested and there is no output for the last entry then output a
206  // zero entry.
207  if (output_bottom_right_zero && ncol() > 0 && nrow() > 0)
208  {
209  // Output as normal using the helper function defined
210  // in each matrix class
212  }
213 
214  // Restore the old value of the precision if we changed it
215  if (precision != 0)
216  {
217  outfile.precision(old_precision);
218  }
219  }
virtual void output_bottom_right_zero_helper(std::ostream &outfile) const =0
virtual void sparse_indexed_output_helper(std::ostream &outfile) const =0

References oomph::Matrix< T, MATRIX_TYPE >::ncol(), oomph::Matrix< T, MATRIX_TYPE >::nrow(), oomph::Matrix< T, MATRIX_TYPE >::output_bottom_right_zero_helper(), and oomph::Matrix< T, MATRIX_TYPE >::sparse_indexed_output_helper().

Referenced by oomph::BlackBoxFDNewtonSolver::black_box_fd_newton_solve(), BiharmonicTestProblem1::dump_jacobian(), main(), print_elemental_jacobian(), and oomph::Matrix< T, MATRIX_TYPE >::sparse_indexed_output().

◆ sparse_indexed_output() [2/2]

template<class T , class MATRIX_TYPE >
void oomph::Matrix< T, MATRIX_TYPE >::sparse_indexed_output ( std::string  filename,
const unsigned precision = 0,
const bool output_bottom_right_zero = false 
) const
inline

Indexed output function to print a matrix to the file named filename as i,j,a(i,j) for a(i,j)!=0 only with specified precision. If optional boolean flag is set to true we also output the "bottom right" entry regardless of it being zero or not (this allows automatic detection of matrix size in e.g. matlab, python).

230  {
231  // Implemented as a wrapper around "sparse_indexed_output(std::ostream)"
232  // so that only one output function needs to be written in matrix
233  // subclasses.
234 
235  // Open file
236  std::ofstream some_file(filename.c_str());
237 
238  // Output as normal
239  sparse_indexed_output(some_file, precision, output_bottom_right_zero);
240 
241  // Close file
242  some_file.close();
243  }
void sparse_indexed_output(std::ostream &outfile, const unsigned &precision=0, const bool &output_bottom_right_zero=false) const
Definition: matrices.h:182
string filename
Definition: MergeRestartFiles.py:39

References MergeRestartFiles::filename, and oomph::Matrix< T, MATRIX_TYPE >::sparse_indexed_output().

◆ sparse_indexed_output_helper()


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