From: <cde...@us...> - 2010-11-26 09:31:58
|
Revision: 7952 http://octave.svn.sourceforge.net/octave/?rev=7952&view=rev Author: cdemills Date: 2010-11-26 09:31:51 +0000 (Fri, 26 Nov 2010) Log Message: ----------- Merging df_rcfunc and df_ccfunc Modified Paths: -------------- trunk/octave-forge/extra/dataframe/inst/@dataframe/eq.m trunk/octave-forge/extra/dataframe/inst/@dataframe/mtimes.m trunk/octave-forge/extra/dataframe/inst/@dataframe/plus.m trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_ccfunc.m trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_name2idx.m trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_rcfunc.m trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m trunk/octave-forge/extra/dataframe/inst/@dataframe/subsref.m Added Paths: ----------- trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_func.m trunk/octave-forge/extra/dataframe/inst/@dataframe/subsindex.m Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/eq.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/eq.m 2010-11-25 23:37:17 UTC (rev 7951) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/eq.m 2010-11-26 09:31:51 UTC (rev 7952) @@ -27,6 +27,6 @@ %# $Id$ %# - resu = df_ccfunc(@eq, A, B); + resu = df_func(@eq, A, B); endfunction Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/mtimes.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/mtimes.m 2010-11-25 23:37:17 UTC (rev 7951) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/mtimes.m 2010-11-26 09:31:51 UTC (rev 7952) @@ -28,7 +28,7 @@ %# try - resu = df_rcfunc(@mtimes, A, B); + resu = df_rcfunc(@mtimes, A, B, true); catch disp(lasterr()); error("Operator * problem for %s vs. %s", class(A), class(B)); Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/plus.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/plus.m 2010-11-25 23:37:17 UTC (rev 7951) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/plus.m 2010-11-26 09:31:51 UTC (rev 7952) @@ -28,7 +28,7 @@ %# try - resu = df_ccfunc(@plus, A, B); + resu = df_func(@plus, A, B); catch disp(lasterr()); error("Operator + problem for %s vs. %s", class(A), class(B)); Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_ccfunc.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_ccfunc.m 2010-11-25 23:37:17 UTC (rev 7951) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_ccfunc.m 2010-11-26 09:31:51 UTC (rev 7952) @@ -1,9 +1,10 @@ -function resu = df_ccfunc(func, A, B, varargin); +function resu = df_ccfunc(func, A, B, whole); - %# function resu = df_ccfunc(func, A, B) + %# function resu = df_ccfunc(func, A, B, whole) %# Implements an column vs column 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. + %# 'whole' indicate if the LHS as to be taken in block, default = false. %% Copyright (C) 2009-2010 Pascal Dupuis <Pas...@uc...> %% @@ -30,20 +31,28 @@ %# [A, B, resu] = df_basecomp(A, B); + if nargin < 4, whole = false; endif if (isa(B, 'dataframe')) if (!isa(A, 'dataframe')), - if (isscalar(A) || ismatrix(A)), + if (isscalar(A) || (ismatrix(A) && whole)), for indi = resu._cnt(2):-1:1, switch resu._type{indi} case "char" - resu._data{indi} = feval(func, A, char(B._data{indi}), \ - varargin{:}); + resu._data{indi} = feval(func, A, char(B._data{indi})); otherwise - resu._data{indi} = feval(func, A, B._data{indi}, \ - varargin{:}); + resu._data{indi} = feval(func, A, B._data{indi}); endswitch endfor + elseif (ismatrix(A)), + 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 error("Function %s not implemented for %s by %s", \ func2str(func), class(A), class(B)); Added: trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_func.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_func.m (rev 0) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_func.m 2010-11-26 09:31:51 UTC (rev 7952) @@ -0,0 +1,103 @@ +function resu = df_func(func, A, B, itercol=true, whole=logical([0 0])); + + %# function resu = df_rcfunc(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-2010 Pascal Dupuis <Pas...@uc...> + %% + %% 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, 59 Temple Place - + %% Suite 330, Boston, MA 02111-1307, USA. + + %# + %# $Id: df_rcfunc.m 7950 2010-11-25 18:07:59Z cdemills $ + %# + + [A, B, resu] = df_basecomp(A, B); + + 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 + 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})); + otherwise + resu._data{indi} = feval(func, A, B._data{indi}); + endswitch + endfor + elseif (itercol && !whole(2)), + 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 + elseif (!whole(2)), + 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 + error("Function %s not implemented for %s by %s", \ + func2str(func), class(A), class(B)); + endif + endif + else + error('To be implemented'); + resu._data = cellfun(@(x, y) feval(func, x, y, varargin{:}), A._data, \ + B._data, "UniformOutput", false); + endif + else + if (isscalar(B) || ismatrix(B)), + resu._data = cellfun(@(x) feval(func, x, B, varargin{:}), A._data, \ + "UniformOutput", false); + else + error("Function %s not implemented for %s by %s", \ + func2str(func), class(A), class(B)); + endif + endif + + resu._type = cellfun(@class, resu._data, "UniformOutput", false); + %# sanity check + dummy = sum(cellfun('size', resu._data, 2)); + if dummy != resu._cnt(2), + resu._cnt(3) = dummy; + endif + +endfunction Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_name2idx.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_name2idx.m 2010-11-25 23:37:17 UTC (rev 7951) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_name2idx.m 2010-11-26 09:31:51 UTC (rev 7952) @@ -51,6 +51,8 @@ switch class(subs) case {"cell" } orig_name = char(subs); + case {"dataframe"} + orig_name = "elements indexed by a dataframe"; otherwise orig_name = num2str(subs); endswitch @@ -111,6 +113,8 @@ elseif isa(subs, 'logical'), idx = 1:length(subs); idx(~subs) = []; + elseif isa(subs, 'dataframe'), + idx = subsindex(subs, 1); else idx = subs; endif Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_rcfunc.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_rcfunc.m 2010-11-25 23:37:17 UTC (rev 7951) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/private/df_rcfunc.m 2010-11-26 09:31:51 UTC (rev 7952) @@ -1,9 +1,10 @@ -function resu = df_rcfunc(func, A, B, varargin); +function resu = df_rcfunc(func, A, B, whole); - %# function resu = df_rcfunc(func, A, B) + %# function resu = df_rcfunc(func, A, B, whole) %# Implements an row vs column 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. + %# 'whole' indicate if the LHS as to be taken in block, default = false. %% Copyright (C) 2009-2010 Pascal Dupuis <Pas...@uc...> %% @@ -30,12 +31,28 @@ %# [A, B, resu] = df_basecomp(A, B); - + if nargin < 4, whole = false; endif + if (isa(B, 'dataframe')) if (!isa(A, 'dataframe')), - if (isscalar(A) || ismatrix(A)), - resu._data = cellfun(@(x) feval(func, A, x, varargin{:}), B._data, \ - "UniformOutput", false); + if (isscalar(A) || (ismatrix(A) && whole)), + 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 + elseif (ismatrix(A)), + 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 error("Function %s not implemented for %s by %s", \ func2str(func), class(A), class(B)); Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m 2010-11-25 23:37:17 UTC (rev 7951) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m 2010-11-26 09:31:51 UTC (rev 7952) @@ -27,17 +27,6 @@ %# $Id$ %# - %# we may use df as index ! - for indi = 1:length(S), - if isa(S(indi).subs, 'cell'), - for indj = 1:length(S(indi).subs), - if isa(S(indi).subs{indj}, 'dataframe'), - S(indi).subs{indj} = horzcat((struct(S(indi).subs{indj}))._data{:}); - endif - endfor - endif - endfor - switch(S(1).type) case '{}' error('Invalid dataframe as cell assignement'); Added: trunk/octave-forge/extra/dataframe/inst/@dataframe/subsindex.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/subsindex.m (rev 0) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/subsindex.m 2010-11-26 09:31:51 UTC (rev 7952) @@ -0,0 +1,46 @@ +function resu = subsindex(df, base) + %# function resu = subsindex(df) + %# This function convert a dataframe to an index + + %% Copyright (C) 2009-2010 Pascal Dupuis <Pas...@uc...> + %% + %% 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, 59 Temple Place - + %% Suite 330, Boston, MA 02111-1307, USA. + + %# + %# $Id$ + %# + + if nargin < 2, + base = -1.0; + else + base = base - 1.0; + endif + + resu = []; + for indi = 1:length(df._data), + %# transform each column to an index + switch(df._type{indi}) + case 'logical' + resu = vertcat(resu, find(df._data{indi}) - base); + otherwise + resu = vertcat(resu, double(df._data{indi}) - base); + endswitch + endfor + +endfunction Property changes on: trunk/octave-forge/extra/dataframe/inst/@dataframe/subsindex.m ___________________________________________________________________ Added: svn:keywords + Id Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/subsref.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/subsref.m 2010-11-25 23:37:17 UTC (rev 7951) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/subsref.m 2010-11-26 09:31:51 UTC (rev 7952) @@ -37,16 +37,6 @@ %# what kind of object should we return ? asked_output_type = ''; asked_output_format = []; - %# we may use df as index ! - for indi = 1:length(S), - if isa(S(indi).subs, 'cell'), - for indj = 1:length(S(indi).subs), - if isa(S(indi).subs{indj}, 'dataframe'), - S(indi).subs{indj} = horzcat((struct(S(indi).subs{indj}))._data{:}); - endif - endfor - endif - endfor if (strcmp(S(1).type, '.')), %# struct access indi = strmatch(S(1).subs, 'as'); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |