ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tag_detail.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * ALPS MPS DMRG Project
4  *
5  * Copyright (C) 2013 Institute for Theoretical Physics, ETH Zurich
6  * 2013-2013 by Sebastian Keller <sebkelle@phys.ethz.ch>
7  *
8  * This software is part of the ALPS Applications, published under the ALPS
9  * Application License; you can use, redistribute it and/or modify it under
10  * the terms of the license, either version 1 or (at your option) any later
11  * version.
12  *
13  * You should have received a copy of the ALPS Application License along with
14  * the ALPS Applications; see the file LICENSE.txt. If not, the license is also
15  * available from http://alps.comp-phys.org/.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  *****************************************************************************/
26 
27 #ifndef MAQUIS_DMRG_MODELS_TAG_DETAIL_H
28 #define MAQUIS_DMRG_MODELS_TAG_DETAIL_H
29 
30 #include <alps/numeric/isnan.hpp>
31 #include <alps/numeric/isinf.hpp>
32 #include <alps/numeric/is_nonzero.hpp>
33 
34 namespace tag_detail {
35 
36  typedef unsigned tag_type;
37 
39 
40  struct pair_cmp
41  {
42  bool operator()(std::pair<tag_type, tag_type> const & i,
43  std::pair<tag_type, tag_type> const & j) const
44  {
45  if (i.first < j.first)
46  return true;
47  else if (i.first > j.first)
48  return false;
49  else
50  return i.second < j.second;
51  }
52  };
53 
54  template <class Matrix, class SymmGroup>
56  {
57  for (typename Matrix::size_type b=0; b < op.n_blocks(); ++b)
58  {
59  bool only_zero = true;
60  const Matrix& m = op[b];
61  for (int i = 0; i < num_rows(m); i++)
62  for(int j = 0; j < num_cols(m); j++)
63  {
64  if (alps::numeric::is_nonzero(m(i,j))) {
65  only_zero = false;
66  break;
67  }
68  }
69  if (only_zero) {
70  op.remove_block(b);
71  --b;
72  }
73  }
74  }
75 
76  template <class Matrix, class SymmGroup>
78  {
79  if (op.n_blocks() == 0)
80  return true;
81 
82  typename Matrix::value_type invscale;
83 
84  // determine scale of matrices
85  const Matrix& m = op[0];
86  for (int i = 0; i < num_rows(m); i++)
87  for(int j = 0; j < num_cols(m); j++)
88  if (std::abs(m(i,j)) > 1.e-20) {
89  invscale = 1./m(i,j);
90  break;
91  }
92 
93  for (typename Matrix::size_type b=0; b < op.n_blocks(); ++b)
94  {
95  const Matrix& m = op[b];
96  for (int i = 0; i < num_rows(m); i++)
97  for(int j = 0; j < num_cols(m); j++)
98  {
99  typename maquis::traits::real_type<typename Matrix::value_type>::type normalized = std::abs(m(i,j) * invscale);
100  // if not 1 and not 0
101  if (std::abs(normalized-1.0) > 1e-15 && normalized > 1e-15)
102  return false;
103  }
104  }
105 
106  return true;
107  }
108 
109  template <class T>
110  bool num_check(T x) {
111  if (alps::numeric::isnan(x) || alps::numeric::isinf(x))
112  throw std::runtime_error("NaN / INF numeric Error occured while comparing operator scales\n");
113  return true;
114  }
115 
116  inline bool num_check(std::complex<double> x) { return true; }
117 
118  template <class Matrix, class SymmGroup>
119  std::pair<bool, typename Matrix::value_type>
121  block_matrix<Matrix, SymmGroup> const& sample)
122  {
123  if (reference.left_basis() != sample.left_basis() || reference.right_basis() != sample.right_basis())
124  return std::make_pair(false, 0.);
125 
126  if (sample.n_blocks() == 0)
127  return std::make_pair(true, 1.0);
128 
129  typename Matrix::value_type invscale1, invscale2;
130 
131  // determine scale of matrices
132  const Matrix& m1 = reference[0];
133  for (int i = 0; i < num_rows(m1); i++)
134  for(int j = 0; j < num_cols(m1); j++)
135  {
136  if (std::abs(m1(i,j)) > 1.e-50) {
137  invscale1 = 1./m1(i,j);
138  break;
139  }
140  if(i == (num_rows(m1)-1) && j == (num_cols(m1)-1)){ return std::make_pair(false, 0.); }
141  }
142 
143  const Matrix& m2 = sample[0];
144  for (int i = 0; i < num_rows(m2); i++)
145  for(int j = 0; j < num_cols(m2); j++)
146  {
147  if (std::abs(m2(i,j)) > 1.e-50) {
148  invscale2 = 1./m2(i,j);
149  break;
150  }
151  if(i == (num_rows(m2)-1) && j == (num_cols(m2)-1)){ return std::make_pair(false, 0.); }
152  }
153 
154  // Check all blocks for equality modulo scale factor
155  for (typename Matrix::size_type b=0; b < reference.n_blocks(); ++b)
156  {
157  const Matrix& mb1 = reference[b];
158  const Matrix& mb2 = sample[b];
159  for (int i = 0; i < num_rows(mb1); i++)
160  for(int j = 0; j < num_cols(mb1); j++)
161  {
162  typename Matrix::value_type t1 = mb1(i,j) * invscale1, t2 = mb2(i,j) * invscale2;
163  if (std::abs(t1 - t2) > 1e-12)
164  return std::make_pair(false, 0.);
165  }
166  }
167 
168  typename Matrix::value_type scale = invscale1 / invscale2;
169 
170 #ifndef NDEBUG
171  try { num_check(invscale1); }
172  catch (std::exception e) { maquis::cout << "invscale1 numcheck failed\n"; exit(1);}
173  try { num_check(invscale2); }
174  catch (std::exception e) { maquis::cout << "invscale2 numcheck failed\n"; exit(1);}
175  try { num_check(scale); }
176  catch (std::exception e) { maquis::cout << "scale numcheck failed\n"; exit(1);}
177 #endif
178 
179  return std::make_pair(true, scale);
180  }
181 }
182 
183 #endif
bool num_check(T x)
Definition: tag_detail.h:110
size_type n_blocks() const
void remove_empty_blocks(block_matrix< Matrix, SymmGroup > &op)
Definition: tag_detail.h:55
std::size_t num_rows(maquis::dmrg::one_matrix< T > const &m)
Definition: one_matrix.hpp:99
bool operator()(std::pair< tag_type, tag_type > const &i, std::pair< tag_type, tag_type > const &j) const
Definition: tag_detail.h:42
bool is_uniform(block_matrix< Matrix, SymmGroup > const &op)
Definition: tag_detail.h:77
real_type< typename T::value_type >::type type
Definition: traits.hpp:9
std::size_t num_cols(maquis::dmrg::one_matrix< T > const &m)
Definition: one_matrix.hpp:102
Index< SymmGroup > const & right_basis() const
void remove_block(charge r, charge c)
std::pair< bool, typename Matrix::value_type > equal(block_matrix< Matrix, SymmGroup > const &reference, block_matrix< Matrix, SymmGroup > const &sample)
Definition: tag_detail.h:120
unsigned tag_type
Definition: tag_detail.h:36
Index< SymmGroup > const & left_basis() const