Rewrote ne, eq, le, ge, lt and gt methods.

These methods overloads ~=, ==, <=, >=, < and > relational operators.
parent 86e58444
function C = eq(A,B) % --*-- Unitary tests --*--
function l = eq(varargin) % --*-- Unitary tests --*--
% Overloads == operator for dates objects.
%
% INPUTS
% o A dates object with n or 1 elements.
% o B dates object with n or 1 elements.
% INPUTS
% - o [dates] dates object with n or 1 elements.
% - p [dates] dates object with n or 1 elements.
%
% OUTPUTS
% o C column vector of max(n,1) elements (zeros or ones).
% OUTPUTS
% - l [logical] column vector of max(n,1) elements (zeros or ones).
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2014 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% This code 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,
% Dynare dates submodule 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.
......@@ -26,27 +24,12 @@ function C = eq(A,B) % --*-- Unitary tests --*--
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if ~isequal(nargin,2)
error('dates::eq: I need exactly two input arguments!')
end
if ~isa(A,'dates') || ~isa(B,'dates')
error(['dates::eq: Input arguments ''' inputname(1) ''' and ''' inputname(2) ''' have to be a dates objects!'])
end
if ~isequal(A.freq,B.freq)
C = false;
return
end
[o, p] = comparison_arg_checks(varargin{:});
if isequal(A.ndat, B.ndat)
C = logical(transpose(all(transpose(eq(A.time,B.time)))));
if isequal(o.ndat, p.ndat)
l = logical(transpose(all(transpose(eq(o.time,p.time)))));
else
if isequal(A.ndat,1) || isequal(B.ndat,1)
C = logical(transpose(all(transpose(bsxfun(@eq,A.time,B.time)))));
else
C = false;
end
l = logical(transpose(all(transpose(bsxfun(@eq,o.time,p.time)))));
end
%@test:1
......
function C = ge(A,B) % --*-- Unitary tests --*--
function l = ge(varargin) % --*-- Unitary tests --*--
% Overloads the >= operator for dates objects.
%
% INPUTS
% o A dates object with n or 1 elements.
% o B dates object with n or 1 elements.
% - o [dates] dates object with n or 1 elements.
% - p [dates] dates object with n or 1 elements.
%
% OUTPUTS
% o C column vector of max(n,1) elements (zeros or ones).
% - l [logical] column vector of max(n,1) elements (zeros or ones).
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2014 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% This code 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,
% Dynare dates submodule 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.
......@@ -26,57 +24,27 @@ function C = ge(A,B) % --*-- Unitary tests --*--
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if ~isequal(nargin,2)
error('dates::ge: I need exactly two input arguments!')
end
if ~isa(A,'dates') || ~isa(B,'dates')
error(['dates::ge: Input arguments ''' inputname(1) ''' and ''' inputname(2) ''' have to be a dates objects!'])
end
[o, p] = comparison_arg_checks(varargin{:});
if ~isequal(A.freq,B.freq)
C = false;
return
end
if isequal(A.ndat, B.ndat)
C = (A==B);
idx = find(C==0);
if isequal(o.ndat, p.ndat)
l = (o==p);
idx = find(l==false);
for i=1:length(idx)
C(idx(i)) = greaterorequal(A.time(idx(i),:), B.time(idx(i),:));
l(idx(i)) = greaterorequal(o.time(idx(i),:), p.time(idx(i),:));
end
else
if isequal(A.ndat,1)
C = false(B.ndat,1);
for i=1:B.ndat
C(i) = greaterorequal(A.time, B.time(i,:));
end
elseif isequal(B.ndat,1)
C = false(A.ndat,1);
for i=1:A.ndat
C(i) = greaterorequal(A.time(i,:), B.time);
if isequal(o.ndat,1)
l = false(p.ndat,1);
for i=1:p.ndat
l(i) = greaterorequal(o.time, p.time(i,:));
end
else
C = false;
end
end
function c = greaterorequal(a,b)
if a(1)>b(1)
c = true;
else
if a(1)<b(1)
c = false;
else
if a(2)>=b(2)
c = true;
else
c = false;
end
l = false(o.ndat,1);
for i=1:o.ndat
l(i) = greaterorequal(o.time(i,:), p.time);
end
end
end
%@test:1
%$ % Define some dates
......
function C = gt(A,B) % --*-- Unitary tests --*--
function l = gt(varargin) % --*-- Unitary tests --*--
% Overloads the > operator for dates objects.
%
% INPUTS
% o A dates object with n or 1 elements.
% o B dates object with n or 1 elements.
% INPUTS
% - o [dates] dates object with n or 1 elements.
% - p [dates] dates object with n or 1 elements.
%
% OUTPUTS
% o C column vector of max(n,1) elements (zeros or ones).
% OUTPUTS
% - l [logical] column vector of max(n,1) elements (zeros or ones).
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2014 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% This code 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,
% Dynare dates submodule 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.
......@@ -26,55 +24,26 @@ function C = gt(A,B) % --*-- Unitary tests --*--
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if ~isequal(nargin,2)
error('dates::gt: I need exactly two input arguments!')
end
if ~isa(A,'dates') || ~isa(B,'dates')
error(['dates::gt: Input arguments ''' inputname(1) ''' and ''' inputname(2) ''' have to be a dates objects!'])
end
[o, p] = comparison_arg_checks(varargin{:});
if ~isequal(A.freq,B.freq)
C = false;
return
end
if isequal(A.ndat, B.ndat)
C = false(A.ndat,1);
for i=1:A.ndat
C(i) = greaterthan(A.time(i,:), B.time(i,:));
if isequal(o.ndat, p.ndat)
l = false(o.ndat,1);
for i=1:o.ndat
l(i) = greaterthan(o.time(i,:), p.time(i,:));
end
else
if isequal(A.ndat,1)
C = false(B.ndat,1);
for i=1:B.ndat
C(i) = greaterthan(A.time, B.time(i,:));
end
elseif isequal(B.ndat,1)
C = false(A.ndat,1);
for i=1:A.ndat
C(i) = greaterthan(A.time(i,:), B.time);
if isequal(o.ndat,1)
l = false(p.ndat,1);
for i=1:p.ndat
l(i) = greaterthan(o.time, p.time(i,:));
end
else
C = false;
end
end
function c = greaterthan(a,b)
if a(1)>b(1)
c = true;
else
if a(1)<b(1)
c = false;
else
if a(2)>b(2)
c = true;
else
c = false;
end
l = false(o.ndat,1);
for i=1:o.ndat
l(i) = greaterthan(o.time(i,:), p.time);
end
end
end
%@test:1
%$ % Define some dates
......
function C = le(A,B) % --*-- Unitary tests --*--
function l = le(varargin) % --*-- Unitary tests --*--
% Overloads the <= operator for dates objects.
%
% INPUTS
% o A dates object with n or 1 elements.
% o B dates object with n or 1 elements.
% - o [dates] dates object with n or 1 elements.
% - p [dates] dates object with n or 1 elements.
%
% OUTPUTS
% o C column vector of max(n,1) elements (zeros or ones).
% - l [logical] column vector of max(n,1) elements (zeros or ones).
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2014 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% This code 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,
% Dynare dates submodule 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.
......@@ -26,57 +24,27 @@ function C = le(A,B) % --*-- Unitary tests --*--
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if ~isequal(nargin,2)
error('dates::le: I need exactly two input arguments!')
end
if ~isa(A,'dates') || ~isa(B,'dates')
error(['dates::le: Input arguments ''' inputname(1) ''' and ''' inputname(2) ''' have to be a dates objects!'])
end
[o, p] = comparison_arg_checks(varargin{:});
if ~isequal(A.freq,B.freq)
C = false;
return
end
if isequal(A.ndat, B.ndat)
C = (A==B);
idx = find(C==0);
if isequal(o.ndat, p.ndat)
l = (o==p);
idx = find(l==0);
for i=1:length(idx)
C(idx(i)) = lessorequal(A.time(idx(i),:), B.time(idx(i),:));
l(idx(i)) = lessorequal(o.time(idx(i),:), p.time(idx(i),:));
end
else
if isequal(A.ndat,1)
C = false(B.ndat,1);
for i=1:B.ndat
C(i) = lessorequal(A.time, B.time(i,:));
end
elseif isequal(B.ndat,1)
C = false(A.ndat,1);
for i=1:A.ndat
C(i) = lessorequal(A.time(i,:), B.time);
if isequal(o.ndat,1)
l = false(p.ndat,1);
for i=1:p.ndat
l(i) = lessorequal(o.time, p.time(i,:));
end
else
C = false;
end
end
function c = lessorequal(a, b)
if a(1)<b(1)
c = true;
else
if a(1)>b(1)
c = false;
else
if a(2)<=b(2)
c = true;
else
c = false;
end
l = false(o.ndat,1);
for i=1:o.ndat
l(i) = lessorequal(o.time(i,:), p.time);
end
end
end
%@test:1
%$ % Define some dates
......
function C = lt(A,B) % --*-- Unitary tests --*--
function l = lt(varargin) % --*-- Unitary tests --*--
% Overloads the < operator for dates objects.
%
% INPUTS
% o A dates object with n or 1 elements.
% o B dates object with n or 1 elements.
% - o [dates] dates object with n or 1 elements.
% - p [dates] dates object with n or 1 elements.
%
% OUTPUTS
% o C column vector of max(n,1) elements (zeros or ones).
% - l [logical] column vector of max(n,1) elements (zeros or ones).
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2014 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% This code 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,
% Dynare dates submodule 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.
......@@ -26,54 +24,26 @@ function C = lt(A,B) % --*-- Unitary tests --*--
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if ~isequal(nargin,2)
error('dates::lt: I need exactly two input arguments!')
end
if ~isa(A,'dates') || ~isa(B,'dates')
error(['dates::lt: Input arguments ''' inputname(1) ''' and ''' inputname(2) ''' have to be a dates objects!'])
end
if ~isequal(A.freq,B.freq)
C = false;
return
end
[o, p] = comparison_arg_checks(varargin{:});
if isequal(A.ndat, B.ndat)
C = false(A.ndat,1);
for i=1:A.ndat
C(i) = lessthan(A.time(i,:),B.time(i,:));
if isequal(o.ndat, p.ndat)
l = false(o.ndat,1);
for i=1:o.ndat
l(i) = lessthan(o.time(i,:),p.time(i,:));
end
else
if isequal(A.ndat,1)
C = false(B.ndat,1);
for i=1:B.ndat
C(i) = lessthan(A.time,B.time(i,:));
if isequal(o.ndat,1)
l = false(p.ndat,1);
for i=1:p.ndat
l(i) = lessthan(o.time,p.time(i,:));
end
elseif isequal(B.ndat,1)
C = false(A.ndat,1);
for i=1:A.ndat
C(i) = lessthan(A.time(i,:),B.time);
end
else
C = false;
end
end
function c = lessthan(a,b)
if a(1)<b(1)
c = true;
else
if a(1)>b(1)
c = false;
else
if a(2)<b(2)
c = true;
else
c = false;
end
l = false(o.ndat,1);
for i=1:o.ndat
l(i) = lessthan(o.time(i,:),p.time);
end
end
end
%@test:1
%$ % Define some dates
......@@ -119,4 +89,4 @@ function c = lessthan(a,b)
%$ t(5) = dassert(dates(B5)<dd,false(4,1));
%$ t(6) = dassert(dates(B1)<dd,[false; true(3,1)]);
%$ T = all(t);
%@eof:2
%@eof:2
\ No newline at end of file
function C = ne(A,B) % --*-- Unitary tests --*--
function l = ne(varargin) % --*-- Unitary tests --*--
% Overloads ~= operator for dates objects.
%
% INPUTS
% o A dates object with n or 1 elements.
% o B dates object with n or 1 elements.
% INPUTS
% - o [dates] dates object with n or 1 elements.
% - p [dates] dates object with n or 1 elements.
%
% OUTPUTS
% o C column vector of max(n,1) elements (zeros or ones).
% OUTPUTS
% - l [logical] column vector of max(n,1) elements (zeros or ones).
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2014 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% This code 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,
% Dynare dates submodule 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.
......@@ -26,27 +24,12 @@ function C = ne(A,B) % --*-- Unitary tests --*--
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if ~isequal(nargin,2)
error('dates::ne: I need exactly two input arguments!')
end
if ~isdates(A) || ~isdates(B)
error(['dates::ne: Input arguments ''' inputname(1) ''' and ''' inputname(2) ''' have to be dates objects!'])
end
if ~isequal(A.freq,B.freq)
C = false;
return
end
[o, p] = comparison_arg_checks(varargin{:});
if isequal(A.ndat, B.ndat)
C = logical(transpose(any(transpose(ne(A.time,B.time)))));
if isequal(o.ndat, p.ndat)
l = logical(transpose(any(transpose(ne(o.time,p.time)))));
else
if isequal(A.ndat,1) || isequal(B.ndat,1)
C = logical(transpose(any(transpose(bsxfun(@ne,A.time,B.time)))));
else
C = false;
end
l = logical(transpose(any(transpose(bsxfun(@ne,o.time,p.time)))));
end
%@test:1
......
function [o, p] = comparison_arg_checks(varargin)
% Returns two dates objects or an error if objects to be compared are not compatible.
%
% INPUTS
% - varargin
%
% OUTPUTS
% - o [dates] dates object with n or 1 elements.
% - p [dates] dates object with n or 1 elements.
% Copyright (C) 2014 Dynare Team
%
% This code 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 dates submodule 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/>.
if ~isequal(nargin,2)
error('dates:ge:ArgCheck','I need exactly two input arguments!')
end
if ~isa(varargin{1},'dates') || ~isa(varargin{2},'dates')
error('dates:ge:ArgCheck','Input arguments have to be dates objects!')
end
if ~isequal(varargin{1}.freq,varargin{2}.freq)
error('dates:ge:ArgCheck','Input arguments must have common frequency!')
end
if ~isequal(varargin{1}.ndat, varargin{2}.ndat) && ~(isequal(varargin{1}.ndat,1) || isequal(varargin{2}.ndat,1))
error('dates:ge:ArgCheck','Dimensions are not consistent!')
end
o = varargin{1};
p = varargin{2};
\ No newline at end of file
function c = greaterorequal(a,b)
% Copyright (C) 2013-2014 Dynare Team
%
% This code 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 dates submodule 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/>.
if a(1)>b(1)
c = true;
else
if a(1)<b(1)
c = false;
else
if a(2)>=b(2)
c = true;
else
c = false;
end
end
end
\ No newline at end of file
function c = greaterthan(a,b)
% Copyright (C) 2013-2014 Dynare Team
%
% This code 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 dates submodule 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/>.
if a(1)>b(1)
c = true;
else
if a(1)<b(1)
c = false;
else
if a(2)>b(2)
c = true;
else
c = false;
end
end
end
\ No newline at end of file
function c = lessorequal(a, b)
% Copyright (C) 2013-2014 Dynare Team
%
% This code 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 dates submodule 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/>.
if a(1)<b(1)
c = true;
else
if a(1)>b(1)
c = false;
else
if a(2)<=b(2)
c = true;
else
c = false;
end
end
end
\ No newline at end of file