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");
85 throw std::runtime_error(
86 "qpp::loadMATLABmatrix(): Can not open MATLAB file "
90 mxArray* pa = matGetVariable(pmat, var_name.c_str());
92 throw std::runtime_error(
93 "qpp::loadMATLABmatrix(): Can not load the variable "
94 + var_name +
" from MATLAB file " + mat_file +
"!");
96 if (mxGetNumberOfDimensions(pa) != 2)
97 throw std::runtime_error(
98 "qpp::loadMATLABmatrix(): Loaded variable " + var_name
99 +
" is not 2-dimensional!");
102 throw std::runtime_error(
103 "qpp::loadMATLABmatrix(): Loaded variable " + var_name
104 +
" is not in double-precision format!");
106 idx rows = mxGetM(pa);
107 idx cols = mxGetN(pa);
109 dmat result(rows, cols);
111 std::memcpy(result.data(), mxGetPr(pa),
112 sizeof(double) * mxGetNumberOfElements(pa));
142 const std::string& var_name)
144 MATFile* pmat = matOpen(mat_file.c_str(),
"r");
147 throw std::runtime_error(
148 "qpp::loadMATLABmatrix(): Can not open MATLAB file "
152 mxArray* pa = matGetVariable(pmat, var_name.c_str());
154 throw std::runtime_error(
155 "qpp::loadMATLABmatrix(): Can not load the variable "
156 + var_name +
" from MATLAB file " + mat_file +
"!");
158 if (mxGetNumberOfDimensions(pa) != 2)
159 throw std::runtime_error(
160 "qpp::loadMATLABmatrix(): Loaded variable " + var_name
161 +
" is not 2-dimensional!");
164 throw std::runtime_error(
165 "qpp::loadMATLABmatrix(): Loaded variable " + var_name
166 +
" is not in double-precision format!");
168 idx rows = mxGetM(pa);
169 idx cols = mxGetN(pa);
171 dmat result_re(rows, cols);
172 dmat result_im(rows, cols);
175 double* pa_re =
nullptr, * pa_im =
nullptr;
178 pa_re =
static_cast<double*
>(mxGetPr(pa));
179 std::memcpy(result_re.data(), pa_re,
180 sizeof(double) * mxGetNumberOfElements(pa));
184 pa_im =
static_cast<double*
>(mxGetPi(pa));
185 std::memcpy(result_im.data(), pa_im,
186 sizeof(double) * mxGetNumberOfElements(pa));
190 std::memset(result_im.data(), 0,
191 sizeof(double) * mxGetNumberOfElements(pa));
197 return (result_re.cast<
cplx>()) + 1_i * (result_im.cast<
cplx>());
208 template<
typename Derived>
210 const std::string& mat_file,
const std::string& var_name,
211 const std::string& mode)
213 throw Exception(
"qpp::saveMATLABmatrix()",
232 const std::string& mat_file,
233 const std::string& var_name,
234 const std::string& mode)
242 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
244 throw std::runtime_error(
245 "qpp::saveMATLABmatrix(): Can not open/create MATLAB file "
248 mxArray* pa = mxCreateDoubleMatrix(rA.rows(), rA.cols(), mxREAL);
250 throw std::runtime_error(
251 "qpp::saveMATLABmatrix(): mxCreateDoubleMatrix failed!");
253 std::memcpy(mxGetPr(pa), rA.data(),
sizeof(double) * rA.size());
255 if (matPutVariable(pmat, var_name.c_str(), pa))
256 throw std::runtime_error(
257 "qpp::saveMATLABmatrix(): Can not write the variable "
258 + var_name +
" to MATLAB file " + mat_file +
"!");
279 const std::string& mat_file,
280 const std::string& var_name,
281 const std::string& mode)
290 dmat tmp_re = rA.real();
291 dmat tmp_im = rA.imag();
293 MATFile* pmat = matOpen(mat_file.c_str(), mode.c_str());
295 throw std::runtime_error(
296 "qpp::saveMATLABmatrix(): Can not open/create MATLAB file "
299 mxArray* pa = mxCreateDoubleMatrix(
300 tmp_re.rows(), tmp_re.cols(), mxCOMPLEX);
302 throw std::runtime_error(
303 "qpp::saveMATLABmatrix(): mxCreateDoubleMatrix failed!");
305 double* pa_re, * pa_im;
308 pa_re =
static_cast<double*
>(mxGetPr(pa));
309 std::memcpy(pa_re, tmp_re.data(),
sizeof(double) * tmp_re.size());
312 pa_im =
static_cast<double*
>(mxGetPi(pa));
313 std::memcpy(pa_im, tmp_im.data(),
sizeof(double) * tmp_im.size());
315 if (matPutVariable(pmat, var_name.c_str(), pa))
316 throw std::runtime_error(
317 "qpp::saveMATLABmatrix(): Can not write the variable "
318 + 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:209
std::size_t idx
Non-negative integer index.
Definition: types.h:36