Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Why is sparse * 3D full array allowed?

Subject: Why is sparse * 3D full array allowed?

From: James Tursa

Date: 16 Oct, 2009 06:36:03

Message: 1 of 6

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. e.g.,

>> A = sprand(10,10,.1)
A =
   (7,1) 0.5940
     : :
   (6,10) 0.6831
>> B = rand(10,1,3)
B(:,:,1) =
    0.9147
        :
B(:,:,2) =
    0.1615
        :
B(:,:,3) =
    0.4279
        :
>> A*B
ans =
    0.1185 0.0032 0.0492
    0.1139 0.0031 0.0473
         0 0 0
    0.0338 0.0261 0.0550
    0.9378 0.7062 0.5906
    0.2056 0.4609 0.1696
    0.5433 0.0959 0.2542
    0.2788 0.2448 0.1858
         0 0 0
    0.4363 0.0469 0.2382
>> full(A)*B
??? Error using ==> mtimes
Input arguments must be 2-D.
>> full(A)*squeeze(B)
ans =
    0.1185 0.0032 0.0492
    0.1139 0.0031 0.0473
         0 0 0
    0.0338 0.0261 0.0550
    0.9378 0.7062 0.5906
    0.2056 0.4609 0.1696
    0.5433 0.0959 0.2542
    0.2788 0.2448 0.1858
         0 0 0
    0.4363 0.0469 0.2382

James Tursa

Subject: Why is sparse * 3D full array allowed?

From: Bruno Luong

Date: 16 Oct, 2009 07:18:01

Message: 2 of 6

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hb948j$28q$1@fred.mathworks.com>...
> 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.

To be more precise, the right (multi-dimensional) array F will be reshaped as
F = reshape(F, size(F,1), [])
before the product S*F takes place.

The same reshaped is carried out with left product full/sparse. F*S is done as following:

F = reshape(F, size(F,1), []) * S

Bruno

Subject: Why is sparse * 3D full array allowed?

From: Bruno Luong

Date: 16 Oct, 2009 07:24:04

Message: 3 of 6

Should read:

> P = reshape(F, size(F,1), []) * S

Bruno

Subject: Why is sparse * 3D full array allowed?

From: Jan Simon

Date: 16 Oct, 2009 08:07:02

Message: 4 of 6

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

Subject: Why is sparse * 3D full array allowed?

From: Steven Lord

Date: 16 Oct, 2009 13:52:40

Message: 5 of 6


"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

Subject: Why is sparse * 3D full array allowed?

From: James Tursa

Date: 16 Oct, 2009 13:56:18

Message: 6 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hb96n9$5og$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hb948j$28q$1@fred.mathworks.com>...
> > 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.
>
> To be more precise, the right (multi-dimensional) array F will be reshaped as
> F = reshape(F, size(F,1), [])
> before the product S*F takes place.
>
> The same reshaped is carried out with left product full/sparse. F*S is done as following:
>
> F = reshape(F, size(F,1), []) * S
>
> Bruno

Thanks for the correction. It looked like a squeeze last night but I now realize it was just my screen display wrapping around.

James Tursa

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us