1function [pnames, enames, xnames, pid, eid, xid] = get_variables_and_parameters_in_equation(lhs, rhs, DynareModel)
2
3% Returns the lists of parameters, endogenous variables and exogenous variables in an equation.
4%
5% INPUTS
6% - lhs         [string]            Left hand side of an equation.
7% - rhs         [string]            Right hand side of an equation.
8% - DynareModel [struct]            Structure describing the current model (M_).
9%
10% OUTPUTS
11% - pnames      [cell]              Cell of row char arrays (p elements), names of the parameters.
12% - enames      [cell]              Cell of row char arrays (n elements), names of the endogenous variables.
13% - xnames      [cell]              Cell of row char arrays (m elements), names of the exogenous variables.
14% - pid         [Integer]           p*1 vector of indices in M_.param_names for the listed parameters in params.
15% - eid         [Integer]           n*1 vector of indices in M_.endo_names for the listed parameters in endogenous.
16% - xid         [Integer]           m*1 vector of indices in M_.exo_names for the listed parameters in exogenous.
17
18% Copyright (C) 2018-2019 Dynare Team
19%
20% This file is part of Dynare.
21%
22% Dynare is free software: you can redistribute it and/or modify
23% it under the terms of the GNU General Public License as published by
24% the Free Software Foundation, either version 3 of the License, or
25% (at your option) any later version.
26%
27% Dynare is distributed in the hope that it will be useful,
28% but WITHOUT ANY WARRANTY; without even the implied warranty of
29% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30% GNU General Public License for more details.
31%
32% You should have received a copy of the GNU General Public License
33% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
34
35% Get the tokens in the rhs member of the equation.
36rhs_ = strsplit(rhs,{'+','-','*','/','^', ...
37                    'log(', 'log10(', 'ln(', 'exp(', ...
38                    'sqrt(', 'abs(', 'sign(', ...
39                    'sin(', 'cos(', 'tan(', 'asin(', 'acos(', 'atan(', ...
40                    'min(', 'max(', ...
41                    'normcdf(', 'normpdf(', 'erf(', ...
42                    'diff(', 'adl(', '(', ')'});
43
44% Filter out the numbers and punctuation.
45rhs_(cellfun(@(x) all(isstrprop(x, 'digit')+isstrprop(x, 'punct')), rhs_)) = [];
46
47% Get list of parameters.
48pnames = DynareModel.param_names;
49pnames = intersect(rhs_, pnames);
50
51% Get list of endogenous variables.
52enames = DynareModel.endo_names;
53enames = intersect(rhs_, enames);
54
55% Get list of exogenous variables
56xnames = DynareModel.exo_names;
57xnames = intersect(rhs_, xnames);
58
59% Decide if we are dealing with a dynamic model. If so, the lhs variable
60% already belongs to enames, we remove this variable from enames.
61id = find(strcmp(lhs, enames));
62if ~isempty(id)
63    enames(id) = [];
64end
65
66% Add lhs variable in first position of enames.
67enames = [lhs; enames];
68
69% Returns vector of indices for parameters endogenous and exogenous
70% variables if required.
71if nargout>3
72    p = length(pnames);
73    pid = zeros(p, 1);
74    for i = 1:p
75        pid(i) = find(strcmp(pnames{i}, DynareModel.param_names));
76    end
77    p = length(enames);
78    eid = zeros(p, 1);
79    for i = 1:p
80        eid(i) = find(strcmp(enames{i}, DynareModel.endo_names));
81    end
82    p = length(xnames);
83    xid = zeros(p, 1);
84    for i = 1:p
85        xid(i) = find(strcmp(xnames{i}, DynareModel.exo_names));
86    end
87end