From 229871f7af453840d8c842036fdf5aed5441ac61 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?=
 <stephane.adjemian@univ-lemans.fr>
Date: Wed, 14 May 2014 21:04:11 +0200
Subject: [PATCH] Improve speed of @dates/colon method (added specialized
 version of add_periods_to_array_of_dates -> add_periods_to_date).

---
 matlab/@dates/colon.m                        |  2 +-
 matlab/utilities/dates/add_periods_to_date.m | 57 ++++++++++++++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 matlab/utilities/dates/add_periods_to_date.m

diff --git a/matlab/@dates/colon.m b/matlab/@dates/colon.m
index 76e78a2278..88a4568247 100644
--- a/matlab/@dates/colon.m
+++ b/matlab/@dates/colon.m
@@ -77,7 +77,7 @@ C.time = NaN(n,2);
 C.time(1,:) = A.time;
 
 for linee=2:n
-    C.time(linee,:) = add_periods_to_array_of_dates(C.time(linee-1,:), C.freq, d);
+    C.time(linee,:) = add_periods_to_date(C.time(linee-1,:), C.freq, d) ;%add_periods_to_array_of_dates(C.time(linee-1,:), C.freq, d);
 end
 
 %@test:1
diff --git a/matlab/utilities/dates/add_periods_to_date.m b/matlab/utilities/dates/add_periods_to_date.m
new file mode 100644
index 0000000000..22ff62c5d1
--- /dev/null
+++ b/matlab/utilities/dates/add_periods_to_date.m
@@ -0,0 +1,57 @@
+function time = add_periods_to_date(time, freq, p)  % --*-- Unitary tests --*--
+
+% Adds a p periods (p can be negative) to a date (or a set of dates) characterized by array time and frequency freq.
+    
+% Copyright (C) 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 <http://www.gnu.org/licenses/>.
+
+time(1) = time(1) + fix(p/freq);
+time(2) = time(2) + rem(p,freq);
+
+if time(2)>freq
+    time(1) = time(1) + 1;
+    time(2) = time(2) - freq;
+end
+
+if time(2)<1
+    time(1) = time(1) - 1;
+    time(2) = time(2) + freq;
+end
+
+%@test:1
+%$ t(1) = dyn_assert(add_periods_to_date([1950 1], 4, 1),[1950 2]);
+%$ t(2) = dyn_assert(add_periods_to_date([1950 1], 4, 2),[1950 3]);
+%$ t(3) = dyn_assert(add_periods_to_date([1950 1], 4, 3),[1950 4]);
+%$ t(4) = dyn_assert(add_periods_to_date([1950 1], 4, 4),[1951 1]);
+%$ t(5) = dyn_assert(add_periods_to_date([1950 1], 4, 5),[1951 2]);
+%$ t(6) = dyn_assert(add_periods_to_date([1950 1], 4, 6),[1951 3]);
+%$ t(7) = dyn_assert(add_periods_to_date([1950 1], 4, 7),[1951 4]);
+%$ t(8) = dyn_assert(add_periods_to_date([1950 1], 4, 8),[1952 1]);
+%$ T = all(t);
+%@eof:1
+
+%@test:2
+%$ t(1) = dyn_assert(add_periods_to_date([1950 1], 4, -1),[1949 4]);
+%$ t(2) = dyn_assert(add_periods_to_date([1950 1], 4, -2),[1949 3]);
+%$ t(3) = dyn_assert(add_periods_to_date([1950 1], 4, -3),[1949 2]);
+%$ t(4) = dyn_assert(add_periods_to_date([1950 1], 4, -4),[1949 1]);
+%$ t(5) = dyn_assert(add_periods_to_date([1950 1], 4, -5),[1948 4]);
+%$ t(6) = dyn_assert(add_periods_to_date([1950 1], 4, -6),[1948 3]);
+%$ t(7) = dyn_assert(add_periods_to_date([1950 1], 4, -7),[1948 2]);
+%$ t(8) = dyn_assert(add_periods_to_date([1950 1], 4, -8),[1948 1]);
+%$ T = all(t);
+%@eof:2
\ No newline at end of file
-- 
GitLab