Commit 262c6a3a authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Changed the naming convention of the variables in the overloaded operators for the dynSeries class.

Names for the results of basic operations on dynSeries objects was '--NA--'. The name of the result
variables now keep track of the operations. For instance, if we have two dynSeries objects, ts1 and
ts2, respectively holding two variables 'A1' and 'B1', then:

 - the name of the variable in ts3=ts1+ts2 is 'plus(A1,B1)'
 - the name of the variable in ts3=ts1-ts2 is 'minus(A1,B1)'
 - the name of the variable in ts3=ts1*ts2 is 'multiply(A1,B1)'
 - the name of the variable in ts3=ts1/ts2 is 'divide(A1,B1)'
 - the tex name of the variable in ts3=ts1+ts2 is '(A1+B1)'
 - the tex name of the variable in ts3=ts1-ts2 is '(A1-B1)'
 - the tex name of the variable in ts3=ts1*ts2 is '(A1*B1)'
 - the tex name of the variable in ts3=ts1/ts2 is '(A1/B1)'
parent 055ae257
......@@ -44,10 +44,9 @@ function ts = exp(ts)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
ts.data = exp(ts.data);
if ~isa(ts,'dynSeries')
error('dynSeries::exp: Input argument has to be a Dynare time series object!')
end
ts.data = exp(ts.data);
\ No newline at end of file
for i=1:ts.vobs
ts.name(i) = {['exp(' ts.name{i} ')']};
ts.tex(i) = {['\exp(' ts.tex{i} ')']};
end
\ No newline at end of file
......@@ -43,14 +43,13 @@ function ts = log(ts)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
if ~isa(ts,'dynSeries')
error('dynSeries::log: Input argument has to be a Dynare time series object!')
end
if any(ts.data<eps)
error('dynSeries::log: Input argument has to be strictly positive!')
end
for i=1:ts.vobs
ts.name(i) = {['log(' ts.name{i} ')']};
ts.tex(i) = {['\log(' ts.tex{i} ')']};
end
ts.data = log(ts.data);
\ No newline at end of file
......@@ -41,6 +41,17 @@ function A = minus(B,C)
if ~isequal(B.vobs,C.vobs) && ~(isequal(B.vobs,1) || isequal(C.vobs,1))
error(['dynSeries::plus: Cannot substract ' inputname(1) ' and ' inputname(2) ' (wrong number of variables)!'])
else
if B.vobs>C.vobs
idB = 1:B.vobs;
idC = ones(1:B.vobs);
elseif B.vobs<C.vobs
idB = ones(1,C.vobs);
idC = 1:C.vobs;
else
idB = 1:B.vobs;
idC = 1:C.vobs;
end
end
if ~isequal(B.nobs,C.nobs)
......@@ -72,8 +83,12 @@ A.init = B.init;
A.time = B.time;
A.nobs = max(B.nobs,C.nobs);
A.vobs = max(B.vobs,C.vobs);
A.name = repmat({'--NA--'},A.vobs,1);
A.tex = repmat({'--NA--'},A.vobs,1);
A.name = cell(A.vobs,1);
A.tex = cell(A.vobs,1);
for i=1:A.vobs
A.name(i) = {['minus(' B.name{idB(i)} ',' C.name{idC(i)} ')']};
A.tex(i) = {['(' B.tex{idB(i)} '-' C.tex{idC(i)} ')']};
end
A.data = bsxfun(@minus,B.data,C.data);
%@test:1
......@@ -83,7 +98,7 @@ A.data = bsxfun(@minus,B.data,C.data);
%$ % Define names
%$ A_name = {'A1';'A2'}; B_name = {'B1'};
%$
%$ t = zeros(4,1);
%$ t = zeros(5,1);
%$
%$ % Instantiate a time series object.
%$ try
......@@ -99,6 +114,7 @@ A.data = bsxfun(@minus,B.data,C.data);
%$ t(2) = dyn_assert(ts3.vobs,2);
%$ t(3) = dyn_assert(ts3.nobs,10);
%$ t(4) = dyn_assert(ts3.data,[A(:,1)-B, A(:,2)-B],1e-15);
%$ t(5) = dyn_assert(ts3.name,{'minus(A1,B1)';'minus(A2,B1)'});
%$ end
%$ T = all(t);
%@eof:1
......@@ -43,6 +43,17 @@ if isa(B,'dynSeries') && isa(C,'dynSeries')
% Element by element divisions of two dynSeries object
if ~isequal(B.vobs,C.vobs) && ~(isequal(B.vobs,1) || isequal(C.vobs,1))
error(['dynSeries::times: Cannot divide ' inputname(1) ' and ' inputname(2) ' (wrong number of variables)!'])
else
if B.vobs>C.vobs
idB = 1:B.vobs;
idC = ones(1:B.vobs);
elseif B.vobs<C.vobs
idB = ones(1,C.vobs);
idC = 1:C.vobs;
else
idB = 1:B.vobs;
idC = 1:C.vobs;
end
end
if ~isequal(B.nobs,C.nobs)
error(['dynSeries::times: Cannot divide ' inputname(1) ' and ' inputname(2) ' (wrong number of observations)!'])
......@@ -59,8 +70,12 @@ if isa(B,'dynSeries') && isa(C,'dynSeries')
A.time = B.time;
A.nobs = max(B.nobs,C.nobs);
A.vobs = max(B.vobs,C.vobs);
A.name = repmat({'--NA--'},A.vobs,1);
A.tex = repmat({'--NA--'},A.vobs,1);
A.name = cell(A.vobs,1);
A.tex = cell(A.vobs,1);
for i=1:A.vobs
A.name(i) = {['divide(' B.name{idB(i)} ',' C.name{idC(i)} ')']};
A.tex(i) = {['(' B.tex{idB(i)} '/' C.tex{idC(i)} ')']};
end
A.data = bsxfun(@rdivide,B.data,C.data);
elseif isnumeric(C) && isreal(C) && isequal(length(C),1) && isa(B,'dynSeries')
% division of a dynSeries object by a real scalar.
......@@ -70,8 +85,12 @@ elseif isnumeric(C) && isreal(C) && isequal(length(C),1) && isa(B,'dynSeries')
A.init = B.init;
A.nobs = B.nobs;
A.vobs = B.vobs;
A.name = repmat({'--NA--'},A.vobs,1);
A.tex = repmat({'--NA--'},A.vobs,1);
A.name = cell(A.vobs,1);
A.tex = cell(A.vobs,1);
for i=1:A.vobs
A.name(i) = {['divide(' B.name{i} ',' num2str(C) ')']};
A.tex(i) = {['(' B.tex{i} '/' num2str(C) ')']};
end
A.data = B.data/C;
elseif isnumeric(B) && isreal(B) && isequal(length(B),1) && isa(C,'dynSeries')
% division of a real scalar by a dynSeries object.
......@@ -81,8 +100,12 @@ elseif isnumeric(B) && isreal(B) && isequal(length(B),1) && isa(C,'dynSeries')
A.init = C.init;
A.nobs = C.nobs;
A.vobs = C.vobs;
A.name = repmat({'--NA--'},A.vobs,1);
A.tex = repmat({'--NA--'},A.vobs,1);
A.name = cell(A.vobs,1);
A.tex = cell(A.vobs,1);
for i=1:A.vobs
A.name(i) = {['divide(' num2str(B) ',' C.name{i} ')']};
A.tex(i) = {['(' num2str(B) '/' C.tex{i} ')']};
end
A.data = B./C.data;
else
error()
......
......@@ -43,6 +43,17 @@ if isa(B,'dynSeries') && isa(C,'dynSeries')
% Element by element multiplication of two dynSeries object
if ~isequal(B.vobs,C.vobs) && ~(isequal(B.vobs,1) || isequal(C.vobs,1))
error(['dynSeries::times: Cannot multiply ' inputname(1) ' and ' inputname(2) ' (wrong number of variables)!'])
else
if B.vobs>C.vobs
idB = 1:B.vobs;
idC = ones(1:B.vobs);
elseif B.vobs<C.vobs
idB = ones(1,C.vobs);
idC = 1:C.vobs;
else
idB = 1:B.vobs;
idC = 1:C.vobs;
end
end
if ~isequal(B.nobs,C.nobs)
error(['dynSeries::times: Cannot multiply ' inputname(1) ' and ' inputname(2) ' (wrong number of observations)!'])
......@@ -59,8 +70,12 @@ if isa(B,'dynSeries') && isa(C,'dynSeries')
A.time = B.time;
A.nobs = max(B.nobs,C.nobs);
A.vobs = max(B.vobs,C.vobs);
A.name = repmat({'--NA--'},A.vobs,1);
A.tex = repmat({'--NA--'},A.vobs,1);
A.name = cell(A.vobs,1);
A.tex = cell(A.vobs,1);
for i=1:A.vobs
A.name(i) = {['multiply(' B.name{idB(i)} ',' C.name{idC(i)} ')']};
A.tex(i) = {['(' B.tex{idB(i)} '*' C.tex{idC(i)} ')']};
end
A.data = bsxfun(@times,B.data,C.data);
elseif isnumeric(C) && isreal(C) && isequal(length(C),1) && isa(B,'dynSeries')
% Multiplication of a dynSeries object by a real scalar.
......@@ -70,8 +85,12 @@ elseif isnumeric(C) && isreal(C) && isequal(length(C),1) && isa(B,'dynSeries')
A.time = B.time;
A.nobs = B.nobs;
A.vobs = B.vobs;
A.name = repmat({'--NA--'},A.vobs,1);
A.tex = repmat({'--NA--'},A.vobs,1);
A.name = cell(A.vobs,1);
A.tex = cell(A.vobs,1);
for i=1:A.vobs
A.name(i) = {['multiply(' B.name{i} ',' num2str(C) ')']};
A.tex(i) = {['(' B.tex{i} '*' num2str(C) ')']};
end
A.data = B.data*C;
elseif isnumeric(B) && isreal(B) && isequal(length(B),1) && isa(C,'dynSeries')
% Multiplication of a dynSeries object by a real scalar.
......@@ -81,8 +100,12 @@ elseif isnumeric(B) && isreal(B) && isequal(length(B),1) && isa(C,'dynSeries')
A.time = C.time;
A.nobs = C.nobs;
A.vobs = C.vobs;
A.name = repmat({'--NA--'},A.vobs,1);
A.tex = repmat({'--NA--'},A.vobs,1);
A.name = cell(A.vobs,1);
A.tex = cell(A.vobs,1);
for i=1:A.vobs
A.name(i) = {['multiply(' num2str(B) ',' C.name{i} ')']};
A.tex(i) = {['(' num2str(B) '*' C.tex{i} ')']};
end
A.data = C.data*B;
else
error()
......@@ -111,6 +134,7 @@ end
%$ t(2) = dyn_assert(ts3.vobs,2);
%$ t(3) = dyn_assert(ts3.nobs,10);
%$ t(4) = dyn_assert(ts3.data,[A(:,1).*B, A(:,2).*B],1e-15);
%$ t(5) = dyn_assert(ts3.name,{'multiply(A1,B1)';'multiply(A2,B1)'});
%$ end
%$ T = all(t);
%@eof:1
......@@ -122,7 +146,7 @@ end
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ t = zeros(4,1);
%$ t = zeros(5,1);
%$
%$ % Instantiate a time series object.
%$ try
......@@ -137,6 +161,7 @@ end
%$ t(2) = dyn_assert(ts2.vobs,2);
%$ t(3) = dyn_assert(ts2.nobs,10);
%$ t(4) = dyn_assert(ts2.data,A*B,1e-15);
%$ t(5) = dyn_assert(ts2.name,{['multiply(A1,' num2str(pi) ')'];['multiply(A2,' num2str(pi) ')']});
%$ end
%$ T = all(t);
%@eof:2
......@@ -163,6 +188,7 @@ end
%$ t(2) = dyn_assert(ts2.vobs,2);
%$ t(3) = dyn_assert(ts2.nobs,10);
%$ t(4) = dyn_assert(ts2.data,A*B,1e-15);
%$ t(5) = dyn_assert(ts2.name,{['multiply(' num2str(pi) ',A1)'];['multiply(' num2str(pi) ',A2)']});
%$ end
%$ T = all(t);
%@eof:3
......@@ -43,6 +43,17 @@ function A = plus(B,C)
if ~isequal(B.vobs,C.vobs) && ~(isequal(B.vobs,1) || isequal(C.vobs,1))
error(['dynSeries::plus: Cannot add ' inputname(1) ' and ' inputname(2) ' (wrong number of variables)!'])
else
if B.vobs>C.vobs
idB = 1:B.vobs;
idC = ones(1:B.vobs);
elseif B.vobs<C.vobs
idB = ones(1,C.vobs);
idC = 1:C.vobs;
else
idB = 1:B.vobs;
idC = 1:C.vobs;
end
end
if ~isequal(B.nobs,C.nobs)
......@@ -73,8 +84,12 @@ A.freq = B.freq;
A.init = B.init;
A.nobs = max(B.nobs,C.nobs);
A.vobs = max(B.vobs,C.vobs);
A.name = repmat({'--NA--'},A.vobs,1);
A.tex = repmat({'--NA--'},A.vobs,1);
A.name = cell(A.vobs,1);
A.tex = cell(A.vobs,1);
for i=1:A.vobs
A.name(i) = {['plus(' B.name{idB(i)} ',' C.name{idC(i)} ')']};
A.tex(i) = {['(' B.tex{idB(i)} '+' C.tex{idC(i)} ')']};
end
A.data = bsxfun(@plus,B.data,C.data);
%@test:1
......@@ -84,7 +99,7 @@ A.data = bsxfun(@plus,B.data,C.data);
%$ % Define names
%$ A_name = {'A1';'A2'}; B_name = {'B1'};
%$
%$ t = zeros(4,1);
%$ t = zeros(5,1);
%$
%$ % Instantiate a time series object.
%$ try
......@@ -100,6 +115,36 @@ A.data = bsxfun(@plus,B.data,C.data);
%$ t(2) = dyn_assert(ts3.vobs,2);
%$ t(3) = dyn_assert(ts3.nobs,10);
%$ t(4) = dyn_assert(ts3.data,[A(:,1)+B, A(:,2)+B],1e-15);
%$ t(5) = dyn_assert(ts3.name,{'plus(A1,B1)';'plus(A2,B1)'});
%$ end
%$ T = all(t);
%@eof:1
%@test:2
%$ % Define a datasets.
%$ A = rand(10,2); B = randn(10,1);
%$
%$ % Define names
%$ A_name = {'A1';'A2'}; B_name = {'B1'};
%$
%$ t = zeros(5,1);
%$
%$ % Instantiate a time series object.
%$ try
%$ ts1 = dynSeries(A,[],A_name,[]);
%$ ts2 = dynSeries(B,[],B_name,[]);
%$ ts3 = ts1+ts2;
%$ ts4 = ts3+ts1;
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if length(t)>1
%$ t(2) = dyn_assert(ts4.vobs,2);
%$ t(3) = dyn_assert(ts4.nobs,10);
%$ t(4) = dyn_assert(ts4.data,[A(:,1)+B, A(:,2)+B]+A,1e-15);
%$ t(5) = dyn_assert(ts4.name,{'plus(plus(A1,B1),A1)';'plus(plus(A2,B1),A2)'});
%$ end
%$ T = all(t);
%@eof:2
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment