<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263272</link>
    <title>MATLAB Central Newsreader - Why is sparse * 3D full array allowed?</title>
    <description>Feed for thread: Why is sparse * 3D full array allowed?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 16 Oct 2009 06:36:03 -0400</pubDate>
      <title>Why is sparse * 3D full array allowed?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263272#687444</link>
      <author>James Tursa</author>
      <description>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.,&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; A = sprand(10,10,.1)&lt;br&gt;
A =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;(7,1)       0.5940&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;:               :&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;(6,10)      0.6831&lt;br&gt;
&amp;gt;&amp;gt; B = rand(10,1,3)&lt;br&gt;
B(:,:,1) =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.9147&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;:&lt;br&gt;
B(:,:,2) =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.1615&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;:&lt;br&gt;
B(:,:,3) =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.4279&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;:&lt;br&gt;
&amp;gt;&amp;gt; A*B&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.1185    0.0032    0.0492&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.1139    0.0031    0.0473&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0         0         0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.0338    0.0261    0.0550&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.9378    0.7062    0.5906&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.2056    0.4609    0.1696&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.5433    0.0959    0.2542&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.2788    0.2448    0.1858&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0         0         0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.4363    0.0469    0.2382&lt;br&gt;
&amp;gt;&amp;gt; full(A)*B&lt;br&gt;
??? Error using ==&amp;gt; mtimes&lt;br&gt;
Input arguments must be 2-D.&lt;br&gt;
&amp;gt;&amp;gt; full(A)*squeeze(B)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.1185    0.0032    0.0492&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.1139    0.0031    0.0473&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0         0         0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.0338    0.0261    0.0550&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.9378    0.7062    0.5906&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.2056    0.4609    0.1696&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.5433    0.0959    0.2542&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.2788    0.2448    0.1858&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0         0         0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.4363    0.0469    0.2382&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Fri, 16 Oct 2009 07:18:01 -0400</pubDate>
      <title>Re: Why is sparse * 3D full array allowed?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263272#687453</link>
      <author>Bruno Luong</author>
      <description>&quot;James Tursa&quot; &amp;lt;aclassyguy_with_a_k_not_a_c@hotmail.com&amp;gt; wrote in message &amp;lt;hb948j$28q$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; 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. &lt;br&gt;
&lt;br&gt;
To be more precise, the right (multi-dimensional) array F will be reshaped as&lt;br&gt;
F = reshape(F, size(F,1), []) &lt;br&gt;
before the product S*F takes place.&lt;br&gt;
&lt;br&gt;
The same reshaped is carried out with left product full/sparse. F*S is done as following:&lt;br&gt;
&lt;br&gt;
F = reshape(F, size(F,1), [])  * S&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 16 Oct 2009 07:24:04 -0400</pubDate>
      <title>Re: Why is sparse * 3D full array allowed?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263272#687456</link>
      <author>Bruno Luong</author>
      <description>Should read:&lt;br&gt;
&lt;br&gt;
&amp;gt; P = reshape(F, size(F,1), [])  * S&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 16 Oct 2009 08:07:02 -0400</pubDate>
      <title>Re: Why is sparse * 3D full array allowed?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263272#687470</link>
      <author>Jan Simon</author>
      <description>Dear James!&lt;br&gt;
&lt;br&gt;
&amp;gt; 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. &lt;br&gt;
&lt;br&gt;
There is no reason why a multiplication of SPARSE matrices should accept not matching dimensions! This is mathematically wrong.&lt;br&gt;
&amp;nbsp;&amp;nbsp;x = rand(2, 3); y = rand(3, 1, 4);&lt;br&gt;
&amp;nbsp;&amp;nbsp;x * y  =&amp;gt; error&lt;br&gt;
&amp;nbsp;&amp;nbsp;sparse(x) * y  =&amp;gt; works&lt;br&gt;
One could argument, that this behaviour should be valid for FULL arrays also.&lt;br&gt;
But as Bruno pointed out, even worse:&lt;br&gt;
&amp;nbsp;&amp;nbsp;x = rand(2, 3); y = rand(3, 2, 2);&lt;br&gt;
&amp;nbsp;&amp;nbsp;sparse(x) * y  =&amp;gt; works: ans = full [3 x 4] array&lt;br&gt;
&amp;nbsp;&amp;nbsp;sparse(x) * sparse(y) =&amp;gt; error&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
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...&lt;br&gt;
&lt;br&gt;
Jan</description>
    </item>
    <item>
      <pubDate>Fri, 16 Oct 2009 13:52:40 -0400</pubDate>
      <title>Re: Why is sparse * 3D full array allowed?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263272#687557</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;Jan Simon&quot; &amp;lt;matlab.THIS_YEAR@nMINUSsimon.de&amp;gt; wrote in message &lt;br&gt;
news:hb99j6$rje$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Dear James!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; I noticed that if a sparse matrix is multiplied by a full nD matrix that &lt;br&gt;
&amp;gt;&amp;gt; can be squeezed to a 2D matrix, the multiplication takes place by doing &lt;br&gt;
&amp;gt;&amp;gt; the squeeze. But the same calculation for a full matrix is not allowed. &lt;br&gt;
&amp;gt;&amp;gt; Anybody know why this behavior should be different for sparse vs full? &lt;br&gt;
&amp;gt;&amp;gt; Seems like it should work the same both ways, either both allow it or &lt;br&gt;
&amp;gt;&amp;gt; both not allow it.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; There is no reason why a multiplication of SPARSE matrices should accept &lt;br&gt;
&amp;gt; not matching dimensions! This is mathematically wrong.&lt;br&gt;
&amp;gt;  x = rand(2, 3); y = rand(3, 1, 4);&lt;br&gt;
&amp;gt;  x * y  =&amp;gt; error&lt;br&gt;
&amp;gt;  sparse(x) * y  =&amp;gt; works&lt;br&gt;
&amp;gt; One could argument, that this behaviour should be valid for FULL arrays &lt;br&gt;
&amp;gt; also.&lt;br&gt;
&amp;gt; But as Bruno pointed out, even worse:&lt;br&gt;
&amp;gt;  x = rand(2, 3); y = rand(3, 2, 2);&lt;br&gt;
&amp;gt;  sparse(x) * y  =&amp;gt; works: ans = full [3 x 4] array&lt;br&gt;
&amp;gt;  sparse(x) * sparse(y) =&amp;gt; error&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; It looks like The MathWorks uses mxGetM and mxGetN to check the dimensions &lt;br&gt;
&amp;gt; for the multiplication of SPARSE and FULL arrays. For FULL*FULL and &lt;br&gt;
&amp;gt; SPARSE*SPARSE the complete check of mxGetDimensions is performed.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; This bug was present since at least Matlab 5.3 (I've removed Matlab 4 for &lt;br&gt;
&amp;gt; unknown reasons). In my opinion it is worth to break with the backward &lt;br&gt;
&amp;gt; compatibility here...&lt;br&gt;
&lt;br&gt;
Yes, the fact that attempting to perform matrix multiplication with a full &lt;br&gt;
n-D array and a sparse matrix does not error is a bug.  Thanks to James for &lt;br&gt;
calling this out.  I've reported it to our development staff.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com&lt;br&gt;
comp.soft-sys.matlab (CSSM) FAQ: &lt;a href=&quot;http://matlabwiki.mathworks.com/MATLAB_FAQ&quot;&gt;http://matlabwiki.mathworks.com/MATLAB_FAQ&lt;/a&gt; </description>
    </item>
    <item>
      <pubDate>Fri, 16 Oct 2009 13:56:18 -0400</pubDate>
      <title>Re: Why is sparse * 3D full array allowed?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263272#687558</link>
      <author>James Tursa</author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;hb96n9$5og$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;James Tursa&quot; &amp;lt;aclassyguy_with_a_k_not_a_c@hotmail.com&amp;gt; wrote in message &amp;lt;hb948j$28q$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; 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. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; To be more precise, the right (multi-dimensional) array F will be reshaped as&lt;br&gt;
&amp;gt; F = reshape(F, size(F,1), []) &lt;br&gt;
&amp;gt; before the product S*F takes place.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The same reshaped is carried out with left product full/sparse. F*S is done as following:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; F = reshape(F, size(F,1), [])  * S&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
Thanks for the correction. It looked like a squeeze last night but I now realize it was just my screen display wrapping around.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
  </channel>
</rss>

