From d17ed416dc8fa3f0cc7145e909c819f52a5b261a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Fri, 11 Oct 2013 18:41:26 +0200 Subject: [PATCH] Merged @dynDate/plus and @dynDates/plus methods (overload the plus operator). Removed @dynDate/plus. --- matlab/@dynDate/plus.m | 107 ------------------------------- matlab/@dynDates/plus.m | 135 +++++++++++++++++++++++++++++----------- 2 files changed, 100 insertions(+), 142 deletions(-) delete mode 100644 matlab/@dynDate/plus.m diff --git a/matlab/@dynDate/plus.m b/matlab/@dynDate/plus.m deleted file mode 100644 index 4e624c5d8..000000000 --- a/matlab/@dynDate/plus.m +++ /dev/null @@ -1,107 +0,0 @@ -function c = plus(a,b) % --*-- Unitary tests --*-- - -%@info: -%! @deftypefn {Function File} {@var{c} =} plus (@var{a},@var{b}) -%! @anchor{@dynDate/plus} -%! @sp 1 -%! Overloads the plus (addition) operator for the Dynare dates class (@ref{dynDate}). Given an initial date @var{a}, -%! computes a new date @var{c} by adding the number of periods @var{b}. -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item a -%! Dynare date object instantiated by @ref{dynDate}. -%! @item b -%! Positive scalar integer, the number of periods -%! @end table -%! @sp 2 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item c -%! Dynare date object instantiated by @ref{dynDate}. -%! @end table -%! @sp 2 -%! @strong{This function is called by:} -%! @sp 2 -%! @strong{This function calls:} -%! @ref{@@dynDate/eq} -%! -%! @end deftypefn -%@eod: - -% Copyright (C) 2011-2013 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 . - -if ~isa(a,'dynDate') - error(['dynDate::plus: Input argument ' inputname(1) ' must be a dynDate object!']) -end - -if b<0 || ~isint(b) - error(['dynDate::plus: Input argument ' inputname(2) ' must be a positive integer']) -end - -if b==0 - c = a; - return -end - -switch a.freq - case 1 - c = a; - c.time(1) = a.time(1) + b - 1; - case {4,12,52} - c = a; - n1 = b + a.time(2); - n2 = floor((n1 - 1)/a.freq); - n3 = mod(n1 - 1,a.freq) + 1; - c.time(2) = n3; - c.time(1) = c.time(1)+n2; - otherwise - error('dynDate::plus: Unknown frequency!') -end - -%@test:1 -%$ % Define some dates -%$ date_1 = 1950; -%$ date_2 = '1950Q4'; -%$ date_3 = '2000M3'; -%$ -%$ % Call the tested routine. -%$ d_1 = dynDate(date_1); -%$ d_2 = dynDate(date_2); -%$ d_3 = dynDate(date_3); -%$ -%$ d1 = d_1+3; -%$ d2 = d_2+5; -%$ d3 = d_3+15; -%$ d4 = d_3+10; -%$ -%$ % Expected results. -%$ e1 = dynDate(1952); -%$ e2 = dynDate('1952Q1'); -%$ e3 = dynDate('2001M6'); -%$ e4 = dynDate('2001M1'); -%$ -%$ % Check the results. -%$ t(1) = dyn_assert(e1.time,d1.time); -%$ t(2) = dyn_assert(e2.time,d2.time); -%$ t(3) = dyn_assert(e3.time,d3.time); -%$ t(4) = dyn_assert(e4.time,d4.time); -%$ T = all(t); -%@eof:1 diff --git a/matlab/@dynDates/plus.m b/matlab/@dynDates/plus.m index 25a8f6e78..07f52928a 100644 --- a/matlab/@dynDates/plus.m +++ b/matlab/@dynDates/plus.m @@ -42,46 +42,111 @@ function C = plus(A,B) % --*-- Unitary tests --*-- % You should have received a copy of the GNU General Public License % along with Dynare. If not, see . -if ~isa(A,'dynDates') || ~isa(B,'dynDates') - error(['dynDates::plus: Input arguments ''' inputname(1) ''' and ''' inputname(2) ''' must be dynDates objects!']) +if isa(A,'dynDates') && isa(B,'dynDates') + % Concatenate dynDates objects without removing repetitions if A and B are not disjoint sets of dates. + if ~isequal(A.freq,B.freq) + error(['dynDates::plus: Input arguments ''' inputname(1) ''' and ''' inputname(2) ''' must have common frequencies!']) + end + if isempty(B) + C = A; + return + end + if isempty(A) + C = B; + return + end + C = dynDates(); + C.freq = A.freq; + C.time = [A.time; B.time]; + C.ndat = A.ndat+B.ndat; +elseif isa(A,'dynDates') && ( (isvector(B) && isequal(length(B),A.ndat) && all(isint(B))) || isscalar(B) && isint(B) || isequal(length(A),1) && isvector(B) && all(isint(B))) + C.time = add_periods_to_array_of_dates(A.time, A.freq, B); +elseif isa(B,'dynDates') && ( (isvector(A) && isequal(length(A),B.ndat) && all(isint(A))) || isscalar(A) && isint(A) ) + C.time = add_periods_to_array_of_dates(B.time, B.freq, A); +else + error('dynDates::plus: I don''t understand what you want to do! Check the manual.') end -if isempty(B) - C = A; - return -end - -if isempty(A) - C = B; - return -end - -if ~isequal(A.freq,B.freq) - error(['dynDates::plus: Input arguments ''' inputname(1) ''' and ''' inputname(2) ''' must have common frequencies!']) -end - -C = dynDates(); - -C.freq = A.freq; -C.time = [A.time; B.time]; -C.ndat = A.ndat+B.ndat; - %@test:1 %$ % Define some dynDates objects -%$ d1 = dynDate('1950Q1'):dynDate('1959Q4') ; -%$ d2 = dynDate('1960Q1'):dynDate('1969Q4') ; -%$ d3 = dynDate('1970Q1'):dynDate('1979Q4') ; +%$ d1 = dynDates('1950Q1','1950Q2') ; +%$ d2 = dynDates('1950Q3','1950Q4') ; +%$ d3 = dynDates('1950Q1','1950Q2','1950Q3','1950Q4') ; %$ %$ % Call the tested routine. -%$ e1 = d1+d2; -%$ e2 = d1+d2+d3; +%$ try +%$ e1 = d1+d2; +%$ e2 = d1+d2+d3; +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end %$ -%$ % Expected results. -%$ f1 = dynDate('1950Q1'):dynDate('1969Q4'); -%$ f2 = dynDate('1950Q1'):dynDate('1979Q4'); -%$ -%$ % Check the results. -%$ t(1) = dyn_assert(e1==f1,1); -%$ t(2) = dyn_assert(e2==f2,1); +%$ if t(1) +%$ t(2) = dyn_assert(e1==d3,1); +%$ t(3) = dyn_assert(e2==dynDates('1950Q1','1950Q2','1950Q3','1950Q4','1950Q1','1950Q2','1950Q3','1950Q4'),1); +%$ end %$ T = all(t); -%@eof:1 \ No newline at end of file +%@eof:1 + +%@test:2 +%$ % Define some dynDates objects +%$ d1 = dynDates('1950Q1'); +%$ e1 = dynDates('1950Q2'); +%$ e2 = dynDates('1950Q3'); +%$ e3 = dynDates('1950Q4'); +%$ e4 = dynDates('1951Q1'); +%$ e5 = dynDates('1950Q2','1950Q3','1950Q4','1951Q1'); +%$ +%$ % Call the tested routine. +%$ try +%$ f1 = d1+1 +%$ f2 = d1+2 +%$ f3 = d1+3 +%$ f4 = d1+4 +%$ f5 = d1+transpose(1:4) +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dyn_assert(e1==f1,1); +%$ t(3) = dyn_assert(e2==f2,1); +%$ t(4) = dyn_assert(e3==f3,1); +%$ t(5) = dyn_assert(e4==f4,1); +%$ t(6) = dyn_assert(e5==f5,1); +%$ end +%$ T = all(t); +%@eof:2 + +%@test:3 +%$ % Define some dynDates objects +%$ d1 = dynDates('1950Q1'); +%$ e1 = dynDates('1949Q4'); +%$ e2 = dynDates('1949Q3'); +%$ e3 = dynDates('1949Q2'); +%$ e4 = dynDates('1949Q1'); +%$ e5 = dynDates('1948Q4'); +%$ +%$ % Call the tested routine. +%$ try +%$ f1 = d1+(-1) +%$ f2 = d1+(-2) +%$ f3 = d1+(-3) +%$ f4 = d1+(-4) +%$ f5 = d1+(-5) +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dyn_assert(e1==f1,1); +%$ t(3) = dyn_assert(e2==f2,1); +%$ t(4) = dyn_assert(e3==f3,1); +%$ t(5) = dyn_assert(e4==f4,1); +%$ t(6) = dyn_assert(e5==f5,1); +%$ end +%$ T = all(t); +%@eof:3 \ No newline at end of file