hyb_parameter.cpp
Go to the documentation of this file.
1 /** @file hyb_parameter.cpp Parameters of linear hybrid automata */
2 
3 /*
4  Hybrid systems plug-in for FAU Discrete Event Systems Library
5 
6  Copyright (C) 2010 Thomas Moor
7 
8 */
9 
10 
11 
12 #include "hyb_parameter.h"
13 
14 using namespace faudes;
15 
16 
17 /*
18 *************************************************************************
19 *************************************************************************
20 *************************************************************************
21 
22 Implementation: Matrix
23 
24 *************************************************************************
25 *************************************************************************
26 *************************************************************************
27 */
28 
29 // faudes Type std
31 
32 // Constructor
33 Matrix::Matrix(void) :
34  mRC(0),
35  mCC(0),
36  mpData(0)
37 {
38 }
39 
40 // Constructor
41 Matrix::Matrix(int rc, int cc) :
42  mRC(0),
43  mCC(0),
44  mpData(0)
45 {
46  if(rc<0 || cc<0) {
47  std::stringstream errstr;
48  errstr << "Index out of range.";
49  throw Exception("Matrix::Matrix()", errstr.str(), 700);
50  }
51  mRC=rc;
52  mCC=cc;
53  if(mRC>0)
54  if(mCC>0)
55  mpData = new Scalar::Type[mRC*mCC];
56 }
57 
58 
59 // Constructor
60 Matrix::Matrix(const Matrix& rSrc) :
61  mRC(0),
62  mCC(0),
63  mpData(0)
64 {
65  DoAssign(rSrc);
66 }
67 
68 // Constructor
69 Matrix::Matrix(const std::string& rFileName) :
70  mRC(0),
71  mCC(0),
72  mpData(0)
73 {
74  Read(rFileName);
75 }
76 
77 
78 // Destructor
80  if(mpData) delete mpData;
81 }
82 
83 
84 // virtual Assignment method
86  mRC=rSrc.mRC;
87  mCC=rSrc.mCC;
88  if(mpData) delete mpData;
89  mpData=0;
90  if(mRC>0)
91  if(mCC>0)
92  mpData = new Scalar::Type[mRC*mCC];
93  int sz=Size();
94  for(int k=0; k<sz; k++)
95  mpData[k]=rSrc.mpData[k];
96  return *this;
97 }
98 
99 // virtual compare
100 bool Matrix::DoEqual(const Matrix& rSrc) const {
101  if(mRC!=rSrc.mRC) return false;
102  if(mCC!=rSrc.mCC) return false;
103  int sz=Size();
104  for(int k=0; k<sz; k++)
105  if(mpData[k]!=rSrc.mpData[k]) return false;
106  return true;
107 }
108 
109 
110 // Clear
111 void Matrix::Clear(void) {
112  mRC=0;
113  mCC=0;
114  if(mpData) delete mpData;
115  mpData=0;
116 }
117 
118 
119 // Size
120 Idx Matrix::Size(void) const {
121  return mRC*mCC;
122 }
123 
124 
125 // Set Dimension.
126 void Matrix::RowCount(int rc) {
127  if(mRC==rc) return;
128  Clear();
129  if(rc<0) return;
130  mRC=rc;
131  if(mRC>0)
132  if(mCC>0)
133  mpData = new Scalar::Type[mRC*mCC];
134 }
135 
136 
137 // Set Dimension.
138 void Matrix::ColumnCount(int cc) {
139  if(mCC==cc) return;
140  Clear();
141  if(cc<0) return;
142  mCC=cc;
143  if(mRC>0)
144  if(mCC>0)
145  mpData = new Scalar::Type[mRC*mCC];
146 }
147 
148 // Set Dimension.
149 void Matrix::Dimension(int rc, int cc) {
150  if(mCC==cc && mRC==rc) return;
151  Clear();
152  if(rc<0) return;
153  if(cc<0) return;
154  mRC=rc;
155  mCC=cc;
156  if(mRC>0)
157  if(mCC>0)
158  mpData = new Scalar::Type[mRC*mCC];
159 }
160 
161 // Get Dimension.
162 int Matrix::RowCount(void) const {
163  return mRC;
164 }
165 
166 // Get Dimension.
167 int Matrix::ColumnCount(void) const {
168  return mCC;
169 }
170 
171 // Zero
172 void Matrix::Zero(int rc, int cc) {
173  if(rc >=0 && rc >=0) Dimension(rc,cc);
174  int sz = Size();
175  for(int k=0; k<sz; k++) mpData[k]=0;
176 }
177 
178 
179 // Zero
180 void Matrix::Identity(int dim) {
181  if(dim >=0) Dimension(dim,dim);
182  int k=0;
183  for(int i=0; i<mRC; i++) {
184  for(int j=0; j<mCC; j++) {
185  mpData[k]=0;
186  if(i==j) mpData[k]=1;
187  k++;
188  }
189  }
190 }
191 
192 
193 // Access
194 const Scalar::Type& Matrix::At(int i, int j) const {
195  if(i<0 || i>= mRC || j<0 || j>= mCC) {
196  std::stringstream errstr;
197  errstr << "Index out of range.";
198  throw Exception("Matrix::At", errstr.str(), 700);
199  }
200  return mpData[i*mCC+j];
201 }
202 
203 // Access
204 void Matrix::At(int i, int j, const Scalar::Type& val) {
205  if(i<0 || i>= mRC || j<0 || j>= mCC) {
206  std::stringstream errstr;
207  errstr << "Index out of range.";
208  throw Exception("Matrix::At", errstr.str(), 700);
209  }
210  mpData[i*mCC+j]=val;
211 }
212 
213 
214 // Access
215 const Scalar::Type& Matrix::operator()(int i, int j) const {
216  if(i<0 || i>= mRC || j<0 || j>= mCC) {
217  std::stringstream errstr;
218  errstr << "Index out of range.";
219  throw Exception("Matrix::At", errstr.str(), 700);
220  }
221  return mpData[i*mCC+j];
222 }
223 
224 // Access
226  if(i<0 || i>= mRC || j<0 || j>= mCC) {
227  std::stringstream errstr;
228  errstr << "Index out of range.";
229  throw Exception("Matrix::At", errstr.str(), 700);
230  }
231  return mpData[i*mCC+j];
232 }
233 
234 
235 // C array access
236 void Matrix::CArray(int rc, int cc, const Scalar::Type *data) {
237  Dimension(rc,cc);
238  int sz=Size();
239  for(int k=0; k<sz; k++)
240  mpData[k]=data[k];
241 }
242 
243 // C array access
244 /*
245 void Matrix::CArray(int rc, int cc, Scalar::Type **data) {
246  Clear();
247  if(rc<0) return;
248  if(cc<0) return;
249  mRC=rc;
250  mCC=cc;
251  mpData=*data;
252 }
253 */
254 
255 // C array access
256 const Scalar::Type* Matrix::CArray(void) const {
257  return mpData;
258 }
259 
260 // token IO
261 void Matrix::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
262  // ignore context
263  (void) pContext;
264  // figure section
265  std::string label=rLabel;
266  if(rLabel=="") label="Matrix";
267  // write section
268  Token btoken;
269  btoken.SetBegin(label);
270  btoken.InsAttributeInteger("rows",mRC);
271  btoken.InsAttributeInteger("columns",mCC);
272  rTw << btoken;
273  int oldcols = rTw.Columns();
274  rTw.Columns(mCC);
275  for(unsigned int k=0; k<Size(); k++)
276  rTw.WriteFloat(mpData[k]);
277  rTw.Columns(oldcols);
278  // end section
279  rTw.WriteEnd(label);
280 }
281 
282 // token IO
283 void Matrix::DoSWrite(TokenWriter& rTw) const {
284  // write dimensions
285  rTw.WriteComment("Matrix with dimensions " + ToStringInteger(mRC)
286  + "x" +ToStringInteger(mCC));
287 }
288 
289 
290 // token IO
291 void Matrix::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
292  // ignore context
293  (void) pContext;
294  // figure section
295  std::string label=rLabel;
296  if(rLabel=="") label="Matrix";
297  // find section
298  Token btoken;
299  rTr.ReadBegin(label,btoken);
300  // read dimensions
301  int rc=btoken.AttributeIntegerValue("rows");
302  int cc=btoken.AttributeIntegerValue("columns");
303  // test/allocate (token integers are nonnegative anyway .. but this may change)
304  if(rc <0 || cc <0) {
305  std::stringstream errstr;
306  errstr << "Invalid dimension. " << rTr.FileLine();
307  throw Exception("Matrix::DoRead", errstr.str(), 700);
308  }
309  Dimension(rc,cc);
310  // read data
311  int k=0;
312  while(!rTr.Eos(label) && k<(int)Size()) {
313  mpData[k]=rTr.ReadFloat();
314  k++;
315  }
316  // test data
317  if(k!=(int)Size()) {
318  std::stringstream errstr;
319  errstr << "Dimension mismatch. " << rTr.FileLine();
320  throw Exception("Matrix::DoRead", errstr.str(), 700);
321  }
322  // end section
323  rTr.ReadEnd(label);
324 }
325 
326 
327 /*
328 *************************************************************************
329 *************************************************************************
330 *************************************************************************
331 
332 Implementation: Vector
333 
334 *************************************************************************
335 *************************************************************************
336 *************************************************************************
337 */
338 
339 // faudes Type std
341 
342 // Constructor
343 Vector::Vector(void) :
344  mDim(0),
345  mpData(0)
346 {
347 }
348 
349 // Constructor
350 Vector::Vector(int dim) :
351  mDim(0),
352  mpData(0)
353 {
354  if(dim<0) {
355  std::stringstream errstr;
356  errstr << "Index out of range.";
357  throw Exception("Vector::Vector()", errstr.str(), 700);
358  }
359  mDim=dim;
360  if(mDim>0)
361  mpData = new Scalar::Type[mDim];
362 }
363 
364 
365 // Constructor
366 Vector::Vector(const Vector& rSrc) :
367  mDim(0),
368  mpData(0)
369 {
370  DoAssign(rSrc);
371 }
372 
373 // Constructor
374 Vector::Vector(const std::string& rFileName) :
375  mDim(0),
376  mpData(0)
377 {
378  Read(rFileName);
379 }
380 
381 
382 // Destructor
384  if(mpData) delete mpData;
385 }
386 
387 
388 // virtual Assignment method
390  mDim=rSrc.mDim;
391  if(mpData) delete mpData;
392  mpData=0;
393  if(mDim>0)
394  mpData = new Scalar::Type[mDim];
395  for(int k=0; k<mDim; k++)
396  mpData[k]=rSrc.mpData[k];
397  return *this;
398 }
399 
400 // virtual compare
401 bool Vector::DoEqual(const Vector& rSrc) const {
402  if(mDim!=rSrc.mDim) return false;
403  for(int k=0; k<mDim; k++)
404  if(mpData[k]!=rSrc.mpData[k]) return false;
405  return true;
406 }
407 
408 
409 // Clear
410 void Vector::Clear(void) {
411  mDim=0;
412  if(mpData) delete mpData;
413  mpData=0;
414 }
415 
416 
417 // Size
418 Idx Vector::Size(void) const {
419  return mDim;
420 }
421 
422 
423 // Set Dimension.
424 void Vector::Dimension(int dim) {
425  if(mDim==dim) return;
426  Clear();
427  if(dim<0) return;
428  mDim=dim;
429  if(mDim>0)
430  mpData = new Scalar::Type[mDim];
431 }
432 
433 
434 // Get Dimension.
435 int Vector::Dimension(void) const {
436  return mDim;
437 }
438 
439 
440 // Zero
441 void Vector::Zero(int dim) {
442  if(dim >=0) Dimension(dim);
443  for(int k=0; k<mDim; k++) mpData[k]=0;
444 }
445 
446 
447 // Ones
448 void Vector::Ones(int dim) {
449  if(dim >=0) Dimension(dim);
450  for(int k=0; k<mDim; k++) mpData[k]=1;
451 }
452 
453 
454 // Access
455 const Scalar::Type& Vector::At(int i) const {
456  if(i<0 || i>= mDim) {
457  std::stringstream errstr;
458  errstr << "Index out of range.";
459  throw Exception("Vector::At", errstr.str(), 700);
460  }
461  return mpData[i];
462 }
463 
464 // Access
465 void Vector::At(int i, const Scalar::Type& val) {
466  if(i<0 || i>= mDim) {
467  std::stringstream errstr;
468  errstr << "Index out of range.";
469  throw Exception("Vector::At", errstr.str(), 700);
470  }
471  mpData[i]=val;
472 }
473 
474 
475 // Access
476 const Scalar::Type& Vector::operator()(int i) const {
477  if(i<0 || i>= mDim) {
478  std::stringstream errstr;
479  errstr << "Index out of range.";
480  throw Exception("Vector::At", errstr.str(), 700);
481  }
482  return mpData[i];
483 }
484 
485 // Access
487  if(i<0 || i>= mDim) {
488  std::stringstream errstr;
489  errstr << "Index out of range.";
490  throw Exception("Vector::At", errstr.str(), 700);
491  }
492  return mpData[i];
493 }
494 
495 
496 // C array access
497 void Vector::CArray(int dim, const Scalar::Type *data) {
498  Dimension(dim);
499  for(int k=0; k<dim; k++)
500  mpData[k]=data[k];
501 }
502 
503 
504 // C array access
505 const Scalar::Type* Vector::CArray(void) const {
506  return mpData;
507 }
508 
509 
510 // token IO
511 void Vector::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
512  // ignore context
513  (void) pContext;
514  // figure section
515  std::string label=rLabel;
516  if(rLabel=="") label="Vector";
517  // write section
518  Token btoken;
519  btoken.SetBegin(label);
520  btoken.InsAttributeInteger("count",mDim);
521  rTw << btoken;
522  int oldcols = rTw.Columns();
523  rTw.Columns(mDim);
524  for(int k=0; k<mDim; k++)
525  rTw.WriteFloat(mpData[k]);
526  rTw.Columns(oldcols);
527  // end section
528  rTw.WriteEnd(label);
529 }
530 
531 // token IO
532 void Vector::DoSWrite(TokenWriter& rTw) const {
533  // write dimensions
534  rTw.WriteComment("Vector with dimension " + ToStringInteger(mDim));
535 }
536 
537 
538 // token IO
539 void Vector::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
540  // ignore context
541  (void) pContext;
542  // figure section
543  std::string label=rLabel;
544  if(rLabel=="") label="Vector";
545  // find section
546  Token btoken;
547  rTr.ReadBegin(label,btoken);
548  // read dimensions
549  int dim=btoken.AttributeIntegerValue("count");
550  // test/allocate (token integers are nonnegative anyway .. but this may change)
551  if(dim <0) {
552  std::stringstream errstr;
553  errstr << "Invalid dimension. " << rTr.FileLine();
554  throw Exception("Vector::DoRead", errstr.str(), 700);
555  }
556  Dimension(dim);
557  // read data
558  int k=0;
559  while(!rTr.Eos(label) && k<(int)Size()) {
560  mpData[k]=rTr.ReadFloat();
561  k++;
562  }
563  // test data
564  if(k!=(int)Size()) {
565  std::stringstream errstr;
566  errstr << "Dimension mismatch. " << rTr.FileLine();
567  throw Exception("Vector::DoRead", errstr.str(), 700);
568  }
569  // end section
570  rTr.ReadEnd(label);
571 }
572 
573 
574 /*
575 *************************************************************************
576 *************************************************************************
577 *************************************************************************
578 
579 Implementation: Polyhedron
580 
581 *************************************************************************
582 *************************************************************************
583 *************************************************************************
584 */
585 
586 // faudes Type std
588 
589 // Constructor
591  mName(),
592  mA(0,0),
593  mB(0),
594  mpUserData(0)
595 {
596 }
597 
598 // Constructor
600  mName(),
601  mA(0,0),
602  mB(0),
603  mpUserData(0)
604 {
605  if(dim<0) {
606  std::stringstream errstr;
607  errstr << "Index out of range.";
608  throw Exception("Polyhedron::Polyhedron()", errstr.str(), 700);
609  }
610  mA.Dimension(0,dim);
611  mB.Dimension(0);
612 }
613 
614 
615 // Constructor
617  mName(),
618  mA(0,0),
619  mB(0),
620  mpUserData(0)
621 {
622  DoAssign(rSrc);
623 }
624 
625 // Constructor
626 Polyhedron::Polyhedron(const std::string& rFileName) :
627  mName(),
628  mA(0,0),
629  mB(0),
630  mpUserData(0)
631 {
632  Read(rFileName);
633 }
634 
635 
636 // Destructor
638  if(mpUserData) delete mpUserData;
639 }
640 
641 
642 // virtual Assignment method
644  mName=rSrc.mName;
645  mA=rSrc.mA;
646  mB=rSrc.mB;
647  if(mpUserData) delete mpUserData;
648  mpUserData=0;
649  return *this;
650 }
651 
652 // virtual compare
653 bool Polyhedron::DoEqual(const Polyhedron& rSrc) const {
654  if(mA!=rSrc.mA) return false;
655  if(mB!=rSrc.mB) return false;
656  return true;
657 }
658 
659 
660 // Clear
661 void Polyhedron::Clear(void) {
662  Dimension(0);
663 }
664 
665 
666 // Size
667 Idx Polyhedron::Size(void) const {
668  return mA.RowCount();
669 }
670 
671 
672 // Set Dimension.
673 void Polyhedron::Dimension(int dim) {
674  if(dim<0) {
675  std::stringstream errstr;
676  errstr << "Invalid dimension";
677  throw Exception("Polyhedron::Dimension", errstr.str(), 700);
678  }
679  mA.Dimension(0,dim);
680  mB.Dimension(0);
681  if(mpUserData) delete mpUserData;
682  mpUserData=0;
683 }
684 
685 
686 // Get Dimension.
687 int Polyhedron::Dimension(void) const {
688  return mA.ColumnCount();
689 }
690 
691 
692 // Access
693 const Matrix& Polyhedron::A(void) const {
694  return mA;
695 }
696 
697 // Access
698 const Vector& Polyhedron::B(void) const {
699  return mB;
700 }
701 
702 // Access
703 void Polyhedron::AB(const Matrix& rA, const Vector& rB) {
704  if(rA.RowCount()!=rB.Dimension()) {
705  std::stringstream errstr;
706  errstr << "Invalid dimansions.";
707  throw Exception("Polyhedron::AB", errstr.str(), 700);
708  }
709  if(mA.ColumnCount()!=rA.ColumnCount()) Dimension(rA.ColumnCount());
710  mA=rA;
711  mB=rB;
712  if(mpUserData) delete mpUserData;
713  mpUserData=0;
714 }
715 
716 
717 // User data
718 void Polyhedron::UserData(Type* data) const {
719  Polyhedron* fakeconst= const_cast<Polyhedron*>(this);
720  if(mpUserData) delete fakeconst->mpUserData;
721  fakeconst->mpUserData=data;
722 }
723 
724 // User data
726  return mpUserData;
727 }
728 
729 
730 
731 // token IO
732 void Polyhedron::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
733  // ignore context
734  (void) pContext;
735  // figure section
736  std::string label=rLabel;
737  std::string ftype="Polyhedron";
738  if(rLabel=="") label=ftype;
739  Token btag;
740  btag.SetBegin(label);
741  if(label!=ftype) btag.InsAttribute("ftype",ftype);
742  if((Name()!=label) && (Name()!="")) btag.InsAttribute("name",Name());
743  // write section
744  rTw.Write(btag);
745  // write data
746  mA.Write(rTw,"AMatrix");
747  mB.Write(rTw,"BVector");
748  // end section
749  rTw.WriteEnd(label);
750 }
751 
752 // token IO
754  // write dimensions
755  rTw.WriteComment("Polyhedron statistics");
756  rTw.WriteComment(" dimension: "+ ToStringInteger(Dimension()));
757  rTw.WriteComment(" inequalities: "+ ToStringInteger(Size()));
758 }
759 
760 
761 // token IO
762 void Polyhedron::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
763  // ignore context
764  (void) pContext;
765  // figure section
766  std::string label=rLabel;
767  if(rLabel=="") label="Polyhedron";
768  // find section
769  Token btag;
770  rTr.ReadBegin(label,btag);
771  if(btag.ExistsAttributeString("name"))
772  Name(btag.AttributeStringValue("name"));
773  // read data
774  mA.Read(rTr,"AMatrix");
775  mB.Read(rTr,"BVector");
776  // test
777  if(mA.RowCount()!=mB.Dimension()) {
778  std::stringstream errstr;
779  errstr << "Dimension mismatch, rows. " << rTr.FileLine();
780  throw Exception("Polyhedron::DoRead", errstr.str(), 700);
781  }
782  // end section
783  rTr.ReadEnd(label);
784  // note: this should not be nacessary since Read calls Clear before DoRead
785  //if(mpUserData) delete mpUserData;
786  //mpUserData=0;
787 }
788 
789 
790 /*
791 *************************************************************************
792 *************************************************************************
793 *************************************************************************
794 
795 Implementation: Relation
796 
797 *************************************************************************
798 *************************************************************************
799 *************************************************************************
800 */
801 
802 // faudes Type std
804 
805 // Constructor
807  mName(),
808  mCase(I),
809  mA1(0,0),
810  mA2(0,0),
811  mB(0),
812  mC(0,0),
813  mD(0),
814  mpUserData(0)
815 {
816 }
817 
818 // Constructor
820  mName(),
821  mCase(I),
822  mA1(0,0),
823  mA2(0,0),
824  mB(0),
825  mC(0,0),
826  mD(0),
827  mpUserData(0)
828 {
829  if(dim<0) {
830  std::stringstream errstr;
831  errstr << "Dimension out of range.";
832  throw Exception("LinearRelation::LinearRelation()", errstr.str(), 700);
833  }
834  Identity(dim);
835 }
836 
837 
838 // Constructor
840  mName(),
841  mCase(I),
842  mA1(0,0),
843  mA2(0,0),
844  mB(0),
845  mC(0,0),
846  mD(0),
847  mpUserData(0)
848 {
849  DoAssign(rSrc);
850 }
851 
852 // Constructor
853 LinearRelation::LinearRelation(const std::string& rFileName) :
854  mName(),
855  mCase(I),
856  mA1(0,0),
857  mA2(0,0),
858  mB(0),
859  mC(0,0),
860  mD(0),
861  mpUserData(0)
862 {
863  Read(rFileName);
864 }
865 
866 
867 // Destructor
869  if(mpUserData) delete mpUserData;
870 }
871 
872 
873 // virtual Assignment method
875  mName=rSrc.mName;
876  mCase=rSrc.mCase;
877  mA1=rSrc.mA1;
878  mA2=rSrc.mA2;
879  mB=rSrc.mB;
880  mC=rSrc.mC;
881  mD=rSrc.mD;
882  if(mpUserData) delete mpUserData;
883  mpUserData=0;
884  return *this;
885 }
886 
887 // virtual compare
888 bool LinearRelation::DoEqual(const LinearRelation& rSrc) const {
889  if(mCase!=rSrc.mCase) return false;
890  if(mA1!=rSrc.mA1) return false;
891  if(mA2!=rSrc.mA2) return false;
892  if(mB!=rSrc.mB) return false;
893  if(mC!=rSrc.mC) return false;
894  if(mD!=rSrc.mD) return false;
895  return true;
896 }
897 
898 
899 // Clear
901  Dimension(0);
902 }
903 
904 
905 // Size
907  return mB.Dimension();
908 }
909 
910 
911 // Set Dimension.
913  if(dim<0) {
914  std::stringstream errstr;
915  errstr << "Invalid dimension";
916  throw Exception("LinearRelation::Dimension", errstr.str(), 700);
917  }
918  Identity(dim);
919 }
920 
921 
922 // Get Dimension.
923 int LinearRelation::Dimension(void) const {
924  if(Relation()) return mA1.ColumnCount();
925  return mC.ColumnCount();
926 }
927 
928 
929 
930 // Test case
931 bool LinearRelation::Identity(void) const {
932  if(mCase==I) return true;
933  return false;
934 }
935 
936 // Test case
937 bool LinearRelation::Map(void) const {
938  if(mCase!=R) return true;
939  return false;
940 }
941 
942 // Test case
943 bool LinearRelation::Relation(void) const {
944  return true;
945 }
946 
947 // Access
948 const Matrix& LinearRelation::A1(void) const {
949  return mA1;
950 }
951 
952 // Access
953 const Matrix& LinearRelation::A2(void) const {
954  return mA2;
955 }
956 
957 // Access
958 const Vector& LinearRelation::B(void) const {
959  return mB;
960 }
961 
962 // Access
963 const Matrix& LinearRelation::C(void) const {
964  return mC;
965 }
966 
967 // Access
968 const Vector& LinearRelation::D(void) const {
969  return mD;
970 }
971 
972 // Access
973 void LinearRelation::Relation(const Matrix& rA1, const Matrix& rA2, const Vector& rB) {
974  if(rA1.RowCount()!=rB.Dimension()) {
975  std::stringstream errstr;
976  errstr << "Invalid dimensions.";
977  throw Exception("LinearRelation::Relation", errstr.str(), 700);
978  }
979  if(rA2.RowCount()!=rB.Dimension()) {
980  std::stringstream errstr;
981  errstr << "Invalid dimensions.";
982  throw Exception("LinearRelation::Relation", errstr.str(), 700);
983  }
984  if(rA1.ColumnCount()!=rA1.ColumnCount()) {
985  std::stringstream errstr;
986  errstr << "Invalid dimensions.";
987  throw Exception("LinearRelation::Relation", errstr.str(), 700);
988  }
989  mCase=R;
990  mA1=rA1;
991  mA2=rA2;
992  mB=rB;
993  mC.Clear();
994  mD.Clear();
995  if(mpUserData) delete mpUserData;
996  mpUserData=0;
997 }
998 
999 // Access
1000 void LinearRelation::Map(const Matrix& rC, const Vector& rD) {
1001  if(rC.RowCount()!=rC.ColumnCount()) {
1002  std::stringstream errstr;
1003  errstr << "Invalid dimensions.";
1004  throw Exception("LinearRelation::Map", errstr.str(), 700);
1005  }
1006  if(rC.RowCount()!=rD.Dimension()) {
1007  std::stringstream errstr;
1008  errstr << "Invalid dimensions.";
1009  throw Exception("LinearRelation::Map", errstr.str(), 700);
1010  }
1011  mCase=M;
1012  mC=rC;
1013  mD=rD;
1014  // fix other data
1015  //
1016  // -C x1 + I x2 <= d
1017  // C x1 - I x2 <= -d
1018  //
1019  int n=rC.RowCount();
1020  mA1.Zero(2*n,n);
1021  mA2.Zero(2*n,n);
1022  mB.Dimension(2*n);
1023  for(int i =0; i<n; i++) {
1024  for(int j =0; j<n; j++) {
1025  mA1(i,j)= -mC(i,j);
1026  mA1(i+n,j)= mC(i,j);
1027  if(i==j) {
1028  mA2(i,j)=1;
1029  mA2(i+n,j)=-1;
1030  }
1031  }
1032  mB(i)=mD(i);
1033  mB(i+n)=-mD(i);
1034  }
1035  if(mpUserData) delete mpUserData;
1036  mpUserData=0;
1037 }
1038 
1039 // Access
1041  mCase=I;
1042  mA1.Clear();
1043  mA2.Clear();
1044  mB.Clear();
1045  mC.Identity(dim);
1046  mD.Zero(dim);
1047  // fix other data
1048  //
1049  // -I x1 + I x2 <= 0
1050  // I x1 - I x2 <= 0
1051  //
1052  int n=mC.RowCount();
1053  mA1.Zero(2*n,n);
1054  mA2.Zero(2*n,n);
1055  mB.Dimension(2*n);
1056  for(int i =0; i<n; i++) {
1057  mA1(i,i)= -1;
1058  mA1(i+n,i)= 1;
1059  mA2(i,i)= 1;
1060  mA2(i+n,i)= -1;
1061  }
1062  if(mpUserData) delete mpUserData;
1063  mpUserData=0;
1064 }
1065 
1066 
1067 // User data
1068 void LinearRelation::UserData(Type* data) const {
1069  LinearRelation* fakeconst= const_cast<LinearRelation*>(this);
1070  if(mpUserData) delete fakeconst->mpUserData;
1071  fakeconst->mpUserData=data;
1072 }
1073 
1074 // User data
1076  return mpUserData;
1077 }
1078 
1079 
1080 // token IO
1081 void LinearRelation::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
1082  // ignore context
1083  (void) pContext;
1084  // figure section
1085  std::string label=rLabel;
1086  if(rLabel=="") label="LinearRelation";
1087  // write section
1088  Token stoken;
1089  stoken.SetBegin(label);
1090  stoken.InsAttributeInteger("dim",Dimension());
1091  rTw.Write(stoken);
1092  // write data
1093  if(Map()) {
1094  mC.Write(rTw,"CMatrix");
1095  mD.Write(rTw,"DVector");
1096  } else {
1097  mA1.Write(rTw,"A1Matrix");
1098  mA2.Write(rTw,"A2Matrix");
1099  mB.Write(rTw,"BVector");
1100  }
1101  // end section
1102  rTw.WriteEnd(label);
1103 }
1104 
1105 // token IO
1107  // write dimensions
1108  rTw.WriteComment("LinearRelation statistics");
1109  rTw.WriteComment(" dimension: "+ ToStringInteger(Dimension()));
1110  if(mCase==I)
1111  rTw.WriteComment(" case: identity");
1112  if(mCase==M)
1113  rTw.WriteComment(" case: map");
1114  if(mCase==R)
1115  rTw.WriteComment(" inequalities: "+ ToStringInteger(Size()));
1116 }
1117 
1118 
1119 // token IO
1120 void LinearRelation::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
1121  // ignore context
1122  (void) pContext;
1123  // figure section
1124  std::string label=rLabel;
1125  if(rLabel=="") label="LinearRelation";
1126  // find section
1127  Token stoken;
1128  rTr.ReadBegin(label,stoken);
1129  int dim=-1;
1130  if(stoken.ExistsAttributeInteger("dim"))
1131  dim=stoken.AttributeIntegerValue("dim");
1132  // figure case
1133  Token token;
1134  rTr.Peek(token);
1135  // case: identity
1136  if(token.IsEnd(label)){
1137  Identity(dim);
1138  }
1139  // case: map
1140  if(token.IsBegin("CMatrix")) {
1141  Matrix c;
1142  Vector d;
1143  c.Read(rTr,"CMatrix");
1144  d.Read(rTr,"DVector");
1145  Map(c,d);
1146  }
1147  // case: relation
1148  if(token.IsBegin("A1Matrix")) {
1149  Matrix a1, a2;
1150  Vector b;
1151  a1.Read(rTr,"A1Matrix");
1152  a2.Read(rTr,"A2Matrix");
1153  b.Read(rTr,"BVector");
1154  Relation(a1,a2,b);
1155  }
1156  // should trace errors ...
1157  // end section
1158  rTr.ReadEnd(label);
1159  // .. done by virtual clear
1160  //if(mpUserData) delete mpUserData;
1161  //mpUserData=0;
1162 }
1163 
#define FAUDES_TYPE_IMPLEMENTATION(ftype, ctype, cbase)
faudes type implementation macros, overall
Definition: cfl_types.h:946
Faudes exception class.
Linear relation on R^n.
Type * mpUserData
User data.
virtual void DoSWrite(TokenWriter &tw) const
Write statistic info to TokenWriter, see Type::SWrite for public wrapper.
const Vector & B(void) const
Get B vector.
const Matrix & C(void) const
Get C matrix.
virtual void DoRead(TokenReader &tr, const std::string &rLabel="", const Type *pContext=0)
Read from TokenReader, see Type::Read for public wrappers.
Idx Size(void) const
Get size.
virtual bool DoEqual(const LinearRelation &rOther) const
Test equality.
bool Map(void) const
Test for data format.
void Clear(void)
Clear all.
const Vector & D(void) const
Get D vector.
virtual void DoWrite(TokenWriter &tw, const std::string &rLabel="", const Type *pContext=0) const
Write to TokenWriter, see Type::Write for public wrappers.
virtual ~LinearRelation(void)
Destructor.
const Matrix & A2(void) const
Get A2 matrix.
bool Relation(void) const
Test for data format.
LinearRelation(void)
Default constructor.
Matrix mA1
LinearRelation: data.
bool Identity(void) const
Test for data format.
Type * UserData(void) const
Get user data.
int Dimension(void) const
Get dimension.
TCase mCase
Case flag.
virtual LinearRelation & DoAssign(const LinearRelation &rSrc)
Assign from other polyhedron.
std::string mName
have a name
const Matrix & A1(void) const
Get A1 matrix.
Matrix of scalars.
Definition: hyb_parameter.h:64
void Identity(int dim=-1)
Set to identity matrix.
const Scalar::Type & operator()(int i, int j) const
Cosmetic access operator.
int ColumnCount(void) const
Get Dimension.
Idx Size(void) const
Get size.
void Clear(void)
Clear all.
int RowCount(void) const
Get Dimension.
virtual void DoSWrite(TokenWriter &tw) const
Write statistic info to TokenWriter, see Type::SWrite for public wrapper.
virtual void DoRead(TokenReader &tr, const std::string &rLabel="", const Type *pContext=0)
Read from TokenReader, see Type::Read for public wrappers.
Scalar::Type * mpData
virtual ~Matrix(void)
Destructor.
void RowCount(int rc)
Set dimension.
virtual void DoWrite(TokenWriter &tw, const std::string &rLabel="", const Type *pContext=0) const
Write to TokenWriter, see Type::Write for public wrappers.
Matrix(void)
Default constructor - sets dimension to 0x0.
const Scalar::Type & At(int i, int j) const
Get entry.
void Zero(int rc=-1, int cc=-1)
Set to zero matrix.
virtual bool DoEqual(const Matrix &rOther) const
Test equality.
void ColumnCount(int cc)
Set Dimension.
const Scalar::Type * CArray(void) const
Access std C array.
virtual Matrix & DoAssign(const Matrix &rSrc)
Assign from other matrix.
void Dimension(int rc, int cc)
Set Dimension.
Polyhedron in R^n.
virtual void DoWrite(TokenWriter &tw, const std::string &rLabel="", const Type *pContext=0) const
Write to TokenWriter, see Type::Write for public wrappers.
Idx Size(void) const
Get size.
virtual void DoRead(TokenReader &tr, const std::string &rLabel="", const Type *pContext=0)
Read from TokenReader, see Type::Read for public wrappers.
Polyhedron(void)
Default constructor - sets dimension to 0.
void Clear(void)
Clear all.
virtual void DoSWrite(TokenWriter &tw) const
Write statistic info to TokenWriter, see Type::SWrite for public wrapper.
const Matrix & A(void) const
Get A matrix.
void AB(const Matrix &rA, const Vector &rB)
Set A matrix and B vector.
int Dimension(void) const
Get dimension.
Type * mpUserData
User data.
Matrix mA
Polyhedron: data.
Vector mB
Polyhedron: data.
const Vector & B(void) const
Get B vector.
virtual ~Polyhedron(void)
Destructor.
virtual Polyhedron & DoAssign(const Polyhedron &rSrc)
Assign from other polyhedron.
virtual const std::string & Name(void) const
Object name.
virtual bool DoEqual(const Polyhedron &rOther) const
Test equality.
Type * UserData(void) const
Get user data.
std::string mName
name
double Type
Datatype for scalar.
Definition: hyb_parameter.h:37
A TokenReader reads sequential tokens from a file or string.
std::string FileLine(void) const
Return "filename:line".
bool Eos(const std::string &rLabel)
Peek a token and check whether it ends the specified section.
void ReadEnd(const std::string &rLabel)
Close the current section by matching the previous ReadBegin().
double ReadFloat(void)
Read float token.
void ReadBegin(const std::string &rLabel)
Open a section by specified label.
bool Peek(Token &token)
Peek next token.
A TokenWriter writes sequential tokens to a file, a string or stdout.
void WriteFloat(const double &val)
Write float.
void WriteComment(const std::string &rComment)
Write comment in faudes format.
void Write(Token &rToken)
Write next token.
void WriteEnd(const std::string &rLabel)
Write end label.
int Columns(void) const
Get number of columns in a line.
Tokens model atomic data for stream IO.
Definition: cfl_token.h:54
Int AttributeIntegerValue(const std::string &name)
Access attribute value.
Definition: cfl_token.cpp:397
bool ExistsAttributeString(const std::string &name)
Test attibute existence.
Definition: cfl_token.cpp:356
bool IsBegin(void) const
Test token Type.
Definition: cfl_token.cpp:259
void InsAttribute(const std::string &name, const std::string &value)
Insert named attribute, no type.
Definition: cfl_token.cpp:300
void SetBegin(const std::string &rName)
Initialize as Begin token.
Definition: cfl_token.cpp:92
bool ExistsAttributeInteger(const std::string &name)
Test attibute existence.
Definition: cfl_token.cpp:366
void InsAttributeInteger(const std::string &name, Int value)
Insert named attribute with integer value.
Definition: cfl_token.cpp:319
bool IsEnd(void) const
Test token Type.
Definition: cfl_token.cpp:270
const std::string & AttributeStringValue(const std::string &name)
Access attribute value.
Definition: cfl_token.cpp:386
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Read configuration data from file with label specified.
Definition: cfl_types.cpp:261
void Write(const Type *pContext=0) const
Write configuration data to console.
Definition: cfl_types.cpp:139
Vector of scalars.
virtual Vector & DoAssign(const Vector &rSrc)
Assign from other vector.
Idx Size(void) const
Get size.
const Scalar::Type & operator()(int i) const
Cosmetic access operator.
const Scalar::Type * CArray(void) const
Access std C array.
const Scalar::Type & At(int i) const
Get entry.
virtual void DoSWrite(TokenWriter &tw) const
Write statistic info to TokenWriter, see Type::SWrite for public wrapper.
void Zero(int dim=-1)
Set to zero vector.
void Ones(int dim=-1)
Set to ones.
void Clear(void)
Clear all.
virtual bool DoEqual(const Vector &rOther) const
Test equality.
int Dimension(void) const
Get dimension.
virtual void DoRead(TokenReader &tr, const std::string &rLabel="", const Type *pContext=0)
Read from TokenReader, see Type::Read for public wrappers.
Vector(void)
Default constructor - sets dimension to 0.
virtual void DoWrite(TokenWriter &tw, const std::string &rLabel="", const Type *pContext=0) const
Write to TokenWriter, see Type::Write for public wrappers.
Scalar::Type * mpData
virtual ~Vector(void)
Destructor.
Parameter types for linear hybrid automata.
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
std::string ToStringInteger(Int number)
integer to string
Definition: cfl_utils.cpp:43

libFAUDES 2.32f --- 2024.12.22 --- c++ api documentaion by doxygen