Efficiency improvements. Rewrote some routines using regular expressions.

time-shift
Stéphane Adjemian (Charybdis) 2013-10-10 12:52:27 +02:00
parent 399b6d2615
commit f4e32dbd6f
4 changed files with 127 additions and 81 deletions

View File

@ -1,6 +1,14 @@
function b = ismonthly(date) function b = ismonthly(str) % --*-- Unitary tests --*--
% Copyright (C) 2012 Dynare Team % Tests if the input can be interpreted as a monthly date.
%
% INPUTS
% o str string.
%
% OUTPUTS
% o b integer scalar, equal to 1 if str can be interpreted as a monthly date or 0 otherwise.
% Copyright (C) 2012-2013 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -17,21 +25,26 @@ function b = ismonthly(date)
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr b = ~isempty(regexp(str,'^-?[0-9]*[Mm]([1-9]|1[12])$'));
[year, remain] = strtok(date,'M'); %@test:1
if ~isint(str2num(year)) %$
b = 0; %$ date_1 = '1950M2';
return %$ date_2 = '1950m2';
end %$ date_3 = '-1950m2';
[month, remain] = strtok(remain,'M'); %$ date_4 = '1950m12';
if ~isempty(remain) %$ date_5 = '1950 azd ';
b = 0; %$ date_6 = '1950Y';
return %$ date_7 = '1950Q3';
end %$ date_8 = '1950m24';
month = str2num(month); %$
if ~isint(month) || month<1 || month>12 %$ t(1) = dyn_assert(ismonthly(date_1),1);
b = 0; %$ t(2) = dyn_assert(ismonthly(date_2),1);
return %$ t(3) = dyn_assert(ismonthly(date_3),1);
end %$ t(4) = dyn_assert(ismonthly(date_4),1);
b = 1; %$ t(5) = dyn_assert(ismonthly(date_5),0);
%$ t(6) = dyn_assert(ismonthly(date_6),0);
%$ t(7) = dyn_assert(ismonthly(date_7),0);
%$ t(8) = dyn_assert(ismonthly(date_8),0);
%$ T = all(t);
%@eof:1

View File

@ -1,6 +1,14 @@
function b = isquaterly(date) function b = isquaterly(str) % --*-- Unitary tests --*--
% Copyright (C) 2012 Dynare Team % Tests if the input can be interpreted as a quaterly date.
%
% INPUTS
% o str string.
%
% OUTPUTS
% o b integer scalar, equal to 1 if str can be interpreted as a quaterly date or 0 otherwise.
% Copyright (C) 2012-2013 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -17,21 +25,24 @@ function b = isquaterly(date)
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr b = ~isempty(regexp(str,'^-?[0-9]*[Qq][1-4]$'));
[year, remain] = strtok(date,'Q'); %@test:1
if ~isint(str2num(year)) %$
b = 0; %$ date_1 = '1950Q2';
return %$ date_2 = '1950q2';
end %$ date_3 = '-1950q2';
[quarter, remain] = strtok(remain,'Q'); %$ date_4 = '1950q12';
if ~isempty(remain) %$ date_5 = '1950 azd ';
b = 0; %$ date_6 = '1950Y';
return %$ date_7 = '1950m24';
end %$
quarter = str2num(quarter); %$ t(1) = dyn_assert(isquaterly(date_1),1);
if ~isint(quarter) || quarter<1 || quarter>4 %$ t(2) = dyn_assert(isquaterly(date_2),1);
b = 0; %$ t(3) = dyn_assert(isquaterly(date_3),1);
return %$ t(4) = dyn_assert(isquaterly(date_4),0);
end %$ t(5) = dyn_assert(isquaterly(date_5),0);
b = 1; %$ t(6) = dyn_assert(isquaterly(date_6),0);
%$ t(7) = dyn_assert(isquaterly(date_7),0);
%$ T = all(t);
%@eof:1

View File

@ -1,6 +1,14 @@
function b = isweekly(date) function b = isweekly(str) % --*-- Unitary tests --*--
% Copyright (C) 2012 Dynare Team % Tests if the input can be interpreted as a weekly date.
%
% INPUTS
% o str string.
%
% OUTPUTS
% o b integer scalar, equal to 1 if str can be interpreted as a weekly date or 0 otherwise.
% Copyright (C) 2012-2013 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -17,21 +25,26 @@ function b = isweekly(date)
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr b = ~isempty(regexp(str,'^-?[0-9]*[Ww]([1-9]|[1-4][0-9]|5[0-2])$'));
[year, remain] = strtok(date,'W'); %@test:1
if ~isint(str2num(year)) %$
b = 0; %$ date_1 = '1950W2';
return %$ date_2 = '1950w2';
end %$ date_3 = '-1950w2';
[week, remain] = strtok(remain,'W'); %$ date_4 = '1950w22';
if ~isempty(remain) %$ date_5 = '1950 azd ';
b = 0; %$ date_6 = '1950Y';
return %$ date_7 = '1950Q3';
end %$ date_8 = '1950m54';
week = str2num(week); %$
if ~isint(week) || week<1 || week>52 %$ t(1) = dyn_assert(isweekly(date_1),1);
b = 0; %$ t(2) = dyn_assert(isweekly(date_2),1);
return %$ t(3) = dyn_assert(isweekly(date_3),1);
end %$ t(4) = dyn_assert(isweekly(date_4),1);
b = 1; %$ t(5) = dyn_assert(isweekly(date_5),0);
%$ t(6) = dyn_assert(isweekly(date_6),0);
%$ t(7) = dyn_assert(isweekly(date_7),0);
%$ t(8) = dyn_assert(isweekly(date_8),0);
%$ T = all(t);
%@eof:1

View File

@ -1,6 +1,14 @@
function b = isyearly(date) function b = isyearly(str) % --*-- Unitary tests --*--
% Copyright (C) 2012 Dynare Team % Tests if the input can be interpreted as a yearly date.
%
% INPUTS
% o str string.
%
% OUTPUTS
% o b integer scalar, equal to 1 if str can be interpreted as a yearly date or 0 otherwise.
% Copyright (C) 2012-2013 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -17,25 +25,26 @@ function b = isyearly(date)
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr b = ~isempty(regexp(str,'^-?[0-9]*[YyAa]$'));
year = str2num(date); %@test:1
if ~isempty(year) %$
if isint(year) %$ date_1 = '1950M2';
b = 1; %$ date_2 = '1950m2';
else %$ date_3 = '-1950m2';
b = 0; %$ date_4 = '1950m12';
end %$ date_5 = '1950 azd ';
return %$ date_6 = '1950Y';
else %$ date_7 = '-1950a';
[year, remain] = strtok(date,'Y'); %$ date_8 = '1950m24';
if ~isequal(remain,'Y') %$
b = 0; %$ t(1) = dyn_assert(isyearly(date_1),0);
return %$ t(2) = dyn_assert(isyearly(date_2),0);
end %$ t(3) = dyn_assert(isyearly(date_3),0);
if ~isint(str2num(year)) %$ t(4) = dyn_assert(isyearly(date_4),0);
b = 0; %$ t(5) = dyn_assert(isyearly(date_5),0);
return %$ t(6) = dyn_assert(isyearly(date_6),1);
end %$ t(7) = dyn_assert(isyearly(date_7),1);
b = 2; %$ t(8) = dyn_assert(isyearly(date_8),0);
end %$ T = all(t);
%@eof:1