{"id":91,"date":"2007-05-16T08:10:40","date_gmt":"2007-05-16T13:10:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/05\/16\/purpose-of-inv\/"},"modified":"2007-05-16T08:11:53","modified_gmt":"2007-05-16T13:11:53","slug":"purpose-of-inv","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/05\/16\/purpose-of-inv\/","title":{"rendered":"Purpose of inv"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I have received comments from several savvy customers suggesting that we remove the function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/inv.html\"><tt>inv<\/tt><\/a> from MATLAB.  The reasons are because people who don't know enough numerical analysis will use it blindly and incorrectly\r\n         and often get a poor quality answer unnecessarily.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Reasons to Keep inv<\/a><\/li>\r\n         <li><a href=\"#2\">Reasons to Not Use or Remove inv<\/a><\/li>\r\n         <li><a href=\"#9\">Your Thoughts?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Reasons to Keep inv<a name=\"1\"><\/a><\/h3>\r\n   <p>There are several reasons why we do keep the function <tt>inv<\/tt>. These reasons are:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>for pedagogical and teaching use.<\/li>\r\n         <li>historical (we'd create an incompatibility).<\/li>\r\n         <li>meeting expectations.  This is somewhat similar to the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/factorial.html\"><tt>factorial<\/tt><\/a>, function; users expected it to be there even though they could use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/gamma.html\"><tt>gamma<\/tt><\/a> instead).\r\n         <\/li>\r\n         <li>for certain computations.  An example is when you need the covariance of least squares estimates.  It's hard to accomplish\r\n            that without <tt>inv<\/tt>.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Reasons to Not Use or Remove inv<a name=\"2\"><\/a><\/h3>\r\n   <p>There are good reasons to <b>not<\/b> use <tt>inv<\/tt> however.  The main one is\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>numerical considerations.<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Here is the second paragraph of the description for <tt>inv<\/tt> in the documentation:\r\n   <\/p>\r\n   <p><i>In practice, it is seldom necessary to form the explicit inverse of a matrix. A frequent misuse of inv arises when solving\r\n         the system of linear equations.<\/i><\/p>\r\n   <p><i>One way to solve this is with <tt>x = inv(A)*b<\/tt>. A better way, from both an execution time and numerical accuracy standpoint, is to use the matrix division operator <tt>x = A\\b<\/tt>. This produces the solution using Gaussian elimination, without forming the inverse. See <tt>\\<\/tt> and <tt>\/<\/tt> for further information.<\/i><\/p>\r\n   <p>According to Cleve, the best way to compute <tt>inv(X'*X)<\/tt> is to avoid computing X'*X, and use the economy-sized QR factorization of <tt>X<\/tt> instead.\r\n   <\/p><pre>  [Q,R] = qr(X,0)<\/pre><pre>  S = inv(R)<\/pre><pre>  S*S'<\/pre><p>To see an extreme example, try<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">d = 1.e-10\r\nX = [1 1; d 0; 0 d]<\/pre><pre style=\"font-style:oblique\">d =\r\n  1.0000e-010\r\nX =\r\n    1.0000    1.0000\r\n    0.0000         0\r\n         0    0.0000\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">inv(X'*X)<\/pre><pre style=\"font-style:oblique\">Warning: Matrix is singular to working precision.\r\nans =\r\n   Inf   Inf\r\n   Inf   Inf\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[Q,R] = qr(X,0)\r\nS = inv(R)\r\nS*S'<\/pre><pre style=\"font-style:oblique\">Q =\r\n   -1.0000    0.0000\r\n   -0.0000   -0.7071\r\n         0    0.7071\r\nR =\r\n   -1.0000   -1.0000\r\n         0    0.0000\r\nS =\r\n  1.0e+009 *\r\n   -0.0000   -7.0711\r\n         0    7.0711\r\nans =\r\n  1.0e+019 *\r\n    5.0000   -5.0000\r\n   -5.0000    5.0000\r\n<\/pre><p>The computed  <tt>X' * X<\/tt>  is exactly singular and does not have an inverse, but <tt>R<\/tt> does have an inverse.  We're still using the <tt>inv<\/tt> function, but on a nonsingular matrix.\r\n   <\/p>\r\n   <h3>Your Thoughts?<a name=\"9\"><\/a><\/h3>\r\n   <p>So, we have the function <tt>inv<\/tt> that is ripe for misuse.  What's the best way we can steer users to better solutions?  Please post your thoughts <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=91#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_ceb325babfe841d1bfadd2b4a85aea2a() {\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='ceb325babfe841d1bfadd2b4a85aea2a ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' ceb325babfe841d1bfadd2b4a85aea2a';\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        author = 'Loren Shure';\r\n        copyright = 'Copyright 2007 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 author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\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      \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_ceb325babfe841d1bfadd2b4a85aea2a()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.4<br><\/p>\r\n<\/div>\r\n<!--\r\nceb325babfe841d1bfadd2b4a85aea2a ##### SOURCE BEGIN #####\r\n%% Purpose of inv\r\n% I have received comments from several savvy customers suggesting that we\r\n% remove the function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/inv.html |inv|>\r\n% from MATLAB.  The reasons are because people who don't know enough\r\n% numerical analysis will use it blindly and incorrectly and often get a\r\n% poor quality answer unnecessarily.\r\n%% Reasons to Keep inv\r\n% There are several reasons why we do keep the function |inv|.\r\n% These reasons are:\r\n%\r\n% * for pedagogical and teaching use.\r\n% * historical (we'd create an incompatibility).\r\n% * meeting expectations.  This is somewhat similar to the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/factorial.html |factorial|>,\r\n% function; users expected it to be there even though they could use \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/gamma.html\r\n% |gamma|> instead).\r\n% * for certain computations.  An example is when you need the covariance\r\n% of least squares estimates.  It's hard to accomplish that without |inv|.\r\n%\r\n%% Reasons to Not Use or Remove inv\r\n% There are good reasons to *not* use |inv| however.  The main one is\r\n%\r\n% * numerical considerations.\r\n%\r\n% Here is the second paragraph of the description\r\n% for |inv| in the documentation:\r\n%%\r\n% _In practice, it is seldom necessary to form the explicit inverse of\r\n% a matrix. A frequent misuse of inv arises when solving\r\n% the system of linear equations._\r\n%% \r\n% _One way to solve this is with |x = inv(A)*b|.\r\n% A better way, from both an execution time and numerical accuracy standpoint,\r\n% is to use the matrix division operator |x = A\\b|.\r\n% This produces the solution using Gaussian elimination, without forming the\r\n% inverse. See |\\| and |\/| for further information._\r\n\r\n%%\r\n% According to Cleve, the best way to compute |inv(X'*X)| is to avoid computing X'*X,\r\n% and use the economy-sized QR factorization of |X| instead.\r\n% \r\n%    [Q,R] = qr(X,0)\r\n% \r\n%    S = inv(R)\r\n% \r\n%    S*S'\r\n% \r\n% To see an extreme example, try\r\nd = 1.e-10\r\nX = [1 1; d 0; 0 d]\r\n%%\r\ninv(X'*X)\r\n%%\r\n[Q,R] = qr(X,0)\r\nS = inv(R)\r\nS*S'\r\n%% \r\n% The computed  |X' * X|  is exactly singular and does not have an inverse,\r\n% but |R| does have an inverse.  We're still using the |inv| function,\r\n% but on a nonsingular matrix.\r\n%% Your Thoughts?\r\n% So, we have the function |inv| that is ripe for misuse.  What's the best\r\n% way we can steer users to better solutions?  Please post your thoughts\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=91#respond here>.\r\n##### SOURCE END ##### ceb325babfe841d1bfadd2b4a85aea2a\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I have received comments from several savvy customers suggesting that we remove the function inv from MATLAB.  The reasons are because people who don't know enough numerical analysis... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/05\/16\/purpose-of-inv\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,14,10,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/91"}],"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=91"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}