1#ifndef VIENNASHE_UTIL_CHECKS_HPP
2#define VIENNASHE_UTIL_CHECKS_HPP
55 template <
typename ValueType =
double>
59 range_checker(ValueType
const & a, ValueType
const & b) : lower_bound_(a), upper_bound_(b)
64 ss <<
"Invalid range [" << a <<
", " << b <<
")";
71 return (v >= lower_bound_) && (v < upper_bound_);
75 ValueType lower_bound_;
76 ValueType upper_bound_;
91 bool operator()(T
const & t)
const {
return t - t != 0; }
96 template <
typename ValueType >
97 bool is_NaN(
const ValueType & val) {
return (val != val); }
100 template <
typename ValueType >
101 bool is_Inf(
const ValueType & val) {
return std::fabs(val - val) > 0; }
104 template <
typename ValueType >
110 template <
typename VectorType>
113 for (std::size_t i=0; i<vec.size(); ++i)
117 std::stringstream ss;
118 ss << message <<
": Found NaN in vector at index " << i << std::endl;
124 std::stringstream ss;
125 ss << message <<
": Found infinity in vector at index " << i << std::endl;
137 template <
typename BasicChecker,
typename ExceptionType>
142 ExceptionType
const & e) : checker_(c), exception_(e) {}
144 template <
typename T>
154 BasicChecker
const & checker_;
155 ExceptionType
const & exception_;
160 template <
typename CheckerType,
typename ExceptionType>
173 template <
typename MatrixType>
176 bool is_m_matrix =
true;
178 for (std::size_t row_index = 0;
179 row_index != A.size1();
182 typedef typename MatrixType::const_iterator2 AlongRowIterator;
183 typedef typename MatrixType::row_type RowType;
185 RowType
const & row_i = A.row(row_index);
187 double abs_offdiagonal = 0.0;
188 double abs_diagonal = 0.0;
190 for (AlongRowIterator col_it = row_i.begin();
191 col_it != row_i.end();
194 std::size_t col_index = col_it->first;
195 double entry = col_it->second;
201 if (row_index != col_index)
206 log::warn() <<
"* WARNING in m_matrix_check(): Entry (" << row_index <<
"," << col_index <<
") is positive: " << entry << std::endl;
209 abs_offdiagonal += std::fabs(col_it->second);
216 log::warn() <<
"* WARNING in m_matrix_check(): Entry (" << row_index <<
"," << col_index <<
") is non-positive: " << entry << std::endl;
219 abs_diagonal = entry;
223 if (abs_offdiagonal > abs_diagonal * 1.0001)
226 log::warn() <<
"* WARNING in m_matrix_check(): Row " << row_index <<
" imbalanced! "
227 <<
"fabs(offdiagonals): " << abs_offdiagonal <<
", diagonal: " << abs_diagonal << std::endl;
232 log::info<log_m_matrix_check>() <<
"* m_matrix_check(): Matrix is M-matrix!" << std::endl;
245 template <
typename MatrixType>
248 typedef typename MatrixType::const_iterator1 RowIterator;
249 typedef typename MatrixType::const_iterator2 ColumnIterator;
253 for (RowIterator row_iter = matrix.begin1();
254 row_iter != matrix.end1();
257 bool has_row =
false;
258 for (ColumnIterator col_iter = row_iter.begin();
259 col_iter != row_iter.end();
266 if (has_row ==
false)
268 log::error() <<
"* FATAL ERROR in check_matrix_consistency(): System matrix has empty row " << row_iter.index1() <<
"!" << std::endl;
282 template <
typename NumericT>
287 for (std::size_t i = 0; i < matrix.
size1(); ++i)
289 bool has_row =
false;
290 for (AlongRowIterator col_iter = matrix.
row(i).begin();
291 col_iter != matrix.
row(i).end();
294 if (col_iter->second)
301 if (has_row ==
false)
303 log::error() <<
"* FATAL ERROR in check_matrix_consistency(): System matrix has empty row " << i <<
"!" << std::endl;
314 template <
typename MatrixType>
317 typedef typename MatrixType::const_iterator1 RowIterator;
318 typedef typename MatrixType::const_iterator2 ColumnIterator;
319 typedef typename MatrixType::value_type ScalarType;
321 std::size_t size = std::min<std::size_t>(matrix.size2(), num_cols);
322 std::vector<ScalarType> col_sums(size);
323 std::vector<ScalarType> col_maximums(size);
328 for (RowIterator row_iter = matrix.begin1();
329 row_iter != matrix.end1();
332 if (row_iter.index1() >= size)
335 for (ColumnIterator col_iter = row_iter.begin();
336 col_iter != row_iter.end();
339 col_sums[col_iter.index2()] += *col_iter;
340 if (std::abs(*col_iter) > col_maximums[col_iter.index2()])
341 col_maximums[col_iter.index2()] = std::abs(*col_iter);
349 for (std::size_t i=0; i<col_sums.size(); ++i)
351 if (col_maximums[i] == 0)
354 else if (std::abs(col_sums[i]) / col_maximums[i] > 1e-10)
356 log::error() <<
"* CHECK FAILED in check_vanishing_column_sums(): Column " << i <<
" does not vanish: " << col_sums[i] <<
" with maximum " << col_maximums[i] << std::endl;
Sparse matrix class based on a vector of binary trees for holding the entries.
MapType const & row(size_t i) const
MapType::const_iterator const_iterator2
A checker class that throws a user-provided exception rather than returning false on its functor inte...
checker_with_exception(BasicChecker const &c, ExceptionType const &e)
bool operator()(T const &t) const
Exception thrown if for a range(a,b) the check a < b fails.
Exception which is thrown if an invalid value is found.
Checker for values inside a half-open interval [a, b), where 'a' is included, while 'b' is not.
range_checker(ValueType const &a, ValueType const &b)
bool operator()(ValueType const &v) const
Implementation of various utilities related to linear algebra.
A logging facility providing fine-grained control over logging in ViennaSHE.
logger< true > error()
Used to log errors. The logging level is logERROR.
logger< true > warn()
Used to log warnings. The logging level is logWARNING.
checker_with_exception< CheckerType, ExceptionType > make_checker_with_exception(CheckerType const &checker, ExceptionType const &ex)
Convenience creator routine for creating a checker with exception from checker returning a bool only.
void check_vanishing_column_sums(MatrixType const &matrix, std::size_t num_cols)
Checks that the first 'num_cols' column sums of the provided matrix vanish (up to round-off) or a col...
bool is_Inf(const ValueType &val)
Checks if a value of type ValueType is Inf using (value - value) != 0.
bool is_negative(const ValueType &val)
Checks if a value of type ValueType is negative using value < 0.
long matrix_consistency_check(MatrixType const &matrix)
Checks a matrix for empty rows.
void check_vector_for_valid_entries(VectorType const &vec, std::string message="Location not specified")
Checks a vector for valid entries (i.e. no NaN).
bool is_NaN(const ValueType &val)
Checks if a value of type ValueType is NaN using value != value.
void m_matrix_check(MatrixType const &A)
Checks a matrix for being an M matrix.
The main ViennaSHE namespace. All functionality resides inside this namespace.
A checker that never complains. Can be used instead of overly picky/erroneous checkers (just for debu...
bool operator()(T const &) const
bool operator()(T const &t) const
bool operator()(T const &t) const
Defines all exceptions used/thrown in the viennashe/util/ namespace.
Defines the log keys used within the viennashe::util namespace.