1#ifndef VIENNASHE_MATH_LINALG_UTIL_HPP
2#define VIENNASHE_MATH_LINALG_UTIL_HPP
35 template<
typename VectorType>
36 typename VectorType::value_type
39 typename VectorType::value_type result = 0;
40 for (std::size_t i = 0; i < v.size (); ++i)
41 result += v[i] * v[i];
42 return std::sqrt (result);
49 template<
typename VectorType>
51 subtract (VectorType
const &x, VectorType
const &y)
54 x.size () == y.size ()
55 && bool (
"Size mismatch for subtracting y from x"));
57 VectorType result (x.size ());
58 for (std::size_t i = 0; i < x.size (); ++i)
59 result[i] = x[i] - y[i];
67 template<
typename NumericT>
77 typedef std::map<size_type, NumericT> MapType;
78 typedef std::vector<MapType> DataContainerType;
92 data_ (num_rows), zero_ (0)
95 num_cols == num_rows &&
bool (
"Only square matrices supported."));
102 return data_.size ();
107 return data_.size ();
115 i < data_.size () && j < data_.size ()
116 && bool (
"Access to matrix out of range!"));
117 return data_.at (i)[j];
124 i < data_.size () && j < data_.size ()
125 && bool (
"Access to matrix out of range!"));
127 if (it == data_.at (i).end ())
148 for (
size_type i = 0; i < data_.size (); ++i)
149 result += data_[i].size ();
156 self_type A_trans (data_.size (), data_.size ());
158 for (
size_type i = 0; i < data_.size (); ++i)
161 it != data_[i].end (); ++it)
162 A_trans (it->first, i) = it->second;
171 for (
size_type i = 0; i < data_.size (); ++i)
174 it != data_[i].end (); ++it)
175 result (it->first, i) *= factor;
188 data_[i][it->first] += it->second;
194 DataContainerType data_;
200 template<
typename NumericT,
typename VectorT>
212 VectorT scale_factors (rhs.size (), 1.0);
216 for (std::size_t i = 0; i < system_matrixc.
size1 (); ++i)
218 RowType &col_i = system_matrixc.
row (i);
221 double row_norm = 0.0;
223 for (AlongRowIterator iter = col_i.begin (); iter != col_i.end ();
226 double temp = std::log (std::fabs (iter->second));
229 row_norm = std::exp (row_norm / col_i.size ());
231 if (system_matrixc (i, i) < 0.0)
235 for (AlongRowIterator iter = col_i.begin (); iter != col_i.end ();
238 iter->second /= row_norm;
243 scale_factors[i] /= row_norm;
245 system_matrix = system_matrixc.
trans ();
248 for (std::size_t i = 0; i < system_matrix.
size1 (); ++i)
250 RowType &row_i = system_matrix.
row (i);
253 double row_norm = 0.0;
254 for (AlongRowIterator iter = row_i.begin (); iter != row_i.end ();
257 row_norm += iter->second * iter->second;
260 row_norm = sqrt (row_norm);
263 if (system_matrix (i, i) < 0.0)
267 for (AlongRowIterator iter = row_i.begin (); iter != row_i.end ();
270 iter->second /= row_norm;
281 return scale_factors;
284 template<
typename NumericT>
291 for (std::size_t i = 0; i < system_matrix.
size1 (); ++i)
293 os << std::endl <<
"Row " << i <<
": ";
294 for (AlongRowIterator iter = system_matrix.
row (i).begin ();
295 iter != system_matrix.
row (i).end (); ++iter)
297 os <<
"(" << iter->first <<
", " << iter->second <<
"), ";
306 template<
typename NumericT,
typename VectorType>
313 double minimaldenormal, minimalnormal;
314 int Total, Inf, NaN, subnor, nor, zero;
315 int Total2, Inf2, NaN2, subnor2, nor2, zero2;
316 Total = Inf = NaN = subnor = nor = zero = 0;
317 Total2 = Inf2 = NaN2 = subnor2 = nor2 = zero2 = 0;
318 minimaldenormal = minimalnormal = 1.0;
319 for (std::size_t i = 0; i < system_matrix.
size1 (); ++i)
321 RowType
const &row_i = system_matrix.
row (i);
322 for (Iterator2 iter2 = row_i.begin (); iter2 != row_i.end ();
325 switch (std::fpclassify (iter2->second))
334 subnor++, Total++, minimaldenormal =
335 iter2->second > minimaldenormal ?
336 minimaldenormal : iter2->second;
339 nor++, Total++, minimalnormal =
340 iter2->second > minimalnormal ?
341 minimalnormal : iter2->second;
348 switch (std::fpclassify (x[i]))
368 std::cout <<
"Matrix: Total:" << Total <<
" Subnormal " << subnor
369 <<
" normal " << nor <<
" Zero " << zero <<
" Inf " << Inf
370 <<
" Nan " << NaN << std::endl;
371 std::cout <<
"Vector: Total:" << Total2 <<
" Subnormal " << subnor2
372 <<
" normal " << nor2 <<
" Zero " << zero2 <<
" Inf " << Inf2
373 <<
" Nan " << NaN2 << std::endl;
374 printf (
"minimaldenormal: %f %A | minimalnormal: %f %A ",
375 minimaldenormal, minimaldenormal, minimalnormal, minimalnormal);
383 template<
typename NumericT,
typename VectorType>
390 VectorType result (system_matrix.
size1 ());
391 for (std::size_t i = 0; i < system_matrix.
size1 (); ++i)
393 RowType
const &row_i = system_matrix.
row (i);
395 for (Iterator2 iter2 = row_i.begin (); iter2 != row_i.end ();
398 val += iter2->second * x[iter2->first];
408 template<
typename NumericT>
417 data_ (rows * cols + 1), size1_ (rows), size2_ (cols)
420 rows == cols &&
bool (
"Only square dense matrices supported!"));
437 assert(i < size1_ && j < size2_ &&
bool (
"Index out of bounds!"));
438 return data_.at (i * size2_ + j);
444 assert(i < size1_ && j < size2_ &&
bool (
"Index out of bounds!"));
445 return data_.at (i * size2_ + j);
449 std::vector<NumericT> data_;
NumericT const & operator()(size_type i, size_type j) const
dense_matrix(size_type rows, size_type cols)
Sparse matrix class based on a vector of binary trees for holding the entries.
MapType const & row(size_t i) const
sparse_matrix(size_type num_rows, size_type num_cols)
self_type & operator+=(self_type B)
NumericT & operator()(size_type i, size_type j)
Non-const access to the entries of the matrix. Use this for assembly.
MapType::const_iterator const_iterator2
MapType::iterator iterator2
self_type operator*(NumericT factor) const
Contains forward declarations and definition of small classes that must be defined at an early stage.
std::ostream & operator<<(std::ostream &os, sparse_matrix< NumericT > const &system_matrix)
Converts a sparse matrix (ublas, ViennaCL) to a string. Handy debugging facility.
VectorType::value_type norm_2(VectorType const &v)
void check_types(sparse_matrix< NumericT > const &system_matrix, VectorType const &x)
VectorType subtract(VectorType const &x, VectorType const &y)
Helper routine for the generic vector subtraction z = x - y;.
VectorType prod(sparse_matrix< NumericT > const &system_matrix, VectorType const &x)
Computes A * x for a sparse A and a vector x.
VectorT row_normalize_system(sparse_matrix< NumericT > &system_matrix, VectorT &rhs)
Normalizes an equation system such that all diagonal entries are non-negative, and such that all 2-no...
The main ViennaSHE namespace. All functionality resides inside this namespace.