K-order DLL, DynareNameList class: removed memory leaks
parent
d95b0cf6d7
commit
2449a4365c
|
@ -33,8 +33,8 @@
|
||||||
/* Dynare DynamicModel class */
|
/* Dynare DynamicModel class */
|
||||||
/**************************************************************************************/
|
/**************************************************************************************/
|
||||||
|
|
||||||
KordpDynare::KordpDynare(const char **endo, int num_endo,
|
KordpDynare::KordpDynare(const vector<string> &endo, int num_endo,
|
||||||
const char **exo, int nexog, int npar,
|
const vector<string> &exo, int nexog, int npar,
|
||||||
Vector &ysteady, TwoDMatrix &vcov, Vector &inParams, int nstat,
|
Vector &ysteady, TwoDMatrix &vcov, Vector &inParams, int nstat,
|
||||||
int npred, int nforw, int nboth, const int jcols, const Vector &nnzd,
|
int npred, int nforw, int nboth, const int jcols, const Vector &nnzd,
|
||||||
const int nsteps, int norder,
|
const int nsteps, int norder,
|
||||||
|
@ -312,52 +312,16 @@ KordpDynare::ReorderDynareJacobianIndices() throw (TLException)
|
||||||
/**************************************************************************************/
|
/**************************************************************************************/
|
||||||
/* DynareNameList class */
|
/* DynareNameList class */
|
||||||
/**************************************************************************************/
|
/**************************************************************************************/
|
||||||
vector<int>
|
|
||||||
DynareNameList::selectIndices(const vector<const char *> &ns) const throw (DynareException)
|
|
||||||
{
|
|
||||||
vector<int> res;
|
|
||||||
for (unsigned int i = 0; i < ns.size(); i++)
|
|
||||||
{
|
|
||||||
int j = 0;
|
|
||||||
while (j < getNum() && strcmp(getName(j), ns[i]) != 0)
|
|
||||||
j++;
|
|
||||||
if (j == getNum())
|
|
||||||
throw DynareException(__FILE__, __LINE__,
|
|
||||||
string("Couldn't find name for ") + ns[i]
|
|
||||||
+" in DynareNameList::selectIndices");
|
|
||||||
res.push_back(j);
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
DynareNameList::DynareNameList(const KordpDynare &dynare)
|
DynareNameList::DynareNameList(const KordpDynare &dynare, const vector<string> &names_arg) : names(names_arg)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < dynare.ny(); i++)
|
|
||||||
names.push_back(dynare.dnl.getName(i));
|
|
||||||
}
|
|
||||||
DynareNameList::DynareNameList(const KordpDynare &dynare, const char **namesp)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < dynare.ny(); i++)
|
|
||||||
names.push_back(namesp[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
DynareExogNameList::DynareExogNameList(const KordpDynare &dynare)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < dynare.nexog(); i++)
|
|
||||||
names.push_back(dynare.denl.getName(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
DynareExogNameList::DynareExogNameList(const KordpDynare &dynare, const char **namesp)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < dynare.nexog(); i++)
|
|
||||||
names.push_back(namesp[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DynareStateNameList::DynareStateNameList(const KordpDynare &dynare, const DynareNameList &dnl,
|
DynareStateNameList::DynareStateNameList(const KordpDynare &dynare, const DynareNameList &dnl,
|
||||||
const DynareExogNameList &denl)
|
const DynareNameList &denl)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < dynare.nys(); i++)
|
for (int i = 0; i < dynare.nys(); i++)
|
||||||
names.push_back(dnl.getName(i+dynare.nstat()));
|
names.push_back(string(dnl.getName(i+dynare.nstat())));
|
||||||
for (int i = 0; i < dynare.nexog(); i++)
|
for (int i = 0; i < dynare.nexog(); i++)
|
||||||
names.push_back(denl.getName(i));
|
names.push_back(string(denl.getName(i)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,10 +45,9 @@ class KordpDynare;
|
||||||
/*////////////////////////////////////////////*/
|
/*////////////////////////////////////////////*/
|
||||||
class DynareNameList : public NameList
|
class DynareNameList : public NameList
|
||||||
{
|
{
|
||||||
vector<const char *> names;
|
vector<string> names;
|
||||||
public:
|
public:
|
||||||
DynareNameList(const KordpDynare &dynare);
|
DynareNameList(const KordpDynare &dynare, const vector<string> &names_arg);
|
||||||
DynareNameList(const KordpDynare &dynare, const char **names);
|
|
||||||
int
|
int
|
||||||
getNum() const
|
getNum() const
|
||||||
{
|
{
|
||||||
|
@ -57,38 +56,16 @@ public:
|
||||||
const char *
|
const char *
|
||||||
getName(int i) const
|
getName(int i) const
|
||||||
{
|
{
|
||||||
return names[i];
|
return names[i].c_str();
|
||||||
}
|
|
||||||
/** This for each string of the input vector calculates its index
|
|
||||||
* in the names. And returns the resulting vector of indices. If
|
|
||||||
* the name cannot be found, then an exception is raised. */
|
|
||||||
vector<int> selectIndices(const vector<const char *> &ns) const throw (DynareException);
|
|
||||||
};
|
|
||||||
|
|
||||||
class DynareExogNameList : public NameList
|
|
||||||
{
|
|
||||||
vector<const char *> names;
|
|
||||||
public:
|
|
||||||
DynareExogNameList(const KordpDynare &dynare);
|
|
||||||
DynareExogNameList(const KordpDynare &dynare, const char **names);
|
|
||||||
int
|
|
||||||
getNum() const
|
|
||||||
{
|
|
||||||
return (int) names.size();
|
|
||||||
}
|
|
||||||
const char *
|
|
||||||
getName(int i) const
|
|
||||||
{
|
|
||||||
return names[i];
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class DynareStateNameList : public NameList
|
class DynareStateNameList : public NameList
|
||||||
{
|
{
|
||||||
vector<const char *> names;
|
vector<string> names;
|
||||||
public:
|
public:
|
||||||
DynareStateNameList(const KordpDynare &dynare, const DynareNameList &dnl,
|
DynareStateNameList(const KordpDynare &dynare, const DynareNameList &dnl,
|
||||||
const DynareExogNameList &denl);
|
const DynareNameList &denl);
|
||||||
int
|
int
|
||||||
getNum() const
|
getNum() const
|
||||||
{
|
{
|
||||||
|
@ -97,7 +74,7 @@ public:
|
||||||
const char *
|
const char *
|
||||||
getName(int i) const
|
getName(int i) const
|
||||||
{
|
{
|
||||||
return names[i];
|
return names[i].c_str();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
|
@ -108,7 +85,6 @@ class DynamicModelDLL;
|
||||||
class KordpDynare : public DynamicModel
|
class KordpDynare : public DynamicModel
|
||||||
{
|
{
|
||||||
friend class DynareNameList;
|
friend class DynareNameList;
|
||||||
friend class DynareExogNameList;
|
|
||||||
friend class DynareStateNameList;
|
friend class DynareStateNameList;
|
||||||
friend class DynamicModelDLL;
|
friend class DynamicModelDLL;
|
||||||
|
|
||||||
|
@ -131,8 +107,7 @@ class KordpDynare : public DynamicModel
|
||||||
Vector ¶ms;
|
Vector ¶ms;
|
||||||
TwoDMatrix &vCov;
|
TwoDMatrix &vCov;
|
||||||
TensorContainer<FSSparseTensor> md; // ModelDerivatives
|
TensorContainer<FSSparseTensor> md; // ModelDerivatives
|
||||||
DynareNameList dnl;
|
DynareNameList dnl, denl;
|
||||||
DynareExogNameList denl;
|
|
||||||
DynareStateNameList dsnl;
|
DynareStateNameList dsnl;
|
||||||
const double ss_tol;
|
const double ss_tol;
|
||||||
const vector<int> &varOrder;
|
const vector<int> &varOrder;
|
||||||
|
@ -140,8 +115,8 @@ class KordpDynare : public DynamicModel
|
||||||
double qz_criterium;
|
double qz_criterium;
|
||||||
vector<int> JacobianIndices;
|
vector<int> JacobianIndices;
|
||||||
public:
|
public:
|
||||||
KordpDynare(const char **endo, int num_endo,
|
KordpDynare(const vector<string> &endo, int num_endo,
|
||||||
const char **exo, int num_exo, int num_par,
|
const vector<string> &exo, int num_exo, int num_par,
|
||||||
Vector &ySteady, TwoDMatrix &vCov, Vector ¶ms, int nstat, int nPred,
|
Vector &ySteady, TwoDMatrix &vCov, Vector ¶ms, int nstat, int nPred,
|
||||||
int nforw, int nboth, const int nJcols, const Vector &NNZD,
|
int nforw, int nboth, const int nJcols, const Vector &NNZD,
|
||||||
const int nSteps, const int ord,
|
const int nSteps, const int ord,
|
||||||
|
|
|
@ -48,41 +48,23 @@
|
||||||
|
|
||||||
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE) // exclude mexFunction for other applications
|
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE) // exclude mexFunction for other applications
|
||||||
|
|
||||||
const char **
|
|
||||||
DynareMxArrayToString(const char *cNamesCharStr, const int len, const int width)
|
|
||||||
{
|
|
||||||
char **cNamesMX;
|
|
||||||
cNamesMX = (char **) calloc(len, sizeof(char *));
|
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
cNamesMX[i] = (char *) calloc(width+1, sizeof(char));
|
|
||||||
|
|
||||||
for (int i = 0; i < width; i++)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < len; j++)
|
|
||||||
{
|
|
||||||
// Allow alphanumeric and underscores "_" only:
|
|
||||||
if (isalnum(cNamesCharStr[j+i*len]) || ('_' == cNamesCharStr[j+i*len]))
|
|
||||||
{
|
|
||||||
cNamesMX[j][i] = cNamesCharStr[j+i*len];
|
|
||||||
}
|
|
||||||
else cNamesMX[j][i] = '\0';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (const char **) cNamesMX;
|
|
||||||
}
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////
|
||||||
// Convert Matlab Dynare endo and exo names array to C type array of string pointers
|
// Convert MATLAB Dynare endo and exo names array to a vector<string> array of string pointers
|
||||||
// Poblem is that Matlab mx function returns a long string concatenated by columns rather than rows
|
// Poblem is that Matlab mx function returns a long string concatenated by columns rather than rows
|
||||||
// hence a rather low level approach is needed
|
// hence a rather low level approach is needed
|
||||||
///////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////
|
||||||
const char **
|
void
|
||||||
DynareMxArrayToString(const mxArray *mxFldp, const int len, const int width)
|
DynareMxArrayToString(const mxArray *mxFldp, const int len, const int width, vector<string> &out)
|
||||||
{
|
{
|
||||||
char *cNamesCharStr = mxArrayToString(mxFldp);
|
char *cNamesCharStr = mxArrayToString(mxFldp);
|
||||||
const char **ret = DynareMxArrayToString(cNamesCharStr, len, width);
|
|
||||||
|
|
||||||
return ret;
|
out.resize(len);
|
||||||
|
|
||||||
|
for (int i = 0; i < width; i++)
|
||||||
|
for (int j = 0; j < len; j++)
|
||||||
|
// Allow alphanumeric and underscores "_" only:
|
||||||
|
if (isalnum(cNamesCharStr[j+i*len]) || (cNamesCharStr[j+i*len] == '_'))
|
||||||
|
out[j] += cNamesCharStr[j+i*len];
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -196,12 +178,14 @@ extern "C" {
|
||||||
mxFldp = mxGetField(M_, 0, "var_order_endo_names");
|
mxFldp = mxGetField(M_, 0, "var_order_endo_names");
|
||||||
const int nendo = (int) mxGetM(mxFldp);
|
const int nendo = (int) mxGetM(mxFldp);
|
||||||
const int widthEndo = (int) mxGetN(mxFldp);
|
const int widthEndo = (int) mxGetN(mxFldp);
|
||||||
const char **endoNamesMX = DynareMxArrayToString(mxFldp, nendo, widthEndo);
|
vector<string> endoNames;
|
||||||
|
DynareMxArrayToString(mxFldp, nendo, widthEndo, endoNames);
|
||||||
|
|
||||||
mxFldp = mxGetField(M_, 0, "exo_names");
|
mxFldp = mxGetField(M_, 0, "exo_names");
|
||||||
const int nexo = (int) mxGetM(mxFldp);
|
const int nexo = (int) mxGetM(mxFldp);
|
||||||
const int widthExog = (int) mxGetN(mxFldp);
|
const int widthExog = (int) mxGetN(mxFldp);
|
||||||
const char **exoNamesMX = DynareMxArrayToString(mxFldp, nexo, widthExog);
|
vector<string> exoNames;
|
||||||
|
DynareMxArrayToString(mxFldp, nexo, widthExog, exoNames);
|
||||||
|
|
||||||
if ((nEndo != nendo) || (nExog != nexo))
|
if ((nEndo != nendo) || (nExog != nexo))
|
||||||
mexErrMsgTxt("Incorrect number of input parameters.");
|
mexErrMsgTxt("Incorrect number of input parameters.");
|
||||||
|
@ -225,7 +209,7 @@ extern "C" {
|
||||||
tls.init(kOrder, nStat+2*nPred+3*nBoth+2*nForw+nExog);
|
tls.init(kOrder, nStat+2*nPred+3*nBoth+2*nForw+nExog);
|
||||||
|
|
||||||
// make KordpDynare object
|
// make KordpDynare object
|
||||||
KordpDynare dynare(endoNamesMX, nEndo, exoNamesMX, nExog, nPar, // paramNames,
|
KordpDynare dynare(endoNames, nEndo, exoNames, nExog, nPar,
|
||||||
ySteady, vCov, modParams, nStat, nPred, nForw, nBoth,
|
ySteady, vCov, modParams, nStat, nPred, nForw, nBoth,
|
||||||
jcols, NNZD, nSteps, kOrder, journal, dynamicDLL,
|
jcols, NNZD, nSteps, kOrder, journal, dynamicDLL,
|
||||||
sstol, var_order_vp, llincidence, qz_criterium);
|
sstol, var_order_vp, llincidence, qz_criterium);
|
||||||
|
|
Loading…
Reference in New Issue