{"id":4059,"date":"2018-10-18T09:12:48","date_gmt":"2018-10-18T09:12:48","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=4059"},"modified":"2018-10-18T09:34:29","modified_gmt":"2018-10-18T09:34:29","slug":"progressive-index-of","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2018\/10\/progressive-index-of\/","title":{"rendered":"Progressive Index-Of"},"content":{"rendered":"<p><code class=\"language-apl\">\u2395io=0 <\/code>is assumed throughout.<\/p>\n<p>A recent <a href=\"https:\/\/forums.dyalog.com\/viewtopic.php?f=13&amp;t=1381&amp;sid=e112c37782d3819aa1a7ffa723a2e9d9\">Forum post<\/a> motivated investigations into the progressive index-of functions in the <a href=\"https:\/\/aplwiki.com\/FinnAplIdiomLibrary?highlight=%28idiom%29#Grade_Up_.2BI0s-\" target=\"_blank\" rel=\"noopener\">FinnAPL Idiom Library<\/a>:<\/p>\n<pre><code class=\"language-apl\">pix  \u2190 {((\u2374\u237a)\u2374\u234b\u234b\u237a\u2373\u237a,\u2375) \u2373 ((\u2374\u2375)\u2374\u234b\u234b\u237a\u2373\u2375,\u237a)}   \u235d FinnAPL Idiom 1\r\npixa \u2190 {((\u234b\u237a\u2373\u237a,\u2375)\u2373\u2373\u2374\u237a) \u2373 ((\u234b\u237a\u2373\u2375,\u237a)\u2373\u2373\u2374\u2375)}   \u235d FinnAPL Idiom 5\r\n<\/code><\/pre>\n<p>In this note, we:<\/p>\n<ul>\n<li>explain what is progressive index-of<\/li>\n<li>explain why the two functions work<\/li>\n<li>investigate the performance of the two functions<\/li>\n<li>provide a more general solution<\/li>\n<\/ul>\n<h2>Progressive Index-Of<\/h2>\n<p>Progressive index-of is like index-of (<code class=\"language-apl\">\u2373<\/code>) except that each find &#8220;uses up&#8221; the target of that find. There are no duplicates in the result with the possible exception of <code class=\"language-apl\">\u2262\u237a<\/code> (for &#8220;not found&#8221;). Thus:<\/p>\n<pre><code class=\"language-apl\">      x\u2190'mississippi'\r\n      y\u2190'dismiss'\r\n\r\n      x pix y\r\n11 1 2 0 4 3 5\r\n<\/code><\/pre>\n<p>The following chart illustrates a step-by-step derivation of each progressive index:<\/p>\n<pre><code>0 1 2 3 4 5 6 7 8 9 10\r\n\r\nm i s s i s s i p p  i      <span style=\"color: red;\">d<\/span> i s m i s s\r\n                           11\r\nm <span style=\"color: red;\">i<\/span> s s i s s i p p  i      d <span style=\"color: red;\">i<\/span> s m i s s\r\n                           11 1\r\nm <span style=\"text-decoration: line-through;\">i<\/span> <span style=\"color: red;\">s<\/span> s i s s i p p  i      d i <span style=\"color: red;\">s<\/span> m i s s\r\n                           11 1 2\r\n<span style=\"color: red;\">m<\/span> <span style=\"text-decoration: line-through;\">i<\/span> <span style=\"text-decoration: line-through;\">s<\/span> s i s s i p p  i      d i s <span style=\"color: red;\">m<\/span> i s s\r\n                           11 1 2 0\r\n<span style=\"text-decoration: line-through;\">m<\/span> <span style=\"text-decoration: line-through;\">i<\/span> <span style=\"text-decoration: line-through;\">s<\/span> s <span style=\"color: red;\">i<\/span> s s i p p  i      d i s m <span style=\"color: red;\">i<\/span> s s\r\n                           11 1 2 0 4\r\n<span style=\"text-decoration: line-through;\">m<\/span> <span style=\"text-decoration: line-through;\">i<\/span> <span style=\"text-decoration: line-through;\">s<\/span> <span style=\"color: red;\">s<\/span> <span style=\"text-decoration: line-through;\">i<\/span> s s i p p  i      d i s m i <span style=\"color: red;\">s<\/span> s\r\n                           11 1 2 0 4 3\r\n<span style=\"text-decoration: line-through;\">m<\/span> <span style=\"text-decoration: line-through;\">i<\/span> <span style=\"text-decoration: line-through;\">s<\/span> <span style=\"text-decoration: line-through;\">s<\/span> <span style=\"text-decoration: line-through;\">i<\/span> <span style=\"color: red;\">s<\/span> s i p p  i      d i s m i s <span style=\"color: red;\">s<\/span>\r\n                           11 1 2 0 4 3 5\r\n<\/code><\/pre>\n<p>It is possible to compute the progressive index without looping or recursion, as the two FinnAPL functions demonstrate.<\/p>\n<h2>Why It Works<\/h2>\n<p>The basic idea of <code class=\"language-apl\">\u237a pix \u2375<\/code> is to substitute for each item of <code class=\"language-apl\">\u237a<\/code> and <code class=\"language-apl\">\u2375<\/code> an equivalent representative, collectively <code class=\"language-apl\">c<\/code> and <code class=\"language-apl\">d<\/code>, whence the result obtains as <code class=\"language-apl\">c\u2373d<\/code>. The equivalent representative used here is <em>ranking<\/em>, specifically the ranking of the indices in <code class=\"language-apl\">\u237a<\/code>.<\/p>\n<p>The ranking of an array <code class=\"language-apl\">\u2375<\/code> is a permutation of order <code class=\"language-apl\">\u2262\u2375<\/code>. The smallest major cell is assigned 0; the next smallest is assigned 1; and so on. Ties are resolved by favoring the earlier-occurring cell. The ranking can be computed by <code class=\"language-apl\">\u234b\u234b\u2375<\/code>. For example:<\/p>\n<pre><code class=\"language-apl\">      x \u236a \u2349\u236a \u234b\u234bx\r\nm i s s i s  s i p p i\r\n4 0 7 8 1 9 10 2 5 6 3\r\n\r\n      y \u236a \u2349\u236a \u234b\u234by\r\nd i s m i s s\r\n0 1 4 3 2 5 6\r\n<\/code><\/pre>\n<p><code class=\"language-apl\">\u237a pix \u2375<\/code> works on two different rankings of indices in <code class=\"language-apl\">\u237a<\/code>:<\/p>\n<p><code class=\"language-apl\">\u234b\u234b\u237a\u2373\u237a,\u2375\u00a0\u00a0\u00a0\u00a0<\/code>rankings of indices in <code class=\"language-apl\">\u237a<\/code> of <code class=\"language-apl\">\u237a<\/code> and <code class=\"language-apl\">\u2375<\/code>, favoring <code class=\"language-apl\">\u237a<\/code><br \/>\n<code class=\"language-apl\">\u234b\u234b\u237a\u2373\u2375,\u237a\u00a0\u00a0\u00a0\u00a0<\/code>rankings of indices in <code class=\"language-apl\">\u237a<\/code> of <code class=\"language-apl\">\u2375<\/code> and <code class=\"language-apl\">\u237a<\/code>, favoring <code class=\"language-apl\">\u2375<\/code><\/p>\n<p>The first <code class=\"language-apl\">\u2374\u237a<\/code> items of the former are those for <code class=\"language-apl\">\u237a<\/code> and the first <code class=\"language-apl\">\u2374\u2375<\/code> of the latter are those for <code class=\"language-apl\">\u2375<\/code>, and we get<\/p>\n<pre><code class=\"language-apl\">pix \u2190 {((\u2374\u237a)\u2374\u234b\u234b\u237a\u2373\u237a,\u2375) \u2373 ((\u2374\u2375)\u2374\u234b\u234b\u237a\u2373\u2375,\u237a)}<\/code><\/pre>\n<p>The second version depends on the following properties of permutations. Let <code class=\"language-apl\">p<\/code> be a permutation. Then <code class=\"language-apl\">p[\u234bp] \u2190\u2192 \u2373\u2262p<\/code>, the identity permutation, and therefore <code class=\"language-apl\">\u234bp<\/code> is the inverse of <code class=\"language-apl\">p<\/code>. Furthermore, <code class=\"language-apl\">p[p\u2373\u2373\u2262p] \u2190\u2192 \u2373\u2262p<\/code> and so <code class=\"language-apl\">p\u2373\u2373\u2262p<\/code> is also the inverse of <code class=\"language-apl\">p<\/code>. The inverse is unique (that&#8217;s why it&#8217;s called <em>the<\/em> inverse), therefore <code class=\"language-apl\">\u234bp \u2190\u2192 p\u2373\u2373\u2262p<\/code>.<\/p>\n<pre><code class=\"language-apl\">      p\u219097?97         \u235d a random permutation\r\n\r\n      p[\u234bp]    \u2261 \u2373\u2262p\r\n1\r\n      p[p\u2373\u2373\u2262p] \u2261 \u2373\u2262p\r\n1\r\n      (\u234bp)     \u2261 p\u2373\u2373\u2262p\r\n1\r\n<\/code><\/pre>\n<p>The two rankings are permutations (because the leftmost functions are <code class=\"language-apl\">\u234b<\/code>) and we just need the first <code class=\"language-apl\">\u2374\u237a<\/code> items of the former and the first <code class=\"language-apl\">\u2374\u2375<\/code> items of the latter. Thus:<\/p>\n<pre><code class=\"language-apl\">pixa \u2190 {((\u234b\u237a\u2373\u237a,\u2375)\u2373\u2373\u2374\u237a) \u2373 ((\u234b\u237a\u2373\u2375,\u237a)\u2373\u2373\u2374\u2375)}\r\n<\/code><\/pre>\n<h2>Performance<\/h2>\n<p>We note that both versions of <code class=\"language-apl\">pix<\/code> contain the expressions <code class=\"language-apl\">\u237a\u2373\u237a,\u2375<\/code> and <code class=\"language-apl\">\u237a\u2373\u2375,\u237a<\/code>, but the latter is just a rotation of the former. Thus:<\/p>\n<pre><code class=\"language-apl\">pixb \u2190 {i\u2190\u237a\u2373\u237a,\u2375 \u22c4 ((\u2374\u237a)\u2374\u234b\u234bi) \u2373 ((\u2374\u2375)\u2374\u234b\u234b(\u2374\u237a)\u233di)}\r\npixc \u2190 {i\u2190\u237a\u2373\u237a,\u2375 \u22c4 ((\u234bi)\u2373\u2373\u2374\u237a) \u2373 ((\u234b(\u2374\u237a)\u233di)\u2373\u2373\u2374\u2375)}\r\n<\/code><\/pre>\n<p>Which is faster? The answer may surprise.<\/p>\n<pre><code class=\"language-apl\">      x\u2190?1e6\u23743e5\r\n      y\u2190?2e5\u23743e5\r\n\r\n      cmpx 'x pixb y' 'x pixc y'\r\n  x pixb y \u2192 9.15E\u00af2 |  0% \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  x pixc y \u2192 9.21E\u00af2 |  0% \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 few factors about the Dyalog APL interpreter are relevant to this performance:<\/p>\n<ul>\n<li>Computing <code class=\"language-apl\">\u237a\u2373\u2375,\u237a<\/code> as a rotation of an already computed <code class=\"language-apl\">i\u2190\u237a\u2373\u237a,\u2375<\/code> produces a worthwhile speed-up, although only on a relatively small part of the overall computation.\n<pre><code class=\"language-apl\">      i\u2190x\u2373x,y\r\n      cmpx '(\u2374x)\u233di' 'x\u2373y,x'\r\n  (\u2374x)\u233di \u2192 5.00E\u00af4 |     0% \u2395\u2395                            \r\n  x\u2373y,x  \u2192 7.19E\u00af3 | +1337% \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<\/li>\n<li>Both <code class=\"language-apl\">\u2373<\/code> and <code class=\"language-apl\">\u234b<\/code> have special code for small range data.\n<pre><code class=\"language-apl\">      s\u2190?1e6\u23745e5           \u235d small range\r\n      t\u2190s \u22c4 t[t\u2373\u2308\/t]\u21902e9   \u235d large range\r\n\r\n      cmpx 's\u2373s' 't\u2373t'\r\n  s\u2373s \u2192 5.87E\u00af3 |    0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395                     \r\n  t\u2373t \u2192 2.00E\u00af2 | +240% \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\r\n      cmpx '\u234bs' '\u234bt'\r\n  \u234bs \u2192 3.25E\u00af2 |   0% \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  \u234bt \u2192 3.84E\u00af2 | +18% \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<\/li>\n<li><code class=\"language-apl\">\u234b\u2375<\/code> has special code when <code class=\"language-apl\">\u2375<\/code> is a permutation.\n<pre><code class=\"language-apl\">      p\u21901e6?1e6           \u235d p is a permutation\r\n      q\u2190p \u22c4 q[999999]\u2190\u2283q  \u235d q is not; both are small-range\r\n\r\n      cmpx '\u234bp' '\u234bq'\r\n  \u234bp \u2192 5.81E\u00af3 |    0% \u2395\u2395\u2395                           \r\n* \u234bq \u2192 5.71E\u00af2 | +882% \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<\/li>\n<li>We saw previously that if <code class=\"language-apl\">p<\/code> is a permutation then <code class=\"language-apl\">\u234bp \u2190\u2192 p\u2373\u2373\u2374p<\/code>. The special code for <code class=\"language-apl\">\u234bp<\/code> makes the two expressions run at roughly the same speed. The slight advantage for <code class=\"language-apl\">\u234b\u234bx<\/code> versus <code class=\"language-apl\">(\u234bx)\u2373\u2373\u2374x<\/code> would increase if and when <code class=\"language-apl\">\u234b\u234b<\/code> is recognized as an idiom.\n<pre><code class=\"language-apl\">      cmpx '\u234bp' 'p\u2373\u2373\u2374p'\r\n  \u234bp    \u2192 6.02E\u00af3 |  0% \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  p\u2373\u2373\u2374p \u2192 6.57E\u00af3 | +9% \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\r\n      cmpx '\u234b\u234bx' '(\u234bx)\u2373\u2373\u2374x'\r\n  \u234b\u234bx      \u2192 3.16E\u00af2 |  0% \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  (\u234bx)\u2373\u2373\u2374x \u2192 3.25E\u00af2 | +2% \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\r\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>A General Solution<\/h2>\n<p>Index-of works on cells rather than just scalars. Likewise, progressive index-of can also be extended to work on cells. The core algorithm remains the same. The generalization obtains by first reshaping <code class=\"language-apl\">\u2375<\/code> to have the same rank as <code class=\"language-apl\">\u237a<\/code> (having major cells with the same shape), applying the core algorithm, and then reshaping its result to have the same leading shape as the original <code class=\"language-apl\">\u2375<\/code>. Thus:<\/p>\n<pre><code class=\"language-apl\">pixd\u2190{\r\n  m\u2190\u2262\u237a\r\n  r\u21900\u230a1-\u2374\u2374\u237a\r\n  n\u2190\u00d7\/r\u2193\u2374\u2375\r\n  i\u2190\u237a\u2373\u237a\u236a(n,1\u2193\u2374\u237a)\u2374\u2375\r\n  (r\u2193\u2374\u2375) \u2374 ((\u234bi)\u2373\u2373m) \u2373 ((\u234bm\u233di)\u2373\u2373n)\r\n}\r\n\r\n   xx              yy\r\nmmmm            dddd\r\niiii            iiii\r\nssss            ssss\r\nssss            mmmm\r\niiii            iiii\r\nssss            ssss\r\nssss            ssss\r\niiii     \r\npppp     \r\npppp                                  x\r\niiii                               mississippi\r\n\r\n   \u2374xx             \u2374yy                y\r\n11 4            7 4                dismiss\r\n\r\n   xx pixd yy                         x pixd y\r\n11 1 2 0 4 3 5                     11 1 2 0 4 3 5\r\n\r\n   xx pixd 3 5 4\u2374yy                   x pixd 3 5\u2374y\r\n11  1  2  0  4                     11  1  2  0  4\r\n 3  5 11  7  6                      3  5 11  7  6\r\n11 10 11 11 11                     11 10 11 11 11\r\n<\/code><\/pre>\n<p><strong>Postscript<\/strong><br \/>\nAfter having written the above, I discovered an alternative exposition on progressive index-of by Bob Smith entitled <a href=\"http:\/\/www.sudleyplace.com\/APL\/Anatomy%20of%20An%20Idiom.pdf\" target=\"_blank\" rel=\"noopener\"><em>Anatomy of an Idiom<\/em><\/a>. Ad\u00e1m Brudzewsky has produced a <a href=\"https:\/\/chat.stackexchange.com\/rooms\/52405\/conversation\/lesson-27-lookup-without-replacement\" target=\"_blank\" rel=\"noopener\">Stack Exchange lesson<\/a> and a <a href=\"https:\/\/github.com\/Dyalog\/dyalog-jupyter-notebooks\/blob\/master\/lookup%20without%20replacement.ipynb\" target=\"_blank\" rel=\"noopener\">Jupyter Notebook<\/a> based on Smith&#8217;s text.<\/p>\n<p>There is also an <a href=\"https:\/\/code.jsoftware.com\/wiki\/Essays\/Progressive_Index-Of\" target=\"_blank\" rel=\"noopener\">exposition in J<\/a> on the same topic, with a more verbose but easier-to-understand derivation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2395io=0 is assumed throughout. A recent Forum post motivated investigations into the progressive index-of functions in the FinnAPL Idiom Library: pix \u2190 {((\u2374\u237a)\u2374\u234b\u234b\u237a\u2373\u237a,\u2375) \u2373 ((\u2374\u2375)\u2374\u234b\u234b\u237a\u2373\u2375,\u237a)} \u235d FinnAPL Idiom 1 pixa \u2190 {((\u234b\u237a\u2373\u237a,\u2375)\u2373\u2373\u2374\u237a) \u2373 ((\u234b\u237a\u2373\u2375,\u237a)\u2373\u2373\u2374\u2375)} \u235d FinnAPL Idiom 5 In this &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2018\/10\/progressive-index-of\/\">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-4059","post","type-post","status-publish","format-standard","hentry","category-apl-language"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4059","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=4059"}],"version-history":[{"count":8,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4059\/revisions"}],"predecessor-version":[{"id":5331,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4059\/revisions\/5331"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=4059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=4059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=4059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}