From 8f07a134a3bc658ed6c7221bc78d0cf1710cf929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Fri, 15 Jan 2021 14:45:25 +0100 Subject: [PATCH] OLS + aggregate: compatibility fix for Octave < 6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit unique(…, 'stable') does not exist in Octave 5. --- matlab/aggregate.m | 22 +++++++++--- matlab/dynare_config.m | 5 +-- matlab/missing/unique_stable/unique_stable.m | 36 ++++++++++++++++++++ matlab/ols/getEquationsByTags.m | 8 +++-- 4 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 matlab/missing/unique_stable/unique_stable.m diff --git a/matlab/aggregate.m b/matlab/aggregate.m index 1f7d4f5af8..e85455506a 100644 --- a/matlab/aggregate.m +++ b/matlab/aggregate.m @@ -2,7 +2,7 @@ function aggregate(ofile, dynopt, rootfolder, varargin) % Agregates cherry-picked models. -% Copyright (C) 2019 Dynare Team +% Copyright (C) 2019-2021 Dynare Team % % This file is part of Dynare. % @@ -101,7 +101,11 @@ for i=1:length(varargin) end end eqlist = eqlist(1:eqnum,:); -[~, idx] = unique(eqlist(:,1), 'stable'); +if isoctave && octave_ver_less_than('6') + [~, idx] = unique_stable(eqlist(:,1)); +else + [~, idx] = unique(eqlist(:,1), 'stable'); +end eqlist = eqlist(idx, :); % Get endogenous variables. @@ -124,7 +128,11 @@ for i=1:length(varargin) fclose(fid); end elist = elist(1:enum,:); -[~, idx] = unique(elist(:,1), 'stable'); +if isoctave && octave_ver_less_than('6') + [~, idx] = unique_stable(elist(:,1)); +else + [~, idx] = unique(elist(:,1), 'stable'); +end elist = elist(idx,:); % Get exogenous variables. @@ -152,7 +160,11 @@ for i=1:length(varargin) fclose(fid); end xlist = xlist(1:xnum,:); -[~, idx] = unique(xlist(:,1), 'stable'); +if isoctave && octave_ver_less_than('6') + [~, idx] = unique_stable(xlist(:,1)); +else + [~, idx] = unique(xlist(:,1), 'stable'); +end xlist = xlist(idx,:); % Get parameter values. @@ -291,4 +303,4 @@ function blkname = getblockname(str, ROOT_FOLDER) str = strrep(str, '/', filesep()); str = strrep(str, [ROOT_FOLDER filesep() 'blocks' filesep()], ''); idx = strfind(str, filesep()); - blkname = str(1:idx(1)-1); \ No newline at end of file + blkname = str(1:idx(1)-1); diff --git a/matlab/dynare_config.m b/matlab/dynare_config.m index f54506f4fa..1ee4177037 100644 --- a/matlab/dynare_config.m +++ b/matlab/dynare_config.m @@ -16,7 +16,7 @@ function dynareroot = dynare_config(path_to_dynare) % SPECIAL REQUIREMENTS % none -% Copyright (C) 2001-2020 Dynare Team +% Copyright (C) 2001-2021 Dynare Team % % This file is part of Dynare. % @@ -90,9 +90,10 @@ if isoctave && octave_ver_less_than('5') p{end+1} = '/missing/ordeig'; end -%% intersect(…, 'stable') doesn't exist in Octave < 6 +%% intersect(…, 'stable') and unique(…, 'stable') doen't exist in Octave < 6 if isoctave && octave_ver_less_than('6') p{end+1} = '/missing/intersect_stable'; + p{end+1} = '/missing/unique_stable'; end % Replacements for functions of the MATLAB statistics toolbox diff --git a/matlab/missing/unique_stable/unique_stable.m b/matlab/missing/unique_stable/unique_stable.m new file mode 100644 index 0000000000..113e12beb5 --- /dev/null +++ b/matlab/missing/unique_stable/unique_stable.m @@ -0,0 +1,36 @@ +% Crude implementation of unique(…, 'stable'), which is missing in Octave < 6 + +% Copyright (C) 2021 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 <http://www.gnu.org/licenses/>. + +function r = unique_stable(x) + +if iscell(x) + error('Cell arrays unsupported') +end + +r = []; +for i = 1:numel(x) + if ~ismember(x(i), r) + r = [ r, x(i) ]; + end +end +if size(x, 2) == 1 + r = r'; +end + +end diff --git a/matlab/ols/getEquationsByTags.m b/matlab/ols/getEquationsByTags.m index 29edee8325..7d7c85b912 100644 --- a/matlab/ols/getEquationsByTags.m +++ b/matlab/ols/getEquationsByTags.m @@ -17,7 +17,7 @@ function [ast] = getEquationsByTags(ast, tagname, tagvalue) % SPECIAL REQUIREMENTS % none -% Copyright (C) 2017-2019 Dynare Team +% Copyright (C) 2017-2021 Dynare Team % % This file is part of Dynare. % @@ -72,5 +72,9 @@ for i = 1:length(tagvalue) end end assert(~isempty(idx2keep), 'getEquationsByTags: no equations selected'); -ast = ast(unique(idx2keep, 'stable')); +if isoctave && octave_ver_less_than('6') + ast = ast(unique_stable(idx2keep)); +else + ast = ast(unique(idx2keep, 'stable')); +end end -- GitLab