chemmisol 0.1
Loading...
Searching...
No Matches
gauss.h
Go to the documentation of this file.
1#ifndef CHEMMISOL_GAUSS_H
2#define CHEMMISOL_GAUSS_H
3
4#include <iostream>
5#include <array>
6#include <complex>
7#include "../logging.h"
8
16namespace chemmisol { namespace gauss {
17
29 template<typename M, typename X>
30 X solve(const M& m, const X& y) {
31 CHEM_LOG(TRACE) << "[GAUSS START]";
32 auto _m = augment(m, y);
33 std::size_t n = _m.size();
34 CHEM_LOG(TRACE) << "Step 0:";
35 for(std::size_t i = 0; i < n; i++) {
36 CHEM_LOG(TRACE) << _m[i];
37 }
38
39 for(std::size_t i = 0; i < n; i++) {
40 for(std::size_t j = i+1; j < n; j++) {
41 auto ratio = _m[j][i]/_m[i][i];
42 for(std::size_t k = 0; k < _m[i].size(); k++) {
43 _m[j][k] = _m[j][k] - ratio*_m[i][k];
44 }
45 }
46 }
47
48 CHEM_LOG(TRACE) << "Step 1:";
49 for(std::size_t i = 0; i < n; i++) {
50 CHEM_LOG(TRACE) << _m[i];
51 }
52
53 X x = y;
54 x[n-1] = _m[n-1][n]/_m[n-1][n-1];
55
56 for(int i = n-2; i>=0; i--) {
57 x[i] = _m[i][n];
58 for(std::size_t j = i+1; j < n; j++) {
59 x[i] = x[i] - _m[i][j]*x[j];
60 }
61 x[i] = x[i]/_m[i][i];
62 }
63 CHEM_LOG(TRACE) << "Step 2:";
64 for(std::size_t i = 0; i < x.size(); i++)
65 CHEM_LOG(TRACE) << "x[" << i << "]=" << x[i];
66 CHEM_LOG(TRACE) << "[GAUSS END]";
67 return x;
68 }
69
81 template<>
82 double solve<double, double>(const double& f, const double& y);
83
84 template<>
85 std::complex<double> solve<std::complex<double>, std::complex<double>>(
86 const std::complex<double>& f, const std::complex<double>& y);
87
88}}
89#endif
X solve(const M &m, const X &y)
Definition gauss.h:30
#define CHEM_LOG(LEVEL)
Definition logging.h:21
Definition chemmisol.h:31
std::array< T, N > X
Definition linear.h:26
M< typename _M::value_type::value_type, std::tuple_size< _M >::value, std::tuple_size< typename _M::value_type >::value+1 > augment(const _M &m, const X< typename _M::value_type::value_type, std::tuple_size< _M >::value > &x)
Definition linear.h:164
std::array< std::array< T, P >, N > M
Definition linear.h:34