| 1 |
/* |
| 2 |
* Copyright (C) 1996-2011 Daniel Waggoner and Tao Zha |
| 3 |
* |
| 4 |
* This free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* It is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* If you did not received a copy of the GNU General Public License |
| 15 |
* with this software, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <string> |
| 19 |
|
| 20 |
#if defined(MATLAB_MEX_FILE) |
| 21 |
# include <dynmex.h> |
| 22 |
# include "mat.h" |
| 23 |
#else |
| 24 |
# include <octave/oct.h> |
| 25 |
# include <octave/interpreter.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
extern "C" |
| 29 |
{ |
| 30 |
#include "VARio.h" |
| 31 |
|
| 32 |
/* |
| 33 |
Writes A0, Aplus, Zeta and the Base Transition Matrix to either |
| 34 |
a Matlab or an Octave .mat file |
| 35 |
*/ |
| 36 |
int Write_VAR_Parameters_Mat(TStateModel *model, char *out_tag) |
| 37 |
{ |
| 38 |
T_VAR_Parameters *p=(T_VAR_Parameters*)(model->theta); |
| 39 |
|
| 40 |
TMatrix* A0 = MakeA0_All(nullptr, p); |
| 41 |
TMatrix* Aplus = MakeAplus_All(nullptr, p); |
| 42 |
TMatrix* Zeta = MakeZeta_All(nullptr, p); |
| 43 |
TMatrix Q = model->sv->baseQ; |
| 44 |
|
| 45 |
|
| 46 |
// Create variables in a format suitable for writing to the MAT file |
| 47 |
|
| 48 |
int nrows, ncols; |
| 49 |
double *data; |
| 50 |
#if defined(MATLAB_MEX_FILE) |
| 51 |
mwSize dims[3]; |
| 52 |
#endif |
| 53 |
|
| 54 |
/* A0 */ |
| 55 |
nrows = RowM(A0[0]); |
| 56 |
ncols = ColM(A0[0]); |
| 57 |
#if defined(MATLAB_MEX_FILE) |
| 58 |
dims[0] = nrows; |
| 59 |
dims[1] = ncols; |
| 60 |
dims[2] = p->nstates; |
| 61 |
mxArray* A0mat = mxCreateNumericArray(3, (mwSize *)dims, mxDOUBLE_CLASS, mxREAL); |
| 62 |
data = mxGetPr(A0mat); |
| 63 |
#else |
| 64 |
NDArray A0mat {dim_vector{nrows, ncols, p->nstates}}; |
| 65 |
# if OCTAVE_MAJOR_VERSION >= 10 |
| 66 |
data = A0mat.rwdata(); |
| 67 |
# else |
| 68 |
data = A0mat.fortran_vec(); |
| 69 |
# endif |
| 70 |
#endif |
| 71 |
for (int s=0; s < p->nstates; s++) |
| 72 |
for (int i=0; i < nrows; i++) |
| 73 |
for (int j=0; j < ncols; j++) |
| 74 |
data[s*(nrows*ncols)+i+j*nrows] = (double)(ElementM(A0[s], i, j)); |
| 75 |
|
| 76 |
/* Aplus */ |
| 77 |
nrows = RowM(Aplus[0]); |
| 78 |
ncols = ColM(Aplus[0]); |
| 79 |
#if defined(MATLAB_MEX_FILE) |
| 80 |
dims[0] = nrows; |
| 81 |
dims[1] = ncols; |
| 82 |
mxArray* Aplusmat = mxCreateNumericArray(3, (mwSize *)dims, mxDOUBLE_CLASS, mxREAL); |
| 83 |
data = mxGetPr(Aplusmat); |
| 84 |
#else |
| 85 |
NDArray Aplusmat {dim_vector{nrows, ncols, p->nstates}}; |
| 86 |
# if OCTAVE_MAJOR_VERSION >= 10 |
| 87 |
data = Aplusmat.rwdata(); |
| 88 |
# else |
| 89 |
data = Aplusmat.fortran_vec(); |
| 90 |
# endif |
| 91 |
#endif |
| 92 |
for (int s=0; s < p->nstates; s++) |
| 93 |
for (int i=0; i < nrows; i++) |
| 94 |
for (int j=0; j < ncols; j++) |
| 95 |
data[s*(nrows*ncols)+i+j*nrows] = (double)(ElementM(Aplus[s], i, j)); |
| 96 |
|
| 97 |
/* Zeta */ |
| 98 |
nrows = RowM(Zeta[0]); |
| 99 |
ncols = ColM(Zeta[0]); |
| 100 |
#if defined(MATLAB_MEX_FILE) |
| 101 |
dims[0] = nrows; |
| 102 |
dims[1] = ncols; |
| 103 |
mxArray* Zetamat = mxCreateNumericArray(3, (mwSize *)dims, mxDOUBLE_CLASS, mxREAL); |
| 104 |
data = mxGetPr(Zetamat); |
| 105 |
#else |
| 106 |
NDArray Zetamat {dim_vector{nrows, ncols, p->nstates}}; |
| 107 |
# if OCTAVE_MAJOR_VERSION >= 10 |
| 108 |
data = Zetamat.rwdata(); |
| 109 |
# else |
| 110 |
data = Zetamat.fortran_vec(); |
| 111 |
# endif |
| 112 |
#endif |
| 113 |
for (int s=0; s < p->nstates; s++) |
| 114 |
for (int i=0; i < nrows; i++) |
| 115 |
for (int j=0; j < ncols; j++) |
| 116 |
data[s*(nrows*ncols)+i+j*nrows] = (double)(ElementM(Zeta[s], i, j)); |
| 117 |
|
| 118 |
/* Q */ |
| 119 |
nrows = RowM(Q); |
| 120 |
ncols = ColM(Q); |
| 121 |
#if defined(MATLAB_MEX_FILE) |
| 122 |
dims[0] = nrows = RowM(Q); |
| 123 |
dims[1] = ncols = ColM(Q); |
| 124 |
dims[2] = 0; |
| 125 |
mxArray* Qmat = mxCreateNumericArray(2, (mwSize *)dims, mxDOUBLE_CLASS, mxREAL); |
| 126 |
data = mxGetPr(Qmat); |
| 127 |
#else |
| 128 |
Matrix Qmat {nrows, ncols}; |
| 129 |
# if OCTAVE_MAJOR_VERSION >= 10 |
| 130 |
data = Qmat.rwdata(); |
| 131 |
# else |
| 132 |
data = Qmat.fortran_vec(); |
| 133 |
# endif |
| 134 |
#endif |
| 135 |
for (int i=0; i < nrows; i++) |
| 136 |
for (int j=0; j < ncols; j++) |
| 137 |
data[i+j*nrows] = (double)(ElementM(Q, i, j)); |
| 138 |
|
| 139 |
|
| 140 |
// Write the file |
| 141 |
|
| 142 |
std::string fullFilename {out_tag}; |
| 143 |
fullFilename += ".mat"; |
| 144 |
|
| 145 |
#if defined(MATLAB_MEX_FILE) |
| 146 |
MATFile* matFile = matOpen(fullFilename.c_str(), "wz"); |
| 147 |
if (matFile == nullptr) |
| 148 |
mexErrMsgTxt(("Error opening mat file " + fullFilename + " in Write_VAR_Parameters_Mat.").c_str()); |
| 149 |
int status; |
| 150 |
|
| 151 |
status = matPutVariable(matFile, "A0", A0mat); |
| 152 |
if (status != 0) |
| 153 |
mexErrMsgTxt("Error writing A0 to matfile in Write_VAR_Parameters_Mat."); |
| 154 |
|
| 155 |
status = matPutVariable(matFile, "Aplus", Aplusmat); |
| 156 |
if (status != 0) |
| 157 |
mexErrMsgTxt("Error writing Aplus to matfile in Write_VAR_Parameters_Mat."); |
| 158 |
|
| 159 |
status = matPutVariable(matFile, "Zeta", Zetamat); |
| 160 |
if (status != 0) |
| 161 |
mexErrMsgTxt("Error writing Zeta to matfile in Write_VAR_Parameters_Mat."); |
| 162 |
|
| 163 |
status = matPutVariable(matFile, "Q", Qmat); |
| 164 |
if (status != 0) |
| 165 |
mexErrMsgTxt("Error writing Q to matfile in Write_VAR_Parameters_Mat."); |
| 166 |
|
| 167 |
matClose(matFile); |
| 168 |
#else |
| 169 |
auto* interp = octave::interpreter::the_interpreter(); |
| 170 |
|
| 171 |
interp->assign("A0", A0mat); |
| 172 |
interp->assign("Aplus", Aplusmat); |
| 173 |
interp->assign("Zeta", Zetamat); |
| 174 |
interp->assign("Q", Qmat); |
| 175 |
|
| 176 |
auto& lss = interp->get_load_save_system(); |
| 177 |
|
| 178 |
octave_value_list save_args; |
| 179 |
save_args(0) = "-mat"; |
| 180 |
save_args(1) = fullFilename; |
| 181 |
save_args(2) = "A0"; |
| 182 |
save_args(3) = "Aplus"; |
| 183 |
save_args(4) = "Zeta"; |
| 184 |
save_args(5) = "Q"; |
| 185 |
|
| 186 |
lss.save(save_args); |
| 187 |
#endif |
| 188 |
|
| 189 |
for (int s=0; s < p->nstates; s++) |
| 190 |
{ |
| 191 |
FreeMatrix(A0[s]); |
| 192 |
FreeMatrix(Aplus[s]); |
| 193 |
FreeMatrix(Zeta[s]); |
| 194 |
} |
| 195 |
return 1; |
| 196 |
} |
| 197 |
|
| 198 |
} // extern "C" |