insert.m 4.88 KB
Newer Older
1
function o = insert(o, p, id) % --*-- Unitary tests --*--
2

3 4
% Adds a variable in a dseries object.
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
5
% INPUTS
6 7 8 9
% - o    [dseries]
% - p    [dseries]
% - id   [integer]   vector of indices.
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
10
% OUTPUTS
11
% - o    [dseries]
12

13
% Copyright (C) 2013-2017 Dynare Team
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
%
% 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 <http://www.gnu.org/licenses/>.

30
[n, message] = common_strings_in_cell_arrays(o.name, p.name);
31 32

if n
Stéphane Adjemian's avatar
Stéphane Adjemian committed
33 34 35 36 37
    if isempty(inputname(1))
        error(['dseries::insert: Variable(s) ' message ' already exist in dseries object!'])
    else
        error(['dseries::insert: Variable(s) ' message ' already exist in ''' inputname(1) '''!'])
    end
38 39
end

40
if ~isequal(frequency(o),frequency(p))
Stéphane Adjemian's avatar
Stéphane Adjemian committed
41 42 43 44 45
    if isempty(inputname(1))
        error(['dseries::insert: dseries objects must have common frequencies!'])
    else
        error(['dseries::insert: ''' inputname(1) ''' and ''' inputname(2) ''' dseries objects must have common frequencies!'])
    end
46 47
end

48 49 50
% Keep the second input argument constant.
p = copy(p);

51
% Add NaNs if necessary.
52
[o, p] = align(o, p);
53 54 55

n = length(id);

56 57 58
% Get tag names in p
ptagnames = fieldnames(p.tags);

59
if n>1
Stéphane Adjemian's avatar
Stéphane Adjemian committed
60 61 62 63
    [id, jd] = sort(id);
    p.data = p.data(:,jd);
    p.name = p.name(jd);
    p.tex = p.tex(jd);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
64
    p.ops = p.ops(jd);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    if ~isempty(ptagnames)
        for i = 1:length(ptagnames)
            p.tags.(ptagnames{i}) = p.tags.(ptagnames{i})(jd);
        end
    end
end

% Get tag names in o
otagnames = fieldnames(o.tags);

% Merge tag names
if isempty(otagnames) && isempty(ptagnames)
    notags = true;
else
    notags = false;
    dtagnames_o = setdiff(ptagnames, otagnames);
    dtagnames_p = setdiff(otagnames, ptagnames);
    if ~isempty(dtagnames_o)
        % If p has tags that are not in o...
        for i=1:length(dtagnames_o)
            o.tags.(dtagnames_o{i}) = cell(vobs(o), 1);
        end
    end
    if ~isempty(dtagnames_p)
        % If o has tags that are not in p...
        for i=1:length(dtagnames_p)
            p.tags.(dtagnames_p{i}) = cell(vobs(p), 1);
        end
    end
94 95
end

96 97 98
% Update list of tag names in o.
otagnames = fieldnames(o.tags);

99
for i=1:n
100 101 102
    o.data = insert_column_vector_in_a_matrix(o.data, p.data(:,i),id(i));
    o.name = insert_object_in_a_one_dimensional_cell_array(o.name, p.name{i}, id(i));
    o.tex = insert_object_in_a_one_dimensional_cell_array(o.tex, p.tex{i}, id(i));
Stéphane Adjemian's avatar
Stéphane Adjemian committed
103
    o.ops = insert_object_in_a_one_dimensional_cell_array(o.ops, p.ops{i}, id(i));
104 105 106 107 108
    if ~notags
        for j=1:length(otagnames)
            o.tags.(otagnames{j}) = insert_object_in_a_one_dimensional_cell_array(o.tags.(otagnames{j}), p.tags.(otagnames{j}){i}, id(i));
        end
    end
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    id = id+1;
end

%@test:1
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(5,2);
%$
%$ % Define names.
%$ A_name = {'A1'; 'A2';'A3'};
%$ B_name = {'B1'; 'B2'};
%$
%$ % Define initial dates.
%$ A_init = '1950Q1';
%$ B_init = '1950Q3';
%$
%$ % Instantiate two dseries objects.
%$ ts1 = dseries(A, A_init, A_name,[]);
%$ ts2 = dseries(B, B_init, B_name,[]);
127 128 129 130 131 132 133 134 135 136
%$ ts1.tag('t1');
%$ ts1.tag('t1','A1',1);
%$ ts1.tag('t1','A2',1);
%$ ts1.tag('t1','A3',0);
%$ ts2.tag('t1');
%$ ts2.tag('t1','B1',1);
%$ ts2.tag('t1','B2',1);
%$ ts2.tag('t2');
%$ ts2.tag('t2','B1','toto');
%$ ts2.tag('t2','B2','titi');
137 138 139 140 141 142 143 144
%$
%$ try
%$    ts1 = insert(ts1,ts2,[1,2]);
%$    t(1) = 1;
%$ catch
%$    t = 0;
%$ end
%$
145 146 147
%$
%$ if t(1)
%$    t(2) = dassert(ts1.name,{'B1';'A1';'B2';'A2';'A3'});
148 149 150
%$    t(3) = dassert(ts1.nobs,10);
%$    eB = [NaN(2,2); B; NaN(3,2)];
%$    t(4) = dassert(ts1.data,[eB(:,1), A(:,1), eB(:,2), A(:,2:3)], 1e-15);
151 152
%$    t(5) = dassert(ts1.tags.t1,{1; 1; 1; 1; 0});
%$    t(6) = dassert(ts1.tags.t2,{'toto'; []; 'titi'; []; []});
153 154
%$ end
%$ T = all(t);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
%@eof:1

%@test:2
%$ % Define a datasets.
%$ A = rand(10,3); B = rand(5,2);
%$
%$ % Define names.
%$ A_name = {'A1'; 'A2';'A3'};
%$ B_name = {'B1'; 'B2'};
%$
%$ % Define initial dates.
%$ A_init = '1950Q1';
%$ B_init = '1950Q3';
%$
%$ % Instantiate two dseries objects.
%$ ts1 = dseries(A, A_init, A_name,[]);
%$ ts2 = dseries(B, B_init, B_name,[]);
%$
%$ try
%$    ts1.insert(ts2,[1,2]);
%$    t(1) = 1;
%$ catch
%$    t = 0;
%$ end
%$
%$ if length(t)>1
%$    t(2) = dassert(ts1.vobs,{'B1';'A1';'B2';'A3'});
%$    t(3) = dassert(ts1.nobs,10);
%$    eB = [NaN(2,2); B; NaN(3,2)];
%$    t(4) = dassert(ts1.data,[eB(:,1), A(:,1), eB(:,2), A(:,2:3)], 1e-15);
%$    t(5) = dassert(ts2.data, B);
%$ end
%$ T = all(t);
%@eof:2