Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Why is sparse * 3D full array allowed?
Date: Fri, 16 Oct 2009 09:52:40 -0400
Organization: The MathWorks, Inc.
Lines: 42
Message-ID: <hb9tq3$mrg$1@fred.mathworks.com>
References: <hb948j$28q$1@fred.mathworks.com> <hb99j6$rje$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1255701123 23408 172.31.44.65 (16 Oct 2009 13:52:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 16 Oct 2009 13:52:03 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
X-RFC2646: Format=Flowed; Original
Xref: news.mathworks.com comp.soft-sys.matlab:577838


"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message 
news:hb99j6$rje$1@fred.mathworks.com...
> 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...

Yes, the fact that attempting to perform matrix multiplication with a full 
n-D array and a sparse matrix does not error is a bug.  Thanks to James for 
calling this out.  I've reported it to our development staff.

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ