{"id":1866,"date":"2015-07-20T12:55:37","date_gmt":"2015-07-20T12:55:37","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=1866"},"modified":"2015-07-20T12:59:57","modified_gmt":"2015-07-20T12:59:57","slug":"permutations","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2015\/07\/permutations\/","title":{"rendered":"Permutations"},"content":{"rendered":"<p>I started composing a set of APL exercises in response to a query from a new APL enthusiast who attended Morten&#8217;s presentation at <a href=\"http:\/\/confengine.com\/functional-conf-2014\/proposal\/436\/pragmatic-functional-programming-using-dyalog\" target=\"_blank\">Functional Conf<\/a>, Bangalore, October 2014. The first set of exercise are at a low level of difficulty, followed by another set at an intermediate level. One of the intermediate exercises is:<\/p>\n<p><strong>Permutations<\/strong><\/p>\n<p>a. Compute all the permutations of <code class=\"language-apl\">\u2373\u2375<\/code> in lexicographic order. For example:<\/p>\n<pre><code class=\"language-apl\">   perm 3\r\n0 1 2\r\n0 2 1\r\n1 0 2\r\n1 2 0\r\n2 0 1\r\n2 1 0\r\n<\/code><\/pre>\n<p>b. Write a function that checks whether <code class=\"language-apl\">\u2375<\/code> is a solution to <code class=\"language-apl\">perm \u237a<\/code>, without computing <code class=\"language-apl\">perm \u237a<\/code>. You can use the function <code class=\"language-apl\">assert<\/code>. For example:<\/p>\n<pre><code class=\"language-apl\">assert\u2190{\u237a\u2190'assertion failure' \u22c4 0\u220a\u2375:\u237a \u2395SIGNAL 8 \u22c4 shy\u21900}\r\n\r\npcheck\u2190{\r\n  assert 2=\u2374\u2374\u2375:\r\n  assert (\u2374\u2375)\u2261(!\u237a),\u237a:\r\n  \u2026\r\n  1\r\n}\r\n\r\n   6 pcheck perm 6\r\n1\r\n\r\n   4 pcheck 2 4\u23740 1 2 3, 0 1 3 2\r\nassertion failure\r\npcheck[2] assert(\u2374\u2375)\u2261(!\u237a),\u237a:\r\n         \u2227\r\n<\/code><\/pre>\n<p>c. What is the index of permutation <code class=\"language-apl\">\u2375<\/code> in <code class=\"language-apl\">perm\u00a0\u237a<\/code>? Do this without computing all the permutations. For example:<\/p>\n<pre><code class=\"language-apl\">   7 ip 1 6 5 2 0 3 4    \u235d index from permutation\r\n1422\r\n<\/code><\/pre>\n<p>(The left argument in this case is redundant, being the same as <code class=\"language-apl\">\u2262\u2375<\/code>.)<\/p>\n<p>d. What is the <code class=\"language-apl\">\u2375<\/code>-th permutation of <code class=\"language-apl\">\u2373\u237a<\/code>? Do this without computing all the permutations. For example:<\/p>\n<pre><code class=\"language-apl\">   7 pi 1442             \u235d permutation from index\r\n1 6 5 2 0 3 4\r\n\r\n   (perm 7) \u2261 7 pi\u23640 \u2373!7\r\n1\r\n<\/code><\/pre>\n<p><strong>The Anagram Kata<\/strong><\/p>\n<p>Coincidentally, <a href=\"http:\/\/plus.google.com\/+GianfrancoAlongi\/posts\" target=\"_blank\">Gianfranco Alongi<\/a> was attempting in APL the anagrams kata from <a href=\"http:\/\/cyber-dojo.org\/setup\/show\/\" target=\"_blank\">Cyber Dojo<\/a>:<\/p>\n<div style=\"margin: 15px 30px 15px 30px;\">\n<p>Write a program to generate all potential anagrams of an input string.<\/p>\n<p>For example, the potential anagrams of &#8220;biro&#8221; are<br \/>\n<code class=\"language-apl\">biro bior brio broi boir bori<br \/>\nibro ibor irbo irob iobr iorb<br \/>\nrbio rboi ribo riob roib robi<br \/>\nobir obri oibr oirb orbi orib<\/code><\/p>\n<\/div>\n<p>This is essentially the same program\/exercise\/kata, because the potential anagrams are <code class=\"language-apl\">'biro'[perm\u00a04]<\/code>. You can compare solutions in other languages to what&#8217;s here (google &#8220;anagrams kata&#8221;).<\/p>\n<h2 style=\"text-align: center;\">Spoiler Alert<\/h2>\n<p><strong>Deriving a Solution<\/strong><\/p>\n<p>I am now going to present solutions to part <strong>a<\/strong> of the exercise, generating all permutations of <code class=\"language-apl\">\u2373\u2375<\/code>.<\/p>\n<p>Commonly, in TDD (test-driven development) you start with a very simple case and try to extend it successively to more general cases. It&#8217;s all too easy to be led into a dead-end because the simple case may have characteristics absent in a more general case. For myself, for this problem, I would start &#8220;in the middle&#8221;: Suppose I have <code class=\"language-apl\">perm\u00a03<\/code>, obtained by whatever means:<\/p>\n<pre><code class=\"language-apl\">   p\r\n0 1 2\r\n0 2 1\r\n1 0 2\r\n1 2 0\r\n2 0 1\r\n2 1 0\r\n<\/code><\/pre>\n<p>How do I get <code class=\"language-apl\">perm\u00a04<\/code> from that? One way is as follows:<\/p>\n<pre><code class=\"language-apl\">   p1\u21900,1+p\r\n   (0 1 2 3[p1]) (1 0 2 3[p1]) (2 0 1 3[p1]) (3 0 1 2[p1])\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25020 1 2 3\u25021 0 2 3\u25022 0 1 3\u25023 0 1 2\u2502\r\n\u25020 1 3 2\u25021 0 3 2\u25022 0 3 1\u25023 0 2 1\u2502\r\n\u25020 2 1 3\u25021 2 0 3\u25022 1 0 3\u25023 1 0 2\u2502\r\n\u25020 2 3 1\u25021 2 3 0\u25022 1 3 0\u25023 1 2 0\u2502\r\n\u25020 3 1 2\u25021 3 0 2\u25022 3 0 1\u25023 2 0 1\u2502\r\n\u25020 3 2 1\u25021 3 2 0\u25022 3 1 0\u25023 2 1 0\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>So it&#8217;s indexing each row of a matrix <code class=\"language-apl\">m<\/code> by <code class=\"language-apl\">0,1+p<\/code>. There are various ways of forming the matrix <code class=\"language-apl\">m<\/code>, one way is:<\/p>\n<pre><code class=\"language-apl\">   \u2352\u23641\u2218.=\u23680 1 2 3\r\n0 1 2 3\r\n1 0 2 3\r\n2 0 1 3\r\n3 0 1 2\r\n<\/code><\/pre>\n<p>(Some authors <a href=\"http:\/\/www.jsoftware.com\/jwiki\/Doc\/Articles\/Play202\" target=\"_blank\">waxed enthusiastic<\/a> about this &#8220;magical matrix&#8221;.) In any case, a solution obtains readily from the above description: Form a matrix from the above individual planes; replace the <code class=\"language-apl\">0 1 2 3<\/code> by <code class=\"language-apl\">\u2373\u2375<\/code>; and make an appropriate computation for the base case (when <code class=\"language-apl\">0=\u2375<\/code>). See the <a href=\"#20150712\">2015-07-12 entry below<\/a>.<\/p>\n<p><strong>The Best <code class=\"language-apl\">perm<\/code> Function<\/strong><\/p>\n<p>What is the &#8220;best&#8221; <code class=\"language-apl\">perm<\/code> function I can write in APL? This &#8220;best&#8221; is a benchmark not only on my own understanding but also on advancements in APL over the years.<\/p>\n<p>&#8220;Best&#8221; is a subjective and personal measure. Brevity comes into it but is not the only criteria. For example, <code class=\"language-apl\">{(\u2227\/(\u2373\u2375)\u220a\u23641\u22a2t)\u233ft\u2190\u2349(\u2375\u2374\u2375)\u22a4\u2373\u2375*\u2375}<\/code> is the shortest known solution, but requires space and time exponential in the size of the result, and that disqualifies it from being &#8220;best&#8221;. The similarly inefficient <code class=\"language-apl\">{(\u2227\/(\u2373\u2375)\u220a\u23641\u22a2t)\u233ft\u2190\u2191,\u2373\u2375\u2374\u2375}<\/code> is shorter still, but does not work for <code class=\"language-apl\">1=\u2375<\/code>.<\/p>\n<p>\u2022 <strong>1981<\/strong>, <em><a href=\"http:\/\/www.jsoftware.com\/papers\/nqueens.htm\" target=\"_blank\">The N Queens Problem<\/a><\/em><\/p>\n<pre><code class=\"language-apl\">    p\u2190perm n;i;ind;t\r\n   \u235d all permutations of \u2373n\r\n    p\u2190(\u00d7n,n)\u2374\u2395io\r\n    \u2192(1\u2265n)\u23740\r\n    t\u2190perm n-1\r\n    p\u2190(0,n)\u2374ind\u2190\u2373n\r\n    i\u2190n-~\u2395io\r\n   l10:p\u2190(i,((i\u2260ind)\/ind)[t]),[\u2395io]p\r\n    \u2192(\u2395io\u2264i\u2190i-1)\u2374l10\r\n<\/code><\/pre>\n<p>It was the fashion at the time that functions be written to work in either index-origin and therefore have <code class=\"language-apl\">\u2395io<\/code> sprinkled hither, thither, and yon.<\/p>\n<p>\u2022 <strong>1987<\/strong>, <a href=\"http:\/\/www.jsoftware.com\/papers\/from.htm\" target=\"_blank\"><em>Some Uses of <code class=\"language-apl\">{<\/code> and <code class=\"language-apl\">}<\/code><\/em><\/a><\/p>\n<pre><code class=\"language-apl\">   perm:  \u236a\u233fk,\u2364\u00af1 (\u2359\u2375-1){\u2364\u00af 1 k~\u23641 0 k\u2190\u2373\u2375\r\n       :  1\u2265\u2375\r\n       :  (1,\u2375)\u23740\r\n<\/code><\/pre>\n<p>Written in <a href=\"http:\/\/www.jsoftware.com\/papers\/APLDictionary.htm\" target=\"_blank\">Dictionary APL<\/a>, wherein: <code class=\"language-apl\">\u236a\u233f\u2375 \u2190\u2192 \u2283\u236a\u233f\u2282\u2364\u00af1\u22a2\u2375<\/code> and differs from its definition in Dyalog APL; <code class=\"language-apl\">\u2359<\/code> is equivalent to <code class=\"language-apl\">\u2207<\/code> in dfns; <code class=\"language-apl\">\u237a{\u2375 \u2190\u2192 (\u2282\u237a)\u2337\u2375<\/code>; and <code class=\"language-apl\">\u00af<\/code> by itself is infinity.<\/p>\n<p>\u2022 <strong>1990-2007<\/strong><\/p>\n<p>I worked on <code class=\"language-apl\">perm<\/code> from time to time in this period, but in J rather than in APL. The results are described in a <a href=\"http:\/\/www.jsoftware.com\/jwiki\/Essays\/Permutations\" target=\"_blank\">J essay<\/a> and in a <a href=\"http:\/\/www.jsoftware.com\/jwiki\/Doc\/Articles\/Play202\" target=\"_blank\">Vector article<\/a>. The lessons translate directly into Dyalog APL.<\/p>\n<p>\u2022 <strong>2008<\/strong>, <a href=\"http:\/\/dfns.dyalog.com\/n_pmat.htm\" target=\"_blank\">http:\/\/dfns.dyalog.com\/n_pmat.htm<\/a><\/p>\n<pre><code class=\"language-apl\">   pmat2\u2190{{,[\u23732]\u2191(\u2282\u2282\u2395io,1+\u2375)\u2337\u00a8\u2352\u00a8\u2193\u2218.=\u2368\u23731+1\u2193\u2374\u2375}\u2363\u2375\u2349\u236a\u236c}\r\n<\/code><\/pre>\n<p>In retrospect, the power operator is not the best device to use, because the left operand function needs both the previous result (equivalent to <code class=\"language-apl\">perm \u2375-1<\/code>) and <code class=\"language-apl\">\u2375<\/code>. It is awkward to supply two arguments to that operand function, and the matter is finessed by computing the latter as <code class=\"language-apl\">1+1\u2193\u2374\u2375<\/code>.<\/p>\n<p>In this formulation, <code class=\"language-apl\">\u2349\u236a\u236c<\/code> is rather circuitous compared to the equivalent <code class=\"language-apl\">1 0\u23740<\/code>. But the latter would have required a <code class=\"language-apl\">\u22a2<\/code> or similar device to separate it from the right operand of the power operator.<\/p>\n<p>\u2022 <strong id=\"20150712\">2015-07-12<\/strong><\/p>\n<pre><code class=\"language-apl\">   perm\u2190{0=\u2375:1 0\u23740 \u22c4 ,[\u23732](\u22820,1+\u2207 \u00af1+\u2375)\u2337\u23641\u2352\u23641\u2218.=\u2368\u2373\u2375}\r\n<\/code><\/pre>\n<p>For a time I thought the base case can be <code class=\"language-apl\">\u23731 0<\/code> instead of <code class=\"language-apl\">1 0\u23740<\/code>, and indeed the function works with that as the base case. Unfortunately <code class=\"language-apl\">(\u23731 0)\u22621 0\u23740<\/code>, having a different prototype and datatype.<\/p>\n<p>\u2022 <strong>Future<\/strong><\/p>\n<p>Where might the improvements come from?<\/p>\n<ul>\n<li>We are contemplating an <em>under<\/em> operator whose monadic case is <code class=\"language-apl\">f\u2362g \u2375 \u2190\u2192 g\u2363\u00af1 f g \u2375<\/code>. Therefore <code class=\"language-apl\">1+\u2207 \u00af1+\u2375 \u2190\u2192 \u2207\u2362(\u00af1\u2218+)\u2375<\/code><\/li>\n<li>Moreover, it is possible to define <code class=\"language-apl\">\u2264\u2375<\/code> as <code class=\"language-apl\">\u2375-1<\/code> (<em>decrement<\/em>) and <code class=\"language-apl\">\u2265\u2375<\/code> as <code class=\"language-apl\">\u2375+1<\/code> (<em>increment<\/em>), as in J; whence <code class=\"language-apl\">1+\u2207 \u00af1+\u2375 \u2190\u2192 \u2207\u2362\u2264\u2375<\/code><\/li>\n<li>Monadic <code class=\"language-apl\">=<\/code> can be defined as in J, <code class=\"language-apl\">=\u2375 \u2190\u2192 (\u222a\u2373\u2368\u2375)\u2218.=\u2373\u2262\u2375<\/code> (<em>self-classify<\/em>); whence <code class=\"language-apl\">\u2218.=\u2368\u2373\u2375 \u2190\u2192 =\u2373\u2375<\/code><\/li>\n<\/ul>\n<p>Putting it all together:<\/p>\n<pre><code class=\"language-apl\">   perm\u2190{0=\u2375:1 0\u23740 \u22c4 ,[\u23732](\u22820,\u2207\u2362\u2264\u2375)\u2337\u23641\u2352\u23641=\u2373\u2375}\r\n<\/code><\/pre>\n<p>We should do something about the <code class=\"language-apl\">,[\u23732]<\/code> :-)\u200b\u200b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I started composing a set of APL exercises in response to a query from a new APL enthusiast who attended Morten&#8217;s presentation at Functional Conf, Bangalore, October 2014. The first set of exercise are at a low level of difficulty, &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2015\/07\/permutations\/\">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":[13,8],"tags":[],"class_list":["post-1866","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-apl-language"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1866","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=1866"}],"version-history":[{"count":10,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1866\/revisions"}],"predecessor-version":[{"id":3113,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1866\/revisions\/3113"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=1866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=1866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=1866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}