insert.m 3.42 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 51
% Keep the second input argument constant.
p = copy(p);

[o, p] = align(o, p);
52 53 54 55

n = length(id);

if n>1
Stéphane Adjemian's avatar
Stéphane Adjemian committed
56 57 58 59
    [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
60
    p.ops = p.ops(jd);
61 62 63
end

for i=1:n
64 65 66
    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
67
    o.ops = insert_object_in_a_one_dimensional_cell_array(o.ops, p.ops{i}, id(i));
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 94 95 96 97 98 99 100
    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,[]);
%$
%$ try
%$    ts1 = insert(ts1,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);
%$ end
%$ T = all(t);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
%@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