diff --git a/doc/dynare.texi b/doc/dynare.texi index 513bbe5b4..74da5d5cc 100644 --- a/doc/dynare.texi +++ b/doc/dynare.texi @@ -9237,6 +9237,44 @@ do3 = dseries([1; 2; 3], 1999Q3, @{`var123'@}, @{`var_@{123@}'@}); @noindent A list of the available methods, by alphabetical order, is given below. +@deftypefn {dseries} {@var{A} =} abs (@var{B}) + +Overloads the @code{abs()} function for @dseries objects. Returns the absolute value of the variables in @dseries object @var{B}. + +@examplehead +@example +>> ts0 = dseries(randn(3,2),'1973Q1',@{'A1'; 'A2'@},@{'A_1'; 'A_2'@}); +>> ts1 = ts0.abs(); +>> ts0 + +ts0 is a dseries object: + + | A1 | A2 +1973Q1 | -0.67284 | 1.4367 +1973Q2 | -0.51222 | -0.4948 +1973Q3 | 0.99791 | 0.22677 + +>> ts1 + +ts1 is a dseries object: + + | abs(A1) | abs(A2) +1973Q1 | 0.67284 | 1.4367 +1973Q2 | 0.51222 | 0.4948 +1973Q3 | 0.99791 | 0.22677 + +>> ts1.tex + +ans = + + '|A_1|' + '|A_2|' +@end example + +@end deftypefn + +@sp 1 + @deftypefn {dseries} {[@var{A}, @var{B}] = } align (@var{A}, @var{B}) If @dseries objects @var{A} and @var{B} are defined on different time ranges, this function extends @var{A} and/or @var{B} with NaNs so that they are defined on the same time range. Note that both @dseries objects must have the same frequency. diff --git a/matlab/@dseries/abs.m b/matlab/@dseries/abs.m new file mode 100644 index 000000000..b8d49bce7 --- /dev/null +++ b/matlab/@dseries/abs.m @@ -0,0 +1,110 @@ +function A = abs(B) % --*-- Unitary tests --*-- + +%@info: +%! @deftypefn {Function File} {@var{A} =} abs (@var{B}) +%! @anchor{@dseries/uminus} +%! @sp 1 +%! Overloads the abs method for the Dynare time series class (@ref{dseries}). +%! @sp 2 +%! @strong{Inputs} +%! @sp 1 +%! @table @ @var +%! @item B +%! Dynare time series object instantiated by @ref{dseries}. +%! @end table +%! @sp 1 +%! @strong{Outputs} +%! @sp 1 +%! @table @ @var +%! @item A +%! Dynare time series object. +%! @end deftypefn +%@eod: + +% 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 . + +A = dseries(); + +A.freq = B.freq; +A.nobs = B.nobs; +A.vobs = B.vobs; +A.init = B.init; +A.dates = B.dates; +A.name = cell(A.vobs,1); +A.tex = cell(A.vobs,1); +for i = 1:A.vobs + A.name(i) = {[ 'abs(' B.name{i} ')']}; + A.tex(i) = {[ '|' B.tex{i} '|']}; +end +A.data = abs(B.data); + +%@test:1 +%$ % Define a datasets. +%$ A = randn(10,2); +%$ +%$ % Define names +%$ A_name = {'A1';'A2'}; +%$ A_tex = {'A_1';'A_2'}; +%$ t = zeros(6,1); +%$ +%$ % Instantiate a time series object and compute the absolute value. +%$ try +%$ ts1 = dseries(A,[],A_name,A_tex); +%$ ts2 = abs(ts1); +%$ t(1) = 1; +%$ catch +%$ t = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dyn_assert(ts2.vobs,2); +%$ t(3) = dyn_assert(ts2.nobs,10); +%$ t(4) = dyn_assert(ts2.data,abs(A),1e-15); +%$ t(5) = dyn_assert(ts2.name,{'abs(A1)';'abs(A2)'}); +%$ t(6) = dyn_assert(ts2.tex,{'|A_1|';'|A_2|'}); +%$ end +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ % Define a datasets. +%$ A = randn(10,2); +%$ +%$ % Define names +%$ A_name = {'A1';'A2'}; +%$ A_tex = {'A_1';'A_2'}; +%$ t = zeros(6,1); +%$ +%$ % Instantiate a time series object and compute the absolute value. +%$ try +%$ ts1 = dseries(A,[],A_name,A_tex); +%$ ts2 = ts1.abs(); +%$ t(1) = 1; +%$ catch +%$ t = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dyn_assert(ts2.vobs,2); +%$ t(3) = dyn_assert(ts2.nobs,10); +%$ t(4) = dyn_assert(ts2.data,abs(A),1e-15); +%$ t(5) = dyn_assert(ts2.name,{'abs(A1)';'abs(A2)'}); +%$ t(6) = dyn_assert(ts2.tex,{'|A_1|';'|A_2|'}); +%$ end +%$ T = all(t); +%@eof:2 \ No newline at end of file diff --git a/matlab/@dseries/subsref.m b/matlab/@dseries/subsref.m index f1cd3be25..486840c56 100644 --- a/matlab/@dseries/subsref.m +++ b/matlab/@dseries/subsref.m @@ -70,7 +70,7 @@ switch S(1).type error(['dseries::subsref: ' S(1).subs ' is not a method but a member!']) end B = builtin('subsref', A, S(1)); - case {'log','exp','ygrowth','qgrowth','ydiff','qdiff'} % Give "dot access" to public methods without args. + case {'log','exp','ygrowth','qgrowth','ydiff','qdiff','abs'} % Give "dot access" to public methods without args. B = feval(S(1).subs,A); if length(S)>1 && isequal(S(2).type,'()') && isempty(S(2).subs) S = shiftS(S);