简体   繁体   English

定义分段函数(例如多项式)

[英]Defining a piecewise function (e.g. polynomial)

What's the best way to define a piecewise function in C++, needed for example when working with splines? 在C ++中定义分段函数的最佳方法是什么,例如在使用样条函数时需要?

Example: 例:

        f1(x) if x from [0, 5)
f(x) =  f2(x) if x from [5, 10)
        f3(x) if x from [10, 20)

My current approach looks like this: 我目前的方法如下:

class Function
{
    virtual double operator()( double x ) = 0;
}

class SomeFun : public Function
{
   // implements operator() in a meaningful way
}

class PiecewiseFunction : public Function
{
    // holds functions along with the upper bound of the interval
    // for which they are defined
    // e.g. (5, f1), (10, f2), (20, f3)
    std::map< double, Function* > fns;

    virtual double operator()( double x )
    {
       // search for the first upper interval boundary which is greater than x
       auto it = fns.lower_bound( x );
       // ... and evaluate the underlying function.
       return *(it->second)(x);
    }
}

This approach lacks of checking if x is in the overall bounds of the function, like [0, 20) in the example above, I know, and perhaps the naming is not the best ( Function vs. std::function and so on). 这种方法缺乏检查x是否在函数的整体范围内,如上例中的[0,20],我知道,也许命名不是最好的( Functionstd::function等) 。

Any ideas how to do that in a smarter way? 任何想法如何以更智能的方式做到这一点? The approach uses the property of the keys to be sorted in a std::map . 该方法使用键的属性在std::map进行排序。 It's not about efficiency, it's more about a clean design. 这不是关于效率,而是关于清洁设计。

SLICING SLICING

Not exactly part of the question, but in one of the comments, slicing is mentioned, here you can read about it. 不完全是问题的一部分,但在其中一条评论中,提到了切片 ,在这里你可以阅读它。

std::map unable to handle polymorphism? std :: map无法处理多态?

I corrected this in my code above. 我在上面的代码中更正了这一点。

An issue with the current design it doesn't allow for functions that are most naturally thought of as being undefined over certain intervals or points (like 0), but there are plenty of these, so it's another motivation for range-checking. 当前设计的一个问题是它不允许在某些间隔或点(如0)上最自然地被认为是未定义的函数,但是有很多这些函数,因此它是范围检查的另一个动机。 Also Function needs to be replaced with Function* which necessitates some other changes in syntax. 此外, Function需要替换为Function* ,这需要对语法进行一些其他更改。

class PiecewiseFunction : public Function
{
    //Holds function and interval
    std::map< std::pair<double,double>, Function* > fns;

   double operator()( double x )
   {
           auto iter = std::find_if(fns.cbegin(), fns.cend(), 
                             [=](const std::pair< std::pair<double,double>, Function*>& fn) 
                             {  
                                return x>=fn.first.first &&  x<fn.first.second; 
                             });

       if (iter == fns.end()) throw... //Or something
       return (*iter->second)(x);
    }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较函数放在哪里(例如std :: sort)? - Where to put comparison function for use with (e.g.) std::sort? 为什么成员函数需要'&'(例如在std :: bind中)? - Why does a member function needs '&' (e.g. in std::bind)? Libclang:在 SourceLocation 获取符号(例如函数) - Libclang: Get Symbol (e.g. function) at SourceLocation 没有匹配的 function 来调用:错误:必须使用 '.*' 或 '-&gt;*' 来调用指向成员 function 在 'f (...)' 中的指针,例如 '(... -&gt;* f) (...)' - No matching function to call: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘f (…)’, e.g. ‘(… ->* f) (…)’ 隐式函数到函数指针转换不适用于标准容器(例如向量) - Implicit function-to-function-pointer conversion not working for std containers (e.g. vector) 具有大阶乘的数学(例如除法?) - Mathematics with large factorials (e.g. division?) 模板 arguments 中的括号,例如 std::function<int(int, float)></int(int,> - Parentheses inside template arguments e.g. std::function<int(int, float)> 必须使用'。*'或' - > *'在'lessThan(...)'中调用指向成员的函数,例如'(... - > * lessThan)(...)' - must use '.*' or '->*' to call pointer-to-member function in 'lessThan (…)', e.g. '(… ->* lessThan) (…)' 如何在链接库中查看函数背后的代码,例如OpenCV - How to view the code behind a function in a linked-to library e.g. OpenCV 从函数返回const char *的正确方法,例如,重写std :: exception :: what() - Proper method of returning const char* from a function, e.g., overriding std::exception::what()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM