Quantum++  v0.7
C++11 quantum computing library
input_output.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2015 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 INPUT_OUTPUT_H_
28 #define INPUT_OUTPUT_H_
29 
30 // input/output
31 
32 namespace qpp
33 {
34 
43 template<typename Derived>
44 internal::IOManipEigen disp(const Eigen::MatrixBase<Derived>& A,
45  double chop = qpp::chop)
46 {
47  return internal::IOManipEigen(A, chop);
48 }
49 
60 {
61  return internal::IOManipEigen(z, chop);
62 }
63 
74 template<typename InputIterator>
75 internal::IOManipRange<InputIterator> disp(const InputIterator& first,
76  const InputIterator& last,
77  const std::string& separator,
78  const std::string& start = "[",
79  const std::string& end = "]")
80 {
81  return internal::IOManipRange<InputIterator
82  >(first, last, separator, start, end);
83 }
84 
95 template<typename Container>
97  const Container& c, const std::string& separator,
98  const std::string& start = "[", const std::string& end = "]")
99 {
101  c.begin(), c.end(), separator, start, end);
102 }
103 
114 template<typename PointerType>
116  const std::string& separator,
117  const std::string& start = "[",
118  const std::string& end = "]")
119 {
120  return internal::IOManipPointer<PointerType>(p, n, separator, start, end);
121 }
122 
131 template<typename Derived>
132 void save(const Eigen::MatrixBase<Derived>& A, const std::string& fname)
133 {
134  const dyn_mat<typename Derived::Scalar>& rA = A;
135 
136  // check zero-size
138  throw Exception("qpp::save()", Exception::Type::ZERO_SIZE);
139 
140  std::fstream fout;
141  fout.open(fname, std::ios::out | std::ios::binary);
142 
143  if (fout.fail())
144  {
145  throw std::runtime_error(
146  "qpp::save(): Error writing output file \""
147  + std::string(fname) + "\"!");
148  }
149 
150  // write the header to file
151  const char _header[] = "TYPE::Eigen::Matrix";
152  fout.write(_header, sizeof(_header));
153 
154  idx rows = static_cast<idx>(rA.rows());
155  idx cols = static_cast<idx>(rA.cols());
156  fout.write((char*) &rows, sizeof(rows));
157  fout.write((char*) &cols, sizeof(cols));
158 
159  fout.write((char*) rA.data(),
160  sizeof(typename Derived::Scalar) * rows * cols);
161 
162  fout.close();
163 }
164 
183 template<typename Derived>
184 dyn_mat<typename Derived::Scalar> load(const std::string& fname)
185 {
186  std::fstream fin;
187  fin.open(fname, std::ios::in | std::ios::binary);
188 
189  if (fin.fail())
190  {
191  throw std::runtime_error(
192  "qpp::load(): Error opening input file \""
193  + std::string(fname) + "\"!");
194  }
195 
196  const char _header[] = "TYPE::Eigen::Matrix";
197  char* _fheader = new char[sizeof(_header)];
198 
199  // read the header from file
200  fin.read(_fheader, sizeof(_header));
201  if (strcmp(_fheader, _header))
202  {
203  delete[] _fheader;
204  throw std::runtime_error(
205  "qpp::load(): Input file \"" + std::string(fname)
206  + "\" is corrupted!");
207  }
208  delete[] _fheader;
209 
210  idx rows, cols;
211  fin.read((char*) &rows, sizeof(rows));
212  fin.read((char*) &cols, sizeof(cols));
213 
214  dyn_mat<typename Derived::Scalar> A(rows, cols);
215 
216  fin.read((char*) A.data(),
217  sizeof(typename Derived::Scalar) * rows * cols);
218 
219  fin.close();
220 
221  return A;
222 }
223 
224 } /* namespace qpp */
225 
226 #endif /* INPUT_OUTPUT_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:67
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:73
Definition: iomanip.h:39
Quantum++ main namespace.
Definition: codes.h:30
Definition: iomanip.h:118
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:41
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
bool _check_nonzero_size(const T &x) noexcept
Definition: util.h:119
Definition: iomanip.h:77
dyn_mat< typename Derived::Scalar > load(const std::string &fname)
Loads Eigen matrix from a binary file (internal format) in double precision.
Definition: input_output.h:184
void save(const Eigen::MatrixBase< Derived > &A, const std::string &fname)
Saves Eigen expression to a binary file (internal format) in double precision.
Definition: input_output.h:132
std::size_t idx
Non-negative integer index.
Definition: types.h:36
internal::IOManipEigen disp(const Eigen::MatrixBase< Derived > &A, double chop=qpp::chop)
Eigen expression ostream manipulator.
Definition: input_output.h:44