{"id":4049,"date":"2018-04-25T10:39:56","date_gmt":"2018-04-25T10:39:56","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=4049"},"modified":"2018-04-25T14:10:14","modified_gmt":"2018-04-25T14:10:14","slug":"dyadic-grade","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2018\/04\/dyadic-grade\/","title":{"rendered":"Dyadic Grade"},"content":{"rendered":"<p><code class=\" language-apl\">\u2395io=0<\/code> is assumed throughout. The essay talks only about <code class=\" language-apl\">\u234b<\/code> but the same ideas apply to <code class=\" language-apl\">\u2352<\/code>.<\/p>\n<h2>Background<\/h2>\n<p><code class=\" language-apl\">\u234b<\/code> has the distinction of being the first (in 1980) APL primitive function defined on major cells: the result orders items of a vector, rows of a matrix, planes of a 3-d array, etc. In the ordering major cells are compared in ravelled order, with leading items being more significant than trailing (lexicographic ordering). Moreover, in dyadic <em>grade<\/em> <code class=\" language-apl\">\u237a\u234b\u2375<\/code>, <code class=\" language-apl\">\u237a<\/code> specifies &#8220;alphabets&#8221; to be used in comparing the items of character array <code class=\" language-apl\">\u2375<\/code>.<\/p>\n<p>Dyadic <em>grade<\/em> has always been an APL primitive which is hard for me to understand, in that way kind of like dyadic <em>transpose<\/em> \u263a. I sat down to really understand it, starting from the simplest cases to the general case. The following is a record of my explorations.<\/p>\n<h2>Vector Left Argument<\/h2>\n<pre><code class=\" language-apl\">   gv\u2190 {\u234b\u237a\u2373\u2375}\r\n\r\n   a0\u2190 'abcdefghij'\r\n   x0\u2190 'chthonic'\r\n      \r\n   a0 gv x0\r\n0 7 1 3 6 2 4 5\r\n   a0 \u234b x0\r\n0 7 1 3 6 2 4 5\r\n\r\n   x0 \u2337\u2368 \u2282 a0 gv x0\r\ncchhiton\r\n<\/code><\/pre>\n<p>That is, grade the indices of <code class=\" language-apl\">\u2375<\/code> in <code class=\" language-apl\">\u237a<\/code>. If an item of <code class=\" language-apl\">\u2375<\/code> is not in <code class=\" language-apl\">\u237a<\/code> then its index is <code class=\" language-apl\">\u2262\u237a<\/code>.<\/p>\n<h2>Higher-Rank Left Argument with Unique Items<\/h2>\n<p>The coordinates of <code class=\" language-apl\">A[i;j;k;\u2026]<\/code> or <code class=\" language-apl\">A[\u2282i,j,k,\u2026]<\/code> is the vector <code class=\" language-apl\">i,j,k,\u2026<\/code>. The phrase <code class=\" language-apl\">\u2373\u2374A<\/code> produces the array of coordinates. For example, if <code class=\" language-apl\">\u237a<\/code> is the <code class=\" language-apl\">(2\u00a026)<\/code>-matrix of the upper and lower case English letters,<\/p>\n<pre><code class=\" language-apl\">   ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n   abcdefghijklmnopqrstuvwxyz\r\n<\/code><\/pre>\n<p>the corresponding coordinates are<\/p>\n<pre><code class=\" language-apl\">   \u250c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c   \u252c\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2510\r\n   \u25020 0\u25020 1\u25020 2\u25020 3\u25020 4\u25020 5\u25020 6\u25020 7\u25020 8\u2502   \u25020 24\u25020 25\u2502\r\n   \u251c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c \u2026 \u251c\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2524\r\n   \u25021 0\u25021 1\u25021 2\u25021 3\u25021 4\u25021 5\u25021 6\u25021 7\u25021 8\u2502   \u25021 24\u25021 25\u2502\r\n   \u2514\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534   \u2534\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>If the items of <code class=\" language-apl\">\u237a<\/code> are unique,<\/p>\n<pre><code class=\" language-apl\">   gu\u2190 {\u234b 0 2 1 \u2349 (\u2282(,\u237a)\u2373\u236a\u2375) \u2337 \u233d (\u2374\u237a) \u236a\u2368 \u2349(\u2374\u237a)\u22a4\u2373\u00d7\/\u2374\u237a}\r\n<\/code><\/pre>\n<p>That is, <code class=\" language-apl\">\u237a\u234b\u2375<\/code> obtains as the grade of the <em>reversed<\/em> coordinates of <code class=\" language-apl\">\u2375<\/code> in <code class=\" language-apl\">\u237a<\/code>. (If an item does not occur in <code class=\" language-apl\">\u237a<\/code>, its coordinates are <code class=\" language-apl\">\u2374\u237a<\/code>.) The <code class=\" language-apl\">\u233d<\/code> implements that in <code class=\" language-apl\">\u237a<\/code>, the first axis is least significant and the last axis is most significant. For the <code class=\" language-apl\">(2\u00a026)<\/code>-matrix above, case (the first axis) is less significant than <code class=\" language-apl\">A-Z<\/code> and <code class=\" language-apl\">a-z<\/code> (the last axis).<\/p>\n<pre><code class=\" language-apl\">   \u22a2 a1\u2190' ',\u2395av[(\u2395av\u2373'Aa')\u2218.+\u237326]\r\n ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n abcdefghijklmnopqrstuvwxyz\r\n\r\n   \u22a2 x1\u2190\u2191' '(\u2260\u2286\u22a2)' Jay roger Roger adam Adam jay' \r\nJay  \r\nroger\r\nRoger\r\nadam \r\nAdam \r\njay  \r\n\r\n   a1 gu x1\r\n4 3 0 5 2 1\r\n   a1 \u234b x1\r\n4 3 0 5 2 1\r\n\r\n   x1 \u2337\u2368 \u2282 a1 gu x1\r\nAdam \r\nadam \r\nJay  \r\njay  \r\nRoger\r\nroger\r\n<\/code><\/pre>\n<h2>Higher-Rank Left Arguments<\/h2>\n<p>Suppose <code class=\" language-apl\">\u237a<\/code> <em>does<\/em> have duplicates? For purposes of <code class=\" language-apl\">\u234b<\/code>, the coordinates of an item <code class=\" language-apl\">c<\/code> are<\/p>\n<pre><code class=\" language-apl\">   \u230a\u233f(c=,\u237a)\u233f\u2191,\u2373\u2374\u237a<\/code><\/pre>\n<p>That is, the minimum of coordinates of all items equal to <code class=\" language-apl\">c<\/code>. Note that the expression also works if <code class=\" language-apl\">c<\/code> is a unique item. Therefore, for a general <code class=\" language-apl\">\u237a<\/code>, with or without duplicates, <code class=\" language-apl\">\u237a\u234b\u2375<\/code> obtains as<\/p>\n<pre><code class=\" language-apl\">   gr\u2190 {\u234b 0 2 1 \u2349 (\u2282(\u222a,\u237a)\u2373\u236a\u2375) \u2337 \u233d (\u2374\u237a) \u236a\u2368 (,\u237a) {\u230a\u233f\u2375}\u2338 \u2349(\u2374\u237a)\u22a4\u2373\u00d7\/\u2374\u237a}\r\n<\/code><\/pre>\n<p>The &#8220;minimum of coordinates&#8221; computation is exploited to effect equal coodinates for disparate characters. For example, an ordering where upper and lower case are significant but diacritical marks are not, can be implemented as follows:<\/p>\n<pre><code class=\" language-apl\">   A    \u235d A has a leading blank column\r\n A\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5BC\u00c7DE\u00c8\u00c9\u00ca\u00cbFGHI\u00cc\u00cd\u00ce\u00cfJKLMN\u00d1O\u00d2\u00d3\u00d4\u00d5\u00d6\u00d8PQRSTU\u00d9\u00da\u00db\u00dcVWXY\u00ddZ\r\n a\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5bc\u00e7de\u00e8\u00e9\u00ea\u00ebfghi\u00ec\u00ed\u00ee\u00efjklmn\u00f1o\u00f2\u00f3\u00f4\u00f5\u00f6\u00f8pqrstu\u00f9\u00fa\u00fb\u00fcvwxy\u00fdz\r\n \u00c0       \u00c7  \u00c8       \u00cc        \u00d1 \u00d2                   \u00dd  \r\n \u00c1       \u00e7  \u00c9       \u00cd        \u00f1 \u00d3                   \u00fd  \r\n \u00c2          \u00ca       \u00ce          \u00d4                      \r\n \u00c3          \u00cb       \u00cf          \u00d6                      \r\n \u00c4          \u00e8       \u00ec          \u00d5                      \r\n \u00c5          \u00e9       \u00ed          \u00d8                      \r\n \u00e0          \u00ea       \u00ee          \u00f2                      \r\n \u00e1          \u00eb       \u00ef          \u00f3                      \r\n \u00e2                             \u00f4                      \r\n \u00e3                             \u00f5                      \r\n \u00e4                             \u00f6                      \r\n \u00e5                             \u00f8                      \r\n   \u2374A\r\n14 54\r\n\r\n   ('\u00c8'=,A)\u233f\u2191,\u2373\u2374A                ('\u00e8'=,A)\u233f\u2191,\u2373\u2374A\r\n0 13                          1 13\r\n2 12                          6 12\r\n   \u230a\u233f('\u00c8'=,A)\u233f\u2191,\u2373\u2374A              \u230a\u233f('\u00e8'=,A)\u233f\u2191,\u2373\u2374A\r\n0 12                          1 12\r\n\r\n   ('E'=,A)\u233f\u2191,\u2373\u2374A                ('e'=,A)\u233f\u2191,\u2373\u2374A\r\n0 12                          1 12\r\n   \u230a\u233f('E'=,A)\u233f\u2191,\u2373\u2374A              \u230a\u233f('e'=,A)\u233f\u2191,\u2373\u2374A\r\n0 12                          1 12\r\n<\/code><\/pre>\n<p><code class=\" language-apl\">'\u00c8'<\/code> occurs twice in <code class=\" language-apl\">A<\/code> with coordinates <code class=\" language-apl\">0\u00a013<\/code> and <code class=\" language-apl\">2\u00a012<\/code>. The coordinates assigned to <code class=\" language-apl\">'\u00c8'<\/code> are the minimum of these, <code class=\" language-apl\">0\u00a012<\/code>. In contrast, <code class=\" language-apl\">'E'<\/code> occurs once and its coordinates are <code class=\" language-apl\">0\u00a012<\/code>, the same as those for <code class=\" language-apl\">'\u00c8'<\/code>. Therefore, <code class=\" language-apl\">'E'<\/code> and <code class=\" language-apl\">'\u00c8'<\/code> are considered equal for purposes of dyadic grade. Similarly, <code class=\" language-apl\">'e'<\/code> and <code class=\" language-apl\">'\u00e8'<\/code> have coordinates <code class=\" language-apl\">1\u00a012<\/code> and are considered equal by <code class=\" language-apl\">\u234b<\/code>, but they follow <code class=\" language-apl\">'E'<\/code> and <code class=\" language-apl\">'\u00c8'<\/code> (because their coordinates are <code class=\" language-apl\">0\u00a012<\/code>).<\/p>\n<p>For example:<\/p>\n<pre><code class=\" language-apl\">   \u22a2 x\u2190 \u2191' '(\u2260\u2286\u22a2)' roger ad\u00e0m R\u00f6ger r\u00d6ger Ad\u00e5m J\u00c3Y JAY J\u00c3Y ad\u00e5m ad\u00e0m'\r\nroger\r\nad\u00e0m \r\nR\u00f6ger\r\nr\u00d6ger\r\nAd\u00e5m \r\nJ\u00c3Y  \r\nJAY  \r\nJ\u00c3Y  \r\nad\u00e5m \r\nad\u00e0m \r\n\r\n   A gr x\r\n4 1 8 9 5 6 7 2 3 0\r\n   A \u234b x\r\n4 1 8 9 5 6 7 2 3 0\r\n\r\n   x \u2337\u2368\u2282 A gr x\r\nAd\u00e5m \r\nad\u00e0m \r\nad\u00e5m \r\nad\u00e0m \r\nJ\u00c3Y  \r\nJAY  \r\nJ\u00c3Y  \r\nR\u00f6ger\r\nr\u00d6ger\r\nroger\r\n<\/code><\/pre>\n<p>Lest you think that dyadic grade in its full generality suffices to implement any ordering: in &#8220;telephone book&#8221; ordering, &#8220;1600 Pennsylvania Avenue&#8221; and &#8220;Unter den Linden 23&#8221; are ordered as if 1600 were spelled out as &#8220;Sixteen Hundred&#8221; and 23 as &#8220;Dreiundzwanzig&#8221;. A program to do <em>that<\/em> ought to be tr\u00e8s amusant.<\/p>\n<h2>Code Archeology<\/h2>\n<p>The above code are improved versions of what appeared in Peter Wooster, <em>Extended Upgrade and Downgrade<\/em>, SHARP APL Technical Notes 35, I.P. Sharp Associates, 1980-09-15. It is interesting to study the code from the two eras. (The code from 1980 is lightly edited for executability and clarity.)<\/p>\n<p>2018<\/p>\n<pre><code class=\" language-apl\">gv\u2190 {\u234b\u237a\u2373\u2375}\r\ngu\u2190 {\u234b 0 2 1 \u2349 (\u2282(,\u237a)\u2373\u236a\u2375) \u2337 \u233d (\u2374\u237a) \u236a\u2368 \u2349(\u2374\u237a)\u22a4\u2373\u00d7\/\u2374\u237a}\r\ngr\u2190 {\u234b 0 2 1 \u2349 (\u2282(\u222a,\u237a)\u2373\u236a\u2375) \u2337 \u233d (\u2374\u237a) \u236a\u2368 (,\u237a) {\u230a\u233f\u2375}\u2338 \u2349(\u2374\u237a)\u22a4\u2373\u00d7\/\u2374\u237a}\r\n<\/code><\/pre>\n<p>1980<\/p>\n<pre><code class=\" language-apl\">eu\u2190 {d\u22a4\u2373\u00d7\/d\u2190\u2374\u2375}\r\ner\u2190 {\u00af1+\u00f7(\u00f71+d\u22a4\u2373\u00d7\/d\u2190\u2374\u2375)\u2308.\u00d7a\u2218.=a\u2190,\u2375}\r\n\r\nfv\u2190 {\u234b\u237a\u2373\u2375}\r\nfu\u2190 {\u234b(\u23520 1,1\u21930\u00d7\u2373\u2374\u2374\u2375)\u2349(\u2296(eu \u237a),\u2374\u237a)[;(,\u237a)\u2373\u2375]}\r\nfr\u2190 {\u234b(\u23520 1,1\u21930\u00d7\u2373\u2374\u2374\u2375)\u2349(\u2296(er \u237a),\u2374\u237a)[;(,\u237a)\u2373\u2375]}\r\n<\/code><\/pre>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<td><code class=\" language-apl\">gv, fv<\/code><\/td>\n<td style=\"text-align: left;\">vector left argument<\/td>\n<\/tr>\n<tr>\n<td><code class=\" language-apl\">gu, fu<\/code><\/td>\n<td style=\"text-align: left;\">higher-ranked left argument with unique items<\/td>\n<\/tr>\n<tr>\n<td><code class=\" language-apl\">gr, fr<\/code><\/td>\n<td style=\"text-align: left;\">higher-ranked left argument<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In the sequence <code class=\" language-apl\">gv gu gr<\/code>, a function is more general than the preceding one and subsumes it. Likewise <code class=\" language-apl\">fv fu fr<\/code>.<\/p>\n<p>Comparison of the code illustrates advances in APL between 1980 and 2018:<\/p>\n<ul>\n<li><code class=\" language-apl\">{\u230a\u233f\u2375}\u2338\u00a0<\/code>minimum of major cells corresponding to identical keys<\/li>\n<li><code class=\" language-apl\">\u222a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code>unique items<\/li>\n<li><code class=\" language-apl\">\u236a\u2375\u00a0\u00a0\u00a0\u00a0\u00a0<\/code>ravel major cells<\/li>\n<li><code class=\" language-apl\">\u237a\u236a\u2375\u00a0\u00a0\u00a0\u00a0<\/code>catenate on first axis<\/li>\n<li><code class=\" language-apl\">\u2368\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code>commute operator<\/li>\n<li>dfns<\/li>\n<\/ul>\n<h2>Alternatives<\/h2>\n<p>If a left argument is large and complicated and is used repeatedly, it may be worthwhile for the APL interpreter to perform precomputations on it. Thus:<\/p>\n<pre><code class=\" language-apl\">   U\u2190 \u222a,A\r\n   C\u2190 \u233d (\u2374A) \u236a\u2368 (,A) {\u230a\u233f\u2375}\u2338 \u2349(\u2374A)\u22a4\u2373\u00d7\/\u2374A\r\n\r\n   \u2374U        \u2374C\r\n107       108 2\r\n\r\n   \u236aU        C\r\n           0  0\r\nA          1  0\r\n\u00c0          1  0\r\n\u00c1          1  0\r\n\u00c2          1  0\r\n\u00c3          1  0\r\n\u00c4          1  0\r\n\u00c5          1  0\r\nB          8  0\r\nC          9  0\r\n\u00c7          9  0\r\n\u2026           \u2026\r\nx         50  1\r\ny         51  1\r\n\u00fd         51  1\r\nz         53  1\r\n          14 54\r\n\r\n   gp\u2190 (U C)\u2218{U C\u2190\u237a \u22c4 \u234b0 2 1\u2349C[U\u2373\u236a\u2375;]}\r\n\r\n   gp x\r\n4 1 8 9 5 6 7 2 3 0\r\n   A \u234b x\r\n4 1 8 9 5 6 7 2 3 0\r\n<\/code><\/pre>\n<p>It makes sense that the number of columns in the coordinate matrix <code class=\" language-apl\">C<\/code> is equal to the rank of the alphabet array <code class=\" language-apl\">A<\/code>: The rank is the number of dimensions, a-z, upper\/lower case, color, etc.; each row of <code class=\" language-apl\">C<\/code> is a vector of the numeric value for each dimension.<\/p>\n<p>With 20\/20 hindsight, the above code can be seen as an argument <i>against<\/i> defining dyadic grade to do ordering with specified alphabets. After all,<\/p>\n<pre><code class=\" language-apl\">   \u237a\u234b\u2375  \u2190\u2192  \u234b0 2 1\u2349C[U\u2373\u236a\u2375;]\r\n<\/code><\/pre>\n<p>and specifying <code class=\" language-apl\">U<\/code> and <code class=\" language-apl\">C<\/code> directly makes the computation easier to understand, easier to use, and as it happens is faster than the primitive in the current implementation. The inverse calculation, from <code class=\" language-apl\">U\u00a0C<\/code> to the alphabet array <code class=\" language-apl\">A<\/code>, is an amusing bit of code left as an exercise for the reader\u263a.<\/p>\n<p>One can further argue that the current definition of dyadic grade precludes an alternative attractive but incompatible definition:<\/p>\n<pre><code class=\" language-apl\">   \u237a\u234b\u2375  \u2190\u2192  \u237a\u2337\u2368\u2282\u234b\u2375\r\n<\/code><\/pre>\n<p>That is, order <code class=\" language-apl\">\u237a<\/code> by the grade of <code class=\" language-apl\">\u2375<\/code>, whence <code class=\" language-apl\">\u234b\u2368<\/code> sorts. In Dyalog APL version 17.0, monadic <em>grade<\/em> is extended to work with a TAO (total array ordering). With a TAO and this alternative definition, <code class=\" language-apl\">\u234b\u2368<\/code> sorts any array.<\/p>\n<p>The present exposition exposes a difficulty with extending the current dyadic <em>grade<\/em> to work with TAO: It is axiomatic that monadic <em>grade<\/em> compares cells itemwise, stopping at the first pair of unequal items. Dyadic <em>grade<\/em> does not do that in general. For example, with an upper and lower case alphabet, you don&#8217;t stop comparing <code class=\" language-apl\">'Rogerz'<\/code> and <code class=\" language-apl\">'rogers'<\/code> on encountering <code class=\" language-apl\">'R'<\/code> and <code class=\" language-apl\">'r'<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2395io=0 is assumed throughout. The essay talks only about \u234b but the same ideas apply to \u2352. Background \u234b has the distinction of being the first (in 1980) APL primitive function defined on major cells: the result orders items of &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2018\/04\/dyadic-grade\/\">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-4049","post","type-post","status-publish","format-standard","hentry","category-apl-language"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4049","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=4049"}],"version-history":[{"count":16,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4049\/revisions"}],"predecessor-version":[{"id":5156,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4049\/revisions\/5156"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=4049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=4049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=4049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}