bugGNU Octave - Bugs: bug #59718, Broadcasting does not work with...

 
 

bug #59718: Broadcasting does not work with sparse matrices

Submitter:  None
Submitted:  Fri 18 Dec 2020 07:46:09 AM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  Originator Email:  -email is unavailable-
Open/Closed:  * Open Release:  * dev
Operating System:  * Any Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 09 Mar 2021 04:34:00 PM UTC, comment #2: 

Changing the Category to "Interpreter" and the Item Group to "Matlab Compatibility" since Matlab does offer broadcasting between sparse and full matrices.

As jwe mentioned, this is probably a big project in the end because the internals of Octave which implement broadcasting need to be overhauled at the same time.

This would be a useful project as we have different ways of implementing broadcasting (through the interpreter or directly through a function like bsxfun) and these different methods mean parallel code to maintain and they exhibit different performance (which is really wacky).

Rik <rik5>
Group administrator
Wed 03 Feb 2021 07:54:35 PM UTC, comment #1: 

I would not define a sparse classdef object.  Instead, this problem needs to be fixed in the Octave internals.  We probably also want to rethink the way that broadcasting is implemented generally.  I believe there are better ways that we could use templates instead of what we currently have.

John W. Eaton <jwe>
Group administrator
Fri 18 Dec 2020 07:46:09 AM UTC, original submission:  

The following has been a bug for over 6 years

bug #41441
bug #55126
bug #55141

q=rand(2);
w=rand(2,1);
q+w;%works fine
sparse(q)+w;%error: mx_el_eq: nonconformant arguments (op1 is 2x2, op2 is 2x1)
sparse(q)+sparse(w);%error: mx_el_eq: nonconformant arguments (op1 is 2x2, op2 is 2x1)
q+sparse(w);%error: mx_el_eq: nonconformant arguments (op1 is 2x2, op2 is 2x1)

The same issue is also present for:
.*
./
.\
.^
==
<=

>=

<

>

!=
|
&

I have attached an incomplete workaround as a proof of concept which could be completed with a few more days work. However,I believe fixing the underlying problem will be a much better solution. I expect to fix the underlying problem the existing sparse class will need to be updated with code which does the same thing as my workaround. Unless the underlying implementation is the worst mess ever written it will probably be quicker to fix it than to finish the attached workaround.

The workaround creates a new class called sparse. The constructor calls the builtin sparse and saves the result into a field. I then overloaded + .* ./ and .\ to perform the operators correctly. The code is almost identical for each operator so a copy and past changing the operator will fix the remaining unimplemented ones.

This workaround then breaks every other built in function. So they all also need to be overridden. I have implemented * to demonstrate this. This task would be much less involved if there was an override for a generic function, something like:

function varargout=fornothingwrongwithexistingsparsefunction(varargin)
  for k=1:nargin()
  if isa(varargin{k},"sparse")
    varargin{k}=varargin{k}.value;
  end
end
varargout=nthargout(1:nargout(),f,varargin{:});
end

I do not know how to get the function f that was called or how to get this function to be called for all functions I have not specifically overridden. See line 100 of the attached code for a working implementation of the above for mtimes.

If overriding everything is the only option, this workaround will require lots of copy and pasting to finish. This task will be difficult to complete without introducing bugs and will break every time a new function is added or renamed. It also introduces unnecessary overhead on all function calls.

Anonymous

 

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

Attach Files:
   
   
Comment:
   

Attached Files
file #50507:  sparse.m added by None (4KiB - text/plain)

 

Depends on the following items: None found

Digest:
   bug dependencies.

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  •  

    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 7 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-12-10 mmuetzel Dependencies- bugs #61651 is dependent
    2021-03-09 rik5 CategoryOctave Function Interpreter
        Item GroupUnexpected Error or Warning Matlab Compatibility
        StatusNone Confirmed
        SummaryWorkaround for operators involving a sparse matrix and broadcasting Broadcasting does not work with sparse matrices
    2021-02-04 jwe Release6.1.0 dev
    2020-12-18 None Attached File- Added sparse.m, #50507

    Back to the top

    Powered by Savane 3.13-4448.
    Corresponding source code