8.98756e_17 Newton's method may also be used for for a complex root: +. d NEWTON ^:0 1 2 3 _ a=. 1j1 1 1 0.0166065 0.99639 _0.990523 0.992532 _1.95338 1.10685 _1.6495 1.69393 POLYNOMIALS Stopes The expression */ x + s * i. n is often called a factorial function but, to avoid confusion with the function !, we will call it a stope; the factors occurring in its definition differ by steps of size s, like the steps in a mine stope. Stopes are useful in actuarial work and in the difference calculus. The stope is a variant of the power ^ (being equivalent for the case s=.0), and is provided by the fit conjunction in the variant ^!.s. For example: x=. 7 [ n=. 5 [ s=. _1 x + s * i. n 7 6 5 4 3 (*/x + s * i. n);(x ^!.s n);(x ^!.0 n);(x^n) õýýýýÃýýýýÃýýýýýÃýýýýýÀ þ2520þ2520þ16807þ16807þ ÁýýýýÂýýýýÂýýýýýÂýýýýýã The expression +/c * x^!.s i.#c is called a stope polynomial, just as +/c*x^i.#c may be called a power polynomial. We define an adverb P that applies to any step size s to provide the corresponding stope polynomial: P=. 1 : '+/@([ * ] ^!. x. i.@#@[)"1 0' c=. 1 3 3 1 [ d=. 1 7 6 1 [ x=. 0 1 2 3 4 (c p. x);(c 0 P x);(d _1 P x);(d p.!._1 x) õýýýýýýýýýýýýýÃýýýýýýýýýýýýýÃýýýýýýýýýýýýýÃýýýýýýýýýýýýýÀ þ1 8 27 64 125þ1 8 27 64 125þ1 8 27 64 125þ1 8 27 64 125þ ÁýýýýýýýýýýýýýÂýýýýýýýýýýýýýÂýýýýýýýýýýýýýÂýýýýýýýýýýýýýã As illustrated above, stope polynomials are (for a suitable choice of coefficients) equivalent to ordinary polynomials. Moreover, the transformations between them are provided by a matrix product as follows VM=. 1 : '[ ^!.x./ i.@#@]' TO=. 2 : '(x. VM %. y. VM)~ @i.@#' (0 TO _1 c) +/ . * c 1 7 6 1 The matrices 0 TO _1 and _1 TO 0 are mutually inverse, and are simply related to Stirling numbers: (0 TO _1 i.5);(_1 TO 0 i.5) õýýýýýýýýýÃýýýýýýýýýýýýÀ þ1 0 0 0 0þ1 0 0 0 0þ þ0 1 1 1 1þ0 1 _1 2 _6þ þ0 0 1 3 7þ0 0 1 _3 11þ þ0 0 0 1 6þ0 0 0 1 _6þ þ0 0 0 0 1þ0 0 0 0 1þ ÁýýýýýýýýýÂýýýýýýýýýýýýã DICTIONARY J is a dialect of APL, a formal imperative language. Because it is imperative, a sentence in J may also be called an instruction, and may be executed to produce a result. Because it is formal and unambiguous it can be executed mechanically by a computer, and is therefore called a programming language. Because it shares the analytic properties of mathematical notation, it is also called an analytic language. APL originated in an attempt to provide consistent notation for the teaching and analysis of topics related to the application of computers, and developed through its use in a variety of topics, and its implementation in computer systems [1-5]. J is implemented in C (as detailed in Hui [6]), and is ported to a number of different host computer systems. The effect of the specific host is minimal, and communication with it is confined to the single foreign conjunctionádetailed in the Appendix. See Burke [7] for other host facilities such as Windows. The Introduction in this book provides guidance to beginners. References [8-10] use J in the exposition of various mathematical topics. I: ALPHABET and WORDS The alphabet is standard ASCII, comprising digits, letters (of the English alphabet), the underline (used in names and numbers), the (single) quote, and others (which include the space) to be referred to as graphics. Alternative spellings for the national use characters (which differ from country to country) appear on page 180. Numbers are denoted by digits, the underbar (for negative signs and for infinity and minus infinity when used alone or in pairs), the period (used for decimal points and necessarily preceded by one or more digits), the letter e (as in 2.4e3 to signify 2400 in exponential form), and the letter j to separate the real and imaginary parts of a complex number, as in 3e4j_0.56. Also see page 201. A numeric list or vector is denoted by a list of numbers separated by spaces. A list of ASCII characters is denoted by the list enclosed in single quotes, a pair of adjacent single quotes signifying the quote itself: 'can''t' is the five-character abbreviation of the six-character word 'cannot'. The ace a: denotes the boxed empty list <$0 . Names (used for pronouns and other surrogates, and assigned referents by the copula, as in prices=. 4.5 12) begin with a letter and may continue with letters, underlines, and digits. A name that ends with an underline is a locative, as discussed in Section II.H. A primitive or primary may be denoted by any single graphic (such as + for plus) or by any graphic modified by one or more following inflections (a period or colon), as in +. and +: for or and nor. A primary may also be an inflected name, as in e. and o. for membershipand pi times. A primary cannot be assigned a referent by a copula. II. GRAMMAR The following sentences illustrate the six parts of speech: fahrenheit=. 50 (fahrenheit-32)*5%9 10 prices=. 3 1 4 2 orders=. 2 0 2 1 orders * prices 6 0 8 2 PARTS of SPEECH +/orders*prices 16 50 fahrenheit Nouns/Pronouns +/\1 2 3 4 5 + - * % bump Verbs/Proverbs 1 3 6 10 15 / \ Adverbs bump=. 1&+ & Conjunction bump prices ( ) Punctuation 4 2 5 3 =. Copula Verbs act upon nouns to produce noun results; the nouns to which a particular verb applies are called its arguments. A verb may have two distinct (but usually related) meanings according to whether it is applied to one argument (to its right), or to two arguments (left and right). For example, 2%5 yields 0.4, and %5 yields 0.2. An adverb acts on a single noun or verb to its left. For example, +/ is a derived verb (which might be called plus over) that sums an argument list to which it is applied, and */ yields the product of a list. A conjunction applies to two arguments, either nouns or verbs. Punctuation is provided by parentheses that specify the sequence of execution as in elementary algebra. The word =. behaves like the copulas is and are in English, and is read as such, as in area is 3 times 4 for area=. 3*4. The name area thus assigned is a pronoun and, as in English, it plays the role of a noun. Similar remarks apply to names assigned to verbs, adverbs, and conjunctions. Entry of a name alone displays its value. Errors are discussed in Section I. A. NOUNS Nouns are classified in three independent ways: numeric or literal; open or boxed; arrays of various ranks. In particular, arrays of ranks 0, 1, and 2 areácalled atom, list, and table, or, in mathematics, scalar, vector, and matrix. Numbers and literals are represented as stated in Part I. Arrays. A single entity such as 2.3 or _2.3j5 or 'A' or '+' is called an atom. The verb denoted by comma chains its arguments to form a list whose shape (given by the verb $) is equal to the number of atoms combined. For example: $ date=. 1,7,7,6 4 word=. 's','a','w' |. word |. date was 6 7 7 1 The verb |. used above is called reverse. The phrase s$b produces an array of shape s from the list b. For example: (3,4) $ date,1,8,6,7,1,9,1,7 1 7 7 6 1 8 6 7 1 9 1 7 table=. 2 3$ word,'bat' table $table saw 2 3 bat The number of atoms in the shape of a noun is called its rank. Each position of the shape is called an axis of the array, and axes are referred to by indices 0, 1, 2, etc. For example, axis 0 of table has length 2 and axis 1 has length 3. The last k axes of an array b determine rank-k cells or k-cells of b. The rest of the shape vector is called the frame of b relative to the cells of rank k; if $c is 2á3á4á5, then c has the frame 2 3 relative to cells of rank 2, the frame 2á3á4á5 relative to 0-cells (atoms), and an empty frame relative to 4-cells. If: ] b=.2 3 4 $ 'abcdefghijklmnopqrstuvwx' abcd efgh ijkl mnop qrst uvwx then the list abcd is a 1-cell of b, and the letters are each 0-cells. A cell of rank one less than the rank of b is called an item of b; an atom has one item, itself. For example, the verb from (denoted by {) selects items from its right argument, as in: 0{b 1{b 0{0{b abcd mnop abcd efgh qrst ijkl uvwx 2 1{0{b 1{2{0{b 0{3 ijkl j 3 efgh Moreover, the verb grade (denoted by /:) provides indices to { that bring items to lexical order. Thus: g=. /: n=. 4 3$3 1 4 2 7 9 3 2 0 n g g{n 3 1 4 1 0 3 2 2 7 9 2 7 9 3 1 4 3 2 0 3 1 4 3 1 4 3 2 0 Negative numbers, as in _2-cell and _1-cell (an item), are also used to refer to cells whose frames are of the length indicated by the magnitude of the number. For example, the list abcd may be referred to either as a _2-cell or as a 1-cell of b. Open and Boxed. The nouns discussed thus far are called open, to distinguish them from boxed nouns produced by the verb box denoted by < . The result of box is an atom, and boxed nouns are displayed in boxes. Box allows one to treat any array (such as the list of letters that represent a word) as a single entity, or atom. Thus: words=.(<'I'),(<'was'),(<'it') letters=. 'I was it' $words $letters 3 8 |. words |. letters õýýÃýýýÃýÀ ti saw I þitþwasþIþ ÁýýÂýýýÂýã 2 3$words,|.words õýýÃýýýÃýýÀ þI þwasþitþ ÈýýÊýýýÊýý³ þitþwasþI þ ÁýýÂýýýÂýýã B. VERBS Monads and Dyads. Verbs have two definitions, one for the monadic case (one argument), and one for the dyadic case. The dyadic definition applies if the verb is preceded by a suitable left argument, that is, any noun that is not itself an argument of a conjunction; otherwise the monadic definition applies. The monadic case of a verb is also called a monad, and we speak of the monad % used in the phrase %4, and of the dyad % used in 3%4. Either or both cases may have empty domains. Ranks of Verbs. The notion of verb rank is closely related to that of noun rank: a verb of rank k applies to each of the k-cells of its argument. For example (using the array b from Section A): ,b abcdefghijklmnopqrstuvwx ,"2 b ,"_1 b abcdefghijkl abcdefghijkl mnopqrstuvwx mnopqrstuvwx Since the verb ravel (denoted by ,) applies to its entire argument, its rank is said to be unbounded. The rank conjunction " used in the phrase ,"2 produces a related verb of rank 2 that ravels each of the 2-cells to produce a result of shape 2 by 12. The shape of a result is the frame (relative to the cells to which the verb applies) catenated with the shape produced by applying the verb to the individual cells. Commonly these individual shapes agree, but if not, they are first brought to a common rank by adding leading unit axes to any of lower rank, and are then brought to a common shape by padding with an appropriate fill element: space for a character array, 0 for a numeric array, and a boxed empty list for a boxed array. For example: i."0 s=. 2 3 4 >'I';'was';'here' 0 1 0 0 I 0 1 2 0 was 0 1 2 3 here The dyadic case of a verb has two ranks, governing the left and right arguments. For example: p=. 'abc' q=. 3 5$'wake read lamp ' p,"0 1 q awake bread clamp Finally, each verb has three intrinsic ranks: monadic, left, and right. The definition of any verb need specify only its behaviour on cells of the intrinsic ranks, and the extension to arguments of higher rank occurs systematically. The ranks of a verb merely place upper limits on the ranks of the cells to which it applies, and its domain may include arguments of lower rank. For example, matrix inverse (%.) has monadic rank 2, but treats degenerate cases of vector and scalar arguments as one-column matrices. Agreement. In the phrase p v q, the arguments of v must agree in the sense that their frames (relative to the ranks of v) must be a prefix of the other, as in p,"0 1 q above, and in the following examples: p," 1 1 q 3 4 5*i. 3 4 abcwake 0 3 6 9 abcread 16 20 24 28 abclamp 40 45 50 55 (i.3 4)*3 4 5 0 3 6 9 16 20 24 28 40 45 50 55 If a frame contains 0, the verb is applied to a cell of fills. For example: ($ #"2 i. 1 0 3 4);($ 2 3 %"1 i. 0 2) õýýýÃýýýÀ þ1 0þ0 2þ ÁýýýÂýýýã ($ $"2 i. 1 0 3 4);($ 2 3 %/"1 i. 0 4) õýýýýýÃýýýýýÀ þ1 0 2þ0 2 4þ ÁýýýýýÂýýýýýã C. ADVERBS AND CONJUNCTIONS Unlike verbs, adverbs and conjunctions have fixed valence: an adverb is monadic (applying to a single argument to its left), and a conjunction is dyadic. Conjunctions and adverbs apply to noun or verb arguments; a conjunction may produce as many as four distinct classes of results. For example, u&v produces a composition of the verbs u and v; and ^&2 produces the square by bonding the power function with the right argument 2; and 2&^ produces the function 2-to-the-power. The conjunction & may therefore be referred to by different names for the different cases, or it may be referred to by the single term and (or with), which roughly covers all cases. D. COMPARATIVES The comparison x=y is treated like the everyday use of equality (that is, with a reasonable relative tolerance), yielding 1 if the difference x-y falls relatively close to zero. Tolerant comparison also applies to other relations and to floor and ceiling (<. and >.); a precise definition is given in Part III under equal (=). An arbitrary tolerance t can be specified by using the fit conjunction (!.), as in x =!.t y. E. PARSING & EXECUTION A sentence is evaluated by executing its phrases in a sequence determined by the parsing rules of the language. For example, in the sentence 10%3+2, the phrase 3+2 is evaluated first to obtain a result that is then used to divide 10. In summary: 1. Execution proceeds from right to left, except that when a right parenthesis is encountered, the segment enclosed by it and its matching left parenthesis is executed, and its result replaces the entire segment and its enclosing parentheses. 2. Adverbs and conjunctions are executed before verbs; the phrase ,"2-a is equivalent to (,"2)-a, not to ,"(2-a). Moreover, the left argument of an adverb or conjunction is the entire verb phrase that precedes it. Thus, in the phrase +/ . */b, the rightmost adverb / applies to the verb derived from the phrase +/ . *, not to the verb *. 3. A verb is applied dyadically if possible; that is, if preceded by a noun that is not itself the right argument of a conjunction. 4. Certain trains form verbs, adverbs, and conjunctions, as described in º F. 5. To ensure that these summary parsing rules agree with the precise parsing rules prescribed below, it may be necessary to parenthesize any adverbial or conjunctival phrase that produces anything other than a noun or verb. One important consequence of these rules is that in an unparenthesized expression the right argument of any verb is the result of the entire phrase to the right of it. The sentence 3*p%q^|r-5 can therefore be read from left to right: the overall result is 3 times the result of the remaining phrase, which is the quotient of p and the part following the %, and so on. Parsing proceeds by moving successive elements (or their values in the case of pronouns and other names) from the tail end of a queue (initially the original sentence prefixed by a left marker º) to the top of a stack, and eventually executing some eligible portion of the stack and replacing it by the result of the execution. For example, if a=. 1 2 3, then b=.+/2*a would be parsed and executed as follows: º b =. + / 2 * a º b =. + / 2 * 1 2 3 º b =. + / 2 * 1 2 3 º b =. + / 2 * 1 2 3 º b =. + / 2 * 1 2 3 º b =. + / 2 4 6 º b =. + / 2 4 6 º b =. + / 2 4 6 º b =. 12 º b =. 12 º 12 º 12 The foregoing illustrates two points: 1) Execution of the phrase 2á*á1á2á3 is deferred until the next element (the /) is transferred; had it been a conjunction, the 2 would have been its argument, and the monad * would have applied to 1 2 3; and 2) Whereas the value of the name a moves to the stack, the name b (because it precedes a copula) moves unchanged, and the pronoun b is assigned the value 12. The executions in the stack are confined to the first four elements only, and eligibility for execution is determined only by the class of each element (noun, verb, etc., an unassigned name being treated as a verb), as prescribed in the following parse table. The classes of the first four elements of the stack are compared with the first four columns of the table, and the first row that agrees in all four columns is selected. The bold italic elements in the row are then subjected to the action shown in the final column, and are replaced by its result. If no row is satisfied, the next element is transferred from the queue. EDGE VERB NOUN ANY Monad EDGE+AVN VERB VERB NOUN Monad EDGE+AVN NOUN VERB NOUN Dyad EDGE+AVN VERB+NOUN ADV ANY Adverb EDGE+AVN VERB+NOUN CONJ VERB+NOUN Conj EDGE+AVN VERB VERB VERB Trident EDGE CAVN CAVN CAVN Trident EDGE CAVN CAVN ANY Bident NAME+NOUN ASGN CAVN ANY Is LPAR CAVN RPAR ANY Punct Legend: AVN denotes ADV+VERB+NOUN CAVN denotes CONJ+ADV+VERB+NOUN EDGE denotes MARK+ASGN+LPAR F. TRAINS An isolated sequence, such as (+ */), which the normal parsing rules (other than the three labelled trident and bident) do not resolve to a single part of speech is called a train, and may be further resolved as described below. Meanings are assigned to certain trains of two or three elements and, by implication, to trains of any length by repeated resolution. For example, the trains +-*% and +-*%^ are equivalent to +(-*%) and +-(*%^). A verb is produced by trains of three or two verbs, as defined by the following diagrams: FORK HOOK g g g g / \ / \ / \ / \ f h f h y h x h | | / \ / \ | | y y x y x y y y For example, 5(+*-)3 is (5+3)*(5-3), but if f is a cap ([:) the capped branch simplifies the forks to g h y and g x h y. The ranks of the hook and fork are infinite. Trains may also produce adverbs and conjunctions, and trains of two and three elements are called bidents, and tridents, respectively; hooks and forks are special cases. Tree displays illustrate the choice of the names fork and trident: mean=. +/ % # conj=. ]. , [. tree=. 5!:4@< tree 'mean' õý / ýýý + ýýÊý % Áý # tree 'conj' õý ]. ýýÊý , Áý [. The following tables define all possible tridents and bidents, using light italics to denote the optional left arguments of (ambivalent) verbs : N0 V1 N2 noun x V1 y V0 V1 V2 verb (x V0 y) V1 (x V2 y) V0 V1 C2 conj V0 V1 (x C2 y) A0 V1 V2 adv (x A0) V1 V2 C0 V1 V2 conj (x C0 y) V1 V2 C0 V1 C2 conj (x C0 y) V1 (x C2 y) A0 A1 V2 conj (x A0) (y A1) V2 A0 A1 A2 adv ((x A0) A1) A2 C0 A1 A2 conj ((x C0 y) A1) A2 N0 C1 N2 verb x (N0 C1 N2) y N0 C1 V2 verb x (N0 C1 V2) y N0 C1 A2 adv N0 C1 (x A2) N0 C1 C2 conj N0 C1 (x C2 y) V0 C1 N2 verb x (V0 C1 N2) y V0 C1 V2 verb x (V0 C1 V2) y V0 C1 A2 adv V0 C1 (x A2) V0 C1 C2 conj V0 C1 (x C2 y) A0 C1 N2 adv (x A0) C1 N2 A0 C1 V2 adv (x A0) C1 V2 A0 C1 A2 conj (x A0) C1 (y A2) A0 C1 C2 conj (x A0) C1 (x C2 y) C0 C1 N2 conj (x C0 y) C1 N2 C0 C1 V2 conj (x C0 y) C1 V2 C0 C1 A2 conj (x C0 y) C1 (y A2) C0 C1 C2 conj (x C0 y) C1 (x C2 y) N0 A1 verb x (N0 A1) y N0 C1 adv N0 C1 x V0 N1 noun V0 N1 V0 V1 verb x (or y) V0 V1 y V0 A1 verb x (V0 A1) y V0 C1 adv V0 C1 x A0 V1 adv (x A0) V1 A0 A1 adv (x A0) A1 A0 C1 adv (x A0) C1 x C0 N1 adv x C0 N1 C0 V1 adv x C0 V1 C0 A1 conj (x C0 y) A1 G. FRETS AND SCRIPTS Host systems commonly use the line-feed or carriage return characters 10{a. or 13{a. (or both together) as frets to mark divisions into individual lines. A character list provided with zero or more frets will be called a script. As detailed in the appendix, a script t may be filed and retrieved by expressions of the form t 1!:2 <'abc' and t=. 1!:1 <'abc', and may be executed by the expression 0!:11 <'abc'. Convenient entry of scripts is provided by the phrase 0 : 0; succeeding keystrokes are accepted as literal characters; the enter key that would normally terminate the entry is accepted as a fret; and the entry is terminated by a lone right parenthesis that is accepted as a final fret. For example: s=. 0 : 0 y.*%:y. : x.*!y.