K-order DLL: code simplification

time-shift
Sébastien Villemot 2010-09-20 19:24:23 +02:00
parent d7754ca288
commit aa8963c1db
4 changed files with 13 additions and 83 deletions

View File

@ -22,13 +22,8 @@
#include <sstream>
/***********************************
* Members of DynamicModelDLL for handling loading and calling
* <model>_dynamic () function
**************************************/
DynamicModelDLL::DynamicModelDLL(const string &modName, const int y_length, const int j_cols,
const int n_max_lag, const int n_exog, const Vector &ySteady_arg, const string &sExt) throw (DynareException) :
length(y_length), jcols(j_cols), nMax_lag(n_max_lag), nExog(n_exog), ySteady(ySteady_arg)
DynamicModelDLL::DynamicModelDLL(const string &modName, int nExog_arg, const string &sExt) throw (DynareException) :
nExog(nExog_arg)
{
string fName;
#if !defined(__CYGWIN32__) && !defined(_WIN32)
@ -94,59 +89,10 @@ DynamicModelDLL::~DynamicModelDLL()
}
void
DynamicModelDLL::eval(double *y, double *x, int nb_row_x, double *params,
int it_, double *residual, double *g1, double *g2, double *g3)
{
double *steady_state = const_cast<double *>(ySteady.base());
Dynamic(y, x, nb_row_x, params, steady_state, it_, residual, g1, g2, g3);
}
void
DynamicModelDLL::eval(const Vector &y, const TwoDMatrix &x, const Vector *modParams,
int it_, Vector &residual, TwoDMatrix *g1, TwoDMatrix *g2, TwoDMatrix *g3) throw (DynareException)
{
double *dresidual, *dg1 = NULL, *dg2 = NULL, *dg3 = NULL;
if ((jcols-nExog) != y.length())
throw DynareException(__FILE__, __LINE__, "DLL Error: (jcols-nExog)!=ys.length()");
if (g1 != NULL)
{
if (g1->nrows() != length)
throw DynareException(__FILE__, __LINE__, "DLL Error: g1 has wrong size");
dg1 = const_cast<double *>(g1->base());
}
if (g2 != NULL)
dg2 = const_cast<double *>(g2->base());
dresidual = const_cast<double *>(residual.base());
if (g3 != NULL)
dg3 = const_cast<double *>(g3->base());
dresidual = const_cast<double *>(residual.base());
double *dy = const_cast<double *>(y.base());
double *dx = const_cast<double *>(x.base());
double *dbParams = const_cast<double *>(modParams->base());
double *steady_state = const_cast<double *>(ySteady.base());
Dynamic(dy, dx, nExog, dbParams, steady_state, it_, dresidual, dg1, dg2, dg3);
}
void
DynamicModelDLL::eval(const Vector &y, const TwoDMatrix &x, const Vector *modParams,
DynamicModelDLL::eval(const Vector &y, const Vector &x, const Vector &modParams, const Vector &ySteady,
Vector &residual, TwoDMatrix *g1, TwoDMatrix *g2, TwoDMatrix *g3) throw (DynareException)
{
eval(y, x, modParams, nMax_lag, residual, g1, g2, g3);
}
void
DynamicModelDLL::eval(const Vector &y, const Vector &x, const Vector *modParams,
Vector &residual, TwoDMatrix *g1, TwoDMatrix *g2, TwoDMatrix *g3) throw (DynareException)
{
/** ignore given exogens and create new 2D x matrix since
* when calling <model>_dynamic(z,x,params,it_) x must be equal to
* zeros(M_.maximum_lag+1,M_.exo_nbr)
**/
TwoDMatrix mx(nMax_lag+1, nExog);
mx.zeros(); // initialise shocks to 0s
eval(y, mx, modParams, nMax_lag, residual, g1, g2, g3);
Dynamic(y.base(), x.base(), nExog, modParams.base(), ySteady.base(), 0, residual.base(),
g1 == NULL ? NULL : g1->base(),
g2 == NULL ? NULL : g2->base(), g3 == NULL ? NULL : g3->base());
}

View File

@ -29,8 +29,8 @@
#include "dynare_exception.h"
// <model>_Dynamic DLL pointer
typedef void (*DynamicFn)
(double *y, double *x, int nb_row_x, double *params, double *steady_state,
typedef void (*DynamicFn)
(const double *y, const double *x, int nb_row_x, const double *params, const double *steady_state,
int it_, double *residual, double *g1, double *g2, double *g3);
/**
@ -40,13 +40,8 @@ typedef void (*DynamicFn)
class DynamicModelDLL
{
private:
DynamicFn Dynamic; // pointer to the Dynamic function in DLL
const int length; // tot num vars = Num of Jacobian rows
const int jcols; // tot num var t-1, t and t+1 instances + exogs = Num of Jacobian columns
const int nMax_lag; // no of lags
DynamicFn Dynamic; // pointer to the Dynamic function in DLL
const int nExog; // no of exogenous
const Vector &ySteady;
#if defined(_WIN32) || defined(__CYGWIN32__)
HINSTANCE dynamicHinstance; // DLL instance pointer in Windows
#else
@ -55,17 +50,9 @@ private:
public:
// construct and load Dynamic model DLL
DynamicModelDLL(const string &fname, const int length, const int jcols,
const int nMax_lag, const int nExog, const Vector &ySteady_arg, const string &sExt) throw (DynareException);
DynamicModelDLL(const string &fname, int nExog_arg, const string &sExt) throw (DynareException);
virtual ~DynamicModelDLL();
// evaluate Dynamic model DLL
void eval(double *y, double *x, int nb_row_x, double *params,
int it_, double *residual, double *g1, double *g2, double *g3);
void eval(const Vector &y, const Vector &x, const Vector *params,
Vector &residual, TwoDMatrix *g1, TwoDMatrix *g2, TwoDMatrix *g3) throw (DynareException);
void eval(const Vector &y, const TwoDMatrix &x, const Vector *params,
int it_, Vector &residual, TwoDMatrix *g1, TwoDMatrix *g2, TwoDMatrix *g3) throw (DynareException);
void eval(const Vector &y, const TwoDMatrix &x, const Vector *params,
void eval(const Vector &y, const Vector &x, const Vector &params, const Vector &ySteady,
Vector &residual, TwoDMatrix *g1, TwoDMatrix *g2, TwoDMatrix *g3) throw (DynareException);
};

View File

@ -113,7 +113,7 @@ KordpDynare::calcDerivativesAtSteady() throw (DynareException)
Vector llxSteady(nJcols-nExog);
LLxSteady(ySteady, llxSteady);
dynamicDLL.eval(llxSteady, xx, &params, out, &g1, g2p, g3p);
dynamicDLL.eval(llxSteady, xx, params, ySteady, out, &g1, g2p, g3p);
populateDerivativesContainer(g1, 1, JacobianIndices);

View File

@ -134,9 +134,6 @@ extern "C" {
const int nEndo = (int) mxGetScalar(mxFldp);
mxFldp = mxGetField(M_, 0, "param_nbr");
const int nPar = (int) mxGetScalar(mxFldp);
// it_ should be set to M_.maximum_lag
mxFldp = mxGetField(M_, 0, "maximum_lag");
const int nMax_lag = (int) mxGetScalar(mxFldp);
nPred -= nBoth; // correct nPred for nBoth.
@ -195,7 +192,7 @@ extern "C" {
std::string jName(fName); //params.basename);
jName += ".jnl";
Journal journal(jName.c_str());
DynamicModelDLL dynamicDLL(fName, nEndo, jcols, nMax_lag, nExog, ySteady, dfExt);
DynamicModelDLL dynamicDLL(fName, nExog, dfExt);
// intiate tensor library
tls.init(kOrder, nStat+2*nPred+3*nBoth+2*nForw+nExog);