我正在从用于解决ODE的类层次结构过渡到用于解决ODE系统的类。

在使用单个函数的实现中,我使用以下代码存储函数:

std::function<const Type(const Type, const Type)> numericalFunction

我有一个包装器来评估数值函数:
Type f (Type t, Type u) const noexcept { return numericalFunction(t,u); }

现在,我要解决一个方程组,因此我需要存储多个函数。我试图使用std::vector存储函数集,如下所示:
std::vector<std::function<const Type(const Type,const Type)>> numericalFunction;

我希望能够使用与上述问题相同的语法。那是,
f[0](12,32);应该执行numericalFunction.at(0)(12,32);dfdt[0](t, u);应该执行(numericalFunction.at(0)(t, u+eps) - numericalFunction.at(0)(t, u))/eps;
如何编写代码以允许这种语法?

编辑我有一个问题..现在我需要更改功能
std::vector<std::function<const Type(const Type,const Type)>> numericalFunction;

成为:
std::vector<std::function<const Type(const Type,const std::vector<Type>)>> numericalFunction;

而且派生类不起作用

最佳答案

您可以将功能集合存储为:

std::vector<std::function<const Type(const Type t, const Type u)>> numericalFunList;

获得正确功能的等效功能将是:
const Type f(size_t idx, const Type t, const Type u) { return numericalFunList.at(idx)(t,u); }
std::vector在内部是一个动态数组。这为每个函数提供了从0numericalFunList.size() - 1的索引。您可以通过 vector 中每个函数的索引来标识它们。

编辑1:

OP倾向于使用以下语法:f[0](arg1, arg2);
上面很容易实现:numericalFunList[0](1.2, 5.9);
如果为了简化起见需要f(对于数学家,程序员会讨厌),
auto& f = numericalFunList;
f[0](10.9, 2.4);

提示,请考虑不要全局使用f作为变量名。如果首选f,请使用auto& f = numericalFunList为您完成。

编辑2:

对于新的语法要求,您需要使用两个类,每个类具有一个运算符重载:一个存储带有[]运算符重载的派生集,另一个用于存储带()运算符重载的派生集。
typedef long double Type;
constexpr Type eps = 1E-12;

using analysisFunction = std::function<const Type(const Type, const Type)>;

class derivatives {
    private:
        class derivative;
    public:
        derivatives(std::vector<analysisFunction>& p_fList) { fList = &p_fList; };
        derivative operator[](int index) {
            return derivative((*fList).at(index));
        }
    private:
        class derivative {
            public:
                derivative(analysisFunction& p_f) { f = &p_f; }
                Type operator()(Type t, Type u) {
                    return (((*f)(t, u + eps)) - ((*f)(t,u)))/eps;
                }
            private:
                analysisFunction *f;
        };
        std::vector<analysisFunction> *fList; //dangling pointer
};

用法示例
int main ()
{
    std::vector <analysisFunction> numericalFunctions;
    auto& f = numericalFunctions;

    f.push_back([](Type t, Type u)->Type { return u*u; });


    std::cout << std::setprecision(16);
    std::cout << f[0](5.0, 10.0) << '\n';

    derivatives dfdt(f);
    std::cout << dfdt[0](5.0, 10.0);
    return 0;
}

测试代码:GeeksForGeeks Online IDE

注意:fList是一个悬空指针。有几种处理方法。如果可以保证不会释放所引用的对象,或者可以在对象中维护一个副本(这会占用内存和CPU),则可以保留原样。

10-02 07:12
查看更多