在c++中定义分段函数的最佳方法是什么?
的例子:
f1(x) if x from [0, 5)
f(x) = f2(x) if x from [5, 10)
f3(x) if x from [10, 20)
我现在的方法是这样的:
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);
}
}
这种方法缺乏检查x
是否在函数的整体边界内,就像上面例子中的[0,20],我知道,也许命名不是最好的(Function
vs. std::function
等等)。
有什么更聪明的方法吗?该方法使用要在std::map
中排序的键的属性。这与效率无关,更重要的是干净的设计。
切片
不完全是问题的一部分,但在其中一个评论中提到了切片,在这里您可以阅读它。
std::map无法处理多态性?
我在上面的代码中纠正了这个错误
当前设计的一个问题是,它不允许在某些间隔或点(如0)上最自然地被认为是未定义的函数,但是有很多这样的函数,所以这是范围检查的另一个动机。此外,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);
}
};