TBTK
Need a break? Support the development by playing Polarity Puzzles
HoppingAmplitudeSet.h
Go to the documentation of this file.
1 /* Copyright 2016 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_HOPPING_AMPLITUDE_SET
24 #define COM_DAFER45_TBTK_HOPPING_AMPLITUDE_SET
25 
26 #include "TBTK/HoppingAmplitude.h"
28 #include "TBTK/IndexTree.h"
29 #include "TBTK/Serializable.h"
30 #include "TBTK/SparseMatrix.h"
31 #include "TBTK/Streams.h"
32 #include "TBTK/TBTKMacros.h"
33 
34 #include <complex>
35 #include <vector>
36 
37 namespace TBTK{
38 
51  virtual public Serializable,
52  private HoppingAmplitudeTree
53 {
54 public:
65 
68 
77  HoppingAmplitudeSet(const std::vector<unsigned int> &capacity);
78 
86  HoppingAmplitudeSet(const std::string &serializeation, Mode mode);
87 
89  virtual ~HoppingAmplitudeSet();
90 
93  void construct();
94 
98  bool getIsConstructed() const;
99 
106  int getFirstIndexInBlock(const Index &blockIndex) const;
107 
114  int getLastIndexInBlock(const Index &blockIndex) const;
115 
121  IndexTree getIndexTree() const;
122 
132  IndexTree getIndexTree(const Index &subspace) const;
133 
140 
141  class Iterator;
142  class ConstIterator;
143 private:
146  template<bool isConstIterator>
147  class _Iterator{
148  public:
151  typedef typename std::conditional<
152  isConstIterator,
153  const HoppingAmplitude&,
155  >::type HoppingAmplitudeReferenceType;
156 
158  void operator++();
159 
161  HoppingAmplitudeReferenceType operator*();
162 
164  bool operator==(const _Iterator &rhs) const;
165 
167  bool operator!=(const _Iterator &rhs) const;
168 
170  int getMinBasisIndex() const;
171 
173  int getMaxBasisIndex() const;
174 
176  int getNumBasisIndices() const;
177  private:
180  typedef typename std::conditional<
181  isConstIterator,
184  >::type HoppingAmplitudeTreeIteratorType;
185 
188  typedef typename std::conditional<
189  isConstIterator,
190  const HoppingAmplitudeTree*,
192  >::type HoppingAmplitudeTreePointerType;
193 
196  HoppingAmplitudeTreeIteratorType iterator;
197 
200  friend class Iterator;
201  friend class ConstIterator;
202 
205  _Iterator(
206  HoppingAmplitudeTreePointerType hoppingAmplitudeTree,
207  bool end = false
208  );
209  };
210 public:
213  class Iterator : public _Iterator<false>{
214  private:
215  Iterator(
216  HoppingAmplitudeTree *hoppingAmplitudeTree,
217  bool end = false
218  ) : _Iterator<false>(hoppingAmplitudeTree, end){};
219 
222  friend class HoppingAmplitudeSet;
223  };
224 
227  class ConstIterator : public _Iterator<true>{
228  private:
230  const HoppingAmplitudeTree *hoppingAmplitudeTree,
231  bool end = false
232  ) : _Iterator<true>(hoppingAmplitudeTree, end){};
233 
236  friend class HoppingAmplitudeSet;
237  };
238 
243  Iterator begin();
244 
249  ConstIterator begin() const;
250 
255  ConstIterator cbegin() const;
256 
263  Iterator begin(const Index &subspace);
264 
272  ConstIterator begin(const Index &subspace) const;
273 
281  ConstIterator cbegin(const Index &subspace) const;
282 
286  Iterator end();
287 
292  ConstIterator end() const;
293 
298  ConstIterator cend() const;
299 
305  Iterator end(const Index &subspace);
306 
314  ConstIterator end(const Index &subspace) const;
315 
323  ConstIterator cend(const Index &subspace) const;
324 
326  void print();
327 
352  void tabulate(
353  std::complex<double> **amplitudes,
354  int **indices,
355  int *numHoppingAmplitudes,
356  int *maxIndexSize
357  ) const;
358 
360  virtual std::string serialize(Mode mode) const;
361 
363  unsigned int getSizeInBytes() const;
364 private:
367  bool isConstructed;
368 };
369 
371  TBTKAssert(
372  !isConstructed,
373  "HoppingAmplitudeSet::construct()",
374  "HoppingAmplitudeSet is already constructed.",
375  ""
376  );
377 
379  isConstructed = true;
380 }
381 
383  return isConstructed;
384 }
385 
387  const Index &blockIndex
388 ) const{
390 }
391 
393  const Index &blockIndex
394 ) const{
396 }
397 
399 ) const{
400  TBTKAssert(
401  isConstructed,
402  "HoppingAmplitudeSet::getSparseMatrix()",
403  "HoppingAmplitudeSet has to be constructed first.",
404  ""
405  );
406 
407  SparseMatrix<std::complex<double>> sparseMatrix(
408  SparseMatrix<std::complex<double>>::StorageFormat::CSC
409  );
410 
411  for(
412  ConstIterator iterator = begin();
413  iterator != end();
414  ++iterator
415  ){
416  sparseMatrix.add(
417  getBasisIndex((*iterator).getToIndex()),
418  getBasisIndex((*iterator).getFromIndex()),
419  (*iterator).getAmplitude()
420  );
421  }
422  sparseMatrix.construct();
423 
424  return sparseMatrix;
425 }
426 
428  return Iterator(this);
429 }
430 
432  return ConstIterator(this);
433 }
434 
436  return ConstIterator(this);
437 }
438 
440  const Index &subspace
441 ){
442  return Iterator(getSubTree(subspace));
443 }
444 
446  const Index &subspace
447 ) const{
448  return ConstIterator(getSubTree(subspace));
449 }
450 
452  const Index &subspace
453 ) const{
454  return ConstIterator(getSubTree(subspace));
455 }
456 
458  return Iterator(this, true);
459 }
460 
462  return ConstIterator(this, true);
463 }
464 
466  return ConstIterator(this, true);
467 }
468 
470  const Index &subspace
471 ){
472  return Iterator(getSubTree(subspace), true);
473 }
474 
476  const Index &subspace
477 ) const{
478  return ConstIterator(getSubTree(subspace), true);
479 }
480 
482  const Index &subspace
483 ) const{
484  return ConstIterator(getSubTree(subspace), true);
485 }
486 
487 template<bool isConstIterator>
488 inline bool HoppingAmplitudeSet::_Iterator<isConstIterator>::operator==(
489  const _Iterator &rhs
490 ) const{
491  return iterator == rhs.iterator;
492 }
493 
494 template<bool isConstIterator>
495 inline bool HoppingAmplitudeSet::_Iterator<isConstIterator>::operator!=(
496  const _Iterator &rhs
497 ) const{
498  return iterator != rhs.iterator;
499 }
500 
501 inline unsigned int HoppingAmplitudeSet::getSizeInBytes() const{
502  unsigned int size = sizeof(*this) - sizeof(HoppingAmplitudeTree);
504 
505  return size;
506 }
507 
508 template<bool isConstIterator>
509 inline void HoppingAmplitudeSet::_Iterator<isConstIterator>::operator++(){
510  ++iterator;
511 }
512 
513 template<bool isConstIterator>
514 inline typename HoppingAmplitudeSet::_Iterator<
515  isConstIterator
516 >::HoppingAmplitudeReferenceType HoppingAmplitudeSet::_Iterator<
517  isConstIterator
518 >::operator*(){
519  return *iterator;
520 }
521 
522 template<bool isConstIterator>
523 inline int HoppingAmplitudeSet::_Iterator<isConstIterator>::getMinBasisIndex() const{
524  return iterator.getMinBasisIndex();
525 }
526 
527 template<bool isConstIterator>
528 inline int HoppingAmplitudeSet::_Iterator<isConstIterator>::getMaxBasisIndex() const{
529  return iterator.getMaxBasisIndex();
530 }
531 
532 template<bool isConstIterator>
533 inline int HoppingAmplitudeSet::_Iterator<isConstIterator>::getNumBasisIndices() const{
534  return iterator.getNumBasisIndices();
535 }
536 
537 template<bool isConstIterator>
538 inline HoppingAmplitudeSet::_Iterator<isConstIterator>::_Iterator(
539  HoppingAmplitudeTreePointerType hoppingAmplitudeTree,
540  bool end
541 ) :
542  iterator(
543  (
544  end ?
545  hoppingAmplitudeTree->end()
546  : hoppingAmplitudeTree->begin()
547  )
548  )
549 {
550 }
551 
552 }; //End of namespace TBTK
553 
554 #endif
TBTK::HoppingAmplitudeTree::getSubTree
HoppingAmplitudeTree * getSubTree(const Index &subspace)
TBTK::HoppingAmplitudeSet::cend
ConstIterator cend() const
Definition: HoppingAmplitudeSet.h:465
TBTK::HoppingAmplitudeSet::begin
Iterator begin()
Definition: HoppingAmplitudeSet.h:427
TBTK::HoppingAmplitudeTree::isProperSubspace
bool isProperSubspace(const Index &subspace) const
TBTK::SparseMatrix
Definition: SparseMatrix.h:35
Streams.h
Streams for TBTK output.
TBTK::SparseMatrix::add
void add(unsigned int row, unsigned int col, const DataType &value)
Definition: SparseMatrix.h:563
TBTK::HoppingAmplitudeTree::HoppingAmplitudeTree
HoppingAmplitudeTree()
TBTK::HoppingAmplitudeSet::~HoppingAmplitudeSet
virtual ~HoppingAmplitudeSet()
SparseMatrix.h
Sparse matrix.
TBTK::HoppingAmplitudeTree::getSubspaceIndices
IndexTree getSubspaceIndices() const
TBTK::HoppingAmplitudeSet::getLastIndexInBlock
int getLastIndexInBlock(const Index &blockIndex) const
Definition: HoppingAmplitudeSet.h:392
TBTK::HoppingAmplitudeTree::getPhysicalIndex
Index getPhysicalIndex(int basisIndex) const
TBTK::HoppingAmplitudeSet::ConstIterator
Definition: HoppingAmplitudeSet.h:227
TBTK::HoppingAmplitudeTree::getLastIndexInSubspace
int getLastIndexInSubspace(const Index &subspaceIndex) const
Definition: HoppingAmplitudeTree.h:516
TBTK::HoppingAmplitudeSet
HoppingAmplitude container.
Definition: HoppingAmplitudeSet.h:50
TBTK::SparseMatrix::construct
void construct()
Definition: SparseMatrix.h:780
TBTK::HoppingAmplitudeTree::getHoppingAmplitudes
const std::vector< HoppingAmplitude > & getHoppingAmplitudes(Index index) const
TBTK::Serializable
Definition: Serializable.h:43
TBTK::HoppingAmplitudeSet::getIndexTree
IndexTree getIndexTree() const
TBTK::HoppingAmplitudeTree::getSubspaceIndex
Index getSubspaceIndex(const Index &index) const
TBTK::HoppingAmplitudeTree::generateBasisIndices
void generateBasisIndices()
TBTK::HoppingAmplitudeSet::getIsConstructed
bool getIsConstructed() const
Definition: HoppingAmplitudeSet.h:382
TBTK::HoppingAmplitudeTree::add
void add(HoppingAmplitude ha)
TBTK::operator!=
bool operator!=(const IndexTree &lhs, const IndexTree &rhs)
Definition: IndexTree.h:391
TBTK::HoppingAmplitudeSet::serialize
virtual std::string serialize(Mode mode) const
Serializable.h
Abstract base class for serializable objects.
TBTK::HoppingAmplitudeTree
Node in tree used by HoppingAmplitudeSet to store HoppingAmplitudes .
Definition: HoppingAmplitudeTree.h:40
TBTK::HoppingAmplitudeTree::getIndexList
std::vector< Index > getIndexList(const Index &pattern) const
TBTK::HoppingAmplitudeTree::getFirstIndexInSubspace
int getFirstIndexInSubspace(const Index &subspaceIndex) const
Definition: HoppingAmplitudeTree.h:500
TBTK::HoppingAmplitudeSet::print
void print()
TBTK::HoppingAmplitudeSet::tabulate
void tabulate(std::complex< double > **amplitudes, int **indices, int *numHoppingAmplitudes, int *maxIndexSize) const
TBTK::HoppingAmplitudeTree::getBasisIndex
int getBasisIndex(const Index &index) const
TBTK::HoppingAmplitudeTree::Iterator
Definition: HoppingAmplitudeTree.h:313
TBTK::HoppingAmplitudeTree::getIndexListMultiplePatterns
std::vector< Index > getIndexListMultiplePatterns(const std::vector< Index > &patterns) const
TBTK::HoppingAmplitude
Hopping amplitude from state 'from' to state 'to'.
Definition: HoppingAmplitude.h:53
TBTK::HoppingAmplitudeTree::getSizeInBytes
unsigned int getSizeInBytes() const
Definition: HoppingAmplitudeTree.h:556
TBTK::IndexTree
Data structure for mapping physical indices to linear indices.
Definition: IndexTree.h:35
HoppingAmplitudeTree.h
Node in tree used by HoppingAmplitudeSet to store HoppingAmplitudes .
HoppingAmplitude.h
Hopping amplitude from state 'from' to state 'to'.
TBTK::HoppingAmplitudeSet::HoppingAmplitudeSet
HoppingAmplitudeSet()
TBTKMacros.h
Precompiler macros.
TBTK::HoppingAmplitudeTree::getBasisSize
int getBasisSize() const
Definition: HoppingAmplitudeTree.h:496
TBTK::HoppingAmplitudeSet::cbegin
ConstIterator cbegin() const
Definition: HoppingAmplitudeSet.h:435
TBTK::HoppingAmplitudeSet::end
Iterator end()
Definition: HoppingAmplitudeSet.h:457
TBTK::operator*
const Vector2d operator*(double lhs, const Vector2d &rhs)
Definition: Vector2d.h:129
TBTK::HoppingAmplitudeSet::construct
void construct()
Definition: HoppingAmplitudeSet.h:370
TBTK::HoppingAmplitudeTree::ConstIterator
Definition: HoppingAmplitudeTree.h:327
TBTK::Serializable::Mode
Mode
Definition: Serializable.h:47
TBTK::HoppingAmplitudeSet::Iterator
Definition: HoppingAmplitudeSet.h:213
TBTK::Index
Physical index.
Definition: Index.h:44
TBTK::HoppingAmplitudeSet::getSparseMatrix
SparseMatrix< std::complex< double > > getSparseMatrix() const
Definition: HoppingAmplitudeSet.h:398
TBTK::HoppingAmplitudeSet::getFirstIndexInBlock
int getFirstIndexInBlock(const Index &blockIndex) const
Definition: HoppingAmplitudeSet.h:386
IndexTree.h
Data structure for mapping physical indices to a linear index.
TBTK::HoppingAmplitudeSet::getSizeInBytes
unsigned int getSizeInBytes() const
Definition: HoppingAmplitudeSet.h:501