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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345

use crate::{interval::*, simd::*};

// NOTE: eq is implemented in interval.rs

impl Interval {
/// Returns true if rhs is a member of self ($\rhs β \self$).
///
/// If rhs is not a real number, false is returned.
///
/// # Examples
///
/// 
/// use inari::*;
/// assert!(const_interval!(1.0, 2.0).contains(1.0));
/// assert!(!Interval::EMPTY.contains(1.0));
/// assert!(Interval::ENTIRE.contains(1.0));
/// 
///
/// $Β±β$ and NaN are not real numbers, thus do not belong to any interval:
///
/// 
/// use inari::*;
/// assert!(!Interval::ENTIRE.contains(f64::INFINITY));
/// assert!(!Interval::ENTIRE.contains(f64::NEG_INFINITY));
/// assert!(!Interval::ENTIRE.contains(f64::NAN));
/// 
pub fn contains(self, rhs: f64) -> bool {
rhs.is_finite() & {
// a β€ c  β§  c β€ b
//   βΊ -c β€ -a  β§  c β€ b
//   βΊ all([-c; c] .β€ [-a; b])
all(le(neg0(splat(rhs)), self.rep))
}
}

/// Returns true if self and rhs are disjoint ($\self β© \rhs = β$).
///
/// The formal definition is:
///
/// $$/// βx β \self, βy β \rhs : x β y. ///$$
///
/// # Examples
///
/// 
/// use inari::*;
/// assert!(const_interval!(1.0, 2.0).disjoint(const_interval!(3.0, 4.0)));
/// assert!(!const_interval!(1.0, 3.0).disjoint(const_interval!(3.0, 4.0)));
/// assert!(!const_interval!(1.0, 5.0).disjoint(const_interval!(3.0, 4.0)));
/// assert!(Interval::EMPTY.disjoint(Interval::EMPTY));
/// assert!(Interval::EMPTY.disjoint(Interval::ENTIRE));
/// 
pub fn disjoint(self, rhs: Self) -> bool {
self.either_empty(rhs) | {
// b < c  β¨  d < a
//   βΊ any([b; d] .< [c; a])
any(lt(
shuffle13(self.rep, rhs.rep),
neg(shuffle02(rhs.rep, self.rep)),
))
}
}

/// Returns true if self is interior to rhs.
///
/// The formal definition is:
///
/// $$/// (βx β \self, βy β \rhs : x < y) β§ (βx β \self, βy β \rhs : y < x). ///$$
///
/// # Examples
///
/// 
/// use inari::*;
/// assert!(const_interval!(1.1, 1.9).interior(const_interval!(1.0, 2.0)));
/// assert!(!const_interval!(1.1, 2.0).interior(const_interval!(1.0, 2.0)));
/// assert!(Interval::EMPTY.interior(Interval::EMPTY));
/// assert!(Interval::ENTIRE.interior(Interval::ENTIRE));
/// 
pub fn interior(self, rhs: Self) -> bool {
// self = β  β¨  b < d  β¨  b = d = +β
let l = self.is_empty()
|| self.sup_raw() < rhs.sup_raw()
|| all(eq(shuffle13(self.rep, rhs.rep), splat(f64::INFINITY)));
// rhs = β  β¨  c < a  β¨  a = c = -β
let r = self.is_empty()
|| rhs.inf_raw() < self.inf_raw()
|| all(eq(shuffle02(self.rep, rhs.rep), splat(f64::INFINITY)));
l && r
}

/// Returns true if self is nonempty and bounded.
///
/// # Examples
///
/// 
/// use inari::*;
/// assert!(const_interval!(1.0, 2.0).is_common_interval());
/// assert!(!const_interval!(1.0, f64::INFINITY).is_common_interval());
/// assert!(!Interval::EMPTY.is_common_interval());
/// assert!(!Interval::ENTIRE.is_common_interval());
/// 
pub fn is_common_interval(self) -> bool {
// -β < a  β§  b < +β
all(lt(self.rep, splat(f64::INFINITY)))
}

/// Returns true if self is empty ($\self = β$).
///
/// # Examples
///
/// 
/// use inari::*;
/// assert!(!const_interval!(1.0, 1.0).is_empty());
/// assert!(Interval::EMPTY.is_empty());
/// assert!(!Interval::ENTIRE.is_empty());
/// 
pub fn is_empty(self) -> bool {
extract0(self.rep).is_nan()
}

/// Returns true if $\self = $-β, +β$$.
///
/// # Examples
///
/// 
/// use inari::*;
/// assert!(!const_interval!(1.0, f64::INFINITY).is_entire());
/// assert!(!Interval::EMPTY.is_entire());
/// assert!(Interval::ENTIRE.is_entire());
/// 
pub fn is_entire(self) -> bool {
all(eq(self.rep, splat(f64::INFINITY)))
}

/// Returns true if self consists of a single real number.
///
/// # Examples
///
/// 
/// use inari::*;
/// assert!(const_interval!(1.0, 1.0).is_singleton());
/// assert!(!const_interval!(1.0, 2.0).is_singleton());
/// assert!(!Interval::EMPTY.is_singleton());
/// assert!(!Interval::ENTIRE.is_singleton());
/// 
///
/// 0.1 is not a member of f64:
///
/// 
/// use inari::*;
/// // The singleton set that consists of the nearest f64 number to 0.1.
/// assert!(const_interval!(0.1, 0.1).is_singleton());
/// // The tightest interval that encloses 0.1.
/// #[cfg(feature = "gmp")]
/// assert!(!interval!("[0.1, 0.1]").unwrap().is_singleton());
/// 
pub fn is_singleton(self) -> bool {
// a = d
self.inf_raw() == self.sup_raw()
}

/// Returns true if self is weakly less than rhs.
///
/// The formal definition is:
///
/// $$/// (βx β \self, βy β \rhs : x β€ y) β§ (βy β \rhs, βx β \self : x β€ y). ///$$
///
/// # Examples
///
/// 
/// use inari::*;
/// assert!(const_interval!(1.0, 2.0).less(const_interval!(3.0, 4.0)));
/// assert!(const_interval!(1.0, 3.0).less(const_interval!(2.0, 4.0)));
/// assert!(const_interval!(1.0, 4.0).less(const_interval!(1.0, 4.0)));
/// assert!(Interval::EMPTY.less(Interval::EMPTY));
/// assert!(!Interval::EMPTY.less(Interval::ENTIRE));
/// assert!(!Interval::ENTIRE.less(Interval::EMPTY));
/// assert!(Interval::ENTIRE.less(Interval::ENTIRE));
/// 
pub fn less(self, rhs: Self) -> bool {
// self = β  β¨  b β€ d
let l = self.is_empty() || self.sup_raw() <= rhs.sup_raw();
// rhs = β  β¨  a β€ c
let r = rhs.is_empty() || self.inf_raw() <= rhs.inf_raw();
l && r
}

/// Returns true if self is to the left of but may touch rhs.
///
/// The formal definition is:
///
/// $$/// βx β \self, βy β \rhs : x β€ y. ///$$
///
/// # Examples
///
/// 
/// use inari::*;
/// assert!(const_interval!(1.0, 2.0).precedes(const_interval!(3.0, 4.0)));
/// assert!(const_interval!(1.0, 3.0).precedes(const_interval!(3.0, 4.0)));
/// assert!(!const_interval!(1.0, 3.0).precedes(const_interval!(2.0, 4.0)));
/// assert!(Interval::EMPTY.precedes(Interval::EMPTY));
/// assert!(Interval::EMPTY.precedes(Interval::ENTIRE));
/// assert!(Interval::ENTIRE.precedes(Interval::EMPTY));
/// assert!(!Interval::ENTIRE.precedes(Interval::ENTIRE));
/// 
pub fn precedes(self, rhs: Self) -> bool {
// self = β  β¨  rhs = β  β¨  b β€ c
self.either_empty(rhs) | (self.sup_raw() <= rhs.inf_raw())
}

/// Returns true if self is strictly less than rhs.
///
/// The formal definition is:
///
/// $$/// (βx β \self, βy β \rhs : x < y) β§ (βy β \self, βx β \rhs : x < y). ///$$
///
/// # Examples
///
/// 
/// use inari::*;
/// assert!(const_interval!(1.0, 2.0).strict_less(const_interval!(3.0, 4.0)));
/// assert!(const_interval!(1.0, 3.0).strict_less(const_interval!(2.0, 4.0)));
/// assert!(!const_interval!(1.0, 4.0).strict_less(const_interval!(2.0, 4.0)));
/// assert!(const_interval!(1.0, f64::INFINITY).strict_less(const_interval!(2.0, f64::INFINITY)));
/// assert!(Interval::EMPTY.strict_less(Interval::EMPTY));
/// assert!(!Interval::EMPTY.strict_less(Interval::ENTIRE));
/// assert!(!Interval::ENTIRE.strict_less(Interval::EMPTY));
/// assert!(Interval::ENTIRE.strict_less(Interval::ENTIRE));
/// 
pub fn strict_less(self, rhs: Self) -> bool {
// self = β  β¨  b < d  β¨  b = d = +β
let l = self.is_empty()
|| self.sup_raw() < rhs.sup_raw()
|| all(eq(shuffle13(self.rep, rhs.rep), splat(f64::INFINITY)));
// rhs = β  β¨  a < c  β¨  a = c = -β
let r = rhs.is_empty()
|| self.inf_raw() < rhs.inf_raw()
|| all(eq(shuffle02(self.rep, rhs.rep), splat(f64::INFINITY)));
l && r
}

/// Returns true if self is strictly to the left of rhs.
///
/// The formal definition is:
///
/// $$/// βx β \self, βy β \rhs : x < y. ///$$
pub fn strict_precedes(self, rhs: Self) -> bool {
// self = β  β¨  rhs = β  β¨  b < c
self.either_empty(rhs) | (self.sup_raw() < rhs.inf_raw())
}

/// Returns true if self is a subset of rhs ($\self β \rhs$).
///
/// The formal definition is:
///
/// $$/// βx β \self, βy β \rhs : x = y. ///$$
///
/// # Examples
///
/// 
/// use inari::*;
/// assert!(const_interval!(1.0, 2.0).subset(const_interval!(1.0, 2.0)));
/// assert!(Interval::EMPTY.subset(Interval::EMPTY));
/// assert!(Interval::EMPTY.subset(Interval::ENTIRE));
/// assert!(Interval::ENTIRE.subset(Interval::ENTIRE));
/// 
pub fn subset(self, rhs: Self) -> bool {
self.is_empty() | {
// c β€ a  β§  b β€ d
//   βΊ -a β€ -c  β§  b β€ d
//   βΊ all([-a; b] .β€ [-c; d])
all(le(self.rep, rhs.rep))
}
}

pub(crate) fn both_empty(self, rhs: Self) -> bool {
self.is_empty() & rhs.is_empty()
}

pub(crate) fn either_empty(self, rhs: Self) -> bool {
self.is_empty() | rhs.is_empty()
}
}

macro_rules! impl_dec {
($f:ident, 1) => { pub fn$f(self) -> bool {
if self.is_nai() {
return false;
}

self.x.$f() } }; ($f:ident, 2) => {
pub fn $f(self, rhs: Self) -> bool { if self.is_nai() || rhs.is_nai() { return false; } self.x.$f(rhs.x)
}
};
}

impl DecInterval {
pub fn contains(self, rhs: f64) -> bool {
if self.is_nai() {
return false;
}

Interval::contains(self.x, rhs)
}

impl_dec!(disjoint, 2);
impl_dec!(interior, 2);
impl_dec!(is_common_interval, 1);
impl_dec!(is_empty, 1);
impl_dec!(is_entire, 1);

pub fn is_nai(self) -> bool {
self.d == Decoration::Ill
}

impl_dec!(is_singleton, 1);
impl_dec!(less, 2);
impl_dec!(precedes, 2);
impl_dec!(strict_less, 2);
impl_dec!(strict_precedes, 2);
impl_dec!(subset, 2);
}