bugGNU Octave - Bugs: bug #58089, [Feature Request] sum along...

 
 

bug #58089: [Feature Request] sum along multiple dimensions

Submitted by:  Guillaume <gyom>
Submitted on:  Tue 31 Mar 2020 05:00:43 PM UTC  
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Feature Request
Status:  Confirmed Assigned to:  None
Originator Name:  Guillaume Open/Closed:  Open
Release:  dev Operating System:  Any

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

( Jump to the original submission)

Fri 10 Apr 2020 02:03:48 AM UTC, comment #6: 

You can use the octave_value function is_scalar_type() to determine if the argument is a scalar.  This is faster than checking whether the number of elements is 1.

Rik <rik5>
Project Administrator
Thu 09 Apr 2020 10:14:04 PM UTC, comment #5: 

diff -r 7ac8b3188b27 libinterp/corefcn/data.cc
--- a/libinterp/corefcn/data.cc Wed Apr 08 16:03:51 2020 -0400
+++ b/libinterp/corefcn/data.cc Thu Apr 09 23:09:42 2020 +0100
@@ -2953,6 +2953,8 @@
   int dim = -1;
   if (nargin == 2)
     {
+      if (args(1).length () != 1)
+        error ("sum: dimension must be a scalar");
       dim = args(1).int_value () - 1;
       if (dim < 0)
         error ("sum: invalid dimension DIM = %d", dim + 1);

but I guess one could also check whether the input is an integer and apply a similar fix to all of the functions listed in comment #2. I can try and have a look.

Guillaume <gyom>
Thu 09 Apr 2020 09:39:55 PM UTC, comment #4: 

I think improved input validation (! isscalar (dim)) would be a fine idea until this feature is actually implemented.

Rik <rik5>
Project Administrator
Thu 09 Apr 2020 09:28:44 PM UTC, comment #3: 

Thanks Rik. Until this is implemented in Octave, do you think it would be possible to raise an error if ! isscalar (dim) ?

Guillaume <gyom>
Fri 03 Apr 2020 03:31:15 PM UTC, comment #2: 

Changing category to Feature Request.  This is something that Matlab has added relatively recently as you point out.

If you really want to use this functionality in your code in the meantime it wouldn't be that hard to add an m-file to override sum().  Below is an example which is also attached as sum.m

function s = sum (x, dim)

  if (nargin < 1 || nargin > 2)
    print_usage ("sum");
  endif

  if (nargin == 1)
    s = builtin ("sum", x);
  else
    if (! isnumeric (dim) || ! isvector (dim))
      error ("sum: DIM must be a numeric scalar or vector");
    endif
    s = builtin ("sum", x, dim(1));
    for i = 2:numel (dim)
      s = builtin ("sum", s, dim(i));
    endfor
  endif

endfunction

(file #48747)

Rik <rik5>
Project Administrator
Tue 31 Mar 2020 07:51:27 PM UTC, comment #1: 

This syntax was recently introduced in R2018b:
https://blogs.mathworks.com/steve/2019/04/30/changes-to-sum-and-dim/
and applies to "all, any, bounds, max, min, mean, median, mode, prod, std, and var".

Guillaume <gyom>
Tue 31 Mar 2020 05:00:43 PM UTC, original submission:  

In Matlab, the dim parameter for sum can be a vector and not just a scalar, in order to sum across multiple dimensions:
https://www.mathworks.com/help/matlab/ref/sum.html#d120e1199287

Octave only implements the scalar version and silently ignores the other dimensions if they were provided. See this line in libinterp/corefcn/data.cc:

dim = args(1).int_value () - 1;

As a consequence, Octave does this:

octave:7> sum(ones(2,3),[1 2])
ans =
   2   2   2
octave:8> sum(ones(2,3),[1 Inf 2 NaN 0 -eps])
ans =
   2   2   2

while Matlab does:

>> sum(ones(2,3),[1 2])
ans =
     6
>> sum(ones(2,3),[1 Inf 2 NaN 0 -eps])
Error using sum
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.

Guillaume <gyom>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #48747:  sum.m added by rik5 (397B - text/x-matlab)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Updated the item)
  • -email is unavailable- added by gyom (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

     

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2020-04-03 rik5 Attached File- => Added sum.m, #48747
        Item GroupIncorrect Result => Feature Request
        StatusNone => Confirmed
        Summarysum along multiple dimensions => [Feature Request] sum along multiple dimensions

    Back to the top


    Powered by Savane 3.7