1#ifndef VIENNASHE_SOLVERS_NATIVE_DENSE_LINEAR_SOLVER_HPP
2#define VIENNASHE_SOLVERS_NATIVE_DENSE_LINEAR_SOLVER_HPP
39 template <
typename NumericT,
typename VectorType>
42 std::vector<std::size_t> pivot_vector,
43 std::size_t current_row)
46 std::size_t pivot_row = current_row;
47 NumericT pivot_element = B(current_row, current_row);
49 for (std::size_t i=current_row; i<B.
size1(); ++i)
51 double element = B(i, current_row);
52 if (std::fabs(element) > std::fabs(pivot_element))
55 pivot_element = element;
60 if (pivot_row > current_row)
62 std::swap(pivot_vector[pivot_row], pivot_vector[current_row]);
63 std::swap(c[pivot_row], c[current_row]);
64 for (std::size_t j=0; j<B.
size2(); ++j)
65 std::swap(B(pivot_row, j), B(current_row, j));
76 template <
typename NumericT,
typename VectorType>
83 log::info<log_linear_solver>() <<
"* solve(): Solving system (Gauss solver, single-threaded)... " << std::endl;
88 std::vector<std::size_t> pivot_vector(B.
size1());
90 for (std::size_t i=0; i<pivot_vector.size(); ++i)
96 for (std::size_t i=0; i<B.
size1(); ++i)
102 throw std::runtime_error(
"Provided matrix is singular!");
105 for (std::size_t elim_row = i+1; elim_row < B.
size1(); ++elim_row)
107 NumericT factor = B(elim_row, i) / B(i,i);
108 c[elim_row] -= c[i] * factor;
109 for (std::size_t j=i; j<B.
size2(); ++j)
110 B(elim_row, j) -= B(i, j) * factor;
117 for (std::size_t i=0; i < B.
size1(); ++i)
119 std::size_t row = B.
size1() - (i+1);
120 for (std::size_t j=row+1; j<B.
size2(); ++j)
121 c[row] -= B(row, j) * c[j];
122 c[row] /= B(row, row);
128 VectorType result(c.size());
129 for (std::size_t i=0; i<result.size(); ++i)
130 result[pivot_vector[i]] = c[i];
137 template <
typename NumericT,
140 VectorType
const & b,
147 for (std::size_t i = 0;
154 RowType
const & row_i = A.
row(i);
156 for (AlongRowIterator col_it = row_i.begin();
157 col_it != row_i.end();
160 A_dense(i, col_it->first) = col_it->second;
Defines a set of checker functors for micro-tests within ViennaSHE.
The main SHE configuration class. To be adjusted by the user for his/her needs.
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
Internal tag used for the specification of a dense linear solver (Gauss, single-threaded)
A configuration class holding options for use within the different linear solvers.
Implementation of various utilities related to linear algebra.
A logging facility providing fine-grained control over logging in ViennaSHE.
void pivot_matrix(viennashe::math::dense_matrix< NumericT > &B, VectorType &c, std::vector< std::size_t > pivot_vector, std::size_t current_row)
std::vector< double > solve(viennashe::math::sparse_matrix< double > &A, std::vector< double > const &b, linear_solver_config const &config)
Public interface for solving a system of linear equations represented using a sparse matrix.
The main ViennaSHE namespace. All functionality resides inside this namespace.
The SHE configuration class is defined here.
Defines all the log keys used within the viennashe::solvers namespace.