ConicBundle
CMgramdense.hxx
Go to the documentation of this file.
1 
2 #ifndef CONICBUNDLE_CMGRAMDENSE_HXX
3 #define CONICBUNDLE_CMGRAMDENSE_HXX
4 
13 #include "Coeffmat.hxx"
14 
15 
16 namespace ConicBundle{
17 
21 
28  class CMgramdense: public Coeffmat
29  {
30  private:
32  bool positive;
33  public:
35  CMgramdense(const CH_Matrix_Classes::Matrix& Ain,bool pos=true,CoeffmatInfo* cip=0)
36  {A=Ain;positive=pos;CM_type=CM_gramdense;infop=cip;}
38  virtual ~CMgramdense(){}
39 
40  //virtual CM_type get_type() const {return CM_type;} //no need to overload
41  //virtual CH_Matrix_Classes::Integer get_userkey() const {return userkey;}
42  //virtual void set_userkey(CH_Matrix_Classes::Integer k) const {userkey=k;}
43 
45  virtual Coeffmat* clone() const
46  { return new CMgramdense(A,positive,ConicBundle::clone(infop)); }
47 
49  virtual CH_Matrix_Classes::Integer dim() const { return A.rowdim(); }
50 
53  {return (positive? 1.:-1.)*CH_Matrix_Classes::mat_ip(A.coldim(),A.get_store()+i,A.rowdim(),A.get_store()+j,A.rowdim());}
54 
57  {if (positive) { CH_Matrix_Classes::rankadd(A,S);} else { CH_Matrix_Classes::rankadd(A,S,-1.);}}
58 
60  virtual CH_Matrix_Classes::Real norm(void) const
62 
64  virtual Coeffmat* subspace(const CH_Matrix_Classes::Matrix& P) const
65  { CH_Matrix_Classes::Matrix B; CH_Matrix_Classes::genmult(P,A,B,1.,0.,1); return new CMgramdense(B,positive,ConicBundle::clone(infop)); }
66 
69  { if (d<0.) {A*=sqrt(-d); positive=!positive;} else {A*=sqrt(d);};
70  if (infop) infop->multiply(d);}
71 
75  else return -CH_Matrix_Classes::ip(CH_Matrix_Classes::genmult(S,A,B),A); }
76 
80  if (positive) return CH_Matrix_Classes::ip(B,B); else return -CH_Matrix_Classes::ip(B,B);}
81 
84  {
85  CH_Matrix_Classes::Matrix B(A.coldim(),P.coldim()); //B=A^T*P
87  for(CH_Matrix_Classes::Integer j=0;j<B.coldim();j++){
88  const CH_Matrix_Classes::Real *pp=P.get_store()+start_row+j*P.rowdim();
89  const CH_Matrix_Classes::Real *ap=A.get_store();
91  for(CH_Matrix_Classes::Integer i=B.rowdim();--i>=0;){
92  (*bp++)=CH_Matrix_Classes::mat_ip(ard,ap,pp);
93  ap+=ard;
94  }
95  }
96  chk_set_init(B,1);
98  if (Lam==0) {
99  trval=CH_Matrix_Classes::ip(B,B);
100  }
101  else {
102  assert(Lam->dim()==P.coldim());
103  const CH_Matrix_Classes::Real *bp=B.get_store();
104  const CH_Matrix_Classes::Real *lp=Lam->get_store();
105  for (CH_Matrix_Classes::Integer i=0;i<B.coldim();i++,bp+=B.rowdim())
106  trval+=(*lp++)*CH_Matrix_Classes::mat_ip(B.rowdim(),bp);
107  }
108  return (positive?trval:-trval);
109  }
110 
113  {if (positive) CH_Matrix_Classes::rankadd(A,S,d,1.); else CH_Matrix_Classes::rankadd(A,S,-d,1.);}
114 
118  if (positive) CH_Matrix_Classes::genmult(A,CH_Matrix_Classes::genmult(A,C,D,1.,0.,1),B,d,1.);
119  else CH_Matrix_Classes::genmult(A,CH_Matrix_Classes::genmult(A,C,D,1.,0.,1),B,-d,1.);}
120 
124  if (positive) CH_Matrix_Classes::genmult(A,CH_Matrix_Classes::genmult(A,C,D,1.,0.,1),B,d,1.);
125  else CH_Matrix_Classes::genmult(A,CH_Matrix_Classes::genmult(A,C,D,1.,0.,1),B,-d,1.);}
126 
129  {
130  CH_Matrix_Classes::Matrix tmp1; CH_Matrix_Classes::genmult(P,A,tmp1,1.,0.,1,0);
131  CH_Matrix_Classes::Matrix tmp2; CH_Matrix_Classes::genmult(A,Q,tmp2,1.,0.,1,0);
132  if (positive) CH_Matrix_Classes::genmult(tmp1,tmp2,R,1.,0.,0,0);
133  else CH_Matrix_Classes::genmult(tmp1,tmp2,R,-1.,0.,0,0);
134  }
135 
138  { return 4*A.rowdim()*A.coldim(); }
139 
141  virtual int dense() const
142  {return 0;}
143 
145  virtual int sparse() const
146  { return 0;}
147 
151  CH_Matrix_Classes::Matrix& /* val */,
152  CH_Matrix_Classes::Real /* d=1. */ )const
153  {return 0;}
154 
156  virtual int support_in(const CH_Matrix_Classes::Sparsesym& /* S */) const
157  {return 0;}
158 
161  {if (positive) return CH_Matrix_Classes::ip(A,S*A); else return -CH_Matrix_Classes::ip(A,S*A);}
162 
166  if (positive) CH_Matrix_Classes::rankadd(B,S); else CH_Matrix_Classes::rankadd(B,S,-1.);
167  }
168 
171  {
172  CH_Matrix_Classes::Matrix B(A.coldim(),P.coldim()); //B=A^T*P
174  for(CH_Matrix_Classes::Integer j=0;j<B.coldim();j++){
175  const CH_Matrix_Classes::Real *pp=P.get_store()+start_row+j*P.rowdim();
176  const CH_Matrix_Classes::Real *ap=A.get_store();
178  for(CH_Matrix_Classes::Integer i=B.rowdim();--i>=0;){
179  (*bp++)=CH_Matrix_Classes::mat_ip(ard,ap,pp);
180  ap+=ard;
181  }
182  }
183  chk_set_init(B,1);
184  if (positive) CH_Matrix_Classes::rankadd(B,S,alpha,1.,1); else CH_Matrix_Classes::rankadd(B,S,-alpha,1.,1);
185  }
186 
189  CH_Matrix_Classes::Real alpha=1.,CH_Matrix_Classes::Real beta=0.,int btrans=0) const
190  {
192  return CH_Matrix_Classes::genmult(A,CH_Matrix_Classes::genmult(A,B,D,1.,0.,1,btrans),C,(positive? 1.:-1.)*alpha,beta);
193  }
194 
197  CH_Matrix_Classes::Real alpha=1.,CH_Matrix_Classes::Real beta=0.,int btrans=0) const
198  {
200  return CH_Matrix_Classes::genmult(CH_Matrix_Classes::genmult(B,A,D,1.,0.,btrans),A,C,(positive? 1.:-1.)*alpha,beta,0,1);
201  }
202 
204  virtual int equal(const Coeffmat* p,double tol=1e-6) const
205  {
206  const CMgramdense *pp=dynamic_cast<const CMgramdense *>(p);
207  if (pp==0)
208  return 0;
209  if ((A.rowdim()!=(pp->A).rowdim())||(A.coldim()!=(pp->A).coldim()))
210  return 0;
211  return (positive==pp->positive)&&(CH_Matrix_Classes::norm2(A-pp->A)<tol);
212  }
213 
215  virtual std::ostream& display(std::ostream& o) const
216  {o<<"CMgramdense\n"; A.display(o); return o;}
217 
219  virtual std::ostream& out(std::ostream& o) const
220  {return o<<"GRAM_DENSE\n"<<positive<<"\n"<<A;}
221 
223  virtual std::istream& in(std::istream& i)
224  {return i>>positive>>A;}
225 
227  CMgramdense(std::istream& is,CoeffmatInfo* cip=0)
228  {CM_type=CM_gramdense;infop=cip;in(is);}
229 
230  //--- specific routines
232  const CH_Matrix_Classes::Matrix& get_A() const {return A;}
234  bool get_positive() const {return positive;}
235  };
236 
238 }
239 #endif
240 
#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
virtual CH_Matrix_Classes::Real ip(const CH_Matrix_Classes::Sparsesym &S) const
returns the inner product of the constraint matrix with S
Definition: CMgramdense.hxx:160
Header declaring the classes ConicBundle::Coeffmat, ConicBundle::CoeffmatPointer, ConicBundle::Coeffm...
virtual void addmeto(CH_Matrix_Classes::Symmatrix &S, CH_Matrix_Classes::Real d=1.) const
computes S+=d*(*this);
Definition: CMgramdense.hxx:112
virtual CH_Matrix_Classes::Real gramip(const CH_Matrix_Classes::Matrix &P) const
returns ip(*this,PP^T)=trace P^T(*this)P
Definition: CMgramdense.hxx:78
virtual CH_Matrix_Classes::Real operator()(CH_Matrix_Classes::Integer i, CH_Matrix_Classes::Integer j) const
returns the value of the matrix element (i,j)
Definition: CMgramdense.hxx:52
Integer rowdim() const
returns the row dimension
Definition: matrix.hxx:215
double Real
all real numbers in calculations are of this type
Definition: matop.hxx:50
allows to memorize the scalings applied to a Coeffmat and offers the basis for storing further user d...
Definition: Coeffmat.hxx:52
virtual const CH_Matrix_Classes::Matrix & postgenmult(const CH_Matrix_Classes::Matrix &B, CH_Matrix_Classes::Matrix &C, CH_Matrix_Classes::Real alpha=1., CH_Matrix_Classes::Real beta=0., int btrans=0) const
computes C= alpha*(*this)*B^(T if btrans) + beta*C, C is also returned
Definition: CMgramdense.hxx:188
virtual int dense() const
returns 1 if its structure is as bad as its dense symmetric representation, otherwise 0 ...
Definition: CMgramdense.hxx:141
virtual std::istream & in(std::istream &i)
counterpart to out(), does not read the class type, though. This is assumed to have been read in orde...
Definition: CMgramdense.hxx:223
virtual void addprodto(CH_Matrix_Classes::Matrix &B, const CH_Matrix_Classes::Matrix &C, CH_Matrix_Classes::Real d=1.) const
computes B+=d*(*this)*C
Definition: CMgramdense.hxx:116
defines a base class for coefficient matrices in semidefinite programming, in particular for use with...
Definition: Coeffmat.hxx:125
virtual void project(CH_Matrix_Classes::Symmatrix &S, const CH_Matrix_Classes::Matrix &P) const
computes S=P^T*(*this)*P
Definition: CMgramdense.hxx:164
Matrix class for integral values of type Integer
Definition: indexmat.hxx:195
bool positive
if true use A*A^T, if false use -A*A^T
Definition: CMgramdense.hxx:32
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
virtual int sparse(CH_Matrix_Classes::Indexmatrix &, CH_Matrix_Classes::Indexmatrix &, CH_Matrix_Classes::Matrix &, CH_Matrix_Classes::Real) const
returns 0 if not sparse. If it is sparse it returns 1 and the nonzero structure in I...
Definition: CMgramdense.hxx:149
virtual CH_Matrix_Classes::Real ip(const CH_Matrix_Classes::Symmatrix &S) const
returns ip(*this,S)=trace(*this*S), the trace inner product
Definition: CMgramdense.hxx:73
virtual Coeffmat * subspace(const CH_Matrix_Classes::Matrix &P) const
delivers a new object on the heap corresponding to the matrix P^T(*this)P, the caller is responsible ...
Definition: CMgramdense.hxx:64
Real * get_store()
returns the current address of the internal value array; use cautiously, do not use delete! ...
Definition: matrix.hxx:326
Matrix class of symmetric matrices with real values of type Real
Definition: symmat.hxx:43
for CMgramdense
Definition: Coeffmat.hxx:36
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...
conic bundle method solver for sum of convex functions. See the ConicBundle_Manual for a quick introd...
Definition: CBSolver.hxx:22
virtual void left_right_prod(const CH_Matrix_Classes::Matrix &P, const CH_Matrix_Classes::Matrix &Q, CH_Matrix_Classes::Matrix &R) const
computes R=P^T*(*this)*Q
Definition: CMgramdense.hxx:128
virtual CH_Matrix_Classes::Integer dim() const
returns the order of the represented symmetric matrix
Definition: CMgramdense.hxx:49
const CH_Matrix_Classes::Matrix & get_A() const
returns the const reference to the internal matrix A forming the Gram matrix
Definition: CMgramdense.hxx:232
virtual int sparse() const
returns 0 if not sparse, otherwise 1
Definition: CMgramdense.hxx:145
virtual void add_projection(CH_Matrix_Classes::Symmatrix &S, const CH_Matrix_Classes::Matrix &P, CH_Matrix_Classes::Real alpha=1., CH_Matrix_Classes::Integer start_row=0) const
computes S+=Q^T(*this)Q for Q=P.rows(start_row,start_row+dim-1)
Definition: CMgramdense.hxx:170
Coeffmattype CM_type
in order to enable type identification
Definition: Coeffmat.hxx:134
CoeffmatInfo * infop
allows the user to specify and output additional information
Definition: Coeffmat.hxx:135
virtual CH_Matrix_Classes::Real gramip(const CH_Matrix_Classes::Matrix &P, CH_Matrix_Classes::Integer start_row, const CH_Matrix_Classes::Matrix *Lam=0) const
returns ip(*this,QQ^T)=trace Q^T(*this)Q for Q=P.rows(start_row,start_row+dim-1)
Definition: CMgramdense.hxx:83
CMgramdense(std::istream &is, CoeffmatInfo *cip=0)
constructor with istream and possibly additional user information
Definition: CMgramdense.hxx:227
virtual CH_Matrix_Classes::Real norm(void) const
returns the Frobenius norm of the matrix
Definition: CMgramdense.hxx:60
virtual int support_in(const CH_Matrix_Classes::Sparsesym &) const
returns 0 if the support of the costraint matrix is not contained in the support of the sparse symmet...
Definition: CMgramdense.hxx:156
Symmatrix & rankadd(const Matrix &A, Symmatrix &C, Real alpha=1., Real beta=0., int trans=0)
returns C=beta*C+alpha* A*A^T, where A may be transposed. If beta==0. then C is initiliazed to the co...
virtual int equal(const Coeffmat *p, double tol=1e-6) const
returns 1, if p is the same derived class and entries differ by less than tol, otherwise zero ...
Definition: CMgramdense.hxx:204
virtual std::ostream & display(std::ostream &o) const
display constraint information
Definition: CMgramdense.hxx:215
Matrix class of symmetric matrices with real values of type Real
Definition: sparssym.hxx:89
void multiply(CH_Matrix_Classes::Real sf)
scales the scale factor
Definition: Coeffmat.hxx:70
Integer coldim() const
returns the column dimension
Definition: matrix.hxx:218
Matrix class for real values of type Real
Definition: matrix.hxx:74
virtual Coeffmat * clone() const
makes an explicit copy of itself and returns a pointer to it
Definition: CMgramdense.hxx:45
virtual void multiply(CH_Matrix_Classes::Real d)
multiply constraint permanentely by d; this is to allow scaling or sign changes in the constraints ...
Definition: CMgramdense.hxx:68
virtual CH_Matrix_Classes::Integer prodvec_flops() const
returns an estimate of number of flops to compute addprodto for a vector
Definition: CMgramdense.hxx:137
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
virtual std::ostream & out(std::ostream &o) const
put entire contents onto ostream with the class type in the beginning so that the derived class can b...
Definition: CMgramdense.hxx:219
implements a Gram matrix as Coeffmat for a dense rectangular CH_Matrix_Classes::Matrix (for use wit...
Definition: CMgramdense.hxx:28
bool get_positive() const
returns the flag on whether the Gram matrix is used in positive or negative form
Definition: CMgramdense.hxx:234
CMgramdense(const CH_Matrix_Classes::Matrix &Ain, bool pos=true, CoeffmatInfo *cip=0)
copy Ain and possibly, the flag for positive/negative and store the user information ...
Definition: CMgramdense.hxx:35
CH_Matrix_Classes::Matrix A
the Coeffmat acts like A*A^T or its negative
Definition: CMgramdense.hxx:31
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=...
virtual const CH_Matrix_Classes::Matrix & pregenmult(const CH_Matrix_Classes::Matrix &B, CH_Matrix_Classes::Matrix &C, CH_Matrix_Classes::Real alpha=1., CH_Matrix_Classes::Real beta=0., int btrans=0) const
computes C= alpha*B^(T if btrans)*(*this) + beta*C, C is also returned
Definition: CMgramdense.hxx:196
virtual void make_symmatrix(CH_Matrix_Classes::Symmatrix &S) const
returns a dense symmetric constraint matrix
Definition: CMgramdense.hxx:56
double sqrt(int a)
return sqrt for int a
Definition: mymath.hxx:121
CoeffmatInfo * clone(const CoeffmatInfo *cip)
if cip is not zero, it calls and returns cip->clone() and 0 otherwise
Definition: Coeffmat.hxx:106
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
virtual void addprodto(CH_Matrix_Classes::Matrix &B, const CH_Matrix_Classes::Sparsemat &C, CH_Matrix_Classes::Real d=1.) const
computes B+=d*(*this)*C
Definition: CMgramdense.hxx:122