This repository was archived by the owner on Oct 22, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinterval.cpp
More file actions
239 lines (227 loc) · 9.48 KB
/
Copy pathinterval.cpp
File metadata and controls
239 lines (227 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <common.hpp>
#include <pybind11/numpy.h>
#include <ipc/utils/interval.hpp>
#ifdef IPC_TOOLKIT_WITH_FILIB
using namespace filib;
#endif
// ============================================================================
#define RETURN_INTERVAL(func) \
[](const Interval& i) -> Interval { return func(i); }
#define OP_II(op) \
[](const Interval& a, const Interval& b) -> Interval { return op; }
#define OP_ID(op) [](const Interval& a, double b) -> Interval { return op; }
#define OP_DI(op) [](const Interval& b, double a) -> Interval { return op; }
#define DEF_OP(name, op) def("__" name "__", op, py::is_operator())
#define DEF_BIN_OP(name, op) \
DEF_OP(name, OP_II(op)).DEF_OP(name, OP_ID(op)).DEF_OP("r" name, OP_DI(op))
#define DEF_UN_OP(name, op) \
DEF_OP(name, [](const Interval& i) -> Interval { return op; })
#define DEF_IOP(name, RHSType, op) \
def( \
"__i" name "__", \
[](Interval& self, RHSType rhs) -> Interval& { \
op; \
return self; \
}, \
py::is_operator())
// ============================================================================
void define_interval(py::module_& m)
{
#ifdef IPC_TOOLKIT_WITH_FILIB
auto m_filib = m.def_submodule(
"filib",
"Python bindings for the `Fast Interval Library (Filib) <https://github.com/zfergus/filib>`_ by Werner Hofschuster and Walter Kraemer.");
py::class_<Interval>(m_filib, "Interval")
.def(py::init())
.def(py::init<double>(), "x"_a)
.def(py::init<double, double>(), "x"_a, "y"_a)
.def(
"__str__",
[](const Interval& self) {
std::stringstream ss;
ss << self;
return ss.str();
})
.def(
"__repr__",
[](const Interval& self) {
std::stringstream ss;
ss << "Interval(" << self.INF << ", " << self.SUP << ")";
return ss.str();
})
.DEF_BIN_OP("add", a + b)
.DEF_UN_OP("pos", +i)
.DEF_IOP("add", const Interval&, self += rhs)
.DEF_IOP("add", double, self += rhs)
.DEF_BIN_OP("sub", a - b)
.DEF_UN_OP("neg", -i)
.DEF_IOP("sub", const Interval&, self -= rhs)
.DEF_IOP("sub", double, self -= rhs)
// .DEF_BIN_OP("mul", a * b)
.DEF_OP(
"mul",
[](const Interval& a, const Interval& b) -> Interval {
if (a == b) {
return sqr(a); // More accurate
}
return a * b;
})
.DEF_OP("mul", OP_ID(a * b))
.DEF_OP("rmul", OP_DI(a * b))
.DEF_IOP("mul", const Interval&, self *= rhs)
.DEF_IOP("mul", double, self *= rhs)
.DEF_BIN_OP("truediv", a / b)
.DEF_IOP("truediv", const Interval&, self /= rhs)
.DEF_IOP("truediv", double, self /= rhs)
.def(py::self == py::self)
.def(py::self == double())
.def(double() == py::self)
.def(py::self != py::self)
.def(py::self != double())
.def(double() != py::self)
.def(py::self < py::self)
.def(py::self < double())
.def(double() < py::self)
.def(py::self <= py::self)
.def(py::self <= double())
.def(double() <= py::self)
.def(py::self > py::self)
.def(py::self > double())
.def(double() > py::self)
.def(py::self >= py::self)
.def(py::self >= double())
.def(double() >= py::self)
.def(
"__contains__",
[](const Interval& self, double x) { return in(x, self); },
py::is_operator())
.def(
"__contains__",
[](const Interval& self, const Interval& i) { return in(i, self); },
py::is_operator())
.def("empty", [](const Interval& self) { return empty(self); })
.DEF_OP("or", OP_II(a | b))
.DEF_OP("and", OP_II(a & b))
.def("mid", [](const Interval& self) { return mid(self); })
.def("diam", [](const Interval& self) { return diam(self); })
.def("drel", [](const Interval& self) { return drel(self); })
.def(
"blow",
[](const Interval& self, double eps) -> Interval {
return blow(self, eps);
})
.def("sqr", RETURN_INTERVAL(sqr))
.def(
"__pow__",
[](const Interval& self, double x) -> Interval {
if (x != 2.0) {
throw std::invalid_argument(
"Only x == 2.0 is supported for Interval**double");
}
return sqr(self);
},
py::is_operator())
.def("sqrt", RETURN_INTERVAL(sqrt))
.def("exp", RETURN_INTERVAL(exp))
.def("exp2", RETURN_INTERVAL(exp2))
.def("exp10", RETURN_INTERVAL(exp10))
.def(
"__rpow__",
[](const Interval& self, double b) -> Interval {
if (b == 2.0) {
return sqr(self);
} else if (b == exp(1.0)) {
return exp(self);
} else if (b == 10.0) {
return exp10(self);
} else {
throw std::invalid_argument(
"Only b in [2.0, e, 10.0] are supported for double**Interval");
}
},
py::is_operator())
.def("expm1", RETURN_INTERVAL(expm1))
.def("log", RETURN_INTERVAL(log))
.def("log2", RETURN_INTERVAL(log2))
.def("log10", RETURN_INTERVAL(log10))
.def("log1p", RETURN_INTERVAL(log1p))
.def("sin", RETURN_INTERVAL(sin))
.def("cos", RETURN_INTERVAL(cos))
.def("cot", RETURN_INTERVAL(cot))
.def("tan", RETURN_INTERVAL(tan))
.def("asin", RETURN_INTERVAL(asin))
.def("acos", RETURN_INTERVAL(acos))
.def("atan", RETURN_INTERVAL(atan))
.def("acot", RETURN_INTERVAL(acot))
.def("arcsin", RETURN_INTERVAL(asin))
.def("arccos", RETURN_INTERVAL(acos))
.def("arctan", RETURN_INTERVAL(atan))
.def("arccot", RETURN_INTERVAL(acot))
.def("sinh", RETURN_INTERVAL(sinh))
.def("cosh", RETURN_INTERVAL(cosh))
.def("tanh", RETURN_INTERVAL(tanh))
.def("coth", RETURN_INTERVAL(coth))
.def("asinh", RETURN_INTERVAL(asinh))
.def("acosh", RETURN_INTERVAL(acosh))
.def("atanh", RETURN_INTERVAL(atanh))
.def("acoth", RETURN_INTERVAL(acoth))
.def("arcsinh", RETURN_INTERVAL(asinh))
.def("arccosh", RETURN_INTERVAL(acosh))
.def("arctanh", RETURN_INTERVAL(atanh))
.def("arccoth", RETURN_INTERVAL(acoth))
.def("erf", RETURN_INTERVAL(erf))
.def("erfc", RETURN_INTERVAL(erfc))
.def_readwrite("INF", &interval::INF)
.def_readwrite("SUP", &interval::SUP);
m_filib.def("disjoint", [](const Interval& i, const Interval& j) {
return disjoint(i, j);
});
m_filib.def("max", [](const Interval& i, const Interval& j) -> Interval {
return max(i, j);
});
m_filib.def("max", [](const Interval& i, double j) -> Interval {
return max(i, j);
});
m_filib.def("max", [](double i, const Interval& j) -> Interval {
return max(i, j);
});
m_filib.def("min", [](const Interval& i, const Interval& j) -> Interval {
return min(i, j);
});
m_filib.def("min", [](const Interval& i, double j) -> Interval {
return min(i, j);
});
m_filib.def("min", [](double i, const Interval& j) -> Interval {
return min(i, j);
});
m_filib.def("sqr", RETURN_INTERVAL(sqr));
m_filib.def("sqrt", RETURN_INTERVAL(sqrt));
m_filib.def("exp", RETURN_INTERVAL(exp));
m_filib.def("exp2", RETURN_INTERVAL(exp2));
m_filib.def("exp10", RETURN_INTERVAL(exp10));
m_filib.def("expm1", RETURN_INTERVAL(expm1));
m_filib.def("log", RETURN_INTERVAL(log));
m_filib.def("log2", RETURN_INTERVAL(log2));
m_filib.def("log10", RETURN_INTERVAL(log10));
m_filib.def("log1p", RETURN_INTERVAL(log1p));
m_filib.def("sin", RETURN_INTERVAL(sin));
m_filib.def("cos", RETURN_INTERVAL(cos));
m_filib.def("cot", RETURN_INTERVAL(cot));
m_filib.def("tan", RETURN_INTERVAL(tan));
m_filib.def("asin", RETURN_INTERVAL(asin));
m_filib.def("acos", RETURN_INTERVAL(acos));
m_filib.def("atan", RETURN_INTERVAL(atan));
m_filib.def("acot", RETURN_INTERVAL(acot));
m_filib.def("sinh", RETURN_INTERVAL(sinh));
m_filib.def("cosh", RETURN_INTERVAL(cosh));
m_filib.def("coth", RETURN_INTERVAL(coth));
m_filib.def("tanh", RETURN_INTERVAL(tanh));
m_filib.def("asinh", RETURN_INTERVAL(asinh));
m_filib.def("acosh", RETURN_INTERVAL(acosh));
m_filib.def("acoth", RETURN_INTERVAL(acoth));
m_filib.def("atanh", RETURN_INTERVAL(atanh));
m_filib.def("erf", RETURN_INTERVAL(erf));
m_filib.def("erfc", RETURN_INTERVAL(erfc));
PYBIND11_NUMPY_DTYPE(Interval, INF, SUP);
#endif
}