Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Why is sparse * 3D full array allowed?
Date: Fri, 16 Oct 2009 08:07:02 +0000 (UTC)
Organization: Universit&#228;t Heidelberg
Lines: 19
Message-ID: <hb99j6$rje$1@fred.mathworks.com>
References: <hb948j$28q$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1255680422 28270 172.30.248.35 (16 Oct 2009 08:07:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 16 Oct 2009 08:07:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869888
Xref: news.mathworks.com comp.soft-sys.matlab:577750


Dear James!

> I noticed that if a sparse matrix is multiplied by a full nD matrix that can be squeezed to a 2D matrix, the multiplication takes place by doing the squeeze. But the same calculation for a full matrix is not allowed. Anybody know why this behavior should be different for sparse vs full?  Seems like it should work the same both ways, either both allow it or both not allow it. 

There is no reason why a multiplication of SPARSE matrices should accept not matching dimensions! This is mathematically wrong.
  x = rand(2, 3); y = rand(3, 1, 4);
  x * y  => error
  sparse(x) * y  => works
One could argument, that this behaviour should be valid for FULL arrays also.
But as Bruno pointed out, even worse:
  x = rand(2, 3); y = rand(3, 2, 2);
  sparse(x) * y  => works: ans = full [3 x 4] array
  sparse(x) * sparse(y) => error

It looks like The MathWorks uses mxGetM and mxGetN to check the dimensions for the multiplication of SPARSE and FULL arrays. For FULL*FULL and SPARSE*SPARSE the complete check of mxGetDimensions is performed.

This bug was present since at least Matlab 5.3 (I've removed Matlab 4 for unknown reasons). In my opinion it is worth to break with the backward compatibility here...

Jan