12 ]c=. 1 2 3;4 5;6 7 8 õıııııÃıııÃıııııÀ ş1 2 3ş4 5ş6 7 8ş ÁıııııÂıııÂıııııã |.&.> c Open, reverse, and then box õıııııÃıııÃıııııÀ ş3 2 1ş5 4ş8 7 6ş ÁıııııÂıııÂıııııã Exercises: 28.1 Use the following as exercises in reading and writing. Try using arguments such as a=. 2 3 5 7 and b=. 1 2 3 4 and c=. <@i."0 i. 3 4 : f=. +&.^. Multiplication by addition of natural logs g=. +&.(10&^.) Multiplication using base-10 logs h=. *&.^ Addition from multiplication i=. |.&.> Reverse each box j=. +/&.> Sum each box k=. +/&> Sum each box and leave open 29: IDENTITY FUNCTIONS The monads 0&+ and %&1 are identity functions, and 0 and 1 are said to be identity elements of the dyads + and % respectively. Insertion on an empty list yields the identity element of the dyad inserted. For example: +/ i.0 +/'' +/0{. 2 3 5 0 0 0 */i.0 */'' */0{. 2 3 5 1 1 1 These results are useful in partitioning lists; they ensure that certain obvious relations continue to hold even when one of the partitions is empty. For example: +/ a=. 2 3 5 7 11 28 (+/4{.a)+(+/4}.a) 28 (+/0{.a)+(+/0}.a) 28 */a 2310 (*/4{.a)*(*/4}.a) 2310 (*/0{.a)*(*/0}.a) 2310 The identity functions and other basic characteristics of functions (such as rank) are given by the adverb b. . For example: ^ b. _1 NB. Inverse ^. ^ b. 0 NB. Ranks _ 0 0 ^ b. 1 NB. Identity function $&1@(}.@$) Exercises: 29.1 Predict and test the results of the following expressions: */'' <./'' >./'' >./0 4 4 $ 0 +/ . */ 0 4 4 $ 0 +&.^./ 29.2 Experiment with the dyad {@; and give the term used to describe it in mathematics. ANSWER: Cartesian product 29.3 Test the assertion that the monads(%:@~. +/ . * =) and %:are equivalent, and state the utility of the former when applied to a list such as 1 4 1 4 2 that has repeated elements. ANSWER: The function %: (which could be a function costly to execute) is applied only to the distinct elements of the argument (as selected by the nub function ~.) 29.4 Comment on the following experiments before reading the comments on the right: a=. 2 3 5 [ b=. 1 2 4 a (f=. *:@+) b Square of sum a (g=. +&*: + +:@*) b Sum of squares plus double product a (f=g) b Expression of the identity of the functions a (f-:g) b f and g in a tautology (whose result is taut=. f-:g always true; that is, 1). 29.5 A phrase such as f-:g may be a tautology for the dyadic case only, for the monadic case only, or for both. Use the following tautologies as reading and writing exercises, including statements of applicability (Dyad only, etc.): t1=. >: -: > +. = (Dyad only) The primitive >: is identical to greater than or equal t2=. <. -: -@>.&- (Both) Lesser-of is neg on greater-of on neg; Floor is neg on ceiling on neg t3=. <. -: >.&.- Same as t2 but uses under t4=. *:@>: -: *: + +: + 1: (Monad) Square of a+1 is square of a plus twice a plus 1 t5=.*:@>:-:#.&1 2 1"0 Same as t4 using polynomial t6=. ^&3@>:-:#.&1 3 3 1"0 Like t5 for cube bc=. i.@>: ! ] Binomial coefficients t7=.(>:@]^[)-:(]#.bc@[)"0 Like t6 with k&t7 for kth power s=. 1&o. [. c=. 2&o. Sine and Cosine t8=.s@+-:(s@[*c@])+(c@[*s@]) (Dyad) Addition and Subtraction t9=.s@--:(s@[*c@])-(c@[*s@]) Formulas for sine det=. -/ . * Determinant perm=. +/ . * Permanent sct=. 1 2&o."0@(,"0) Sine and cosine tables t10=. s@- -: det@sct Same as t9 but using the determinant of the sin and cos table t11=. s@+ -: perm@sct Like t8 using the permanent S=. 5&o. [. C=. 6&o. Hyperbolic sine and cosine SCT=. 5 6&o."0@(,"0) Sinh and Cosh table t12=. S@+ -: perm@SCT Addition theorem for sinh SINH=. ^ .: - Odd part of exponential COSH=. ^ .. - Even part of exponential t13=. SINH -: S Sinh is odd part of exponential t14=. COSH -: 6&o. Cosh is the even part of exponential sine=. ^&.j. .: - Sine is the odd part of exponential t15=. sine -: s under multiplication by 0j1 29.6 Comment on the following expressions before reading the comments on the right: g=. + > >. Test if sum exceeds maximum 5 g 2 True for positive arguments 5 g _2 _1 0 1 2 but not true in general f=. *.&(0&<) Test if both arguments exceed 0 theorem=. f <: g The truth value of the result of f does not exceed that of g. This may also be stated as If f (is true) then g (is true) or as f implies g 5 theorem _2 _1 0 1 2 30: SECONDARIES It is convenient to supplement the primitives or primaries provided in a language by secondaries whose names belong to an easily recognized class. The following examples use names beginning with a capital letter: Ad=. ["_ 0:}-@>:@$@]{.] Append diagonal scalar Ai=. >:@i. Augmented integers Area=. [: Det ] ,. %@!@#"1 Area (Vol) try Area tet=.0,=i.3 Bc=. i. !/ i. Binomial coefficients Bca=. %.@Bc Binomial coeffs (alternating) By=. ' '&;@,.@[ ,. ] By (format) Cpa=. ]%.i.@#@]^/Ei@[ Coeffs of poly approx CPA=. (@])%.i.@#@]^/Ei@[ Coeffs of poly approx (adverb) Det=. -/ . * Determinant Dpc=. 1: }. ] * i.@# Differentiate poly coeffs D1=. ("0)(D.1) Derivative (scalar, first) Ei=. i.@(+*+0&=) Extended integers Epc=. Bc@# X ] Expand poly coeffs Ipc=. 0: , ] % Ai@# Integrate poly coeffs Inv=. ^:_1 Inverse Id=. =@i. Identity matrix Mat=. ] -: /:~ Monotone ascending test Mdt=. ] -: \:~ Monotone descending test Mrg=. +&$ {. ,@(|:@,:) Merge Over=. ({.;}.)@":@, Over (format) Pad=. 2 :'x.%.]^/Ei@(y."_)' Polynomial approx of degree Pp=. +//.@(*/) Polynomial coeffs product Si=. (Ei@+: - |) : (-/ i.) Symmetric and subsiding int Span=. 2 : 'y."_ x.\ ]' Span of apply of left arg S1=. |:@|@(^!._1/~%.^/~)@i. Stirling numbers (1st kind) S2=. |:@ (^/~%.^!._1/~)@i. Stirling numbers (2nd kind) Ta=. 1 : '[By]Over x./' Table adverb Thr=. ] * 0.1&^@[ <: |@] Threshold for non-zero Tile=. $@]{.[$~$@]+2:|1:+$@] Tile (try 0 1 Tile i. 2 3 4) X=. +/ . * Times (matrix product) XA=. -/ . * Times (alternating) Exercises: 30.1 Enter the definitions of the secondaries listed in Lesson 30 (or at least those used in these exercises), and then enter the following expressions: (Ai 2 3);(Ai 2 _3);(Ei 2 3);(Ei 2 _3) (i.;i.@-;Ai;Ai@-;Ei;Ei@-) 4 (Si 4);(7 4 Si 4) +Ta~@i. 4 (S1;S2) 7 (];X/;%/;%./;(%./%{.)) y=. (Bc ,: Bca) 5 (0 1&Cb;1 _1&Cb) i. 2 3 4 30.2 Perform further experiments with the secondaries of Lesson 30. SAMPLE TOPICS This section provides examples of the use of J in various topics; it is designed to be used in conjunction with the dictionary and at the keyboard of a J system. It is also designed to be used inductively, as follows: * Read one or two sentences and their results (which begin at the left margin), and attempt to state clearly in English what each sentence does. * Enter similar sentences to test the validity of your statements. * Consult the dictionary to confirm your understanding of the meaning of primitives such as i. (used both with one argument and with two). Use the Vocabulary at the end of the book as an index to pages in the dictionary. * Enter partsof a complex sentence, such as i. 28 and j+/i.28 in the case of (j+/i.28){a. used on the following page. SPELLING phr=.'index=.a.i.''aA''' ;:phr õıııııÃııÃııÃııÃııııÀ şindexş=.şa.şi.ş'aA'ş ÁıııııÂııÂııÂııÂııııã $ ;:phr 5 >;:phr index =. a. i. 'aA' (do=. ".) phr 97 65 do 'abc =. 3 1 4 2' 3 1 4 2 abc 3 1 4 2 ALPHABET and NUMBERS $ a. ] j=. a. i. 'aA' 256 97 65 j +/ i. 8 97 98 99 100 101 102 103 104 65 66 67 68 69 70 71 72 (j+/i.28){a. "Non-printing" and abcdefghijklmnopqrstuvwxyz{| control characters ABCDEFGHIJKLMNOPQRSTUVWXYZ[\ make odd displays, õııııııııııııııııııııııııııııııııÀ but spaces are sub- ş ş stituted for them in ş !"#$%&'()*+,-./0123456789:;<=>?ş displays of boxed ş@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_ş arrays. ş`abcdefghijklmnopqrstuvwxyz{|}~ ş ş ş ş ş ş ş ş ş Áııııııııııııııııııııııııııııııııã 1 2 3{ 8 32$a. The major alphabet !"#$%&'()*+,-./0123456789:;<=>? @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_ `abcdefghijklmnopqrstuvwxyz{|}~ á i. 2 5 Table of integers 0 1 2 3 4 5 6 7 8 9 r=. 0j1 _1 0j_1 1 Square roots of plus and minus 1 + r (Complex) conjugates 0j_1 _1 0j1 1 r * +r 1 1 1 1 r */ r Multiplication table of roots of unity _1 0j_1 1 0j1 0j_1 1 0j1 _1 1 0j1 _1 0j_1 0j1 _1 0j_1 1 GRAMMAR fahrenheit =. 50 (fahrenheit - 32) * 5%9 10 prices =. 3 1 4 2 orders =. 2 0 2 1 orders * prices 6 0 8 2 +/ orders * prices 16 +/ \ 1 2 3 4 5 1 3 6 10 15 2 3 * / 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 (cube=. ^&3) i. 9 0 1 8 27 64 125 216 343 512 PARTS OF SPEECH 50 fahrenheit Nouns/Pronouns + - * % cube Proverbs / \ Adverbs & Conjunction =. Copula ( ) Punctuation FUNCTION TABLES Just as the behaviour of addition is made clear by addition tablesin elementary school, so the behaviour of other verbs (or functions) can be made clear by function tables. The next few pages show how to make function tables, and how to use the utility functions over and by to border them with their arguments to make them easier to interpret. Study the tables shown, and make tables for other functions (such as < <. and %) suggested by the Vocabulary. TABLES n=. 0 1 2 n +/ n Addition table 0 1 2 1 2 3 2 3 4 */ ~ n Times table 0 0 0 0 1 2 0 2 4 ^/ ~ i. 4 Power table 1 0 0 0 1 1 1 1 1 2 4 8 1 3 9 27 +./~ 0 1 Or table 0 1 1 1 BORDERING A TABLE over=. ({.;}.)@":@, Utilityáfunctions, intended for use by=. ' '&;@,.@[,.] rather than for immediate study primes=. 2 3 5 [ i=. 0 1 2 3 4 primes by i over primes */ i õıÃııııııııııııÀ ş ş0 1 2 3 4ş ÈıÊıııııııııııı³ ş2ş0 2 4 6 8ş ş3ş0 3 6 9 12ş ş5ş0 5 10 15 20ş ÁıÂııııııııııııã tba=. 1 : '[ by ] over x./' primes * tba i NB. Table adverb õıÃııııııııııııÀ ş ş0 1 2 3 4ş ÈıÊıııııııııııı³ ş2ş0 2 4 6 8ş ş3ş0 3 6 9 12ş ş5ş0 5 10 15 20ş ÁıÂııııııııııııã 7 11 ^ tba i õııÃıııııııııııııııııııÀ ş ş0 1 2 3 4ş ÈııÊııııııııııııııııııı³ ş 7ş1 7 49 343 2401ş ş11ş1 11 121 1331 14641ş ÁııÂıııııııııııııııııııã TABLES (Letter Frequency) text=. ' i sing of olaf glad and big' alph=. ' abcdefghijklmnopqrstuvwxyz' 10{.alph=/text 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 '01'{~10{.alph=/text 1010000100100001000010001000 0000000000000100001001000000 0000000000000000000000000100 0000000000000000000000000000 0000000000000000000100010000 0000000000000000000000000000 0000000001000010000000000000 0000001000000000100000000001 0000000000000000000000000000 0100100000000000000000000010 ]LF=. 2 13 $ +/"1 alph =/ text Letter frequency table 7 3 1 0 2 0 2 3 0 3 0 0 2 0 2 2 0 0 0 1 0 0 0 0 0 0 +/+/LF 28 $text 28 TABLES div=. 0=rem=. i |/i=. i.7 Divisibility and remainder tables (,.i) ; rem ; div õıÃıııııııııııııÃıııııııııııııÀ ş0ş0 1 2 3 4 5 6ş1 0 0 0 0 0 0ş ş1ş0 0 0 0 0 0 0ş1 1 1 1 1 1 1ş