27 #ifndef MATLAB_MATLAB_H_ 28 #define MATLAB_MATLAB_H_ 59 template<
typename Derived>
60 typename std::enable_if<std::is_same<typename Derived::Scalar, cplx>::value,
64 loadMATLAB(
const std::string& mat_file,
const std::string& var_name)
66 MATFile* pmat = matOpen(mat_file.c_str(),
"r");
72 throw std::runtime_error(
73 "qpp::loadMATLAB(): Can not open MATLAB file " 77 mxArray* pa = matGetVariable(pmat, var_name.c_str());
79 throw std::runtime_error(
80 "qpp::loadMATLAB(): Can not load the variable " 81 + var_name +
" from MATLAB file " + mat_file +
"!");
83 if (mxGetNumberOfDimensions(pa) != 2)
84 throw std::runtime_error(
85 "qpp::loadMATLAB(): Loaded variable " + var_name
86 +
" is not 2-dimensional!");
89 throw std::runtime_error(
90 "qpp::loadMATLAB(): Loaded variable " + var_name
91 +
" is not in double-precision format!");
94 idx rows = mxGetM(pa);
95 idx cols = mxGetN(pa);
101 double* pa_re =
nullptr;
102 double* pa_im =
nullptr;
106 std::memcpy(result_re.data(), pa_re,
107 sizeof(double) * mxGetNumberOfElements(pa));
112 std::memcpy(result_im.data(), pa_im,
113 sizeof(double) * mxGetNumberOfElements(pa));
116 std::memset(result_im.data(), 0,
117 sizeof(double) * mxGetNumberOfElements(pa));
123 return (result_re.cast<
cplx>()) + 1_i * (result_im.cast<
cplx>());
148 template<
typename Derived>
149 typename std::enable_if<!std::is_same<typename Derived::Scalar, cplx>::value,
153 loadMATLAB(
const std::string& mat_file,
const std::string& var_name)
155 MATFile* pmat = matOpen(mat_file.c_str(),
"r");
161 throw std::runtime_error(
162 "qpp::loadMATLAB(): Can not open MATLAB file " 166 mxArray* pa = matGetVariable(pmat, var_name.c_str());
168 throw std::runtime_error(
169 "qpp::loadMATLAB(): Can not load the variable " 170 + var_name +
" from MATLAB file " + mat_file +
"!");
172 if (mxGetNumberOfDimensions(pa) != 2)
173 throw std::runtime_error(
174 "qpp::loadMATLAB(): Loaded variable " + var_name
175 +
" is not 2-dimensional!");
178 throw std::runtime_error(
179 "qpp::loadMATLAB(): Loaded variable " + var_name
180 +
" is not in double-precision format!");
183 idx rows = mxGetM(pa);
184 idx cols = mxGetN(pa);
188 std::memcpy(result.data(), mxGetPr(pa),
189 sizeof(double) * mxGetNumberOfElements(pa));
195 return result.cast<
typename Derived::Scalar>();
210 template<
typename Derived>
213 std::enable_if<std::is_same<typename Derived::Scalar, cplx>::value>::type
215 const std::string& mat_file,
216 const std::string& var_name,
217 const std::string& mode)
231 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
233 throw std::runtime_error(
234 "qpp::saveMATLAB(): Can not open/create MATLAB file " 237 mxArray* pa = mxCreateDoubleMatrix(
238 tmp_re.rows(), tmp_re.cols(), mxCOMPLEX);
240 throw std::runtime_error(
241 "qpp::saveMATLAB(): mxCreateDoubleMatrix failed!");
245 double* pa_re =
nullptr;
246 double* pa_im =
nullptr;
250 std::memcpy(pa_re, tmp_re.data(),
sizeof(double) * tmp_re.size());
254 std::memcpy(pa_im, tmp_im.data(),
sizeof(double) * tmp_im.size());
256 if (matPutVariable(pmat, var_name.c_str(), pa))
257 throw std::runtime_error(
258 "qpp::saveMATLAB(): Can not write the variable " 259 + var_name +
" to MATLAB file " + mat_file +
"!");
277 template<
typename Derived>
280 std::enable_if<!std::is_same<typename Derived::Scalar, cplx>::value>::type
282 const std::string& mat_file,
283 const std::string& var_name,
284 const std::string& mode)
295 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
297 throw std::runtime_error(
298 "qpp::saveMATLAB(): Can not open/create MATLAB file " 301 mxArray* pa = mxCreateDoubleMatrix(rA.rows(), rA.cols(), mxREAL);
303 throw std::runtime_error(
304 "qpp::saveMATLAB(): mxCreateDoubleMatrix failed!");
307 std::memcpy(mxGetPr(pa), rA.data(),
sizeof(double) * rA.size());
309 if (matPutVariable(pmat, var_name.c_str(), pa))
310 throw std::runtime_error(
311 "qpp::saveMATLAB(): Can not write the variable " 312 + var_name +
" to MATLAB file " + mat_file +
"!");
bool check_nonzero_size(const T &x) noexcept
Definition: util.h:129
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:77
Quantum++ main namespace.
Definition: codes.h:30
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:64
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:45
std::size_t idx
Non-negative integer index.
Definition: types.h:35
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:214
Object has zero size exception.
Definition: exception.h:134