ConicBundle
symmat.hxx
Go to the documentation of this file.
1 
2 
3 #ifndef CH_MATRIX_CLASSES__SYMMAT_HXX
4 #define CH_MATRIX_CLASSES__SYMMAT_HXX
5 
14 #ifndef CH_MATRIX_CLASSES__MATRIX_HXX
15 #include "matrix.hxx"
16 #endif
17 
18 namespace CH_Matrix_Classes {
19 
20 
21 // **************************************************************************
22 // class definition
23 // **************************************************************************
24 
28 
43 class Symmatrix: protected Memarrayuser
44 {
45  friend class Matrix;
46  friend class Sparsesym;
47  friend class Sparsemat;
48 
49 private:
50 
51  static const Mtype mtype;
54  Real *m;
55 
56  bool is_init;
57 
59  inline void init_to_zero();
60 
62  Integer tred2(Integer nm, Integer n, Real *a, Real *d, Real *e, Real *z) const;
64  Integer imtql2(Integer nm, Integer n, Real *d, Real *e, Real *z) const;
65 
66 public:
67 
68  //----------------------------------------------------
69  //---- constructors, destructor, and initialization
70  //----------------------------------------------------
71 
72 
76 
78  inline Symmatrix();
80  inline Symmatrix(const Symmatrix& A,double d=1.);
87  inline Symmatrix(Integer nr);
89  inline Symmatrix(Integer nr,Real d);
91  inline Symmatrix(Integer nr,const Real* dp);
93  inline ~Symmatrix();
94 
95 #if (CONICBUNDLE_DEBUG>=1)
96  void set_init(bool i){is_init=i;}
99  int get_init() const {return is_init;}
100 #else
101  void set_init(bool /* i */){}
104  bool get_init() const {return true;}
105 #endif
106 
108  inline Symmatrix& init(const Symmatrix& A,double d=1.);
110  inline Symmatrix& init(const Matrix& A,double d=1.);
112  inline Symmatrix& init(const Indexmatrix& A,double d=1.);
114  inline Symmatrix& init(const Sparsesym& A,Real d=1.);
116  inline Symmatrix& init(Integer nr,Real d);
118  inline Symmatrix& init(Integer nr,const Real* dp);
119 
126  void newsize(Integer n); //resize matrix without Initialization
127 
128 
130 
134 
136  inline Symmatrix(const Matrix&,double d=1.);
138  inline Symmatrix(const Indexmatrix&,double d=1.);
140  inline Symmatrix(const Sparsesym& A,Real d=1.);
141 
143 
144  //----------------------------------------------------
145  //---- size and type information
146  //----------------------------------------------------
147 
151 
153  void dim(Integer& _nr, Integer& _nc) const {_nr=nr; _nc=nr;}
154 
156  Integer dim() const {return nr;}
157 
159  Integer rowdim() const {return nr;}
160 
162  Integer coldim() const {return nr;}
163 
165  Mtype get_mtype() const {return mtype;}
166 
168 
169 
170  //--------------------------------
171  //---- Indexing and Submatrices
172  //--------------------------------
173 
177 
179  inline Real& operator()(Integer i,Integer j);
180 
182  inline Real& operator()(Integer i);
183 
185  inline Real operator()(Integer i,Integer j) const;
186 
188  inline Real operator()(Integer i) const;
189 
191  Matrix col(Integer i) const;
193  Matrix row(Integer i) const;
195  Matrix cols(const Indexmatrix &vec) const; //returns cols as indexed by vec
197  Matrix rows(const Indexmatrix &vec) const; //returns rows as indexed by vec
198 
201 
203  Symmatrix& pivot_permute(const Indexmatrix& piv,bool inverse=false);
204 
206  Symmatrix& principal_submatrix(const Indexmatrix& ind,Symmatrix& S) const;
207 
209  inline Symmatrix principal_submatrix(const Indexmatrix& ind) const;
210 
211 
213  Symmatrix& delete_principal_submatrix(const Indexmatrix& ind,bool sorted_increasingly=false);
214 
217 
220 
221 
222 
224  Real* get_store() {return m;} //use cautiously, do not use delete!
226  const Real* get_store() const {return m;} //use cautiously
227 
229 
230 
234 
236  friend Matrix diag(const Symmatrix& A); //=(A(1,1),A(2,2),...)^t
238  friend Symmatrix Diag(const Matrix& A); //vec(A) on the diagonal
239 
241  friend inline void swap(Symmatrix& A,Symmatrix& B);
242 
244 
245  //------------------------------
246  //---- BLAS-like Routines
247  //------------------------------
248 
252 
254  Symmatrix& xeya(const Symmatrix& A,Real d=1.); //*this=d*A
256  Symmatrix& xpeya(const Symmatrix& A,Real d=1.); //*this+=d*A;
257 
258 
260 
264 
266  friend Symmatrix& rankadd(const Matrix& A,Symmatrix& C,
267  Real alpha,Real beta,int trans);
269  friend Symmatrix& scaledrankadd(const Matrix& A,const Matrix& D,Symmatrix& C,
270  Real alpha,Real beta,int trans);
272  friend Symmatrix& rank2add(const Matrix& A, const Matrix& B, Symmatrix& C,
273  Real alpha,Real beta,int trans);
274 
275 
277  friend Symmatrix& xbpeya(Symmatrix& x,const Symmatrix& y,Real alpha,Real beta);
278 
279 
281  friend Symmatrix& xeyapzb(Symmatrix& x,const Symmatrix& y,const Symmatrix& z,Real alpha,Real beta);
282 
284  friend Matrix& genmult(const Symmatrix& A,const Matrix& B,Matrix& C,
285  Real alpha, Real beta, int btrans);
286 
288  friend Matrix& genmult(const Matrix& A,const Symmatrix& B,Matrix& C,
289  Real alpha, Real beta, int atrans);
290 
292 
293  //------------------------------
294  //---- usual operators
295  //------------------------------
296 
300 
302  inline Symmatrix& operator=(const Symmatrix &A);
304  inline Symmatrix& operator+=(const Symmatrix &A);
306  inline Symmatrix& operator-=(const Symmatrix &A);
308  inline Symmatrix& operator%=(const Symmatrix &A); //Hadamard product
310  inline Symmatrix operator-() const;
311 
313  inline Symmatrix& operator*=(Real d);
315  inline Symmatrix& operator/=(Real d);
317  inline Symmatrix& operator+=(Real d);
319  inline Symmatrix& operator-=(Real d);
320 
322  Symmatrix& transpose(){return *this;}
323 
325 
329 
331  friend inline Matrix operator*(const Symmatrix &A,const Symmatrix &B);
333  friend inline Symmatrix operator%(const Symmatrix &A,const Symmatrix &B);
335  friend inline Symmatrix operator+(const Symmatrix &A,const Symmatrix &B);
337  friend inline Symmatrix operator-(const Symmatrix &A,const Symmatrix &B);
339  friend inline Matrix operator*(const Symmatrix &A,const Matrix &B);
341  friend inline Matrix operator*(const Matrix &A,const Symmatrix &B);
343  friend inline Matrix operator+(const Symmatrix &A,const Matrix &B);
345  friend inline Matrix operator+(const Matrix &A,const Symmatrix &B);
347  friend inline Matrix operator-(const Symmatrix &A,const Matrix &B);
349  friend inline Matrix operator-(const Matrix &A,const Symmatrix &B);
350 
352  friend inline Symmatrix operator*(const Symmatrix &A,Real d);
354  friend inline Symmatrix operator*(Real d,const Symmatrix &A);
356  friend inline Symmatrix operator/(const Symmatrix &A,Real d);
358  friend inline Symmatrix operator+(const Symmatrix &A,Real d);
360  friend inline Symmatrix operator+(Real d,const Symmatrix &A);
362  friend inline Symmatrix operator-(const Symmatrix &A,Real d);
364  friend inline Symmatrix operator-(Real d,const Symmatrix &A);
365 
367  friend inline Symmatrix transpose(const Symmatrix& A);
368 
370 
371  //------------------------------------------
372  //---- Connections to other Matrix Classes
373  //------------------------------------------
374 
378 
380  Symmatrix& xeya(const Matrix& A,Real d=1.); //*this=d*A
382  Symmatrix& xpeya(const Matrix& A,Real d=1.); //*this+=d*A;
384  Symmatrix& xeya(const Indexmatrix& A,Real d=1.); //*this=d*A
386  Symmatrix& xpeya(const Indexmatrix& A,Real d=1.); //*this+=d*A;
388  Symmatrix& xeya(const Sparsesym& A,Real d=1.); //*this=d*A
390  Symmatrix& xpeya(const Sparsesym& A,Real d=1.); //*this+=d*A;
391 
393  Symmatrix& xetriu_yza(const Matrix& A,const Matrix& B,Real d=1.);
395  Symmatrix& xpetriu_yza(const Matrix& A,const Matrix& B,Real d=1.);
397  Symmatrix& xetriu_yza(const Sparsemat& A,const Matrix& B,Real d=1.);
399  Symmatrix& xpetriu_yza(const Sparsemat& A,const Matrix& B,Real d=1.);
400 
402  inline Symmatrix& operator=(const Sparsesym& A);
404  inline Symmatrix& operator+=(const Sparsesym& A);
406  inline Symmatrix& operator-=(const Sparsesym& A);
407 
409 
413 
415  friend Matrix& genmult(const Symmatrix& A,const Sparsemat& B,Matrix& C,
416  Real alpha,Real beta, int btrans);
417 
419  friend Matrix& genmult(const Sparsemat& A,const Symmatrix& B,Matrix& C,
420  Real alpha,Real beta, int atrans);
421 
423  friend Symmatrix& rankadd(const Sparsemat& A,Symmatrix& C,
424  Real alpha,Real beta,int trans);
425 
427  friend Symmatrix& scaledrankadd(const Sparsemat& A,const Matrix& D,Symmatrix& C,
428  Real alpha,Real beta,int trans);
429 
431  friend Symmatrix& rank2add(const Sparsemat& A,const Matrix& B,Symmatrix& C,
432  Real alpha,Real beta,int trans);
433 
435 
436  //------------------------------
437  //---- Elementwise Operations
438  //------------------------------
439 
440 
444 
446  friend Symmatrix abs(const Symmatrix& A);
447 
449 
450  //----------------------------
451  //---- Numerical Methods
452  //----------------------------
453 
457 
459  Symmatrix& shift_diag(Real s); //add s to all diagonal elements
460 
461  //----- LDL Factorization (for positive definite matrices, no pivots so far!)
463  int LDLfactor(Real tol=1e-10);
465  int LDLsolve(Matrix& x) const; //call LDLfactor before using LDLsolve!
467  int LDLinverse(Symmatrix& S) const; //call LDLfactor before using LDLinverse!
468 
469  //----- Cholesky Factorization with pivoting
471  int Chol_factor(Real tol=1e-10); //stores fact. in *this
473  int Chol_solve(Matrix& x) const; //call _factor before
475  int Chol_inverse(Symmatrix& S) const; // --- " ---
477  int Chol_Lsolve(Matrix& rhs) const; // --- " ---, solve only Ly=x
479  int Chol_Ltsolve(Matrix& rhs) const; // --- " ---, solve only L^Ty=x
481  int Chol_scaleLi(Symmatrix& S) const;
483  int Chol_scaleLt(Symmatrix& S) const;
485  int Chol_Lmult(Matrix& rhs) const;
487  int Chol_Ltmult(Matrix& rhs) const;
489  int Chol_factor(Indexmatrix &piv, Real tol=1e-10); //stores fact. in *this
491  int Chol_solve(Matrix& x,const Indexmatrix& piv) const; //call _factor before
493  int Chol_inverse(Symmatrix& S,const Indexmatrix & piv) const; // --- " ---
494 
495  //----- Aasen factorization with pivoting
497  int Aasen_factor(Indexmatrix &piv);
499  int Aasen_Lsolve(Matrix& x) const;
501  int Aasen_Ltsolve(Matrix& x) const;
503  int Aasen_tridiagsolve(Matrix& x) const;
505  int Aasen_solve(Matrix& x, const Indexmatrix& piv) const; //call Aasen_factor before!
506 
507  //----- Eigenvalue decomposition
509  Integer eig(Matrix& P,Matrix& d,bool sort_non_decreasingly=true) const;
510  //if return value ==0 then P contains eigenvectors as columns
511  //and d is a column vector containing the eigenvalues.
512 
514 
518 
520  friend Real trace(const Symmatrix& A); //=sum(diag(A))
522  friend Real ip(const Symmatrix& A, const Symmatrix& B); //=trace(B^t*A)
524  friend Real ip(const Matrix& A, const Symmatrix& B); //=trace(B^t*A)
526  friend Real ip(const Symmatrix& A, const Matrix& B); //=trace(B^t*A)
528  friend inline Real norm2(const Symmatrix& A); //{return sqrt(ip(A,A));}
529 
531  friend Matrix sumrows(const Symmatrix& A); //=(1 1 1 ... 1)*A
533  friend Matrix sumcols(const Symmatrix& A); //=A*(1 1 ... 1)^t
535  friend Real sum(const Symmatrix& A); //=(1 1 ... 1)*A*(1 1 ... 1)^t
536 
537  //---- svec and skron
538 
540  friend void svec(const Symmatrix& A,Matrix& v,Real a,bool add,Integer startindex_vec,Integer startindex_A,Integer blockdim);
542  friend inline Matrix svec(const Symmatrix& A);
544  friend void sveci(const Matrix& v,Symmatrix& A,Real a,bool add,Integer startindex_vec,Integer startindex_A,Integer blockdim);
545 
546  // initialize from an svec stored in a real array (or matrix)
547  void init_svec(Integer nr,const Real *dp,Integer incr=1,Real d=1.);
548  // store in the form of an svec in the real array (or matrix)
549  void store_svec(Real *dp,Integer incr=1,Real d=1.) const;
550 
552  friend inline Symmatrix skron(const Symmatrix& A,const Symmatrix& B,Real alpha,bool add,Integer startindex_S);
554  friend void skron(const Symmatrix& A,const Symmatrix& B,Symmatrix& S,Real a,bool add,Integer startindex_S);
556  friend void symscale(const Symmatrix &A,const Matrix& B,Symmatrix& S,Real a,Real b,int btrans);
557 
559 
560  //---------------------------------------------
561  //---- Comparisons / Max / Min / sort / find
562  //---------------------------------------------
563 
567 
569  friend Matrix minrows(const Symmatrix& A); //minimum of each column (over rows)
571  friend Matrix mincols(const Symmatrix& A); //minimum of each row (over colums)
573  friend Real min(const Symmatrix& A); //minimum of matrix
575  friend Matrix maxrows(const Symmatrix& A); //similar
577  friend Matrix maxcols(const Symmatrix& A); //similar
579  friend Real max(const Symmatrix& A); //similar
580 
581 
583 
584  //--------------------------------
585  //---- Input / Output
586  //--------------------------------
587 
588 
592 
595  void display(std::ostream& out,
596  int precision=0,
597  int width=0,
598  int screenwidth=0
599  ) const;
600 
603  void mfile_output(std::ostream& out,
604  int precision=16,
605  int width=0
606  ) const;
607 
609 
610 
614 
616  friend std::ostream& operator<<(std::ostream& o,const Symmatrix &A);
617 
619  friend std::istream& operator>>(std::istream& i,Symmatrix &A);
620 
622 
623 
624 };
625 
626 
628 
629 // **************************************************************************
630 // make non inline friends available outside
631 // **************************************************************************
632 
634 Matrix diag(const Symmatrix& A); //=(A(1,1),A(2,2),...)^t
635 
637 Symmatrix Diag(const Matrix& A); //vec(A) on the diagonal
638 
640 Symmatrix& rankadd(const Matrix& A,Symmatrix& C,
641  Real alpha=1.,Real beta=0.,int trans=0);
642 
644 Symmatrix& scaledrankadd(const Matrix& A,const Matrix& D,Symmatrix& C,
645  Real alpha=1.,Real beta=0.,int trans=0);
646 
648 Symmatrix& rank2add(const Matrix& A, const Matrix& B, Symmatrix& C,
649  Real alpha=1.,Real beta=0.,int trans=0);
650 
652 Matrix& genmult(const Symmatrix& A,const Matrix& B,Matrix& C,
653  Real alpha=1., Real beta=0., int btrans=0);
654 
656 Matrix& genmult(const Matrix& A,const Symmatrix& B,Matrix& C,
657  Real alpha=1., Real beta=0., int atrans=0);
658 
660 Matrix& genmult(const Symmatrix& A,const Sparsemat& B,Matrix& C,
661  Real alpha=1.,Real beta=0., int btrans=0);
662 
664 Matrix& genmult(const Sparsemat& A,const Symmatrix& B,Matrix& C,
665  Real alpha=1.,Real beta=0., int atrans=0);
666 
668 Symmatrix& rankadd(const Sparsemat& A,Symmatrix& C,
669  Real alpha=1.,Real beta=0.,int trans=0);
670 
672 Symmatrix& scaledrankadd(const Sparsemat& A,const Matrix& D,Symmatrix& C,
673  Real alpha=1.,Real beta=0.,int trans=0);
674 
676 Symmatrix& rank2add(const Sparsemat& A,const Matrix& B,Symmatrix& C,
677  Real alpha=1.,Real beta=0.,int trans=0);
678 
680 Symmatrix abs(const Symmatrix& A);
681 
683 Real trace(const Symmatrix& A); //=sum(diag(A))
684 
686 Real ip(const Symmatrix& A, const Symmatrix& B); //=trace(B^t*A)
687 
689 Real ip(const Matrix& A, const Symmatrix& B); //=trace(B^t*A)
690 
692 Real ip(const Symmatrix& A, const Matrix& B); //=trace(B^t*A)
693 
695 Matrix sumrows(const Symmatrix& A); //=(1 1 1 ... 1)*A
696 
698 Matrix sumcols(const Symmatrix& A); //=A*(1 1 ... 1)^t
699 
701 Real sum(const Symmatrix& A); //=(1 1 ... 1)*A*(1 1 ... 1)^t
702 
703 
705  void svec(const Symmatrix& A,Matrix& sv,Real a=1.,bool add=false,Integer startindex_vec=-1,Integer startindex_A=0,Integer blockdim=-1); //stores svec in sv
706 
708 void sveci(const Matrix& sv,Symmatrix& A,Real a=1.,bool add=false,Integer startindex_vec=0,Integer startindex_A=-1,Integer blockdim=-1);
709 
711 void skron(const Symmatrix& A,const Symmatrix& B,Symmatrix& S,Real alpha=1.,bool add=false,Integer startindex_S=-1);
712 
714 void symscale(const Symmatrix &A,const Matrix& B,Symmatrix& S,Real alpha=1.,Real beta=0.,int btrans=0);
715 
717 Matrix minrows(const Symmatrix& A); //minimum of each column (over rows)
718 
720 Matrix mincols(const Symmatrix& A); //minimum of each row (over colums)
721 
723 Real min(const Symmatrix& A); //minimum of matrix
724 
726 Matrix maxrows(const Symmatrix& A); //similar
727 
729 Matrix maxcols(const Symmatrix& A); //similar
730 
732 Real max(const Symmatrix& A); //similar
733 
735  std::ostream& operator<<(std::ostream& o,const Symmatrix &A);
736 
738 std::istream& operator>>(std::istream& i,Symmatrix &A);
739 
740 
741 // **************************************************************************
742 // implemenation of inline functions
743 // **************************************************************************
744 
746 {
747  nr=0;mem_dim=0;m=0;
748 #if (CONICBUNDLE_DEBUG>=1)
749  is_init=true;
750 #endif
751 }
752 
753 inline Symmatrix& Symmatrix::init(const Symmatrix& A,double d)
754 { return xeya(A,d); }
755 
756 inline Symmatrix& Symmatrix::init(const Matrix& A,double d)
757 { return xeya(A,d); }
758 
759 inline Symmatrix& Symmatrix::init(const Indexmatrix& A,double d)
760 { return xeya(A,d); }
761 
763 {
764  newsize(inr);
765  mat_xea((nr*(nr+1))/2,m,d);
766  chk_set_init(*this,1);
767  return *this;
768 }
769 
770 inline Symmatrix& Symmatrix::init(Integer inr,const Real* pd)
771 {
772  newsize(inr);
773  mat_xey((nr*(nr+1))/2,m,pd);
774  chk_set_init(*this,1);
775  return *this;
776 }
777 
779 { init_to_zero(); }
780 
781 inline Symmatrix::Symmatrix(const Symmatrix& A,double d):Memarrayuser()
782 {init_to_zero(); xeya(A,d);}
783 
784 inline Symmatrix::Symmatrix(const Matrix &M,double d)
785 { init_to_zero(); xeya(M,d); }
786 
787 inline Symmatrix::Symmatrix(const Indexmatrix &M,double d)
788 { init_to_zero(); xeya(M,d); }
789 
791 { init_to_zero(); newsize(i); }
792 
794 { init_to_zero(); init(i,d); }
795 
796 inline Symmatrix::Symmatrix(Integer i,const Real *pd)
797 { init_to_zero(); init(i,pd); }
798 
799 inline Symmatrix::~Symmatrix()
800 {memarray->free(m);}
801 
803 {
804  chk_range(i,j,nr,nr);
805  //if (j>=i) return m[((i*((nr<<1)-(i+1)))>>1)+j];
806  //return m[((j*((nr<<1)-(j+1)))>>1)+i];
807  //now unsigned is used to avoid "cc1plus: warning: assuming signed overflow does not occur when assuming that (X + c) < X is always false [-Wstrict-overflow]"
808  if (unsigned(j)>=unsigned(i)) return m[((unsigned(i)*((unsigned(nr)<<1)-(unsigned(i)+1)))>>1)+unsigned(j)];
809  return m[((unsigned(j)*((unsigned(nr)<<1)-(unsigned(j)+1)))>>1)+unsigned(i)];
810 }
811 
813 {
814  chk_range(i,j,nr,nr);
815  if (unsigned(j)>=unsigned(i)) return m[((unsigned(i)*((unsigned(nr)<<1)-(unsigned(i)+1)))>>1)+unsigned(j)];
816  return m[((unsigned(j)*((unsigned(nr)<<1)-(unsigned(j)+1)))>>1)+unsigned(i)];
817 }
818 
820 {
821  chk_range(i,0,nr*nr,1);
822  // Integer j=i/nr;
823  // i=i%nr;
824  // if (j>=i) return m[((i*((nr<<1)-(i+1)))>>1)+j];
825  // return m[((j*((nr<<1)-(j+1)))>>1)+i];
826  const unsigned long uj=unsigned(i/nr);
827  const unsigned long ui=unsigned(i%nr);
828  if (uj>=ui) return m[((ui*((unsigned(nr)<<1)-(ui+1)))>>1)+uj];
829  return m[((uj*((unsigned(nr)<<1)-(uj+1)))>>1)+ui];
830 }
831 
833 {
834  chk_range(i,0,nr*nr,1);
835  const unsigned long uj=unsigned(i/nr);
836  const unsigned long ui=unsigned(i%nr);
837  if (uj>=ui) return m[((ui*((unsigned(nr)<<1)-(ui+1)))>>1)+uj];
838  return m[((uj*((unsigned(nr)<<1)-(uj+1)))>>1)+ui];
839 }
840 
842 {
843  Symmatrix S;
844  principal_submatrix(ind,S);
845  return S;
846 }
847 
849 inline void swap(Symmatrix &A,Symmatrix &B)
850 {
851  Real *hm=A.m;A.m=B.m;B.m=hm;
852  Integer hi=A.nr;A.nr=B.nr;B.nr=hi;
853  hi=A.mem_dim;A.mem_dim=B.mem_dim;B.mem_dim=hi;
854 #if (CONICBUNDLE_DEBUG>=1)
855  bool hb=A.is_init;A.is_init=B.is_init;B.is_init=hb;
856 #endif
857 }
858 
860 inline Symmatrix& xbpeya(Symmatrix& x,const Symmatrix& y,Real alpha=1.,Real beta=0.)
861  //returns x= alpha*y+beta*x, where y may be transposed (ytrans=1)
862  //if beta==0. then x is initialized to the correct size
863 {
864  if (beta==0.){
865  chk_init(y);
866  x.init(y,alpha);
867  }
868  else {
869  chk_add(x,y);
870  mat_xbpeya((x.rowdim()*(x.rowdim()+1))/2,
871  x.get_store(),y.get_store(),alpha,beta);
872  }
873  return x;
874 }
875 
877 inline Symmatrix& xeyapzb(Symmatrix& x,const Symmatrix& y,const Symmatrix& z,Real alpha=1.,Real beta=1.)
878  //returns x= alpha*y+beta*z
879  //x is initialized to the correct size
880 {
881  chk_add(y,z);
882  x.newsize(y.rowdim()); chk_set_init(x,1);
883  mat_xeyapzb((x.rowdim()*(x.rowdim()+1))/2,
884  x.get_store(),y.get_store(),z.get_store(),alpha,beta);
885  return x;
886 }
887 
888 inline Symmatrix& Symmatrix::operator=(const Symmatrix &A)
889  {return xeya(A);}
890 inline Symmatrix& Symmatrix::operator+=(const Symmatrix &A)
891  {return xpeya(A);}
892 inline Symmatrix& Symmatrix::operator-=(const Symmatrix &A)
893  {return xpeya(A,-1.);}
895  { chk_add(*this,A); mat_xhadey((nr*(nr+1))/2,m,A.m); return *this; }
896 inline Symmatrix Symmatrix::operator-() const
897  {return Symmatrix(*this,-1.);}
898 
899 inline Symmatrix& Symmatrix::operator*=(Real d)
900  {chk_init(*this); mat_xmultea(nr*(nr+1)/2,m,d); return *this;}
902  {chk_init(*this); mat_xmultea(nr*(nr+1)/2,m,1./d); return *this;}
903 inline Symmatrix& Symmatrix::operator+=(Real d)
904  {chk_init(*this); mat_xpea(nr*(nr+1)/2,m,d); return *this;}
905 inline Symmatrix& Symmatrix::operator-=(Real d)
906  {chk_init(*this); mat_xpea(nr*(nr+1)/2,m,-d); return *this;}
907 
909 inline Matrix operator*(const Symmatrix &A,const Symmatrix &B)
910  {Matrix C;return genmult(Matrix(A),B,C);}
911 
913 inline Symmatrix operator%(const Symmatrix &A,const Symmatrix &B)
914  {Symmatrix C(A);C%=B;return C;}
915 
917 inline Symmatrix operator+(const Symmatrix &A,const Symmatrix &B)
918  {Symmatrix C(A);return C.xpeya(B);}
919 
921 inline Symmatrix operator-(const Symmatrix &A,const Symmatrix &B)
922  {Symmatrix C(A);return C.xpeya(B,-1.);}
923 
925 inline Matrix operator*(const Symmatrix &A,const Matrix &B)
926  {Matrix C; return genmult(A,B,C);}
927 
929 inline Matrix operator*(const Matrix &A,const Symmatrix &B)
930  {Matrix C; return genmult(A,B,C);}
931 
933 inline Matrix operator+(const Symmatrix &A,const Matrix &B)
934  {Matrix C(A);return C.xpeya(B);}
935 
937 inline Matrix operator+(const Matrix &A,const Symmatrix &B)
938  {Matrix C(A);return C.xpeya(B);}
939 
941 inline Matrix operator-(const Symmatrix &A,const Matrix &B)
942  {Matrix C(A);return C.xpeya(B,-1);}
943 
945 inline Matrix operator-(const Matrix &A,const Symmatrix &B)
946  {Matrix C(A);return C.xpeya(B,-1);}
947 
949 inline Symmatrix operator*(const Symmatrix& A,Real d)
950  { return Symmatrix(A,d);}
951 
953 inline Symmatrix operator*(Real d,const Symmatrix &A)
954  { return Symmatrix(A,d);}
955 
957 inline Symmatrix operator/(const Symmatrix& A,Real d)
958  { return Symmatrix(A,1./d);}
959 
961 inline Symmatrix operator+(const Symmatrix& A,Real d)
962  { Symmatrix B(A); return B+=d;}
963 
965 inline Symmatrix operator+(Real d,const Symmatrix &A)
966  {Symmatrix B(A); return B+=d;}
967 
969 inline Symmatrix operator-(const Symmatrix& A,Real d)
970  { Symmatrix B(A); return B-=d;}
971 
973 inline Symmatrix operator-(Real d,const Symmatrix &A)
974  { Symmatrix B(A,-1.);return B+=d;}
975 
976 
978 inline Matrix svec(const Symmatrix& A) //=(A(11),sqrt(2)A(12),...A(22)..)^t
979  {Matrix sv;svec(A,sv);return sv;}
980 
982 inline Symmatrix skron(const Symmatrix& A,const Symmatrix& B,Real alpha=1.,bool add=false,Integer startindex_S=-1)
983 {Symmatrix S;skron(A,B,S,alpha,add,startindex_S);return S;}
984 
986 inline Real norm2(const Symmatrix& A)
987  {return ::sqrt(ip(A,A));}
988 
990 inline Symmatrix transpose(const Symmatrix& A)
991  {return Symmatrix(A);}
992 
993 
994 inline Matrix::Matrix(const Symmatrix &A,Real d)
995 
996  {init_to_zero(); xeya(A,d);}
997 
998  inline Matrix& Matrix::init(const Symmatrix &A,Real d)
999 
1000  { return xeya(A,d); }
1001 
1002  inline Matrix& Matrix::operator=(const Symmatrix &A)
1003 
1004  { return xeya(A); }
1005 
1006  inline Matrix& Matrix::operator*=(const Symmatrix &A)
1007 
1008  { Matrix C; return *this=genmult(*this,A,C);}
1009 
1010  inline Matrix& Matrix::operator+=(const Symmatrix &A)
1011 
1012  { return xpeya(A); }
1013 
1014  inline Matrix& Matrix::operator-=(const Symmatrix &A)
1015 
1016  { return xpeya(A,-1.); }
1017 
1018 
1019 }
1020 
1021 #ifndef CH_MATRIX_CLASSES__SPARSMAT_HXX
1022 #include "sparsmat.hxx"
1023 #endif
1024 
1025 #endif
1026 
#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
Integer tred2(Integer nm, Integer n, Real *a, Real *d, Real *e, Real *z) const
a subroutine needed internally for eigenvalue computations (eigval.cxx)
static const Mtype mtype
used for MatrixError templates (runtime type information was not yet existing)
Definition: symmat.hxx:51
friend Matrix diag(const Symmatrix &A)
returns a column vector v consisting of the elements v(i)=(*this)(i,i), 0<=i<row dimension ...
friend Real trace(const Symmatrix &A)
returns the sum of the diagonal elements A(i,i) over all i
void set_init(bool)
after external initialization, call matrix.set_init(true) (not needed if CONICBUNDLE_DEBUG is undefin...
Definition: symmat.hxx:102
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
Real * m
lower triangle stored columnwise (a11,a21,...,anr1,a22,.....)
Definition: symmat.hxx:54
friend Real norm2(const Symmatrix &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: symmat.hxx:986
friend Real min(const Symmatrix &A)
returns the minimum value over all elements of the matrix
Matrix & xpeya(const Matrix &A, Real d=1.)
sets *this+=d*A and returns *this
friend Real sum(const Symmatrix &A)
returns the sum over all elements of A, i.e., (1 1 ... 1)*A*(1 1 ... 1)^T
int Aasen_factor(Indexmatrix &piv)
computes Aasen factorization LTL^T with pivoting, where L is unit lower triangular with first colum e...
friend Matrix sumrows(const Symmatrix &A)
returns a row vector holding the sum over all rows, i.e., (1 1 ... 1)*A
friend Symmatrix abs(const Symmatrix &A)
returns a Symmatrix with elements abs(A(i,j))
double Real
all real numbers in calculations are of this type
Definition: matop.hxx:50
Symmatrix & xpetriu_yza(const Matrix &A, const Matrix &B, Real d=1.)
adds to *this(i,j), i<=j the upper triangle of the matrix product d*transpose(A)*B ...
int Chol_solve(Matrix &x) const
computes, after Chol_factor was executed succesfully, the solution to (*old_this)x=rhs; rhs is overwr...
Integer imtql2(Integer nm, Integer n, Real *d, Real *e, Real *z) const
a subroutine needed internally for eigenvalue computations (eigval.cxx)
friend std::istream & operator>>(std::istream &i, Symmatrix &A)
input format (nr and nc are Integer values, all others Real values): nr nc \n A(1,1) A(1,2) ... A(1,nc) \n A(2,1) ... A(nr,nc) \n
Real * get_store()
returns the current address of the internal value array; use cautiously, do not use delete! ...
Definition: symmat.hxx:224
int Chol_Ltsolve(Matrix &rhs) const
computes, after Chol_factor into LL^T was executed succesfully, the solution to L^Tx=rhs; rhs is over...
int Chol_factor(Real tol=1e-10)
computes the Cholesky factorization, for positive definite matrices only, (*this) is overwritten by t...
Matrix row(Integer i) const
returns row i copied to a new Matrix
friend Symmatrix operator%(const Symmatrix &A, const Symmatrix &B)
ATTENTION: this is redefined as the Hadamard product and sets (i,j)=A(i,j)*B(i,j) for all i<=j...
Definition: symmat.hxx:913
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
Matrix class for integral values of type Integer
Definition: indexmat.hxx:195
Symmatrix & init(const Symmatrix &A, double d=1.)
initialize to *this=A*d
Definition: symmat.hxx:753
Symmatrix & operator%=(const Symmatrix &A)
ATTENTION: this is redefined as the Hadamard product, (*this)(i,j)=(*this)(i,j)*A(i,j) for all i<=j.
Definition: symmat.hxx:894
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
Mtype
serves for specifying the source (matrix class or function) of the error
Definition: matop.hxx:1585
Matrix col(Integer i) const
returns column i copied to a new Matrix
friend Real ip(const Symmatrix &A, const Symmatrix &B)
returns the usual inner product of A and B, i.e., the sum of A(i,j)*B(i,j) over all i...
Symmatrix & operator/=(Real d)
ATTENTION: d is NOT checked for 0.
Definition: symmat.hxx:901
int Chol_Lmult(Matrix &rhs) const
computes, after Chol_factor into LL^T was executed succesfully, L*rhs, overwriting rhs by the result;...
void init_to_zero()
initialize the matrix to a 0x0 matrix without storage
Definition: symmat.hxx:745
friend Symmatrix & xeyapzb(Symmatrix &x, const Symmatrix &y, const Symmatrix &z, Real alpha, Real beta)
returns x= alpha*y+beta*z; x is initialized to the correct size
Definition: symmat.hxx:877
void display(std::ostream &out, int precision=0, int width=0, int screenwidth=0) const
displays a matrix in a pretty way for bounded screen widths; for variables of value zero default valu...
Integer eig(Matrix &P, Matrix &d, bool sort_non_decreasingly=true) const
computes an eigenvalue decomposition P*Diag(d)*tranpose(P)=(*this) by symmetric QR; returns 0 on succ...
friend Matrix sumcols(const Symmatrix &A)
returns a column vector holding the sum over all columns, i.e., A*(1 1 ... 1)^T
Symmatrix operator-(Real d, const Symmatrix &A)
returns a Symmatrix that equals d-A (each element subtracted from d)
Definition: symmat.hxx:973
Integer mem_dim
amount of memory currently allocated
Definition: symmat.hxx:52
friend void swap(Symmatrix &A, Symmatrix &B)
swap the content of the two matrices A and B (involves no copying)
Definition: symmat.hxx:849
Symmatrix & xetriu_yza(const Matrix &A, const Matrix &B, Real d=1.)
sets *this(i,j), i<=j to the upper triangle of the matrix product d*transpose(A)*B ...
void dim(Integer &_nr, Integer &_nc) const
returns the number of rows in _nr and _nc
Definition: symmat.hxx:153
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 ...
friend void symscale(const Symmatrix &A, const Matrix &B, Symmatrix &S, Real a, Real b, int btrans)
sets S=beta*S+alpha*B&#39;*A*B for symmatrix A and matrix B
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
int Chol_inverse(Symmatrix &S) const
computes, after Chol_factor was executed succesfully, the inverse to (*old_this) and stores it in S (...
Symmatrix & pivot_permute(const Indexmatrix &piv, bool inverse=false)
for i=0 to rowdim row (and column) i of this matrix is swapped with row piv(j); for inverse=true the ...
Matrix class of symmetric matrices with real values of type Real
Definition: symmat.hxx:43
Integer rowdim() const
returns the row dimension
Definition: symmat.hxx:159
friend Matrix maxrows(const Symmatrix &A)
returns a row vector holding in each column the maximum over all rows in this column ...
Symmatrix & swapij(Integer i, Integer j)
swaps rows (and columns) i and j
Symmatrix & principal_submatrix(const Indexmatrix &ind, Symmatrix &S) const
returns S and in S the principal submatrix indexed by ind (multiple indices are allowed) ...
const Real * get_store() const
returns the current address of the internal value array; use cautiously!
Definition: symmat.hxx:226
static Memarray * memarray
pointer to common memory manager for all Memarrayusers, instantiated in memarray.cxx ...
Definition: memarray.hxx:121
friend Matrix minrows(const Symmatrix &A)
returns a row vector holding in each column the minimum over all rows in this column ...
int free(void *addr)
free the array pointed to by addr (addr must be an address returned by get)
int LDLfactor(Real tol=1e-10)
computes LDLfactorization (implemented only for positive definite matrices so far, no pivoting), (*this) is overwritten by the factorization; returns 1 if diagonal elements go below tol
friend Symmatrix & rankadd(const Matrix &A, Symmatrix &C, Real alpha, Real beta, int trans)
returns C=beta*C+alpha* A*A^T, where A may be transposed. If beta==0. then C is initiliazed to the co...
Symmatrix()
empty matrix
Definition: symmat.hxx:778
friend Symmatrix & scaledrankadd(const Matrix &A, const Matrix &D, Symmatrix &C, Real alpha, Real beta, int trans)
returns C=beta*C+alpha* A*D*A^T, where D is a vector representing a diagonal matrix and A may be tran...
Mtype get_mtype() const
returns the type of the matrix, MTsymmetric
Definition: symmat.hxx:165
Symmatrix & xeya(const Symmatrix &A, Real d=1.)
sets *this=d*A and returns *this
friend void svec(const Symmatrix &A, Matrix &v, Real a, bool add, Integer startindex_vec, Integer startindex_A, Integer blockdim)
the symmetric vec operator stacks the lower triangle of A to a n*(n+1)/2 vector with the same norm2 a...
friend Symmatrix Diag(const Matrix &A)
returns a symmetric diagonal matrix S of order A.dim() with vec(A) on the diagonal, i.e., S(i,i)=A(i) for all i and S(i,j)=0 for i!=j
friend Matrix maxcols(const Symmatrix &A)
returns a column vector holding in each row the maximum over all columns in this row ...
int LDLinverse(Symmatrix &S) const
computes, after LDLfactor was executed succesfully, the inverse to (*old_this) and stores it in S (nu...
friend Matrix & genmult(const Symmatrix &A, const Matrix &B, Matrix &C, Real alpha, Real beta, int btrans)
returns C=beta*C+alpha*A*B, where B may be transposed; C must not be equal to B; if beta==0...
int LDLsolve(Matrix &x) const
computes, after LDLfactor was executed succesfully, the solution to (*old_this)x=rhs; rhs is overwrit...
Matrix Classes and Linear Algebra. See Matrix Classes (namespace CH_Matrix_Classes) for a quick intro...
Definition: PSCOracle.hxx:20
Matrix class of symmetric matrices with real values of type Real
Definition: sparssym.hxx:89
friend Symmatrix & rank2add(const Matrix &A, const Matrix &B, Symmatrix &C, Real alpha, Real beta, int trans)
returns C=beta*C+alpha*(A*B^T+B*A^T)/2 [or for transposed (A^T*B+B^T*A)/2]. If beta==0. then C is initiliazed to the correct size.
friend void sveci(const Matrix &v, Symmatrix &A, Real a, bool add, Integer startindex_vec, Integer startindex_A, Integer blockdim)
the inverse operator to svec, extracts from v at startindex_vec (>=0) the symmetric matrix of blockdi...
Integer nr
number of rows = number of columns
Definition: symmat.hxx:53
#define chk_add(x, y)
CONICBUNDLE_DEBUG being undefined, the template function is removed. Otherwise it would check...
Definition: matop.hxx:1763
int Chol_Ltmult(Matrix &rhs) const
computes, after Chol_factor into LL^T was executed succesfully, L^Trhs, overwriting rhs by the result...
Symmatrix & enlarge_below(Integer addn)
increases the order of the matrix by appending storage for further addn rows and columns (marked as n...
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
int Chol_scaleLi(Symmatrix &S) const
computes, after Chol_factor into LL^T was executed succesfully, L^{-1}SL^{-T} overwriting S ...
Real & operator()(Integer i, Integer j)
returns reference to element (i,j) of the matrix (rowindex i, columnindex j)
Definition: symmat.hxx:802
int Aasen_tridiagsolve(Matrix &x) const
computes, after Aasen_factor into LTL^T was executed, the solution to Tx=rhs; rhs is overwritten by t...
Matrix class for real values of type Real
Definition: matrix.hxx:74
int Chol_Lsolve(Matrix &rhs) const
computes, after Chol_factor into LL^T was executed succesfully, the solution to Lx=rhs; rhs is overwr...
friend Real max(const Symmatrix &A)
returns the maximum value over all elements of the matrix
Matrix class of sparse matrices with real values of type Real
Definition: sparsmat.hxx:74
friend Symmatrix operator/(const Symmatrix &A, Real d)
returns a Symmatrix that equals A/d; ATTENTION: no check against division by zero ...
Definition: symmat.hxx:957
bool get_init() const
returns true if the matrix has been declared initialized (not needed if CONICBUNDLE_DEBUG is undefine...
Definition: symmat.hxx:104
friend std::ostream & operator<<(std::ostream &o, const Symmatrix &A)
output format (nr and nc are Integer values, all others Real values): nr nc \n A(1,1) A(1,2) ... A(1,nc) \n A(2,1) ... A(nr,nc) \n
All derived classes share a common Memarray memory manager, which is generated with the first user an...
Definition: memarray.hxx:117
void mat_xbpeya(Integer len, Val *x, const Val *y, const Val a, const Val b)
Set x[i]=a*y[i]+b*x[i] for len elements of the arrays x and y.
Definition: matop.hxx:628
friend Symmatrix & xbpeya(Symmatrix &x, const Symmatrix &y, Real alpha, Real beta)
returns x= alpha*y+beta*x; if beta==0. then x is initialized to the correct size
Definition: symmat.hxx:860
#define chk_range(i, j, ubi, ubj)
CONICBUNDLE_DEBUG being undefined, the template function is removed. Otherwise it would check...
Definition: matop.hxx:1772
Symmatrix & transpose()
transposes itself (at almost no cost)
Definition: symmat.hxx:322
friend Symmatrix skron(const Symmatrix &A, const Symmatrix &B, Real alpha, bool add, Integer startindex_S)
the symmetric Kronecker product, defined via (A skron B)svec(C)=(BCA&#39;+ACB&#39;)/2; sets or adds (if add==...
Definition: symmat.hxx:982
Symmatrix & shift_diag(Real s)
shifts the diagonal by s, i.e., (*this)(i,i)+=s for all i
#define chk_init(x)
CONICBUNDLE_DEBUG being undefined, the template function is removed. Otherwise it would check...
Definition: matop.hxx:1761
void mat_xeyapzb(Integer len, Val *x, const Val *y, const Val *z, const Val a, const Val b)
Set x[i]=a*y[i]+b*z[i] for len elements of the arrays x, y and z.
Definition: matop.hxx:1024
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
int Aasen_solve(Matrix &x, const Indexmatrix &piv) const
computes, after Aasen_factor into LTL^T was executed, the solution to (*old_this)x=rhs; rhs is overwr...
int Aasen_Ltsolve(Matrix &x) const
computes, after Aasen_factor into LTL^T was executed, the solution to L^Tx=rhs; rhs is overwritten by...
void newsize(Integer n)
resize the matrix to nr x nr elements but WITHOUT initializing the memory
double sqrt(int a)
return sqrt for int a
Definition: mymath.hxx:121
Matrix()
empty matrix
Definition: matrix.hxx:1082
friend Symmatrix operator+(const Symmatrix &A, const Symmatrix &B)
returns a Matrix that equals A+B
Definition: symmat.hxx:917
friend Matrix operator*(const Symmatrix &A, const Symmatrix &B)
returns a Matrix that equals A*B
Definition: symmat.hxx:909
int Chol_scaleLt(Symmatrix &S) const
computes, after Chol_factor into LL^T was executed succesfully, L^TSL overwriting S ...
Symmatrix & delete_principal_submatrix(const Indexmatrix &ind, bool sorted_increasingly=false)
returns this afte deleting the principal submatrix indexed by ind (no repetitions!); ...
Header declaring the class CH_Matrix_Classes::Sparsemat for sparse matrices with Real elements...
bool is_init
flag whether memory is initialized, it is only used if CONICBUNDLE_DEBUG is defined ...
Definition: symmat.hxx:56
Integer dim() const
returns the dimension rows * columns when the matrix is regarded as a vector
Definition: symmat.hxx:156
int Aasen_Lsolve(Matrix &x) const
computes, after Aasen_factor into LTL^T was executed, the solution to Lx=rhs; rhs is overwritten by t...
friend Matrix mincols(const Symmatrix &A)
returns a column vector holding in each row the minimum over all columns in this row ...
Symmatrix & xpeya(const Symmatrix &A, Real d=1.)
sets *this+=d*A and returns *this
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 ...
void mfile_output(std::ostream &out, int precision=16, int width=0) const
outputs a matrix A in the format "[ A(0,1) ... A(0,nc-1)\n ... A(nr-1,nc-1)];\n" so that it can be re...
Integer coldim() const
returns the column dimension
Definition: symmat.hxx:162