-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterval.cpp
2704 lines (2666 loc) · 89.2 KB
/
interval.cpp
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Simple interval library from Luc JAULIN, with minor modifications from Fabrice LE BARS and Jeremy NICOLA.
#include "interval.h"
#ifdef __GNUC__
// Disable some GCC warnings.
#if (__GNUC__ >= 9)
#pragma GCC diagnostic ignored "-Wdeprecated-copy"
#endif // (__GNUC__ >= 9)
#if (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4))
#pragma GCC diagnostic push
#endif // (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4))
#endif // __GNUC__
#if defined(_MSC_VER) || defined(__BORLANDC__)
// Enable the use of isnan().
#include <float.h>
#ifndef isnan
#define isnan _isnan
#endif // !isnan
// Used to define NAN (Not A Number).
#ifdef NAN_CONSTS_NEEDED
const unsigned long nan[2] = {0xffffffff, 0xffffffff};
const double nan_double = -*(double*)nan;
#endif // NAN_CONSTS_NEEDED
#endif // defined(_MSC_VER) || defined(__BORLANDC__)
#ifdef NAI_CONST_NEEDED
// Used to define NAI (Not An Interval).
const interval nai = interval();
#endif // NAI_CONST_NEEDED
const double PI = M_PI;
const double PI_2 = M_PI_2;
const double TROIS_PI_2 = 3 * PI_2;
const double CINQ_PI_2 = 5 * PI_2;
const double SEPT_PI_2 = 7 * PI_2;
const double DEUX_PI = 4 * PI_2;
using namespace std;
//----------------------------------------------------------------------
// Useful real-valued functions
//----------------------------------------------------------------------
void Min1(double& zmin0, double& zmin1, double a, double b, double c)
{
if ((a <= b) && (a <= c)) { zmin0 = a; zmin1 = min(b, c); return; };
if ((b <= a) && (b <= c)) { zmin0 = b; zmin1 = min(a, c); return; };
if ((c <= b) && (c <= a)) { zmin0 = c; zmin1 = min(a, b); return; };
}
//----------------------------------------------------------------------
void Max1(double& zmax0, double& zmax1, double a, double b, double c)
{
if ((a >= b) && (a >= c)) { zmax0 = a; zmax1 = max(b, c); return; };
if ((b >= a) && (b >= c)) { zmax0 = b; zmax1 = max(a, c); return; };
if ((c >= b) && (c >= a)) { zmax0 = c; zmax1 = max(a, b); return; };
}
//----------------------------------------------------------------------
double Min(vector<double>& x)
{
double d = oo;
for (unsigned int i = 0; i < x.size(); i++)
d = min(x[i], d);
return d;
}
//----------------------------------------------------------------------
double Max(vector<double>& x)
{
double d = -oo;
for (unsigned int i = 0; i < x.size(); i++)
d = max(x[i], d);
return d;
}
//----------------------------------------------------------------------
double Sign(const double x)
{
if (x > 0) return (1);
else return (0);
}
//----------------------------------------------------------------------
double Chi(const double a, const double b, const double c)
{
if (a < 0) return (b);
else return (c);
}
//----------------------------------------------------------------------
static double Power(double x, int n, int RndMode)
{
int ChangeRndMode; // for x < 0 and odd n
double p, z;
ChangeRndMode = ((x < 0.0) && (n % 2 == 1));
if (ChangeRndMode) { z = -x; RndMode = -RndMode; }
else z = x;
p = 1.0;
switch (RndMode)
{ // Separate while-loops used
case -1:
while (n > 0)
{
if (n % 2 == 1) p = p*z; //--------------------------
n = n / 2; if (n > 0) z = z*z;
}
break;
case +1:
while (n > 0)
{
if (n % 2 == 1) p = p*z;
n = n / 2;
if (n > 0) z = z*z;
}
break;
}
if (ChangeRndMode) return -p; else return p;
}
//----------------------------------------------------------------------
double Arccossin(const double x, const double y)
{
if (y > 0) return (acos(x));
else return (-acos(x));
}
//----------------------------------------------------------------------
double Arg(const double x, const double y)
{
double r = sqrt(x*x + y*y);
if (r == 0) return 0;
return Arccossin(x / r, y / r);
}
//----------------------------------------------------------------------
double Det(double ux, double uy, double vx, double vy)
{
return (ux*vy - vx*uy);
}
//----------------------------------------------------------------------
double DistanceDirSegment(double mx, double my, double theta, double ax, double ay, double bx, double by)
{
// Distance directionnelle du point m au segment [a,b]. La direction est donnee par theta
double ma_x = ax - mx;
double ma_y = ay - my;
double mb_x = bx - mx;
double mb_y = by - my;
double ab_x = bx - ax;
double ab_y = by - ay;
double ux = cos(theta);
double uy = sin(theta);
double z1 = Det(ma_x, ma_y, ux, uy);
double z2 = Det(ux, uy, mb_x, mb_y);
double z3 = Det(ma_x, ma_y, ab_x, ab_y);
double z4 = Det(ux, uy, ab_x, ab_y);
double z5 = min(z1, min(z2, z3));
double d1 = 0;
if (z4 == 0) d1 = oo;
else d1 = z3 / z4;
return (Chi(z5, oo, d1));
}
//----------------------------------------------------------------------
void DistanceDirSegment(double& d,double& phi, double mx, double my, double theta, double ax, double ay, double bx, double by)
{
// Distance directionnelle du point m au segment [a,b].
double ma_x=ax-mx;
double ma_y=ay-my;
double mb_x=bx-mx;
double mb_y=by-my;
double ab_x=bx-ax;
double ab_y=by-ay;
double ux=cos(theta);
double uy=sin(theta);
double z1=Det(ma_x,ma_y,ux,uy);
double z2=Det(ux,uy,mb_x,mb_y);
double z3=Det(ma_x,ma_y,ab_x,ab_y);
double z4=Det(ux,uy,ab_x,ab_y);
double z5=min(z1,min(z2,z3));
double d1=z3/z4;
d=Chi(z5,oo,d1);
phi=atan2(-ab_x,ab_y); //phi is the angle of the normal vector of [a,b]
}
//----------------------------------------------------------------------
double DistanceDirSegments(double mx, double my, double theta, vector<double> ax, vector<double> ay, vector<double> bx, vector<double> by)
{
// Distance directionnelle relativement a un polygone
vector<double> dist(ax.size());
for (unsigned int j = 0; j < ax.size(); j++)
dist[j] = DistanceDirSegment(mx, my, theta, ax[j], ay[j], bx[j], by[j]);
double distmin = Min(dist);
return (distmin);
}
//----------------------------------------------------------------------
void DistanceDirSegments(double& d, double& phi, double mx, double my, double theta, vector<double> ax, vector<double> ay, vector<double> bx, vector<double> by)
{
// Distance directionnelle relativement a un polygone (le triedre m-a-b doit etre direct, sinon cette distance est infinie)
d = oo; phi = 0;
for (unsigned int j = 0; j < ax.size(); j++)
{
double dj = 0, phij = 0;
DistanceDirSegment(dj,phij,mx,my,theta,ax[j],ay[j],bx[j],by[j]);
if (dj < d) { d=dj;phi=phij; };
}
}
//----------------------------------------------------------------------
double DistanceDirCircle(double mx, double my, double theta, double cx, double cy, double r)
{
// Distance directionnelle du point m au cercle de centre c et de rayon r.
double ux, uy, alpha, beta, a, b, c, delta, px1, py1, px2, py2, d1, d2;
ux = cos(theta);
uy = sin(theta);
if (fabs(uy) > 0.00) //pour eviter la division par zero. Il conviendrait de traiter le cas uy=0
{
alpha = ux / uy;
beta = mx - my*alpha;
a = alpha*alpha + 1;
b = 2 * alpha*(beta - cx) - 2 * cy;
c = (beta - cx)*(beta - cx) + cy*cy - r*r;
delta = b*b - 4 * a*c;
if (delta < 0) return(oo);
py1 = (-b - sqrt(delta)) / (2 * a);
px1 = alpha*py1 + beta;
py2 = (-b + sqrt(delta)) / (2 * a);
px2 = alpha*py2 + beta;
d1 = Chi((px1 - mx)*ux + (py1 - my)*uy, oo, sqrt((px1 - mx)*(px1 - mx) + (py1 - my)*(py1 - my)));
d2 = Chi((px2 - mx)*ux + (py2 - my)*uy, oo, sqrt((px2 - mx)*(px2 - mx) + (py2 - my)*(py2 - my)));
return min(d1, d2);
}
return oo;
}
//----------------------------------------------------------------------
void DistanceDirCircle(double& d, double& phi, double mx, double my, double theta, double cx, double cy, double r)
{
// d is the directional distance from m to the circle of center c and radius r.
// phi is the angle of the normal vector of of the impact point
double ux,uy,alpha,beta,a,b,c,delta,px1,py1,px2,py2,d1,d2,phi1,phi2;
ux=cos(theta);
uy=sin(theta);
if (fabs(uy)<0.01) // pour eviter une division par zero, on permutte x et y
{ double aux; aux=mx; mx=my; my=aux; aux=cx; cx=cy; cy=aux; aux=ux; ux=uy; uy=aux; }
alpha=ux/uy;
beta=mx-my*alpha;
a=alpha*alpha+1;
b=2*alpha*(beta-cx)-2*cy;
c=(beta-cx)*(beta-cx)+cy*cy-r*r;
delta=b*b-4*a*c;
if (delta<0) {d=oo;phi=-999;return;};
py1=(-b-sqrt(delta))/(2*a);
px1=alpha*py1+beta;
phi1=atan2(cy-py1,cx-px1);
py2=(-b+sqrt(delta))/(2*a);
px2=alpha*py2+beta;
phi2=atan2(cy-py2,cx-px2);
d1=oo;d2=oo;
if ((px1-mx)*ux+(py1-my)*uy>0) d1=sqrt((px1-mx)*(px1-mx)+(py1-my)*(py1-my));
if ((px2-mx)*ux+(py2-my)*uy>0) d2=sqrt((px2-mx)*(px2-mx)+(py2-my)*(py2-my));
if (d1<d2) { d=d1; phi=phi1; } else { d=d2; phi=phi2; }
}
//----------------------------------------------------------------------
double DistanceDirCircles(double mx, double my, double theta, vector<double> cx, vector<double> cy, vector<double> r)
{
// Distance directionnelle relativement a plusieurs cercles.
vector<double> dist(cx.size());
for (unsigned int j = 0; j < cx.size(); j++)
dist[j] = DistanceDirCircle(mx, my, theta, cx[j], cy[j], r[j]);
double distmin = Min(dist);
return (distmin);
}
//----------------------------------------------------------------------
void DistanceDirCircles(double& d,double& phi, double mx, double my, double theta, vector<double> cx, vector<double> cy, vector<double> r)
{
d = oo; phi = 0;
for (unsigned int j = 0; j < cx.size(); j++)
{
double dj = 0, phij = 0;
DistanceDirCircle(dj,phij,mx,my,theta,cx[j],cy[j],r[j]);
if (dj < d) { d=dj; phi=phij; };
}
}
//----------------------------------------------------------------------
double DistanceDirSegmentsOrCircles(double mx, double my, double theta,
vector<double> ax, vector<double> ay, vector<double> bx, vector<double> by,
vector<double> cx, vector<double> cy, vector<double> r)
{
double d1a, d1b;
d1a = DistanceDirSegments(mx, my, theta, ax, ay, bx, by);
d1b = DistanceDirCircles(mx, my, theta, cx, cy, r);
return min(d1a, d1b);
}
//----------------------------------------------------------------------
void DistanceDirSegmentsOrCircles(double& d, double& phi, double mx, double my, double theta,
vector<double> ax, vector<double> ay, vector<double> bx, vector<double> by,
vector<double> cx, vector<double> cy, vector<double> r)
{
// returns the distance and orientation collected by a laser rangefinder in a room made with segments and circles
double phi1a = 0, phi1b = 0, d1a = oo, d1b = oo;
DistanceDirSegments(d1a,phi1a,mx,my,theta,ax,ay,bx,by);
DistanceDirCircles(d1b,phi1b,mx,my,theta,cx,cy,r);
if (d1a < d1b) { d = d1a; phi = phi1a-theta; } else { d = d1b; phi = phi1b-theta; }
}
//----------------------------------------------------------------------
borne::borne()
{
val = 0; ouverture = 0;
}
//----------------------------------------------------------------------
borne::borne(const double& a, const int& b)
{
val = a; ouverture = b;
}
//----------------------------------------------------------------------
bool operator<(const borne &x, const borne &y)
{
return x.val < y.val;
}
//----------------------------------------------------------------------
// Constructors/destructors
//----------------------------------------------------------------------
interval::interval()
{
inf = NAN; sup = NAN;
isEmpty = true;
}
//----------------------------------------------------------------------
interval::interval(const double& m)
{
if (m == NAN)
{
inf = NAN; sup = NAN;
isEmpty = true;
}
else
{
inf = m; sup = m;
isEmpty = false;
}
}
//----------------------------------------------------------------------
interval::interval(const double& a, const double& b)
{
if ((a == NAN)||(b == NAN))
{
inf = NAN; sup = NAN;
isEmpty = true;
}
else
{
if (a >= b) { inf = b; sup = a; } //modif sur >=
else { inf = a; sup = b; }
isEmpty = false;
}
}
//----------------------------------------------------------------------
interval::interval(const interval& a) { *this = a; }
//----------------------------------------------------------------------
// Operators
//----------------------------------------------------------------------
interval& interval::operator=(const interval& v)
{
(*this).inf = v.inf; (*this).sup = v.sup; (*this).isEmpty = v.isEmpty;
return *this;
}
//----------------------------------------------------------------------
interval operator+(const interval& x, const interval& y)
{
if (x.isEmpty || y.isEmpty) return interval();
else return (interval(x.inf + y.inf, x.sup + y.sup));
}
//----------------------------------------------------------------------
interval operator-(const interval& a)
{
if (a.isEmpty) return interval();
else return (interval(-(a.sup), -(a.inf)));
}
//----------------------------------------------------------------------
interval operator-(const interval& x, const interval& y)
{
if (x.isEmpty || y.isEmpty) return interval();
else return (x + (-y));
}
//----------------------------------------------------------------------
interval operator*(const double x, const interval& y)
{
if (y.isEmpty) return interval();
else return interval(x*y.inf, x*y.sup);
}
//----------------------------------------------------------------------
interval operator*(const interval& x, const double y)
{
return interval(y*x);
}
//----------------------------------------------------------------------
interval operator*(const interval& x, const interval& y)
{
if (x.isEmpty || y.isEmpty) return interval();
double x1 = x.inf, y1 = y.inf, x2 = x.sup, y2 = y.sup;
if (x1 >= 0)
{
if (y1 >= 0) return interval(x1*y1, x2*y2);
else
if (y2 <= 0) return interval(x2*y1, x1*y2);
else return interval(x2*y1, x2*y2);
}
else
if (x2 <= 0)
{
if (y1 >= 0) return interval(x1*y2, x2*y1);
else if (y2 <= 0) return interval(x2*y2, x1*y1);
else return interval(x1*y2, x1*y1);
}
else
{
if (y1 >= 0) return interval(x1*y2, x2*y2);
else if (y2 <= 0) return interval(x2*y1, x1*y1);
else return interval(min(x1*y2, x2*y1), max(x1*y1, x2*y2));
}
}
//----------------------------------------------------------------------
interval operator/(const interval& a, const interval& b)
{
if (a.isEmpty || b.isEmpty) return interval();
if (b.inf > 0)
{
if (a.inf >= 0) return interval(a.inf / b.sup, a.sup / b.inf);
else if (a.sup < 0) return interval(a.inf / b.inf, a.sup / b.sup);
else return interval(a.inf / b.inf, a.sup / b.inf);
}
else if (b.sup < 0)
{
if (a.inf >= 0) return interval(a.sup / b.sup, a.inf / b.inf);
else if (a.sup < 0) return interval(a.sup / b.inf, a.inf / b.sup);
else return interval(a.sup / b.sup, a.inf / b.sup);
}
else return interval(-oo, oo);
}
//----------------------------------------------------------------------
interval operator&(const interval& x, const interval& y)
{
return (Inter(x, y));
}
//----------------------------------------------------------------------
interval operator|(const interval& x, const interval& y)
{
return (Union(x, y));
}
//----------------------------------------------------------------------
bool operator==(const interval& a, const interval& b)
{
if ((a.inf == b.inf)&&(a.sup == b.sup)&&(a.isEmpty == b.isEmpty)) return true;
return false;
}
//----------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const interval& a)
{
if (a.isEmpty) os << "EmptyInterval";
else if (a.inf != a.sup)
{
os << "[" << setprecision(4) << a.inf << ", " << setprecision(4) << a.sup << "] ";
}
else os << a.inf;
return os;
}
//----------------------------------------------------------------------
// Member functions
//----------------------------------------------------------------------
interval& interval::Intersect(const interval& Y)
{
interval X = *this;
interval Z = Inter(X, Y);
*this = Z;
return *this;
}
//----------------------------------------------------------------------
// Interval-valued functions
//----------------------------------------------------------------------
interval Min(const interval& x, const interval& y)
{
if (x.isEmpty || y.isEmpty) return interval();
double a = min(x.sup, y.sup);
double b = min(x.inf, y.inf);
if ((x.sup == x.inf) && (y.sup == y.inf)) return interval(b);
return interval(a, b);
}
//----------------------------------------------------------------------
interval Min(const interval& x, const interval& y, const interval& z)
{
return (Min(Min(x, y), z));
}
//----------------------------------------------------------------------
interval Max(const interval& x, const interval& y)
{
return (-Min(-x, -y));
}
//----------------------------------------------------------------------
interval Max(const interval& x, const interval& y, const interval& z)
{
return (Max(Max(x, y), z));
}
//----------------------------------------------------------------------
interval Abs(const interval& a)
{
if (a.isEmpty) return interval();
double a1 = a.inf, a2 = a.sup;
if ((a1 >= 0) || (a2 <= 0)) return interval(fabs(a1), fabs(a2));
if (fabs(a1)>fabs(a2)) return interval(0, fabs(a1));
else return interval(0, fabs(a2));
}
//----------------------------------------------------------------------
interval Sign(const interval& x)
{
if (x.isEmpty) return interval();
if (x.inf > 0) return interval(1);
if (x.sup < 0) return interval(-1);
return interval(-1, 1);
}
//----------------------------------------------------------------------
interval Modulo(const interval& a, double x)
{
if ((a.inf >= 0) && (a.inf<x)) return (a);
int k = (int)floor(a.inf / x);
double offset = x * k;
return interval(a.inf - offset, a.sup - offset);
}
//----------------------------------------------------------------------
interval Sqr(const interval& a)
{
double a1 = a.inf, a2 = a.sup;
if (a.isEmpty) return interval();
if ((a1 >= 0) || (a2 <= 0)) return interval(a1*a1, a2*a2);
if (fabs(a1) > fabs(a2)) return interval(0, a1*a1);
else return interval(0, a2*a2);
}
//----------------------------------------------------------------------
interval Sqrt(const interval& a)
{
if ((a.isEmpty) || (a.sup < 0)) return interval();
double a1 = a.inf, a2 = a.sup;
if (a1 >= 0) return (interval(sqrt(a1), sqrt(a2)));
else return (interval(0, sqrt(a2)));
}
//----------------------------------------------------------------------
interval InvSqrt(const interval& a)
{
if ((a.isEmpty) || (a.sup < 0)) return interval();
double a2 = max(a.inf, a.sup);
//if (In(a1,interval(a2))) return sqrt(a1); else
return interval(-sqrt(a2), sqrt(a2));
}
//----------------------------------------------------------------------
interval Exp(const interval& a)
{
if (a.isEmpty) return(interval());
else return(interval(exp(a.inf), exp(a.sup)));
}
//----------------------------------------------------------------------
interval Log(const interval& a)
{
if ((a.isEmpty) || (a.sup <= 0)) return interval();
interval b = Abs(a);
if (In(0, a)) return interval(-oo, log(a.sup));
return interval(log(b.inf), log(b.sup));
}
//----------------------------------------------------------------------
interval Pow(const interval& x, int n)
{
// [x]^n
if (x.isEmpty) return interval();
double a1 = min(x.inf, x.sup), a2 = max(x.inf, x.sup);
if (n > 0)
{
if (n % 2 == 0)
{
if (a1*a2 < 0) return interval(0, pow(a2, n));
else return interval(pow(a1, n), pow(a2, n));
}
else return interval(pow(a1, n), pow(a2, n));
}
else return interval(1.0) / Pow(x, abs(n));
}
//----------------------------------------------------------------------------
interval Pow(const interval& x, int num, int den)
{
// [x]^(num/den)
if (x.isEmpty) return interval();
if (num*den > 0)
{
double a1 = min(x.inf, x.sup), a2 = max(x.inf, x.sup);
double n = num, m = den;
if (den % 2 == 0)
{
if ((a1 >= 0) || (a1*a2 <= 0))
return interval(-pow(a2, n / m), pow(a2, n / m));
else return interval();
}
else
{
if (a1*a2 <= 0)
return interval(-pow(fabs(a1), n / m), pow(a2, n / m));
if (a1 > 0) return interval(pow(a1, n / m), pow(a2, n / m));
else return interval(-pow(fabs(a1), n / m), -pow(fabs(a2), n / m));
}
}
else return interval(1.0) / Pow(x, abs(num), abs(den));
}
//----------------------------------------------------------------------
interval Power(const interval& x, int n)
{
int m; double Lower, Upper;
if (n == 0) return(interval(1.0, 1.0));
if (n > 0) m = n; else m = -n;
if ((0.0 < x.inf) || (m % 2 == 1))
{
Lower = Power(x.inf, m, -1); Upper = Power(x.sup, m, +1);
}
else if (0.0 > x.sup)
{
Lower = Power(x.sup, m, -1); Upper = Power(x.inf, m, +1);
}
else { Lower = 0.0; Upper = Power(AbsMax(x), m, +1); }
if (n > 0) return(interval(Lower, Upper));
else return(1.0 / interval(Lower, Upper)); // if 0 in 'x'.
}
//----------------------------------------------------------------------------
interval PowRoot(const interval& x, int num, int den)
{
// [x]^(num/den)
if (x.isEmpty) return interval();
if (num*den > 0)
{
double a1 = min(x.inf, x.sup), a2 = max(x.inf, x.sup), n = num, m = den;
if (den % 2 == 0)
{
if (a1 >= 0) return interval(pow(a1, n / m), pow(a2, n / m));
if (a1*a2 <= 0) return interval(0, pow(a2, n / m));
else return interval();
}
else
{
if (a1*a2 <= 0) return interval(-pow(fabs(a1), n / m), pow(a2, n / m));
if (a1 > 0) return interval(pow(a1, n / m), pow(a2, n / m));
else return interval(-pow(fabs(a1), n / m), -pow(fabs(a2), n / m));
}
}
else return interval(1.0) / Pow(x, abs(num), abs(den));
}
//----------------------------------------------------------------------
interval Cos(const interval& a)
{
if (a.isEmpty) return interval();
else return Sin(a + PI_2);
}
//----------------------------------------------------------------------
interval Sin(const interval& a)
{
if (a.isEmpty) return interval();
if (a.sup == a.inf) return sin(a.inf);
interval b;
double sin1, sin2, r1, r2;
b = Modulo(a, DEUX_PI);
if (Width(a) > DEUX_PI) return (interval(-1, 1));
sin1 = sin(b.inf); sin2 = sin(b.sup);
if ((b.inf < TROIS_PI_2) && (b.sup > TROIS_PI_2)) r1 = -1.0;
else if ((b.inf < SEPT_PI_2) && (b.sup > SEPT_PI_2)) r1 = -1.0;
else r1 = ((sin1 < sin2) ? sin1 : sin2);
if ((b.inf < PI_2) && (b.sup > PI_2)) r2 = 1.0;
else if ((b.inf < CINQ_PI_2) && (b.sup > CINQ_PI_2)) r2 = 1.0;
else r2 = ((sin1 > sin2) ? sin1 : sin2);
return Inter(interval(-1, 1), interval(r1, r2));
}
//----------------------------------------------------------------------
interval Tan(const interval& a) { return Sin(a) / Cos(a); }
//----------------------------------------------------------------------
interval Atan(const interval& x)
{
if (x.isEmpty) return interval();
else return interval(atan(x.inf), atan(x.sup));
}
//----------------------------------------------------------------------
interval Det(interval& ux, interval& uy, interval& vx, interval& vy)
{
return (ux*vy - vx*uy);
}
//----------------------------------------------------------------------
interval Det(interval& ux, interval& uy, double& vx, double& vy)
{
return (vy*ux - vx*uy);
}
//----------------------------------------------------------------------
interval Step(const interval& X)
{
if (X.isEmpty) return interval();
if (X.inf > 0) return (interval(1));
if (X.sup < 0) return (interval(0));
return (interval(0,1));
}
//----------------------------------------------------------------------
interval Parabole(const interval& x, double a, double b, double c)
{
return (a*Sqr(x + (b / (2 * a))) - (b*b) / (4 * a) + c);
}
//----------------------------------------------------------------------
interval Inter(const interval& a, const interval& b)
{
if (a.isEmpty || b.isEmpty) return interval();
interval r(0, 0);
if ((a.inf > b.sup) || (b.inf > a.sup)) return interval();
if (a.inf <= b.inf) r.inf = b.inf; else r.inf = a.inf;
if (a.sup >= b.sup) r.sup = b.sup; else r.sup = a.sup;
return r;
}
//------------------------------------------------------------------------------
interval Inter(vector<interval> x)
{
interval r = x[0];
for (unsigned int i = 1; i < x.size(); i++)
r = Inter(r, x[i]);
return r;
}
//----------------------------------------------------------------------
interval Union(const interval& a, const interval& b)
{
if (a.isEmpty) return b;
if (b.isEmpty) return a;
interval r(0, 0);
if (a.inf <= b.inf) r.inf = a.inf; else r.inf = b.inf;
if (a.sup >= b.sup) r.sup = a.sup; else r.sup = b.sup;
return r;
}
//----------------------------------------------------------------------
interval Union(vector<interval> x)
{
interval r = x[0];
for (unsigned int i = 1; i<x.size(); i++)
r = Union(r, x[i]);
return r;
}
//----------------------------------------------------------------------
interval InterMin(const interval& a, const interval& b, char c)
{
interval y(0, 0);
if (c == '-') { interval temp(a.inf, oo); y = Inter(temp, b); }
else { interval temp(0, a.sup); y = Inter(temp, b); }
return y;
}
//----------------------------------------------------------------------
interval Inflate(const interval& a, double eps)
{
interval r(a.inf - eps, a.sup + eps);
r.isEmpty = a.isEmpty;
return interval(r);
}
//----------------------------------------------------------------------
interval Envelope(vector<double>& x)
{
if (x.size() == 0) return (interval());
return (interval(Min(x), Max(x)));
}
//----------------------------------------------------------------------
// Other functions
//----------------------------------------------------------------------
double Inf(const interval& a)
{
if (a.inf<a.sup) return a.inf;
else return a.sup;
}
//----------------------------------------------------------------------
double Sup(const interval& a)
{
if (a.sup>a.inf) return a.sup;
else return a.inf;
}
//-----------------------------------------------------------------------
double Center(const interval& a)
{
if (a.isEmpty) return oo;
else return (a.sup + a.inf) / 2;
}
//----------------------------------------------------------------------
double Width(const interval& a)
{
if (a.isEmpty) return (-1);
else return (a.sup - a.inf);
}
//----------------------------------------------------------------------
double Volume(const interval& a)
{
if (a.isEmpty) return (-1);
else return (a.sup - a.inf);
}
//----------------------------------------------------------------------
double Rad(const interval& a)
{
if (a.isEmpty) return (-1);
else return (a.sup - a.inf)/2.0;
}
//----------------------------------------------------------------------
double Marge(const interval& a, const interval& b)
{
return min(a.inf - b.inf, b.sup - a.sup);
}
//----------------------------------------------------------------------
double ToReal(const interval& a)
{
if ((a.isEmpty)||(a.sup != a.inf)) return NAN;
return a.inf;
}
//----------------------------------------------------------------------
double Rand(const interval& a)
{
if (a.isEmpty) return oo;
else return a.inf + (a.sup - a.inf)*rand() / RAND_MAX;
}
//----------------------------------------------------------------------
double Eloignement(const interval& x, const interval& y)
{
if ((x.isEmpty) || (y.isEmpty)) return oo;
double r = max(0.0, x.inf - y.sup);
r = max(r, y.inf - x.sup);
return r;
}
//----------------------------------------------------------------------
double AbsMax(const interval& x) // Absolute maximum of
{
if (x.isEmpty) return 0;
double a = fabs(x.inf), b = fabs(x.sup);
return max(a, b);
}
//----------------------------------------------------------------------
bool OverLap(const interval& a, const interval& b)
{
return (a.sup>b.inf) && (b.sup > a.inf);
}
//----------------------------------------------------------------------
bool Disjoint(const interval& a, const interval& b)
{
if (a.isEmpty || b.isEmpty) return true;
return ((a.sup < b.inf) || (b.sup < a.inf));
}
//----------------------------------------------------------------------
bool Subset(const interval& a, const interval& b)
{
if (a.isEmpty) return true;
if (b.isEmpty) return false;
return ((a.inf >= b.inf) && (a.sup <= b.sup));
}
//----------------------------------------------------------------------
bool Subset(const interval& a, const interval& b, double epsilon)
{
if (a.isEmpty || b.isEmpty) return false;
else return (Subset(a, b) && ((b.inf < a.inf - epsilon) || (a.sup < b.sup - epsilon)));
}
//----------------------------------------------------------------------
bool SubsetStrict(const interval& a, const interval& b)
{
if (a.isEmpty) return true;
if (b.isEmpty) return false;
return ((a.inf > b.inf) && (a.sup < b.sup));
}
//----------------------------------------------------------------------
iboolean In(const interval& F, const interval& Y)
{
interval Z = Inflate(Y, 1e-17);
if (Disjoint(F, Z)) return false;
if (Subset(F, Z)) return true;
return iboolean(iperhaps);
}
//----------------------------------------------------------------------
bool In(double a, const interval& b)
{
interval z = Inflate(b, 1e-6);
if (b.isEmpty) return false;
return ((z.inf <= a)&&(a <= z.sup));
}
//----------------------------------------------------------------------
// Contractors
//----------------------------------------------------------------------
void Cadd(interval& Z, interval& X, interval& Y, int dir)
{
// Z=X+Y => dir=1;
// X=Z-Y Y=Z-X; => dir=-1;
if (dir != -1) { Z = Inter(Z, X + Y); }
if (dir != 1) { X = Inter(X, Z - Y); Y = Inter(Y, Z - X); }
}
//----------------------------------------------------------------------
void Cadd(interval& Z, double x, interval& Y, int dir)
{
if (dir != -1) { Z = Inter(Z, x + Y); }
if (dir != 1) { Y = Inter(Y, Z - x); }
}
//----------------------------------------------------------------------
void Cadd(interval& Z, interval& X, double y, int dir)
{
if (dir != -1) { Z = Inter(Z, X + y); }
if (dir != 1) { X = Inter(X, Z - y); }
}
//----------------------------------------------------------------------
void Cadd(double z, interval& X, interval& Y, int dir)
{
if (dir != 1) { X = Inter(X, z - Y); Y = Inter(Y, z - X); }
}
//----------------------------------------------------------------------
void Csub(interval& Z, interval& X, interval& Y, int dir)
{
// Z=X-Y => dir=1;
// X=Z+Y Y=X-Z; => dir=-1;
if (dir != -1) { Z = Inter(Z, X - Y); }
if (dir != 1) { X = Inter(X, Z + Y); Y = Inter(Y, X - Z); }
}
//----------------------------------------------------------------------
void Csub(interval& Z, double x, interval& Y, int dir)
{
if (dir != -1) { Z = Inter(Z, x - Y); }
if (dir != 1) { Y = Inter(Y, x - Z); }
}
//----------------------------------------------------------------------
void Csub(interval& Z, interval& X, double y, int dir)
{
if (dir != -1) { Z = Inter(Z, X - y); }
if (dir != 1) { X = Inter(X, Z + y); }
}
//----------------------------------------------------------------------
void Csub(double z, interval& X, interval& Y, int dir)
{
if (dir != 1) { X = Inter(X, z + Y); Y = Inter(Y, X - z); }
}
//----------------------------------------------------------------------
void Csub(interval& Y, interval& X, int dir)
{
// Y=-X => dir=1;
// X=-Y => dir=-1;
if (dir != -1) Y = Inter(Y, -X);
if (dir != 1) X = Inter(X, -Y);
}
//----------------------------------------------------------------------
void Cmul(interval& Z, interval& X, interval& Y, int dir)
{
// Z=X*Y => dir=1;
// X=Z/Y; Y=Z/X => dir=-1;
if (dir != -1)
{
Z = Inter(Z, X*Y);
}
if (dir != 1)
{
//modifs ???
if (In(0, Z) == false)
{
interval Xd, Xg;
interval Yd, Yg;
Xg = Inter(X, interval(-oo, 0)), Xd = Inter(X, interval(0, oo));
Yg = Inter(Y, interval(-oo, 0)), Yd = Inter(Y, interval(0, oo));
if (Z.inf > 0)
{
if (Yd.sup*Xd.sup < Z.inf)
{
Xd = interval();
Yd = interval();
}
else
{
if ((Yd.sup != 0) && (Xd.sup != 0))
{
Yd = Inter(Yd, interval(Z.inf / Xd.sup, oo));
Xd = Inter(Xd, interval(Z.inf / Yd.sup, oo));
}
}
if (Yg.inf*Xg.inf < Z.inf)
{
Xg = interval();
Yg = interval();
}
else
{
if ((Yg.inf != 0) && (Xg.inf != 0))
{
Yg = Inter(Yg, interval(Z.inf / Xg.inf, -oo));
Xg = Inter(Xg, interval(Z.inf / Yg.inf, -oo));
}
}
Y = Inter(Y, Union(Yd, Yg));
X = Inter(X, Union(Xd, Xg));
}
if (Z.sup<0)
{
if (Yg.inf*Xd.sup>Z.sup)
{
Xd = interval();
Yg = interval();
}
else
{
if ((Yg.inf != 0) && (Xd.sup != 0))
{
Yg = Inter(Yg, interval(Z.sup / Xd.sup, -oo));
Xd = Inter(Xd, interval(Z.sup / Yg.inf, oo));
}
}
if (Yd.sup*Xg.inf > Z.sup)
{
Yd = interval();
Xg = interval();
}
else
{