v2DM-DOCI  1.0
BlockStructure.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 "Matrix.h"
27 #include "Vector.h"
28 #include "BlockStructure.h"
29 
30 using namespace doci2DM;
31 
37  template<class BlockType>
39 {
40  blocks.resize(nr);
41 
42  degen.resize(nr,0);
43 }
44 
49  template<class BlockType>
51 {
52  blocks.resize(blockmat_copy.blocks.size());
53 
54 #pragma omp parallel for
55  for(int i=0;i<blocks.size();i++)
56  blocks[i].reset(new BlockType(blockmat_copy[i]));
57 
58  degen = blockmat_copy.degen;
59 }
60 
61  template<class BlockType>
63 {
64  blocks = std::move(blockmat_copy.blocks);
65 
66  degen = std::move(blockmat_copy.degen);
67 }
68 
75  template<class BlockType>
76 void BlockStructure<BlockType>::setDim(int block,int dim,int degeneracy)
77 {
78  assert(block < blocks.size());
79 
80  this->degen[block] = degeneracy;
81 
82  blocks[block].reset(new BlockType(dim));
83 }
84 
90  template<class BlockType>
92 {
93  return *blocks[block];
94 }
95 
101 template<class BlockType>
102 const BlockType &BlockStructure<BlockType>::operator[](int block) const
103 {
104  return *blocks[block];
105 }
106 
107 
112  template<class BlockType>
114 {
115  blocks.resize(blockmat_copy.blocks.size());
116 
117 #pragma omp parallel for
118  for(int i=0;i<blocks.size();i++)
119  blocks[i].reset(new BlockType(blockmat_copy[i]));
120 
121  return *this;
122 }
123 
128  template<class BlockType>
130 {
131 #pragma omp parallel for
132  for(int i = 0;i < blocks.size();++i)
133  *blocks[i] = a;
134 
135  return *this;
136 }
137 
142  template<class BlockType>
144 {
145 #pragma omp parallel for
146  for(int i = 0;i < blocks.size();++i)
147  *blocks[i] += blockmat_pl[i];
148 
149  return *this;
150 }
151 
156  template<class BlockType>
158 {
159 #pragma omp parallel for
160  for(int i = 0;i < blocks.size();++i)
161  *blocks[i] -= blockmat_pl[i];
162 
163  return *this;
164 }
165 
171  template<class BlockType>
173 {
174 #pragma omp parallel for
175  for(int i = 0;i < blocks.size();++i)
176  blocks[i]->daxpy(alpha,blockmat_pl[i]);
177 
178  return *this;
179 }
180 
185  template<class BlockType>
187 {
188 #pragma omp parallel for
189  for(int i = 0;i < blocks.size();++i)
190  *blocks[i] /= c;
191 
192  return *this;
193 }
194 
195  template<class BlockType>
197 {
198 #pragma omp parallel for
199  for(int i = 0;i < blocks.size();++i)
200  *blocks[i] *= c;
201 
202  return *this;
203 }
204 
205 namespace doci2DM {
213  template<>
214 double &BlockStructure<Matrix>::operator()(int block,int i,int j)
215 {
216  return (*blocks[block])(i,j);
217 }
218 
226 template<>
227 double BlockStructure<Matrix>::operator()(int block,int i,int j) const
228 {
229  return (*blocks[block])(i,j);
230 }
231 
232  template<>
233 double& BlockStructure<Vector>::operator()(int block,int index)
234 {
235  return (*blocks[block])[index];
236 }
237 
238 template<>
239 double BlockStructure<Vector>::operator()(int block,int index) const
240 {
241  return (*blocks[block])[index];
242 }
243 
244 }
245 
249 template<class BlockType>
251 {
252  return blocks.size();
253 }
254 
258 template<class BlockType>
260 {
261  return blocks[i]->gn();
262 }
263 
267 template<class BlockType>
269 {
270  return degen[i];
271 }
272 
276 template<class BlockType>
278 {
279  double ward = 0;
280 
281 #pragma omp parallel for reduction(+:ward)
282  for(int i = 0;i < blocks.size();++i)
283  ward += degen[i]*blocks[i]->trace();
284 
285  return ward;
286 }
287 
292 template<class BlockType>
294 {
295  double ward = 0.0;
296 
297 #pragma omp parallel for reduction(+:ward)
298  for(int i = 0;i < blocks.size();i++)
299  ward += degen[i]*blocks[i]->ddot(blocks_in[i]);
300 
301  return ward;
302 }
303 
307  template<class BlockType>
309 {
310 #pragma omp parallel for
311  for(int i = 0;i < blocks.size();++i)
312  blocks[i]->invert();
313 }
314 
319  template<class BlockType>
321 {
322 #pragma omp parallel for
323  for(int i = 0;i < blocks.size();++i)
324  blocks[i]->dscal(alpha);
325 }
326 
330  template<class BlockType>
332 {
333  for(int i = 0;i < blocks.size();++i)
334  blocks[i]->fill_Random();
335 }
336 
341  template<class BlockType>
343 {
344  for(int i = 0;i < blocks.size();++i)
345  blocks[i]->fill_Random(seed);
346 }
347 
352  template<class BlockType>
354 {
355 #pragma omp parallel for
356  for(int i = 0;i < blocks.size();++i)
357  blocks[i]->sqrt(option);
358 }
359 
366  template<class BlockType>
368 {
369 #pragma omp parallel for
370  for(int i = 0;i < blocks.size();++i)
371  blocks[i]->L_map(map[i],object[i]);
372 }
373 
379  template<class BlockType>
381 {
382 #pragma omp parallel for
383  for(int i = 0;i < blocks.size();++i)
384  blocks[i]->mprod(A[i],B[i]);
385 
386  return *this;
387 }
388 
392  template<class BlockType>
394 {
395 #pragma omp parallel for
396  for(int i = 0;i < blocks.size();++i)
397  blocks[i]->symmetrize();
398 }
399 
400 namespace doci2DM {
401 template<>
403 {
404  for(unsigned int i = 0;i < blocks.size();++i)
405  blocks[i]->sort();
406 }
407 }
408 
409  template<class BlockType>
411 {
412  for(unsigned int i=0;i<blocks.size();++i)
413  blocks[i]->sep_pm(*pos.blocks[i], *neg.blocks[i]);
414 }
415 
416 
417 namespace doci2DM
418 {
419  template<class MyBlockType>
420  std::ostream &operator<< (std::ostream &output,const doci2DM::BlockStructure<MyBlockType> &blocks_p)
421  {
422  for(int i = 0;i < blocks_p.blocks.size();++i)
423  {
424  output << i << "\t" << blocks_p.blocks[i]->gn() << "\t" << blocks_p.degen[i] << std::endl;
425  output << std::endl;
426 
427  output << *blocks_p.blocks[i] << std::endl;
428  }
429 
430  return output;
431  }
432 
433  // explicit instantiation must occur in namespace
434  template class BlockStructure<doci2DM::Matrix>;
435  template class BlockStructure<doci2DM::Vector>;
436 
437  // also instantiation for friend functions
438  template std::ostream &operator<< (std::ostream &output,const doci2DM::BlockStructure<doci2DM::Matrix> &blocks_p);
439  template std::ostream &operator<< (std::ostream &output,const doci2DM::BlockStructure<doci2DM::Vector> &blocks_p);
440 }
441 
442 
443 /* vim: set ts=3 sw=3 expandtab :*/
BlockStructure & operator*=(double)
virtual void sqrt(int)
std::vector< int > degen
degeneracy of the blocks
double ddot(const BlockStructure< BlockType > &) const
BlockStructure & operator-=(const BlockStructure< BlockType > &)
virtual BlockStructure< BlockType > & mprod(const BlockStructure< BlockType > &, const BlockStructure< BlockType > &)
double & operator()(int block, int i, int j)
BlockType & operator[](int block)
void setDim(int, int, int)
void dscal(double alpha)
std::vector< std::unique_ptr< BlockType > > blocks
BlockStructure & operator=(const BlockStructure< BlockType > &)
BlockStructure & operator/=(double)
virtual void L_map(const BlockStructure< BlockType > &, const BlockStructure< BlockType > &)
virtual void sep_pm(BlockStructure< BlockType > &, BlockStructure< BlockType > &)
BlockStructure & operator+=(const BlockStructure< BlockType > &)
BlockStructure & daxpy(double alpha, const BlockStructure< BlockType > &)