27 #ifndef INPUT_OUTPUT_H_
28 #define INPUT_OUTPUT_H_
43 template<
typename Derived>
74 template<
typename InputIterator>
77 const std::string& separator,
78 const std::string& start =
"[",
79 const std::string& end =
"]")
82 first, last, separator, start, end);
95 template<
typename Container>
97 const Container& c,
const std::string& separator,
98 const std::string& start =
"[",
const std::string& end =
"]",
102 std::begin(c), std::end(c), separator, start, end);
115 template<
typename Po
interType>
117 const std::string& separator,
118 const std::string& start =
"[",
119 const std::string& end =
"]")
132 template<
typename Derived>
133 void save(
const Eigen::MatrixBase<Derived>& A,
const std::string& fname)
144 fout.open(fname, std::ios::out | std::ios::binary);
148 throw std::runtime_error(
149 "qpp::save(): Error writing output file \""
150 + std::string(fname) +
"\"!");
155 const std::string header_ =
"TYPE::Eigen::Matrix";
156 fout.write(header_.c_str(), header_.length());
158 idx rows =
static_cast<idx>(rA.rows());
159 idx cols =
static_cast<idx>(rA.cols());
160 fout.write(reinterpret_cast<char*>(&rows),
sizeof(rows));
161 fout.write(reinterpret_cast<char*>(&cols),
sizeof(cols));
163 fout.write(reinterpret_cast<const char*>(rA.data()),
164 sizeof(
typename Derived::Scalar) * rows * cols);
186 template<
typename Derived>
190 fin.open(fname, std::ios::in | std::ios::binary);
196 throw std::runtime_error(
197 "qpp::load(): Error opening input file \""
198 + std::string(fname) +
"\"!");
201 const std::string header_ =
"TYPE::Eigen::Matrix";
202 std::unique_ptr<char[]> fheader_{
new char[header_.length()]};
205 fin.read(fheader_.get(), header_.length());
206 if (std::string(fheader_.get(), header_.length()) != header_)
208 throw std::runtime_error(
209 "qpp::load(): Input file \"" + std::string(fname)
210 +
"\" is corrupted!");
215 fin.read(reinterpret_cast<char*>(&rows),
sizeof(rows));
216 fin.read(reinterpret_cast<char*>(&cols),
sizeof(cols));
220 fin.read(reinterpret_cast<char*>(A.data()),
221 sizeof(
typename Derived::Scalar) * rows * cols);
bool check_nonzero_size(const T &x) noexcept
Definition: util.h:112
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:78
Definition: iomanip.h:129
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:212
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:46
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
Definition: iomanip.h:173
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:187
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