编写方法的不同方法
different way to write a method
本文关键字:方法 更新时间:2023-10-16
我有一个类来定义微分方程的右手侧术语:此类提供计算RHS功能及其衍生的方法该函数以这种方式存储在向量容器中,同类也适用于微分方程系统。
在这里定义我要更改的方法的接口
template <typename Type = double>
class rhsODEProblem {
using analysisFunction = std::function<const Type(const Type, const std::valarray<Type>)>;
public:
rhsODEProblem(const std::vector<std::function<const Type(const Type,const std::valarray<Type>)>> numfun ,
const std::vector<std::function<const Type(const Type,const Type)>> exactfun ,
const Type,const Type,const Type,const std::valarray<Type>,const std::string ) noexcept ;
rhsODEProblem(const std::vector<std::function<const Type(const Type,const std::valarray<Type>)>> numfun,
const Type, const Type, const Type, const std::valarray<Type> ) noexcept ;
virtual ~rhsODEProblem() = default ;
rhsODEProblem(const rhsODEProblem &) = default ;
rhsODEProblem(rhsODEProblem&& ) = default ;
rhsODEProblem& operator=(const rhsODEProblem&) = default ;
rhsODEProblem& operator=(rhsODEProblem&& ) = default ;
const std::vector<std::function<const Type(const Type,const std::valarray<Type>)>> numericalFunction ;
const std::vector<std::function<const Type(const Type,const Type)>> analiticalFunction ;
const std::vector<analysisFunction>& f = numericalFunction ;
const auto dfdt(std::size_t indx , const Type t , const std::valarray<Type> u) {
return (f[indx](t, u+eps )-f[indx](t,u))/eps ;
}
auto setRhs (const std::vector<
std::function<const Type(const Type,const std::valarray<Type>)>> numfun) noexcept
{
for(auto i=0 ; i < numfun.size() ; i++)
{
numericalFunction.push_back(numfun.at(i)) ;
}
}
auto setExact(const std::vector<std::function<const Type(const Type,const Type)>> exactfun) noexcept
{
for(auto i=0 ; i < exactfun.size(); i++)
{
analiticalFunction.push_back(exactfun.at(i));
}
}
auto solveExact() noexcept ;
const Type t0() const noexcept { return _t0 ;}
const Type tf() const noexcept { return _tf ;}
const Type dt() const noexcept { return _dt ;}
const std::valarray<Type> u0() const noexcept { return _u0 ;}
const std::string fname () const noexcept { return filename ;}
//---
private:
Type _t0 ; // start time
Type _tf ; // final time
Type _dt ; // time-step
std::valarray<Type> _u0 ; // iv
std::string filename ;
Type eps = 1e-12 ;
};
我想以以下语法dfdt[index]( t , u_valarray )
而不是dfdt(index, t, u_valarray )
更改方法DFDT我可以以哪种方式更改此方法?
edit 感谢您的回答,因此在我的情况下是:
foo_helper(foo &, int index);
operator()(int n, Type t, std::valarray<Type> u );
对吗?
编辑不,我没有明白。我写道:
class dfdx {
public:
dfdx( rhsODEProblem<Type> &r , int index_ ) : index{index_ } {}
void operator()(Type t, std::valarray<Type> u){
return (f[index](t, u + eps )-f[index](t,u))/eps ;
}
int index ;
};
dfdx operator[](std::size_t index) {
return dfdx(*this, index);
}
然后我以这种方式称呼它:
rhs.dfdx[j](t , uOld) )
但是我有一个错误:
BackwardEulerSolver.H:114:50: error: invalid use of ‘class mg::numeric::odesystem::rhsODEProblem<double>::dfdx’
( 1- dt() * rhs.dfdx[j](t , uOld) ) ;
~~~~^~~~
我想以以下语法
dfdt[index]( t , u_valarray )
来更改方法DFDT,而不是dfdt(index, t, u_valarray )
,以哪种方式可以更改此方法?
您可以执行索引运算符([]
)并返回内部辅助类型的超载,该类型超载呼叫操作员(()
)。
这是一个草图:
class foo {
class foo_helper;
friend class foo_helper;
public:
class foo_helper {
public:
foo_helper(foo &, int index);
void operator()(int n, double y);
};
foo_helper operator[](int index) {
return foo_helper(*this, index);
}
};
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 枚举环境变量的惯用C++14/C++17方法
- 初始化具有非默认构造函数的std::数组项的更好方法