{"id":2436,"date":"2009-06-26T13:38:18","date_gmt":"2009-06-26T13:38:18","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2009\/06\/26\/dont-let-that-inv-go-past-your-eyes-to-solve-that-system-factorize\/"},"modified":"2018-01-08T15:41:50","modified_gmt":"2018-01-08T20:41:50","slug":"dont-let-that-inv-go-past-your-eyes-to-solve-that-system-factorize","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2009\/06\/26\/dont-let-that-inv-go-past-your-eyes-to-solve-that-system-factorize\/","title":{"rendered":"&#8220;Don&#8217;t let that INV go past your eyes; to solve that system, FACTORIZE!&#8221;"},"content":{"rendered":"<div class=\"content\">\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a><tt>FACTORIZE<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/908\">Tim Davis<\/a>.\r\n\r\nIf you browse through the <a href=\"http:\/\/\">MATLAB Newsgroup<\/a>, you will occasionally find discussions on \"matrix inverse\". In many of those discussions, the ultimate goal of wanting matrix\r\ninversion was to solve a linear system\r\n<pre>   Ax = b<\/pre>\r\n&nbsp;\r\n\r\nTake this simple example:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">A = randn(3, 3)\r\nb = randn(3, 1)<\/pre>\r\n<pre style=\"font-style: oblique;\">A =\r\n      0.52006     -0.79816     -0.71453\r\n    -0.020028       1.0187       1.3514\r\n    -0.034771     -0.13322     -0.22477\r\nb =\r\n     -0.58903\r\n     -0.29375\r\n     -0.84793\r\n<\/pre>\r\nTechnically, you can solve it using the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/inv.html\"><tt>INV<\/tt><\/a> function.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">x1 = inv(A)*b<\/pre>\r\n<pre style=\"font-style: oblique;\">x1 =\r\n      -30.333\r\n      -56.674\r\n       42.054\r\n<\/pre>\r\nBut as the documentation for <tt>inv<\/tt> suggests, there is a better way to solve this problem, both in terms of efficiency as well as accuracy. That is to use the\r\n<a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/mldivide.html\">backslash<\/a> operator:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">x2 = A\\b<\/pre>\r\n<pre style=\"font-style: oblique;\">x2 =\r\n      -30.333\r\n      -56.674\r\n       42.054\r\n<\/pre>\r\nTim takes this one step further and extends the capability with his Factorize object. One of the benefits is that you can\r\n\"reuse\" this efficiency in different conditions:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">c = randn(3, 1);\r\n\r\nF = factorize(A);\r\nx3 = F\\b\r\nx4 = F\\c;<\/pre>\r\n<pre style=\"font-style: oblique;\">x3 =\r\n      -30.333\r\n      -56.674\r\n       42.054\r\n<\/pre>\r\nThis is a simple 3-by-3 example for illustration, but you'll appreciate its power when you're working with larger systems.\r\nI like this entry for several reasons.\r\n<div>\r\n<ul>\r\n\t<li>He uses the new MATLAB Class system (introduced in R2008a). Specifically, he utilizes object-oriented techniques, such as\r\nproperty attributes and abstract classes.<\/li>\r\n\t<li>He includes a very thorough, pedagogical document that explains the uses of the object and the theory behind different techniques for solving linear systems.<\/li>\r\n\t<li>He includes a test suite for testing the accuracy, performance, error-handling, and display methods of the Factorize object.<\/li>\r\n<\/ul>\r\n<\/div>\r\nI recommend looking at this highly-rated entry by Tim.\r\n\r\n<a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2436#respond\"><b>Comments?<\/b><\/a>\r\n\r\n<script>\/\/ <![CDATA[\r\nfunction grabCode_e2acb16731e74bfc8991489ba84a5b69() {\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='e2acb16731e74bfc8991489ba84a5b69 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e2acb16731e74bfc8991489ba84a5b69';\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 = 'Jiro Doke';\r\n        copyright = 'Copyright 2008 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n<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>\r\n\r\n\r\n\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\r\n<a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code\r\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a>\r\n\r\nPublished with MATLAB\u00ae 7.8<\/p>\r\n\r\n<\/div>\r\n<!--\r\ne2acb16731e74bfc8991489ba84a5b69 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007 % Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24119-don-t-let-that-inv-go-past-your-eyes--to-solve-that-system--factorize- % |FACTORIZE|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/908 % Tim Davis>.\r\n%\r\n% If you browse through the\r\n% <http:\/\/ MATLAB Newsgroup>,\r\n% you will occasionally find discussions on \"matrix inverse\". In many of\r\n% those discussions, the ultimate goal of wanting matrix inversion was to\r\n% solve a linear system\r\n%\r\n%     Ax = b\r\n\r\n%%\r\n% Take this simple example:\r\n\r\nA = randn(3, 3)\r\nb = randn(3, 1)\r\n\r\n%%\r\n% Technically, you can solve it using the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/inv.html % |INV|> function.\r\n%\r\n\r\nx1 = inv(A)*b\r\n\r\n%%\r\n% But as the documentation for |inv| suggests, there is a better way to\r\n% solve this problem, both in terms of efficiency as well as accuracy. That\r\n% is to use the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/mldivide.html % backslash> operator:\r\n\r\nx2 = A\\b\r\n\r\n%%\r\n% Tim takes this one step further and extends the capability with his\r\n% Factorize object. One of the benefits is that you can \"reuse\" this\r\n% efficiency in different conditions:\r\n\r\nc = randn(3, 1);\r\n\r\nF = factorize(A);\r\nx3 = F\\b\r\nx4 = F\\c;\r\n\r\n%%\r\n% This is a simple 3-by-3 example for illustration, but you'll appreciate\r\n% its power when you're working with larger systems. I like this entry for\r\n% several reasons.\r\n%\r\n% * He uses the new MATLAB Class system (introduced in R2008a).\r\n% Specifically, he utilizes object-oriented techniques, such as property\r\n% attributes and abstract classes.\r\n% * He includes a very thorough, pedagogical\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fx_files\/24119\/6\/content\/Factorize\/html\/factorize_demo.html % document> that explains the uses of the object and the theory behind\r\n% different techniques for solving linear systems.\r\n% * He includes a test suite for testing the accuracy, performance,\r\n% error-handling, and display methods of the Factorize object.\r\n%\r\n% I recommend looking at this highly-rated entry by Tim.\r\n%\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2436#respond *Comments?*>\r\n\r\n##### SOURCE END ##### e2acb16731e74bfc8991489ba84a5b69\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nJiro's pick this week is FACTORIZE by Tim Davis.\r\n\r\nIf you browse through the MATLAB Newsgroup, you will occasionally find discussions on \"matrix inverse\". In many of those discussions, the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2009\/06\/26\/dont-let-that-inv-go-past-your-eyes-to-solve-that-system-factorize\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2436"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2436"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2436\/revisions"}],"predecessor-version":[{"id":9318,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2436\/revisions\/9318"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}