[7c0880]: inst / @dataframe / private / df_func.m Maximize Restore History

Download this file

df_func.m    236 lines (227 with data), 9.0 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
function resu = df_func(func, A, B, itercol=true, whole=logical([0 0]));
%# function resu = df_func(func, A, B, whole)
%# Implements an iterator to apply some func when at
%# least one argument is a dataframe. The output is a dataframe with
%# the same metadata, types may be altered, like f.i. double=>logical.
%# When itercol is 'true', the default, LHS is iterated by columns,
%# otherwise by rows. 'Whole' is a two-elements logical vector with
%# the meaning that LHS and or RHS must be iterated at once or not
%% 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$
%#
[A, B, resu] = df_basecomp (A, B, itercol, func);
itercol = itercol(1); %# drop second value
if (isa (B, 'dataframe'))
if (~isa (A, 'dataframe')),
if (isscalar (A)),
for indi = (resu._cnt(2):-1:1)
switch resu._type{indi}
case "char"
resu._data{indi} = feval (func, A, char (B._data{indi}));
otherwise
resu._data{indi} = feval (func, A, B._data{indi});
endswitch
endfor
resu._rep = B._rep;
else
if (whole(1) && ~whole(2))
for indi = (resu._cnt(2):-1:1)
switch resu._type{indi}
case "char"
resu._data{indi} = feval (func, A, \
char (B._data{indi}(:, B._rep{indi})));
otherwise
resu._data{indi} = feval (func, A, \
B._data{indi}(:, B._rep{indi}));
endswitch
resu._rep{indi} = 1:size (resu._data{indi}, 2);
endfor
elseif (itercol && ~whole(2)),
for indi = (resu._cnt(2):-1:1)
switch resu._type{indi}
case "char"
resu._data{indi} = feval (func, squeeze (A(:, indi, :)), \
char (B._data{indi}(:, B._rep{indi})));
otherwise
resu._data{indi} = feval (func, squeeze (A(:, indi, :)), \
B._data{indi}(:, B._rep{indi}));
endswitch
resu._rep{indi} = 1:size (resu._data{indi}, 2);
endfor
elseif (~whole(2)),
warning ("no 3D yet");
for indi = (resu._cnt(2):-1:1)
switch resu._type{indi}
case "char"
resu._data{indi} = feval (func, A(indi, :), char (B._data{indi}));
otherwise
resu._data{indi} = feval (func, A(indi, :), B._data{indi});
endswitch
endfor
else
dummy = feval (func, A, df_whole (B));
for indi = (resu._cnt(2):-1:1) %# store column-wise
resu._data{indi} = squeeze (dummy(:, indi, :));
resu._rep{indi} = 1:size (resu._data{indi}, 2);
resu._type{indi} = class (dummy);
endfor
endif
endif
else
if (itercol)
for indi = (resu._cnt(2):-1:1)
switch resu._type{indi}
case "char"
resu._data{indi} = feval \
(func, char (A._data{indi}(:, A._rep{indi})), \
char (B._data{indi}(B._rep{indi})));
otherwise
resu._data{indi} = feval \
(func, A._data{indi}(:, A._rep{indi}), \
B._data{indi}(:, B._rep{indi}));
endswitch
resu._rep{indi} = 1:size (resu._data{indi}, 2);
endfor
else %# itercol is false
dummy = df_whole(A);
if (whole(1))
for indi = (resu._cnt(2):-1:1)
switch resu._type{indi}
case "char"
resu._data{indi} = feval (func, dummy, \
char (B._data{indi}(:, B._rep{indi})));
otherwise
resu._data{indi} = feval (func, dummy, \
B._data{indi}(:, B._rep{indi}));
endswitch
resu._rep{indi} = 1:size (resu._data{indi}, 2);
endfor
elseif (~whole(2))
for indi = (resu._cnt(2):-1:1)
switch resu._type{indi}
case "char"
resu._data{indi} = squeeze \
(feval (func, dummy(indi, :, :),\
char (B._data{indi}(:, B._rep{indi}))));
otherwise
resu._data{indi} = squeeze \
(feval (func, dummy(indi, :, :), \
B._data{indi}(:, B._rep{indi})));
endswitch
resu._rep{indi} = 1:size (resu._data{indi}, 2);
endfor
else
dummy = feval (func, dummy, df_whole(B));
for indi = (resu._cnt(2):-1:1) %# store column-wise
resu._data{indi} = squeeze (dummy(:, indi, :));
resu._rep{indi} = 1:size (resu._data{indi}, 2);
resu._type{indi} = class (dummy);
endfor
endif
endif
endif
else %# B is not a dataframe
if (isscalar (B))
for indi = (resu._cnt(2):-1:1)
switch resu._type{indi}
case "char"
resu._data{indi} = feval (func, char (A._data{indi}), B);
otherwise
resu._data{indi} = feval (func, A._data{indi}, B);
endswitch
endfor
resu._rep = A._rep;
else
if (itercol)
if (whole(2))
for indi = (resu._cnt(2):-1:1)
switch resu._type{indi}
case "char"
unfolded = char (A._data{indi}(:, A._rep{indi}));
otherwise
unfolded = A._data{indi}(:, A._rep{indi});
endswitch
resu._data{indi} = squeeze (feval (func, unfolded, B));
resu._rep{indi} = 1:size (resu._data{indi}, 2);
endfor
else
for indi = (resu._cnt(2):-1:1)
switch resu._type{indi}
case "char"
unfolded = char (A._data{indi}(:, A._rep{indi}));
otherwise
unfolded = A._data{indi}(:, A._rep{indi});
endswitch
resu._data{indi} = feval (func, unfolded, \
squeeze (B(:, indi, :)));
resu._rep{indi} = 1:size (resu._data{indi}, 2);
endfor
endif
else
dummy = df_whole(A);
if (whole(1))
for indi = (columns (B):-1:1)
resu._data{indi} = squeeze (feval(func, dummy, B(:, indi, :)));
resu._rep{indi} = 1:size (resu._data{indi}, 2);
endfor
else
if (~whole(2))
for indi = (resu._cnt(1):-1:1)
resu._data{indi} = squeeze (feval (func, dummy(indi, :, :), \
B(:, indi, :)));
resu._rep{indi} = 1:size (resu._data{indi}, 2);
endfor
else
for indi = (resu._cnt(1):-1:1) %# in place computation
dummy(indi, :, :) = feval (func, dummy(indi, :, :), B);
endfor
for indi = (resu._cnt(2):-1:1) %# store column-wise
resu._data{indi} = squeeze (dummy(:, indi, :));
resu._rep{indi} = 1:size (resu._data{indi}, 2);
endfor
endif
endif
%# verify that sizes match, this is required for "\"
resu._cnt(2) = length (resu._data);
resu._cnt(1) = max (cellfun ('size', resu._data, 1));
if (length (resu._ridx) < resu._cnt(1)),
if (1 == length (resu._ridx))
resu._ridx(end+1:resu._cnt(1), 1) = resu._ridx(1);
else
resu._ridx(end+1:resu._cnt(1), 1) = NA;
endif
endif
if (length (resu._name{2}) < resu._cnt(2)),
if (1 == length (resu._name{2})),
resu._name{2}(end+1:resu._cnt(2), 1) = resu._name{2};
resu._over{2}(end+1:resu._cnt(2), 1) = resu._over{2};
else
resu._name{2}(end+1:resu._cnt(2), 1) = '_';
resu._over{2}(end+1:resu._cnt(2), 1) = true;
endif
endif
endif
endif
endif
resu._type = cellfun (@class, resu._data, "UniformOutput", false);
resu = df_thirddim (resu);
endfunction