bugGNU Octave - Bugs: bug #63291, var fails to preserve sparseness


bug #63291: var fails to preserve sparseness

Submitter:  Nicholas Jankowski <nrjank>
Submitted:  Sun 30 Oct 2022 02:59:35 AM UTC
Category:  Octave Function Severity:  2 - Minor
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Fixed Assigned to:  None
Originator Name:  Nicholas Jankowski Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  9.1.0 Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup

Wed 12 Apr 2023 05:23:26 PM UTC, comment #4: 

Verified that sparseness is now preserved.

This problem has been fixed in the development version. The fix
will be available in the next major software release. Thank you
for your bug report.

Rik <rik5>
Group administrator
Wed 01 Mar 2023 06:41:10 PM UTC, comment #3: 

patch #10314 was pushed to default (ported from statistics to core) that includes matlab compatible var that also avoids the error on sparse and diagonal inputs but also preserves sparse outputs.  This would satisfy the other remaining item on this report. Marking as Ready for Test.

Nicholas Jankowski <nrjank>
Group Member
Mon 28 Nov 2022 03:02:14 PM UTC, comment #2: 

after a month with no errors, I'm going to retitle and re-status this to focus the report down to the remaining issue regarding var not preserving sparseness.

Nicholas Jankowski <nrjank>
Group Member
Mon 31 Oct 2022 03:43:04 AM UTC, comment #1: 

for the error in item 2, I made a patch that uses the same bsxfun fix that was used for center.m.  this does have the side effect of not preserving sparseness, but it's consistent with what has been done elsewhere to work around the broadcasting problem.  since #2 is an error in a frequently used function and it's a pretty simple and narrowly focused fix, after checking tests i've pushed it with a few BISTs to stable as:


Nicholas Jankowski <nrjank>
Group Member
Sun 30 Oct 2022 02:59:35 AM UTC, original submission:  

noticed two things about var (shared by std since that's mainly a passthrough) and sparse matrices -

1 - because it calls center for the simpler, scalar weighting path, the output for a sparse input is always a full matrix. This is because center was rewritten to use bsxfun until sparse/diagonal arrays stop throwing errors at automatic broadcasting (bug #51249), and it uses functions in bsxfun that always return full (bug #63281). Matlab preserves sparseness in both outputs from std and var regardless of size.


[v,m]=var (sparse([1,2,3]))
v = 1
m = Compressed Column Sparse (rows = 1, cols = 1, nnz = 1 [100%])

  (1, 1) -> 2

2 - the path with vector weights uses automatic broadcasting, and will throw an error for sparse or diag inputs:

[v,m]=var (sparse([1,2,3;4,5,6]),1:2)
error: product: nonconformant arguments (op1 is 2x1, op2 is 2x3)
error: called from
    var at line 227 column 10

#2 should be able to be solved with a switch to bsxfun like was done in center to avoid the error.

#1 as noted in bug #63281, sparseness could be preserved in both var and center by replacing the current bsxfun call with an anonymous function, and using a try/catch block to mitigate the performance hit.

Nicholas Jankowski <nrjank>
Group Member


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

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by nrjank (Submitted the item)

    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.


    Follow 8 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-04-12 rik5 StatusReady For Test Fixed
        Open/ClosedOpen Closed
        Fixed ReleaseNone 9.1.0
    2023-03-01 nrjank StatusConfirmed Ready For Test
    2022-11-28 nrjank Item GroupUnexpected Error or Warning Incorrect Result
        StatusReady For Test Confirmed
        Summaryvar sparse matrix error with vector weighting, and fails to preserve sparseness on scalar weighting var fails to preserve sparseness
    2022-10-31 nrjank StatusNone Ready For Test

    Back to the top

    Powered by Savane 3.13-0159.
    Corresponding source code