{"id":2223,"date":"2011-08-27T00:01:10","date_gmt":"2011-08-26T22:01:10","guid":{"rendered":"http:\/\/djalil.chafai.net\/blog\/?p=2223"},"modified":"2011-08-27T12:55:22","modified_gmt":"2011-08-27T10:55:22","slug":"a-couple-of-fast-algorithms","status":"publish","type":"post","link":"https:\/\/djalil.chafai.net\/blog\/2011\/08\/27\/a-couple-of-fast-algorithms\/","title":{"rendered":"A couple of fast algorithms"},"content":{"rendered":"<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Difference_engine\"><\/a><a href=\"http:\/\/en.wikipedia.org\/wiki\/Difference_engine\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-2243\" title=\"Closeup of the London Science Museum's difference engine.\" src=\"\/blog\/wp-content\/uploads\/2011\/08\/LondonScienceMuseumsReplicaDifferenceEngine1.jpg\" alt=\"Closeup of the London Science Museum's difference engine.\" width=\"417\" height=\"374\" srcset=\"https:\/\/djalil.chafai.net\/blog\/wp-content\/uploads\/2011\/08\/LondonScienceMuseumsReplicaDifferenceEngine1.jpg 417w, https:\/\/djalil.chafai.net\/blog\/wp-content\/uploads\/2011\/08\/LondonScienceMuseumsReplicaDifferenceEngine1-300x269.jpg 300w\" sizes=\"(max-width: 417px) 100vw, 417px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">This post is devoted to a couple of simple fast algorithms.<\/p>\n<p style=\"text-align: justify;\"><strong>The Horner algorithm.<\/strong> The naive way to evaluate at point \\( {z} \\) the polynomial<\/p>\n<p style=\"text-align: center;\">\\[ P(z):=a_nz^n+\\cdots+a_1z+a_0 \\]<\/p>\n<p style=\"text-align: justify;\">is to compute separately \\( {a_nz^n,\\ldots,a_1z,a_0} \\) and to sum up the results. The complexity of this evaluation algorithm is \\( {O(n^2)} \\). It could be more clever to compute recursively the sequence \\( {1,z,z^2,\\ldots,z^n} \\). This idea is behind a very simple \\( {O(n)} \\) algorithm called the <em>Horner algorithm<\/em>:<\/p>\n<p style=\"text-align: center;\">\\[ P(z)=a_0+z(a_1+\\cdots+z(a_{n-1}+za_n)\\cdots). \\]<\/p>\n<p style=\"text-align: justify;\">The <a href=\"http:\/\/en.wikipedia.org\/wiki\/Horner_scheme\">Horner scheme<\/a> can be used for the division of polynomials and for roots finding. It runs nowadays in microprocessors and in numerical softwares. <a href=\"http:\/\/en.wikipedia.org\/wiki\/William_George_Horner\">William George Horner<\/a> (1786 - 1837) was a British mathematician. His algorithm was actually already known at least by the Chinese mathematician <a href=\"http:\/\/en.wikipedia.org\/wiki\/Zhu_Shijie\">Zhu Shijie<\/a> and also by the Italian mathematician <a href=\"http:\/\/en.wikipedia.org\/wiki\/Paolo_Ruffini\">Paolo Ruffini<\/a>.<\/p>\n<p style=\"text-align: justify;\"><strong>The Strassen algorithm.<\/strong> The naive way to compute the multiplication \\( {C:=AB} \\) of a couple of \\( {2\\times 2} \\) matrices \\( {A} \\) and \\( {B} \\) consists in the following computations:<\/p>\n<p style=\"text-align: center;\">\\[ \\begin{array}{rcl} C_{11}&amp;=&amp;A_{11}B_{11}+A_{12}B_{21}\\\\ C_{12}&amp;=&amp;A_{11}B_{12}+A_{12}B_{22}\\\\ C_{21}&amp;=&amp;A_{21}B_{11}+A_{22}B_{21}\\\\ C_{22}&amp;=&amp;A_{21}B_{12}+A_{22}B_{22}. \\end{array} \\]<\/p>\n<p style=\"text-align: justify;\">This requires \\( {8} \\) multiplications (we neglect the cost of additions). Surprisingly, Mister Volker Strassen suggest to use the following curious alternative scheme:<\/p>\n<p style=\"text-align: center;\">\\[ \\begin{array}{rcl} D_1&amp;=&amp;(A_{11}+A_{22})(B_{11}+B_{22})\\\\ D_2&amp;=&amp;(A_{21}+A_{22})B_{11}\\\\ D_3&amp;=&amp;A_{11}(B_{12}-B_{22})\\\\ D_4&amp;=&amp;A_{22}(B_{21}-B_{11})\\\\ D_5&amp;=&amp;(A_{11}+A_{12})B_{22}\\\\ D_6&amp;=&amp;(A_{21}-A_{11})(B_{11}+B_{12})\\\\ D_7&amp;=&amp;(A_{12}-A_{22})(B_{21}+B_{22}) \\end{array} \\]<\/p>\n<p style=\"text-align: justify;\">and then<\/p>\n<p style=\"text-align: center;\">\\[ \\begin{array}{rcl} C_{11}&amp;=&amp;D_1+D_4-D_5+D_7\\\\ C_{12}&amp;=&amp;D_3+D_5\\\\ C_{21}&amp;=&amp;D_2+D_4\\\\ C_{22}&amp;=&amp;D_1-D_2+D_3+D_6. \\end{array} \\]<\/p>\n<p style=\"text-align: justify;\">This algorithm involves only \\( {7} \\) multiplications instead of \\( {8} \\). This simple fact, used form \\( {n\\times n} \\) matrices when \\( {n} \\) is a power of \\( {2} \\) (via multiplication by blocks), allows to reduce the complexity of the multiplication of a couple of \\( {n\\times n} \\) matrices from \\( {O(n^3)} \\) to \\( {O(n^{2.8})} \\). It is known as the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Strassen_algorithm\">Strassen algorithm<\/a>. It was published in 1969 in a famous paper of 3 pages called <a href=\"http:\/\/www.ams.org\/mathscinet-getitem?mr=248973\">Gaussian elimination is not optimal<\/a>. It has some similarities with the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fast_Fourier_transform\">fast Fourier transform<\/a>. <a href=\"http:\/\/en.wikipedia.org\/wiki\/Volker_Strassen\">Volker Strassen<\/a> (1936 - ) is a German mathematician who is also well known, among other things, for<\/p>\n<ul>\n<li>a version of the <a href=\"\/scripts\/search.php\/?q=Strassen+law+of+iterated+logarithm\">law of iterated logarithm<\/a> (\\(\\neq\\) Kolmogorov and Khinchine)<\/li>\n<li>the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Schonhage-Strassen_algorithm\">Sch\u00f6nhage-Strassen algorithm<\/a> for fast integers multiplication<\/li>\n<li>the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Solovay-Strassen_primality_test\">Solovay-Strassen primality test<\/a> (the first randomized primality test).<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">He has also a <a href=\"http:\/\/www.ams.org\/mathscinet-getitem?mr=177430\">nice paper on the existence of probability measures with given marginals<\/a>. I have a great admiration for his achievements!<\/p>\n<p style=\"text-align: justify;\">I have learnt the Horner scheme in my youth, at school in Algeria, and the Strassen algorithm ten years ago from <a href=\"\/scripts\/search.php\/?q=Terry+Lyons+Oxford\">Terry J. Lyons<\/a> during a postdoc in the Oxford mathematical institute. Of course one can mix the two for the evaluation of a matrix polynomial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is devoted to a couple of simple fast algorithms. The Horner algorithm. The naive way to evaluate at point \\( {z} \\) the&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/djalil.chafai.net\/blog\/2011\/08\/27\/a-couple-of-fast-algorithms\/\">Continue reading<span class=\"screen-reader-text\">A couple of fast algorithms<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":43},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts\/2223"}],"collection":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/comments?post=2223"}],"version-history":[{"count":44,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts\/2223\/revisions"}],"predecessor-version":[{"id":2304,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts\/2223\/revisions\/2304"}],"wp:attachment":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/media?parent=2223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/categories?post=2223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/tags?post=2223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}