oomph::SumOfMatrices Class Reference

#include <sum_of_matrices.h>

+ Inheritance diagram for oomph::SumOfMatrices:

Public Member Functions

 SumOfMatrices ()
 Default constructor. More...
 
 SumOfMatrices (DoubleMatrixBase *main_matrix_pt)
 Constructor taking a pointer to the main matrix as input. More...
 
 SumOfMatrices (const SumOfMatrices &matrix)=delete
 Broken copy constructor. More...
 
void operator= (const SumOfMatrices &)=delete
 Broken assignment operator. More...
 
 ~SumOfMatrices ()
 
const DoubleMatrixBasemain_matrix_pt () const
 Access to the main matrix. More...
 
DoubleMatrixBase *& main_matrix_pt ()
 
void set_delete_main_matrix ()
 
void output_bottom_right_zero_helper (std::ostream &outfile) const
 
void sparse_indexed_output_helper (std::ostream &outfile) const
 
void get_as_indices (Vector< int > &row, Vector< int > &col, Vector< double > &values)
 
void add_matrix (DoubleMatrixBase *added_matrix_pt_in, const AddedMainNumberingLookup *main_to_added_rows_pt, const AddedMainNumberingLookup *main_to_added_cols_pt, bool should_delete_matrix=false)
 
DoubleMatrixBaseadded_matrix_pt (const unsigned &i) const
 
const AddedMainNumberingLookuprow_map_pt (const unsigned &i) const
 Access to the maps. More...
 
const AddedMainNumberingLookupcol_map_pt (const unsigned &i) const
 
unsigned long nrow () const
 Return the number of rows of the main matrix. More...
 
unsigned long ncol () const
 Return the number of columns of the main matrix. More...
 
unsigned n_added_matrix () const
 Return the number of added matrices in the sum. More...
 
void multiply (const DoubleVector &x, DoubleVector &soln) const
 
doubleentry (const unsigned long &i, const unsigned long &j) const
 
double operator() (const unsigned long &i, const unsigned long &j) const
 
virtual void multiply_transpose (const DoubleVector &x, DoubleVector &soln) const
 
- Public Member Functions inherited from oomph::DoubleMatrixBase
 DoubleMatrixBase ()
 (Empty) constructor. More...
 
 DoubleMatrixBase (const DoubleMatrixBase &matrix)=delete
 Broken copy constructor. More...
 
void operator= (const DoubleMatrixBase &)=delete
 Broken assignment operator. More...
 
virtual ~DoubleMatrixBase ()
 virtual (empty) destructor More...
 
LinearSolver *& linear_solver_pt ()
 Return a pointer to the linear solver object. More...
 
LinearSolver *const & linear_solver_pt () const
 Return a pointer to the linear solver object (const version) More...
 
void solve (DoubleVector &rhs)
 
void solve (const DoubleVector &rhs, DoubleVector &soln)
 
void solve (Vector< double > &rhs)
 
void solve (const Vector< double > &rhs, Vector< double > &soln)
 
virtual void residual (const DoubleVector &x, const DoubleVector &b, DoubleVector &residual_)
 Find the residual, i.e. r=b-Ax the residual. More...
 
virtual double max_residual (const DoubleVector &x, const DoubleVector &rhs)
 
- Public Member Functions inherited from oomph::Matrix< double, SumOfMatrices >
 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...
 
double operator() (const unsigned long &i, const unsigned long &j) const
 
doubleoperator() (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
 

Private Attributes

DoubleMatrixBaseMain_matrix_pt
 Pointer to the matrix which we are adding the others to. More...
 
Vector< DoubleMatrixBase * > Added_matrix_pt
 List of pointers to the matrices that are added to the main matrix. More...
 
Vector< const AddedMainNumberingLookup * > Row_map_pt
 
Vector< const AddedMainNumberingLookup * > Col_map_pt
 
Vector< unsignedShould_delete_added_matrix
 Should we delete the sub matrices when destructor is called? More...
 
bool Should_delete_main_matrix
 

Additional Inherited Members

- Protected Member Functions inherited from oomph::Matrix< double, SumOfMatrices >
void range_check (const unsigned long &i, const unsigned long &j) const
 
- Protected Attributes inherited from oomph::DoubleMatrixBase
LinearSolverLinear_solver_pt
 
LinearSolverDefault_linear_solver_pt
 

Detailed Description

Class for a matrix of the form M = S + G + H + ... where S is the main matrix and G,H etc. are matrices of size S or smaller. This may be useful if, for example, G,H etc. are subblocks of M that must be stored in a different format to S. Maps mut be provided which gives a map from the rows/cols of the main matrix to the rows/cols of each of the added matrices.

Constructor & Destructor Documentation

◆ SumOfMatrices() [1/3]

oomph::SumOfMatrices::SumOfMatrices ( )
inline

Default constructor.

288  : Main_matrix_pt(0),
289  Added_matrix_pt(0),
290  Row_map_pt(0),
291  Col_map_pt(0),
294  {
295  }
Vector< DoubleMatrixBase * > Added_matrix_pt
List of pointers to the matrices that are added to the main matrix.
Definition: sum_of_matrices.h:268
Vector< const AddedMainNumberingLookup * > Col_map_pt
Definition: sum_of_matrices.h:276
Vector< const AddedMainNumberingLookup * > Row_map_pt
Definition: sum_of_matrices.h:272
Vector< unsigned > Should_delete_added_matrix
Should we delete the sub matrices when destructor is called?
Definition: sum_of_matrices.h:279
bool Should_delete_main_matrix
Definition: sum_of_matrices.h:283
DoubleMatrixBase * Main_matrix_pt
Pointer to the matrix which we are adding the others to.
Definition: sum_of_matrices.h:265

◆ SumOfMatrices() [2/3]

oomph::SumOfMatrices::SumOfMatrices ( DoubleMatrixBase main_matrix_pt)
inline

Constructor taking a pointer to the main matrix as input.

300  Added_matrix_pt(0),
301  Row_map_pt(0),
302  Col_map_pt(0),
305  {
306  }
const DoubleMatrixBase * main_matrix_pt() const
Access to the main matrix.
Definition: sum_of_matrices.h:333

◆ SumOfMatrices() [3/3]

oomph::SumOfMatrices::SumOfMatrices ( const SumOfMatrices matrix)
delete

Broken copy constructor.

◆ ~SumOfMatrices()

oomph::SumOfMatrices::~SumOfMatrices ( )
inline

Destructor: delete matrices as instructed by Should_delete_added_matrix vector and Should_delete_main_matrix.

317  {
318  for (unsigned i_matrix = 0; i_matrix < Added_matrix_pt.size(); i_matrix++)
319  {
320  if (Should_delete_added_matrix[i_matrix] == 1)
321  {
322  delete Added_matrix_pt[i_matrix];
323  }
324  }
325 
327  {
328  delete Main_matrix_pt;
329  }
330  }

Member Function Documentation

◆ add_matrix()

void oomph::SumOfMatrices::add_matrix ( DoubleMatrixBase added_matrix_pt_in,
const AddedMainNumberingLookup main_to_added_rows_pt,
const AddedMainNumberingLookup main_to_added_cols_pt,
bool  should_delete_matrix = false 
)
inline

Add a new matrix to the sum by giving a matrix pointer and a mapping from the main matrix numbering to the added matrix's numbering.

419  {
420 #ifdef PARANOID
421  // Check that row mapping has correct size
422  if (main_to_added_rows_pt->main_to_added_mapping_pt()->size() >
423  added_matrix_pt_in->nrow())
424  {
425  throw OomphLibError(
426  "Row mapping size should be less than or equal to nrow (less than if "
427  "it is a sparse matrix and there are some empty rows).",
430  }
431 
432  // Check that col mapping has correct size
433  if (main_to_added_cols_pt->main_to_added_mapping_pt()->size() >
434  added_matrix_pt_in->ncol())
435  {
436  throw OomphLibError(
437  "Col mapping size should be less than or equal to ncol (less than if "
438  "it is a sparse matrix and there are some empty cols).",
441  }
442 #endif
443 #ifdef RANGE_CHECKING
444  // Check that all entries in the row mapping are "in range" for the
445  // main matrix.
446  const Vector<unsigned>* rowmap_pt =
447  main_to_added_rows_pt->added_to_main_mapping_pt();
448  unsigned max_row =
449  *std::max_element(rowmap_pt->begin(), rowmap_pt->end());
450  if (max_row > main_matrix_pt()->nrow())
451  {
452  std::string err =
453  "Trying to add a matrix with a mapping which specifices";
454  err += " a max row of " + to_string(max_row) + " but the main matrix ";
455  err += "only has " + to_string(main_matrix_pt()->nrow()) + " rows!";
456  throw OomphLibError(
458  }
459 
460  // Check that all entries in the row mapping are "in range" for the
461  // main matrix.
462  const Vector<unsigned>* colmap_pt =
463  main_to_added_cols_pt->added_to_main_mapping_pt();
464  unsigned max_col =
465  *std::max_element(colmap_pt->begin(), colmap_pt->end());
466  if (max_col > main_matrix_pt()->ncol())
467  {
468  std::string err =
469  "Trying to add a matrix with a mapping which specifices";
470  err += " a max col of " + to_string(max_col) + " but the main matrix ";
471  err += "only has " + to_string(main_matrix_pt()->ncol()) + " cols!";
472  throw OomphLibError(
474  }
475 #endif
476 
477  Added_matrix_pt.push_back(added_matrix_pt_in);
478  Row_map_pt.push_back(main_to_added_rows_pt);
479  Col_map_pt.push_back(main_to_added_cols_pt);
480  Should_delete_added_matrix.push_back(unsigned(should_delete_matrix));
481  }
unsigned long nrow() const
Return the number of rows of the main matrix.
Definition: sum_of_matrices.h:501
unsigned long ncol() const
Return the number of columns of the main matrix.
Definition: sum_of_matrices.h:515
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
std::string to_string(T object, unsigned float_precision=8)
Definition: oomph_utilities.h:189
#define OOMPH_EXCEPTION_LOCATION
Definition: oomph_definitions.h:61
#define OOMPH_CURRENT_FUNCTION
Definition: oomph_definitions.h:86

References oomph::AddedMainNumberingLookup::added_to_main_mapping_pt(), oomph::AddedMainNumberingLookup::main_to_added_mapping_pt(), oomph::DoubleMatrixBase::ncol(), oomph::DoubleMatrixBase::nrow(), OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION, oomph::Global_string_for_annotation::string(), and oomph::StringConversion::to_string().

◆ added_matrix_pt()

DoubleMatrixBase* oomph::SumOfMatrices::added_matrix_pt ( const unsigned i) const
inline

Access function for ith added matrix (main matrix not included in numbering).

486  {
487  return Added_matrix_pt[i];
488  }
int i
Definition: BiCGSTAB_step_by_step.cpp:9

References i.

Referenced by multiply().

◆ col_map_pt()

const AddedMainNumberingLookup* oomph::SumOfMatrices::col_map_pt ( const unsigned i) const
inline
496  {
497  return Col_map_pt[i];
498  }

References i.

◆ entry()

double& oomph::SumOfMatrices::entry ( const unsigned long &  i,
const unsigned long &  j 
) const
inline

Broken operator() because it does not make sense to return anything by reference.

541  {
542  throw OomphLibError(
543  "Broken write to entry: it does not make sense to write to a sum, you "
544  "must write to one of the component matrices.",
547  }

References OOMPH_CURRENT_FUNCTION, and OOMPH_EXCEPTION_LOCATION.

◆ get_as_indices()

void oomph::SumOfMatrices::get_as_indices ( Vector< int > &  row,
Vector< int > &  col,
Vector< double > &  values 
)
inline

Get a list of row/col indices and total entry for non-zeros in the matrix. e.g. for use as input to other matrix classes. Warning this is SLOW! for sparse matrices.

392  {
393  row.clear();
394  col.clear();
395  values.clear();
396 
397  for (int i = 0; i < int(nrow()); i++)
398  {
399  for (int j = 0; j < int(ncol()); j++)
400  {
401  double entry = operator()(i, j);
402  // Output if non-zero entry
403  if (entry != 0.0)
404  {
405  row.push_back(i);
406  col.push_back(j);
407  values.push_back(entry);
408  }
409  }
410  }
411  }
m col(1)
m row(1)
double & entry(const unsigned long &i, const unsigned long &j) const
Definition: sum_of_matrices.h:540
double operator()(const unsigned long &i, const unsigned long &j) const
Definition: sum_of_matrices.h:552
return int(ret)+1
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References col(), i, int(), j, and row().

◆ main_matrix_pt() [1/2]

DoubleMatrixBase*& oomph::SumOfMatrices::main_matrix_pt ( )
inline
338  {
339  return Main_matrix_pt;
340  }

◆ main_matrix_pt() [2/2]

const DoubleMatrixBase* oomph::SumOfMatrices::main_matrix_pt ( ) const
inline

Access to the main matrix.

334  {
335  return Main_matrix_pt;
336  }

◆ multiply()

void oomph::SumOfMatrices::multiply ( const DoubleVector x,
DoubleVector soln 
) const
virtual

Multiply: just call multiply on each of the matrices and add up the results (with appropriate bookeeping of the relative positions).

Matrix-vector multiplication for a sumofmatrices class. Just delegate each multiplication to the appropriate class then add up the results.

Implements oomph::DoubleMatrixBase.

41  {
42  // We assume that appropriate checks and initialisation on x and soln are
43  // carried out within the individual matrix multiplys.
44 
45  // Multiply for the main matrix
46  Main_matrix_pt->multiply(x, soln);
47 
48  // Now add contribution for the added matrices
49  for (unsigned i_matrix = 0; i_matrix < Added_matrix_pt.size(); i_matrix++)
50  {
51  // If possible copy the matrix distribution, otherwise it isn't
52  // distributed so make a serial LinearAlgebraDistribution object.
53  LinearAlgebraDistribution col_dist, row_dist;
54  OomphCommunicator serial_comm; // Serial communcator (does nothing)
55  col_dist.build(&serial_comm, added_matrix_pt(i_matrix)->ncol(), false);
56  row_dist.build(&serial_comm, added_matrix_pt(i_matrix)->nrow(), false);
57 
58  // Create temporary output DoubleVector
59  DoubleVector temp_soln(row_dist);
60 
61  // Create a const iterator for the map (faster than .find() or []
62  // access, const means can't change the map via the iterator).
63  std::map<unsigned, unsigned>::const_iterator it;
64 
65  // Pull out the appropriate values into a temp vector
66  //??ds not parallel
67  DoubleVector temp_x(col_dist);
68  for (it = Col_map_pt[i_matrix]->main_to_added_mapping_pt()->begin();
69  it != Col_map_pt[i_matrix]->main_to_added_mapping_pt()->end();
70  it++)
71  {
72  temp_x[it->second] = x[it->first];
73  }
74 
75  // Perform the multiplication
76  Added_matrix_pt[i_matrix]->multiply(temp_x, temp_soln);
77 
78  // Add result to solution vector
79  //??ds not parallel
80  for (it = Row_map_pt[i_matrix]->main_to_added_mapping_pt()->begin();
81  it != Row_map_pt[i_matrix]->main_to_added_mapping_pt()->end();
82  it++)
83  {
84  soln[it->first] += temp_soln[it->second];
85  }
86  }
87  }
std::vector< double > DoubleVector
loads clump configuration
Definition: ClumpInput.h:26
virtual void multiply(const DoubleVector &x, DoubleVector &soln) const =0
Multiply the matrix by the vector x: soln=Ax.
DoubleMatrixBase * added_matrix_pt(const unsigned &i) const
Definition: sum_of_matrices.h:485
static constexpr lastp1_t end
Definition: IndexedViewHelper.h:79
list x
Definition: plotDoE.py:28

References Added_matrix_pt, added_matrix_pt(), oomph::LinearAlgebraDistribution::build(), Col_map_pt, Eigen::placeholders::end, Main_matrix_pt, oomph::DoubleMatrixBase::multiply(), ncol(), nrow(), Row_map_pt, and plotDoE::x.

◆ multiply_transpose()

virtual void oomph::SumOfMatrices::multiply_transpose ( const DoubleVector x,
DoubleVector soln 
) const
inlinevirtual

Dummy overload of a pure virtual function. I'm not sure how best to implement this and I don't think I need it.

Implements oomph::DoubleMatrixBase.

575  {
576  std::ostringstream error_msg;
577  error_msg << "Function not yet implemented.";
578  throw OomphLibError(
580 
581  // Possible implementations (not really thought through):
582  // - just call multiply transpose on submatrices?
583  // - do something funny with switching row and column maps?
584  }

References OOMPH_CURRENT_FUNCTION, and OOMPH_EXCEPTION_LOCATION.

◆ n_added_matrix()

unsigned oomph::SumOfMatrices::n_added_matrix ( ) const
inline

Return the number of added matrices in the sum.

530  {
531  return Added_matrix_pt.size();
532  }

◆ ncol()

unsigned long oomph::SumOfMatrices::ncol ( ) const
inlinevirtual

Return the number of columns of the main matrix.

Implements oomph::DoubleMatrixBase.

516  {
517 #ifdef PARANOID
518  if (Main_matrix_pt == 0)
519  {
520  throw OomphLibError("Main_matrix_pt not set",
523  }
524 #endif
525  return Main_matrix_pt->ncol();
526  }
virtual unsigned long ncol() const =0
Return the number of columns of the matrix.

References oomph::DoubleMatrixBase::ncol(), OOMPH_CURRENT_FUNCTION, and OOMPH_EXCEPTION_LOCATION.

Referenced by multiply().

◆ nrow()

unsigned long oomph::SumOfMatrices::nrow ( ) const
inlinevirtual

Return the number of rows of the main matrix.

Implements oomph::DoubleMatrixBase.

502  {
503 #ifdef PARANOID
504  if (Main_matrix_pt == 0)
505  {
506  throw OomphLibError("Main_matrix_pt not set",
509  }
510 #endif
511  return Main_matrix_pt->nrow();
512  }
virtual unsigned long nrow() const =0
Return the number of rows of the matrix.

References oomph::DoubleMatrixBase::nrow(), OOMPH_CURRENT_FUNCTION, and OOMPH_EXCEPTION_LOCATION.

Referenced by multiply().

◆ operator()()

double oomph::SumOfMatrices::operator() ( const unsigned long &  i,
const unsigned long &  j 
) const
inlinevirtual

Access function to get the total value of entries in position (i,j). Warning: this way of getting entries is far too slow to use inside of loops.

Implements oomph::DoubleMatrixBase.

553  {
554  // Get contributions from all matrices
555  double sum = main_matrix_pt()->operator()(i, j);
556  for (unsigned i_matrix = 0; i_matrix < n_added_matrix(); i_matrix++)
557  {
558  int li = Row_map_pt[i_matrix]->unsafe_main_to_added(i);
559  int lj = Col_map_pt[i_matrix]->unsafe_main_to_added(j);
560 
561  // If the added matrix contributes to this entry then add it
562  if ((li != -1) && (lj != -1))
563  {
564  sum += added_matrix_pt(i_matrix)->operator()(li, lj);
565  }
566  }
567 
568  return sum;
569  }
unsigned n_added_matrix() const
Return the number of added matrices in the sum.
Definition: sum_of_matrices.h:529

References i, and j.

◆ operator=()

void oomph::SumOfMatrices::operator= ( const SumOfMatrices )
delete

Broken assignment operator.

◆ output_bottom_right_zero_helper()

void oomph::SumOfMatrices::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).

Implements oomph::Matrix< double, SumOfMatrices >.

354  {
355  int last_row = this->nrow() - 1;
356  int last_col = this->ncol() - 1;
357 
358  double last_value = operator()(last_row, last_col);
359 
360  if (last_value == 0.0)
361  {
362  outfile << last_row << " " << last_col << " " << 0.0 << std::endl;
363  }
364  }

◆ row_map_pt()

const AddedMainNumberingLookup* oomph::SumOfMatrices::row_map_pt ( const unsigned i) const
inline

Access to the maps.

492  {
493  return Row_map_pt[i];
494  }

References i.

◆ set_delete_main_matrix()

void oomph::SumOfMatrices::set_delete_main_matrix ( )
inline

Set the main matrix to be deleted by the destructor of the SumOfMatrices (default is to not delete it).

345  {
347  }

◆ sparse_indexed_output_helper()

void oomph::SumOfMatrices::sparse_indexed_output_helper ( std::ostream &  outfile) const
inlinevirtual

Output the matrix in sparse format. Note that this is going to be slow because we have to check every entry of every matrix for non-zeros.

Implements oomph::Matrix< double, SumOfMatrices >.

370  {
371  for (unsigned long i = 0; i < nrow(); i++)
372  {
373  for (unsigned long j = 0; j < ncol(); j++)
374  {
375  double entry = operator()(i, j);
376  // Output if non-zero entry
377  if (entry != 0.0)
378  {
379  outfile << i << " " << j << " " << entry << std::endl;
380  }
381  }
382  }
383  }

References i, and j.

Member Data Documentation

◆ Added_matrix_pt

Vector<DoubleMatrixBase*> oomph::SumOfMatrices::Added_matrix_pt
private

List of pointers to the matrices that are added to the main matrix.

Referenced by multiply().

◆ Col_map_pt

Vector<const AddedMainNumberingLookup*> oomph::SumOfMatrices::Col_map_pt
private

List of maps between col numbers of the main matrix and the added matrices.

Referenced by multiply().

◆ Main_matrix_pt

DoubleMatrixBase* oomph::SumOfMatrices::Main_matrix_pt
private

Pointer to the matrix which we are adding the others to.

Referenced by multiply().

◆ Row_map_pt

Vector<const AddedMainNumberingLookup*> oomph::SumOfMatrices::Row_map_pt
private

List of maps between row numbers of the main matrix and the added matrices.

Referenced by multiply().

◆ Should_delete_added_matrix

Vector<unsigned> oomph::SumOfMatrices::Should_delete_added_matrix
private

Should we delete the sub matrices when destructor is called?

◆ Should_delete_main_matrix

bool oomph::SumOfMatrices::Should_delete_main_matrix
private

Should we delete the main matrix when destructor is called? Default is no.


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