Quantum++  v1.0-rc4
A modern C++11 quantum computing library
iomanip.h
Go to the documentation of this file.
1 /*
2  * This file is part of Quantum++.
3  *
4  * MIT License
5  *
6  * Copyright (c) 2013 - 2018 Vlad Gheorghiu (vgheorgh@gmail.com)
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
32 #ifndef INTERNAL_CLASSES_IOMANIP_H_
33 #define INTERNAL_CLASSES_IOMANIP_H_
34 
35 namespace qpp {
36 namespace internal {
37 // ostream manipulators for nice formatting of
38 // Eigen matrices and STL/C-style containers/vectors
39 
40 template <typename InputIterator>
41 class IOManipRange : public IDisplay {
42  InputIterator first_, last_;
43  std::string separator_, start_, end_;
44 
45  public:
46  explicit IOManipRange(InputIterator first, InputIterator last,
47  const std::string& separator,
48  const std::string& start = "[",
49  const std::string& end = "]")
50  : first_{first}, last_{last}, separator_{separator}, start_{start},
51  end_{end} {}
52 
53  // to silence -Weffc++ warnings for classes that have pointer members
54  // (whenever we have a pointer instantiation,
55  // i.e. iterator is a raw pointer)
56  IOManipRange(const IOManipRange&) = default;
57 
58  IOManipRange& operator=(const IOManipRange&) = default;
59 
60  private:
61  std::ostream& display(std::ostream& os) const override {
62  os << start_;
63 
64  bool first = true;
65  for (InputIterator it = first_; it != last_; ++it) {
66  if (!first)
67  os << separator_;
68  first = false;
69  os << *it;
70  }
71  os << end_;
72 
73  return os;
74  }
75 }; // class IOManipRange
76 
77 template <typename PointerType>
78 class IOManipPointer : public IDisplay {
79  const PointerType* p_;
81  std::string separator_, start_, end_;
82 
83  public:
84  explicit IOManipPointer(const PointerType* p, idx N,
85  const std::string& separator,
86  const std::string& start = "[",
87  const std::string& end = "]")
88  : p_{p}, N_{N}, separator_{separator}, start_{start}, end_{end} {}
89 
90  // to silence -Weffc++ warnings for classes that have pointer members
91  IOManipPointer(const IOManipPointer&) = default;
92 
93  IOManipPointer& operator=(const IOManipPointer&) = default;
94 
95  private:
96  std::ostream& display(std::ostream& os) const override {
97  os << start_;
98 
99  for (idx i = 0; i < N_ - 1; ++i)
100  os << p_[i] << separator_;
101  if (N_ > 0)
102  os << p_[N_ - 1];
103 
104  os << end_;
105 
106  return os;
107  }
108 }; // class IOManipPointer
109 
110 // silence g++4.8.x bogus warning -Wnon-virtual-dtor for
111 // qpp::internal::Display_impl_ when class qpp::internal::IOManipEigen
112 // privately inherits from it
113 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ == 8) && !__clang__)
114 #pragma GCC diagnostic push
115 #pragma GCC diagnostic ignored "-Weffc++"
116 #endif
117 class IOManipEigen : public IDisplay, private Display_Impl_ {
118 #if ((__GNUC__ == 4) && (__GNUC_MINOR__ == 8) && !__clang__)
119 #pragma GCC diagnostic pop
120 #endif
122  double chop_;
123 
124  public:
125  // Eigen matrices
126  template <typename Derived>
127  explicit IOManipEigen(const Eigen::MatrixBase<Derived>& A,
128  double chop = qpp::chop)
129  : A_{A.template cast<cplx>()}, // copy, so we can bind rvalues safely
130  chop_{chop} {}
131 
132  // Complex numbers
133  explicit IOManipEigen(const cplx z, double chop = qpp::chop)
134  : A_{cmat::Zero(1, 1)}, chop_{chop} {
135  // put the complex number inside an Eigen matrix
136  A_(0, 0) = z;
137  }
138 
139  private:
140  std::ostream& display(std::ostream& os) const override {
141  return display_impl_(A_, os, chop);
142  }
143 }; // class IOManipEigen
144 
145 } /* namespace internal */
146 } /* namespace qpp */
147 
148 #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:58
IOManipEigen(const Eigen::MatrixBase< Derived > &A, double chop=qpp::chop)
Definition: iomanip.h:127
const PointerType * p_
Definition: iomanip.h:79
std::ostream & display(std::ostream &os) const override
Must be overridden by all derived classes.
Definition: iomanip.h:61
Definition: iomanip.h:41
Quantum++ main namespace.
Definition: codes.h:35
std::string end_
Definition: iomanip.h:43
Definition: util.h:391
IOManipRange & operator=(const IOManipRange &)=default
std::ostream & display(std::ostream &os) const override
Must be overridden by all derived classes.
Definition: iomanip.h:96
double chop_
Definition: iomanip.h:122
Abstract class (interface) that mandates the definition of virtual std::ostream& display(std::ostream...
Definition: idisplay.h:46
Definition: iomanip.h:117
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:49
cmat A_
Definition: iomanip.h:121
IOManipRange(InputIterator first, InputIterator last, const std::string &separator, const std::string &start="[", const std::string &end="]")
Definition: iomanip.h:46
IOManipEigen(const cplx z, double chop=qpp::chop)
Definition: iomanip.h:133
Definition: iomanip.h:78
std::string start_
Definition: iomanip.h:81
std::ostream & display(std::ostream &os) const override
Must be overridden by all derived classes.
Definition: iomanip.h:140
std::string start_
Definition: iomanip.h:43
std::string separator_
Definition: iomanip.h:43
Eigen::MatrixXcd cmat
Complex (double precision) dynamic Eigen matrix.
Definition: types.h:64
InputIterator first_
Definition: iomanip.h:42
idx N_
Definition: iomanip.h:80
std::size_t idx
Non-negative integer index.
Definition: types.h:39
InputIterator last_
Definition: iomanip.h:42
IOManipPointer(const PointerType *p, idx N, const std::string &separator, const std::string &start="[", const std::string &end="]")
Definition: iomanip.h:84