dynamic_abstract_class.cc 2.22 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2014 Dynare Team
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

20 21
#include <assert.h>

22 23 24 25 26
#include "dynamic_abstract_class.hh"

void
DynamicModelAC::copyDoubleIntoTwoDMatData(double *dm, TwoDMatrix *tdm, int rows, int cols)
{
27 28 29
  assert(rows == tdm->nrows());
  assert(cols == tdm->ncols());

30
  int dmIdx = 0;
31 32 33
  for (int j = 0; j < cols; j++)
    for (int i = 0; i < rows; i++)
      tdm->get(i, j) = dm[dmIdx++];
34 35
}

36 37
void
DynamicModelAC::unpackSparseMatrixAndCopyIntoTwoDMatData(mxArray *sparseMat, TwoDMatrix *tdm)
38 39 40 41 42 43
{
  int totalCols = mxGetN(sparseMat);
  mwIndex *rowIdxVector = mxGetIr(sparseMat);
  mwSize sizeRowIdxVector = mxGetNzmax(sparseMat);
  mwIndex *colIdxVector = mxGetJc(sparseMat);

44 45 46
  assert(tdm->ncols() == 3);
  assert(tdm->nrows() == sizeRowIdxVector);

47 48 49
  double *ptr = mxGetPr(sparseMat);

  int rind = 0;
50
  int output_row = 0;
51

52 53
  for (int i = 0; i < totalCols; i++)
    for (int j = 0; j < (int) (colIdxVector[i+1]-colIdxVector[i]); j++, rind++)
54
      {
55 56 57 58
        tdm->get(output_row, 0) = rowIdxVector[rind] + 1;
        tdm->get(output_row, 1) = i + 1;
        tdm->get(output_row, 2) = ptr[rind];
        output_row++;
59 60
      }

61 62 63 64
  /* If there are less elements than Nzmax (that might happen if some
     derivative is symbolically not zero but numerically zero at the evaluation
     point), then fill in the matrix with empty entries, that will be
     recognized as such by KordpDynare::populateDerivativesContainer() */
65
  while (output_row < (int) sizeRowIdxVector)
66
    {
67 68 69 70
      tdm->get(output_row, 0) = 0;
      tdm->get(output_row, 1) = 0;
      tdm->get(output_row, 2) = 0;
      output_row++;
71
    }
72
}