Quantum++  v1.0-rc2
A modern C++11 quantum computing library
iomanip.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2017 Vlad Gheorghiu (vgheorgh@gmail.com)
5  *
6  * This file is part of Quantum++.
7  *
8  * Quantum++ 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  * Quantum++ 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 Quantum++. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef INTERNAL_CLASSES_IOMANIP_H_
28 #define INTERNAL_CLASSES_IOMANIP_H_
29 
30 namespace qpp
31 {
32 namespace internal
33 {
34 // ostream manipulators for nice formatting of
35 // Eigen matrices and STL/C-style containers/vectors
36 
37 template<typename InputIterator>
38 class IOManipRange : public IDisplay
39 {
40  InputIterator first_, last_;
41  std::string separator_, start_, end_;
42 public:
43  explicit IOManipRange(InputIterator first, InputIterator last,
44  const std::string& separator,
45  const std::string& start = "[",
46  const std::string& end = "]") :
47  first_{first},
48  last_{last},
49  separator_{separator},
50  start_{start},
51  end_{end}
52  {
53  }
54 
55  // to silence -Weffc++ warnings for classes that have pointer members
56  // (whenever we have a pointer instantiation,
57  // i.e. iterator is a raw pointer)
58  IOManipRange(const IOManipRange&) = default;
59 
60  IOManipRange& operator=(const IOManipRange&) = default;
61 
62 private:
63  std::ostream& display(std::ostream& os) const override
64  {
65  os << start_;
66 
67  bool first = true;
68  for (InputIterator it = first_; it != last_; ++it)
69  {
70  if (!first)
71  os << separator_;
72  first = false;
73  os << *it;
74  }
75  os << end_;
76 
77  return os;
78  }
79 }; // class IOManipRange
80 
81 template<typename PointerType>
82 class IOManipPointer : public IDisplay
83 {
84  const PointerType* p_;
86  std::string separator_, start_, end_;
87 public:
88  explicit IOManipPointer(const PointerType* p, idx N,
89  const std::string& separator,
90  const std::string& start = "[",
91  const std::string& end = "]") :
92  p_{p},
93  N_{N},
94  separator_{separator},
95  start_{start},
96  end_{end}
97  {
98  }
99 
100  // to silence -Weffc++ warnings for classes that have pointer members
101  IOManipPointer(const IOManipPointer&) = default;
102 
103  IOManipPointer& operator=(const IOManipPointer&) = default;
104 
105 private:
106  std::ostream& display(std::ostream& os) const override
107  {
108  os << start_;
109 
110  for (idx i = 0; i < N_ - 1; ++i)
111  os << p_[i] << separator_;
112  if (N_ > 0)
113  os << p_[N_ - 1];
114 
115  os << end_;
116 
117  return os;
118  }
119 }; // class IOManipPointer
120 
121 // silence g++4.8.x bogus warning -Wnon-virtual-dtor for
122 // qpp::internal::Display_impl_ when class qpp::internal::IOManipEigen
123 // privately inherits from it
124 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ == 8) && !__clang__)
125 #pragma GCC diagnostic push
126 #pragma GCC diagnostic ignored "-Weffc++"
127 #endif
128 class IOManipEigen : public IDisplay, private Display_Impl_
129 {
130 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ == 8) && !__clang__)
131 #pragma GCC diagnostic pop
132 #endif
134  double chop_;
135 public:
136  // Eigen matrices
137  template<typename Derived>
138  explicit IOManipEigen(const Eigen::MatrixBase<Derived>& A,
139  double chop = qpp::chop) :
140  A_{A.template cast<cplx>()}, // copy, so we can bind rvalues safely
141  chop_{chop}
142  {
143  }
144 
145  // Complex numbers
146  explicit IOManipEigen(const cplx z, double chop = qpp::chop) :
147  A_{cmat::Zero(1, 1)}, chop_{chop}
148  {
149  // put the complex number inside an Eigen matrix
150  A_(0, 0) = z;
151  }
152 
153 private:
154  std::ostream& display(std::ostream& os) const override
155  {
156  return display_impl_(A_, os, chop);
157  }
158 }; // class IOManipEigen
159 
160 } /* namespace internal */
161 } /* namespace qpp */
162 
163 #endif /* INTERNAL_CLASSES_IOMANIP_H_ */
constexpr double chop
Used in qpp::disp() for setting to zero numbers that have their absolute value smaller than qpp::chop...
Definition: constants.h:56
IOManipEigen(const Eigen::MatrixBase< Derived > &A, double chop=qpp::chop)
Definition: iomanip.h:138
const PointerType * p_
Definition: iomanip.h:84
std::ostream & display(std::ostream &os) const override
Must be overridden by all derived classes.
Definition: iomanip.h:63
Definition: iomanip.h:38
Quantum++ main namespace.
Definition: codes.h:30
std::string end_
Definition: iomanip.h:41
Definition: util.h:418
IOManipRange & operator=(const IOManipRange &)=default
std::ostream & display(std::ostream &os) const override
Must be overridden by all derived classes.
Definition: iomanip.h:106
double chop_
Definition: iomanip.h:134
Abstract class (interface) that mandates the definition of virtual std::ostream& display(std::ostream...
Definition: idisplay.h:42
Definition: iomanip.h:128
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:45
cmat A_
Definition: iomanip.h:133
IOManipRange(InputIterator first, InputIterator last, const std::string &separator, const std::string &start="[", const std::string &end="]")
Definition: iomanip.h:43
IOManipEigen(const cplx z, double chop=qpp::chop)
Definition: iomanip.h:146
Definition: iomanip.h:82
std::string start_
Definition: iomanip.h:86
std::ostream & display(std::ostream &os) const override
Must be overridden by all derived classes.
Definition: iomanip.h:154
std::string start_
Definition: iomanip.h:41
std::string separator_
Definition: iomanip.h:41
Eigen::MatrixXcd cmat
Complex (double precision) dynamic Eigen matrix.
Definition: types.h:60
InputIterator first_
Definition: iomanip.h:40
idx N_
Definition: iomanip.h:85
std::size_t idx
Non-negative integer index.
Definition: types.h:35
InputIterator last_
Definition: iomanip.h:40
IOManipPointer(const PointerType *p, idx N, const std::string &separator, const std::string &start="[", const std::string &end="]")
Definition: iomanip.h:88