v2DM-DOCI  1.0
Lineq.cpp
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Copyright (C) 2014-2015 Ward Poelmans
5  *
6  * This file is part of v2DM-DOCI.
7  *
8  * v2DM-DOCI is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Foobar is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
20  *
21  * @END LICENSE
22  */
23 
24 #include <assert.h>
25 
26 #include "include.h"
27 
28 using namespace doci2DM;
29 
36 Lineq::Lineq(int L,int N, bool partial_trace)
37 {
38  this->L = L;
39  this->N = N;
40 
41  if(partial_trace)
42  {
43  TPM trace_constr(L,N);
44  trace_constr = 0;
45 
46  for(int i=0;i<trace_constr.gdimMatrix(0);i++)
47  trace_constr(0,i,i) = 1;
48 
49  E.push_back(trace_constr);
50  e.push_back(N/2.0);
51 
52  trace_constr.getMatrix(0) = 0;
53  for(int i=0;i<trace_constr.gdimVector(0);i++)
54  trace_constr(0,i) = 1;
55 
56  E.push_back(std::move(trace_constr));
57  e.push_back(N*(N/2.0-1)); // keep the fourfold degenaracy in mind
58  } else
59  {
60  TPM trace_constr(L,N);
61  trace_constr.unit();
62 
63  // make room for 1 trace constrains and L DOCI constrains
64  e.resize(1+L, 0);
65 
66  // first trace on Block
67  E.push_back(std::move(trace_constr));
68  e[0] = N*(N-1)/2.0;
69 
70  auto doci_constr = trace_constr.DOCI_constrains();
71  E.insert(E.end(), std::make_move_iterator(doci_constr.begin()), std::make_move_iterator(doci_constr.end()));
72 
73  assert(E.size() == 1+L);
74  }
75 
76  orthogonalize();//speaks for itself, doesn't it?
77 
78  //construct the u^0 SUP matrices
79  constr_u_0();
80 
82 }
83 
87 int Lineq::gN() const
88 {
89  return N;
90 }
91 
95 int Lineq::gnr() const
96 {
97  return E.size();
98 }
99 
103 int Lineq::gL() const
104 {
105  return L;
106 }
107 
113 const TPM &Lineq::gE(int i) const
114 {
115  return E[i];
116 }
117 
123 double Lineq::ge(int i) const
124 {
125  return e[i];
126 }
127 
133 const TPM &Lineq::gE_ortho(int i) const
134 {
135  return E_ortho[i];
136 }
137 
143 double Lineq::ge_ortho(int i) const
144 {
145  return e_ortho[i];
146 }
147 
148 namespace doci2DM
149 {
150  std::ostream &operator<<(std::ostream &output,doci2DM::Lineq &lineq_p)
151  {
152  output << "first print the constraint matrices:";
153  output << std::endl;
154  output << std::endl;
155 
156  for(int i = 0;i < lineq_p.gnr();++i)
157  {
158  output << "constraint nr :" << i << std::endl;
159  output << std::endl;
160 
161  output << lineq_p.gE(i);
162  }
163 
164  output << std::endl;
165  output << std::endl;
166  output << "the desired values are:" << std::endl;
167  output << std::endl;
168 
169  for(int i = 0;i < lineq_p.gnr();++i)
170  output << i << "\t" << lineq_p.ge(i) << std::endl;
171 
172  return output;
173  }
174 }
175 
180 {
181  //construct the overlapmatrix of the E's
182  Matrix S(E.size());
183 
184  for(int i = 0;i < gnr();++i)
185  for(int j = i;j < gnr();++j)
186  S(i,j) = S(j,i) = E[i].ddot(E[j]);
187 
188  //take the inverse square root
189  S.sqrt(-1);
190 
191  // make room
192  E_ortho.reserve(gnr());
193  e_ortho.reserve(gnr());
194  // throw away old stuff (leave capacity unchanged)
195  E_ortho.clear();
196  e_ortho.clear();
197 
198  //make the orthogonal ones:
199  for(int i=0;i<gnr();++i)
200  {
201  TPM ortho_constr(L,N);
202  ortho_constr = 0;
203 
204  double tmp_e_ortho = 0;
205 
206  for(int j = 0;j<gnr();++j)
207  {
208  ortho_constr.daxpy(S(i,j), E[j]);
209  tmp_e_ortho += S(i,j) * e[j];
210  }
211 
212  E_ortho.push_back(std::move(ortho_constr));
213  e_ortho.push_back(tmp_e_ortho);
214  }
215 
216  assert(E_ortho.size() == E.size());
217 }
218 
224 const SUP &Lineq::gu_0(int i) const
225 {
226  return u_0[i];
227 }
228 
234 const SUP &Lineq::gu_0_ortho(int i) const
235 {
236  return u_0_ortho[i];
237 }
238 
244 {
245  u_0.reserve(gnr());
246 
247  for(int i = 0;i < gnr();++i)
248  {
249  SUP tmp(L,N);
250 
251  tmp.fill(E_ortho[i]);
252 
253  u_0.push_back(std::move(tmp));
254  }
255 
256  assert(u_0.size() == E.size());
257 }
258 
263 {
264  //construct the overlapmatrix of the E's
265  Matrix S(u_0.size());
266 
267  for(int i = 0;i < gnr();++i)
268  for(int j = i;j < gnr();++j)
269  S(i,j) = S(j,i) = u_0[i].ddot(u_0[j]);
270 
271  //take the inverse square root
272  S.sqrt(-1);
273 
274  // make room
275  u_0_ortho.reserve(gnr());
276  // throw away old stuff (leave capacity unchanged)
277  u_0_ortho.clear();
278 
279  //make the orthogonal ones:
280  for(int i=0;i<gnr();++i)
281  {
282  SUP ortho_constr(L,N);
283  ortho_constr = 0;
284 
285  for(int j = 0;j<gnr();++j)
286  ortho_constr.daxpy(S(i,j), u_0[j]);
287 
288  u_0_ortho.push_back(std::move(ortho_constr));
289  }
290 
291  assert(u_0_ortho.size() == E.size());
292 // //first allocate the u_0_ortho matrices:
293 // u_0_ortho = new SUP * [nr];
294 //
295 // for(int i = 0;i < nr;++i)
296 // u_0_ortho[i] = new SUP(M,N);
297 //
298 // //construct the overlapmatrix of the u_0's
299 // Matrix S(nr);
300 //
301 // for(int i = 0;i < nr;++i)
302 // for(int j = i;j < nr;++j)
303 // S(i,j) = u_0[i]->ddot(*u_0[j]);
304 //
305 // S.symmetrize();
306 //
307 // //take the inverse square root
308 // S.sqrt(-1);
309 //
310 // //make the orthogonal ones:
311 // for(int i = 0;i < nr;++i){
312 //
313 // *u_0_ortho[i] = 0;//init
314 //
315 // for(int j = 0;j < nr;++j)
316 // u_0_ortho[i]->daxpy(S(i,j),*u_0[j]);
317 //
318 // }
319 }
320 
321 void Lineq::check(const TPM &tpm) const
322 {
323  for(int i = 0;i < gnr();++i)
324  {
325  std::cout << "constrain " << i << " gives " << tpm.ddot(gE(i)) << " = " << ge(i);
326  double tmp = fabs(tpm.ddot(gE(i)) - ge(i));
327  std::cout << ((tmp>1e-10) ? "\tFAILED" : "") << std::endl;
328  }
329 }
330 
331 /* vim: set ts=3 sw=3 expandtab :*/
void daxpy(double, const SUP &)
Definition: SUP.cpp:323
Matrix & getMatrix(int)
Definition: Container.cpp:174
double ge(int) const
Definition: Lineq.cpp:123
std::vector< double > e
pointer of doubles, will contain the values of the projections. (the desired equalities) ...
Definition: Lineq.h:89
std::vector< TPM > E
double pointer to TPM object, will contain the linear equality constraints
Definition: Lineq.h:87
std::vector< SUP > u_0_ortho
will contain the orthogonalized matrices that span u^0 space
Definition: Lineq.h:100
double ge_ortho(int) const
Definition: Lineq.cpp:143
int gnr() const
Definition: Lineq.cpp:95
int gL() const
Definition: Lineq.cpp:103
double ddot(const Container &) const
Definition: Container.cpp:255
std::ostream & operator<<(std::ostream &output, const doci2DM::BlockStructure< MyBlockType > &blocks_p)
void orthogonalize_u_0()
Definition: Lineq.cpp:262
std::vector< SUP > u_0
will contain the matrices that span u^0 space
Definition: Lineq.h:97
int gdimVector(int) const
Definition: Container.cpp:235
std::vector< TPM > E_ortho
orthogonalized constraints, these will be hidden from the public.
Definition: Lineq.h:92
Lineq(int L, int N, bool=false)
Definition: Lineq.cpp:36
int L
nr of sp orbs
Definition: Lineq.h:106
void check(const TPM &tpm) const
Definition: Lineq.cpp:321
void constr_u_0()
Definition: Lineq.cpp:243
int gN() const
Definition: Lineq.cpp:87
const SUP & gu_0(int) const
Definition: Lineq.cpp:224
const TPM & gE(int) const
Definition: Lineq.cpp:113
int N
nr of particles
Definition: Lineq.h:103
Container & daxpy(double alpha, const Container &)
Definition: Container.cpp:116
const SUP & gu_0_ortho(int) const
Definition: Lineq.cpp:234
void fill(const TPM &)
Definition: SUP.cpp:231
void unit()
Definition: TPM.cpp:523
void orthogonalize()
Definition: Lineq.cpp:179
int gdimMatrix(int) const
Definition: Container.cpp:230
const TPM & gE_ortho(int) const
Definition: Lineq.cpp:133
std::vector< double > e_ortho
the values accompanying the orthogonalized constraints
Definition: Lineq.h:94
std::vector< TPM > DOCI_constrains() const
Definition: TPM.cpp:872