| 1 |
! Copyright © 2021-2026 Dynare Team |
| 2 |
! |
| 3 |
! This file is part of Dynare. |
| 4 |
! |
| 5 |
! Dynare is free software: you can redistribute it and/or modify |
| 6 |
! it under the terms of the GNU General Public License as published by |
| 7 |
! the Free Software Foundation, either version 3 of the License, or |
| 8 |
! (at your option) any later version. |
| 9 |
! |
| 10 |
! Dynare is distributed in the hope that it will be useful, |
| 11 |
! but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
! GNU General Public License for more details. |
| 14 |
! |
| 15 |
! You should have received a copy of the GNU General Public License |
| 16 |
! along with Dynare. If not, see <https://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
subroutine mexFunction(nlhs, plhs, nrhs, prhs) bind(c, name='mexFunction') |
| 19 |
use iso_fortran_env |
| 20 |
use iso_c_binding |
| 21 |
use struct |
| 22 |
use simulation |
| 23 |
use matlab_mex |
| 24 |
use partitions |
| 25 |
implicit none (type, external) |
| 26 |
|
| 27 |
type(c_ptr), dimension(*), intent(in) :: prhs |
| 28 |
type(c_ptr), dimension(*), intent(out) :: plhs |
| 29 |
integer(c_int), intent(in), value :: nlhs, nrhs |
| 30 |
type(c_ptr) :: M_mx, options_mx, dr_mx, tmp, g |
| 31 |
type(tensor), dimension(:), allocatable, target :: fdr |
| 32 |
integer :: order, npred, nboth, nstatic, nfwrd, endo_nbr, exo_nbr, nys, nvar |
| 33 |
type(pascal_triangle) :: p |
| 34 |
type(uf_matching), dimension(:), allocatable :: matching |
| 35 |
character(kind=c_char, len=10), dimension(:), allocatable :: fieldnames |
| 36 |
integer :: d, m, n |
| 37 |
|
| 38 |
dr_mx = prhs(1) |
| 39 |
M_mx = prhs(2) |
| 40 |
options_mx = prhs(3) |
| 41 |
|
| 42 |
! Checking the consistence and validity of input arguments |
| 43 |
if (nrhs /= 3 .or. nlhs /= 1) call mexErrMsgTxt("Must have exactly 3 inputs and 1 output") |
| 44 |
if (.not. mxIsStruct(dr_mx)) call mexErrMsgTxt("1st argument (dr) should be a struct") |
| 45 |
if (.not. mxIsStruct(M_mx)) call mexErrMsgTxt("2nd argument (M) should be a struct") |
| 46 |
if (.not. mxIsStruct(options_mx)) call mexErrMsgTxt("3rd argument (options) should be a struct") |
| 47 |
|
| 48 |
nstatic = get_int_field(M_mx, "nstatic") |
| 49 |
npred = get_int_field(M_mx, "npred") |
| 50 |
nboth = get_int_field(M_mx, "nboth") |
| 51 |
nfwrd = get_int_field(M_mx, "nfwrd") |
| 52 |
endo_nbr = nstatic+npred+nboth+nfwrd |
| 53 |
exo_nbr = get_int_field(M_mx, "exo_nbr") |
| 54 |
order = get_int_field(options_mx, "order") |
| 55 |
nys = npred+nboth |
| 56 |
nvar = nys+exo_nbr |
| 57 |
|
| 58 |
allocate(fdr(0:order), fieldnames(0:order)) |
| 59 |
do d = 0, order |
| 60 |
write (fieldnames(d), '(a2, i1)') "g_", d |
| 61 |
tmp = mxGetField(dr_mx, 1_mwIndex, trim(fieldnames(d))) |
| 62 |
if (.not. (c_associated(tmp) .and. mxIsDouble(tmp))) & |
| 63 |
call mexErrMsgTxt(trim(fieldnames(d))//" is not allocated in dr") |
| 64 |
m = int(mxGetM(tmp)) |
| 65 |
n = int(mxGetN(tmp)) |
| 66 |
allocate(fdr(d)%m(m,n)) |
| 67 |
fdr(d)%m = reshape(mxGetDoubles(tmp), [m,n]) |
| 68 |
end do |
| 69 |
|
| 70 |
plhs(1) = mxCreateStructMatrix(1_mwSize, 1_mwSize, fieldnames) |
| 71 |
g = mxCreateDoubleMatrix(int(endo_nbr, mwSize), 1_mwSize, mxREAL) |
| 72 |
mxGetDoubles(g) = reshape(fdr(0)%m, [size(fdr(0)%m)]) |
| 73 |
call mxSetField(plhs(1), 1_mwIndex, "g_0", g) |
| 74 |
g = mxCreateDoubleMatrix(int(endo_nbr, mwSize), int(nvar, mwSize), mxREAL) |
| 75 |
mxGetDoubles(g) = reshape(fdr(1)%m, [size(fdr(1)%m)]) |
| 76 |
call mxSetField(plhs(1), 1_mwIndex, "g_1", g) |
| 77 |
|
| 78 |
if (order > 1) then |
| 79 |
! Compute the useful binomial coefficients from Pascal's triangle |
| 80 |
p = pascal_triangle(nvar+order-1) |
| 81 |
allocate(matching(2:order)) |
| 82 |
! Pinpointing the corresponding offsets between folded and unfolded tensors |
| 83 |
do d=2,order |
| 84 |
allocate(matching(d)%folded(nvar**d)) |
| 85 |
call fill_folded_indices(matching(d)%folded, nvar, d, p) |
| 86 |
g = mxCreateDoubleMatrix(int(endo_nbr, mwSize), int(nvar**d, mwSize), mxREAL) |
| 87 |
mxGetDoubles(g) = reshape(fdr(d)%m(:,matching(d)%folded), [size(fdr(d)%m(:,matching(d)%folded))]) |
| 88 |
call mxSetField(plhs(1), 1_mwIndex, trim(fieldnames(d)), g) |
| 89 |
end do |
| 90 |
end if |
| 91 |
|
| 92 |
end subroutine mexFunction |