[afaced]: inst / @dataframe / private / df_basecomp.m Maximize Restore History

Download this file

df_basecomp.m    194 lines (180 with data), 6.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 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
 94
 95
 96
 97
 98
 99
100
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
189
190
191
192
193
function [A, B, C] = df_basecomp(A, B, itercol=true, func=@plus);
%# function [A, B, C] = df_basecomp(A, B, itercol)
%# Basic size and metadata compatibility verifications for
%# two-arguments operations on dataframe. Returns a scalar, a matrix,
%# or a dataframe. Cell arrays are converted to df. Third output
%# contains a merge of the metadata.
%% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
%%
%% This file is part of Octave.
%%
%% Octave 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 2, or (at your option) any later version.
%%
%% Octave 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 Octave; see the file COPYING. If not,
%% write to the Free Software Foundation, 51 Franklin Street -
%% Fifth Floor, Boston, MA 02110-1301, USA.
%#
%# $Id$
%#
if (1 == length (itercol))
strict = false;
else
strict = itercol(2); itercol = itercol(1);
endif
if (iscell (A)), A = dataframe (A); endif
if (iscell (B)), B = dataframe (B); endif
switch (func2str (func)),
case 'bsxfun'
%# bsxfun compatibility rule: if there is at least one singleton
%# dim, the smallest is repeated to reach the size of the
%# greatest. Otherwise, all dims must be equal.
if (any (size (A)(1:2) ~= size (B)(1:2)))
if (~any (1 == [size(A) size(B)]))
error ('bsxfun: both arguments must have the same dim, of one of them must have at least one singleton dim');
else
Csize = max ([size(A)(1:2); size(B)(1:2)]);
endif
else
Csize = size (A)(1:2);
endif
case 'mldivide'
if (isscalar (A)),
Csize = size (B)(1:2);
else
if (size (A, 1) ~= size (B, 1))
error ("Non compatible row sizes (op1 is %dx%d, op2 is %dx%d)",\
size (A), size (B)(1:2));
endif
Csize = [size(A, 2) size(B, 2)];
endif
otherwise
%# if strict is set, B may not be non-scalar vs scalar
if ((~(isscalar (A) || isscalar (B)))||(strict && isscalar (A)))
if (itercol), %# requires full compatibility
Csize = size (A)(1:2);
if (any (Csize - size (B)(1:2)))
%# disp([size(A) size(B)])
error ("Non compatible row and columns sizes (op1 is %dx%d, op2 is %dx%d)",\
Csize, size (B));
endif
else %# compatibility with matrix product
if (size (A, 2) - size (B, 1)),
error ("Non compatible columns vs. rows size (op1 is %dx%d, op2 is %dx%d)",\
size (A)(1:2), size (B)(1:2));
endif
Csize = [size(A, 1) size(B, 2)];
endif
endif
endswitch
if (~(isscalar (A) || isscalar (B)))
C = [];
if (isa (A, 'dataframe'))
if (nargout > 2 && all (Csize == size (A)(1:2))),
C = df_allmeta (A, Csize);
endif
if (isa (B, 'dataframe'))
if (nargout > 2 && isempty (C) && all (Csize == size (B)(1:2))),
C = df_allmeta (B, Csize);
endif
if (strict)
%# compare indexes if both exist
if (~isempty (A._ridx))
if (~isempty(B._ridx) && itercol)
if (any (A._ridx-B._ridx)),
error ("Non compatible indexes");
endif
endif
else
if (nargout > 2 && itercol), C._ridx = B._ridx; endif
endif
if (itercol),
idxB = 1; %# row-row comparison
else
idxB = 2; %# row-col comparsion
endif
if (~isempty (A._name{1}))
if (~isempty (B._name{idxB}))
dummy = ~(strcmp (cellstr (A._name{1}), cellstr (B._name{idxB}))\
| (A._over{1}(:)) | (B._over{idxB}(:)));
if (any (dummy))
if (itercol),
error ("Incompatible row names");
else
error ("Incompatible row vs. column names");
endif
endif
dummy = A._over{1} > B._over{idxB};
if (any (dummy)),
C._name{1}(dummy) = B._name{idxB}(dummy);
C._over{1}(dummy) = B._over{idxB}(dummy);
endif
endif
else
if (nargout > 2),
C._name{1} = B._name{idxB}; C._over{1} = B._over{idxB};
endif
endif
idxB = 3-idxB;
if (~isempty(A._name{2}))
if (~isempty(B._name{idxB}))
dummy = ~(strcmp (cellstr (A._name{2}), cellstr (B._name{2}))\
| (A._over{2}(:)) | (B._over{2}(:)));
if (any (dummy)),
if (itercol),
error ("Incompatible column vs row names");
else
error ("Incompatible column names");
endif
endif
dummy = A._over{2} > B._over{idxB};
if (any (dummy)),
C._name{2}(dummy) = B._name{idxB}(dummy);
C._over{2}(dummy) = B._over{idxB}(dummy);
endif
endif
else
if (nargout > 2 && ~isempty (B._name{idxB})),
C._name{2} = B._name{idxB}; C._over{2} = B._over{idxB};
endif
endif
endif
if (isempty (A._src) && nargout > 2 && ~isempty (B._src)),
C._src = B._src;
endif
if (isempty (A._cmt) && nargout > 2 && ~isempty (B._cmt)),
C._cmt = B._cmt;
endif
else %# B is not a df
if (nargout > 2 && isempty (C)),
C = df_allmeta (A);
endif
endif
else %# A is not a df
if (nargout > 2),
if (all (Csize == size (B)(1:2))),
C = df_allmeta (B, Csize);
else
C = df_allmeta (B);
endif
endif
endif
else %# both arg are scalar
if (nargout > 2)
if (isa (A, 'dataframe'))
C = df_allmeta (A);
else
C = df_allmeta (B);
endif
endif
endif
endfunction