检查函数是否单调
Checking whether a function is monotonic or not
我有一个函数,我想检查它在特定域内是否是单调的。我可以替换域中的所有点,并检查函数的单调性,但这是一种相当天真的方法。在C++中,有没有其他方法可以检查函数在域内是否单调?
只需做一些类似的事情
enum monotonic_e {
MONOTONIC_INCREASE, /* function is monotonically increasing */
MONOTONIC_DECREASE, /* function is monotonically decreasing */
MONOTONIC_CONSTANS, /* function is constant */
MONOTONIC_NOT /* function is not monotonic */
};
double sign(double x) {
return x < 0 ? -1. : (x > 0 ? 1. : 0.);
}
monotonic_e monotonic(const function< double(double) >& f,
double a, double b, double eps) {
double x = a, y1 = f(x), y2 = f(x + eps);
double d = y2 - y1;
double s = sign(d);
while( x < b ) {
x += eps;
y1 = y2;
y2 = f(x + eps);
d = y2 - y1;
if( s == 0. ) {
s = sign(d);
}
if( s * d < 0 ) {
return MONOTONIC_NOT;
}
}
return s > 0 ? MONOTONIC_INCREASE :
(s < 0 ? MONOTONIC_DECREASE :
MONOTONIC_CONSTANS);
}
如果您必须检查一个巨大的值范围,您可以通过将整个范围划分为更小的值来并行化。
以下是由有限个点组成的域的特殊情况的示例实现(你不能用C++真正检查任意数学函数及其范围——没有足够的内存/CPU能力来存储/检查无限的范围):
std::function<double(double)> myFunction; //Set this to your function
std::vector<double> domain; //Fill this in with your domain of interest
double previous_value = std::numeric_limits<double>::min();
auto first_non_mono_value =
std::find_if(domain.begin(), domain.end(),
[&myFunction](double this_input)
{
double this_output = myFunction(this_input);
if(previous_value > this_output)
return true; //Violation of monotonic assumption
previous_value = this_output;
return false; //Still may be monitonic (need to check next point)
});
if(first_non_mono_value == domain.end())
{
cout << "MONOTONIC" << endl;
}
else
{
cout << "Not monotonic" << endl;
}
警告:我没有编译这个——所以它可能有错误,但你已经大致了解了。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++/Linux中设置单调时钟的一些技巧
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否需要删除包含对象的"pair"?
- 是否可以从int转换为enum类类型
- 无论条件是否为true,if总是在c++中执行
- 如何找到大小'x'数组是否完全填充,在C++?
- 检查值是否在集合p1和p2中,但不在p3中
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 在没有单调时钟的系统上,是否不可能实现一致性
- 检查函数是否单调