ConicBundle
CMsingleton.hxx
Go to the documentation of this file.
1 
2 
3 #ifndef CONICBUNDLE_CMSINGLETON_HXX
4 #define CONICBUNDLE_CMSINGLETON_HXX
5 
14 #include "CMgramdense.hxx"
15 #include "CMlowrankdd.hxx"
16 #include "sparssym.hxx"
17 
18 namespace ConicBundle {
19 
23 
29  class CMsingleton: public Coeffmat
30  {
31  private:
36  public:
39  {nr=innr;ii=ini;jj=inj;val=inval;CM_type=CM_singleton;infop=cip;}
41  virtual ~CMsingleton(){}
42 
44  virtual Coeffmat* clone() const
45  {return new CMsingleton(nr,ii,jj,val,ConicBundle::clone(infop));}
46 
48  virtual CH_Matrix_Classes::Integer dim() const {return nr;}
49 
52  { if (((i==ii)&&(j==jj))||((i==jj)&&(j==ii))) return val; return 0.; }
53 
56  {S.init(nr,0.); S(ii,jj)=val;}
57 
59  virtual CH_Matrix_Classes::Real norm(void) const
60  {if (ii==jj) return fabs(val); return sqrt(2.)*fabs(val);}
61 
63  virtual Coeffmat* subspace(const CH_Matrix_Classes::Matrix& P) const
64  {
65  if (ii==jj){ return new CMgramdense(sqrt(fabs(val))*(P.row(ii)).transpose(),val>0,ConicBundle::clone(infop)); }
66  return new CMlowrankdd((P.row(ii)).transpose()*val,(P.row(jj)).transpose(),ConicBundle::clone(infop));
67  }
68 
71  { val*=d; if (infop) infop->multiply(d);}
72 
75  {if (ii==jj) return val*S(ii,ii); return 2.*val*S(ii,jj);}
76 
79  {
80  if (ii==jj)
81  return val*CH_Matrix_Classes::mat_ip(P.coldim(),P.get_store()+ii,P.rowdim(),P.get_store()+ii,P.rowdim());
82  return 2.*val*CH_Matrix_Classes::mat_ip(P.coldim(),P.get_store()+ii,P.rowdim(),P.get_store()+jj,P.rowdim());
83  }
84 
87  {
88  if (Lam==0){
89  if (ii==jj)
90  return val*CH_Matrix_Classes::mat_ip(P.coldim(),P.get_store()+ii+start_row,P.rowdim(),P.get_store()+ii+start_row,P.rowdim());
91  return 2.*val*CH_Matrix_Classes::mat_ip(P.coldim(),P.get_store()+ii+start_row,P.rowdim(),P.get_store()+jj+start_row,P.rowdim());
92  }
93  else {
94  assert(Lam->dim()==P.coldim());
95  if (ii==jj)
96  return val*CH_Matrix_Classes::mat_ip(P.coldim(),P.get_store()+ii+start_row,P.rowdim(),P.get_store()+ii+start_row,P.rowdim(),Lam->get_store(),1);
97  return 2.*val*CH_Matrix_Classes::mat_ip(P.coldim(),P.get_store()+ii+start_row,P.rowdim(),P.get_store()+jj+start_row,P.rowdim(),Lam->get_store(),1);
98  }
99  }
100 
102  virtual void addmeto(CH_Matrix_Classes::Symmatrix& S,CH_Matrix_Classes::Real d=1.) const {S(ii,jj)+=d*val;}
103 
106  {
108  if (ii==jj) return;
110  }
111 
114  {
115  if (C.coldim()==1){
116  B(ii)+=d*val*C(jj);
117  if (ii!=jj) B(jj)+=d*val*C(ii);
118  return;
119  }
121  for(CH_Matrix_Classes::Integer i=0;i<tmp.nonzeros();i++){
123  tmp.get_edge(i,indi,indj,v);
124  B(ii,indj)+=d*val*v;
125  }
126  if (ii==jj) return;
127  tmp.init(C.row(ii));
128  {for(CH_Matrix_Classes::Integer i=0;i<tmp.nonzeros();i++){
130  tmp.get_edge(i,indi,indj,v);
131  B(jj,indj)+=d*val*v;
132  }}
133  }
134 
137  {
138  if (ii==jj) CH_Matrix_Classes::genmult(P.row(ii),Q.row(ii),R,val,0.,1,0);
139  else {
140  CH_Matrix_Classes::genmult(P.row(ii),Q.row(jj),R,val,0.,1,0);
141  CH_Matrix_Classes::genmult(P.row(jj),Q.row(ii),R,val,1.,1,0);
142  }
143  }
144 
147  { return (ii==jj)?2:4; }
148 
150  virtual int dense() const
151  {return 0;}
152 
154  virtual int sparse() const
155  { return 1;}
156 
159  { I.init(1,1,ii); J.init(1,1,jj); v.init(1,1,val*d); return 1;}
160 
162  virtual int support_in(const CH_Matrix_Classes::Sparsesym& S) const
163  {return S.check_support(ii,jj);}
164 
167  {if (ii==jj) return val*S(ii,jj); return 2.*val*S(ii,jj);}
168 
171  {
172  S.newsize(P.coldim());chk_set_init(S,1);
173  const CH_Matrix_Classes::Real *ap=P.get_store()+ii-nr;
175  if (ii==jj){
176  for(CH_Matrix_Classes::Integer i=P.coldim();--i>=0;){
177  const CH_Matrix_Classes::Real *aap=ap; CH_Matrix_Classes::Real a=*(aap=(ap+=nr));
178  *sp++=a*a*val; a*=val;
179  for(CH_Matrix_Classes::Integer j=i;--j>=0;) *sp++=a*(*(aap+=nr));
180  }
181  return;
182  }
183  const CH_Matrix_Classes::Real *bp=P.get_store()+jj-nr;
184  for(CH_Matrix_Classes::Integer i=P.coldim();--i>=0;){
185  const CH_Matrix_Classes::Real *aap; CH_Matrix_Classes::Real a=*(aap=(ap+=nr))*val;
186  const CH_Matrix_Classes::Real *bbp; CH_Matrix_Classes::Real b=*(bbp=(bp+=nr));
187  *sp++=2.*a*b; b*=val;
188  for(CH_Matrix_Classes::Integer j=i;--j>=0;) *sp++=a*(*(bbp+=nr))+b*(*(aap+=nr));
189  }
190  }
191 
194  {
196  const CH_Matrix_Classes::Real *ap=P.get_store()+start_row+ii-pnr;
198  const CH_Matrix_Classes::Real alval=alpha*val;
199  if (ii==jj){
200  for(CH_Matrix_Classes::Integer i=P.coldim();--i>=0;){
201  const CH_Matrix_Classes::Real *aap=ap;
202  CH_Matrix_Classes::Real a=*(aap=(ap+=pnr));
203  (*sp++)+=a*a*alval; a*=alval;
204  for(CH_Matrix_Classes::Integer j=i;--j>=0;) (*sp++)+=a*(*(aap+=pnr));
205  }
206  return;
207  }
208  const CH_Matrix_Classes::Real *bp=P.get_store()+start_row+jj-pnr;
209  for(CH_Matrix_Classes::Integer i=P.coldim();--i>=0;){
210  const CH_Matrix_Classes::Real *aap;
211  CH_Matrix_Classes::Real a=*(aap=(ap+=pnr))*alval;
212  const CH_Matrix_Classes::Real *bbp;
213  CH_Matrix_Classes::Real b=*(bbp=(bp+=pnr));
214  (*sp++)+=2.*a*b; b*=alval;
215  for(CH_Matrix_Classes::Integer j=i;--j>=0;) (*sp++)+=a*(*(bbp+=pnr))+b*(*(aap+=pnr));
216  }
217  }
218 
221  CH_Matrix_Classes::Real alpha=1.,CH_Matrix_Classes::Real beta=0.,int btrans=0) const
222  {
223  CH_Matrix_Classes::Sparsesym S(nr,1,&ii,&jj,&val);
224  return CH_Matrix_Classes::genmult(S,B,C,alpha,beta,btrans);
225  }
226 
229  CH_Matrix_Classes::Real alpha=1.,CH_Matrix_Classes::Real beta=0.,int btrans=0) const
230  {
231  CH_Matrix_Classes::Sparsesym S(nr,1,&ii,&jj,&val);
233  return CH_Matrix_Classes::genmult(B,S,C,alpha,beta,btrans);
234  }
235 
237  virtual int equal(const Coeffmat* p,double tol=1e-6) const
238  {
239  const CMsingleton *pp=dynamic_cast<const CMsingleton *>(p);
240  if (pp==0)
241  return 0;
242  return ((nr==pp->nr)&&(ii==pp->ii)&&(jj==pp->jj)
243  &&(fabs(val-pp->val)<tol));
244  }
245 
247  virtual std::ostream& display(std::ostream& o) const
248  {o<<"CMsingleton\n"; o<<nr<<" "<<ii<<" "<<jj<<" "<<val<<"\n"; return o;}
249 
250 
252  virtual std::ostream& out(std::ostream& o) const
253  {return o<<"SINGLETON\n"<<nr<<" "<<ii<<" "<<jj<<" "<<val<<"\n";}
254  //put entire contents onto outstream with the class type in the beginning so
255  //that the derived class can be recognized.
256 
258  virtual std::istream& in(std::istream& is)
259  {
260  if (!(is>>nr>>ii>>jj>>val)) {
261  if (CH_Matrix_Classes::materrout) (*CH_Matrix_Classes::materrout)<<"*** ERROR: CMsingleton::in(): reading from input failed";
262  is.clear(std::ios::failbit);
263  return is;
264  }
265  if (nr<0) {
266  if (CH_Matrix_Classes::materrout) (*CH_Matrix_Classes::materrout)<<"*** ERROR: CMsingleton::in(): dimension of matrix must positive";
267  if (CH_Matrix_Classes::materrout) (*CH_Matrix_Classes::materrout)<<"but is "<<nr<<std::endl;
268  is.clear(std::ios::failbit);
269  return is;
270  }
271  if ((ii<0)||(ii>nr)){
272  if (CH_Matrix_Classes::materrout) (*CH_Matrix_Classes::materrout)<<"*** ERROR: CMsingleton::in(): row index outside range, ";
273  if (CH_Matrix_Classes::materrout) (*CH_Matrix_Classes::materrout)<<0<<"<="<<ii<<"<"<<nr<<std::endl;
274  is.clear(std::ios::failbit);
275  return is;
276  }
277  if ((jj<0)||(jj>nr)){
278  if (CH_Matrix_Classes::materrout) (*CH_Matrix_Classes::materrout)<<"*** ERROR: CMsingleton::in(): column index outside range, ";
279  if (CH_Matrix_Classes::materrout) (*CH_Matrix_Classes::materrout)<<0<<"<="<<jj<<"<"<<nr<<std::endl;
280  is.clear(std::ios::failbit);
281  return is;
282  }
283  return is;
284  }
285 
287  CMsingleton(std::istream& is,CoeffmatInfo* cip=0)
288  {CM_type=CM_singleton;infop=cip;in(is);}
289 
290  //--- specific routines
293  {i=ii; j=jj; v=val; return 0;}
294 
295  };
296 
298 }
299 
300 #endif
301 
#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
CH_Matrix_Classes::Integer nr
order/size of the matrix
Definition: CMsingleton.hxx:32
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: CMsingleton.hxx:136
virtual int sparse(CH_Matrix_Classes::Indexmatrix &I, CH_Matrix_Classes::Indexmatrix &J, CH_Matrix_Classes::Matrix &v, CH_Matrix_Classes::Real d=1.) const
returns 0 if not sparse. If it is sparse it returns 1 and the nonzero structure in I...
Definition: CMsingleton.hxx:158
virtual CH_Matrix_Classes::Real gramip(const CH_Matrix_Classes::Matrix &P) const
returns ip(*this,PP^T)=trace P^T(*this)P
Definition: CMsingleton.hxx:78
CH_Matrix_Classes::Integer ii
row index i of the nonzero element
Definition: CMsingleton.hxx:33
Integer rowdim() const
returns the row dimension
Definition: matrix.hxx:215
std::ostream * materrout
if not zero, this is the output stream for runtime error messages, by default it is set to &std::cout...
double Real
all real numbers in calculations are of this type
Definition: matop.hxx:50
virtual void project(CH_Matrix_Classes::Symmatrix &S, const CH_Matrix_Classes::Matrix &P) const
computes S=P^T*(*this)*P
Definition: CMsingleton.hxx:170
allows to memorize the scalings applied to a Coeffmat and offers the basis for storing further user d...
Definition: Coeffmat.hxx:52
virtual int sparse() const
returns 0 if not sparse, otherwise 1
Definition: CMsingleton.hxx:154
int get_ijval(CH_Matrix_Classes::Integer &i, CH_Matrix_Classes::Integer &j, CH_Matrix_Classes::Real &v) const
return the nonzero entry information
Definition: CMsingleton.hxx:292
Real * get_store()
returns the current address of the internal value array; use cautiously, do not use delete! ...
Definition: symmat.hxx:224
CH_Matrix_Classes::Real val
value of the element
Definition: CMsingleton.hxx:35
defines a base class for coefficient matrices in semidefinite programming, in particular for use with...
Definition: Coeffmat.hxx:125
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
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
virtual std::ostream & out(std::ostream &o) const
put entire contents onto outstream with the class type in the beginning so that the derived class can...
Definition: CMsingleton.hxx:252
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: CMsingleton.hxx:86
for CMsingleton
Definition: Coeffmat.hxx:38
virtual int dense() const
returns 1 if its structure is as bad as its dense symmetric representation, otherwise 0 ...
Definition: CMsingleton.hxx:150
virtual std::ostream & display(std::ostream &o) const
display constraint information
Definition: CMsingleton.hxx:247
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
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: CMsingleton.hxx:193
virtual void addmeto(CH_Matrix_Classes::Symmatrix &S, CH_Matrix_Classes::Real d=1.) const
computes S+=d*(*this);
Definition: CMsingleton.hxx:102
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: CMsingleton.hxx:113
conic bundle method solver for sum of convex functions. See the ConicBundle_Manual for a quick introd...
Definition: CBSolver.hxx:22
int check_support(Integer i, Integer j) const
returns 0 if (i,j) is not in the support, 1 otherwise
virtual int support_in(const CH_Matrix_Classes::Sparsesym &S) const
returns 0 if the support of the costraint matrix is not contained in the support of the sparse symmet...
Definition: CMsingleton.hxx:162
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: CMsingleton.hxx:51
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: CMsingleton.hxx:228
Matrix row(Integer i) const
returns row i copied to a new matrix
virtual CH_Matrix_Classes::Integer dim() const
returns the order of the represented symmetric matrix
Definition: CMsingleton.hxx:48
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 norm(void) const
returns the Frobenius norm of the matrix
Definition: CMsingleton.hxx:59
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: CMsingleton.hxx:237
Matrix class of symmetric matrices with real values of type Real
Definition: sparssym.hxx:89
Indexmatrix & init(const Indexmatrix &A, Integer d=1)
initialize to *this=A*d
void multiply(CH_Matrix_Classes::Real sf)
scales the scale factor
Definition: Coeffmat.hxx:70
CH_Matrix_Classes::Integer jj
column index j of the nonzero element
Definition: CMsingleton.hxx:34
implements a low rank matrix as Coeffmat with each a dense rectangular CH_Matrix_Classes::Matrix (f...
Definition: CMlowrankdd.hxx:24
implements a Coeffmat having just one nonzero element (or two by symmetry) for use with MatrixSDPfunc...
Definition: CMsingleton.hxx:29
int get_edge(Integer i, Integer &indi, Integer &indj, Real &val) const
stores element i of the get_edge_rep() function (ordered as in row representation); returns 1 if i is...
Integer coldim() const
returns the column dimension
Definition: matrix.hxx:218
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: CMsingleton.hxx:74
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
virtual Coeffmat * clone() const
makes an explicit copy of itself and returns a pointer to it
Definition: CMsingleton.hxx:44
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 void multiply(CH_Matrix_Classes::Real d)
multiply constraint permanentely by d; this is to allow scaling or sign changes in the constraints ...
Definition: CMsingleton.hxx:70
virtual CH_Matrix_Classes::Integer prodvec_flops() const
returns an estimate of number of flops to compute addprodto for a vector
Definition: CMsingleton.hxx:146
implements a Gram matrix as Coeffmat for a dense rectangular CH_Matrix_Classes::Matrix (for use wit...
Definition: CMgramdense.hxx:28
CMsingleton(std::istream &is, CoeffmatInfo *cip=0)
constructor with instream and possibly additional user information
Definition: CMsingleton.hxx:287
virtual void make_symmatrix(CH_Matrix_Classes::Symmatrix &S) const
returns a dense symmetric constraint matrix (useful for testing)
Definition: CMsingleton.hxx:55
CMsingleton(CH_Matrix_Classes::Integer innr, CH_Matrix_Classes::Integer ini, CH_Matrix_Classes::Integer inj, CH_Matrix_Classes::Real inval, CoeffmatInfo *cip=0)
the order is innr and the nonzero element (ini,inj) has values inval
Definition: CMsingleton.hxx:38
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 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: CMsingleton.hxx:63
Integer coldim() const
returns the column dimension
Definition: sparsmat.hxx:202
virtual CH_Matrix_Classes::Real ip(const CH_Matrix_Classes::Sparsesym &S) const
returns the inner product of the constraint matrix with S
Definition: CMsingleton.hxx:166
Header declaring the class CH_Matrix_Classes::Sparsesym for sparse symmetric matrices with Real eleme...
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
CoeffmatInfo * clone(const CoeffmatInfo *cip)
if cip is not zero, it calls and returns cip->clone() and 0 otherwise
Definition: Coeffmat.hxx:106
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: CMsingleton.hxx:220
virtual std::istream & in(std::istream &is)
counterpart to out(), does not read the class type, though. This is assumed to have been read in orde...
Definition: CMsingleton.hxx:258
Header declaring the class ConicBundle::CMgramdense (needed for ConicBundle::AffineMatrixFunction) ...
Header declaring the class ConicBundle::CMlowrankdd (needed for ConicBundle::AffineMatrixFunction) ...
virtual void addprodto(CH_Matrix_Classes::Matrix &B, const CH_Matrix_Classes::Matrix &C, CH_Matrix_Classes::Real d=1.) const
comutes B+=d*(*this)*C
Definition: CMsingleton.hxx:105