3#include "dg/algorithm.h"
22template<
class ContainerType>
43 template<
class MatrixType>
45 const ContainerType& weights,
value_type eps_rel,
47 unsigned max_iter = 500,
unsigned cauchy_steps = 40
48 ) : m_weights(weights),
49 m_exp(exp), m_cauchy( cauchy_steps), m_eps(eps_rel),
50 m_abs(nrmb_correction)
52 m_A = [&](
const ContainerType&
x, ContainerType&
y){
55 m_lanczos.construct( weights, max_iter);
58 const ContainerType rnd = weights;
60 auto T = eigen.
tridiag( A, rnd, weights);
64 template<
class ...Params>
68 *
this =
MatrixSqrt( std::forward<Params>( ps)...);
81 m_benchmark = benchmark;
91 template<
class ContainerType0,
class ContainerType1>
92 void operator()(
const ContainerType0 b, ContainerType1& x)
96 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
101 m_number = m_lanczos.solve(
x, func, m_A, b, m_weights, m_eps, m_abs,
105 DG_RANK0 std::cout <<
"# `"<<m_message<<
"` solve with {"<<m_number<<
","<<m_cauchy<<
"} iterations took "<<t.
diff()<<
"s\n";
109 ContainerType m_weights;
110 std::function< void(
const ContainerType&, ContainerType&)> m_A;
111 std::array<value_type, 2> m_EVs;
113 unsigned m_number, m_cauchy;
115 bool m_benchmark =
true;
116 std::string m_message =
"SQRT";
139template<
class ContainerType>
160 template<
class MatrixType>
162 const ContainerType& weights,
value_type eps_rel,
164 unsigned max_iter = 500,
166 ) : m_weights(weights),
167 m_f_inner(f_inner), m_eps(eps_rel),
168 m_abs(nrmb_correction)
170 m_A = [&](
const ContainerType&
x, ContainerType&
y){
173 m_lanczos.construct( weights, max_iter);
176 template<
class ...Params>
193 m_benchmark = benchmark;
204 template<
class UnaryOp,
class ContainerType0,
class ContainerType1>
205 void operator()( UnaryOp f_outer,
const ContainerType0 b, ContainerType1& x)
209 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
214 m_number = m_lanczos.solve(
x, func, m_A, b, m_weights, m_eps, m_abs,
218 DG_RANK0 std::cout <<
"# `"<<m_message<<
"` solve with {"<<m_number<<
"} iterations took "<<t.
diff()<<
"s\n";
222 ContainerType m_weights;
223 std::function< void(
const ContainerType&, ContainerType&)> m_A;
224 std::array<value_type, 2> m_EVs;
228 bool m_benchmark =
true;
229 std::string m_message =
"Function";
Tridiagonalize and approximate via Lanczos algorithm. A is self-adjoint in the weights .
Definition lanczos.h:154
const dg::TriDiagonal< thrust::host_vector< double > > & tridiag(MatrixType &&A, const ContainerType0 &b, const ContainerType1 &weights, double eps=1e-4, double nrmb_correction=1., std::string error_norm="compute_extreme_EV", double res_fac=1., unsigned q=1)
Tridiagonalization of A using Lanczos method with .
Definition lanczos.h:264
void transform(const ContainerType1 &x, ContainerType &y, UnaryOp op)
void apply(get_value_type< ContainerType1 > alpha, MatrixType &&M, const ContainerType1 &x, get_value_type< ContainerType1 > beta, ContainerType2 &y)
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
std::array< value_type, 2 > compute_extreme_EV(const dg::TriDiagonal< thrust::host_vector< value_type > > &T)
Compute extreme Eigenvalues of a symmetric tridiangular matrix.
Definition tridiaginv.h:727
auto make_FuncEigen_Te1(UnaryOp f)
Create a functor that uses Eigenvalue decomposition to compute for symmetric tridiagonal T.
Definition matrixfunction.h:29
auto make_SqrtCauchyEigen_Te1(int exp, std::array< value_type, 2 > EVs, unsigned stepsCauchy)
Create a functor that computes using either Eigen or SqrtCauchy solve based on whichever is fastest ...
Definition matrixfunction.h:123
Functions for optimizing Contours.
Computation of for self-adjoint positive definite .
Definition matrixsqrt.h:141
void operator()(UnaryOp f_outer, const ContainerType0 b, ContainerType1 &x)
Apply matrix function.
Definition matrixsqrt.h:205
MatrixFunction(MatrixType &A, const ContainerType &weights, value_type eps_rel, value_type nrmb_correction=1., unsigned max_iter=500, std::function< value_type(value_type)> f_inner=[](value_type x){return x;})
Construct from matrix.
Definition matrixsqrt.h:161
unsigned get_iter() const
Get the number of Lanczos iterations in latest call to operator()
Definition matrixsqrt.h:183
dg::get_value_type< ContainerType > value_type
Definition matrixsqrt.h:143
ContainerType container_type
Definition matrixsqrt.h:142
MatrixFunction()=default
Construct empty.
void set_benchmark(bool benchmark, std::string message="Function")
Set or unset performance timings during iterations.
Definition matrixsqrt.h:192
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition matrixsqrt.h:177
Fast computation of for self-adjoint positive definite .
Definition matrixsqrt.h:24
void operator()(const ContainerType0 b, ContainerType1 &x)
Apply matrix sqrt.
Definition matrixsqrt.h:92
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition matrixsqrt.h:65
dg::get_value_type< ContainerType > value_type
Definition matrixsqrt.h:26
unsigned get_iter() const
Get the number of Lanczos iterations in latest call to operator()
Definition matrixsqrt.h:71
ContainerType container_type
Definition matrixsqrt.h:25
void set_benchmark(bool benchmark, std::string message="SQRT")
Set or unset performance timings during iterations.
Definition matrixsqrt.h:80
MatrixSqrt(MatrixType &A, int exp, const ContainerType &weights, value_type eps_rel, value_type nrmb_correction=1., unsigned max_iter=500, unsigned cauchy_steps=40)
Construct from matrix.
Definition matrixsqrt.h:44
MatrixSqrt()=default
Construct empty.
double value_type
Definition tridiaginv_b.cpp:6