]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/easter.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / easter.m
1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {[m, d] =} easter (y)
18 ## @deftypefnx {Function File} {datenum =} easter (y)
19 ##
20 ## Return the month (@var{m}) and day (@var{d}) of Easter in the
21 ## Gregorial calendar on a given year or years.
22 ##
23 ## @seealso{holidays}
24 ## @end deftypefn
25
26 function varargout = easter (y)
27
28   ## This uses the Meesus/Jones/Butcher Gregorian algorithm as described
29   ## on http://en.wikipedia.org/wiki/Computus#Algorithms
30   a = mod (y, 19);
31   b = floor (y/100);
32   c = mod (y, 100);
33   d = floor (b/4);
34   e = mod (b, 4);
35   f = floor ((b + 8)/25);
36   g = floor ((b - f + 1)/3);
37   h = mod ((19*a+b-d-g+15), 30);
38   i = floor (c/4);
39   k = mod (c, 4);
40   L = mod ((32 + 2*e + 2*i - h - k), 7);
41   m = floor ((a + 11*h + 22*L)/451);
42   mon = floor ((h + L - 7*m + 114)/31);
43   day = 1 + mod ((h + L - 7*m + 114), 31);
44
45   if nargout == 2
46         varargout = {mon(:), day(:)};
47   else
48         varargout{1} = reshape (datenum (y(:), mon(:), day(:)), size (y));
49   end
50
51 endfunction
52
53 ## Tests
54 ## Validate that it calculates the correct date for a decade
55 %!assert(easter(1990), datenum(1990, 4, 15))
56 %!assert(easter(1991), datenum(1991, 3, 31))
57 %!assert(easter(1992), datenum(1992, 4, 19))
58 %!assert(easter(1993), datenum(1993, 4, 11))
59 %!assert(easter(1994), datenum(1994, 4, 3))
60 %!assert(easter(1995), datenum(1995, 4, 16))
61 %!assert(easter(1996), datenum(1996, 4, 7))
62 %!assert(easter(1997), datenum(1997, 3, 30))
63 %!assert(easter(1998), datenum(1998, 4, 12))
64 %!assert(easter(1999), datenum(1999, 4, 4))
65 ## Validate vector and matrix inputs
66 %!assert(easter([2000 2001]), [datenum(2000, 4, 23) datenum(2001, 4, 15)])
67 %!assert(easter([2002;2003]), [datenum(2002, 3, 31);datenum(2003, 4, 20)])
68 %!assert(easter([2004 2005;2006 2007;2008 2009]), [datenum(2004, 4, 11) datenum(2005, 3, 27);datenum(2006, 4, 16) datenum(2007, 4, 8);datenum(2008, 3, 23) datenum(2009, 4, 12)])
69 %!assert(easter([2002;2003]), [datenum(2002, 3, 31);datenum(2003, 4, 20)])