27 #ifndef MATLAB_MATLAB_H_
28 #define MATLAB_MATLAB_H_
47 template<
typename Derived>
49 const std::string& var_name)
51 throw Exception(
"qpp::loadMATLABmatrix()",
80 const std::string& var_name)
82 MATFile* pmat = matOpen(mat_file.c_str(),
"r");
88 throw std::runtime_error(
89 "qpp::loadMATLABmatrix(): Can not open MATLAB file "
93 mxArray* pa = matGetVariable(pmat, var_name.c_str());
95 throw std::runtime_error(
96 "qpp::loadMATLABmatrix(): Can not load the variable "
97 + var_name +
" from MATLAB file " + mat_file +
"!");
99 if (mxGetNumberOfDimensions(pa) != 2)
100 throw std::runtime_error(
101 "qpp::loadMATLABmatrix(): Loaded variable " + var_name
102 +
" is not 2-dimensional!");
105 throw std::runtime_error(
106 "qpp::loadMATLABmatrix(): Loaded variable " + var_name
107 +
" is not in double-precision format!");
110 idx rows = mxGetM(pa);
111 idx cols = mxGetN(pa);
113 dmat result(rows, cols);
115 std::memcpy(result.data(), mxGetPr(pa),
116 sizeof(double) * mxGetNumberOfElements(pa));
146 const std::string& var_name)
148 MATFile* pmat = matOpen(mat_file.c_str(),
"r");
154 throw std::runtime_error(
155 "qpp::loadMATLABmatrix(): Can not open MATLAB file "
159 mxArray* pa = matGetVariable(pmat, var_name.c_str());
161 throw std::runtime_error(
162 "qpp::loadMATLABmatrix(): Can not load the variable "
163 + var_name +
" from MATLAB file " + mat_file +
"!");
165 if (mxGetNumberOfDimensions(pa) != 2)
166 throw std::runtime_error(
167 "qpp::loadMATLABmatrix(): Loaded variable " + var_name
168 +
" is not 2-dimensional!");
171 throw std::runtime_error(
172 "qpp::loadMATLABmatrix(): Loaded variable " + var_name
173 +
" is not in double-precision format!");
176 idx rows = mxGetM(pa);
177 idx cols = mxGetN(pa);
179 dmat result_re(rows, cols);
180 dmat result_im(rows, cols);
183 double* pa_re =
nullptr, * pa_im =
nullptr;
186 pa_re =
static_cast<double*
>(mxGetPr(pa));
187 std::memcpy(result_re.data(), pa_re,
188 sizeof(double) * mxGetNumberOfElements(pa));
192 pa_im =
static_cast<double*
>(mxGetPi(pa));
193 std::memcpy(result_im.data(), pa_im,
194 sizeof(double) * mxGetNumberOfElements(pa));
198 std::memset(result_im.data(), 0,
199 sizeof(double) * mxGetNumberOfElements(pa));
205 return (result_re.cast<
cplx>()) + 1_i * (result_im.cast<
cplx>());
216 template<
typename Derived>
218 const std::string& mat_file,
const std::string& var_name,
219 const std::string& mode)
221 throw Exception(
"qpp::saveMATLABmatrix()",
240 const std::string& mat_file,
241 const std::string& var_name,
242 const std::string& mode)
252 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
254 throw std::runtime_error(
255 "qpp::saveMATLABmatrix(): Can not open/create MATLAB file "
258 mxArray* pa = mxCreateDoubleMatrix(rA.rows(), rA.cols(), mxREAL);
260 throw std::runtime_error(
261 "qpp::saveMATLABmatrix(): mxCreateDoubleMatrix failed!");
264 std::memcpy(mxGetPr(pa), rA.data(),
sizeof(double) * rA.size());
266 if (matPutVariable(pmat, var_name.c_str(), pa))
267 throw std::runtime_error(
268 "qpp::saveMATLABmatrix(): Can not write the variable "
269 + var_name +
" to MATLAB file " + mat_file +
"!");
290 const std::string& mat_file,
291 const std::string& var_name,
292 const std::string& mode)
303 dmat tmp_re = rA.real();
304 dmat tmp_im = rA.imag();
306 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
308 throw std::runtime_error(
309 "qpp::saveMATLABmatrix(): Can not open/create MATLAB file "
312 mxArray* pa = mxCreateDoubleMatrix(
313 tmp_re.rows(), tmp_re.cols(), mxCOMPLEX);
315 throw std::runtime_error(
316 "qpp::saveMATLABmatrix(): mxCreateDoubleMatrix failed!");
319 double* pa_re, * pa_im;
322 pa_re =
static_cast<double*
>(mxGetPr(pa));
323 std::memcpy(pa_re, tmp_re.data(),
sizeof(double) * tmp_re.size());
326 pa_im =
static_cast<double*
>(mxGetPi(pa));
327 std::memcpy(pa_im, tmp_im.data(),
sizeof(double) * tmp_im.size());
329 if (matPutVariable(pmat, var_name.c_str(), pa))
330 throw std::runtime_error(
331 "qpp::saveMATLABmatrix(): Can not write the variable "
332 + var_name +
" to MATLAB file " + mat_file +
"!");
Eigen::MatrixXd dmat
Real (double precision) dynamic Eigen matrix.
Definition: types.h:71
Quantum++ main namespace.
Definition: codes.h:30
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:51
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
bool _check_nonzero_size(const T &x) noexcept
Definition: util.h:113
Derived loadMATLABmatrix(const std::string &mat_file, const std::string &var_name)
Loads an Eigen dynamic matrix from a MATLAB .mat file, generic version.
Definition: matlab.h:48
Eigen::MatrixXcd cmat
Complex (double precision) dynamic Eigen matrix.
Definition: types.h:66
void saveMATLABmatrix(const Eigen::MatrixBase< Derived > &A, const std::string &mat_file, const std::string &var_name, const std::string &mode)
Saves an Eigen dynamic matrix to a MATLAB .mat file, generic version.
Definition: matlab.h:217
std::size_t idx
Non-negative integer index.
Definition: types.h:36