27 #ifndef MATLAB_MATLAB_H_
28 #define MATLAB_MATLAB_H_
47 template<
typename Derived>
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;
184 double* pa_im =
nullptr;
187 pa_re =
reinterpret_cast<double*
>(mxGetPr(pa));
188 std::memcpy(result_re.data(), pa_re,
189 sizeof(double) * mxGetNumberOfElements(pa));
193 pa_im =
reinterpret_cast<double*
>(mxGetPi(pa));
194 std::memcpy(result_im.data(), pa_im,
195 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>
222 throw Exception(
"qpp::saveMATLABmatrix()",
241 const std::string& mat_file,
242 const std::string& var_name,
243 const std::string& mode)
245 const dmat& rA = A.derived();
253 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
255 throw std::runtime_error(
256 "qpp::saveMATLABmatrix(): Can not open/create MATLAB file "
259 mxArray* pa = mxCreateDoubleMatrix(rA.rows(), rA.cols(), mxREAL);
261 throw std::runtime_error(
262 "qpp::saveMATLABmatrix(): mxCreateDoubleMatrix failed!");
265 std::memcpy(mxGetPr(pa), rA.data(),
sizeof(double) * rA.size());
267 if (matPutVariable(pmat, var_name.c_str(), pa))
268 throw std::runtime_error(
269 "qpp::saveMATLABmatrix(): Can not write the variable "
270 + var_name +
" to MATLAB file " + mat_file +
"!");
291 const std::string& mat_file,
292 const std::string& var_name,
293 const std::string& mode)
295 const cmat& rA = A.derived();
304 dmat tmp_re = rA.real();
305 dmat tmp_im = rA.imag();
307 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
309 throw std::runtime_error(
310 "qpp::saveMATLABmatrix(): Can not open/create MATLAB file "
313 mxArray* pa = mxCreateDoubleMatrix(
314 tmp_re.rows(), tmp_re.cols(), mxCOMPLEX);
316 throw std::runtime_error(
317 "qpp::saveMATLABmatrix(): mxCreateDoubleMatrix failed!");
321 double* pa_re =
nullptr;
322 double* pa_im =
nullptr;
325 pa_re =
reinterpret_cast<double*
>(mxGetPr(pa));
326 std::memcpy(pa_re, tmp_re.data(),
sizeof(double) * tmp_re.size());
329 pa_im =
reinterpret_cast<double*
>(mxGetPi(pa));
330 std::memcpy(pa_im, tmp_im.data(),
sizeof(double) * tmp_im.size());
332 if (matPutVariable(pmat, var_name.c_str(), pa))
333 throw std::runtime_error(
334 "qpp::saveMATLABmatrix(): Can not write the variable "
335 + var_name +
" to MATLAB file " + mat_file +
"!");
bool check_nonzero_size(const T &x) noexcept
Definition: util.h:112
Eigen::MatrixXd dmat
Real (double precision) dynamic Eigen matrix.
Definition: types.h:66
void saveMATLABmatrix(const Eigen::MatrixBase< Derived > &, const std::string &, const std::string &, const std::string &)
Saves an Eigen dynamic matrix to a MATLAB .mat file, generic version.
Definition: matlab.h:217
Quantum++ main namespace.
Definition: codes.h:30
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:46
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
Derived loadMATLABmatrix(const std::string &, const std::string &)
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:61
std::size_t idx
Non-negative integer index.
Definition: types.h:36