Medusa  1.1
Coordinate Free Mehless Method implementation
ImplicitOperators.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_OPERATORS_IMPLICITOPERATORS_HPP_
2 #define MEDUSA_BITS_OPERATORS_IMPLICITOPERATORS_HPP_
3 
10 #include "ShapeStorage_fwd.hpp"
11 
12 namespace mm {
13 
14 template <class shape_storage_type, class matrix_type, class rhs_type>
16  const shape_storage_t& ss, matrix_t& M, rhs_t& rhs, int row_offset, int col_offset) :
17  ss(&ss), M(&M), rhs(&rhs), row_offset(row_offset), col_offset(col_offset) {
18  assert_msg(0 <= row_offset, "Row offset cannot be negative, got %d.", row_offset);
19  assert_msg(0 <= col_offset, "Col offset cannot be negative, got %d.", col_offset);
20  assert_msg(M.rows() >= ss.size() + row_offset,
21  "Matrix does not have enough rows, expected at least %d = %d + %d, got %d.",
22  ss.size()+row_offset, row_offset, ss.size(), M.rows());
23  assert_msg(M.cols() >= ss.size(),
24  "Matrix does not have enough columns, expected at least %d = %d + %d, got %d.",
25  ss.size()+col_offset, col_offset, ss.size(), M.cols());
26  assert_msg(rhs.size() >= ss.size() + row_offset,
27  "RHS vector does not have enough rows, expected at least %d = %d + %d, got %d.",
28  ss.size()+row_offset, row_offset, ss.size(), M.rows());
29  #ifndef NDEBUG
30  // Warning if the matrix or rhs is not zero initialized.
31  if (M.squaredNorm() > 1e-10) {
32  std::cerr << "Warning: matrix in implicit operators not initialized to zero!" << std::endl;
33  }
34  if (rhs.squaredNorm() > 1e-10) {
35  std::cerr << "Warning: rhs in implicit operators not initialized to zero!" << std::endl;
36  }
37  #endif
38 }
39 
40 template <class S, class matrix_type, class rhs_type>
42  assert_msg(M->rows() >= ss->size(),
43  "Matrix does not have enough rows, expected at least %d, got %d.",
44  ss->size(), M->rows());
45  assert_msg(M->cols() >= ss->size(),
46  "Matrix does not have enough columns, expected at least %d, got %d.",
47  ss->size(), M->cols());
48  assert_msg(v == v, "You tried to set M(%d, %d) to NaN. Check your computed shapes.",
49  row_offset+i, col_offset+j);
50  M->coeffRef(i+row_offset, j+col_offset) += v;
51 }
52 
53 template <class shape_storage_type, class matrix_type, class rhs_type>
55  assert_msg(rhs->size() >= ss->size(),
56  "RHS vector does not have enough rows, expected at least %d, got %d.",
57  ss->size(), rhs->size());
58  assert_msg(v == v, "You tried to set rhs(%d) to NaN. Check your computed shapes.",
59  row_offset+i);
60  rhs->operator[](i+row_offset) += v;
61 }
62 
64 template <typename S, typename M, typename R>
65 std::ostream& operator<<(std::ostream& os, const ImplicitOperators<S, M, R>& op) {
66  os << "Implicit operators ";
67  os << ((op.hasShapes()) ? "with" : "without") << " linked storage ";
68  if (!op.hasShapes()) {
69  os << "without linked storage";
70  } else {
71  os << "over storage: " << *op.ss;
72  }
73  os << " with " << ((op.hasMatrix()) ? "defined" : "undefined") << " problem matrix";
74  os << " and with " << ((op.hasRhs()) ? "defined" : "undefined") << " problem rhs.";
75  return os;
76 }
77 
79 template <typename Derived, typename vec_t, typename OpFamilies>
80 template <typename M, typename R>
81 ImplicitOperators<Derived, M, R>
83  return {*static_cast<const Derived*>(this), matrix, rhs};
84 }
86 
87 } // namespace mm
88 
89 #endif // MEDUSA_BITS_OPERATORS_IMPLICITOPERATORS_HPP_
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::ImplicitOperators::rhs_t
rhs_type rhs_t
Right hand side type.
Definition: ImplicitOperators_fwd.hpp:44
mm::ImplicitOperators::ss
const shape_storage_t * ss
Shape storage, but name is shortened for readability.
Definition: ImplicitOperators_fwd.hpp:54
mm::ImplicitOperators::addToRhs
void addToRhs(int i, scalar_t v)
Adds v to rhs[i].
Definition: ImplicitOperators.hpp:54
ShapeStorage_fwd.hpp
mm::ImplicitOperators::scalar_t
matrix_t::Scalar scalar_t
Scalar type of matrix elements.
Definition: ImplicitOperators_fwd.hpp:48
mm::ImplicitOperators::ImplicitOperators
ImplicitOperators()
Default constructor sets offset to 0 and pointers to nullptr.
Definition: ImplicitOperators_fwd.hpp:225
mm::ImplicitOperators::rhs
rhs_t * rhs
Pointer to right hand side.
Definition: ImplicitOperators_fwd.hpp:56
mm::operator<<
std::ostream & operator<<(std::ostream &os, const Gaussian< S > &b)
Output basic information about given Gaussian RBF.
Definition: Gaussian.hpp:37
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
mm::ShapeStorage::implicitOperators
ImplicitOperators< Derived, M, R > implicitOperators(M &matrix, R &rhs) const
Construct implicit operators over this storage.
mm::ImplicitOperators::hasMatrix
bool hasMatrix() const
Returns true if operators have a non-null pointer to problem matrix.
Definition: ImplicitOperators_fwd.hpp:268
mm::ImplicitOperators::M
matrix_t * M
Pointer to problem matrix.
Definition: ImplicitOperators_fwd.hpp:55
mm::ImplicitOperators::hasShapes
bool hasShapes() const
Returns true if operators have a non-null pointer to storage.
Definition: ImplicitOperators_fwd.hpp:266
mm::ImplicitOperators::matrix_t
matrix_type matrix_t
Matrix type.
Definition: ImplicitOperators_fwd.hpp:43
mm::ImplicitOperators
This class represents implicit operators that fill given matrix M and right hand side rhs with approp...
Definition: ImplicitOperators_fwd.hpp:40
mm::ImplicitOperators::row_offset
int row_offset
Row offset to be used when accessing matrix or rhs coefficients.
Definition: ImplicitOperators_fwd.hpp:57
ImplicitOperators_fwd.hpp
mm::ImplicitOperators::hasRhs
bool hasRhs() const
Returns true if operators have a non-null pointer to problem right hand side.
Definition: ImplicitOperators_fwd.hpp:270
mm::ImplicitOperators::addToM
void addToM(int i, int j, scalar_t v)
Add v to M(i, j).
Definition: ImplicitOperators.hpp:41
mm::ImplicitOperators::shape_storage_t
shape_storage_type shape_storage_t
Type of shape storage.
Definition: ImplicitOperators_fwd.hpp:42
mm::ImplicitOperators::col_offset
int col_offset
Column offset to be used when accessing matrix coefficients.
Definition: ImplicitOperators_fwd.hpp:58