bugGNU Octave - Bugs: bug #67724, Octave should have "rms"...

 
 

bug #67724: Octave should have "rms" function in its core

Submitter:  Dmitri A. Sergatskov <dasergatskov>
Submitted:  Thu 20 Nov 2025 05:29:15 PM UTC
   
 
Category:  Octave Function Severity:  1 - Wish
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Ready For Test Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  11.1.0 (current default)
* Mandatory Fields

Post a Comment

Add a New Comment Rich Markup
   

Discussion

Jump to the original submission

Wed 10 Dec 2025 02:58:56 PM UTC, comment #13: 

With the latest patch for bug #67714, this bug is now ready for test.

Arun Giridhar <arungiridhar>
Group Member
Mon 01 Dec 2025 02:23:58 PM UTC, comment #12: 

@Andreas: It should be even simpler to add rms() to Octave.  I updated the function meansq.m to include support for nanflag and vecdim.  The function rms.m should now just be

y = sqrt (meansq (x, varargin{:}));

There is a performance advantage to avoiding isnan unless you absolutely need it (only in the case of "omitnan" option).  As an example,

octave:1> x = rand (1e6,10);
octave:2> tic; y = meansq (x); toc
Elapsed time is 0.00643802 seconds.
octave:3> tic; y2 = meansq2 (x); toc
Elapsed time is 0.0503051 seconds.

The code I wrote for Octave core is 7.8X faster than the straightforward implementation I benchmarked in meansq2 shown below.

function y = meansq2 (x, varargin)
  y = sumsq (x, varargin{:}) ./ sum (! isnan (x), varargin{:});
endfunction



Rik <rik5>
Group administrator
Mon 01 Dec 2025 01:31:11 PM UTC, comment #11: 

Since you are going to edit M functions in statistics, could you tuck this function in?

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Mon 01 Dec 2025 01:12:06 PM UTC, comment #10: 

The patch in file #57876 still parses optional arguments, instead of passing them directly to sumsq. Also the help docstring should be adapted to be consistent with recent changes appleid to the docstrings of other statistics functions that already support nanflag and vedim.

Since nanflag support has been fixed in compliled core functions, the code of the rms function should literally be:

function y = rms (x, varargin)

  if (nargin < 1)
    print_usage ();
  endif

  y = sqrt (sumsq (x, varargin{:}) ./ sum (! isnan (x), varargin{:}));

endfunction



Andreas Bertsatos <pr0m1th3as>
Group Member
Fri 28 Nov 2025 11:23:01 PM UTC, comment #9: 

I’ve attached an updated patch which replaces my earlier rms.patch.

This version:

relies on core sumsq nanflag support instead of manual branching.
keeps the fast path for the default case.
validates dimension arguments more carefully.

All %!test blocks in rms.m pass on latest dev build.

Please consider this as a replacement for file #57861.

(file #57876)

jayant <jayant>
Wed 26 Nov 2025 10:18:52 PM UTC, comment #8: 

Just to inform that given the latest changes in dev branch, there is no need for branching the nanflag option.  It is now faster to pass it to core Octave sumsq along with any other input (dim, dimvec, "all").  It also allow for simplified code.

Andreas Bertsatos <pr0m1th3as>
Group Member
Mon 24 Nov 2025 11:47:19 PM UTC, comment #7: 

Hi Octave developers,

I have implemented a core rms function based on the discussion in this bug.

I’ve attached a Mercurial-formatted patch (rms.patch) containing:

rms.m added under scripts/statistics/

Support for scalar dim, vecdim, and the 'omitnan' nanflag

Efficient default path using sumsq(x, dim) without unnecessary isnan calls .

Manual 'omitnan' handling (zeroing NaNs for the numerator and counting non-NaN entries for the denominator), making the implementation compatible with builds where sumsq does not accept a nanflag .

Inline %!assert tests for numeric, complex, dimension, and omitnan behavior .

Texinfo documentation

This follows Rik’s recommendation in comment #3 to avoid nested functions and use a fast code path for the common case, while remaining compatible with the sumsq behavior mentioned in comment #6.

(file #57861)

jayant <jayant>
Mon 24 Nov 2025 09:49:21 AM UTC, comment #6: 

I have some updated code for meansq, but it testing it I ran into a problem with sumsq which I reported on Discourse.  I will probably still get this code working and check it in as a reference for how to possible proceed with rms.

Rik <rik5>
Group administrator
Mon 24 Nov 2025 07:46:30 AM UTC, comment #5: 

The overhead of input validation is a fair point.  The only argument that forces the longer code path with isnan is if 'omitnan' is present.  The worst case input is if varargin contains 3 valid strings.  Benchmark is below.


octave:1> args = {"all", "default", "omitnan"}
args =
{
  [1,1] = all
  [1,2] = default
  [1,3] = omitnan
}

octave:2> tic; idx = strcmpi (args, 'omitnan'); toc
Elapsed time is 3.19481e-05 seconds.


Adding in validation results in ~55 microseconds run time versus 8 milliseconds run time, or 145X faster.  Even with a call to any() and the cost of an if statement it is still probably 100X faster.

Rik <rik5>
Group administrator
Sun 23 Nov 2025 09:14:19 PM UTC, comment #4: 

Is the overhead of parsing optional input arguments inside the rms function accounted for?

I agree that we should avoid the overhead of nested functions.

Andreas Bertsatos <pr0m1th3as>
Group Member
Sun 23 Nov 2025 01:22:53 PM UTC, comment #3: 

Octave doesn't have to do things internally in the same way as Matlab as long as the external results are the same.  Since Octave has a function meansq it might make sense to use it.

However, there is overhead in the interpreter to calling any function. 
Since meansq is effectively just one-line, it would be more efficient to have that one line in rms.m and then take the square root of it.

@Andreas: The proposed code in comment #1 looks fine for the "omitnan" case, but for the default case of "includenan" it is expensive to call isnan which is unnecessary.

Here is a benchmark


octave:35> x = rand (1e6, 10);
octave:36> tic; tf = isnan (x); toc
Elapsed time is 0.00825095 seconds.
octave:37> tic; sz = size (x); toc
Elapsed time is 2.40803e-05 seconds.


For the ordinary case it would be better to call


y = sqrt (sumsq (x, varargin{:}) ./ size (x, dim));


where dim can be a scalar or vector (supports VECDIM).


Rik <rik5>
Group administrator
Sun 23 Nov 2025 12:10:12 PM UTC, comment #2: 

It looks to me `meansq()` is not a Matlab function (at least I could not find it in on-line help. So we should derine `rms` directly from `sumsq`.

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Sun 23 Nov 2025 12:08:53 AM UTC, comment #1: 

Now the sumsq supports vecdim and nanflag, it should be fairly easy to adapt meansq to support these options and by extension rms will work out of the box. Most work needed involves input validation, tests and documentation.

The working line in meansq should be

function y = meansq (x, varargin)
  y = sumsq (x, varargin{:}) ./ sum (! isnan (x), varargin{:});
endfunction


Andreas Bertsatos <pr0m1th3as>
Group Member
Thu 20 Nov 2025 05:29:15 PM UTC, original submission:  

Octave has "rms()" via "signal" package, but since matlab has this function in its core, so should Octave.

The "rms.m" from the "signal" is attached (file rms.m).

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>

 

Attached Files

Attached Files
file #57861:  rms.patch added by jayant (4.3KiB - application/octet-stream)
file #57827:  rms.m added by dasergatskov (1.8KiB - text/x-objcsrc)

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

Attach Files:
   
   
Comment:
   

 

Dependencies

This item does not depend on any other items.

No items depend on this one.

 

Mail Notification Carbon-Copy List

Carbon-Copy List
  • -email is unavailable- added by arungiridhar (Posted a comment)
  • -email is unavailable- added by jayant (Updated the item)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by pr0m1th3as (Posted a comment)
  • -email is unavailable- added by philipnienhuis (Updated the item)
  • -email is unavailable- added by dasergatskov (Submitted the item)
  •  

    Votes

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

    History

    Follow 8 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2025-12-10 arungiridhar StatusIn Progress Ready For Test
    2025-12-01 rik5 Severity3 - Normal 1 - Wish
        StatusNone In Progress
        Planned ReleaseNone 11.1.0 (current default)
    2025-11-28 jayant Attached File- Added 0001-scripts-statistics-simplify-rms-using-core-sumsq-nan.patch, #57876
    2025-11-24 jayant Attached File- Added rms.patch, #57861
    2025-11-20 philipnienhuis SummaryOctave should have &quot;rms&quot; dunction in its core Octave should have "rms" function in its core
    2025-11-20 dasergatskov Attached File- Added rms.m, #57827

    Back to the top

    Powered by Savane 3.16-a7ba.
    Corresponding source code