32 #ifndef MATLAB_MATLAB_H_ 33 #define MATLAB_MATLAB_H_ 63 template <
typename Derived>
64 typename std::enable_if<std::is_same<typename Derived::Scalar, cplx>::value,
66 loadMATLAB(
const std::string& mat_file,
const std::string& var_name) {
67 MATFile* pmat = matOpen(mat_file.c_str(),
"r");
72 throw std::runtime_error(
73 "qpp::loadMATLAB(): Can not open MATLAB file " + mat_file +
"!");
76 mxArray* pa = matGetVariable(pmat, var_name.c_str());
78 throw std::runtime_error(
79 "qpp::loadMATLAB(): Can not load the variable " + var_name +
80 " from MATLAB file " + mat_file +
"!");
82 if (mxGetNumberOfDimensions(pa) != 2)
83 throw std::runtime_error(
"qpp::loadMATLAB(): Loaded variable " +
84 var_name +
" is not 2-dimensional!");
87 throw std::runtime_error(
"qpp::loadMATLAB(): Loaded variable " +
89 " is not in double-precision format!");
92 idx rows = mxGetM(pa);
93 idx cols = mxGetN(pa);
99 double* pa_re =
nullptr;
100 double* pa_im =
nullptr;
104 std::memcpy(result_re.data(), pa_re,
105 sizeof(double) * mxGetNumberOfElements(pa));
110 std::memcpy(result_im.data(), pa_im,
111 sizeof(double) * mxGetNumberOfElements(pa));
114 std::memset(result_im.data(), 0,
115 sizeof(double) * mxGetNumberOfElements(pa));
121 return (result_re.cast<
cplx>()) + 1_i * (result_im.cast<
cplx>());
145 template <
typename Derived>
146 typename std::enable_if<!std::is_same<typename Derived::Scalar, cplx>::value,
148 loadMATLAB(
const std::string& mat_file,
const std::string& var_name) {
149 MATFile* pmat = matOpen(mat_file.c_str(),
"r");
154 throw std::runtime_error(
155 "qpp::loadMATLAB(): Can not open MATLAB file " + mat_file +
"!");
158 mxArray* pa = matGetVariable(pmat, var_name.c_str());
160 throw std::runtime_error(
161 "qpp::loadMATLAB(): Can not load the variable " + var_name +
162 " from MATLAB file " + mat_file +
"!");
164 if (mxGetNumberOfDimensions(pa) != 2)
165 throw std::runtime_error(
"qpp::loadMATLAB(): Loaded variable " +
166 var_name +
" is not 2-dimensional!");
169 throw std::runtime_error(
"qpp::loadMATLAB(): Loaded variable " +
171 " is not in double-precision format!");
174 idx rows = mxGetM(pa);
175 idx cols = mxGetN(pa);
179 std::memcpy(result.data(), mxGetPr(pa),
180 sizeof(double) * mxGetNumberOfElements(pa));
186 return result.cast<
typename Derived::Scalar>();
201 template <
typename Derived>
203 typename std::enable_if<
204 std::is_same<typename Derived::Scalar, cplx>::value>::type
205 saveMATLAB(
const Eigen::MatrixBase<Derived>& A,
const std::string& mat_file,
206 const std::string& var_name,
const std::string& mode) {
219 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
221 throw std::runtime_error(
222 "qpp::saveMATLAB(): Can not open/create MATLAB file " + mat_file +
225 mxArray* pa = mxCreateDoubleMatrix(tmp_re.rows(), tmp_re.cols(), mxCOMPLEX);
227 throw std::runtime_error(
228 "qpp::saveMATLAB(): mxCreateDoubleMatrix failed!");
232 double* pa_re =
nullptr;
233 double* pa_im =
nullptr;
237 std::memcpy(pa_re, tmp_re.data(),
sizeof(double) * tmp_re.size());
241 std::memcpy(pa_im, tmp_im.data(),
sizeof(double) * tmp_im.size());
243 if (matPutVariable(pmat, var_name.c_str(), pa))
244 throw std::runtime_error(
245 "qpp::saveMATLAB(): Can not write the variable " + var_name +
246 " to MATLAB file " + mat_file +
"!");
264 template <
typename Derived>
266 typename std::enable_if<
267 !std::is_same<typename Derived::Scalar, cplx>::value>::type
268 saveMATLAB(
const Eigen::MatrixBase<Derived>& A,
const std::string& mat_file,
269 const std::string& var_name,
const std::string& mode) {
279 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
281 throw std::runtime_error(
282 "qpp::saveMATLAB(): Can not open/create MATLAB file " + mat_file +
285 mxArray* pa = mxCreateDoubleMatrix(rA.rows(), rA.cols(), mxREAL);
287 throw std::runtime_error(
288 "qpp::saveMATLAB(): mxCreateDoubleMatrix failed!");
291 std::memcpy(mxGetPr(pa), rA.data(),
sizeof(double) * rA.size());
293 if (matPutVariable(pmat, var_name.c_str(), pa))
294 throw std::runtime_error(
295 "qpp::saveMATLAB(): Can not write the variable " + var_name +
296 " to MATLAB file " + mat_file +
"!");
bool check_nonzero_size(const T &x) noexcept
Definition: util.h:123
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:81
Quantum++ main namespace.
Definition: codes.h:35
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:49
std::enable_if< std::is_same< typename Derived::Scalar, cplx >::value, dyn_mat< cplx > >::type loadMATLAB(const std::string &mat_file, const std::string &var_name)
Loads a complex Eigen dynamic matrix from a MATLAB .mat file,.
Definition: matlab.h:66
std::size_t idx
Non-negative integer index.
Definition: types.h:39
std::enable_if< std::is_same< typename Derived::Scalar, cplx >::value >::type saveMATLAB(const Eigen::MatrixBase< Derived > &A, const std::string &mat_file, const std::string &var_name, const std::string &mode)
Saves a complex Eigen dynamic matrix to a MATLAB .mat file,.
Definition: matlab.h:205
Object has zero size exception.
Definition: exception.h:132