chemmisol 0.1
Loading...
Searching...
No Matches
homotopy.h
1#ifndef CHEMMISOL_HOMOTOPY_H
2#define CHEMMISOL_HOMOTOPY_H
3
4#include "newton.h"
5
6namespace chemmisol {
7
8 template<typename X>
9 class H {
10 private:
11 double t;
12 const std::function<X(const X&)>& f; // target system
13 const std::function<X(const X&)>& g; // start system
14 public:
15 H(
16 double t,
17 const std::function<X(const X&)>& f,
18 const std::function<X(const X&)>& g)
19 : t(t), f(f), g(g) {
20 }
21
22 X operator()(const X& x) {
23 return typename X::value_type(1-t) * g(x)
24 + typename X::value_type(t) * f(x);
25 }
26 };
27
28 template<typename X, typename M>
29 class dH {
30 private:
31 double t;
32 const std::function<M(const X&)>& df;
33 const std::function<M(const X&)>& dg;
34 public:
35 dH(
36 double t,
37 const std::function<M(const X&)>& df,
38 const std::function<M(const X&)>& dg)
39 : t(t), df(df), dg(dg) {
40 }
41
42 M operator()(const X& x) {
43 return typename X::value_type(1-t) * dg(x)
44 + typename X::value_type(t) * df(x);
45 }
46 };
47
48 template<typename X, typename M>
49 class Homotopy {
50 private:
51 std::list<X> x0;
52 std::function<X(const X&)> f; // target system
53 std::function<M(const X&)> df;
54 std::function<X(const X&)> g; // start system
55 std::function<M(const X&)> dg;
56
57 X solve_iter(double t, double delta_t, const X& x, std::size_t n) const;
58
59 public:
61 const std::list<X>& x0,
62 std::function<X(const X&)> f,
63 std::function<M(const X&)> df,
64 std::function<X(const X&)> g,
65 std::function<M(const X&)> dg
66 ) :
67 x0(x0), f(f), df(df), g(g), dg(dg) {
68 }
69
70 std::list<X> solve_iter(std::size_t n) const;
71 };
72
73 template<typename X, typename M>
74 std::list<X> Homotopy<X, M>::solve_iter(std::size_t n) const {
75 std::list<X> results;
76 for(const auto& x : x0) {
77 double delta = 1.0/n;
78 results.push_back(solve_iter(delta, delta, x, n));
79 }
80 return results;
81 }
82
83 template<typename X, typename M>
84 X Homotopy<X, M>::solve_iter(
85 double t, double delta_t, const X& x, std::size_t n
86 ) const {
87 CHEM_LOG(TRACE) << "[HOMOTOPY] Current X: " << x << " (t=" << t << ")";
88 Newton<X, M> newton(
89 x,
90 H<X>(t, f, g),
91 dH<X, M>(t, df, dg));
92
93 X xt = newton.solve_iter(n);
94 if(t < 1.0) {
95 t = t + delta_t;
96 return solve_iter(t, delta_t, xt, n);
97 }
98 return xt;
99 }
100}
101#endif
Definition homotopy.h:9
Definition homotopy.h:49
Definition homotopy.h:29
#define CHEM_LOG(LEVEL)
Definition logging.h:21
Definition chemmisol.h:31
std::array< T, N > X
Definition linear.h:26
std::array< std::array< T, P >, N > M
Definition linear.h:34