-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIVparals.cpp
More file actions
1134 lines (1058 loc) · 38.5 KB
/
Copy pathIVparals.cpp
File metadata and controls
1134 lines (1058 loc) · 38.5 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
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
////////////////////////////////////////////////////////////////////////////
// IVparals.cpp : representation of polyhedrons as intersections of M_i X_i
////////////////////////////////////////////////////////////////////////////
#include <codac.h>
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include "codac2_expIMat.h"
#include "IVparals.h"
using namespace codac;
using namespace codac2;
namespace diffincl {
uint_fast64_t IVparals::matIdCnt=0;
IVparals::IVparals(int dim) :
dim(dim), empty(true), nbmat(0), nbNcst(0), mats(), Imats(), matId(0), rhs(2)
{
IntervalMatrix mA(dim,dim);
mA = Matrix::eye(dim);
this->mats[0] = mA;
this->Imats[0] = mA;
this->rhs[0] = this->rhs[1] = IntervalVector::empty(dim);
}
IVparals::IVparals(const IntervalVector& iv) :
dim(iv.size()), empty(iv.is_empty()), nbmat(1), nbNcst(1), mats(1,IntervalMatrix(iv.size(),iv.size())), Imats(1,IntervalMatrix(iv.size(),iv.size())), matId(0), rhs(2)
{
IntervalMatrix mA(dim,dim);
mA = Matrix::eye(dim);
this->mats[0] = mA;
this->Imats[0] = mA;
this->rhs[0] = this->rhs[1] = iv;
}
IVparals::IVparals(const IVparals& iv, const IntervalVector& box) :
dim(iv.dim), empty(box.is_empty()),
nbmat(iv.nbmat), nbNcst(iv.nbNcst), mats(iv.mats),
Imats(iv.Imats), matId(iv.matId), rhs(iv.rhs) {
assert(iv.dim == box.size());
if (box.is_empty()) {
for (int i=0;i<=nbmat;i++) this->rhs[i].set_empty();
return;
}
for (int i =0;i<this->nbmat;i++) {
this->rhs[i] = this->Imats[i] * box;
}
this->rhs[this->nbmat] = box;
}
IVparals::IVparals(const IVparals& iv) :
dim(iv.dim), empty(iv.empty),
nbmat(iv.nbmat), nbNcst(iv.nbNcst), matId(iv.matId), mats(iv.mats),
Imats(iv.Imats), rhs(iv.rhs) { }
IVparals::IVparals(const IntervalMatrix& M, const IntervalMatrix& rM,
const IntervalVector& V) :
dim(V.size()), empty(V.is_empty()),
nbmat(1), nbNcst(1), matId(++matIdCnt), mats(1,M),
Imats(1,rM), rhs(2) {
if (V.is_empty()) {
for (int i=0;i<=nbmat;i++) this->rhs[i].set_empty();
return;
}
rhs[0] = V;
rhs[1] = M*V;
}
IVparals::IVparals(const IntervalMatrix& M, const IntervalVector& V) :
dim(V.size()), empty(V.is_empty()),
nbmat(1), nbNcst(1), matId(++matIdCnt), mats(1,M),
Imats(1,inv_IntervalMatrix(M)), rhs(2) {
assert(!Imats[0].is_empty()); /* FIXME : approach for singular M */
if (V.is_empty()) {
for (int i=0;i<=nbmat;i++) this->rhs[i].set_empty();
return;
}
rhs[0] = V;
rhs[1] = M*V;
}
/****** Access ******/
IntervalVector IVparals::bounding_box() const {
return this->rhs[this->nbmat];
}
Vector IVparals::mid() const {
return this->bounding_box().mid(); /* FIXME : better ? */
}
bool IVparals::contains(const Vector& iv) const {
if (this->empty) return false;
if (!this->rhs[this->nbmat].contains(iv)) return false;
for (int i=0;i<this->nbmat;i++) {
if (!this->rhs[i].intersects(this->Imats[i]*iv)) return false;
}
return true;
}
const IntervalMatrix& IVparals::getMat(int i) const {
assert (i>=0 && i<nbmat);
return this->mats[i];
}
const IntervalVector& IVparals::getVec(int i) const {
assert (i>=0 && i<=nbmat);
return this->rhs[i];
}
IntervalVector IVparals::getPar(const IntervalMatrix& M, int i) const {
if (i==nbmat) return M*this->rhs[nbmat];
return (M*this->mats[i])*this->rhs[i];
}
IntervalVector IVparals::getPar(const IntervalMatrix& M) const {
IntervalVector Res = M*this->rhs[nbmat];
for (int i=0;i<this->nbmat;i++) {
Res &= (M*this->mats[i])*this->rhs[i];
}
return Res;
}
double IVparals::rel_distanceFast(const IVparals& iv) const {
assert(this->matId==iv.matId);
double a=0;
for (int i=0;i<this->nbmat;i++) {
double b= this->rhs[i].rel_distance(iv.rhs[i]);
if (a<b) a=b;
}
return a;
}
/*** modification ***/
void IVparals::set_empty() {
this->empty=true;
for (int i=0;i<=this->nbmat;i++) (this->rhs[i]).set_empty();
}
void IVparals::clear() {
this->empty=false;
for (int i=0;i<=this->nbmat;i++) (this->rhs[i]).clear();
}
IVparals& IVparals::inflate(double rad) {
if (this->empty) return *this;
IntervalVector V(dim,rad);
return (*this += V);
}
IVparals& IVparals::inflateBall(double rad) {
if (this->empty) return *this;
for (int i=0;i<=this->nbmat;i++) {
for (int j=0;j<dim;j++) {
double a = (i<this->nbmat ? (rad/this->Imats[i][j].norm2()).ub()
: rad);
this->rhs[i][j].inflate(a);
}
}
return *this;
}
void IVparals::inflate_from_baseFast(const IVparals& iv, double fact) {
assert(this->matId==iv.matId);
for (int i=0;i<=this->nbmat;i++) {
IntervalVector& a = this->rhs[i];
const IntervalVector& b = iv.rhs[i];
for (int j=0;j<dim;j++) {
double mn = fact*(b[j].lb() - a[j].lb());
if (mn>0) mn=0.0;
double mx = fact*(b[j].ub() - a[j].ub());
if (mx<0) mx=0.0;
Interval ev(mn,mx);
a[j] += ev;
}
}
}
IVparals& IVparals::homothety(IntervalVector c, double delta) {
if (this->empty) return *this;
IntervalVector nc = (1-delta)*c;
for (int i=0;i<=this->nbmat;i++) {
this->rhs[i] *= delta;
}
return (*this += nc);
}
IVparals& IVparals::operator=(const IntervalVector& x) {
if (x.is_empty()) { this->set_empty(); return *this; }
this->empty=false;
this->rhs[nbmat] = x;
for (int i=0;i<this->nbmat;i++) {
this->rhs[i] = this->Imats[i]*x;
}
return *this;
}
IVparals& IVparals::assign(const IVparals& iv) {
if (iv.empty) { this->set_empty(); return *this; }
this->empty=false;
this->rhs[nbmat] = iv.rhs[iv.nbmat];
for (int i=0;i<this->nbmat;i++) {
this->rhs[i] = this->Imats[i]*iv.rhs[iv.nbmat];
}
for (int i=0;i<this->nbmat;i++) {
for (int j=0;i<this->nbmat;i++) {
this->rhs[i] &= (this->Imats[i]*this->mats[j])*iv.rhs[j];
}
}
this->simplify();
return *this;
}
IVparals& IVparals::linMult(const IntervalMatrix& M,
const IntervalMatrix& IM) {
if (this->is_empty()) return *this;
/* not an optimal algorithm, we may need the
piecewise-affine interval functions to get a better
(but possibly non-optimal application */
for (int i=this->nbNcst;i<this->nbmat;i++) { /* for each unchanging
matrices */
IntervalMatrix ImpM = this->Imats[i]*M;
IntervalVector newRhs((ImpM*this->mats[i])*this->rhs[i]);
/* basic modification. */
for (int j=0;j<this->nbNcst;j++) {
/* computing Imats[i]*M*mats[j] / Imats[i]*mat[j]
(componentwise division) */
IntervalMatrix R = ImpM*this->mats[j];
IntervalMatrix denom = this->Imats[i]*this->mats[j];
for (int k=0;k<dim;k++)
for (int l=0;l<dim;l++) {
if (denom[k][l].contains(0.0)) { R[k][l]=1.0; continue; }
R[k][l]/=denom[k][l];
}
R = R.mid();
/* foreach column V of R */
for (int k=0;k<dim;k++) {
/* compute X = V*rhs[i] */
IntervalVector X(dim);
for (int l=0;l<dim;l++) X[l] = R[l][k]*this->rhs[i][l];
/* compute K = ImpM- V*Imats[i] (line by line) */
IntervalMatrix K(ImpM);
for (int l=0;l<dim;l++) K[l]-=R[l][k]*this->Imats[i][l];
X += (K*this->mats[j])*this->rhs[j];
newRhs &= X;
}
}
this->rhs[i]=newRhs;
}
/* case i=nbmat */
{
IntervalVector newRhs(M*this->rhs[nbmat]);
for (int j=0;j<this->nbNcst;j++) {
IntervalMatrix R = M*this->mats[j];
IntervalMatrix& denom = this->mats[j];
for (int k=0;k<dim;k++)
for (int l=0;l<dim;l++) {
if (this->mats[j][k][l].contains(0.0))
{ R[k][l]=1.0; continue; }
R[k][l]/=denom[k][l];
}
R = R.mid();
/* foreach column V of R */
for (int k=0;k<dim;k++) {
/* compute X = V*rhs[nbmat] */
IntervalVector X(dim);
for (int l=0;l<dim;l++) X[l] = R[l][k]*this->rhs[nbmat][l];
/* compute K = M-V*Id (line by line) */
IntervalMatrix K(M);
for (int l=0;l<dim;l++) K[l][l]-=R[l][k];
X += (K*this->mats[j])*this->rhs[j];
newRhs &= X;
}
}
this->rhs[nbmat]=newRhs;
}
this->matId=(++matIdCnt);
for (int i=0;i<this->nbNcst;i++) {
IntervalMatrix MProd(this->Imats[i]);
this->Imats[i] = this->Imats[i] * IM;
this->mats[i] = M * this->mats[i];
}
this->simplify(); // absolutely needed
return *this;
}
/**** intersections ******/
IVparals& IVparals::operator&=(const IntervalVector& x) {
if (this->empty) return *this;
this->rhs[this->nbmat] &= x;
if (this->rhs[this->nbmat].is_empty()) {
this->set_empty(); return *this;
}
for (int i=0;i<this->nbmat;i++) {
this->rhs[i] &= this->Imats[i]*x;
if (this->rhs[i].is_empty()) {
this->set_empty(); return *this;
}
}
this->simplify();
return *this;
}
IVparals operator&(const IVparals& iv, const IntervalVector& x) {
IVparals res(iv);
res &= x;
return res;
}
IVparals& IVparals::meetFast(const IVparals& iv) {
assert(this->matId==iv.matId);
if (this->empty) return *this;
if (iv.empty) { this->set_empty(); return *this; }
this->rhs[this->nbmat] &= iv.rhs[iv.nbmat];
if (this->rhs[this->nbmat].is_empty()) {
this->set_empty(); return *this;
}
for (int i=0;i<this->nbmat;i++) {
this->rhs[i] &= iv.rhs[i];
if (this->rhs[i].is_empty()) {
this->set_empty(); return *this;
}
}
this->simplify();
return (*this);
}
IVparals& IVparals::meet(const IVparals& iv) {
if (this->empty) return *this;
if (iv.empty) { this->set_empty(); return *this; }
this->rhs[this->nbmat] &= iv.rhs[iv.nbmat];
if (this->rhs[this->nbmat].is_empty()) {
this->set_empty(); return *this;
}
for (int i=0;i<this->nbmat;i++) {
this->rhs[i] &= this->Imats[i]*iv.rhs[iv.nbmat];
for (int j=0;j<this->nbmat;j++) {
this->rhs[i] &= (this->Imats[i]*iv.mats[j])*iv.rhs[iv.nbmat];
}
if (this->rhs[i].is_empty()) {
this->set_empty(); return *this;
}
}
this->simplify();
return *this;
}
bool IVparals::meetLN
(const IntervalVector& V, const Interval& b, bool keep) {
/* WARNING : use of bwd_mul */
if (this->empty) return false;
Interval bcopy(b);
IntervalVector VCopy(V);
if (!bwd_mul(bcopy,VCopy,this->rhs[this->nbmat])) {
this->set_empty(); return false;
}
for (int i=0;i<this->nbmat;i++) {
IntervalVector VM = VCopy*this->mats[i];
if (!bwd_mul(bcopy,VM,this->rhs[i])) {
this->set_empty(); return false;
}
}
if (keep) {
this->simplify();
return !(this->empty);
}
IntervalVector Vmid = VCopy.mid();
int numM=-1; int nlig=-1;
this->replaceVectorImat (Vmid, numM, nlig);
/* VCopy.x \in b
=> Vmid.x + Veps.x \in b
=> Vmid.x \in b - Veps.x
=> Vmid.x \in (b - Veps rhs[nbmat]) */
VCopy -= Vmid;
this->rhs[numM][nlig] &= bcopy - (VCopy*this->rhs[nbmat]);
if (this->rhs[numM][nlig].is_empty()) {
this->set_empty(); return false;
}
this->simplify();
return !(this->empty);
}
bool IVparals::meetLN(const IntervalVector& V, const IntervalVector& C,
const Interval& b, bool keep) {
return this->meetLN(V,(b-V*C),keep);
}
/* FIXME : maybe we can do better... though I'm not sure */
bool IVparals::meetLM(const IntervalMatrix& S, const IntervalVector& b, bool keep) {
assert(S.nbrows()==b.size());
/* WARNING : use of bwd_mul */
if (this->empty) return false;
IntervalVector bcopy(b);
IntervalMatrix SCopy(S);
if (!bwd_mul(bcopy,SCopy,this->rhs[this->nbmat],1e-3)) {
this->set_empty(); return false;
}
for (int i=0;i<this->nbmat;i++) {
IntervalMatrix SM = SCopy*this->mats[i];
if (!bwd_mul(bcopy,SM,this->rhs[i],1e-3)) {
this->set_empty(); return false;
}
}
if (keep) {
this->simplify();
return !(this->empty);
}
/* not keep : we apply replaceVectorImat sequentially
I'm not sure that's a good idea nevertheless... */
IntervalMatrix Smid = SCopy.mid();
SCopy -= Smid;
for (int i=0;i<S.nb_rows();i=i+1) {
int numM=-1; int nlig=-1;
this->replaceVectorImat (Smid[0], numM, nlig);
/* VCopy.x \in b
=> Vmid.x + Veps.x \in b
=> Vmid.x \in b - Veps.x
=> Vmid.x \in (b - Veps Mi rhs[i]) */
this->rhs[numM][nlig] &= bcopy[i] -
((SCopy[i]*this->mats[numM])*this->rhs[numM]);
if (this->rhs[numM][nlig].is_empty()) {
this->set_empty(); return false;
}
this->simplify();
if (this->empty) return false;
}
return true;
}
/* FIXME : maybe we can do better... though I'm not sure */
bool IVparals::meetLM(const IntervalMatrix& S, const IntervalVector& C,
const IntervalVector& b, bool keep) {
return this->meetLM(S,b+S*C,keep);
}
bool IVparals::meetLM(const IntervalMatrix& S, const Vector& C,
const IntervalVector& b, bool keep) {
return this->meetLM(S,b+S*C,keep);
}
/** union with a box
*/
IVparals& IVparals::operator|= (const IntervalVector& x) {
if (x.is_empty()) return *this;
if (this->empty) {
return (*this = x);
}
for (int i=1;i<this->nbmat;i++) {
this->rhs[i] |= this->Imats[i]*x;
}
this->rhs[this->nbmat] |= x;
return *this;
}
IVparals operator|(const IVparals& iv, const IntervalVector& x) {
IVparals Res(iv);
Res |= x;
return Res;
}
IVparals& IVparals::operator+=(const IntervalVector& V) {
if (this->empty) return *this;
for (int i=0;i<this->nbmat;i++) {
this->rhs[i] += this->Imats[i]*V;
}
this->rhs[this->nbmat] += V;
return *this;
/* simplification not needed : max C(x+v) = max Cx + max Cv */
}
IVparals& IVparals::operator-=(const IntervalVector& V) {
if (this->empty) return *this;
for (int i=0;i<this->nbmat;i++) {
this->rhs[i] -= this->Imats[i]*V;
}
this->rhs[this->nbmat] -= V;
return *this;
/* simplification not needed : max C(x+v) = max Cx + max Cv */
}
IVparals operator+(const IVparals& iv, const IntervalVector& V) {
IVparals res(iv);
res += V;
return res;
}
IVparals operator-(const IVparals& iv, const IntervalVector& V) {
IVparals res(iv);
res -= V;
return res;
}
/** product : compared with linMult :
M is (generally) small and contains singular matrices
we just want an IntervalVector */
IntervalVector operator*(const IntervalMatrix& M, const IVparals& iv) {
IntervalVector res = M*iv.rhs[iv.nbmat];
for (int i=0;i<iv.nbmat;i++) {
IntervalMatrix MP = M*iv.mats[i];
res &= MP*iv.rhs[i];
}
return res;
}
IVparals sum_tau(const IVparals& iv, const IntervalVector& V, bool keep) {
IVparals res(iv);
Interval Tau(0.0,1.0);
for (int i=0;i<res.nbmat;i++) {
res.rhs[i] += Tau*(res.Imats[i]*V);
/* better than res.Imats[i]*(Tau*V) */
}
res.rhs[res.nbmat] += Tau*V;
res.simplify(true);
return res;
}
void IVparals::cmult_and_add (const Vector& center,
const IntervalMatrix& M,
const IntervalMatrix& IM,
const IntervalVector& V)
{
if (this->empty) return;
this->linMult(M,IM);
(*this) += (-M*center + center +V);
}
/* quick algorithm :
1) we want to keep the matrices
2) more precise would require (much) more work
FIXME : can we adapt a bit nevertheless, from the algorithm of
linMult ? */
void IVparals::ctau_mult_and_add
(const Vector& center,
const IntervalMatrix& M,
const IntervalVector& V)
{
if (this->empty) return;
Interval Tau(0.0,1.0);
for (int i=0;i<this->nbmat;i++) {
this->rhs[i] += Tau*((this->Imats[i]*M*this->mats[i])*this->rhs[i]
-(this->Imats[i]*M)*center
+this->Imats[i]*V);
}
this->rhs[this->nbmat] += Tau*(M*(this->rhs[this->nbmat]-center)+V);
this->simplify(true);
}
/* FIXME : check and simplify this algorithm */
bool IVparals::join_intersect_with_tau(const IVparals& iv,
const Vector& center, const IntervalMatrix& M,
const IntervalVector& V,
const IntervalVector& box, int d, double val) {
if (iv.empty) return false;
Interval Tau(0.0,1.0);
bool ret = false;
IntervalVector cbox(box);
cbox[d]=val;
IntervalVector bbox = iv.bounding_box();
Interval v1(val-bbox[d]);
Interval v2((M*(bbox-center)+V)[d]);
if (!bwd_mul(v1,Tau,v2)) return false;
if (Tau.ub()<=0.0) return false;
v1 &= Tau*v2;
bbox[d] = val-v1;
IVparals ivInter(*this, bbox);
IntervalMatrix mId(dim,dim);
mId = (Matrix::eye(dim) + Tau*M);
ivInter.rhs[this->nbmat] &= mId*(iv.rhs[this->nbmat])
+ Tau * (V-M*center);
if (ivInter.rhs[this->nbmat].is_empty()) return false;
for (int j=0;j<iv.nbmat;j++) {
IntervalVector tmpVect= iv.mats[j]*iv.rhs[j];
ivInter.rhs[this->nbmat] &= mId*tmpVect
+ Tau * (V-M*center);
if (ivInter.rhs[this->nbmat].is_empty()) return false;
ivInter.rhs[j] &= (ivInter.Imats[j]*mId*ivInter.mats[j])*iv.rhs[j]
+ ivInter.Imats[j]*(Tau*(V-M*center));
if (ivInter.rhs[j].is_empty()) return false;
}
/*
for (int i=0;i<this->nbmat;i++) {
IntervalVector Vi(ivInter.Imats[i]*V);
for (int j=0;j<iv.nbmat;j++) {
IntervalMatrix tmpM(ivInter.Imats[i] * M);
ivInter.rhs[i] &= (ivInter.Imats[i] * iv.mats[j]) * iv.rhs[j]
+ Tau * ((tmpM * iv.mats[j]) * iv.rhs[j] +
tmpM*(iv.center - center)+Vi)
+ ivInter.Imats[i] * (iv.center - ivInter.center);
if (ivInter.rhs[i].is_empty()) return false;
}
}
*/
ivInter.simplify(true);
if (ivInter.is_empty()) return false;
if (this->is_empty()) {
for (int i=0;i<=this->nbmat;i++) this->rhs[i] = iv.rhs[i];
return true;
}
for (int i=0;i<=this->nbmat;i++) {
if (!ivInter.rhs[i].is_subset(this->rhs[i])) {
this->rhs[i] |= ivInter.rhs[i];
ret = true;
}
}
if (ret) { this->simplify(true); return true; }
return false;
}
IVparals& IVparals::sumFast(const IVparals& iv) {
if (this->empty) return *this;
if (iv.empty) { this->set_empty(); return *this; }
assert(this->matId==iv.matId);
for (int i=0;i<this->nbmat;i++) {
this->rhs[i] += iv.rhs[i];
}
this->rhs[this->nbmat] += iv.rhs[this->nbmat];
return *this;
}
IVparals& IVparals::diffFast(const IVparals& iv) {
if (this->empty) return *this;
if (iv.empty) { this->set_empty(); return *this; }
assert(this->matId==iv.matId);
for (int i=0;i<this->nbmat;i++) {
this->rhs[i] -= iv.rhs[i];
}
this->rhs[this->nbmat] -= iv.rhs[this->nbmat];
return *this;
}
bool IVparals::is_subset(const IntervalVector& V) const {
if (this->empty) return true;
return this->rhs[this->nbmat].is_subset(V);
}
bool IVparals::is_subsetFast(const IVparals& iv) const {
if (this->empty) return true;
if (iv.empty) return false;
assert(this->matId==iv.matId);
for (int i=0;i<this->nbmat;i++) {
if (!this->rhs[i].is_subset(iv.rhs[i])) return false;
}
return this->rhs[this->nbmat].is_subset(iv.rhs[iv.nbmat]);
}
IVparals& IVparals::toPointMatrices() {
this->matId = (++matIdCnt);
for (int i=0;i<this->nbmat;i++) {
IntervalMatrix M1 = this->mats[i].mid();
this->Imats[i] = inv_IntervalMatrix(M1);
this->rhs[i] = (this->Imats[i]*this->mats[i])*this->rhs[i];
/* FIXME : is there a different approach ? */
this->mats[i] = M1;
}
this->simplify();
return *this;
}
/* "orthogonalise" a vector of a matrix:
* modify the other vectors to make them orthogonal */
IVparals& IVparals::orthogonalise
(int numM, int ncol) {
this->matId = (++matIdCnt);
IntervalVector nl = this->mats[numM].col(ncol);
Interval sqnormNL = nl.sqnorm2();
/* constructing new inverse matrix :
M''^-1 = T2 M'^-1
with T2 = (Id except nlig which is nl.ci/nl^2 (or 1) )
and rhs :
V' = T2 V
*/
IntervalVector prod = nl*this->mats[numM]; /* dot products */
for (int i=0;i<dim;i=i+1) {
if (i==ncol) continue;
this->Imats[numM][ncol] += (prod[i]/sqnormNL)*this->Imats[numM][i];
this->rhs[numM][ncol] += (prod[i]/sqnormNL)*this->rhs[numM][i];
}
/* constructing new matrix (just change nth column) */
for (int i=0;i<dim;i=i+1) {
if (i==ncol) continue;
Interval md = prod[i]/sqnormNL;
for (int j=0;i<dim;i=i+1) {
this->mats[numM][j][i] -= md*nl[ncol];
}
}
return *this;
}
/* replace a generator (column) in a matrix by another one */
/* numM= -1 : last matrix ; ncol=-1 : find best column */
/* modify numM and ncol to get the matrix and column changed */
IVparals& IVparals::replaceVectorMat
(const IntervalVector& nl, int& numM, int& ncol,
bool ortho) {
assert(numM>=-1 && numM<this->nbmat);
if (numM==-1) numM=this->nbmat-1;
assert(ncol>=-1 && ncol<dim);
bool tryPoint=false;
IntervalVector u = this->Imats[numM] * nl;
while (ncol==-1) {
/* look for a replaceable column : we consider
M^(-1) * nl = (u1,u2,...) and select u_i s.t. 0 \notin u_i
et mag(rhs[i]/ui) minimal
-> pb if all u_i has 0 */
double valbest=0.0;
for (int i=0;i<dim;i=i+1) {
if (!u[i].contains(0.0)) {
double val = (this->rhs[numM][i]/u[i]).mag();
if (ncol==-1 || val<valbest) {
ncol=i; valbest=val;
}
}
}
if (ncol==-1) {
assert(!tryPoint); /* FIXME : other approach? */
this->toPointMatrices();
u = this->Imats[numM] * nl;
tryPoint=true;
}
}
this->matId = (++matIdCnt);
/* constructing new inverse matrix :
M'^-1 = T M^-1
with T = (Id except ncol which is -uk/ui (or 1/ui) )
and rhs :
V' = T V
*/
this->Imats[numM][ncol] *= (1.0/u[ncol]);
this->rhs[numM][ncol] /= u[ncol];
for (int i=0;i<dim;i=i+1) {
if (i==ncol) continue;
this->Imats[numM][i] -= u[i]*this->Imats[numM][ncol];
this->rhs[numM][i] -= u[i]*this->rhs[numM][ncol];
}
/* constructing new matrix (just change nth column) */
for (int i=0;i<dim;i=i+1) {
this->mats[numM][i][ncol] = nl[i];
}
if (ortho) { /* warning : quasi-point matrices recommended... */
this->orthogonalise(numM,ncol);
}
return *this;
}
/* replace a constraint (line of inverse) in a matrix by another one */
/* numM= -1 : last matrix ; nlig=-1 : find best line */
/* modify numM and ncol to get the matrix and line changed */
IVparals& IVparals::replaceVectorImat
(const IntervalVector& nl, int& numM, int& nlig) {
assert(numM>=-1 && numM<this->nbmat);
if (numM==-1) numM=this->nbmat-1;
assert(nlig>=-1 && nlig<dim);
bool tryPoint=false;
IntervalVector u = nl * this->mats[numM];
while (nlig==-1) {
/* look for a replaceable column : we consider
nl * M= (u1,u2,...) and select u_i s.t. diam(rhs[i])*mig(ui) maximal
(diam(rhs[i]) : we don't want to lose "strong" constraint)
-> pb if all u_i has 0 */
double valbest=-1.0;
for (int i=0;i<dim;i=i+1) {
if (!u[i].contains(0.0)) {
double val = this->rhs[numM][i].diam() * u[i].mig();
if (val>valbest) {
nlig=i; valbest=val;
}
}
}
if (nlig==-1) {
assert(!tryPoint); /* FIXME : other approach? */
this->toPointMatrices();
u = nl * this->mats[numM];
tryPoint=true;
}
}
this->matId = (++matIdCnt);
/* constructing new inverse matrix :
M'^-1 = replacement of nl in ith line
M' = M T with T = (Id except nlig which is -uk/ui (or 1/ui)
and rhs :
V' = T^-1 V ( T^-1 = (Id except nlig which is u) )
*/
this->Imats[numM][nlig] = nl;
for (int i=0;i<dim;i=i+1)
this->mats[numM][i][nlig] /= u[i];
for (int i=0;i<dim;i=i+1)
for (int j=0;j<dim;j=j+1) {
if (j==nlig) continue;
this->mats[numM][i][j] -= u[j]*this->mats[numM][i][nlig];
}
this->rhs[numM][nlig] = u*this->rhs[numM];
return *this;
}
void IVparals::simplify(double ratio, int nbit) {
if (this->empty) return;
/* FIXME : use ratio, rel_distance and a stack ??? */
for (int i=0;i<=nbit;i++) {
this->rhs[0] &= this->Imats[0] * this->rhs[nbmat];
this->rhs[nbmat] &= this->mats[0] * this->rhs[0];
if (!bwd_mul(this->rhs[0],this->Imats[0],
this->rhs[nbmat],ratio)) {
this->set_empty(); return; }
if (!bwd_mul(this->rhs[nbmat],this->mats[0],
this->rhs[0],ratio)) {
this->set_empty(); return; }
for (int j=0;j<nbmat-1;j++) {
IntervalMatrix Jp1J = this->Imats[j+1] * this->mats[j];
IntervalMatrix JJp1 = this->Imats[j] * this->mats[j+1];
this->rhs[j+1] &= Jp1J * this->rhs[j];
this->rhs[j] &= JJp1 * this->rhs[j+1];
if (this->rhs[j].is_empty() || this->rhs[j+1].is_empty())
{ this->set_empty(); return; }
if (!bwd_mul(this->rhs[j+1],Jp1J,this->rhs[j],ratio)) {
this->set_empty(); return; }
if (!bwd_mul(this->rhs[j],JJp1,this->rhs[j+1],ratio)) {
this->set_empty(); return; }
}
this->rhs[nbmat] &= this->mats[nbmat-1] * this->rhs[nbmat-1];
this->rhs[nbmat-1] &= this->Imats[nbmat-1] * this->rhs[nbmat];
if (!bwd_mul(this->rhs[nbmat],this->mats[nbmat-1],
this->rhs[nbmat-1],ratio)) {
this->set_empty(); return; }
if (!bwd_mul(this->rhs[nbmat-1],this->Imats[nbmat-1],
this->rhs[nbmat],ratio)) {
this->set_empty(); return; }
}
}
/** generate a list of (2D) points, the convex hull of which is an
* (over)-approximation of the projection of the polyhedron
*/
ConvexPolygon IVparals::over_polygon(const Matrix& M) const {
/* first we generate a projection of the parallelotope */
if (this->empty) return ConvexPolygon();
/* just the first polygon (not manage intersection) */
Vector V1(this->dim);
ConvexPolygon res;
/* compute the projection for large dimension is a bit complex
(but interesting), will do it dirty */
for (int k=0;k<=this->nbmat;k++) {
bool val[this->dim];
vector<ThickPoint> lpoints;
for (int i=0;i<this->dim;i++) {
val[i]=false;
V1[i] = this->rhs[k][i].lb();
}
while (true) {
if (k<this->nbmat) {
lpoints.push_back(ThickPoint(M*(this->mats[k]*V1)));
} else {
lpoints.push_back(ThickPoint(M*V1));
}
int j=dim-1;
while (j>=0 && val[j]==true) {
V1[j]=this->rhs[k][j].lb();
val[j]=false;
j--;
}
if (j<0) break;
val[j]=true;
V1[j] = this->rhs[k][j].ub();
}
ConvexPolygon a(lpoints);
if (k==0) res=a; else res= res & a;
}
return res;
}
std::ostream& operator<<(ostream& str, const IVparals& iv) {
if (iv.empty) { str << "IVparals : empty\n" << flush; return str; }
str << "IVparals : box " << iv.rhs[iv.nbmat] << "\n";
for (int i=0;i<iv.nbmat;i++) {
str << " /\\ " << iv.mats[i] << "\n X " << iv.rhs[i];
}
str << "\n" << flush;
return str;
}
#if 0
/**** old code ****/
void IVparals::join_with(const IVparals& iv) {
if (iv.is_empty()) return;
if (this->isempty) {
this->center = iv.center;
this->rhs[this->nbmat] = iv.rhs[iv.nbmat];
for (int i=0;i<this->nbmat;i++) {
this->rhs[i] = this->Imats[i] * iv.rhs[iv.nbmat];
for (int j=0;j<this->nbmat;i++) {
this->rhs[i] &= (this->Imats[i] * iv.mats[j]) * iv.rhs[j];
}
}
} else {
this->rhs[this->nbmat] |= (iv.rhs[iv.nbmat] + iv.center - this->center);
Vector nCent = this->rhs[this->nbmat].mid() + this->center;
this->recenter(nCent);
Vector bCent = iv.center - this->center;
for (int i=0;i<this->nbmat;i++) {
IntervalVector nM =
this->Imats[i] * (iv.rhs[iv.nbmat] + bCent);
for (int j=0;j<this->nbmat;i++) {
nM &= (this->Imats[i] * iv.mats[j]) * iv.rhs[j] +
(this->Imats[i] * bCent);
}
this->rhs[i] |= nM;
}
this->simplify(true);
}
}
/*
void IVparals::intersect_with(const IntervalVector& iv, int d, double val) {
if (this->isempty) return;
Vector newCenter = iv.mid();
newCenter[d]=val;
this->recenter(newCenter);
this->rhs[this->nbmat] &= iv - newCenter[d];
this->rhs[this->nbmat][d]= Interval::zero();
this->simplify(true);
/* FIXME : make a cuboid */ /*
if (!this->isempty) {
this->rhs[
}
}
*/
void IVparals::intersect_with(/* const */ IVparals& ivp) {
// TODO : intersection with the different elements
this->intersect_with(ivp.bounding_box());
}
/* intersection with quasi-linear constraints,
keeping the actual "constraints boxes"
*/
void IVparals::intersect_with(const IntervalMatrix& M,
const IntervalVector& Y) {
if (this->isempty) return;
IntervalMatrix M1 = M;
IntervalVector cRhs = this->rhs[this->nbmat] + this->center;
bwd_mul(Y,M1,cRhs,0.001);
if (cRhs.is_empty()) { this->isempty=true;
this->rhs[this->nbmat].set_empty(); return; }
this->rhs[nbmat] &= cRhs - this->center;
for (int j=0;j<this->nbmat;j++) {
IntervalMatrix M1 = M * this->mats[j];
IntervalVector cRhs = this->rhs[j] + this->Imats[j] * this->center;
bwd_mul(Y,M1,cRhs,0.001);
if (cRhs.is_empty()) {
this->isempty=true;
this->rhs[this->nbmat].set_empty(); return; }
this->rhs[j] &= cRhs - this->Imats[j] * this->center;
}
this->simplify(true);
}
/* intersection with quasi-linear constraints,
keeping the actual "constraints boxes"
*/
void IVparals::intersect_with(const IntervalMatrix& M, const Vector& c,
const IntervalVector& Y) {
if (this->isempty) return;
IntervalMatrix M1 = M;
IntervalVector cRhs = this->rhs[this->nbmat] + (this->center - c);
bwd_mul(Y,M1,cRhs,0.001);
if (cRhs.is_empty()) { this->isempty=true;
this->rhs[this->nbmat].set_empty(); return; }
this->rhs[nbmat] &= cRhs - (this->center - c);
for (int j=0;j<this->nbmat;j++) {
IntervalMatrix M1 = M * this->mats[j];
IntervalVector cRhs = this->rhs[j] + this->Imats[j] * (this->center-c);
bwd_mul(Y,M1,cRhs,0.001);
if (cRhs.is_empty()) {
this->isempty=true;
this->rhs[this->nbmat].set_empty(); return; }
this->rhs[j] &= (cRhs - this->Imats[j] * (this->center-c));
}
this->simplify(true);
}
void IVparals::inflate_from_base_fast(const IVparals& iv, double fact) {
assert(this->nbmat==iv.nbmat);
for (int i=0;i<=this->nbmat;i++) {
IntervalVector& a = this->rhs[i];
const IntervalVector& b = iv.rhs[i];
for (int j=0;j<dim;j++) {
double mn = fact*(b[j].lb() - a[j].lb());
if (mn>0) mn=0.0;
double mx = fact*(b[j].ub() - a[j].ub());
if (mx<0) mx=0.0;
Interval ev(mn,mx);
a[j] += ev;
}
}
}
bool IVparals::join_intersect_with(const IVparals& iv,
const IntervalVector& box, int d, double val) {
if (iv.is_empty()) return false;
bool ret = false;
IntervalVector bbox(iv.bounding_box());
bbox &= box;
if (!bbox[d].contains(val)) return false;
bbox[d] = val;
IVparals ivInter(*this,bbox);
for (int j=0;j<iv.nbmat;j++) {
ivInter.rhs[this->nbmat] &= iv.mats[j] * iv.rhs[j] +