/* * Copyright (C) 2007-2009 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 A*kron(B,C) or A*kron(B,B) without explicitly building kron(B,C) or kron(B,B), so that * one can consider large matrices A, B and/or C, and assuming that A is a the hessian of a dsge model * (dynare format). This mex file should not be used outside dr1.m. */ #include #include "mex.h" #ifdef NO_OPENMP #define USE_OMP 0 #define DEBUG_OMP 0 #else #define USE_OMP 1 #define DEBUG_OMP 0 #include #endif #ifdef MWTYPES_NOT_DEFINED typedef int mwIndex; typedef int mwSize; #endif void sparse_hessian_times_B_kronecker_B(mwIndex *isparseA, mwIndex *jsparseA, double *vsparseA, double *B, double *D, mwSize mA, mwSize nA, mwSize mB, mwSize nB) { /* ** Loop over the columns of kron(B,B) (or of the result matrix D). ** This loop is splitted into two nested loops because we use the ** symmetric pattern of the hessian matrix. */ #if USE_OMP #pragma omp parallel for num_threads(atoi(getenv("DYNARE_NUM_THREADS"))) #endif for(int j1B=0; j1B0) { memcpy(&D[(j2B*nB+j1B)*mA],&D[jj*mA],mA*sizeof(double)); } } } } void sparse_hessian_times_B_kronecker_C(mwIndex *isparseA, mwIndex *jsparseA, double *vsparseA, double *B, double *C, double *D, mwSize mA, mwSize nA, mwSize mB, mwSize nB, mwSize mC, mwSize nC) { /* ** Loop over the columns of kron(B,B) (or of the result matrix D). */ #if USE_OMP #pragma omp parallel for num_threads(atoi(getenv("DYNARE_NUM_THREADS"))) #endif for(long int jj=0; jj 3) || (nrhs <2) ) { mexErrMsgTxt("Two or Three input arguments required."); } if (nlhs>1) { mexErrMsgTxt("Too many output arguments."); } if (!mxIsSparse(prhs[0])) { mexErrMsgTxt("First input must be a sparse (dynare) hessian matrix."); } // Get & Check dimensions (columns and rows): mwSize mA, nA, mB, nB, mC, nC; mA = mxGetM(prhs[0]); nA = mxGetN(prhs[0]); mB = mxGetM(prhs[1]); nB = mxGetN(prhs[1]); if (nrhs == 3)// A*kron(B,C) is to be computed. { mC = mxGetM(prhs[2]); nC = mxGetN(prhs[2]); if (mB*mC != nA) { mexErrMsgTxt("Input dimension error!"); } } else// A*kron(B,B) is to be computed. { if (mB*mB != nA) { mexErrMsgTxt("Input dimension error!"); } } // Get input matrices: double *B, *C; B = mxGetPr(prhs[1]); if (nrhs == 3) { C = mxGetPr(prhs[2]); } // Sparse (dynare) hessian matrix. mwIndex *isparseA = (mwIndex*)mxGetIr(prhs[0]); mwIndex *jsparseA = (mwIndex*)mxGetJc(prhs[0]); double *vsparseA = mxGetPr(prhs[0]); // Initialization of the ouput: double *D; if (nrhs == 3) { plhs[0] = mxCreateDoubleMatrix(mA,nB*nC,mxREAL); } else { plhs[0] = mxCreateDoubleMatrix(mA,nB*nB,mxREAL); } D = mxGetPr(plhs[0]); // Computational part: if (nrhs == 2) { sparse_hessian_times_B_kronecker_B(isparseA, jsparseA, vsparseA, B, D, mA, nA, mB, nB); } else { sparse_hessian_times_B_kronecker_C(isparseA, jsparseA, vsparseA, B, C, D, mA, nA, mB, nB, mC, nC); } }