J Operators (mainly) 19 Feb 1993 unary then binary form, if applicable NB. execution is RIGHT TO LEFT! eg. 3-2-1 --> 3-(2-1) --> 2 123456789 123456789 Constants: ---------- _ minus sign, part of number _3 minus three _ infinity 1%0 --> _ __ minus infinity _1%0 --> __ _. indeterminate _ - _ --> _. digit: eg. 7: yields the constant verb of infinite rank, 7: y --> 7 _: verb, yields _ _: y --> _ j imaginary part 1j2 + 3j4 --> 4j6 . decimal part 3.141593 'a string' Operators: ---------- + conjugate (_) + 1j2 --> 1j_2 + plus, addition (0 0) 3 + 2 --> 5 2 + i. 6 --> 2 3 4 5 6 7 +. real imag (_) +. 2j3 --> 2 3 +. GCD (or) (0 0) 12 +. 15 --> 3 +: double (_) +: 77 --> 154 +: not-or (0 0) 0+:0 --> 1 - negate (_) -2 --> _2 - subtract (0 0) 3-1 --> 2 3-2-1 --> 3-(2-1) --> 2 -. not (_) (1&-) -. 3 --> _2 (0 == false, 1 == true) -. less (_ _) set subtraction (i. 9) -. 2 4 5 8 --> 0 1 3 6 7 -: halve (_) -: i. 6 --> 0 0.5 1 1.5 2 2.5 -: match (_ _), yields 1 if arg's match in shape, boxing, elts, see = m -: m --> 1 where m =. i. 2 3 m -: |: m --> 0 * signum (_) * _2 --> _1 * multiply, times (0 0) 3 * 2 --> 6 *. polar *. 1j1 --> 1.41421 0.785398 *. LCM (and) 12 *. 15 --> 60 *: square *: 1.41 --> 1.9881 *: not-and 1 *: 1 --> 0 % reciprocal (_) % 3 --> 0.333333 % divide (0 0) 2 % 3 --> 0.666667 1 % 0 --> _ %. matrix inverse (2) % i. 2 2 --> _1.5 0.5 so (%.m) +/ . *m = id 1 0 %. matrix divide (_ 2) (for solution of linear equations) %: square root (_) %: 2 --> 1.41421 %: root (_ _) 3 %: 8 --> 2 ^ exp (_) ^2 --> e^2 --> 7.38906 ^ exponentiation, power (0 0) 3^2 --> 9 ^. natural log (_) ^. 2 --> 0.693147 ^. log (0 0) 2 ^. 1024 --> 10 ^: u ^: n conjunction POWER (_) 1st case numeric n u ^: n is apply u n times, if n<0 the obverse of u is used, if n is infinite, _, produces limit of application of u. (*: ^: 2) 4 --> *: 16 --> 256 (*: ^: _1) 4 --> %: 4 --> 2 (%: ^: _) 10 --> 1 2nd case n is a gerund, see dictionary CHAIN (_) x (u ^: n) y if x u ^: n y is Rn, R0 == x R1 ==y then Rn == Rn-2 u Rn-1 if n is infinite iterates until Rn=Rn+1 and Rn+2=Rn+3 ^: u ^: v conjunction POWER (_) 'u^:(v y.)y.' : '' ^:_ CHAIN (_ _) '' : 'x. & u ^: (x. & v) y.' = self classify (_) =y compares the nub of y, ~. y, to y produces a table = 1 2 1 3 --> 1 0 1 0 0 1 0 0 0 0 0 1 = equals (0 0) 1=2 --> 0 (false) NB. 'fred' = 'fred' --> 1 1 1 1 'fred' -: 'fred' --> 1 also see match -: normal tolerance is 2^_44 =. COPULA, is, define (local) seven =.7 NB. indirection x =. 'y';'z' (x) =. 1 2; 3 4 5 y --> 1 2 z --> 3 4 5 =: COPULA, is, define (global) < box (_), see open > <(i. 3) --> +-----+ |0 1 2| +-----+ < less than (0 0) 1<2 --> 1 (true) <. floor (_) <. _2.3 --> _3 <. lesser of (0 0) 2<.3 --> 2 <: decrement (_) <: i. 6 --> _1 0 1 2 3 4 <: less or eq (0 0) 2<:3 --> 1 (true) > open (0), inverse of box < ie. x -: > greater than (0 0) (i.6) > (i._6) --> 0 0 0 1 1 1 >. ceiling (_) >. _2.3 --> _2 >. larger of (0 0) 2>.3 --> 3 >: increment (_) >:3 --> 4 >: greater or eq (0 0) 2>:3 --> 0 (false) | abs, magnitude (_) | _2 --> 2 | 3j4 --> 5 | mod, residue (0 0) 5|23 --> 3 |. reverse (_) |. i. 6 --> 5 4 3 2 1 0 |. 'fred' --> 'derf' |. rotate (0 _) 2 |. i. 6 --> 2 3 4 5 0 1 |: transpose (_) |: i. 2 2 --> 0 2 1 3 |: transpose (1 _) x |: y makes x-axis of y last 0 |: i. 2 3 --> 0 3 1 4 2 5 $ shape of (_) $ i. 6 --> 6 $ 'fred' --> 4 $ < 'fred' --> (boxed, it's an atom!) $ i. 2 3 --> 2 3 $ shape (1 _) 4$i.6 --> 0 1 2 3 $. suite, see EXPLICIT DEFINITION the boxed sentences are executed in a sequence determined by $. . $: self reference, for anonymous recursive definitions eg. 1: `(* $: @ <:) @. * is the factorial function # tally (_) # 2 3 4 5 --> 4 # copy (1 _) 3 # 1 2 3 4 --> 1 1 1 2 2 2 3 3 3 4 4 4 2 3 # 4 5 --> 4 4 5 5 5 #. base 2 (1), # y == 2 #. y #. base (1 1) 10 #. 1 2 3 --> 123 #: antibase 2 (_) #: 123 --> 1 1 1 1 0 1 1 #: antibase (1 0) ~ EVOKE (_) adverb if m is a proverb then 'm'~y == m y 'fact'~ 3 --> fact 3 (eg. where fact =. !) ~ REFLEX (_) adverb u~ y == y u y ^~ 4 --> 4^4 --> 256 ~ PASS, PASSIVE 4 %~ 5 --> 5%4 --> 1.25 3 -~ 2 --> 2-3 --> _1 ~. nub (_) (unique elts) ~. 1,2,1,3,2,1,3 --> 1 2 3 ~: nub sieve (_) ~: 1,2,1,3,2,1,3 --> 1 1 0 1 0 0 0 ~: not equal (0 0) 1 ~: 2 --> 1 / INSERT or REDUCE (_) adverb */ 1 2 3 4 5 --> 120 +/ i. 2 3 --> 3 5 7 mean =. +/ % # / TABLE (_ _) (i. 2)+/ i. 3 --> 0 1 2 1 2 3 /. OBLIQUE (_) adverb , u /. y apply u to oblique (minor diagonals) of table y +/ /. m --> 0 4 6 5 where m == 0 1 2 3 4 5 /. KEY (_ _) x u/. y == (=x) u@# y items of specify keys of items in y u is pplied to each collection of y having identical keys eg. 1 2 3 1 3 2 2 1 'adg';bf';'ce' /: grade up (_) /: 'fred' --> 3 2 0 1 (ie. defr) /: sort (_ _) \ TRAIN, m\ == train of verbs represented by gerund m \ PREFIX (_) +/\ i. 5 --> 0 1 3 6 10 (partial sums, +/) <\ 'abc' --> +-+--+---+ |a|ab|abc| +-+--+---+ !\ 1 2 3 --> 1 0 0 1 2 0 1 2 6 \ INFIX (0 _) 3 +/\ 1 2 3 4 5 --> (+/ 1 2 3) (+/ 2 3 4) (+/ 3 4 5) --> 6 9 12 \. SUFFIX (_) +/\. i. 5 --> 10 10 9 7 4 (partial rev' sums) \. OUTFIX (0 _) \: grade down (_) see /: \: sort (_ _) see /: @ ATOP conjunction u@v y == u &v y == u v y and x u@v y == u x v y -@! 3 --> -!3 --> _6 3 !@- i. 2 --> ! 3 - i. 2 --> ! 3 2 --> 6 2 @. AGENDA conjunction -`+`^`!`* @. [ 3 --> 6 @: AT conjunction == @ except that ranks are infinite & bond with (_), conjunction (3&*) 2 --> 6 log10 =. 10&^. lessthan6 =. <&6 & compose, conjunction u&v y == u v y and x u&v y == (v x)u(v y) 6 (%&!) 4 --> (!6)%(!4) --> 30 &. UNDER (DUAL) conjunction equivalent to composition u&v except that obverse of v is applied to the result of each cell. &: APPOSE equivalent to &, but ranks are infinite " constant conjunction " RANK conjunction u"v ,"2 (2 3 4 $ i. 24) --> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 +/"1 i. 2 3 --> 3 12 +/"2 i. 2 3 --> 3 5 7 (= +/ i. 2 3) ". do, execute (1) ". '4%5' --> 4%5 --> 0.8 1 + ". '1.3' --> 2.3 (string to number ***) ". do left if error (1 1) ": format (_) see dictionary 5 ": i. 2 3 --> 0 1 2 3 4 5 4.1 ": 1 % i. 2 3 --> _ 1.0 0.5 0.3 0.2 0.2 ` TIE (GERUND) (,`+)/ 1+i. 11 --> 1,2+3,4+5,6+7,8+9,10+11 --> 1,2+3,4+5,6+7,8+9,21 --> 1,2+3,4+5,6+7,17,29 --> 1,2+3,4+5,13,23,35 --> 1,2+3,9,17,27,39 --> 1,5,11,19,29,41 `: EVOKE GERUND , ravel (_) , i. 2 3 --> 0 1 2 3 4 5 , append (_ _) 5 , i. 6 --> 5 0 1 2 3 4 5 ,. ravel items (_), make 2-D table by ravelling items of arg' ,. append (_ _), == ,"_1 ,: itemize (_), add a single unit axis to y, increasing the rank by 1 shape of ,: y is 1,$y ,: laminate (_ _) itemize and catenate (,: x),(,: y) ; raze (_) ;y assembles along a leading axis the opened elts of ,y ; ;: '1+23' --> 1+23 ; link (_ _), x;y == ( +------+--------+ useful for |x =. 7|y =. x+1| fns, see : +------+--------+ explicit defn ;. CUT (_) u ;. n conjunction various ways to cut a list and operate on the intervals ;. CUT (_ _) u ;. n various ... ;: words (1) ;: '1+23' --> +-+-+--+ |1|+|23| +-+-+--+ : EXPLICIT DEFINITION (_ _ _) (think f y and x f y, see x. y. and $.) eg. f =. '2 f y.' : 'y. ^ % x.' then f 64 --> 8 and 3 f 64 --> 4 NB. see file on J Procedures :. OBVERSE conjunction result of u :. v is the verb u but with assigned obverse v (used as inverse in &. and ^:) :: ADVERSE conjunction result of u:: v is result of u if no error, else result of v ({ :: +/) 1.2 3.4 --> 4.6 . DET DETERMINANT (2) conjunction (-/ . *) mat is the determinant of matrix mat (-/ . *) (1 + i. 2 2) --> |1 2| --> 4-2 --> _2 |3 4| . DOT PRODUCT (_ _) vector1 (+/ . *) vector2 is the dot product of vector1 and vector2 .. EVEN conjunction u .. v == u -: @: + u&v .: ODD conjunction u .: v == u -: @: - u&v ! factorial (_) !4 --> 24 ! out of, combinations (0 0) x!y == num' of ways to choose x out of y, (!y)%(!x)*(!y-x) !. FIT (CUSTOMIZE) conjunction: modifies certain verbs as spec' in verb's defn !: FOREIGN conjunction: operating system escape, also kbrd and screen eg. 0!:2 < 'zzz' will read and execute contents of file zzz. ? roll (_), random number generator, ?n --> numbers >=0 and deal (random) m from n without replacement [ same (_) [3 --> 3 [ left (_ _) 1[2 --> 1 [. LEV conjunction +[.- --> + (![.%)3 --> !3 --> 6 ] same (_) ]3 --> 3 ] right(_ _) 1]2 --> 2 ]. DEX conjunction +].- --> - (!].%)3 --> %3 --> 0.3333 { catalogue (1) { 'ab';'cde' --> +--+--+--+ |ac|ad|ae| +--+--+--+ |bc|bd|be| +--+--+--+ { from (0 _) 1 { 4 6 2 8 --> 6 (numbered from 0) {. head (_) {. 4 6 2 8 --> 4 {. take (1 _) 2 {. 4 6 2 8 --> 4 6 first 2 _2 {. 4 6 2 8 --> 2 8 last 2 {: tail (_) (last!!!) {: 4 6 2 8 --> 8 } ITEM AMEND (_) adverb } AMEND (_ _) }. behead (_) }. 4 6 2 8 --> 6 2 8 }. drop (0 _) 2 }. 4 6 2 8 --> 2 8 }: curtail (_) }: 4 6 2 8 --> 4 6 2 ) label, see : a. alphabet noun, length is 256, NB not all are printable A. atomic permute (1 0) A. permute 0 A. 1 2 3 --> 1 2 3 1 A. 1 2 3 --> 1 3 2 b. BOOLEAN adverb (_), m b. y == 0 m b. y b. BOOLEAN adverb (0 0) m b. yields the m-th boolean fn BASE CHARACTERISTICS (_) u b. _1 gives obverse of u u b. 0 gives ranks of u u b. 1 gives identity fn of u c. characteristic (2) C. cyc-dir (0 2) C. perm (1) C. permute (1 _) D. DERIVATIVE conjunction, u D. n is the nth derivative of u e. raze in e. member (in) 2 e. 1 2 3 --> 1 (true) E. member of interval f. FIX VERB i. integers, iota (1) i. 6 --> 0 1 2 3 4 5 1. 2 3 --> 0 1 2 3 4 5 i. -6 --> 5 4 3 2 1 0 i. index of (_ _) 2 i. (0 1 2 3 2 1 0) --> 1 1 0 1 0 1 1 j. imaginary j. 1 --> 0j1 j. complex 1 j. 2 --> 1j2 NB. COMMENT o. pi times o. 1 --> 3.14159 o. circle p. polynomial r. polar, angle r. complex x. Left Argument of function y. Right Argument of function 0: 1: ... 9: verbs yielding 0 1 ... 9 respectively !: FOREIGN CONJUNCTIONS (selected). ----------------------------------- 0!:2 y j reads and executes file y eg. 0!:2 < 'zzz' eg. (<'logfile') 0!:2 < 'script' J reads and executes file script and appends results to file logfile. 1!:1 y file read eg. f =. 1!:1 < 'zzz' read contents of file zzz into f x 1!:2 y file (re)write eg. 'abc' 1!:2 <'opfile' (re)writes abc to opfile x 1!:3 y file append