File: df_basecomp.m

package info (click to toggle)
octave-dataframe 1.2.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 784 kB
  • sloc: makefile: 126
file content (206 lines) | stat: -rw-r--r-- 7,012 bytes parent folder | download | duplicates (3)
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
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-2017 Pascal Dupuis <cdemills@gmail.com>
  %%
  %% This file is part of the dataframe package for Octave.
  %%
  %% This package 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.
  %%
  %% This package 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 this package; see the file COPYING.  If not,
  %% see <http://www.gnu.org/licenses/>.
  
  if (1 == length (itercol)) 
    strict = false;
  else
    strict = itercol(2); itercol = itercol(1);
  end

  if (~isa (A, 'dataframe'))
    if (iscell (A)) A = dataframe (A); end
  end
  if (~isa (B, 'dataframe'))
    if (iscell (B)) B = dataframe (B); end
  end

  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)]);
        end
      else
        Csize = size (A)(1:2);
      end
    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));
        end
        Csize = [size(A, 2) size(B, 2)];
      end
    otherwise
      %# if strict is set, B may not be non-scalar vs scalar
      if (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));
          end
        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));
          end
          Csize = [size(A, 1) size(B, 2)];
        end
      end
      if (~(isscalar (A) || isscalar (B)))
      %# can it be broadcasted ?
        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)]);
          end
        else
          Csize = size (A)(1:2);
        end
      end
  end

  if (~(isscalar (A) || isscalar (B)))
    C = [];
    if (isa (A, 'dataframe'))
      if (nargout > 2 && all (Csize == size (A)(1:2)))
        C = df_allmeta (A, Csize);
      end         
      if (isa (B, 'dataframe'))
        if (nargout > 2 && isempty (C) && all (Csize == size (B)(1:2)))
          C = df_allmeta (B, Csize);
        end
        if (strict)
          %# compare indexes if both exist
          if (~isempty (A.x_ridx))
            if (~isempty(B.x_ridx) && itercol)
              if (any (A.x_ridx-B.x_ridx))
                error ('Non compatible indexes');
              end
            end
          else
            if (nargout > 2 && itercol) C.x_ridx = B.x_ridx; end
          end
          
          if (itercol)
            idxB = 1; %# row-row comparison
          else
            idxB = 2; %# row-col comparsion
          end
          
          if (~isempty (A.x_name{1})) 
            if (~isempty (B.x_name{idxB}))
              dummy = ~(strcmp (cellstr (A.x_name{1}), cellstr (B.x_name{idxB}))...
                        | (A.x_over{1}(:)) | (B.x_over{idxB}(:)));
              if (any (dummy))
                if (itercol)
                  error ('Incompatible row names');
                else
                  error ('Incompatible row vs. column names');
                end
              end
              dummy = A.x_over{1} > B.x_over{idxB};
              if (any (dummy))
                C.x_name{1}(dummy) = B.x_name{idxB}(dummy);
                C.x_over{1}(dummy) = B.x_over{idxB}(dummy);
              end
            end
          else
            if (nargout > 2)
              C.x_name{1} = B.x_name{idxB}; C.x_over{1} = B.x_over{idxB};
            end
          end
          
          idxB = 3-idxB;
          
          if (~isempty(A.x_name{2}))
            if (~isempty(B.x_name{idxB}))
              dummy = ~(strcmp (cellstr (A.x_name{2}), cellstr (B.x_name{2}))...
                        | (A.x_over{2}(:)) | (B.x_over{2}(:)));
              if (any (dummy))
                if (itercol)
                  error ('Incompatible column vs row names');
                else
                  error ('Incompatible column names');
                end
              end
              dummy = A.x_over{2} > B.x_over{idxB};
              if (any (dummy))
                C.x_name{2}(dummy) = B.x_name{idxB}(dummy);
                C.x_over{2}(dummy) = B.x_over{idxB}(dummy);
              end
            end
          else
            if (nargout > 2 && ~isempty (B.x_name{idxB}))
              C.x_name{2} = B.x_name{idxB}; C.x_over{2} = B.x_over{idxB}; 
            end
          end
        end

        if (isempty (A.x_src) && nargout > 2 && ~isempty (B.x_src))
          C.x_src = B.x_src;
        end
        if (isempty (A.x_cmt) && nargout > 2 && ~isempty (B.x_cmt))
          C.x_cmt = B.x_cmt;
        end
      else %# B is not a df
        B = dataframe (B, 'colnames', '');
        if (nargout > 2 && isempty (C))
          C = df_allmeta (A, Csize);
        end
      end
    else %# A is not a df
      A = dataframe (A, 'colnames', '');
      if (nargout > 2)
        if (all (Csize == size (B)(1:2)))
          C = df_allmeta (B, Csize);
        else
          C = df_allmeta (B, Csize);
        end         
      end
    end
  else %# both arg are  scalar
    if (nargout > 2)
      if (isa (A, 'dataframe')) 
        C = df_allmeta (A);     
      else
        C = df_allmeta (B); 
      end
    end
  end
  
end