1function q = mpower(o, p) % --*-- Unitary tests --*--
2
3% Overloads the power (^) operator for dseries objects.
4%
5% INPUTS
6% - o [dseries]           T observations and N variables.
7% - p [dseries,double]    scalar, vector or dseries object.
8%
9% OUTPUTS
10% - q [dseries]           T observations and N variables.
11
12% Copyright (C) 2013-2017 Dynare Team
13%
14% This file is part of Dynare.
15%
16% Dynare is free software: you can redistribute it and/or modify
17% it under the terms of the GNU General Public License as published by
18% the Free Software Foundation, either version 3 of the License, or
19% (at your option) any later version.
20%
21% Dynare is distributed in the hope that it will be useful,
22% but WITHOUT ANY WARRANTY; without even the implied warranty of
23% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24% GNU General Public License for more details.
25%
26% You should have received a copy of the GNU General Public License
27% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
28
29if isnumeric(o) && isvector(o) && isdseries(p) && length(o)==nobs(p)
30    q = dseries(zeros(size(p.data)), p.firstdate);
31    q.data = bsxfun(@power, o, p.data);
32    for i=1:vobs(q)
33        q.ops(i) = {sprintf('power(%s, %s)', matrix2string(o), p.name{i})};
34    end
35    return
36end
37
38if isnumeric(p) && isvector(p) && isdseries(o) && length(p)==nobs(o)
39    q = copy(o);
40    q.data = bsxfun(@power, o.data, p);
41    for i=1:vobs(q)
42        if isempty(q.ops{i})
43            q.ops(i) = {sprintf('power(%s, %s)', q.name{i}, matrix2string(p))};
44        else
45            q.ops(i) = {sprintf('power(%s, %s)', q.name{i}, matrix2string(p))};
46        end
47    end
48    return
49end
50
51if isdseries(o) && isnumeric(p) && isreal(p) &&  isscalar(p)
52    q = copy(o);
53    q.data = o.data.^p;
54    for i=1:vobs(q)
55        if isempty(o.ops{i})
56            q.ops(i) = {sprintf('power(%s, %s)', q.name{i}, num2str(p))};
57        else
58            q.ops(i) = {sprintf('power(%s, %s)', o.ops{i}, num2str(p))};
59        end
60    end
61    return
62end
63
64if isdseries(o) && isdseries(p)
65    if isequal(nobs(o),nobs(p)) && isequal(vobs(o), vobs(p)) && isequal(frequency(o),frequency(p))
66        q = copy(o);
67        q.data = (o.data).^p.data;
68        for i=1:vobs(q)
69            if isempty(o.ops{i})
70                if isempty(p.ops{i})
71                    q.ops(i) = {sprintf('power(%s, %s)', q.name{i}, p.name{i})};
72                else
73                    q.ops(i) = {sprintf('power(%s, %s)', q.name{i}, p.ops{i})};
74                end
75            else
76                if isempty(p.ops{i})
77                    q.ops(i) = {sprintf('power(%s, %s)', o.ops{i}, p.name{i})};
78                else
79                    q.ops(i) = {sprintf('power(%s, %s)', o.ops{i}, p.ops{i})};
80                end
81            end
82        end
83    else
84        error('dseries:WrongInputArguments', 'If both input arguments are dseries objects, they must have the same numbers of variables and observations and common frequency!')
85    end
86    return
87end
88
89error('dseries:WrongInputArguments', 'Wrong calling sequence! Please check the manual.')
90
91%@test:1
92%$ % Define a datasets.
93%$ A = rand(10,2); B = randn(10,2);
94%$
95%$ % Define names
96%$ A_name = {'A1';'A2'}; B_name = {'B1';'B2'};
97%$
98%$
99%$ % Instantiate a time series object.
100%$ try
101%$    ts1 = dseries(A,[],A_name,[]);
102%$    ts2 = dseries(B,[],B_name,[]);
103%$    ts3 = ts1^ts2;
104%$    t(1) = true;
105%$ catch
106%$    t(1) = false;
107%$ end
108%$
109%$ if t(1)
110%$    t(2) = dassert(ts3.vobs,2);
111%$    t(3) = dassert(ts3.nobs,10);
112%$    t(4) = dassert(ts3.data,A.^B,1e-15);
113%$    t(5) = dassert(ts3.name,{'A1';'A2'});
114%$    t(6) = dassert(ts3.tex,{'A1';'A2'});
115%$    t(7) = dassert(ts1.data, A, 1e-15);
116%$    t(8) = dassert(ts3.ops,{'power(A1, B1)';'power(A2, B2)'});
117%$ end
118%$ T = all(t);
119%@eof:1
120
121%@test:2
122%$ % Define a datasets.
123%$ A = rand(10,2);
124%$
125%$ % Define names
126%$ A_name = {'A1';'A2'};
127%$
128%$
129%$ % Instantiate a time series object.
130%$ try
131%$    ts1 = dseries(A,[],A_name,[]);
132%$    ts3 = ts1^2;
133%$    t(1) = true;
134%$ catch
135%$    t(1) = false;
136%$ end
137%$
138%$ if t(1)
139%$    t(2) = dassert(ts3.vobs,2);
140%$    t(3) = dassert(ts3.nobs,10);
141%$    t(4) = dassert(ts3.data,A.^2,1e-15);
142%$    t(5) = dassert(ts3.name,{'A1';'A2'});
143%$    t(6) = dassert(ts3.tex,{'A1';'A2'});
144%$    t(7) = dassert(ts3.ops,{'power(A1, 2)';'power(A2, 2)'});
145%$ end
146%$ T = all(t);
147%@eof:2
148
149%@test:3
150%$ % Define a dseries object
151%$ ts1=dseries([1 1;2 2;3 3], '1999y', {'MyVar1','MyVar2'});
152%$
153%$ % Use the power
154%$ try
155%$    ts2 = ts1^transpose(1:3);
156%$    t(1) = true;
157%$ catch
158%$    t(1) = false;
159%$ end
160%$
161%$ if t(1)
162%$    t(2) = dassert(ts2.vobs,2);
163%$    t(3) = dassert(ts2.nobs,3);
164%$    t(4) = dassert(ts2.data,bsxfun(@power,ts1.data,transpose(1:3)),1e-15);
165%$    t(5) = dassert(ts2.name,{'MyVar1';'MyVar2'});
166%$    t(6) = dassert(ts2.tex,{'MyVar1';'MyVar2'});
167%$ end
168%$ T = all(t);
169%@eof:3