From kei@interlog.com Mon Oct 16 15:28:04 2000 Date: Mon, 16 Oct 2000 15:16:48 -0400 From: K. E. Iverson Reply-To: forum@jsoftware.com To: forum@jsoftware.com Subject: Jforum: Smple Views Programmers and mathematicians commonly demand (and present) complete definitions of new notions, rather than simple introductions that begin by illustrating the main point. In particular we tend to use unfamilair technical terms that may eventually prove important, but may only complicate an introduction. I will here present some examples of simple views of important notions in J. I would appreciate comments on their utility, and suggestions for further topics, or better yet, some similar simple treatments of them. I. A Simple Composition of Functions. x=:i.6 ]x NB. The identity function 0 1 2 3 4 5 (]+])x NB. Double 0 2 4 6 8 10 (]*])x NB. Square 0 1 4 9 16 25 sq=:]*] NB. The parentheses serve only as punctuation sq x 0 1 4 9 16 25 ((]*])+])x 0 2 6 12 20 30 (]*(]+]))x NB. This is the normal default punctuation 0 2 8 18 32 50 (]*]+])x 0 2 8 18 32 50 mean=:+/%# mean x 2.5 com=:]-+/%# NB. Center on mean com x _2.5 _1.5 _0.5 0.5 1.5 2.5 ([: sq com)x NB. "dummy" [: executes sq monadically 6.25 2.25 0.25 0.25 2.25 6.25 For full definitions and further examples, see Forks and Trains in the dictionary. II. Operations on parts of a report. A (non-realistic) report on 5 products for 2 years of 4 quarters of 3 months each may be defined and displayed as follows: ]report=:i.5 2 4 3 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 $report NB. The shape of the report 5 2 4 3 Lists of entire years may be obtained by ravelling each of the 4-by-3 tables as follows: ,"2 report 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 Lists of entire two-year periods may be obtained by ravelling each of the 2-by-4-by-3 reports: ,"3 report 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 $,"3 report 5 24 $,"4 report NB. Shape of the list of the entire report 120 The same result is given by the expression $,report A full definition of the rank conjunction " is given in the dictionary. The foregoing illustrates only the case of monadic use of a function. |||. Operations on parts of the two arguments of a dyadic function. t2=:10+t1=:10+i.3 3 t1 10 11 12 13 14 15 16 17 18 t2 20 21 22 23 24 25 26 27 28 t1,"1 1 t2 NB. Catenate rows with rows 10 11 12 20 21 22 13 14 15 23 24 25 16 17 18 26 27 28 t1,"0 1 t2 NB. Catenate elements with rows 10 20 21 22 11 20 21 22 12 20 21 22 13 23 24 25 14 23 24 25 15 23 24 25 16 26 27 28 17 26 27 28 18 26 27 28 t1,"0 0 t2 NB. Catenate elements with elements 10 20 11 21 12 22 13 23 14 24 15 25 16 26 17 27 18 28 t1,"2 2 t2 NB. Catenate entire reports 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25 26 27 28 t1,t2 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25 26 27 28 IV. Re-ordering the categories of a report. A report by quarter-by-month-by-year-by-product may be obtained from the product-by-year-by-quarter-by-month report by selecting categories as follows: 2 3 1 0 |: report 0 24 12 36 1 25 13 37 2 26 14 38 3 27 15 39 4 28 16 40 5 29 17 41 6 30 18 42 7 31 19 43 8 32 20 44 9 33 21 45 10 34 22 46 11 35 23 47 $ 2 3 1 0 |: report 4 3 2 5 A full definition may be found under "transpose" in the dictionary. V. Function Tables Those among us lucky enough to have encountered addition and multiplication tables in elementary school were provided tools for showing important relations in math. These included the commutativity of both addition and multiplication (seen in the symmetry of the tables), and the fact that (x+1)+y equals x+(y+1) (seen in the diagonal lines of equal numbers in the addition table): a=:1 2 3 4 5 6 7 a+/a 2 3 4 5 6 7 8 3 4 5 6 7 8 9 4 5 6 7 8 9 10 5 6 7 8 9 10 11 6 7 8 9 10 11 12 7 8 9 10 11 12 13 8 9 10 11 12 13 14 a*/a 1 2 3 4 5 6 7 2 4 6 8 10 12 14 3 6 9 12 15 18 21 4 8 12 16 20 24 28 5 10 15 20 25 30 35 6 12 18 24 30 36 42 7 14 21 28 35 42 49 Color enhances patterns, and the function viewmat (view matrix) provided by entering load 'graph plot' may be applied to tables. To see this, enter: color=:viewmat color a+/a Then use the mouse or Alt Tab to return focus to the main window, and enter: color a*/a Use of the list b=:a-4 introduces negative arguments, and entering color b+/b shows that the same pattern prevails. However, the table b*/b 9 6 3 0 _3 _6 _9 6 4 2 0 _2 _4 _6 3 2 1 0 _1 _2 _3 0 0 0 0 0 0 0 _3 _2 _1 0 1 2 3 _6 _4 _2 0 2 4 6 _9 _6 _3 0 3 6 9 shows a pattern that breaks the table into four quadrants, giving the pattern of signs resulting from products of various signs. Coloring this table enhances the pattern, and the use of the signum function in the expression color *b*/b shows the pattern of signs more strikingly. Use of other functions such as < = > <. >. +. and *. in tables also show interesting patterns. Permutation tables such as (i.!3) A. 'ABC' and (i.!4) A. 0 1 2 3 and ((i.!4) A. 0 1 2 3){'ABCD' also show important patterns when colored, patterns that essentially show why the number of permutations of order 4 is given by 4*3*2*1. It may be helpful to maximize the size of the color window. VI Case Statements The expression ]`%`*`-`+ produces a result from which any one of the five component functions (identity, reciprocal, sign, negate, conjugate [which is the identity on the real numbers used here]) may be chosen for execution, as illustrated below: x=:2 _3 4 _5 6 ]`%`*`-`+ @. 0 x 2 _3 4 _5 6 ]`%`*`-`+ @. 1 x 0.5 _0.333333 0.25 _0.2 0.166667 ]`%`*`-`+ @. 2 x 1 _1 1 _1 1 ]`%`*`-`+ @. 3 x _2 3 _4 5 _6 ]`%`*`-`+ @. 4 x 2 _3 4 _5 6 Because they select a "case" from among a number of possible actions, expressions of the form ]`%`*`-`+ @. k are called "case statements". In the foregoing examples, the values used for k were all single integers. If k is a list, then a train is selected for execution. For example: ]`%`*`-`+ @. 0 1 2 3 4 x _2 _1.5 _1.33333 _1.25 _1.2 (] % * - +)x _2 _1.5 _1.33333 _1.25 _1.2 In the foregoing example, the functions % and - are used dyadically. In the following example * is used both monadically and dyadically. The result is the magnitude of the argument: ]`%`*`-`+ @. 2 2 ] x 2 3 4 5 6 (* * ]) x 2 3 4 5 6 |x 2 3 4 5 6 For full information on ` and @. see Tie (or Gerund) and Agenda in the J dictionary. Date: Tue, 17 Oct 2000 15:29:39 -0400 From: Clement Kent Reply-To: forum@jsoftware.com To: forum@jsoftware.com Subject: Re: Jforum: Smple Views As another example of coloring tables, consider the following: pascal =: [ ! [+/[ viewmat 2| pascal i.50x viewmat 3| pascal i.50x viewmat 4| pascal i.50x ,,,etc. These produce a series of what I think of as pretty quilt patterns. They also show internal structure in Pascal's triangle in interesting ways. It's interesting to compare the pictures that result when the modulus used is prime to those where the modulus is composite, a power of 2, etc. Clement Kent     President, Godel Computer Solutions Ltd.    phone: (416) 707-7801    fax: (416) 533-9624 godel@godel.net -------------------------------------------------------------------------------- J Forum: for information about this list, see http://www.jsoftware.com/forum.htm >A lot of these expressions are presented as forks, when they work >equally well as hooks. Is this intentional? Yes, since the hook f g can be replaced by the fork [ f g, and I thought that hooks would be an unnecessary complication in an introduction. >What about use of the Reflex in some of these cases? > > +~ 12 >24 > *~ 12 >144 > I agree that reflex (and passive) deserve a good deal of attention, but I wanted to get some reaction before cluttering the forum with even longer messages. -------------------------------------------------------------------------------- J Forum: for information about this list, see http://www.jsoftware.com/forum.htm