Extension: Matrix functions
#include "dg/matrix/matrix.h"
Loading...
Searching...
No Matches
sqrt_cauchy.h
Go to the documentation of this file.
1#pragma once
2// #undef BOOST_MATH_MAX_SERIES_ITERATION_POLICY
3// #define BOOST_MATH_MAX_SERIES_ITERATION_POLICY 1000000000
4#include <boost/math/special_functions.hpp>
5
6#include "dg/algorithm.h"
7
9#ifndef M_PI
10#define M_PI 3.14159265358979323846
11#endif
12
13
17namespace dg {
18namespace mat {
19
35template<class Container>
37{
38 public:
47 SqrtCauchyInt( const Container& copyable)
48 {
49 m_helper = m_temp = m_helper3 = copyable;
50 }
52 template<class ...Params>
53 void construct( Params&& ...ps)
54 {
55 //construct and swap
56 *this = SqrtCauchyInt( std::forward<Params>( ps)...);
57 }
58
60 const double& w() const{return m_w;}
61
65 template<class MatrixType>
66 auto make_denominator(MatrixType& A) const{
67 return [&A=A, &w = m_w] ( const auto& x, auto& y)
68 {
69 dg::blas2::symv(A, x, y); // A x
70 dg::blas1::axpby(w*w, x, 1., y); // ( w^2 + A) x
71 };
72 }
73
98 template<class MatrixType0, class MatrixType1, class ContainerType0,
99 class ContainerType1>
100 void operator()(MatrixType0&& A, MatrixType1&& wAinv, const ContainerType0&
101 b, ContainerType1& x, std::array<value_type,2> EVs, unsigned
102 steps, int exp = +1)
103 {
104 dg::blas1::copy(0., m_helper3);
105 value_type s=0.;
106 value_type c=0.;
107 value_type d=0.;
108 m_w=0.;
109 value_type t=0.;
110 value_type minEV = EVs[0], maxEV = EVs[1];
111 value_type sqrtminEV = std::sqrt(minEV);
112 const value_type k2 = minEV/maxEV;
113 const value_type sqrt1mk2 = std::sqrt(1.-k2);
114 const value_type Ks=boost::math::ellint_1(sqrt1mk2 );
115 const value_type fac = 2.* Ks*sqrtminEV/(M_PI*steps);
116 for (unsigned j=1; j<steps+1; j++)
117 {
118 t = (j-0.5)*Ks/steps; //imaginary part .. 1i missing
119 // approx 5e-6 s each evaluation of 3 boost fcts
120 c = 1./boost::math::jacobi_cn(sqrt1mk2, t);
121 s = boost::math::jacobi_sn(sqrt1mk2, t)*c;
122 d = boost::math::jacobi_dn(sqrt1mk2, t)*c;
123 m_w = sqrtminEV*s;
124 dg::blas1::axpby(c*d, b, 0.0 , m_helper); //m_helper = c d b
125 dg::blas2::symv( std::forward<MatrixType1>(wAinv), m_helper, m_temp);
126
127 dg::blas1::axpby(fac, m_temp, 1.0, m_helper3); // m_helper3 += fac (w^2 + A )^(-1) c d x
128 }
129 if( exp > 0)
130 dg::blas2::symv(A, m_helper3, x);
131 else
132 dg::blas1::copy( m_helper3, x);
133 }
134
135 private:
136 Container m_helper, m_temp, m_helper3;
137 value_type m_w;
138};
139
143template< class Container>
145{
146 public:
169 template<class MatrixType>
171 MatrixType& A,
172 const Container& weights,
173 value_type epsCG,
174 unsigned iterCauchy,
175 std::array<value_type,2> EVs, int exp)
176 {
177 m_pcg.construct( weights, 10000);
178 Container m_temp = weights;
179 m_iterCauchy = iterCauchy;
180 m_cauchysqrtint.construct(weights);
181 m_EVs = EVs;
182 m_A = [&]( const Container& x, Container& y){
183 dg::blas2::symv( A, x, y);
184 };
185 m_op = m_cauchysqrtint.make_denominator(A);
186 m_wAinv = [&, eps = epsCG, w = weights]
187 ( const Container& x, Container& y){
188 m_pcg.solve( m_op, y, x, 1., w, eps);
189 };
190 m_exp = exp;
191 }
193 template<class ...Params>
194 void construct( Params&& ...ps)
195 {
196 //construct and swap
197 *this = DirectSqrtCauchy( std::forward<Params>( ps)...);
198 }
206 unsigned operator()(const Container& b, Container& x)
207 {
208 m_cauchysqrtint(m_A, m_wAinv, b, x, m_EVs, m_iterCauchy, m_exp);
209 return m_iterCauchy;
210 }
211 private:
212 unsigned m_iterCauchy;
213 std::function<void ( const Container&, Container&)> m_A, m_wAinv, m_op;
214 dg::PCG<Container> m_pcg;
215 dg::mat::SqrtCauchyInt<Container> m_cauchysqrtint;
216 std::array<value_type,2> m_EVs;
217 int m_exp;
218};
219
220
221} //namespace mat
222} //namespace dg
unsigned solve(MatrixType0 &&A, ContainerType0 &x, const ContainerType1 &b, MatrixType1 &&P, const ContainerType2 &W, double eps=1e-12, double nrmb_correction=1, int test_frequency=1)
void construct(Params &&...ps)
void copy(const ContainerTypeIn &source, ContainerTypeOut &target)
void axpby(value_type alpha, const ContainerType1 &x, value_type1 beta, ContainerType &y)
void symv(MatrixType &&M, const ContainerType1 &x, ContainerType2 &y)
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
dg::DVec Container
Definition lanczos_b.cpp:19
Functions for optimizing Contours.
#define M_PI
M_PI is non-standard ... so MSVC complains.
Definition sqrt_cauchy.h:10
Shortcut for solve directly via sqrt Cauchy combined with PCG inversions.
Definition sqrt_cauchy.h:145
Container container_type
Definition sqrt_cauchy.h:147
unsigned operator()(const Container &b, Container &x)
Compute via sqrt Cauchy integral solve.
Definition sqrt_cauchy.h:206
DirectSqrtCauchy(MatrixType &A, const Container &weights, value_type epsCG, unsigned iterCauchy, std::array< value_type, 2 > EVs, int exp)
Construct DirectSqrtCauchy.
Definition sqrt_cauchy.h:170
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition sqrt_cauchy.h:194
DirectSqrtCauchy()
empty object ( no memory allocation)
Definition sqrt_cauchy.h:150
dg::get_value_type< Container > value_type
Definition sqrt_cauchy.h:148
Cauchy integral
Definition sqrt_cauchy.h:37
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition sqrt_cauchy.h:53
dg::get_value_type< Container > value_type
Definition sqrt_cauchy.h:40
void operator()(MatrixType0 &&A, MatrixType1 &&wAinv, const ContainerType0 &b, ContainerType1 &x, std::array< value_type, 2 > EVs, unsigned steps, int exp=+1)
Cauchy integral
Definition sqrt_cauchy.h:100
auto make_denominator(MatrixType &A) const
Definition sqrt_cauchy.h:66
SqrtCauchyInt(const Container &copyable)
Construct Rhs operator.
Definition sqrt_cauchy.h:47
SqrtCauchyInt()
Definition sqrt_cauchy.h:41
const double & w() const
The in .
Definition sqrt_cauchy.h:60
Container container_type
Definition sqrt_cauchy.h:39
double value_type
Definition tridiaginv_b.cpp:6