/*
* Copyright (C) 2010-2013 Dynare Team
*
* This file is part of Dynare.
*
* Dynare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dynare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dynare. If not, see .
*/
/*
* This mex file computes particles at time t+1 given particles and innovations at time t,
* using a second order approximation of the nonlinear state space model.
*/
#include
#include
#include
#include
#include
#ifdef USE_OMP
#include
#endif
using namespace std;
#define FIRST_ORDER_LOOP 1// Comment out this line to use mkl-blas instead of loops when computing ghx*yhat and ghu*epsilon
void set_vector_of_indices(const int n, const int r, vector &v1, vector &v2, vector &v3)
{
const int m = n*(n+1)/2;
v1.resize(m,0);
v2.resize(m,0);
v3.resize(m,0);
for(int i=0, index=0, jndex=0;i ii1, ii2, ii3;// vector indices for ghxx
vector jj1, jj2, jj3;// vector indices for ghuu
set_vector_of_indices(n, m, ii1, ii2, ii3);
set_vector_of_indices(q, m, jj1, jj2, jj3);
#ifdef USE_OMP
#pragma omp parallel for num_threads(number_of_threads)
#endif
for (int particle = 0; particle ii1, ii2, ii3;// vector indices for ghxx
vector jj1, jj2, jj3;// vector indices for ghuu
set_vector_of_indices(n, m, ii1, ii2, ii3);
set_vector_of_indices(q, m, jj1, jj2, jj3);
#ifdef USE_OMP
#pragma omp parallel for num_threads(number_of_threads)
#endif
for (int particle = 0; particle 2)
{
mexErrMsgTxt("Too many output arguments.");
}
// Get dimensions.
size_t n = mxGetM(prhs[0]);// Number of states.
size_t s = mxGetN(prhs[0]);// Number of particles.
size_t q = mxGetM(prhs[1]);// Number of innovations.
size_t m = mxGetM(prhs[2]);// Number of elements in the union of states and observed variables.
//mexPrintf("\n s (the number of column of yhat) is equal to %d.", s);
//mexPrintf("\n The number of column of epsilon is %d.", mxGetN(prhs[1]));
// Check the dimensions.
if (
(s != mxGetN(prhs[1])) || // Number of columns for epsilon
(n != mxGetN(prhs[2])) || // Number of columns for ghx
(m != mxGetM(prhs[3])) || // Number of rows for ghu
(q != mxGetN(prhs[3])) || // Number of columns for ghu
(m != mxGetM(prhs[4])) || // Number of rows for 2nd order constant correction + deterministic steady state
(m != mxGetM(prhs[5])) || // Number of rows for ghxx
(n*n != mxGetN(prhs[5])) || // Number of columns for ghxx
(m != mxGetM(prhs[6])) || // Number of rows for ghuu
(q*q != mxGetN(prhs[6])) || // Number of columns for ghuu
(m != mxGetM(prhs[7])) || // Number of rows for ghxu
(n*q != mxGetN(prhs[7])) // Number of rows for ghxu
)
{
mexErrMsgTxt("Input dimension mismatch!.");
}
if (nrhs>9)
{
if (
(n != mxGetM(prhs[8])) || // Number of rows for yhat_
(s != mxGetN(prhs[8])) || // Number of columns for yhat_
(m != mxGetM(prhs[9])) // Number of rows for ss
)
{
mexErrMsgTxt("Input dimension mismatch!.");
}
}
// Get Input arrays.
double *yhat = mxGetPr(prhs[0]);
double *epsilon = mxGetPr(prhs[1]);
double *ghx = mxGetPr(prhs[2]);
double *ghu = mxGetPr(prhs[3]);
double *constant = mxGetPr(prhs[4]);
double *ghxx = mxGetPr(prhs[5]);
double *ghuu = mxGetPr(prhs[6]);
double *ghxu = mxGetPr(prhs[7]);
double *yhat_ = NULL, *ss = NULL;
if (nrhs>9)
{
yhat_ = mxGetPr(prhs[8]);
ss = mxGetPr(prhs[9]);
}
if (nrhs==9)
{
int numthreads = (int) mxGetScalar(prhs[8]);
double *y;
plhs[0] = mxCreateDoubleMatrix(m, s, mxREAL);
y = mxGetPr(plhs[0]);
ss2Iteration(y, yhat, epsilon, ghx, ghu, constant, ghxx, ghuu, ghxu, (int) m, (int) n, (int) q, (int) s, numthreads);
}
else
{
int numthreads = (int) mxGetScalar(prhs[10]);
double *y, *y_;
plhs[0] = mxCreateDoubleMatrix(m, s, mxREAL);
plhs[1] = mxCreateDoubleMatrix(m, s, mxREAL);
y = mxGetPr(plhs[0]);
y_ = mxGetPr(plhs[1]);
ss2Iteration_pruning(y, y_, yhat, yhat_, epsilon, ghx, ghu, constant, ghxx, ghuu, ghxu, ss, (int) m, (int) n, (int) q, (int) s, numthreads);
}
}