32 #ifndef MATLAB_MATLAB_H_ 33 #define MATLAB_MATLAB_H_ 62 template <
typename Derived>
63 typename std::enable_if<std::is_same<typename Derived::Scalar, cplx>::value,
65 loadMATLAB(
const std::string& mat_file,
const std::string& var_name) {
66 MATFile* pmat = matOpen(mat_file.c_str(),
"r");
71 throw std::runtime_error(
72 "qpp::loadMATLAB(): Can not open MATLAB file " + mat_file +
"!");
75 mxArray* pa = matGetVariable(pmat, var_name.c_str());
77 throw std::runtime_error(
78 "qpp::loadMATLAB(): Can not load the variable " + var_name +
79 " from MATLAB file " + mat_file +
"!");
81 if (mxGetNumberOfDimensions(pa) != 2)
82 throw std::runtime_error(
"qpp::loadMATLAB(): Loaded variable " +
83 var_name +
" is not 2-dimensional!");
86 throw std::runtime_error(
"qpp::loadMATLAB(): Loaded variable " +
88 " is not in double-precision format!");
91 idx rows = mxGetM(pa);
92 idx cols = mxGetN(pa);
94 dyn_mat<double> result_re(rows, cols);
95 dyn_mat<double> result_im(rows, cols);
98 double* pa_re =
nullptr;
99 double* pa_im =
nullptr;
103 std::memcpy(result_re.data(), pa_re,
104 sizeof(double) * mxGetNumberOfElements(pa));
109 std::memcpy(result_im.data(), pa_im,
110 sizeof(double) * mxGetNumberOfElements(pa));
113 std::memset(result_im.data(), 0,
114 sizeof(double) * mxGetNumberOfElements(pa));
120 return (result_re.cast<cplx>()) + 1_i * (result_im.cast<cplx>());
143 template <
typename Derived>
144 typename std::enable_if<!std::is_same<typename Derived::Scalar, cplx>::value,
145 dyn_mat<typename Derived::Scalar>>::type
146 loadMATLAB(
const std::string& mat_file,
const std::string& var_name) {
147 MATFile* pmat = matOpen(mat_file.c_str(),
"r");
152 throw std::runtime_error(
153 "qpp::loadMATLAB(): Can not open MATLAB file " + mat_file +
"!");
156 mxArray* pa = matGetVariable(pmat, var_name.c_str());
158 throw std::runtime_error(
159 "qpp::loadMATLAB(): Can not load the variable " + var_name +
160 " from MATLAB file " + mat_file +
"!");
162 if (mxGetNumberOfDimensions(pa) != 2)
163 throw std::runtime_error(
"qpp::loadMATLAB(): Loaded variable " +
164 var_name +
" is not 2-dimensional!");
167 throw std::runtime_error(
"qpp::loadMATLAB(): Loaded variable " +
169 " is not in double-precision format!");
172 idx rows = mxGetM(pa);
173 idx cols = mxGetN(pa);
175 dyn_mat<double> result(rows, cols);
177 std::memcpy(result.data(), mxGetPr(pa),
178 sizeof(double) * mxGetNumberOfElements(pa));
184 return result.cast<
typename Derived::Scalar>();
199 template <
typename Derived>
201 typename std::enable_if<
202 std::is_same<typename Derived::Scalar, cplx>::value>::type
203 saveMATLAB(
const Eigen::MatrixBase<Derived>& A,
const std::string& mat_file,
204 const std::string& var_name,
const std::string& mode) {
205 const dyn_mat<cplx>& rA = A.derived();
210 if (!internal::check_nonzero_size(rA))
211 throw exception::ZeroSize(
"qpp::saveMATLAB()");
214 dyn_mat<double> tmp_re = rA.real();
215 dyn_mat<double> tmp_im = rA.imag();
217 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
219 throw std::runtime_error(
220 "qpp::saveMATLAB(): Can not open/create MATLAB file " + mat_file +
223 mxArray* pa = mxCreateDoubleMatrix(tmp_re.rows(), tmp_re.cols(), mxCOMPLEX);
225 throw std::runtime_error(
226 "qpp::saveMATLAB(): mxCreateDoubleMatrix failed!");
230 double* pa_re =
nullptr;
231 double* pa_im =
nullptr;
235 std::memcpy(pa_re, tmp_re.data(),
sizeof(double) * tmp_re.size());
239 std::memcpy(pa_im, tmp_im.data(),
sizeof(double) * tmp_im.size());
241 if (matPutVariable(pmat, var_name.c_str(), pa))
242 throw std::runtime_error(
243 "qpp::saveMATLAB(): Can not write the variable " + var_name +
244 " to MATLAB file " + mat_file +
"!");
262 template <
typename Derived>
264 typename std::enable_if<
265 !std::is_same<typename Derived::Scalar, cplx>::value>::type
266 saveMATLAB(
const Eigen::MatrixBase<Derived>& A,
const std::string& mat_file,
267 const std::string& var_name,
const std::string& mode) {
269 const dyn_mat<double>& rA = A.template cast<double>();
274 if (!internal::check_nonzero_size(rA))
275 throw exception::ZeroSize(
"qpp::saveMATLAB()");
277 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
279 throw std::runtime_error(
280 "qpp::saveMATLAB(): Can not open/create MATLAB file " + mat_file +
283 mxArray* pa = mxCreateDoubleMatrix(rA.rows(), rA.cols(), mxREAL);
285 throw std::runtime_error(
286 "qpp::saveMATLAB(): mxCreateDoubleMatrix failed!");
289 std::memcpy(mxGetPr(pa), rA.data(),
sizeof(double) * rA.size());
291 if (matPutVariable(pmat, var_name.c_str(), pa))
292 throw std::runtime_error(
293 "qpp::saveMATLAB(): Can not write the variable " + var_name +
294 " to MATLAB file " + mat_file +
"!");
Quantum++ main namespace.
Definition: circuits.h:35