ConicBundle
CMgramsparse.hxx
Go to the documentation of this file.
1 
2 
3 #ifndef CONICBUNDLE_CMGRAMSPARSE_HXX
4 #define CONICBUNDLE_CMGRAMSPARSE_HXX
5 
13 #include "Coeffmat.hxx"
14 #include "CMgramdense.hxx"
15 
16 namespace ConicBundle {
17 
18 
22 
30  class CMgramsparse: public Coeffmat
31  {
32  private:
34  bool positive;
35  public:
37  CMgramsparse(const CH_Matrix_Classes::Sparsemat& Ain,bool pos=true,CoeffmatInfo* cip=0)
38  {A=Ain;positive=pos;CM_type=CM_gramsparse;infop=cip;}
40  virtual ~CMgramsparse(){}
41 
43  virtual Coeffmat* clone() const
44  { return new CMgramsparse(A,positive,ConicBundle::clone(infop));}
45 
47  virtual CH_Matrix_Classes::Integer dim() const { return A.rowdim(); }
48 
51  {return (positive? 1.:-1.)*CH_Matrix_Classes::ip(A.row(i),A.row(j));}
52 
55  {if (positive) { CH_Matrix_Classes::rankadd(A,S);}
56  else { CH_Matrix_Classes::rankadd(A,S,-1.);}}
57 
59  virtual CH_Matrix_Classes::Real norm(void) const
61 
63  virtual Coeffmat* subspace(const CH_Matrix_Classes::Matrix& P) const
65 
68  { if (d<0.) {A*=sqrt(-d); positive=!positive;} else {A*=sqrt(d);}
69  if (infop) infop->multiply(d);}
70 
74  else return -CH_Matrix_Classes::ip(CH_Matrix_Classes::genmult(S,A,B),A); }
75 
79  if (positive) return CH_Matrix_Classes::ip(B,B); else return -CH_Matrix_Classes::ip(B,B);}
80 
83  {
86  const CH_Matrix_Classes::Integer brd=B.rowdim();
87  const CH_Matrix_Classes::Integer pcd=P.coldim();
88  const CH_Matrix_Classes::Integer prd=P.rowdim();
89  const CH_Matrix_Classes::Real *pp=P.get_store()+start_row;
91  const CH_Matrix_Classes::Real *vp=(A.get_colval()).get_store();
92  const CH_Matrix_Classes::Integer *ip=(A.get_colindex()).get_store();
93  for(CH_Matrix_Classes::Integer j=0;j<colinfo.rowdim();j++){
94  CH_Matrix_Classes::Integer indj=colinfo(j,0);
95  for(CH_Matrix_Classes::Integer i=colinfo(j,1);--i>=0;){
96  CH_Matrix_Classes::mat_xpeya(pcd,bp+indj,brd,pp+(*ip++),prd,(*vp++));
97  }
98  }
100  if (Lam==0) {
101  trval=CH_Matrix_Classes::ip(B,B);
102  }
103  else {
104  assert(Lam->dim()==P.coldim());
105  const CH_Matrix_Classes::Real *bp=B.get_store();
106  const CH_Matrix_Classes::Real *lp=Lam->get_store();
107  for (CH_Matrix_Classes::Integer i=0;i<B.coldim();i++,bp+=B.rowdim())
108  trval+=(*lp++)*CH_Matrix_Classes::mat_ip(B.rowdim(),bp);
109  }
110  return (positive?trval:-trval);
111  }
112 
115  {if (positive) CH_Matrix_Classes::rankadd(A,S,d,1.); else CH_Matrix_Classes::rankadd(A,S,-d,1.);}
116 
120  if (positive) CH_Matrix_Classes::genmult(A,CH_Matrix_Classes::genmult(A,C,D,1.,0.,1),B,d,1.);
121  else CH_Matrix_Classes::genmult(A,CH_Matrix_Classes::genmult(A,C,D,1.,0.,1),B,-d,1.);}
122 
126  if (positive) CH_Matrix_Classes::genmult(A,CH_Matrix_Classes::genmult(A,C,D,1.,0.,1),B,d,1.);
127  else CH_Matrix_Classes::genmult(A,CH_Matrix_Classes::genmult(A,C,D,1.,0.,1),B,-d,1.);}
128 
131  {
132  CH_Matrix_Classes::Matrix tmp1; CH_Matrix_Classes::genmult(P,A,tmp1,1.,0.,1,0);
133  CH_Matrix_Classes::Matrix tmp2; CH_Matrix_Classes::genmult(A,Q,tmp2,1.,0.,1,0);
134  if (positive) CH_Matrix_Classes::genmult(tmp1,tmp2,R,1.,0.,0,0);
135  else CH_Matrix_Classes::genmult(tmp1,tmp2,R,-1.,0.,0,0);
136  }
137 
140  { return 4*A.nonzeros(); }
141 
143  virtual int dense() const
144  {return 0;}
145 
147  virtual int sparse() const
148  { return 0;}
149 
153  CH_Matrix_Classes::Matrix& /* val */,
154  CH_Matrix_Classes::Real /* d=1. */)const
155  {return 0;}
156 
158  virtual int support_in(const CH_Matrix_Classes::Sparsesym& /* S */) const
159  {return 0;}
160 
163  {if (positive) return CH_Matrix_Classes::ip(A,S*A); else return -CH_Matrix_Classes::ip(A,S*A); }
164 
168  if (positive) CH_Matrix_Classes::rankadd(B,S); else CH_Matrix_Classes::rankadd(B,S,-1.);
169  }
170 
173  {
176  const CH_Matrix_Classes::Integer brd=B.rowdim();
177  const CH_Matrix_Classes::Integer pcd=P.coldim();
178  const CH_Matrix_Classes::Integer prd=P.rowdim();
179  const CH_Matrix_Classes::Real *pp=P.get_store()+start_row;
180  const CH_Matrix_Classes::Indexmatrix &colinfo=A.get_colinfo();
181  const CH_Matrix_Classes::Real *vp=(A.get_colval()).get_store();
182  const CH_Matrix_Classes::Integer *ip=(A.get_colindex()).get_store();
183  for(CH_Matrix_Classes::Integer j=0;j<colinfo.rowdim();j++){
184  CH_Matrix_Classes::Integer indj=colinfo(j,0);
185  for(CH_Matrix_Classes::Integer i=colinfo(j,1);--i>=0;){
186  CH_Matrix_Classes::mat_xpeya(pcd,bp+indj,brd,pp+(*ip++),prd,(*vp++));
187  }
188  }
189  if (positive) CH_Matrix_Classes::rankadd(B,S,alpha,1.,1); else CH_Matrix_Classes::rankadd(B,S,-alpha,1.,1);
190  }
191 
194  CH_Matrix_Classes::Real alpha=1.,CH_Matrix_Classes::Real beta=0.,int btrans=0) const
195  {
197  return CH_Matrix_Classes::genmult(A,CH_Matrix_Classes::genmult(A,B,D,1.,0.,1,btrans),C,(positive? 1.:-1.)*alpha,beta);
198  }
199 
202  CH_Matrix_Classes::Real alpha=1.,CH_Matrix_Classes::Real beta=0.,int btrans=0) const
203  {
205  return CH_Matrix_Classes::genmult(CH_Matrix_Classes::genmult(B,A,D,1.,0.,btrans),A,C,(positive? 1.:-1.)*alpha,beta,0,1);
206  }
207 
209  virtual int equal(const Coeffmat* p,double tol=1e-6) const
210  {
211  const CMgramsparse *pp=dynamic_cast<const CMgramsparse *>(p);
212  if (pp==0)
213  return 0;
214  return (positive==pp->positive)&&CH_Matrix_Classes::equal(A,pp->A,tol);
215  }
216 
218  virtual std::ostream& display(std::ostream& o) const
219  {o<<"CMgramsparse\n"; A.display(o); return o;}
220 
222  virtual std::ostream& out(std::ostream& o) const
223  {return o<<"GRAM_SPARSE\n"<<positive<<"\n"<<A;}
224 
226  virtual std::istream& in(std::istream& i)
227  {return i>>positive>>A;}
228 
230  CMgramsparse(std::istream& is,CoeffmatInfo* cip=0)
231  {CM_type=CM_gramsparse;infop=cip;in(is);}
232 
233  //--- specific routines
235  const CH_Matrix_Classes::Sparsemat& get_A() const {return A;}
237  bool get_positive() const {return positive;}
238 };
239 
241 
242 }
243 #endif
244 
int Integer
all integer numbers in calculations and indexing are of this type
Definition: matop.hxx:40
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: CMgramsparse.hxx:226
CMgramsparse(const CH_Matrix_Classes::Sparsemat &Ain, bool pos=true, CoeffmatInfo *cip=0)
copy Ain, the flag for positive/negative and store the user information
Definition: CMgramsparse.hxx:37
Header declaring the classes ConicBundle::Coeffmat, ConicBundle::CoeffmatPointer, ConicBundle::Coeffm...
virtual CH_Matrix_Classes::Real ip(const CH_Matrix_Classes::Sparsesym &S) const
returns the inner product of the constraint matrix with S
Definition: CMgramsparse.hxx:162
virtual void project(CH_Matrix_Classes::Symmatrix &S, const CH_Matrix_Classes::Matrix &P) const
computes S=P^T*(*this)*P
Definition: CMgramsparse.hxx:166
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
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: CMgramsparse.hxx:130
allows to memorize the scalings applied to a Coeffmat and offers the basis for storing further user d...
Definition: Coeffmat.hxx:52
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: CMgramsparse.hxx:222
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: CMgramsparse.hxx:124
const Indexmatrix & get_colinfo() const
returns information on nonzero columns, k by 3, listing: index, %nonzeros, first index in colindex/co...
Definition: sparsmat.hxx:265
defines a base class for coefficient matrices in semidefinite programming, in particular for use with...
Definition: Coeffmat.hxx:125
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...
Matrix class for integral values of type Integer
Definition: indexmat.hxx:195
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: CMgramsparse.hxx:193
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
bool get_positive() const
returns the flag on whether the Gram matrix is used in positive or negative form
Definition: CMgramsparse.hxx:237
virtual std::ostream & display(std::ostream &o) const
display constraint information
Definition: CMgramsparse.hxx:218
const Matrix & get_colval() const
returns the value vector of the column representation holding the value for each element ...
Definition: sparsmat.hxx:269
virtual CH_Matrix_Classes::Integer dim() const
returns the order of the represented symmetric matrix
Definition: CMgramsparse.hxx:47
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
bool positive
if true use A*A^T, if false use -A*A^T
Definition: CMgramsparse.hxx:34
implements a Gram matrix as Coeffmat for a sparse rectangular CH_Matrix_Classes::Sparsemat (for use...
Definition: CMgramsparse.hxx:30
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: CMgramsparse.hxx:151
conic bundle method solver for sum of convex functions. See the ConicBundle_Manual for a quick introd...
Definition: CBSolver.hxx:22
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: CMgramsparse.hxx:118
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 ip(const CH_Matrix_Classes::Symmatrix &S) const
returns ip(*this,S)=trace(*this*S), the trace inner product
Definition: CMgramsparse.hxx:72
CMgramsparse(std::istream &is, CoeffmatInfo *cip=0)
constructor with istream and possibly additional user information
Definition: CMgramsparse.hxx:230
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: CMgramsparse.hxx:209
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: CMgramsparse.hxx:82
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
CH_Matrix_Classes::Sparsemat A
the Coeffmat acts like A*A^T or its negative
Definition: CMgramsparse.hxx:33
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: CMgramsparse.hxx:67
Integer coldim() const
returns the column dimension
Definition: matrix.hxx:218
virtual CH_Matrix_Classes::Real gramip(const CH_Matrix_Classes::Matrix &P) const
returns ip(*this,PP^T)=trace P^T(*this)P
Definition: CMgramsparse.hxx:77
for CMgramsparse
Definition: Coeffmat.hxx:37
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: CMgramsparse.hxx:43
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
void mat_xpeya(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:543
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: CMgramsparse.hxx:158
implements a Gram matrix as Coeffmat for a dense rectangular CH_Matrix_Classes::Matrix (for use wit...
Definition: CMgramdense.hxx:28
const Indexmatrix & get_colindex() const
returns the index vector of the column representation holding the row index for each element ...
Definition: sparsmat.hxx:267
Integer rowdim() const
returns the row dimension
Definition: indexmat.hxx:321
const CH_Matrix_Classes::Sparsemat & get_A() const
returns the const reference to the internal matrix A forming the Gram matrix
Definition: CMgramsparse.hxx:235
virtual CH_Matrix_Classes::Integer prodvec_flops() const
returns an estimate of number of flops to compute addprodto for a vector
Definition: CMgramsparse.hxx:139
virtual int sparse() const
returns 0 if not sparse, otherwise 1
Definition: CMgramsparse.hxx:147
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: CMgramsparse.hxx:50
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: CMgramsparse.hxx:63
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=...
Sparsemat row(Integer i) const
returns row i copied to a new sparse matrix
virtual CH_Matrix_Classes::Real norm(void) const
returns the Frobenius norm of the matrix
Definition: CMgramsparse.hxx:59
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
Integer coldim() const
returns the column dimension
Definition: sparsmat.hxx:202
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: CMgramsparse.hxx:201
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: CMgramsparse.hxx:172
virtual void make_symmatrix(CH_Matrix_Classes::Symmatrix &S) const
returns a dense symmetric constraint matrix
Definition: CMgramsparse.hxx:54
Integer rowdim() const
returns the row dimension
Definition: sparsmat.hxx:199
virtual int dense() const
returns 1 if its structure is as bad as its dense symmetric representation, otherwise 0 ...
Definition: CMgramsparse.hxx:143
virtual void addmeto(CH_Matrix_Classes::Symmatrix &S, CH_Matrix_Classes::Real d=1.) const
computes S+=d*(*this);
Definition: CMgramsparse.hxx:114
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
Header declaring the class ConicBundle::CMgramdense (needed for ConicBundle::AffineMatrixFunction) ...
Integer nonzeros() const
returns the number of nonzeros
Definition: sparsmat.hxx:205