ConicBundle
indexmat.hxx
Go to the documentation of this file.
1 
2 
3 #ifndef CH_MATRIX_CLASSES__INDEXMAT_HXX
4 #define CH_MATRIX_CLASSES__INDEXMAT_HXX
5 
15 #include <iostream>
16 #include <iomanip>
17 #include <vector>
18 #include <math.h>
19 #ifndef CH_MATRIX_CLASSES__MEMARRAY_HXX
20 #include "memarray.hxx"
21 #endif
22 #ifndef CH_MATRIX_CLASSES__MYMATH_HXX
23 #include "mymath.hxx"
24 #endif
25 
26 
27 namespace CH_Matrix_Classes {
28 
29 
30 //everything involving a "Sparsesym" is implemented in "sparssym.cxx/hxx"
31 //everything else involving a "Sparsemat" is implemented in "sparsmat.cxx/hxx"
32 //everything else involving a "Symmatrix" is implemented in "symmat.cxx/hxx"
33 //everything else involving a "Matrix" is implemented in "matrix.cxx/hxx"
34 
35 
39 
40 // **************************************************************************
41 // Range
42 // **************************************************************************
43 
44 //specifies a range of numbers: {i: i=from+k*step for some k\in N_0 and i<=to}
45 
47 class Range
48 {
49 public:
56 
58  Range(Integer _from,Integer _to,Integer _step=1):from(_from),to(_to),step(_step){}
60  ~Range(){}
61 };
62 
64 
65 
66 
67 // **************************************************************************
68 // Indexmatrix
69 // **************************************************************************
70 
71 class Matrix;
72 class Symmatrix;
73 class Sparsemat;
74 class Sparsesym;
75 class Indexmatrix;
76 
77 // **************************************************************************
78 // make non inline friends available outside
79 // **************************************************************************
80 // since some of them have default values, all of them are declared in advance
81 
86 
88  Indexmatrix diag(const Indexmatrix& A); //=(A(1,1),A(2,2),...)^t
90  void swap(Indexmatrix &A,Indexmatrix &B);
91 
110  Indexmatrix& xbpeya(Indexmatrix& x,const Indexmatrix& y,Integer alpha=1,Integer beta=0,int ytrans=0);
111 
113  Indexmatrix& xeyapzb(Indexmatrix& x,const Indexmatrix& y,const Indexmatrix& z,Integer alpha=1,Integer beta=1);
114 
116  Indexmatrix& genmult(const Indexmatrix& A,const Indexmatrix& B,Indexmatrix& C,
117  Integer alpha=1,Integer beta=0,int atrans=0,int btrans=0);
121  Indexmatrix abs(const Indexmatrix& A);
123  Integer trace(const Indexmatrix& A); //=sum(diag(A))
125  Indexmatrix sumrows(const Indexmatrix& A);
127  Indexmatrix sumcols(const Indexmatrix& A);
129  Integer sum(const Indexmatrix& A);
130 
131 
133  Indexmatrix operator<(const Indexmatrix &A,const Indexmatrix &B);
135  Indexmatrix operator<=(const Indexmatrix &A,const Indexmatrix &B);
137  Indexmatrix operator==(const Indexmatrix &A,const Indexmatrix &B);
139  Indexmatrix operator!=(const Indexmatrix &A,const Indexmatrix &B);
152 
154  Indexmatrix minrows(const Indexmatrix& A);
156  Indexmatrix mincols(const Indexmatrix& A);
158  Integer min(const Indexmatrix& A,Integer *iindex=0,Integer *jindex=0);
160  Indexmatrix maxrows(const Indexmatrix& A);
162  Indexmatrix maxcols(const Indexmatrix& A);
164  Integer max(const Indexmatrix& A,Integer *iindex=0,Integer *jindex=0);
165 
167  inline Indexmatrix sortindex(const Indexmatrix& vec,bool nondecreasing=true);
169  void sortindex(const Indexmatrix& vec,Indexmatrix &ind, bool nondecreasing=true);
170 
172  std::ostream& operator<<(std::ostream& o,const Indexmatrix &A);
174  std::istream& operator>>(std::istream& i,Indexmatrix &A);
175 
177 
178 
182 
195 class Indexmatrix: protected Memarrayuser
196 {
197  friend class Matrix;
198  friend class Symmatrix;
199  friend class Sparsemat;
200  friend class Sparsesym;
201 
202 
203 private:
204  static const Mtype mtype;
207  nc;
209 
210  bool is_init;
211 
213  inline void init_to_zero();
214 
215 public:
216 
217  //----------------------------------------------------
218  //---- constructors, destructor, and initialization
219  //----------------------------------------------------
220 
224 
226  inline Indexmatrix();
227 
229  inline Indexmatrix(const Indexmatrix& A,Integer d=1);
230 
232  inline Indexmatrix(const Range&);
233 
240  inline Indexmatrix(Integer nr,Integer nc);
241 
243  inline Indexmatrix(Integer nr,Integer nc,Integer d);
244 
246  inline Indexmatrix(Integer nr,Integer nc,const Integer* dp,Integer incr=1);
247 
249  inline Indexmatrix(const std::vector<Integer>& vec);
250 
252  ~Indexmatrix(){memarray->free(m);}
253 
254 #if (CONICBUNDLE_DEBUG>=1)
255  void set_init(bool i){is_init=i;}
258  bool get_init() const {return is_init;}
259 #else
260  void set_init(bool /* i */){}
263  bool get_init() const {return true;}
264 #endif
265 
267  inline Indexmatrix& init(const Indexmatrix &A,Integer d=1);
268 
270  Indexmatrix& init(const Range&);
271 
273  inline Indexmatrix& init(Integer nr,Integer nc,Integer d);
274 
276  inline Indexmatrix& init(Integer nr,Integer nc,const Integer *dp,Integer incr=1);
277 
279  inline Indexmatrix& init(const std::vector<Integer>& vec);
280 
287  void newsize(Integer nr,Integer nc); //resize matrix without initialization
288 
290 
294 
296  Indexmatrix(const Matrix&);
298  Indexmatrix(const Symmatrix&);
300  Indexmatrix(const Sparsemat&);
302  Indexmatrix(const Sparsesym&);
303 
305 
306  //----------------------------------------------------
307  //---- size and type information
308  //----------------------------------------------------
309 
313 
315  void dim(Integer& _nr, Integer& _nc) const {_nr=nr; _nc=nc;}
316 
318  Integer dim() const {return nr*nc;}
319 
321  Integer rowdim() const {return nr;}
322 
324  Integer coldim() const {return nc;}
325 
327  Mtype get_mtype() const {return mtype;}
328 
330 
331  //--------------------------------
332  //---- Indexing and Submatrices
333  //--------------------------------
334 
335 
339 
341  inline Integer& operator()(Integer i,Integer j);
342 
344  inline Integer& operator()(Integer i); //index to vector of stacked columns
345 
347  inline Integer operator()(Integer i,Integer j) const;
348 
350  inline Integer operator()(Integer i) const; //index to vector of stacked columns
351 
353  Indexmatrix operator()(const Indexmatrix& vecrow,const Indexmatrix& veccol) const;
354 
356  Indexmatrix operator()(const Indexmatrix& A) const;
357  //*this matrix is interpreted as vector, the resulting matrix has
358  //has the same shape as A
359 
361  inline Integer& operator[](Integer i); //{return (*this)(i);}
362 
364  inline Integer operator[](Integer i) const; //{return (*this)(i);}
365 
367  Indexmatrix col(Integer i) const; //returns column i as column vector
369  Indexmatrix row(Integer i) const; //returns row i as row vector
371  Indexmatrix cols(const Indexmatrix &vec) const; //returns cols as indexed by vec
372 
374  Indexmatrix rows(const Indexmatrix &vec) const; //returns rows as indexed by vec
375 
376 
378  Indexmatrix& triu(Integer d=0); // (*this)(i,j)=0 for i<j+d
379 
381  Indexmatrix& tril(Integer d=0); // (*this)(i,j)=0 for i>j+d
382 
384  Indexmatrix& subassign(const Indexmatrix& vecrow,const Indexmatrix& veccol,
385  const Indexmatrix& A);
386  //(*this)(vecrow(i),veccol(j))=A(i,j) for all i,j
387 
389  Indexmatrix& subassign(const Indexmatrix& vec,const Indexmatrix& A);
390 
392  Indexmatrix& delete_rows(const Indexmatrix& ind,bool sorted_increasingly=false);
394  Indexmatrix& delete_cols(const Indexmatrix& ind,bool sorted_increasingly=false);
395 
397  Indexmatrix& insert_row(Integer i,const Indexmatrix& v); // 0<=i<=nr, v vector
399  Indexmatrix& insert_col(Integer i,const Indexmatrix& v); // 0<=i<=nc, v vector
400 
402  inline Indexmatrix& reduce_length(Integer n);
403 
409  Indexmatrix& concat_below(Integer d); //only for column vectors (nr==1)
411  Indexmatrix& concat_right(Integer d); //only for row vectors (nc==1)
413  Indexmatrix& enlarge_right(Integer addnc);
415  Indexmatrix& enlarge_below(Integer addnr);
417  Indexmatrix& enlarge_right(Integer addnc,Integer d);
419  Indexmatrix& enlarge_below(Integer addnr,Integer d);
421  Indexmatrix& enlarge_right(Integer addnc,const Integer* dp,Integer d=1);
423  Indexmatrix& enlarge_below(Integer addnr,const Integer *dp,Integer d=1);
424 
425 
427  Integer* get_store() {return m;} //use cautiously, do not use delete!
429  const Integer* get_store() const {return m;} //use cautiously
430 
432 
433 
437 
438 
440  friend Indexmatrix diag(const Indexmatrix& A); //=(A(1,1),A(2,2),...)^t
441 
443  friend Indexmatrix triu(const Indexmatrix& A,Integer d);
444 
446  friend Indexmatrix tril(const Indexmatrix& A,Integer d);
447 
449  friend inline Indexmatrix concat_right(const Indexmatrix& A,const Indexmatrix& B);
451  friend inline Indexmatrix concat_below(const Indexmatrix& A,const Indexmatrix& B);
452 
454  friend void swap(Indexmatrix &A,Indexmatrix &B);
455 
457 
458 
459  //------------------------------
460  //---- BLAS-like Routines
461  //------------------------------
462 
466 
468  Indexmatrix& xeya(const Indexmatrix& A,Integer d=1);
470  Indexmatrix& xpeya(const Indexmatrix& A,Integer d=1);
471 
473 
477 
479  friend Indexmatrix& xbpeya(Indexmatrix& x,const Indexmatrix& y,Integer alpha,Integer beta,int ytrans);
480 
482  friend Indexmatrix& xeyapzb(Indexmatrix& x,const Indexmatrix& y,const Indexmatrix& z,Integer alpha,Integer beta);
483 
485  friend Indexmatrix& genmult(const Indexmatrix& A,const Indexmatrix& B,Indexmatrix& C,
486  Integer alpha,Integer beta,int atrans,int btrans);
487 
489 
490 
491  //-----------------------------------
492  //---- usual arithmetic operators
493  //-----------------------------------
494 
498 
500  inline Indexmatrix& operator=(const Indexmatrix &A);
502  inline Indexmatrix& operator*=(const Indexmatrix &s);
504  inline Indexmatrix& operator+=(const Indexmatrix &v);
506  inline Indexmatrix& operator-=(const Indexmatrix &v);
508  inline Indexmatrix& operator%=(const Indexmatrix &A); //Hadamard product!!
510  inline Indexmatrix operator-() const;
511 
513  inline Indexmatrix& operator*=(Integer d);
515  inline Indexmatrix& operator/=(Integer d);
517  inline Indexmatrix& operator%=(Integer d);
519  inline Indexmatrix& operator+=(Integer d);
521  inline Indexmatrix& operator-=(Integer d);
522 
525 
527 
531 
533  friend inline Indexmatrix operator*(const Indexmatrix &A,const Indexmatrix& B);
535  friend inline Indexmatrix operator+(const Indexmatrix &A,const Indexmatrix& B);
537  friend inline Indexmatrix operator-(const Indexmatrix &A,const Indexmatrix& B);
539  friend inline Indexmatrix operator%(const Indexmatrix &A,const Indexmatrix& B);
541  friend inline Indexmatrix operator*(const Indexmatrix &A,Integer d);
543  friend inline Indexmatrix operator*(Integer d,const Indexmatrix &A);
545  friend inline Indexmatrix operator/(const Indexmatrix& A,Integer d);
547  friend inline Indexmatrix operator%(const Indexmatrix& A,Integer d);
548 
550  friend inline Indexmatrix operator+(const Indexmatrix& A,Integer d);
552  friend inline Indexmatrix operator+(Integer d,const Indexmatrix& A);
554  friend inline Indexmatrix operator-(const Indexmatrix& A,Integer d);
556  friend inline Indexmatrix operator-(Integer d,const Indexmatrix& A);
557 
559  friend Indexmatrix transpose(const Indexmatrix& A);
560 
562 
563  //------------------------------------------
564  //---- Connections to other Matrix Classes
565  //------------------------------------------
566 
570 
572  friend inline std::vector<int>& assign(std::vector<int>& vec,
573  const Indexmatrix& A);
575  friend inline std::vector<long>& assign(std::vector<long>& vec,
576  const Indexmatrix& A);
577 
579 
580 
581  //------------------------------
582  //---- Elementwise Operations
583  //------------------------------
584 
588 
590  Indexmatrix& rand(Integer nr,Integer nc,Integer lowerb,Integer upperb,CH_Tools::GB_rand* random_generator=0);
592  Indexmatrix& shuffle(CH_Tools::GB_rand* random_generator=0);
594  Indexmatrix& sign(void);
596  Indexmatrix& abs(void);
597 
599 
603 
605  friend Indexmatrix rand(Integer nr,Integer nc,Integer lb,Integer ub,CH_Tools::GB_rand* random_generator);
607  friend inline Indexmatrix sign(const Indexmatrix& A);
609  friend Indexmatrix abs(const Indexmatrix& A);
610 
612 
613  //----------------------------
614  //---- Numerical Methods
615  //----------------------------
616 
620 
622  Indexmatrix& scale_rows(const Indexmatrix& vec);
624  Indexmatrix& scale_cols(const Indexmatrix& vec);
625 
627 
631 
633  friend Integer trace(const Indexmatrix& A); //=sum(diag(A))
635  friend inline Integer ip(const Indexmatrix& A, const Indexmatrix& B); //=trace(B^t*A)
637  friend inline Real norm2(const Indexmatrix& A);
638 
639 
641  friend Indexmatrix sumrows(const Indexmatrix& A);
643  friend Indexmatrix sumcols(const Indexmatrix& A);
645  friend Integer sum(const Indexmatrix& A);
646 
648 
649 
650  //---------------------------------------------
651  //---- Comparisons / Max / Min / sort / find
652  //---------------------------------------------
653 
657 
659  Indexmatrix find() const; //finds nonzeros
661  Indexmatrix find_number(Integer num=0) const;
662 
664 
668 
670  friend Indexmatrix operator<(const Indexmatrix &A,const Indexmatrix &B);
672  friend inline Indexmatrix operator>(const Indexmatrix &A,const Indexmatrix &B);
674  friend Indexmatrix operator<=(const Indexmatrix &A,const Indexmatrix &B);
676  friend inline Indexmatrix operator>=(const Indexmatrix &A,const Indexmatrix &B);
678  friend Indexmatrix operator==(const Indexmatrix &A,const Indexmatrix &B);
680  friend Indexmatrix operator!=(const Indexmatrix &A,const Indexmatrix &B);
682  friend Indexmatrix operator<(const Indexmatrix &A,Integer d);
684  friend Indexmatrix operator>(const Indexmatrix &A,Integer d);
686  friend Indexmatrix operator<=(const Indexmatrix &A,Integer d);
688  friend Indexmatrix operator>=(const Indexmatrix &A,Integer d);
690  friend Indexmatrix operator==(const Indexmatrix &A,Integer d);
692  friend Indexmatrix operator!=(const Indexmatrix &A,Integer d);
694  friend inline Indexmatrix operator<(Integer d,const Indexmatrix &A);
696  friend inline Indexmatrix operator>(Integer d,const Indexmatrix &A);
698  friend inline Indexmatrix operator<=(Integer d,const Indexmatrix &A);
700  friend inline Indexmatrix operator>=(Integer d,const Indexmatrix &A);
702  friend inline Indexmatrix operator==(Integer d,const Indexmatrix &A);
704  friend inline Indexmatrix operator!=(Integer d,const Indexmatrix &A);
705 
707  friend inline bool equal(const Indexmatrix& A,const Indexmatrix& b);
708 
710  friend Indexmatrix minrows(const Indexmatrix& A);
712  friend Indexmatrix mincols(const Indexmatrix& A);
714  friend Integer min(const Indexmatrix& A,Integer *iindex,Integer *jindex);
716  friend Indexmatrix maxrows(const Indexmatrix& A);
718  friend Indexmatrix maxcols(const Indexmatrix& A);
720  friend Integer max(const Indexmatrix& A,Integer *iindex,Integer *jindex);
721 
723  friend inline Indexmatrix sortindex(const Indexmatrix& vec,bool nondecreasing);
725  friend void sortindex(const Indexmatrix& vec,Indexmatrix &ind, bool nondecreasing);
726 
728  friend inline Indexmatrix find(const Indexmatrix& A);
730  friend Indexmatrix find_number(const Indexmatrix& A,Integer num);
731 
733 
734  //--------------------------------
735  //---- Input / Output
736  //--------------------------------
737 
741 
744  void display(std::ostream& out,
745  int precision=0,
746  int width=0,
747  int screenwidth=0
748  ) const;
749 
752  void mfile_output(std::ostream& out,
753  int precision=16,
754  int width=0
755  ) const;
756 
757 
759 
763 
765  friend std::ostream& operator<<(std::ostream& o,const Indexmatrix &A);
767  friend std::istream& operator>>(std::istream& i,Indexmatrix &A);
768 
770 
771 };
772 
774 
775 
776 
777 //@cond
778 
779 // **************************************************************************
780 // implementation of inline functions
781 // **************************************************************************
782 
783 inline void Indexmatrix::init_to_zero()
784 {
785  nr=nc=0;mem_dim=0;m=0;
786  chk_set_init(*this,1);
787 }
788 
790 {
791  newsize(inr,inc);
792  mat_xea(nr*nc,m,d);
793  chk_set_init(*this,1);
794  return *this;
795 }
796 
797 inline Indexmatrix& Indexmatrix::init(Integer inr,Integer inc,const Integer* d,Integer incr)
798 {
799  newsize(inr,inc);
800  if (incr==1) mat_xey(nr*nc,m,d);
801  else mat_xey(nr*nc,m,Integer(1),d,incr);
802  chk_set_init(*this,1);
803  return *this;
804 }
805 
807 {
808  return xeya(A,d);
809 }
810 
811 inline Indexmatrix& Indexmatrix::init(const std::vector<Integer>& vec)
812 {
813  newsize(Integer(vec.size()),1); chk_set_init(*this,1);
814  for(Integer i=0;i<nr;i++) m[i]=vec[(unsigned long)(i)];
815  return *this;
816 }
817 
819 {
820  init_to_zero();
821 }
822 
824 {
825  init_to_zero();
826  xeya(A,d);
827 }
828 
830 {
831  init_to_zero();
832  newsize(inr,inc);
833 }
834 
835 inline Indexmatrix::Indexmatrix(const Range& range)
836 {
837  init_to_zero();
838  init(range);
839 }
840 
842 {
843  init_to_zero();
844  init(inr,inc,d);
845 }
846 
847 inline Indexmatrix::Indexmatrix(Integer inr,Integer inc,const Integer *d,Integer incr)
848 {
849  init_to_zero();
850  init(inr,inc,d,incr);
851 }
852 
853 inline Indexmatrix::Indexmatrix(const std::vector<Integer>& vec)
854 {
855  init_to_zero();
856  init(vec);
857 }
858 
860 {
861  chk_range(i,j,nr,nc);
862  return m[j*nr+i];
863 }
864 
866 {
867  chk_range(i,0,nr*nc,1);
868  return m[i];
869 }
870 
872 {
873  chk_range(i,j,nr,nc);
874  return m[j*nr+i];
875 }
876 
878 {
879  chk_range(i,0,nr*nc,1);
880  return m[i];
881 }
882 
884 {return (*this)(i);}
885 
886 inline Integer Indexmatrix::operator[](Integer i) const
887 {return (*this)(i);}
888 
890 { nr=min(nr*nc,max(Integer(0),n)); nc=1; return *this;}
891 
892 
893 inline Integer ip(const Indexmatrix& A, const Indexmatrix& B)
894 {
895  chk_add(A,B);
896  return mat_ip(A.nc*A.nr,A.m,B.m);
897 }
898 
899 inline Real norm2(const Indexmatrix& A)
900 {
901  chk_init(A);
902  return ::sqrt(double(mat_ip(A.nc*A.nr,A.m,A.m)));
903 }
904 
905 inline Indexmatrix& Indexmatrix::operator=(const Indexmatrix &A)
906 { return xeya(A);}
907 
908 inline Indexmatrix& Indexmatrix::operator*=(const Indexmatrix &A)
909 { Indexmatrix C; return xeya(genmult(*this,A,C));}
910 
911 inline Indexmatrix& Indexmatrix::operator+=(const Indexmatrix &A)
912 { return xpeya(A); }
913 
914 inline Indexmatrix& Indexmatrix::operator-=(const Indexmatrix &A)
915 { return xpeya(A,-1); }
916 
918 { chk_add(*this,A); mat_xhadey(nr*nc,m,A.m); return *this; }
919 
920 inline Indexmatrix Indexmatrix::operator-() const
921 { return Indexmatrix(*this,-1); }
922 
923 inline Indexmatrix& Indexmatrix::operator*=(Integer d)
924 { chk_init(*this); mat_xmultea(nr*nc,m,d); return *this; }
925 
927 { chk_init(*this); mat_xdivea(nr*nc,m,d); return *this; }
928 
930 { chk_init(*this); mat_xmodea(nr*nc,m,d); return *this; }
931 
932 inline Indexmatrix& Indexmatrix::operator+=(Integer d)
933 { chk_init(*this); mat_xpea(nr*nc,m,d); return *this; }
934 
935 inline Indexmatrix& Indexmatrix::operator-=(Integer d)
936 { chk_init(*this); mat_xpea(nr*nc,m,-d); return *this; }
937 
938 
939 inline Indexmatrix operator*(const Indexmatrix &A,const Indexmatrix &B)
940  {Indexmatrix C; return genmult(A,B,C);}
941 inline Indexmatrix operator+(const Indexmatrix &A,const Indexmatrix &B)
942  {Indexmatrix C; return xeyapzb(C,A,B,1,1);}
943 inline Indexmatrix operator-(const Indexmatrix &A,const Indexmatrix &B)
944  {Indexmatrix C; return xeyapzb(C,A,B,1,-1);}
945 inline Indexmatrix operator%(const Indexmatrix &A,const Indexmatrix &B)
946  {Indexmatrix C(A); return C%=B;}
947 inline Indexmatrix operator*(const Indexmatrix &A,Integer d)
948  {return Indexmatrix(A,d);}
949 inline Indexmatrix operator*(Integer d,const Indexmatrix &A)
950  {return Indexmatrix(A,d);}
951 inline Indexmatrix operator/(const Indexmatrix &A,Integer d)
952  {Indexmatrix B(A); return B/=d;}
953 inline Indexmatrix operator%(const Indexmatrix &A,Integer d)
954  {Indexmatrix B(A); return B%=d;}
955 inline Indexmatrix operator+(const Indexmatrix &A,Integer d)
956  {Indexmatrix B(A); return B+=d;}
957 inline Indexmatrix operator+(Integer d,const Indexmatrix &A)
958  {Indexmatrix B(A); return B+=d;}
959 inline Indexmatrix operator-(const Indexmatrix &A,Integer d)
960  {Indexmatrix B(A); return B-=d;}
961 inline Indexmatrix operator-(Integer d,const Indexmatrix &A)
962  {Indexmatrix B(A,-1); return B+=d;}
963 
964 inline Indexmatrix triu(const Indexmatrix& A,Integer i=0)
965  {Indexmatrix B(A); B.triu(i); return B;}
966 inline Indexmatrix tril(const Indexmatrix& A,Integer i=0)
967  {Indexmatrix B(A); B.tril(i); return B;}
968 
969 inline Indexmatrix concat_right(const Indexmatrix& A,const Indexmatrix& B)
970  {Indexmatrix C(A.dim()+B.dim(),1);C=A;C.concat_right(B);return C;}
971 inline Indexmatrix concat_below(const Indexmatrix& A,const Indexmatrix& B)
972  {Indexmatrix C(A.dim()+B.dim(),1);C=A;C.concat_below(B);return C;}
973 
974 inline void swap(Indexmatrix &A,Indexmatrix &B)
975 {
976  Integer *hm=A.m;A.m=B.m;B.m=hm;
977  Integer hi=A.nr;A.nr=B.nr;B.nr=hi;
978  hi=A.nc;A.nc=B.nc;B.nc=hi;
979  hi=A.mem_dim;A.mem_dim=B.mem_dim;B.mem_dim=hi;
980 #if (CONICBUNDLE_DEBUG>=1)
981  bool hb=A.is_init;A.is_init=B.is_init;B.is_init=hb;
982 #endif
983 }
984 
985 inline Indexmatrix rand(Integer rows,Integer cols,Integer lb,Integer ub,CH_Tools::GB_rand* random_generator=0)
986 {Indexmatrix A; return A.rand(rows,cols,lb,ub,random_generator);}
987 inline Indexmatrix sign(const Indexmatrix& A)
988  {Indexmatrix B(A); return B.sign();}
989 
990 inline Indexmatrix operator>(const Indexmatrix &A,const Indexmatrix &B)
991 {return B<A;}
992 inline Indexmatrix operator>=(const Indexmatrix &A,const Indexmatrix &B)
993 {return B<=A;}
994 inline Indexmatrix operator<(Integer d,const Indexmatrix &A)
995 {return A>d;}
996 inline Indexmatrix operator>(Integer d,const Indexmatrix &A)
997 {return A<d;}
998 inline Indexmatrix operator<=(Integer d,const Indexmatrix &A)
999 {return A>=d;}
1000 inline Indexmatrix operator>=(Integer d,const Indexmatrix &A)
1001 {return A<=d;}
1002 inline Indexmatrix operator==(Integer d,const Indexmatrix &A)
1003 {return A==d;}
1004 inline Indexmatrix operator!=(Integer d,const Indexmatrix &A)
1005 {return A!=d;}
1006 
1007 inline bool equal(const Indexmatrix& A,const Indexmatrix& B)
1008 {return ((A.nc==B.nc)&&(A.nr==B.nr)&&(mat_equal(A.nr*A.nc,A.get_store(),B.get_store())));}
1009 
1010 inline Indexmatrix sortindex(const Indexmatrix& vec,bool nondecreasing)
1011 {Indexmatrix ind;sortindex(vec,ind,nondecreasing);return ind;}
1012 
1013 inline Indexmatrix find(const Indexmatrix& A)
1014  {return A.find();}
1015 inline Indexmatrix find_number(const Indexmatrix& A,Integer num=0)
1016  {return A.find_number(num);}
1017 
1018 inline std::vector<int>& assign(std::vector<int>& vec,const Indexmatrix& A)
1019 {
1020  chk_init(A);
1021  vec.resize((unsigned long)(A.dim()));
1022  for(Integer i=0;i<A.dim();i++) vec[(unsigned long)(i)]=int(A(i));
1023  return vec;
1024 }
1025 
1026 inline std::vector<long>& assign(std::vector<long>& vec,const Indexmatrix& A)
1027 {
1028  chk_init(A);
1029  vec.resize((unsigned long)(A.dim()));
1030  for(Integer i=0;i<A.dim();i++) vec[(unsigned long)(i)]=long(A(i));
1031  return vec;
1032 }
1033 
1034  //@endcond
1035 
1036 }
1037 
1038 
1039 #ifndef CH_MATRIX_CLASSES__MATRIX_HXX
1040 #include "matrix.hxx"
1041 #endif
1042 
1043 
1044 #endif
Integer coldim() const
returns the column dimension
Definition: indexmat.hxx:324
#define chk_set_init(x, y)
CONICBUNDLE_DEBUG being undefined, the template function is removed. Otherwise it would allow to set ...
Definition: matop.hxx:1774
int Integer
all integer numbers in calculations and indexing are of this type
Definition: matop.hxx:40
bool get_init() const
returns true if the matrix has been declared initialized (not needed if CONICBUNDLE_DEBUG is undefine...
Definition: indexmat.hxx:263
Indexmatrix mincols(const Indexmatrix &A)
returns a column vector holding in each row the minimum over all columns in this row ...
Header declaring the classes CH_Matrix_Classes::Realrange and CH_Matrix_Classes::Matrix having Real e...
void mat_xmultea(Integer len, Val *x, const Val a)
Set x[i]*=a for len elements of the array x.
Definition: matop.hxx:734
Matrix operator-(const Matrix &A, const Matrix &B)
returns Matrix equal to A-B
Definition: matrix.hxx:1283
Integer nr
number of rows
Definition: indexmat.hxx:206
Indexmatrix find() const
returns an Indexmatrix ind so that (*this)(ind(i)) 0<=i<ind.dim() runs through all nonzero elements ...
Indexmatrix maxrows(const Indexmatrix &A)
returns a row vector holding in each column the maximum over all rows in this column ...
double Real
all real numbers in calculations are of this type
Definition: matop.hxx:50
Matrix tril(const Matrix &A, Integer i=0)
retuns a matrix that keeps the lower triangle of A starting with diagonal d, i.e., (i,j)=A(i,j) for 0<=i<row dimension, 0<=j<min(i+d+1,column dimension), and sets (i,j)=0 otherwise
Definition: matrix.hxx:1342
void swap(double &a, double &b)
swaps two double variables
Definition: mymath.hxx:79
Indexmatrix maxcols(const Indexmatrix &A)
returns a column vector holding in each row the maximum over all columns in this row ...
void mat_xea(Integer len, Val *x, const Val a)
Set x[i]=a for len elements of the array x.
Definition: matop.hxx:77
Indexmatrix sortindex(const Indexmatrix &vec, bool nondecreasing=true)
returns an Indexmatrix ind so that vec(ind(0))<=vec(ind(1))<=...<=vec(ind(vec.dim()-1)) (vec may be r...
Matrix class for integral values of type Integer
Definition: indexmat.hxx:195
Mtype
serves for specifying the source (matrix class or function) of the error
Definition: matop.hxx:1585
Header defining simple functions like max, min, abs.
Indexmatrix & sign(void)
using sign assign (*this)(i,j)=sign((*this)(i,j)) for all i,j
Real norm2(const Matrix &A)
returns the Frobenius norm of A, i.e., the square root of the sum of A(i,j)*A(i,j) over all i...
Definition: matrix.hxx:1235
Matrix concat_below(const Matrix &A, const Matrix &B)
returns a bew matrix [A; B], i.e., it concats matrices A and B columnwise; A or B may be a 0x0 matrix...
Definition: matrix.hxx:1350
Matrix triu(const Matrix &A, Integer i=0)
retuns a matrix that keeps the upper triangle of A starting with diagonal d, i.e., (i,j)=A(i,j) for 0<=i<row dimension, max(0,i+d)<=j<column dimension, and sets (i,j)=0 otherwise
Definition: matrix.hxx:1338
Matrix operator*(const Matrix &A, const Matrix &B)
returns Matrix equal to A*B
Definition: matrix.hxx:1275
Indexmatrix & tril(Integer d=0)
keeps lower triangle starting with diagonal d; set (*this)(i,j)=0 for 0<=i<row dimension, max(0,i+d)<=j<column dimension, returns *this
Indexmatrix & concat_below(const Indexmatrix &A)
concats matrix A to the bottom of *this, A or *this may be the 0x0 matrix initally, returns *this
Integer from
value of starting element
Definition: indexmat.hxx:51
Indexmatrix operator<=(const Indexmatrix &A, const Indexmatrix &B)
returns a matrix having elements (i,j)=Integer(A(i,j)<=B(i,j)) for all i,j
void mat_xhadey(Integer len, Val *x, const Val *y)
Set x[i]*=y[i] for len elements of the arrays x and y.
Definition: matop.hxx:470
Indexmatrix & rand(Integer nr, Integer nc, Integer lowerb, Integer upperb, CH_Tools::GB_rand *random_generator=0)
resize *this to an nr x nc matrix and assign to (i,j) a random number uniformly from [lowerb...
Indexmatrix & reduce_length(Integer n)
(*this) is set to a column vector of length min{max{0,n},dim()}; usually used to truncate a vector...
Matrix class of symmetric matrices with real values of type Real
Definition: symmat.hxx:43
Indexmatrix find_number(const Matrix &A, Real num=0., Real tol=1e-10)
returns an Indexmatrix ind so that A(ind(i)) 0<=i<ind.dim() runs through all elements of A having val...
Definition: matrix.hxx:1444
void init_to_zero()
initialize the matrix to a 0x0 matrix without storage
Indexmatrix sumcols(const Indexmatrix &A)
returns a column vector holding the sum over all columns, i.e., A*(1 1 ... 1)^T
Indexmatrix operator==(const Indexmatrix &A, const Indexmatrix &B)
returns a matrix having elements (i,j)=Integer(A(i,j)==B(i,j)) for all i,j
device independent random number generator based on long int with seed
Definition: gb_rand.hxx:28
Indexmatrix & operator%=(const Indexmatrix &A)
ATTENTION: this is redefined as the Hadamard product, (*this)(i,j)=(*this)(i,j)*A(i,j) for all i,j.
Range(Integer _from, Integer _to, Integer _step=1)
constructor for {i: i=from+k*step for some k in N_0 and i<=to}
Definition: indexmat.hxx:58
Indexmatrix & operator/=(Integer d)
ATTENTION: d is NOT checked for 0.
Integer nc
number of columns
Definition: indexmat.hxx:206
Indexmatrix & concat_right(const Indexmatrix &A)
concats matrix A to the right of *this, A or *this may be the 0x0 matrix initally, returns *this
Integer & operator[](Integer i)
returns reference to element (i) of the matrix if regarded as vector of stacked columns [element (iro...
Integer trace(const Indexmatrix &A)
returns the sum of the diagonal elements A(i,i) over all i
bool mat_equal(Integer len, const Val *x, const Val *y)
returns true if the elements of the arrays x and y are exactly equal.
Definition: matop.hxx:1415
Integer step
step distance between successive elements starting with from
Definition: indexmat.hxx:55
Integer & operator()(Integer i, Integer j)
returns reference to element (i,j) of the matrix (rowindex i, columnindex j)
Indexmatrix operator<(const Indexmatrix &A, const Indexmatrix &B)
returns a matrix having elements (i,j)=Integer(A(i,j)<B(i,j)) for all i,j
int sign(int a)
return the signum of an int a (1 for a>0,-1 for a<0,0 for a==0)
Definition: mymath.hxx:133
Indexmatrix diag(const Indexmatrix &A)
returns a column vector v consisting of the elements v(i)=A(i,i), 0<=i<min(row dimension,column dimension)
Matrix operator/(const Matrix &A, const Matrix &B)
ATTENTION: this is redefined to act componentwise without checking for zeros, C(i,j)=A(i,j)/B(i,j) for all i,j.
Definition: matrix.hxx:1291
Matrix Classes and Linear Algebra. See Matrix Classes (namespace CH_Matrix_Classes) for a quick intro...
Definition: PSCOracle.hxx:20
Integer mem_dim
amount of memory currently allocated
Definition: indexmat.hxx:205
Matrix class of symmetric matrices with real values of type Real
Definition: sparssym.hxx:89
std::ostream & operator<<(std::ostream &o, const Indexmatrix &A)
output format (all Integer values): nr nc \n A(1,1) A(1,2) ... A(1,nc) \n A(2,1) ... A(nr,nc) \n
Indexmatrix & init(const Indexmatrix &A, Integer d=1)
initialize to *this=A*d
#define chk_add(x, y)
CONICBUNDLE_DEBUG being undefined, the template function is removed. Otherwise it would check...
Definition: matop.hxx:1763
void mat_xey(Integer len, Val *x, const Val *y)
Copy an array of length len to destination x from source y.
Definition: matop.hxx:117
Indexmatrix operator>=(const Indexmatrix &A, Integer d)
returns a matrix having elements (i,j)=Integer(A(i,j)>=d) for all i,j
Integer to
upper bound on element values
Definition: indexmat.hxx:53
void mat_xmodea(Integer len, Val *x, const Val a)
Set x[i]%=a for len elements of the array x.
Definition: matop.hxx:836
Matrix class for real values of type Real
Definition: matrix.hxx:74
Indexmatrix transpose(const Indexmatrix &A)
returns an Indexmatrix that is the transpose of A
Indexmatrix & triu(Integer d=0)
keeps upper triangle starting with diagonal d; set (*this)(i,j)=0 for 0<=i<row dimension, 0<=j<min(i+d,column dimension), returns *this
Val mat_ip(Integer len, const Val *x, const Val *y, const Val *d=0)
return sum(x[i]*y[i]) summing over len elements of the arrays x and y.
Definition: matop.hxx:1096
~Range()
destructor, nothing to do
Definition: indexmat.hxx:60
Matrix class of sparse matrices with real values of type Real
Definition: sparsmat.hxx:74
Integer dim() const
returns the dimension rows * columns when the matrix is regarded as a vector
Definition: indexmat.hxx:318
const Integer * get_store() const
returns the current address of the internal value array; use cautiously!
Definition: indexmat.hxx:429
allows to specify a range of integral values via (from, to, step) meaning {j=from+i*step:j in[from...
Definition: indexmat.hxx:47
Indexmatrix operator>(const Indexmatrix &A, Integer d)
returns a matrix having elements (i,j)=Integer(A(i,j)>d) for all i,j
std::vector< double > & assign(std::vector< double > &vec, const Matrix &A)
interpret A as a vector and copy it to a std::vector<double> which is also returned ...
Definition: matrix.hxx:1447
double max(double a, double b)
maximum value of two double variables
Definition: mymath.hxx:43
Indexmatrix sumrows(const Indexmatrix &A)
returns a row vector holding the sum over all rows, i.e., (1 1 ... 1)*A
static const Mtype mtype
used for MatrixError templates (runtime type information was not yet existing)
Definition: indexmat.hxx:204
All derived classes share a common Memarray memory manager, which is generated with the first user an...
Definition: memarray.hxx:117
Integer rowdim() const
returns the row dimension
Definition: indexmat.hxx:321
#define chk_range(i, j, ubi, ubj)
CONICBUNDLE_DEBUG being undefined, the template function is removed. Otherwise it would check...
Definition: matop.hxx:1772
Indexmatrix minrows(const Indexmatrix &A)
returns a row vector holding in each column the minimum over all rows in this column ...
Integer * get_store()
returns the current address of the internal value array; use cautiously, do not use delete! ...
Definition: indexmat.hxx:427
double min(double a, double b)
minimum value of two double variables
Definition: mymath.hxx:49
Indexmatrix & genmult(const Indexmatrix &A, const Indexmatrix &B, Indexmatrix &C, Integer alpha=1, Integer beta=0, int atrans=0, int btrans=0)
returns C=beta*C+alpha*A*B, where A and B may be transposed; C must not be equal to A and B; if beta=...
bool is_init
flag whether memory is initialized, it is only used if CONICBUNDLE_DEBUG is defined ...
Definition: indexmat.hxx:210
Matrix concat_right(const Matrix &A, const Matrix &B)
returns a new matrix [A, B], i.e., it concats matrices A and B rowwise; A or B may be a 0x0 matrix ...
Definition: matrix.hxx:1346
double abs(double d)
absolute value of a double
Definition: mymath.hxx:25
Mtype get_mtype() const
returns the type of the matrix, MTindexmatrix
Definition: indexmat.hxx:327
Header for simple memory management tools that support frequent allocation and deallocation of arrays...
Integer * m
pointer to store, order is columnwise (a11,a21,...,anr1,a12,a22,.....)
Definition: indexmat.hxx:208
Indexmatrix & xbpeya(Indexmatrix &x, const Indexmatrix &y, Integer alpha=1, Integer beta=0, int ytrans=0)
returns x= alpha*y+beta*x, where y may be transposed (ytrans=1); if beta==0. then x is initialized to...
Integer sum(const Indexmatrix &A)
returns the sum over all elements of A, i.e., (1 1 ... 1)*A*(1 1 ... 1)^T
Matrix operator+(const Matrix &A, const Matrix &B)
returns Matrix equal to A+B
Definition: matrix.hxx:1279
#define chk_init(x)
CONICBUNDLE_DEBUG being undefined, the template function is removed. Otherwise it would check...
Definition: matop.hxx:1761
Matrix rand(Integer rows, Integer cols, CH_Tools::GB_rand *random_generator=0)
return a nr x nc matrix with (i,j) assigned a random number uniformly from [0,1] for all i...
Definition: matrix.hxx:1366
Matrix operator%(const Matrix &A, const Matrix &B)
ATTENTION: this is redefined as the Hadamard product, C(i,j)=A(i,j)*B(i,j) for all i...
Definition: matrix.hxx:1287
void mat_xpea(Integer len, Val *x, const Val a)
Set x[i]+=a for len elements of the array x.
Definition: matop.hxx:690
void dim(Integer &_nr, Integer &_nc) const
returns the number of rows in _nr and the number of columns in _nc
Definition: indexmat.hxx:315
bool equal(const Matrix &A, const Matrix &B)
returns true if both matrices have the same size and the same elements
Definition: matrix.hxx:1433
Indexmatrix & xeyapzb(Indexmatrix &x, const Indexmatrix &y, const Indexmatrix &z, Integer alpha=1, Integer beta=1)
returns x= alpha*y+beta*z; x is initialized to the correct size; alpha and beta have default value 1 ...
double sqrt(int a)
return sqrt for int a
Definition: mymath.hxx:121
Real ip(const Matrix &A, const Matrix &B)
returns the usual inner product of A and B, i.e., the sum of A(i,j)*B(i,j) over all i...
Definition: matrix.hxx:1165
void mat_xdivea(Integer len, Val *x, const Val a)
Set x[i]/=a for len elements of the array x.
Definition: matop.hxx:791
Indexmatrix operator!=(const Indexmatrix &A, const Indexmatrix &B)
returns a matrix having elements (i,j)=Integer(A(i,j)!=B(i,j)) for all i,j
Indexmatrix find_number(Integer num=0) const
returns an Indexmatrix ind so that (*this)(ind(i)) 0<=i<ind.dim() runs through all elements having va...
Indexmatrix find(const Matrix &A, Real tol=1e-10)
returns an Indexmatrix ind so that A(ind(i)) 0<=i<ind.dim() runs through all nonzero elements with ab...
Definition: matrix.hxx:1441
std::istream & operator>>(std::istream &i, Indexmatrix &A)
input format (all Integer values): nr nc \n A(1,1) A(1,2) ... A(1,nc) \n A(2,1) ... A(nr,nc) \n