{"id":3337,"date":"2016-06-24T14:46:53","date_gmt":"2016-06-24T14:46:53","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=3337"},"modified":"2016-07-19T07:28:37","modified_gmt":"2016-07-19T07:28:37","slug":"a-memo-operator","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2016\/06\/a-memo-operator\/","title":{"rendered":"A Memo Operator"},"content":{"rendered":"<p><strong>Abstract<\/strong><\/p>\n<p>Some functions are simply stated and easily understood as multiply-recursive functions. For example, the Fibonacci numbers are <code class=\"language-apl\">{1\u2265\u2375:\u2375 \u22c4 (\u2207 \u2375-2)+\u2207 \u2375-1}<\/code>. A drawback of multiple recursion is abysmal performance even for moderate-sized arguments, consequently motivating resorts to circumlocutions. The memo operator addresses the performance problem and restores multiple recursion to the toolkit of thought. We present a memo operator modelled as a 6-line d-operator.<\/p>\n<p><code class=\"language-apl\">\u2395io\u21900<\/code> throughout.<\/p>\n<p><strong><em>The Man Who Knew Infinity<\/em><\/strong><\/p>\n<p>Jay sent out an e-mail asking who would be interested to go see the film <a href=\"https:\/\/en.wikipedia.org\/wiki\/The_Man_Who_Knew_Infinity_%28film%29\" target=\"_blank\"><i>The Man Who Knew Infinity<\/i><\/a>. Fiona&#8217;s response: &#8220;Ah, <a href=\"http:\/\/www.dyalog.com\/blog\/2015\/03\/unforgettable-numbers\/\" target=\"_blank\">Mr. 1729<\/a>; I&#8217;m in.&#8221; John was game, and so was I.<\/p>\n<p><strong>The Number of Partitions<\/strong><br \/>\nNick found an <a href=\"https:\/\/backchannel.com\/who-was-ramanujan-83b4d5b05665\" target=\"_blank\">essay by Stephen Wolfram<\/a> on Ramanujan written on occasion of the film, wherein Wolfram said:<\/p>\n<div style=\"margin: 15px 50px 15px 50px;\">\n<p>A <a href=\"http:\/\/ramanujan.sirinudi.org\/Volumes\/published\/ram36.pdf\" target=\"_blank\">notable paper<\/a> [Ramanujan] wrote with Hardy concerns the partition function (<code class=\"language-apl\">PartitionsP<\/code> in the Wolfram Language) that counts the number of ways an integer can be written as a sum of positive integers. \u2026<\/p>\n<p>In Ramanujan&#8217;s day, computing the exact value of <code class=\"language-apl\">PartitionsP[200]<\/code> was a big deal \u2014 and the climax of his paper. But now, thanks to Ramanujan&#8217;s method, it&#8217;s instantaneous:<\/p>\n<pre><code class=\"language-apl\">In[1]:= PartitionsP[200]\r\nOut[1]= 3 972 999 029 388\r\n<\/code><\/pre>\n<\/div>\n<p>A <a href=\"http:\/\/code.jsoftware.com\/wiki\/Essays\/Partitions\" target=\"_blank\"><em>partition<\/em><\/a> of a non-negative integer <code class=\"language-apl\">n<\/code> is a vector <code class=\"language-apl\">v<\/code> of positive integers such that <code class=\"language-apl\">n = +\/v<\/code>, where the order in <code class=\"language-apl\">v<\/code> is not significant. For example, the partitions of the first seven non-negative integers are:<\/p>\n<pre><code class=\"language-apl\">\u250c\u2510\r\n\u2502\u2502\r\n\u2514\u2518\r\n\u250c\u2500\u2510\r\n\u25021\u2502\r\n\u2514\u2500\u2518\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u25022\u25021 1\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2518\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25023\u25022 1\u25021 1 1\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25024\u25023 1\u25022 2\u25022 1 1\u25021 1 1 1\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25025\u25024 1\u25023 2\u25023 1 1\u25022 2 1\u25022 1 1 1\u25021 1 1 1 1\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25026\u25025 1\u25024 2\u25024 1 1\u25023 3\u25023 2 1\u25023 1 1 1\u25022 2 2\u25022 2 1 1\u25022 1 1 1 1\u25021 1 1 1 1 1\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>Can we compute the partition counting function &#8220;instantaneously&#8221; in APL?<\/p>\n<p>We will use a recurrence relation derived from the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pentagonal_number_theorem\" target=\"_blank\">pentagonal number theorem<\/a>, proved by Euler more than 160 years before Hardy and Ramanujan:<br \/>\n<a href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2016\/05\/euler_pn.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4025\" style=\"padding: none!important; border: none!important; text-decoration: none; margin: 0px 0px -10px 0px;\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2016\/05\/euler_pn.gif\" alt=\"\" width=\"446\" height=\"58\" \/><\/a><br \/>\nequation (11) in <a href=\"http:\/\/mathworld.wolfram.com\/PartitionFunctionP.html\" target=\"_blank\">http:\/\/mathworld.wolfram.com\/PartitionFunctionP.html<\/a>. In APL:<\/p>\n<pre><code class=\"language-apl\">      pn  \u2190 {0\u2265\u2375:0=\u2375 \u22c4 -\/+\u233f\u2207\u00a8rec \u2375}\r\n      rec \u2190 {\u2375 - (\u00f7\u22182 (\u00d7\u23641) \u00af1 1 \u2218.+ 3\u2218\u00d7) 1+\u2373\u23080.5*\u2368\u2375\u00d72\u00f73}\r\n\r\n      pn\u00a80 1 2 3 4 5 6\r\n1 1 2 3 5 7 11\r\n\r\n      pn 30\r\n5604\r\n<\/code><\/pre>\n<p>Warning: don&#8217;t try <code class=\"language-apl\">pn 200<\/code> because that would take a while! Why? <code class=\"language-apl\">pn 200<\/code> would engender <code class=\"language-apl\">pn<\/code> being applied to each element of <code class=\"language-apl\">rec 200<\/code> and:<\/p>\n<pre><code class=\"language-apl\">   rec 200\r\n199 195 188 178 165 149 130 108 83 55 24 \u00af10\r\n198 193 185 174 160 143 123 100 74 45 13 \u00af22\r\n<\/code><\/pre>\n<p>Each of the non-negative values would itself engender further recursive applications.<\/p>\n<p><strong>A Memo Operator<\/strong><\/p>\n<p>I recalled from J the <em>memo<\/em> adverb (monadic operator) <code class=\"language-apl\">M<\/code>. Paraphrasing the <a href=\"http:\/\/www.jsoftware.com\/help\/dictionary\/dmcapdot.htm\" target=\"_blank\">J dictionary<\/a>: <code class=\"language-apl\">f M<\/code> is the same as <code class=\"language-apl\">f<\/code> but may keep a record of the arguments and results for reuse. It is commonly used for multiply-recursive functions. Sounds like just the ticket!<\/p>\n<p>The functionistas had previously implemented a (dyadic) <a href=\"http:\/\/dfns.dyalog.com\/n_memo.htm\" target=\"_blank\">memo operator<\/a>. The version here is more restrictive but simpler. The restrictions are as follows:<\/p>\n<ul>\n<li>The cache is constructed &#8220;on the fly&#8221; and is discarded once the operand finishes execution.<\/li>\n<li>The operand is a dfn <code class=\"language-apl\">{condition:basis \u22c4 expression}<\/code> where none of <code class=\"language-apl\">condition<\/code>, <code class=\"language-apl\">base<\/code> and <code class=\"language-apl\">expression<\/code> contain a <code class=\"language-apl\">\u22c4<\/code> and recursion is denoted by <code class=\"language-apl\">\u2207<\/code> in <code class=\"language-apl\">expression<\/code><\/li>\n<li>The arguments are scalar integers.<\/li>\n<li>The operand function does not have <code class=\"language-apl\">\u00af1<\/code> as a result.<\/li>\n<li>A recursive call is on a smaller integer.<\/li>\n<\/ul>\n<p>With these restrictions, our memo operator can be defined as follows:<\/p>\n<pre><code class=\"language-apl\">M\u2190{\r\n f\u2190\u237a\u237a\r\n i\u21902+'\u22c4'\u2373\u2368t\u21903\u2193,\u2395CR'f'\r\n 0=\u2395NC'\u237a':\u234e' {T\u2190(1+  \u2375)\u2374\u00af1 \u22c4  {',(i\u2191t),'\u00af1\u2262T[\u2375]  :\u2283T[\u2375]   \u22c4 \u2283T[\u2375]  \u2190\u2282',(i\u2193t),'\u2375}\u2375'\r\n          \u234e'\u237a{T\u2190(1+\u237a,\u2375)\u2374\u00af1 \u22c4 \u237a{',(i\u2191t),'\u00af1\u2262T[\u237a;\u2375]:\u2283T[\u237a;\u2375] \u22c4 \u2283T[\u237a;\u2375]\u2190\u2282',(i\u2193t),'\u2375}\u2375'\r\n}\r\n\r\n      pn M 10\r\n42\r\n\r\n      pn 10\r\n42\r\n<\/code><\/pre>\n<p>Obviously, the <code class=\"language-apl\">\u234e<\/code> lines in <code class=\"language-apl\">M<\/code> are crucial. When the operand is <code class=\"language-apl\">pn<\/code>, the expression which is executed is as follows; the characters inserted by <code class=\"language-apl\">M<\/code> are shown in red.<\/p>\n<pre><span style=\"color: red;\">{T\u2190(1+\u2375)\u2374\u00af1 \u22c4 <\/span>{0\u2265\u2375:0=\u2375 \u22c4 <span style=\"color: red;\">\u00af1\u2262T[\u2375]:\u2283T[\u2375] \u22c4 \u2283T[\u2375]\u2190\u2282<\/span>-\/+\u233f\u2207\u00a8rec \u2375}<span style=\"color: red;\">\u2375}<\/span>\u2375\r\n<\/pre>\n<p>The machinations produce a worthwhile performance improvement:<\/p>\n<pre><code class=\"language-apl\">      pn M 30\r\n5604\r\n      cmpx 'pn M 30'\r\n3.94E\u00af4\r\n      cmpx 'pn 30'\r\n4.49E1 \r\n      4.49e1 \u00f7 3.94e\u00af4\r\n113959\r\n<\/code><\/pre>\n<p>That is, <code class=\"language-apl\">pn M 30<\/code> is faster than <code class=\"language-apl\">pn 30<\/code> by a factor of 114 thousand.<\/p>\n<p>Can APL compute <code class=\"language-apl\">pn M 200<\/code> &#8220;instantaneously&#8221;? Yes it can, for a suitable definition of &#8220;instantaneous&#8221;.<\/p>\n<pre><code class=\"language-apl\">      cmpx 'm\u2190pn M 200'\r\n4.47E\u00af3\r\n      m\r\n3.973E12\r\n      0\u2355m\r\n 3972999029388\r\n<\/code><\/pre>\n<p><code class=\"language-apl\">M<\/code> also works on operands which are anonymous, dyadic, or have non-scalar results.<\/p>\n<p><strong>Fibonacci Numbers<\/strong><\/p>\n<pre><code class=\"language-apl\">   fib\u2190{1\u2265\u2375:\u2375 \u22c4 (\u2207 \u2375-2)+\u2207 \u2375-1}\r\n\r\n   fib\u00a8 \u237315\r\n0 1 1 2 3 5 8 13 21 34 55 89 144 233 377\r\n   fib M\u00a8 \u237315\r\n0 1 1 2 3 5 8 13 21 34 55 89 144 233 377\r\n\r\n   cmpx 'fib 30'\r\n1.45E0 \r\n   cmpx 'fib M 30'\r\n1.08E\u00af4\r\n<\/code><\/pre>\n<p>Anonymous function:<\/p>\n<pre><code class=\"language-apl\">      {1\u2265\u2375:\u2375 \u22c4 (\u2207 \u2375-2)+\u2207 \u2375-1}M\u00a8 \u237315\r\n0 1 1 2 3 5 8 13 21 34 55 89 144 233 377\r\n<\/code><\/pre>\n<p><strong>Combinations<\/strong><br \/>\n<code class=\"language-apl\">\u237a comb \u2375<\/code> produces a matrix of all the size-<code class=\"language-apl\">\u237a<\/code> combinations of <code class=\"language-apl\">\u2373\u2375<\/code>. The algorithm is from the venerable <em>APL: An Interactive Approach<\/em> by Gilman and Rose.<\/p>\n<pre><code class=\"language-apl\">      comb\u2190{(\u237a\u2265\u2375)\u22280=\u237a:((\u237a\u2264\u2375),\u237a)\u2374\u2373\u237a \u22c4 (0,1+(\u237a-1)\u2207 \u2375-1)\u236a1+\u237a \u2207 \u2375-1}\r\n\r\n      3 comb 5\r\n0 1 2\r\n0 1 3\r\n0 1 4\r\n0 2 3\r\n0 2 4\r\n0 3 4\r\n1 2 3\r\n1 2 4\r\n1 3 4\r\n2 3 4\r\n\r\n      3 (comb \u2261 comb M) 5\r\n1\r\n\r\n      cmpx '8 comb M 16' '8 comb 16'\r\n  8 comb M 16 \u2192 1.00E\u00af3 |     0% \u2395                             \r\n  8 comb 16   \u2192 3.63E\u00af2 | +3526% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n<\/code><\/pre>\n<p>A faster <code class=\"language-apl\">comb<\/code> is possible without a memo operator, but it&#8217;s not easy. (See for example section 4.1 of <a href=\"http:\/\/www.jsoftware.com\/papers\/from.htm\" target=\"_blank\">my APL87 paper<\/a>.) A memo operator addresses the performance problem with multiple recursion and restores it to the toolkit of thought.<\/p>\n<p><em><a href=\"http:\/\/www.youtube.com\/watch?v=1adJ8qo6Vkc\" target=\"_blank\">Presented at the BAA seminar<\/a> at the Royal Society of Arts on 2016-05-20<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Abstract Some functions are simply stated and easily understood as multiply-recursive functions. For example, the Fibonacci numbers are {1\u2265\u2375:\u2375 \u22c4 (\u2207 \u2375-2)+\u2207 \u2375-1}. A drawback of multiple recursion is abysmal performance even for moderate-sized arguments, consequently motivating resorts to circumlocutions. &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2016\/06\/a-memo-operator\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-3337","post","type-post","status-publish","format-standard","hentry","category-apl-language"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/3337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=3337"}],"version-history":[{"count":13,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/3337\/revisions"}],"predecessor-version":[{"id":4247,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/3337\/revisions\/4247"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=3337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=3337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=3337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}