🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
UnarySolver.hpp
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstMath/Function.hpp"
25#include "AstMath/SolverStats.h"
26#include <type_traits>
27
28
29AST_NAMESPACE_BEGIN
30
31
32constexpr const double kDefaultRelTol = 1e-14;
33constexpr const double kDefaultAbsTol = 1e-12;
34constexpr const int kDefaultMaxIter = 100;
35
42AST_MATH_CAPI bool aIsClose(double a, double b, double relTol = kDefaultRelTol, double absTol = kDefaultAbsTol);
43
44
45
48class AST_MATH_API IUnarySolver
49{
50public:
51 virtual ~IUnarySolver() = default;
52
58 virtual errc_t solve(UnaryScalarFunc& func, double min, double max, double& result) = 0;
59};
60
61
65class AST_MATH_API UnarySolver: public IUnarySolver
66{
67public:
68 using IUnarySolver::solve;
69
75 UnarySolver(double relTol, double absTol, int maxIter);
76
79 UnarySolver(double absTol);
80
81 ~UnarySolver() override = default;
82
84 double getRelTol() const{return relTol_;};
86 double getAbsTol() const{return absTol_;};
88 int getMaxIter() const{return maxIter_;};
89
91 void setRelTol(double relTol){relTol_ = relTol;};
93 void setAbsTol(double absTol){absTol_ = absTol;};
95 void setMaxIter(int maxIter){maxIter_ = maxIter;};
96
98 const SolverStats& getStats() const{return stats_;};
99
106 template<typename Func>
107 typename std::enable_if<!std::is_base_of<UnaryScalarFunc, typename std::remove_pointer<Func>::type>::value, errc_t>::type
108 solve(Func func, double min, double max, double& result) {
109 UnaryScalarGenericFunc<Func> adapter(std::move(func));
110 return this->solve(adapter, min, max, result);
111 }
112protected:
113 static double unarycfunc(double x, void* params);
114protected:
115 double relTol_;
116 double absTol_;
119};
120
122
123
124AST_NAMESPACE_END
一元方程求解器接口
定义 UnarySolver.hpp:49
virtual errc_t solve(UnaryScalarFunc &func, double min, double max, double &result)=0
求解一元方程
一元标量值函数
定义 UnaryScalarFunc.hpp:31
泛型一元函数适配器
定义 UnaryScalarFunc.hpp:48
一元方程求解器
定义 UnarySolver.hpp:66
int maxIter_
最大迭代次数
定义 UnarySolver.hpp:117
void setRelTol(double relTol)
设置相对误差容限
定义 UnarySolver.hpp:91
SolverStats stats_
求解器统计信息
定义 UnarySolver.hpp:118
double absTol_
绝对误差容限
定义 UnarySolver.hpp:116
const SolverStats & getStats() const
获取求解器统计信息
定义 UnarySolver.hpp:98
void setMaxIter(int maxIter)
设置最大迭代次数
定义 UnarySolver.hpp:95
double getAbsTol() const
获取绝对误差容限
定义 UnarySolver.hpp:86
std::enable_if<!std::is_base_of< UnaryScalarFunc, typenamestd::remove_pointer< Func >::type >::value, errc_t >::type solve(Func func, double min, double max, double &result)
求解一元方程(支持lambda函数)
定义 UnarySolver.hpp:108
double getRelTol() const
获取相对误差容限
定义 UnarySolver.hpp:84
double relTol_
相对误差容限
定义 UnarySolver.hpp:115
int getMaxIter() const
获取最大迭代次数
定义 UnarySolver.hpp:88
void setAbsTol(double absTol)
设置绝对误差容限
定义 UnarySolver.hpp:93
bool aIsClose(double a, double b, double relTol, double absTol)
判断两个数是否接近
定义 UnarySolver.cpp:27
求解器统计信息
定义 SolverStats.h:30