Quantum++  v0.8.8
C++11 quantum computing library
input_output.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2016 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>
76  InputIterator last,
77  const std::string& separator,
78  const std::string& start = "[",
79  const std::string& end = "]")
80 {
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  typename std::enable_if<is_iterable<Container>::value>::type* = nullptr)
100 {
102  std::begin(c), std::end(c), separator, start, end);
103 }
104 
115 template<typename PointerType>
117  const std::string& separator,
118  const std::string& start = "[",
119  const std::string& end = "]")
120 {
121  return internal::IOManipPointer<PointerType>(p, n, separator, start, end);
122 }
123 
132 template<typename Derived>
133 void save(const Eigen::MatrixBase<Derived>& A, const std::string& fname)
134 {
135  const dyn_mat<typename Derived::Scalar>& rA = A.derived();
136 
137  // EXCEPTION CHECKS
138 
139  // check zero-size
141  throw Exception("qpp::save()", Exception::Type::ZERO_SIZE);
142 
143  std::fstream fout;
144  fout.open(fname, std::ios::out | std::ios::binary);
145 
146  if ( fout.fail())
147  {
148  throw std::runtime_error(
149  "qpp::save(): Error writing output file \""
150  + std::string(fname) + "\"!");
151  }
152  // END EXCEPTION CHECKS
153 
154  // write the header to file
155  const char _header[] = "TYPE::Eigen::Matrix";
156  fout.write(_header, sizeof(_header));
157 
158  idx rows = static_cast<idx>(rA.rows());
159  idx cols = static_cast<idx>(rA.cols());
160  fout.write((char*) &rows, sizeof(rows));
161  fout.write((char*) &cols, sizeof(cols));
162 
163  fout.write((char*) rA.data(),
164  sizeof(typename Derived::Scalar) * rows * cols);
165 
166  fout.close();
167 }
168 
187 template<typename Derived>
188 dyn_mat<typename Derived::Scalar> load(const std::string& fname)
189 {
190  std::fstream fin;
191  fin.open(fname, std::ios::in | std::ios::binary);
192 
193  // EXCEPTION CHECKS
194 
195  if ( fin.fail())
196  {
197  throw std::runtime_error(
198  "qpp::load(): Error opening input file \""
199  + std::string(fname) + "\"!");
200  }
201 
202  const char _header[] = "TYPE::Eigen::Matrix";
203  char* _fheader = new char[sizeof(_header)];
204 
205  // read the header from file
206  fin.read(_fheader, sizeof(_header));
207  if ( strcmp(_fheader, _header))
208  {
209  delete[] _fheader;
210  throw std::runtime_error(
211  "qpp::load(): Input file \"" + std::string(fname)
212  + "\" is corrupted!");
213  }
214  delete[] _fheader;
215  // END EXCEPTION CHECKS
216 
217  idx rows, cols;
218  fin.read((char*) &rows, sizeof(rows));
219  fin.read((char*) &cols, sizeof(cols));
220 
221  dyn_mat<typename Derived::Scalar> A(rows, cols);
222 
223  fin.read((char*) A.data(),
224  sizeof(typename Derived::Scalar) * rows * cols);
225 
226  fin.close();
227 
228  return A;
229 }
230 
231 } /* namespace qpp */
232 
233 #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:59
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:83
Definition: iomanip.h:132
Quantum++ main namespace.
Definition: codes.h:30
Checks whether T is compatible with an STL-like iterable container.
Definition: traits.h:52
Definition: iomanip.h:215
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:51
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
bool _check_nonzero_size(const T &x) noexcept
Definition: util.h:112
Definition: iomanip.h:176
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:188
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:133
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