32 #ifndef INPUT_OUTPUT_H_ 33 #define INPUT_OUTPUT_H_ 43 template <
typename Derived>
44 internal::IOManipEigen disp(
const Eigen::MatrixBase<Derived>& A,
45 double chop = qpp::chop) {
46 return internal::IOManipEigen(A, chop);
57 inline internal::IOManipEigen disp(cplx z,
double chop = qpp::chop) {
58 return internal::IOManipEigen(z, chop);
72 template <
typename InputIterator>
73 internal::IOManipRange<InputIterator>
74 disp(InputIterator first, InputIterator last,
const std::string& separator,
75 const std::string& start =
"[",
const std::string& end =
"]",
76 double chop = qpp::chop) {
77 return internal::IOManipRange<InputIterator>(first, last, separator, start,
92 template <
typename Container>
93 internal::IOManipRange<typename Container::const_iterator>
94 disp(
const Container& c,
const std::string& separator,
95 const std::string& start =
"[",
const std::string& end =
"]",
96 double chop = qpp::chop,
97 typename std::enable_if<is_iterable<Container>::value>::type* =
nullptr) {
98 return internal::IOManipRange<typename Container::const_iterator>(
99 std::begin(c), std::end(c), separator, start, end, chop);
113 template <
typename Po
interType>
114 internal::IOManipPointer<PointerType>
115 disp(
const PointerType* p, idx N,
const std::string& separator,
116 const std::string& start =
"[",
const std::string& end =
"]",
117 double chop = qpp::chop) {
118 return internal::IOManipPointer<PointerType>(p, N, separator, start, end,
130 template <
typename Derived>
131 void save(
const Eigen::MatrixBase<Derived>& A,
const std::string& fname) {
132 const dyn_mat<typename Derived::Scalar>& rA = A.derived();
137 if (!internal::check_nonzero_size(rA))
138 throw exception::ZeroSize(
"qpp::save()");
141 fout.open(fname, std::ios::out | std::ios::binary);
144 throw std::runtime_error(
"qpp::save(): Error writing output file \"" +
145 std::string(fname) +
"\"!");
150 const std::string header_ =
"TYPE::Eigen::Matrix";
151 fout.write(header_.c_str(), header_.length());
153 idx rows =
static_cast<idx
>(rA.rows());
154 idx cols =
static_cast<idx
>(rA.cols());
155 fout.write(reinterpret_cast<const char*>(&rows),
sizeof(rows));
156 fout.write(reinterpret_cast<const char*>(&cols),
sizeof(cols));
158 fout.write(reinterpret_cast<const char*>(rA.data()),
159 sizeof(
typename Derived::Scalar) * rows * cols);
180 template <
typename Derived>
181 dyn_mat<typename Derived::Scalar> load(
const std::string& fname) {
183 fin.open(fname, std::ios::in | std::ios::binary);
188 throw std::runtime_error(
"qpp::load(): Error opening input file \"" +
189 std::string(fname) +
"\"!");
192 const std::string header_ =
"TYPE::Eigen::Matrix";
193 std::unique_ptr<char[]> fheader_{
new char[header_.length()]};
196 fin.read(fheader_.get(), header_.length());
197 if (std::string(fheader_.get(), header_.length()) != header_) {
198 throw std::runtime_error(
"qpp::load(): Input file \"" +
199 std::string(fname) +
"\" is corrupted!");
204 fin.read(reinterpret_cast<char*>(&rows),
sizeof(rows));
205 fin.read(reinterpret_cast<char*>(&cols),
sizeof(cols));
207 dyn_mat<typename Derived::Scalar> A(rows, cols);
209 fin.read(reinterpret_cast<char*>(A.data()),
210 sizeof(
typename Derived::Scalar) * rows * cols);
Quantum++ main namespace.
Definition: circuits.h:35