c++ - 如何在 IR 中表示数学域?

标签 c++ math mathematical-morphology

我想从约束列表中定义一个代表数学域的对象,但我不清楚如何去做。

例如,我从 IR 开始,我有以下约束:

  • x > 0
  • x 不在]3,5]
  • x 不在 [7,12[

那么,我的域是 ]0,3] U ]5,7[ U [12,+oo .

我怎样才能很好地将它存储在 C++ 结构中?您以前做过吗?此外,我希望能够轻松检查域是否为空。

最佳答案

除非您想使用评论中提到的“第 3 方”工具,否则您必须编写自己的 Interval 类。 为此,您可以执行以下操作:

class Interval{
  struct Range{
     bool leftInclusive, rightInclusive;
     double left, right;
     bool operator<(Range other){return left<other.left;}
  }
  std::Set<Range> trueRanges;
  void addTrueRange(Range r){
    //check for overlaps
    //merge if overlapping
    //otherwise add to trueRanges
  }
  bool trueAt(double at){
   //find the range with the highest left-bound lower than at
   auto candidate = truethRanges.upper_bound(at); 

   if(candidate == trueRanged.end()) return false; // no range found

   //on-point checking here
   if(at <= candidate->left) return false;
   if(at >= candidate->right) return false;
   return true;
 }
}

此处省略了点检查,因为您不能简单地说 doubleOne == doubleTwo因为这可能会导致漏报。所以你必须说 ABS(doubleOne-doubleTwo) < tinyValue .

要查找重叠,您可以查看 this .

关于c++ - 如何在 IR 中表示数学域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30574957/

相关文章:

c++ - Z-Buffer 的高效实现

c - math.h 默认舍入模式不明确

c# - .Net 的好的统计数学包是什么?

math - 将一个坐标系的旋转矩阵变换到另一个坐标系

matlab - 两个相连边界的形态分离

image-processing - 图像膨胀和腐 eclipse 的实现

c++ - 如果编译器可以自行内联函数,为什么内联函数必须在 header 中为 "defined"

属于模板类的成员模板的 C++ 显式特化

arduino 的 C++ - 错误 : class does not name a type

python - 用于平滑零区域的 Numpy 过滤器