{"id":2084,"date":"2016-11-10T09:57:58","date_gmt":"2016-11-10T14:57:58","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=2084"},"modified":"2016-11-08T08:32:55","modified_gmt":"2016-11-08T13:32:55","slug":"more_thoughts_about_implicit_expansion","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2016\/11\/10\/more_thoughts_about_implicit_expansion\/","title":{"rendered":"More thoughts about implicit expansion"},"content":{"rendered":"<div class=\"content\"><h3>More thoughts about implicit expansion<\/h3><p>Steve Eddins returns with another thoughtful post on implicit expansion.<\/p><p>Several people have expressed some trepidation about the increased possibility of making an undetected error because of the introduction of <a href=\"https:\/\/blogs.mathworks.com\/loren\/2016\/10\/24\/matlab-arithmetic-expands-in-r2016b\/\">implicit expansion in MATLAB R2016b<\/a>.<\/p><p>Usually, people are worried about trying to perform element-wise addition of two same-length vectors, <tt>a<\/tt> and <tt>b<\/tt>, without realizing that one is a row vector and the other is a column vector. MATLAB R2016b will produce a matrix, whereas earlier versions of MATLAB produced an error. The concern is that a user might not realize that the computation wasn't what they intended, or that a user might run out of memory if <tt>a<\/tt> and <tt>b<\/tt> are really long.<\/p><p>People expressing this concern sometimes suggest adding some kind of warning for the case of adding a row vector and a column vector.<\/p><p>This is a legitimate concern, and it is one that was discussed a number of times during the years that implicit expansion was under consideration.<\/p><p>We have a couple of reasons for thinking that this kind of unintended computation won't be a significant problem in practice. First, we don't think it is likely that the unintended computation will go unnoticed. For an interactive computation in the command window, it will be immediately obvious that the computation produced a matrix instead of a vector. For code in a script or function, if subsequent code is expecting an N-element vector, then something will likely error or be noticeably different if it gets an NxN matrix instead. Compared to typing <tt>*<\/tt> when you meant <tt>.*<\/tt>, or <tt>'<\/tt> instead of <tt>.'<\/tt>, an implicit expansion error with vectors is more readily identifiable.<\/p><p>The second reason is that we haven't seen this issue crop up as a significant problem in our internal experience at MathWorks. Internal development builds of MATLAB have used implicit expansion since October 2015. We have seen that people typically get quickly accustomed to the new behavior. They start using it and stop overthinking it.<\/p><p>Also, we don't think that running out of memory will be a big problem. For the past several releases, MATLAB has had a user-settable upper limit on the number of elements in an array. The limit is intended to prevent trying to make an array so large that your computer grinds to a swapping-induced halt. The introduction of this feature significantly influenced our final decision to go ahead with implicit expansion.<\/p><p>The idea of introducing a warning message turns out to be infeasible, for a few reasons. First, there's not really a good way for MATLAB to determine whether or not you meant to get a matrix if you typed <tt>row + col<\/tt>. A pairwise summation of two vectors, which you might think of as an outer sum, is actually one of the common computational patterns that <tt>bsxfun<\/tt> was used for. So any warning message will probably appear at times when the arithmetic operators are being used correctly and as intended.<\/p><p>MATLAB users are understandably not tolerant of code that produces warning messages when used correctly, so anybody writing mathematical code that might do implicit expansion would need to write extra code to save the warning state, suppress the warning message, and then restore the warning state. That's a lot of slow-executing machinery to be wrapping around simple matrix arithmetic.<\/p><p>Also, warning-generating logic severely interferes with our ability to generate super-fast mathematical computations that fully exploit optimized processor instruction sets, instruction pipelining, and multiple cores. Adding warning logic to the implementations of basic MATLAB arithmetic would result in unacceptable slow-downs.<\/p><p>These are some of the same reasons that the MATLAB Math team has actually removed some warning messages in recent years. The best-known example is the old division-by-zero warning that MATLAB no longer produces.<\/p><p>To summarize, although we acknowledge that implicit expansion behavior may cause confusion in some circumstances, we believe based on our experiences that it will not be a big problem. Our observations of the use of <tt>repmat<\/tt> and <tt>bsxfun<\/tt> over the years led us to conclude that, on balance, the benefits of implicit expansion will significantly outweight the disadvantages. And finally, we think that a warning message of some kind is neither feasible nor desirable.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_9c82824aa9d843f390c606b808983f43() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='9c82824aa9d843f390c606b808983f43 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 9c82824aa9d843f390c606b808983f43';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_9c82824aa9d843f390c606b808983f43()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016b<br><\/p><\/div><!--\r\n9c82824aa9d843f390c606b808983f43 ##### SOURCE BEGIN #####\r\n%% More thoughts about implicit expansion\r\n%\r\n% Steve Eddins returns with another thoughtful post on implicit expansion.\r\n%\r\n% Several people have expressed some trepidation about the increased\r\n% possibility of making an undetected error because of the introduction of\r\n% <https:\/\/blogs.mathworks.com\/loren\/2016\/10\/24\/matlab-arithmetic-expands-in-r2016b\/\r\n% implicit expansion in MATLAB R2016b>.\r\n%\r\n% Usually, people are worried about trying to perform element-wise addition\r\n% of two same-length vectors, |a| and |b|, without realizing that one is a\r\n% row vector and the other is a column vector. MATLAB R2016b will produce a\r\n% matrix, whereas earlier versions of MATLAB produced an error. The concern\r\n% is that a user might not realize that the computation wasn't what they\r\n% intended, or that a user might run out of memory if |a| and |b| are\r\n% really long.\r\n%\r\n% People expressing this concern sometimes suggest adding some kind of\r\n% warning for the case of adding a row vector and a column vector.\r\n%\r\n% This is a legitimate concern, and it is one that was discussed a number\r\n% of times during the years that implicit expansion was under\r\n% consideration.\r\n%\r\n% We have a couple of reasons for thinking that this kind of unintended\r\n% computation won't be a significant problem in practice. First, we don't\r\n% think it is likely that the unintended computation will go unnoticed. For\r\n% an interactive computation in the command window, it will be immediately\r\n% obvious that the computation produced a matrix instead of a vector. For\r\n% code in a script or function, if subsequent code is expecting an\r\n% N-element vector, then something will likely error or be noticeably\r\n% different if it gets an NxN matrix instead. Compared to typing |*| when\r\n% you meant |.*|, or |'| instead of |.'|, an implicit expansion error with\r\n% vectors is more readily identifiable.\r\n%\r\n% The second reason is that we haven't seen this issue crop up as a\r\n% significant problem in our internal experience at MathWorks. Internal\r\n% development builds of MATLAB have used implicit expansion since October\r\n% 2015. We have seen that people typically get quickly accustomed to the\r\n% new behavior. They start using it and stop overthinking it.\r\n%\r\n% Also, we don't think that running out of memory will be a big problem.\r\n% For the past several releases, MATLAB has had a user-settable upper limit\r\n% on the number of elements in an array. The limit is intended to prevent\r\n% trying to make an array so large that your computer grinds to a\r\n% swapping-induced halt. The introduction of this feature significantly\r\n% influenced our final decision to go ahead with implicit expansion.\r\n%\r\n% The idea of introducing a warning message turns out to be infeasible, for\r\n% a few reasons. First, there's not really a good way for MATLAB to\r\n% determine whether or not you meant to get a matrix if you typed |row +\r\n% col|. A pairwise summation of two vectors, which you might think of as an\r\n% outer sum, is actually one of the common computational patterns that\r\n% |bsxfun| was used for. So any warning message will probably appear at\r\n% times when the arithmetic operators are being used correctly and as\r\n% intended.\r\n% \r\n% MATLAB users are understandably not tolerant of code that produces\r\n% warning messages when used correctly, so anybody writing mathematical\r\n% code that might do implicit expansion would need to write extra code to\r\n% save the warning state, suppress the warning message, and then restore\r\n% the warning state. That's a lot of slow-executing machinery to be\r\n% wrapping around simple matrix arithmetic.\r\n%\r\n% Also, warning-generating logic severely interferes with our ability to\r\n% generate super-fast mathematical computations that fully exploit\r\n% optimized processor instruction sets, instruction pipelining, and\r\n% multiple cores. Adding warning logic to the implementations of basic\r\n% MATLAB arithmetic would result in unacceptable slow-downs.\r\n%\r\n% These are some of the same reasons that the MATLAB Math team has actually\r\n% removed some warning messages in recent years. The best-known example is\r\n% the old division-by-zero warning that MATLAB no longer produces.\r\n%\r\n% To summarize, although we acknowledge that implicit expansion behavior\r\n% may cause confusion in some circumstances, we believe based on our\r\n% experiences that it will not be a big problem. Our observations of the\r\n% use of |repmat| and |bsxfun| over the years led us to conclude that, on\r\n% balance, the benefits of implicit expansion will significantly outweight\r\n% the disadvantages. And finally, we think that a warning message of some\r\n% kind is neither feasible nor desirable.\r\n##### SOURCE END ##### 9c82824aa9d843f390c606b808983f43\r\n-->","protected":false},"excerpt":{"rendered":"<p>More thoughts about implicit expansionSteve Eddins returns with another thoughtful post on implicit expansion.Several people have expressed some trepidation about the increased possibility of making... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2016\/11\/10\/more_thoughts_about_implicit_expansion\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/2084"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=2084"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/2084\/revisions"}],"predecessor-version":[{"id":2087,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/2084\/revisions\/2087"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=2084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=2084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=2084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}