ConicBundle
matrix.hxx
Go to the documentation of this file.
1 
2 #ifndef CH_MATRIX_CLASSES__MATRIX_HXX
3 #define CH_MATRIX_CLASSES__MATRIX_HXX
4 
13 #include <math.h>
14 #ifndef CH_MATRIX_CLASSES__INDEXMAT_HXX
15 #include "indexmat.hxx"
16 #endif
17 
18 
19 namespace CH_Matrix_Classes {
20 
21 //everything involving a "Sparsesym" is implemented in "sparssym.cxx/hxx"
22 //everything else involving a "Sparsemat" is implemented in "sparsmat.cxx/hxx"
23 //everything else involving a "Symmatrix" is implemented in "symmat.cxx/hxx"
24 
25 // **************************************************************************
26 // Range
27 // **************************************************************************
28 
32 
34 class Realrange
35 {
36 public:
46  Realrange(Real _from,Real _to,Real _step=1,Real _tol=1e-8):
47  from(_from),to(_to),step(_step),tol(_tol){}
50 };
51 
53 
54 // **************************************************************************
55 // Matrix
56 // **************************************************************************
57 
61 
74 class Matrix: protected Memarrayuser
75 {
76  friend class Indexmatrix;
77  friend class Symmatrix;
78  friend class Sparsemat;
79  friend class Sparsesym;
80 
81 private:
82 
83  static const Mtype mtype;
86  nc;
87  Real *m;
88 
89  //--- auxilliary storage space for repeated use of information, e.g., in qr
91  enum AuxTask {
92  none,
93  qr
94  };
98 
99  bool is_init;
100 
102  inline void init_to_zero();
103 
104 public:
105 
106  //----------------------------------------------------
107  //---- constructors, destructor, and initialization
108  //----------------------------------------------------
109 
110 
114 
116  inline Matrix();
118  inline Matrix(const Matrix&,Real d=1.,int atrans=0);
120  inline Matrix(const Realrange&);
127  inline Matrix(Integer nr,Integer nc);
129  inline Matrix(Integer nr,Integer nc,Real d);
131  inline Matrix(Integer nr,Integer nc,const Real* dp,Integer incr=1,Real d=1.);
133  inline Matrix(const std::vector<Real>& vec);
135  ~Matrix(){memarray->free(m);if (aux_m) memarray->free(aux_m);}
136 
137 #if (CONICBUNDLE_DEBUG>=1)
138  void set_init(bool i){is_init=i;}
141  bool get_init() const {return is_init;}
142 #else
143  void set_init(bool /* i */){}
146  bool get_init() const {return true;}
147 #endif
148 
150  inline Matrix& init(const Matrix &A,Real d=1.,int atrans=0);
152  inline Matrix& init(const Indexmatrix& A,Real d=1.);
154  inline Matrix& init(const Sparsemat& A, Real d=1.); //*this=A*d;
156  inline Matrix& init(const Symmatrix& S,Real d=1.);
158  inline Matrix& init(const Sparsesym&,Real d=1.);
160  Matrix& init(const Realrange&);
162  inline Matrix& init(Integer nr,Integer nc,Real d);
164  inline Matrix& init(Integer nr,Integer nc,const Real *dp,Integer incr=1,Real d=1.);
166  inline Matrix& init(const std::vector<Real>& vec);
168  inline Matrix& init_diag(int nr,Real d=1.);
170  inline Matrix& init_diag(const Matrix& vec,Real d=1.);
172  inline Matrix& init_diag(const Indexmatrix& vec,Real d=1.);
173 
180  void newsize(Integer nr,Integer nc); //resize matrix without initialization
181 
182 
184 
188 
190  inline Matrix(const Indexmatrix& A,Real d=1.);
192  inline Matrix(const Sparsemat& A, Real d=1.);
194  inline Matrix(const Symmatrix& S,Real d=1.);
196  inline Matrix(const Sparsesym&,Real d=1.);
197 
199 
200  //----------------------------------------------------
201  //---- size and type information
202  //----------------------------------------------------
203 
207 
209  void dim(Integer& _nr, Integer& _nc) const {_nr=nr; _nc=nc;}
210 
212  Integer dim() const {return nr*nc;}
213 
215  Integer rowdim() const {return nr;}
216 
218  Integer coldim() const {return nc;}
219 
221  Mtype get_mtype() const {return mtype;}
222 
224 
225 
226  //--------------------------------
227  //---- Indexing and Submatrices
228  //--------------------------------
229 
230 
234 
236  inline Real& operator()(Integer i,Integer j);
237 
239  inline Real& operator()(Integer i); //index to vector of stacked columns
240 
242  inline Real operator()(Integer i,Integer j) const;
243 
245  inline Real operator()(Integer i) const; //index to vector of stacked columns
246 
248  Matrix operator()(const Indexmatrix& vecrow,const Indexmatrix& veccol) const;
249 
251  Matrix operator()(const Indexmatrix& A) const;
252 
253 
255  inline Real& operator[](Integer i); //{return (*this)(i);}
257  inline Real operator[](Integer i) const; //{return (*this)(i);}
258 
260  Matrix col(Integer i) const; //returns column i as column vector
262  Matrix row(Integer i) const; //returns row i as row vector
264  Matrix cols(const Indexmatrix &vec) const; //returns cols as indexed by vec
266  Matrix rows(const Indexmatrix &vec) const; //returns rows as indexed by vec
268  Matrix& swap_rowsij(Integer i,Integer j);
270  Matrix& swap_colsij(Integer i,Integer j);
272  Matrix& pivot_permute_rows(const Indexmatrix & piv,bool inverse=false);
274  Matrix& pivot_permute_cols(const Indexmatrix & piv,bool inverse=false);
275 
277  Matrix& triu(Integer d=0); // (*this)(i,j)=0 for j<i+d
279  Matrix& tril(Integer d=0); // (*this)(i,j)=0 for j>i+d
280 
282  Matrix& subassign(const Indexmatrix& vecrow,const Indexmatrix& veccol,
283  const Matrix& A);
284  //(*this)(vecrow(i),veccol(j))=A(i,j) for all i,j
286  Matrix& subassign(const Indexmatrix& vec,const Matrix& A);
287  //(*this)(vec(i))=A(i);
288  //*this, vec, and A may be rect matrices but will be used as vectors
289 
291  Matrix& delete_rows(const Indexmatrix& ind,bool sorted_increasingly=false);
293  Matrix& delete_cols(const Indexmatrix& ind,bool sorted_increasingly=false);
294 
296  Matrix& insert_row(Integer i,const Matrix& v); // 0<=i<=nr, v vector
298  Matrix& insert_col(Integer i,const Matrix& v); // 0<=i<=nc, v vector
299 
301  inline Matrix& reduce_length(Integer n);
302  //interpret *this as a column vector and cut at n
303 
305  Matrix& concat_right(const Matrix& A,int Atrans=0);
307  Matrix& concat_below(const Matrix& A);
309  Matrix& concat_right(Real d); //only for column vectors (nr==1)
311  Matrix& concat_below(Real d); //only for row vectors (nc==1)
313  Matrix& enlarge_right(Integer addnc);
315  Matrix& enlarge_below(Integer addnr);
317  Matrix& enlarge_right(Integer addnc,Real d);
319  Matrix& enlarge_below(Integer addnr,Real d);
321  Matrix& enlarge_right(Integer addnc,const Real* dp,Real d=1.);
323  Matrix& enlarge_below(Integer addnr,const Real *dp,Real d=1.);
324 
326  Real* get_store() {return m;} //use cautiously, do not use delete!
328  const Real* get_store() const {return m;} //use cautiously
329 
330 
332 
333 
337 
339  friend Matrix diag(const Matrix& A); //=(A(1,1),A(2,2),...)^t
340 
342  friend Matrix triu(const Matrix& A,Integer i);
344  friend Matrix tril(const Matrix& A,Integer i);
345 
346 
348  friend Matrix concat_right(const Matrix& A,const Matrix& B);
350  friend Matrix concat_below(const Matrix& A,const Matrix& B);
351 
353  friend void swap(Matrix &A,Matrix &B);
354 
356 
357 
358  //------------------------------
359  //---- BLAS-like Routines
360  //------------------------------
361 
365 
367  Matrix& xeya(const Matrix& A,Real d=1.,int atrans=0); //*this=d*A
369  Matrix& xpeya(const Matrix& A,Real d=1.); //*this+=d*A;
371  Matrix& xeya(const Indexmatrix& A,Real d=1.); //*this=d*A
373  Matrix& xpeya(const Indexmatrix& A,Real d=1.); //*this+=d*A;
374 
376 
380 
382  friend Matrix& xbpeya(Matrix& x,const Matrix& y,Real alpha,Real beta,int ytrans);
383 
385  friend Matrix& xeyapzb(Matrix& x,const Matrix& y,const Matrix& z,Real alpha,Real beta);
386 
388  friend Matrix& genmult(const Matrix& A,const Matrix& B,Matrix& C,
389  Real alpha,Real beta,int atrans,int btrans);
391 
392 
393  //------------------------------
394  //---- usual operators
395  //------------------------------
396 
400 
402  inline Matrix& operator=(const Matrix &A);
404  inline Matrix& operator*=(const Matrix &s);
406  inline Matrix& operator+=(const Matrix &v);
408  inline Matrix& operator-=(const Matrix &v);
410  inline Matrix& operator%=(const Matrix &A);
412  inline Matrix& operator/=(const Matrix &A);
414  inline Matrix operator-() const;
415 
417  inline Matrix& operator*=(Real d);
419  inline Matrix& operator/=(Real d);
421  inline Matrix& operator+=(Real d);
423  inline Matrix& operator-=(Real d);
424 
426  Matrix& transpose(); //transposes itself
427 
429 
433 
435  friend Matrix operator*(const Matrix &A,const Matrix& B);
437  friend Matrix operator+(const Matrix &A,const Matrix& B);
439  friend Matrix operator-(const Matrix &A,const Matrix& B);
441  friend Matrix operator%(const Matrix &A,const Matrix& B);
443  friend Matrix operator/(const Matrix &A,const Matrix& B);
445  friend Matrix operator*(const Matrix &A,Real d);
447  friend Matrix operator*(Real d,const Matrix &A);
449  friend Matrix operator/(const Matrix& A,Real d);
451  friend Matrix operator+(const Matrix& A,Real d);
453  friend Matrix operator+(Real d,const Matrix& A);
455  friend Matrix operator-(const Matrix& A,Real d);
457  friend Matrix operator-(Real d,const Matrix& A);
458 
460  friend Matrix transpose(const Matrix& A);
461 
463 
464  //------------------------------------------
465  //---- Connections to other Matrix Classes
466  //------------------------------------------
467 
471 
472 
474  Matrix& xeya(const Symmatrix& A,Real d=1.); //*this=A*d;
476  Matrix& xpeya(const Symmatrix& A,Real d=1.); //*this+=A*d;
478  inline Matrix& operator=(const Symmatrix& S);
480  inline Matrix& operator*=(const Symmatrix& S);
482  inline Matrix& operator+=(const Symmatrix& S);
484  inline Matrix& operator-=(const Symmatrix& S);
485 
487  Matrix& xeya(const Sparsesym& A,Real d=1.); //*this=A*d;
489  Matrix& xpeya(const Sparsesym& A,Real d=1.); //*this+=A*d;
491  inline Matrix& operator=(const Sparsesym &);
493  inline Matrix& operator*=(const Sparsesym& S);
495  inline Matrix& operator+=(const Sparsesym& S);
497  inline Matrix& operator-=(const Sparsesym& S);
498 
500  Matrix& xeya(const Sparsemat& A,Real d=1.); //*this=A*d;
502  Matrix& xpeya(const Sparsemat& A,Real d=1.); //*this+=A*d;
504  inline Matrix& operator=(const Sparsemat & A);
506  inline Matrix& operator*=(const Sparsemat &A);
508  inline Matrix& operator+=(const Sparsemat &A);
510  inline Matrix& operator-=(const Sparsemat &A);
511 
513 
517 
519  friend std::vector<double>& assign(std::vector<double>& vec,const Matrix& A);
520 
521 
523  friend Matrix& genmult(const Symmatrix& A,const Matrix& B,Matrix& C,
524  Real alpha,Real beta,int btrans);
526  friend Matrix& genmult(const Matrix& A,const Symmatrix& B,Matrix& C,
527  Real alpha,Real beta,int atrans);
528 
530  friend Matrix& genmult(const Sparsesym& A,const Matrix& B,Matrix& C,
531  Real alpha,Real beta,int btrans);
533  friend Matrix& genmult(const Matrix& A,const Sparsesym& B,Matrix& C,
534  Real alpha,Real beta,int atrans);
535 
537  friend Matrix& genmult(const Sparsemat& A,const Matrix& B,Matrix &C,
538  Real alpha,Real beta, int atrans,int btrans);
540  friend Matrix& genmult(const Sparsemat& A,const Matrix& B,Integer colB,Matrix &C,Integer colC,Real alpha,Real beta, int atrans,int btrans);
542  friend Matrix& genmult(const Matrix& A,const Sparsemat& B,Matrix &C,
543  Real alpha,Real beta, int atrans,int btrans);
544 
546 
547  //------------------------------
548  //---- Elementwise Operations
549  //------------------------------
550 
554 
556  Matrix& rand(Integer nr,Integer nc,CH_Tools::GB_rand* random_generator=0);
558  Matrix& rand_normal(Integer nr,Integer nc,Real mean=0.,Real variance=1.,int generator_type=0);
560  Matrix& shuffle(CH_Tools::GB_rand* random_generator=0);
562  Matrix& inv(void); //reciprocal value componentwise
564  Matrix& sqrt(void);
566  Matrix& sqr(void);
568  Matrix& sign(Real tol=1e-12);
570  Matrix& floor(void);
572  Matrix& ceil(void);
574  Matrix& rint(void);
576  Matrix& round(void);
578  Matrix& abs(void);
579 
581 
585 
587  friend Matrix rand(Integer nr,Integer nc,CH_Tools::GB_rand* random_generator);
589  friend Matrix inv(const Matrix& A);
591  friend Matrix sqrt(const Matrix& A);
593  friend Matrix sqr(const Matrix& A);
595  friend Matrix sign(const Matrix& A,Real tol);
597  friend Matrix floor(const Matrix& A);
599  friend Matrix ceil(const Matrix& A);
601  friend Matrix rint(const Matrix& A);
603  friend Matrix round(const Matrix& A);
605  friend Matrix abs(const Matrix& A);
606 
608 
609  //----------------------------
610  //---- Numerical Methods
611  //----------------------------
612 
616  bool contains_nan();
618 
620  Matrix& scale_rows(const Matrix& vec); //A=diag(vec)*A
622  Matrix& scale_cols(const Matrix& vec); //A=A*diag(vec)
623 
624  //----- triangular routines
626  int triu_solve(Matrix& rhs,Real tol=1e-10); //only elements i>=j are used
628  int tril_solve(Matrix& rhs,Real tol=1e-10); //only elements i<=j are used
629 
630 
631  //----- QR-Factorization
633  int QR_factor(Real tol=1e-10); //factorization stored in *this
635  int QR_factor(Matrix& Q,Real tol=1e-10); //afterwards *this is R
637  int QR_factor(Matrix& Q,Matrix& R,Real tol) const; //*this is unchanged
638 
640  int QR_factor(Indexmatrix& piv,Real tol=1e-10);
642  int QR_factor_relpiv(Indexmatrix& piv,Real tol=1e-10);
644  int QR_factor(Matrix& Q,Indexmatrix& piv,Real tol=1e-10);
646  int QR_factor(Matrix& Q,Matrix& R,Indexmatrix& piv,Real tol) const;
648  int Qt_times(Matrix& A,Integer r) const;
650  int Q_times(Matrix& A,Integer r) const;
652  int times_Q(Matrix& A,Integer r) const;
658  int QR_solve(Matrix& rhs,Real tol=1e-10);
659 
674  int QR_concat_right(const Matrix& A,Indexmatrix& piv,Integer r,Real tol=1e-10);
675 
676 
677  //----- Least squares
679  int ls(Matrix & rhs, Real tol);
680 
692  int nnls(Matrix& rhs,Matrix* dual=0,Real tol=1e-10) const;
693 
695 
699 
701  friend Real trace(const Matrix& A); //=sum(diag(A))
703  friend Real ip(const Matrix& A, const Matrix& B); //=trace(B^t*A)
705  friend Real ip_min_max(const Matrix& A, const Matrix& B,Real &minval,Real& maxval); //=trace(B^t*A), minval=min(B_ij*A_ij), maxval=max(B_ij*A_ij)
707  friend Real colip(const Matrix& A,Integer j,const Matrix* scaling);
709  friend Real rowip(const Matrix& A,Integer i,const Matrix* scaling);
711  friend Matrix colsip(const Matrix& A);
713  friend Matrix rowsip(const Matrix& A);
715  friend Real norm2(const Matrix& A); //=sqrt(ip(A,A));
717  friend Real normDsquared(const Matrix& A,const Matrix& d,int atrans,int dinv);
718 
720  friend Matrix sumrows(const Matrix& A); //=(1 1 1 ... 1)*A
722  friend Matrix sumcols(const Matrix& A); //=A*(1 1 ... 1)^t
724  friend Real sum(const Matrix& A); //=(1 1 ... 1)*A*(1 1 ... 1)^t
725 
726  //----- Householder rotations
728  friend Matrix house(const Matrix &A,Integer i,Integer j,Real tol);
730  friend int rowhouse(Matrix &A,const Matrix& v,Integer i,Integer j);
732  friend int colhouse(Matrix &A,const Matrix& v,Integer i,Integer j);
733 
735  friend int QR_factor(const Matrix& A,Matrix& Q,Matrix &R,Real tol);
737  friend int QR_factor(const Matrix& A,Matrix& Q,Matrix &R,Indexmatrix& piv,Real tol);
738 
740 
741  //---------------------------------------------
742  //---- Comparisons / Max / Min / sort / find
743  //---------------------------------------------
744 
748 
750  Indexmatrix find(Real tol=1e-10) const; //finds nonzeros
752  Indexmatrix find_number(Real num=0.,Real tol=1e-10) const;
753 
755 
759 
761  friend Matrix operator<(const Matrix &A,const Matrix &B);
763  friend Matrix operator>(const Matrix &A,const Matrix &B);
765  friend Matrix operator<=(const Matrix &A,const Matrix &B);
767  friend Matrix operator>=(const Matrix &A,const Matrix &B);
769  friend Matrix operator==(const Matrix &A,const Matrix &B);
771  friend Matrix operator!=(const Matrix &A,const Matrix &B);
773  friend Matrix operator<(const Matrix &A,Real d);
775  friend Matrix operator>(const Matrix &A,Real d);
777  friend Matrix operator<=(const Matrix &A,Real d);
779  friend Matrix operator>=(const Matrix &A,Real d);
781  friend Matrix operator==(const Matrix &A,Real d);
783  friend Matrix operator!=(const Matrix &A,Real d);
785  friend Matrix operator<(Real d,const Matrix &A);
787  friend Matrix operator>(Real d,const Matrix &A);
789  friend Matrix operator<=(Real d,const Matrix &A);
791  friend Matrix operator>=(Real d,const Matrix &A);
793  friend Matrix operator==(Real d,const Matrix &A);
795  friend Matrix operator!=(Real d,const Matrix &A);
796 
798  friend bool equal(const Matrix& A,const Matrix& B);
799 
801  friend Matrix minrows(const Matrix& A); //min of each column (over the rows)
803  friend Matrix mincols(const Matrix& A); //min of each row (over the columns)
805  friend Real min(const Matrix& A,Integer *iindex,Integer *jindex);
807  friend Matrix maxrows(const Matrix& A); //similar
809  friend Matrix maxcols(const Matrix& A); //similar
811  friend Real max(const Matrix& A,Integer *iindex,Integer *jindex);
812 
814  friend Indexmatrix sortindex(const Matrix& vec,bool nondecreasing);
816  friend void sortindex(const Matrix& vec,Indexmatrix &ind,bool nondecreasing);
817 
819  friend Indexmatrix find(const Matrix& A,Real tol);
821  friend Indexmatrix find_number(const Matrix& A,Real num,Real tol);
822 
824 
825  //--------------------------------
826  //---- Input / Output
827  //--------------------------------
828 
832 
835  void display(std::ostream& out,
836  int precision=0,
837  int width=0,
838  int screenwidth=0
839  ) const;
840 
843  void mfile_output(std::ostream& out,
844  int precision=16,
845  int width=0
846  ) const;
847 
849 
853 
855  friend std::ostream& operator<<(std::ostream& o,const Matrix &v);
857  friend std::istream& operator>>(std::istream& i,Matrix &v);
858 
860 
861 };
862 
864 
865 // **************************************************************************
866 // make non inline friends available outside
867 // **************************************************************************
868 
870 Matrix diag(const Matrix& A); //=(A(1,1),A(2,2),...)^t
871 
873 Matrix& xbpeya(Matrix& x,const Matrix& y,Real alpha=1.,Real beta=0.,int ytrans=0);
874 
876 Matrix& xeyapzb(Matrix& x,const Matrix& y,const Matrix& z,Real alpha=1.,Real beta=1.);
877 
879 Matrix& genmult(const Matrix& A,const Matrix& B,Matrix& C,
880  Real alpha=1.,Real beta=0.,int atrans=0,int btrans=0);
881 
883 Matrix transpose(const Matrix& A);
884 
886 std::vector<double>& assign(std::vector<double>& vec,const Matrix& A);
887 
889 Matrix& genmult(const Symmatrix& A,const Matrix& B,Matrix& C,
890  Real alpha,Real beta,int btrans);
891 
893 Matrix& genmult(const Matrix& A,const Symmatrix& B,Matrix& C,
894  Real alpha,Real beta,int atrans);
895 
897 Matrix& genmult(const Sparsesym& A,const Matrix& B,Matrix& C,
898  Real alpha,Real beta,int btrans);
899 
901 Matrix& genmult(const Matrix& A,const Sparsesym& B,Matrix& C,
902  Real alpha,Real beta,int atrans);
903 
905 Matrix& genmult(const Sparsemat& A,const Matrix& B,Matrix &C,
906  Real alpha,Real beta, int atrans,int btrans);
907 
909 Matrix& genmult(const Matrix& A,const Sparsemat& B,Matrix &C,
910  Real alpha,Real beta, int atrans,int btrans);
911 
913 Matrix abs(const Matrix& A);
914 
916 Real trace(const Matrix& A);
917 
919 Real normDsquared(const Matrix& A,const Matrix& d,int atrans=0,int dinv=0);
920 
922 Matrix sumrows(const Matrix& A); //=(1 1 1 ... 1)*A
923 
925 Matrix sumcols(const Matrix& A); //=A*(1 1 ... 1)^t
926 
928 Real sum(const Matrix& A); //=(1 1 ... 1)*A*(1 1 ... 1)^t
929 
931 Matrix house(const Matrix &x,Integer i=0,Integer j=0,Real tol=1e-10);
932 
934 int rowhouse(Matrix &A,const Matrix& v,Integer i=0,Integer j=0);
935 
937 int colhouse(Matrix &A,const Matrix& v,Integer i=0,Integer j=0);
938 
940 Matrix operator<(const Matrix &A,const Matrix &B);
941 
943 Matrix operator<=(const Matrix &A,const Matrix &B);
944 
946 Matrix operator==(const Matrix &A,const Matrix &B);
947 
949 Matrix operator!=(const Matrix &A,const Matrix &B);
950 
952 Matrix operator<(const Matrix &A,Real d);
953 
955 Matrix operator>(const Matrix &A,Real d);
956 
958 Matrix operator<=(const Matrix &A,Real d);
959 
961 Matrix operator>=(const Matrix &A,Real d);
962 
964 Matrix operator==(const Matrix &A,Real d);
965 
967 Matrix operator!=(const Matrix &A,Real d);
968 
970 Matrix minrows(const Matrix& A); //min of each column (over the rows)
971 
973 Matrix mincols(const Matrix& A); //min of each row (over the columns)
974 
976 Real min(const Matrix& A,Integer *iindex=0,Integer *jindex=0);
977 
979 Matrix maxrows(const Matrix& A); //similar
980 
982 Matrix maxcols(const Matrix& A); //similar
983 
985 Real max(const Matrix& A,Integer *iindex=0,Integer *jindex=0);
986 
988 Indexmatrix sortindex(const Matrix& vec,bool nondecreasing=true);
989 
991 void sortindex(const Matrix& vec,Indexmatrix &ind, bool nondecreasing=true);
992 
994 std::ostream& operator<<(std::ostream& o,const Matrix &v);
995 
997 std::istream& operator>>(std::istream& i,Matrix &v);
998 
999 // **************************************************************************
1000 // implementation of inline functions
1001 // **************************************************************************
1002 
1004 {
1005  nr=nc=0;mem_dim=0;m=0;
1006  aux_task=none;
1007  aux_dim=0;
1008  aux_m=0;
1009  chk_set_init(*this,1);
1010 }
1011 
1013 {
1014  newsize(inr,inc);
1015  mat_xea(nr*nc,m,d);
1016  chk_set_init(*this,1);
1017  return *this;
1018 }
1019 
1020 inline Matrix& Matrix::init(Integer inr,Integer inc,const Real* p,Integer incr,Real d)
1021 {
1022  newsize(inr,inc);
1023  if (d==1.){
1024  if (incr==1) mat_xey(nr*nc,m,p);
1025  else mat_xey(nr*nc,m,Integer(1),p,incr);
1026  }
1027  else {
1028  if (incr==1) mat_xeya(nr*nc,m,p,d);
1029  else mat_xeya(nr*nc,m,Integer(1),p,incr,d);
1030  }
1031  chk_set_init(*this,1);
1032  return *this;
1033 }
1034 
1035 inline Matrix& Matrix::init(const Matrix& A,Real d,int atrans)
1036 {
1037  return xeya(A,d,atrans);
1038 }
1039 
1041 {
1042  return xeya(A,d);
1043 }
1044 
1045 inline Matrix& Matrix::init(const std::vector<Real>& vec)
1046 {
1047  newsize(Integer(vec.size()),1); chk_set_init(*this,1);
1048  for(Integer i=0;i<nr;i++) m[i]=vec[(unsigned long)(i)];
1049  return *this;
1050 }
1051 
1053 {
1054  init(n,n,0.);
1055  mat_xea(n,m,n+1,d);
1056  return *this;
1057 }
1058 
1059 inline Matrix& Matrix::init_diag(const Matrix& vec,Real d)
1060 {
1061  Integer n=vec.dim();
1062  init(n,n,0.);
1063  if(d==1.)
1064  mat_xey(n,m,n+1,vec.get_store(),1);
1065  else
1066  mat_xeya(n,m,n+1,vec.get_store(),1,d);
1067  return *this;
1068 }
1069 
1071 {
1072  Integer n=vec.dim();
1073  init(n,n,0.);
1074  if(d==1.)
1075  for(Integer i=0;i<n;i++) m[i*(n+1)]=Real(vec.m[i]);
1076  else
1077  for(Integer i=0;i<n;i++) m[i*(n+1)]=vec.m[i]*d;
1078  return *this;
1079 }
1080 
1081 
1083 {
1084  init_to_zero();
1085 }
1086 
1087 inline Matrix::Matrix(const Matrix &A,Real d,int atrans):Memarrayuser()
1088 {
1089  init_to_zero();
1090  xeya(A,d,atrans);
1091 }
1092 
1094 {
1095  init_to_zero();
1096  xeya(A,d);
1097 }
1098 
1100 {
1101  init_to_zero();
1102  newsize(inr,inc);
1103 }
1104 
1105 inline Matrix::Matrix(const Realrange& range):Memarrayuser()
1106 {
1107  init_to_zero();
1108  init(range);
1109 }
1110 
1112 {
1113  init_to_zero();
1114  init(inr,inc,d);
1115 }
1116 
1117 inline Matrix::Matrix(Integer inr,Integer inc,const Real *p,Integer incr,Real d):Memarrayuser()
1118 {
1119  init_to_zero();
1120  init(inr,inc,p,incr,d);
1121 }
1122 
1123 inline Matrix::Matrix(const std::vector<Real>& vec):Memarrayuser()
1124 {
1125  init_to_zero();
1126  init(vec);
1127 }
1128 
1130 {
1131  chk_range(i,j,nr,nc);
1132  return m[j*nr+i];
1133 }
1134 
1136 {
1137  chk_range(i,0,nr*nc,1);
1138  return m[i];
1139 }
1140 
1142 {
1143  chk_range(i,j,nr,nc);
1144  return m[j*nr+i];
1145 }
1146 
1148 {
1149  chk_range(i,0,nr*nc,1);
1150  return m[i];
1151 }
1152 
1154 {return (*this)(i);}
1155 
1157 {return (*this)(i);}
1158 
1160 { nr=min(nr*nc,max(Integer(0),n)); nc=1; return *this;}
1161 
1162 
1163 
1165 inline Real ip(const Matrix& A, const Matrix& B)
1166 {
1167  chk_add(A,B);
1168  return mat_ip(A.nc*A.nr,A.m,B.m);
1169 }
1170 
1171 
1173 inline Real colip(const Matrix& A,Integer j,const Matrix* scaling=0)
1174 {
1175  chk_init(A);
1176  chk_range(j,j,A.nc,A.nc);
1177  if (scaling==0)
1178  return mat_ip(A.nr,A.m+j*A.nr);
1179  assert(scaling->dim()==A.nr);
1180  const Real* mp=A.m+j*A.nr;
1181  const Real* const mend=mp+A.nr;
1182  const Real* sp=scaling->m;
1183  Real sum=0;
1184  for(;mp!=mend;) {
1185  const Real d=(*mp++);
1186  sum+=d*d*(*sp++);
1187  }
1188  return sum;
1189 }
1190 
1192 inline Real rowip(const Matrix& A,Integer i,const Matrix* scaling=0)
1193 {
1194  chk_init(A);
1195  chk_range(i,i,A.nr,A.nr);
1196  if (scaling==0)
1197  return mat_ip(A.nc,A.m+i,A.nr);
1198  assert(scaling->dim()==A.nc);
1199  const Real* mp=A.m+i;
1200  const Real* const mend=mp+A.nc*A.nr;
1201  const Real* sp=scaling->m;
1202  Real sum=0;
1203  for(;mp!=mend;mp+=A.nr) {
1204  const Real d=(*mp);
1205  sum+=d*d*(*sp++);
1206  }
1207  return sum;
1208 }
1209 
1211 inline Matrix colsip(const Matrix& A)
1212 {
1213  chk_init(A);
1214  Matrix tmp(1,A.nc); chk_set_init(tmp,1);
1215  for (Integer j=0;j<A.nc;j++)
1216  tmp[j]=colip(A,j);
1217  return tmp;
1218 }
1219 
1221 inline Matrix rowsip(const Matrix& A)
1222 {
1223  chk_init(A);
1224  Matrix tmp(A.nr,1,0.);
1225  Real* mp =A.m;
1226  for (Integer j=0;j<A.nc;j++){
1227  Real *tp=tmp.m;
1228  for (Integer i=0;i<A.nr;i++,mp++)
1229  (*tp++)+=(*mp)*(*mp);
1230  }
1231  return tmp;
1232 }
1233 
1235 inline Real norm2(const Matrix& A)
1236 {
1237  chk_init(A);
1238  return ::sqrt(mat_ip(A.nc*A.nr,A.m));
1239 }
1240 
1241 inline Matrix& Matrix::operator=(const Matrix &A)
1242 { return xeya(A);}
1243 
1244 inline Matrix& Matrix::operator*=(const Matrix &A)
1245 { Matrix C; return xeya(genmult(*this,A,C));}
1246 
1247 inline Matrix& Matrix::operator+=(const Matrix &A)
1248 { return xpeya(A); }
1249 
1250 inline Matrix& Matrix::operator-=(const Matrix &A)
1251 { return xpeya(A,-1.); }
1252 
1254 { chk_add(*this,A); mat_xhadey(nr*nc,m,A.m); return *this; }
1255 
1257 { chk_add(*this,A); mat_xinvhadey(nr*nc,m,A.m); return *this; }
1258 
1259 inline Matrix Matrix::operator-() const
1260 { return Matrix(*this,-1.); }
1261 
1262 inline Matrix& Matrix::operator*=(Real d)
1263 { chk_init(*this); mat_xmultea(nr*nc,m,d); return *this; }
1264 
1266 { chk_init(*this); mat_xmultea(nr*nc,m,1./d); return *this; }
1267 
1268 inline Matrix& Matrix::operator+=(Real d)
1269 { chk_init(*this); mat_xpea(nr*nc,m,d); return *this; }
1270 
1271 inline Matrix& Matrix::operator-=(Real d)
1272 { chk_init(*this); mat_xpea(nr*nc,m,-d); return *this; }
1273 
1275 inline Matrix operator*(const Matrix &A,const Matrix &B)
1276  {Matrix C; return genmult(A,B,C);}
1277 
1279 inline Matrix operator+(const Matrix &A,const Matrix &B)
1280  {Matrix C; return xeyapzb(C,A,B,1.,1.);}
1281 
1283 inline Matrix operator-(const Matrix &A,const Matrix &B)
1284  {Matrix C; return xeyapzb(C,A,B,1.,-1.);}
1285 
1287 inline Matrix operator%(const Matrix &A,const Matrix &B)
1288  {Matrix C(A); return C%=B;}
1289 
1291 inline Matrix operator/(const Matrix &A,const Matrix &B)
1292  {Matrix C(A); return C/=B;}
1293 
1295 inline Matrix operator*(const Matrix &A,Real d)
1296  {return Matrix(A,d);}
1297 
1299 inline Matrix operator*(Real d,const Matrix &A)
1300  {return Matrix(A,d);}
1301 
1303 inline Matrix operator/(const Matrix &A,Real d)
1304  {return Matrix(A,1./d);}
1305 
1307 inline Matrix operator+(const Matrix &A,Real d)
1308  {Matrix B(A); return B+=d;}
1309 
1311 inline Matrix operator+(Real d,const Matrix &A)
1312  {Matrix B(A); return B+=d;}
1313 
1315 inline Matrix operator-(const Matrix &A,Real d)
1316  {Matrix B(A); return B-=d;}
1317 
1319 inline Matrix operator-(Real d,const Matrix &A)
1320  {Matrix B(A,-1.); return B+=d;}
1321 
1322 inline int Matrix::QR_factor(Matrix& Q,Matrix& R,Real tol=1e-10) const //*this is unchanged
1323 {R=*this;return R.QR_factor(Q,tol);}
1324 inline int Matrix::QR_factor(Matrix& Q,Matrix& R,Indexmatrix& piv,Real tol=1e-10) const
1325 {R=*this;return R.QR_factor(Q,piv,tol);}
1326 inline int Matrix::ls(Matrix & rhs, Real tol=1e-10)
1327  { return this->QR_solve(rhs,tol);}
1328 
1330 inline int QR_factor(const Matrix& A,Matrix& Q,Matrix &R,Real tol)
1331 {return A.QR_factor(Q,R,tol);}
1332 
1334 inline int QR_factor(const Matrix& A,Matrix& Q,Matrix &R,Indexmatrix& piv,Real tol)
1335 {return A.QR_factor(Q,R,piv,tol);}
1336 
1338 inline Matrix triu(const Matrix& A,Integer i=0)
1339  {Matrix B(A); B.triu(i); return B;}
1340 
1342 inline Matrix tril(const Matrix& A,Integer i=0)
1343  {Matrix B(A); B.tril(i); return B;}
1344 
1346 inline Matrix concat_right(const Matrix& A,const Matrix& B)
1347  {Matrix C(A.dim()+B.dim(),1);C=A;C.concat_right(B);return C;}
1348 
1350 inline Matrix concat_below(const Matrix& A,const Matrix& B)
1351  {Matrix C(A.dim()+B.dim(),1);C=A;C.concat_below(B);return C;}
1352 
1354 inline void swap(Matrix &A,Matrix &B)
1355 {
1356  Real *hm=A.m;A.m=B.m;B.m=hm;
1357  Integer hi=A.nr;A.nr=B.nr;B.nr=hi;
1358  hi=A.nc;A.nc=B.nc;B.nc=hi;
1359  hi=A.mem_dim;A.mem_dim=B.mem_dim;B.mem_dim=hi;
1360 #if (CONICBUNDLE_DEBUG>=1)
1361  bool hb=A.is_init;A.is_init=B.is_init;B.is_init=hb;
1362 #endif
1363 }
1364 
1366 inline Matrix rand(Integer rows,Integer cols,CH_Tools::GB_rand* random_generator=0)
1367 {Matrix A; return A.rand(rows,cols,random_generator);}
1368 
1370 inline Matrix inv(const Matrix& A)
1371  {Matrix B(A); return B.inv();}
1372 
1374 inline Matrix sqrt(const Matrix& A)
1375  {Matrix B(A); return B.sqrt();}
1376 
1378 inline Matrix sqr(const Matrix& A)
1379  {Matrix B(A); return B.sqr();}
1380 
1382 inline Matrix sign(const Matrix& A,Real tol=1e-12)
1383  {Matrix B(A); return B.sign(tol);}
1384 
1386 inline Matrix floor(const Matrix& A)
1387  {Matrix B(A); return B.floor();}
1388 
1390 inline Matrix ceil(const Matrix& A)
1391  {Matrix B(A); return B.floor();}
1392 
1394 inline Matrix rint(const Matrix& A)
1395  {Matrix B(A); return B.floor();}
1396 
1398 inline Matrix round(const Matrix& A)
1399  {Matrix B(A); return B.floor();}
1400 
1401 
1403 inline Matrix operator>(const Matrix &A,const Matrix &B)
1404 {return B<A;}
1405 
1407 inline Matrix operator>=(const Matrix &A,const Matrix &B)
1408 {return B<=A;}
1410 inline Matrix operator<(Real d,const Matrix &A)
1411 {return A>d;}
1413 inline Matrix operator>(Real d,const Matrix &A)
1414 {return A<d;}
1415 
1417 inline Matrix operator<=(Real d,const Matrix &A)
1418 {return A>=d;}
1419 
1421 inline Matrix operator>=(Real d,const Matrix &A)
1422 {return A<=d;}
1423 
1425 inline Matrix operator==(Real d,const Matrix &A)
1426 {return A==d;}
1427 
1429 inline Matrix operator!=(Real d,const Matrix &A)
1430 {return A!=d;}
1431 
1433 inline bool equal(const Matrix& A,const Matrix& B)
1434 {return ((A.nc==B.nc)&&(A.nr==B.nr)&&(mat_equal(A.nr*A.nc,A.get_store(),B.get_store())));}
1435 
1436 inline Indexmatrix sortindex(const Matrix& vec,bool nondecreasing)
1437 {Indexmatrix ind;sortindex(vec,ind,nondecreasing);return ind;}
1438 
1439 
1441 inline Indexmatrix find(const Matrix& A,Real tol=1e-10)
1442  {return A.find(tol);}
1444 inline Indexmatrix find_number(const Matrix& A,Real num=0.,Real tol=1e-10)
1445  {return A.find_number(num,tol);}
1446 
1447 inline std::vector<double>& assign(std::vector<double>& vec,const Matrix& A)
1448 {
1449  chk_init(A);
1450  vec.resize((unsigned long)(A.dim()));
1451  for(Integer i=0;i<A.dim();i++) vec[(unsigned long)(i)]=A(i);
1452  return vec;
1453 }
1454 
1455 }
1456 
1457 
1458 //in order to define the inlines associated with the other matrix
1459 //classes, the hxx-files of these are included here, as well.
1460 
1461 #ifndef CH_MATRIX_CLASSES__SYMMAT_HXX
1462 #include "symmat.hxx"
1463 #endif
1464 
1465 #endif
#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
friend Matrix & genmult(const Matrix &A, const Matrix &B, Matrix &C, Real alpha, Real beta, int atrans, int btrans)
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=...
AuxTask aux_task
the current purpose of the auxilliary information
Definition: matrix.hxx:95
Indexmatrix mincols(const Indexmatrix &A)
returns a column vector holding in each row the minimum over all columns in this row ...
Matrix & floor(void)
sets (*this)(i,j)=floor((*this)(i,j)) for all i,j and returns *this
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 sqrt(const Matrix &A)
returns a matrix with elements (i,j)=sqrt((*this)(i,j)) for all i,j
Definition: matrix.hxx:1374
Integer mem_dim
amount of memory currently allocated
Definition: matrix.hxx:84
Matrix operator-(const Matrix &A, const Matrix &B)
returns Matrix equal to A-B
Definition: matrix.hxx:1283
Matrix & xpeya(const Matrix &A, Real d=1.)
sets *this+=d*A and returns *this
friend Matrix rowsip(const Matrix &A)
returns the row vector of the squared Frobenius norm of all rowd i of A, i.e., the sum of A(i...
Definition: matrix.hxx:1221
Matrix cols(const Indexmatrix &vec) const
returns a matrix of size this->rowdim() x vec.dim(), with column i a copy of column vec(i) of *this ...
Real & operator[](Integer i)
returns reference to element (i) of the matrix if regarded as vector of stacked columns [element (iro...
Definition: matrix.hxx:1153
Integer rowdim() const
returns the row dimension
Definition: matrix.hxx:215
Real & operator()(Integer i, Integer j)
returns reference to element (i,j) of the matrix (rowindex i, columnindex j)
Definition: matrix.hxx:1129
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
int QR_factor(const Matrix &A, Matrix &Q, Matrix &R, Real tol)
computes a Householder QR factorization of A and outputs Q and R leaving A unchanged; always returns ...
Definition: matrix.hxx:1330
Matrix rowsip(const Matrix &A)
returns the row vector of the squared Frobenius norm of all rowd i of A, i.e., the sum of A(i...
Definition: matrix.hxx:1221
Matrix & init_diag(int nr, Real d=1.)
initialize to a diagonal nr x nr matrix with constant diagonal value d
Definition: matrix.hxx:1052
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
int ls(Matrix &rhs, Real tol)
computes a least squares solution by QR_solve, overwriting (*this). rhs is overwritten with the solut...
Definition: matrix.hxx:1326
Matrix floor(const Matrix &A)
returns a matrix with elements (i,j)=floor((*this)(i,j)) for all i,j
Definition: matrix.hxx:1386
friend 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
Real * aux_m
pointer to supplementary store for repeated use in some routines like qr
Definition: matrix.hxx:97
Indexmatrix maxcols(const Indexmatrix &A)
returns a column vector holding in each row the maximum over all columns in this row ...
Real tol
tolerance for chekcing equality
Definition: matrix.hxx:44
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...
int rowhouse(Matrix &A, const Matrix &v, Integer i=0, Integer j=0)
Housholder pre-multiplication of A with Householder vector v; the first nonzero of v is index i...
friend 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
Matrix class for integral values of type Integer
Definition: indexmat.hxx:195
Matrix & init(const Matrix &A, Real d=1., int atrans=0)
initialize to *this=A*d where A may be transposed
Definition: matrix.hxx:1035
void dim(Integer &_nr, Integer &_nc) const
returns the number of rows in _nr and the number of columns in _nc
Definition: matrix.hxx:209
Mtype
serves for specifying the source (matrix class or function) of the error
Definition: matop.hxx:1585
Matrix & tril(Integer d=0)
keeps everything below and including diagonal d, everything above is set to zero, returns *this ...
friend Matrix operator*(const Matrix &A, const Matrix &B)
returns Matrix equal to A*B
Definition: matrix.hxx:1275
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
Real normDsquared(const Matrix &A, const Matrix &d, int atrans=0, int dinv=0)
returns trace(A^TDA)=|A|^2_D with D=Diag(d). A may be transposed, D may be inverted but there is no c...
Matrix & concat_right(const Matrix &A, int Atrans=0)
concats matrix A (or its tranpose) to the right of *this, A or *this may be the 0x0 matrix initally...
friend Matrix operator+(const Matrix &A, const Matrix &B)
returns Matrix equal to A+B
Definition: matrix.hxx:1279
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
~Realrange()
destructor, nothing to do
Definition: matrix.hxx:49
Matrix operator*(const Matrix &A, const Matrix &B)
returns Matrix equal to A*B
Definition: matrix.hxx:1275
static const Mtype mtype
used for MatrixError templates (runtime type information was not yet existing)
Definition: matrix.hxx:83
void mat_xeya(Integer len, Val *x, const Val *y, const Val a)
Set x[i]=a*y[i] for len elements of the arrays x and y.
Definition: matop.hxx:340
friend 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
allows to specify a range of real values via (from, to, step,tol) meaning {x=from+i*step:x in(from-to...
Definition: matrix.hxx:34
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
Real rowip(const Matrix &A, Integer i, const Matrix *scaling=0)
returns the squared Frobenius norm of row i of A, i.e., the sum of A(i,j)*A(i,j) over all j with poss...
Definition: matrix.hxx:1192
Matrix & rint(void)
sets (*this)(i,j)=rint((*this)(i,j)) for all i,j and returns *this
Matrix & concat_below(const Matrix &A)
concats matrix A to the bottom of *this, A or *this may be the 0x0 matrix initally, returns *this
Mtype get_mtype() const
returns the type of the matrix, MTmatrix
Definition: matrix.hxx:221
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
Real * get_store()
returns the current address of the internal value array; use cautiously, do not use delete! ...
Definition: matrix.hxx:326
Matrix rint(const Matrix &A)
returns a matrix with elements (i,j)=rint((*this)(i,j)) for all i,j
Definition: matrix.hxx:1394
Matrix class of symmetric matrices with real values of type Real
Definition: symmat.hxx:43
Matrix ceil(const Matrix &A)
returns a matrix with elements (i,j)=ceil((*this)(i,j)) for all i,j
Definition: matrix.hxx:1390
Matrix operator-(Real d, const Matrix &A)
returns (i,j)=d-A(i,j) for all i,j
Definition: matrix.hxx:1319
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
Header declaring the class CH_Matrix_Classes::Symmatrix for symmetric matrices with Real elements...
friend Real colip(const Matrix &A, Integer j, const Matrix *scaling)
returns the squared Frobenius norm of column j of A, i.e., the sum of A(i,j)*A(i,j) over all i with p...
Definition: matrix.hxx:1173
friend Real sum(const Matrix &A)
returns the sum over all elements of A, i.e., (1 1 ... 1)*A*(1 1 ... 1)^T
int QR_factor(Real tol=1e-10)
computes a Householder QR_factorization overwriting (*this); returns 0 on success, otherwise column index +1 if the norm of the column is below tol when reaching it.
Real step
step distance between successive elements starting with from
Definition: matrix.hxx:42
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
friend 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
friend Indexmatrix sortindex(const Matrix &vec, bool nondecreasing)
returns an Indexmatrix ind so that vec(ind(0))<=vec(ind(1))<=...<=vec(ind(vec.dim()-1)) (vec may be r...
Definition: matrix.hxx:1436
device independent random number generator based on long int with seed
Definition: gb_rand.hxx:28
Matrix & round(void)
sets (*this)(i,j)=round((*this)(i,j)) for all i,j and returns *this
friend 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
friend Matrix operator!=(const Matrix &A, const Matrix &B)
returns a matrix having elements (i,j)=Real(A(i,j)!=B(i,j)) for all i,j
Matrix & xeya(const Matrix &A, Real d=1., int atrans=0)
sets *this=d*A where A may be transposed and returns *this
int colhouse(Matrix &A, const Matrix &v, Integer i=0, Integer j=0)
Housholder post-multiplication of A with Householder vector v; the first nonzero of v is index i...
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
friend Matrix & xeyapzb(Matrix &x, const Matrix &y, const Matrix &z, Real alpha, Real beta)
returns x= alpha*y+beta*z; x is initialized to the correct size
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
void newsize(Integer nr, Integer nc)
resize the matrix to nr x nc elements but WITHOUT initializing the memory
friend Matrix colsip(const Matrix &A)
returns the column vector of the squared Frobenius norm of all columns j of A, i.e., the sum of A(i,j)*A(i,j) over all i for each j
Definition: matrix.hxx:1211
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
Matrix round(const Matrix &A)
returns a matrix with elements (i,j)=round((*this)(i,j)) for all i,j
Definition: matrix.hxx:1398
Matrix & operator/=(const Matrix &A)
ATTENTION: this is redefined to act componentwise without checking for zeros, (*this)(i,j)=(*this)(i,j)/A(i,j) for all i,j.
Definition: matrix.hxx:1256
Matrix & sqr(void)
sets (*this)(i,j)=sqr((*this)(i,j)) for all i,j and returns *this
#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
void mat_xinvhadey(Integer len, Val *x, const Val *y)
Set x[i]/=y[i] for len elements of the arrays x and y, no zero checking!
Definition: matop.hxx:483
friend Real min(const Matrix &A, Integer *iindex, Integer *jindex)
returns the minimum value over all elements of the matrix
Integer dim() const
returns the dimension rows * columns when the matrix is regarded as a vector
Definition: matrix.hxx:212
Indexmatrix operator>=(const Indexmatrix &A, Integer d)
returns a matrix having elements (i,j)=Integer(A(i,j)>=d) for all i,j
Indexmatrix find_number(Real num=0., Real tol=1e-10) const
returns an Indexmatrix ind so that (*this)(ind(i)) 0<=i<ind.dim() runs through all elements of value ...
Matrix & sign(Real tol=1e-12)
sets (*this)(i,j)=sign((*this)(i,j),tol) for all i,j using sign(double,double) and returns *this ...
Integer coldim() const
returns the column dimension
Definition: matrix.hxx:218
Integer aux_dim
amount of memory currently allocated in auxm
Definition: matrix.hxx:96
bool is_init
flag whether memory is initialized, it is only used if CONICBUNDLE_DEBUG is defined ...
Definition: matrix.hxx:99
int QR_solve(Matrix &rhs, Real tol=1e-10)
solves (*this)*x=rhs by factorizing and overwriting (*this); rhs is overwritten with the solution...
friend Matrix operator<(const Matrix &A, const Matrix &B)
returns a matrix having elements (i,j)=Real(A(i,j)<B(i,j)) for all i,j
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
const Real * get_store() const
returns the current address of the internal value array; use cautiously!
Definition: matrix.hxx:328
friend Real rowip(const Matrix &A, Integer i, const Matrix *scaling)
returns the squared Frobenius norm of row i of A, i.e., the sum of A(i,j)*A(i,j) over all j with poss...
Definition: matrix.hxx:1192
Matrix & triu(Integer d=0)
keeps everything above and including diagonal d, everything below is set to zero, returns *this ...
Realrange(Real _from, Real _to, Real _step=1, Real _tol=1e-8)
constructor for {d: d=from+k*step for some k in N_0 and d<=to+tol}
Definition: matrix.hxx:46
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
Matrix class of sparse matrices with real values of type Real
Definition: sparsmat.hxx:74
Matrix & inv(void)
sets (*this)(i,j)=1./(*this)(i,j) for all i,j and returns *this
int sqr(int a)
return a*a for int a
Definition: mymath.hxx:103
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
All derived classes share a common Memarray memory manager, which is generated with the first user an...
Definition: memarray.hxx:117
friend Real max(const Matrix &A, Integer *iindex, Integer *jindex)
returns the maximum value over all elements of the matrix
Integer nc
number of columns
Definition: matrix.hxx:85
Integer nr
number of rows
Definition: matrix.hxx:85
#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 ...
Real to
upper bound on element values
Definition: matrix.hxx:40
friend Matrix operator>=(const Matrix &A, const Matrix &B)
returns a matrix having elements (i,j)=Real(A(i,j)>=B(i,j)) for all i,j
Definition: matrix.hxx:1407
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=...
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
Matrix house(const Matrix &x, Integer i=0, Integer j=0, Real tol=1e-10)
returns the Householder vector of size A.rowdim() for the subcolumn A(i:A.rowdim(),j)
double abs(double d)
absolute value of a double
Definition: mymath.hxx:25
friend Matrix operator<=(const Matrix &A, const Matrix &B)
returns a matrix having elements (i,j)=Real(A(i,j)<=B(i,j)) for all i,j
Integer * m
pointer to store, order is columnwise (a11,a21,...,anr1,a12,a22,.....)
Definition: indexmat.hxx:208
friend void swap(Matrix &A, Matrix &B)
swap the content of the two matrices A and B (involves no copying)
Definition: matrix.hxx:1354
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...
Matrix & operator%=(const Matrix &A)
ATTENTION: this is redefined as the Hadamard product, (*this)(i,j)=(*this)(i,j)*A(i,j) for all i,j.
Definition: matrix.hxx:1253
Header declaring the classes CH_Matrix_Classes::Range and CH_Matrix_Classes::Indexmatrix for supporti...
AuxTask
the auxilliary storage aux_m may be used for different pruposes which are listed here; this is used t...
Definition: matrix.hxx:91
Integer sum(const Indexmatrix &A)
returns the sum over all elements of A, i.e., (1 1 ... 1)*A*(1 1 ... 1)^T
Matrix & ceil(void)
sets (*this)(i,j)=ceil((*this)(i,j)) for all i,j and returns *this
Matrix inv(const Matrix &A)
returns a matrix with elements (i,j)=1./((*this)(i,j)) for all i,j; ATTENTION: no check for division ...
Definition: matrix.hxx:1370
bool get_init() const
returns true if the matrix has been declared initialized (not needed if CONICBUNDLE_DEBUG is undefine...
Definition: matrix.hxx:146
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 & reduce_length(Integer n)
(*this) is set to a column vector of length min{max{0,n},dim()}; usually used to truncate a vector...
Definition: matrix.hxx:1159
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
Matrix & rand(Integer nr, Integer nc, 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 [0...
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
Matrix()
empty matrix
Definition: matrix.hxx:1082
friend Matrix operator>(const Matrix &A, const Matrix &B)
returns a matrix having elements (i,j)=Real(A(i,j)>B(i,j)) for all i,j
Definition: matrix.hxx:1403
aux_m currently not in use
Definition: matrix.hxx:92
friend Matrix operator==(const Matrix &A, const Matrix &B)
returns a matrix having elements (i,j)=Real(A(i,j)==B(i,j)) for all i,j
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
Matrix rows(const Indexmatrix &vec) const
returns a matrix of size vec.dim() x this->coldim(), with row i a copy of row vec(i) of *this ...
Real colip(const Matrix &A, Integer j, const Matrix *scaling=0)
returns the squared Frobenius norm of column j of A, i.e., the sum of A(i,j)*A(i,j) over all i with p...
Definition: matrix.hxx:1173
Matrix & sqrt(void)
sets (*this)(i,j)=sqrt((*this)(i,j)) for all i,j and returns *this
Real from
value of starting element
Definition: matrix.hxx:38
Indexmatrix find(Real tol=1e-10) const
returns an Indexmatrix ind so that (*this)(ind(i)) 0<=i<ind.dim() runs through all nonzero elements ...
Real * m
pointer to store, order is columnwise (a11,a21,...,anr1,a12,a22,.....)
Definition: matrix.hxx:87
Matrix colsip(const Matrix &A)
returns the column vector of the squared Frobenius norm of all columns j of A, i.e., the sum of A(i,j)*A(i,j) over all i for each j
Definition: matrix.hxx:1211
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 init_to_zero()
initialize the matrix to a 0x0 matrix without storage
Definition: matrix.hxx:1003
friend 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
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