{"id":1482,"date":"2014-11-12T12:20:41","date_gmt":"2014-11-12T12:20:41","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=1482"},"modified":"2014-12-04T10:45:13","modified_gmt":"2014-12-04T10:45:13","slug":"musings-on-reduction","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2014\/11\/musings-on-reduction\/","title":{"rendered":"Musings on Reduction"},"content":{"rendered":"<p>In one man&#8217;s humble opinion, <strong>reduction<\/strong> (<code class=\"language-apl\">\u233f<\/code>) is the <strong>Queen of Operators<\/strong>.<\/p>\n<p><strong>Each<\/strong> (<code class=\"language-apl\">\u00a8<\/code>) comes a close second, but doesn&#8217;t get the cigar because <strong>each<\/strong> can be written in terms of <strong>reduction<\/strong>.<\/p>\n<p>Two special cases are of interest: reduction along axes of length 1 (or reduction of a scalar) and reduction along axes of length 0.<\/p>\n<p>With a <em>length-1<\/em> axis (or scalar), the operand function is not applied (<code class=\"language-apl\">+\u233f'A' \u2192 'A'<\/code>). This can be useful as an implicit no-op \u2013 see\u00a0<a href=\"http:\/\/www.youtube.com\/watch?v=DsZdfnlh_d0\">DFS video on YouTube<\/a>.<\/p>\n<p>With a <em>length-0<\/em> axis, a primitive operand returns its right identity item \u2013 but only if one is defined (<code class=\"language-apl\">\u230a\u233f\u236c<\/code>). Otherwise: DOMAIN ERROR.<\/p>\n<p>Another way to think about the 0-length axis case is that a right identity item (if there is one) is catenated to the argument prior to the reduction. Functional Programming languages tend to define reduction in this way by supplying an explicit initial value (<code class=\"language-apl\">ival<\/code>) to the reduction:<\/p>\n<pre><code class=\"language-apl\">    fold fn ival [] = ival\r\n    fold fn ival (x:xs) = fn x (fold fn ival xs)<\/code><\/pre>\n<p>We can write such a variant of <code class=\"language-apl\">\u233f<\/code> in APL, supplying the initial value as right operand <code class=\"language-apl\">\u2375\u2375<\/code>:<\/p>\n<pre><code class=\"language-apl\">      fold \u2190 {\u237a\u237a\u233f\u2375\u236a\u2375\u2375} \u00a0 \u00a0 \u00a0 \u00a0\u235d right operand \u2375\u2375 is initial value\r\n\r\n      \u00d7 fold 1 \u22a22 3 4 \u00a0 \u00a0 \u00a0   \u235d same as regular \u00d7\u233f\r\n24\r\n\r\n      {\u237a\u00d7\u2375}fold 1 \u22a22 3 4 \u00a0   \u00a0\u235d non-primitive operand function\r\n24\r\n\r\n      {\u237a\u00d7\u2375}fold 1 \u22a2\u236c \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u235d initial value returned for empty argument\r\n1<\/code><\/pre>\n<p>Whilst it doesn&#8217;t provide the no-op trick for length-1 axes, <code class=\"language-apl\">fold<\/code> gives us better control for null cases than does primitive reduction, which relies on the single prototypical item of its argument array:<\/p>\n<pre><code class=\"language-apl\">      \u22a2mat \u2190 2 3 \u2218.+ 0(0 0)\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u25022\u25022 2\u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2524\r\n\u25023\u25023 3\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2518<\/code><\/pre>\n<p>Notice the discontinuity in the depth of the result with regular <code class=\"language-apl\">+\u233f<\/code>\u00a0as the number of rows reaches 0:<\/p>\n<pre><code class=\"language-apl\">      +\u233f 2\u2191mat\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u25025\u25025 5\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2518\r\n\r\n      +\u233f 1\u2191mat\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u25022\u25022 2\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2518\r\n\r\n      +\u233f 0\u2191mat \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u235d Eh?\r\n0 0<\/code><\/pre>\n<p>Supplying the variant with a <em>prototypical row<\/em> produces a more uniform convergence:<\/p>\n<pre><code class=\"language-apl\">      +fold 0(0 0) \u22a22\u2191mat\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u25025\u25025 5\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2518\r\n\r\n      +fold 0(0 0) \u22a21\u2191mat\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u25022\u25022 2\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2518\r\n\r\n      +fold 0(0 0) \u22a20\u2191mat \u00a0 \u235d Ah!\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u25020\u25020 0\u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2518<\/code><\/pre>\n<p>A similar discontinuity can be seen <em>even for axes of length 1<\/em>, with <em>non-scalar<\/em>\u00a0primitive operand functions:<\/p>\n<pre><code class=\"language-apl\">      \u22a2mat \u2190 3 3\u2374\u23739\r\n1 2 3\r\n4 5 6\r\n7 8 9<\/code><\/pre>\n<p>Now:<\/p>\n<pre><code class=\"language-apl\">      ,\u233f 3\u2191mat \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u235d join reduction\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25021 4 7\u25022 5 8\u25023 6 9\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\r\n\r\n      ,\u233f 2\u2191mat\r\n\u250c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u25021 4\u25022 5\u25023 6\u2502\r\n\u2514\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2518\r\n\r\n      ,\u233f 1\u2191mat \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u235d Tsk!\r\n1 2 3\r\n\r\n      ,\u233f 0\u2191mat \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u235d Bah!\r\nDOMAIN ERROR<\/code><\/pre>\n<p>But:<\/p>\n<pre><code class=\"language-apl\">      ,fold(\u2282\u236c) \u22a23\u2191mat\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25021 4 7\u25022 5 8\u25023 6 9\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\r\n\r\n      ,fold(\u2282\u236c) \u22a22\u2191mat\r\n\u250c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u25021 4\u25022 5\u25023 6\u2502\r\n\u2514\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2518\r\n\r\n      ,fold(\u2282\u236c) \u22a21\u2191mat   \u00a0 \u00a0\u235d Ooh!\r\n\u250c\u2500\u252c\u2500\u252c\u2500\u2510\r\n\u25021\u25022\u25023\u2502\r\n\u2514\u2500\u2534\u2500\u2534\u2500\u2518\r\n\r\n      ,fold(\u2282\u236c) \u22a20\u2191mat   \u00a0 \u00a0\u235d Aah!\r\n\u250c\u252c\u252c\u2510\r\n\u2502\u2502\u2502\u2502\r\n\u2514\u2534\u2534\u2518<\/code><\/pre>\n<p>Although we have no specific plans to do so, it is conceivable that this definition of <code class=\"language-apl\">fold<\/code> could be introduced as a variant of primitive reduction:<\/p>\n<pre><code class=\"language-apl\">        nums \u2190 \u2360(\u2282\u236c)        \u235d possible variant for numeric reduction\r\n\r\n      ,\u233fnums 1\u2191mat       \u00a0 \u00a0\u235d Ooh!\r\n\u250c\u2500\u252c\u2500\u252c\u2500\u2510\r\n\u25021\u25022\u25023\u2502\r\n\u2514\u2500\u2534\u2500\u2534\u2500\u2518\r\n      ,\u233fnums 0\u2191mat       \u00a0 \u00a0\u235d Aah!\r\n\u250c\u252c\u252c\u2510\r\n\u2502\u2502\u2502\u2502\r\n\u2514\u2534\u2534\u2518\r\n      ,\/nums 3 1\u2191mat       \u00a0\u235d Mmm! reduction along last axis\r\n\u250c\u2500\u252c\u2500\u252c\u2500\u2510\r\n\u25021\u25024\u25027\u2502\r\n\u2514\u2500\u2534\u2500\u2534\u2500\u2518\u0013<\/code><\/pre>\n<p>Notice that the left and right arguments of a reduction&#8217;s operand function need not be of the same kind. Using an informal type notation:<\/p>\n<pre><code class=\"language-apl\">      \u233f :: (\u237a \u2207 \u2375 \u2192 \u2375) \u2207\u2207 [\u237a]\u236a\u2375 \u2192 \u2282\u2375<\/code><\/pre>\n<p>which, given an argument of uniform kind, collapses to:<\/p>\n<pre><code class=\"language-apl\">      \u233f :: (\u237a \u2207 \u237a \u2192 \u237a) \u2207\u2207 [\u237a] \u2192 \u2282\u237a<\/code><\/pre>\n<p>I hope to say more about this style of polymorphic type notation in a future posting. In the meantime, the significant point is only that, in the general case, the operand function is of &#8220;kind&#8221; <code class=\"language-apl\">(\u237a \u2207 \u2375 \u2192 \u2375)<\/code>, which means that the kind of its left argument may differ from that of its right argument and result. See more discussion on this idea in the notes for <a href=\"http:\/\/dfns.dyalog.com\/n_foldl.htm\">foldl<\/a> in dfns.dws.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one man&#8217;s humble opinion, reduction (\u233f) is the Queen of Operators. Each (\u00a8) comes a close second, but doesn&#8217;t get the cigar because each can be written in terms of reduction. Two special cases are of interest: reduction along &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2014\/11\/musings-on-reduction\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,23],"tags":[],"class_list":["post-1482","post","type-post","status-publish","format-standard","hentry","category-apl-language","category-musings"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1482","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\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=1482"}],"version-history":[{"count":87,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1482\/revisions"}],"predecessor-version":[{"id":3992,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1482\/revisions\/3992"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=1482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=1482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=1482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}