NB. J BY EXAMPLE /K by EXAMPLE NB. J is product of JSoftware Inc. http://jsoftware.com / K is product of Kx Inc. http://kx.com NB. v3 07/04/2005 (C) Oleg Kobchenko http://olegykj.sourceforge.net / 2005.06.29. Attila Vrabecz (VrAbi) http://vrabi.web.elte.hu/k /based on J by EXAMPLE by / 06/11/2005 (C) Oleg Kobchenko http://olegykj.sourceforge.net NB. simple arithmetic =============================================== /simple arithmetic =============================================== 2 + 2 NB. comment is 'NB.' 2+2 /comment is ' /': left of /: whitespace or nothing 4 4 2 - 3 NB. negative numbers use '_' 2-3 /negative numbers _1 -1 2 * 3 + 4 NB. no precedence, right to left 2*3+4 /no precedence, right to left 14 14 (2 * 3) + 4 NB. parentheses changes order (2*3) + 4 /parentheses changes order 10 10 3 % 4 NB. division represented by '%' 3%4 /division represented by '%' 0.75 0.75 *: 4 NB. square _sqr 4 /square 16 16.0 %: 4 NB. square root _sqrt 4 /square root 2 2.0 % 4 NB. 1/x %4 /1/x 0.25 0.25 NB. operations using lists ========================================== /operations using lists ========================================== 2 * 1 2 3 NB. numeric list with space separators 2*1 2 3 /numeric list with space separators 2 4 6 2 4 6 1 2 3 % 2 4 6 NB. list to list operations, same size 1 2 3%2 4 6 /list to list operations, same size 0.5 0.5 0.5 0.5 0.5 0.5 #1 2 3 NB. size of vector #1 2 3 /size of vector 3 3 3$1 NB. generate sequence of same numbers 3#1 /generate sequence of same numbers 1 1 1 1 1 1 5$1 2 NB. or from a list of given elements 5#1 2 /or from a list of given elements 1 2 1 2 1 1 2 1 2 1 NB. list elements =================================================== / list elements =================================================== {.1 2 3 NB. first element *1 2 3 /first element 1 1 {:1 2 3 NB. last element *|1 2 3 /last element 3 3 }.1 2 3 NB. rest without first element 1_1 2 3 /rest without first element 2 3 2 3 }:1 2 3 NB. rest without last element -1_1 2 3 /rest without last element 1 2 1 2 |.1 2 3 NB. reverse |1 2 3 /reverse 3 2 1 3 2 1 NB. indexing and sorting ============================================ / indexing and sorting ============================================ 1{1 2 3 NB. indexing is zero-based 1 2 3@1 /indexing is zero-based 2 2 1 0{1 2 3 NB. index can be vector too 1 2 3@1 0 /index can be vector too 2 1 2 1 i.3 NB. generate zero-based sequence !3 /generate zero-based sequence 0 1 2 0 1 2 2 4 6 i. 4 NB. index of given element(s) 2 4 6?4 /index of given element(s) 1 1 /:2 1 6 NB. indices of sorted order <2 1 6 /indices of sorted order 1 0 2 1 0 2 /:~2 1 6 NB. sort vector {x@ y F y 1 2 6 NB. list aggregation ================================================ / list aggregation ================================================ 1 2 3,10 20 NB. join vectors 1 2 3,10 20 /join vectors 1 2 3 10 20 1 2 3 10 20 1 + 2 + 3 NB. sum of elements 1 + 2 + 3 /sum of elements 6 6 +/1 2 3 NB. insert '+' between elements +/1 2 3 /insert '+' between elements 6 6 +/\1 2 3 NB. running sum of elements +\1 2 3 /running sum of elements 1 3 6 1 3 6 1,(1+2),(1+2+3) NB. same as this 1,(1+2),(1+2+3) /same as this 1 3 6 1 3 6 2+/\1 2 3 4 5 NB. sum or running pairs +':1 2 3 4 5 /sum running pairs 3 5 7 9 3 5 7 9 _2+/\1 2 3 4 5 NB. non-intersecting pairs +/'{(2*!-_-.5*#x)_ x}1 2 3 4 5 /non-intersecting pairs 3 7 5 3 7 5 (<1 2),3 4 6;7 6 NB. < is boxing, ; is box and join (1 2;3 4 6;7 6) /list +---+-----+---+ (1 2 |1 2|3 4 6|7 6| 3 4 6 +---+-----+---+ 7 6) >{. 3 4 6;7 6 NB. > is unboxing *(3 4 6;7 6) /first item in the list 3 4 6 3 4 6 NB. function combinations =========================================== / function combinations =========================================== (+ *:) 4 NB. hook (F G) y <=> y F (G y) {x+_sqr x}4 /a + a^2 20 NB. a + a^2 20.0 (%: , *:) 4 NB. fork (F G H) y <=> (F y) G (H y) (_sqrt;_sqr)@\:4 /[sqrt(a), a^2] 2 16 NB. [sqrt(a), a^2] 2 16.0 *:@(+/) 2 3 NB. composition (F o G) y <=> F G y _sqr+/2 3 /(a +b)^2 25 NB. (a + b)^2 25.0 2 +&*: 3 NB. x F & G y <=> (G x) F (G y) +/_sqr 2 3 /a^2 + b^2 13 NB. a^2 + b^2 13.0 2 (+&*: + 2: * *) 3 NB. (a + b)^2 = a^2 + b^2 + 2ab {+/(_sqr x),2*/x}2 3 /(a + b)^2 = a^2 + b^2 + 2ab 25 NB. 0: 1: 2: ... are const functions 25.0 3 +&.*: 4 NB. F&.G y <=> (G^:_1) F G y _sqrt+/_sqr 3 4 /sqrt(a^2 + b^2) 5 NB. sqrt(a^2 + b^2) 5.0 NB. user defined functions and arguments ============================ / user defined functions and arguments ============================ m1=: - NB. ambivalent tacit d1:- /dyadic projection m2=: 3 : '-y.' NB. monadic explicit d2:{x-y} /explicit dyad m3=: 4 : 'x.-y.' NB. dyadic explcit m1:-: /monadic projection m2:0- /monadic projection (m1 , m2 , 0&m3) 4 NB. monadic use, 0& is bonding m3:{-x} /explicit monad _4 _4 _4 3 (m1 , (+ m2) , m3) 4 NB. dyadic use, hook for dyadization (m1;m2;m3)@\:4 / monads _1 _1 _1 -4 -4 -4 (m1 , m3) / 3 4 NB. distribute arguments: dyadization (d1;d2).\:3 4 / dyads _1 _1 -1 -1 3 (m1 , m4) @ , 4 NB. collect arguments: monadization _3 _4 _3 _4 NB. exponent and logarithm ========================================== / exponent and logarithm ========================================== 1x1 2x1 1x2 NB. e, 2e, e squared (e;2*e;_sqr e:_exp 1) /e, 2e, e squared 2.71828 5.43656 7.38906 2.718282 5.436564 7.389056 ^2 NB. exponent, e^2 _exp 2 /exponent, e^2 7.38906 7.389056 2^16 NB. exponent base 2, 2^16 2^16 /exponent base 2, 2^16 65536 65536.0 ^. 1x2 NB. logarithm, ln e^2 _log _exp 2 /logarithm, ln e^2 2 2.0 2^.65536 NB. logarithm base 2, log2 65536 _log[65536]%_log[2] /logarithm base 2, log2 65536 16 16.0 NB. trigonometry ==================================================== / trigonometry ==================================================== 1p1 2p1 1p2 NB. pi, 2 pi, pi squared :a:(pi;2*pi;_sqr pi:_acos-1) /pi, 2 pi, pi squared 3.14159 6.28319 9.8696 3.141593 6.283185 9.869604 load'trig' NB. load trigonometry library _cos pi /cosine of pi cos 1p1 NB. cosine of pi -1.0 _1 (t:+/_sqr(_cos;_sin)@\:)pi /theorem of trigonometry (*:cos 1p1) + *:sin 1p1 NB. theorem of trigonometry 1.0 1 t a /test theorem at angles (cos +&*: sin) 1 2p1 1p2 NB. same using fork and & 1 1 1.0 1 1 1 NB. matrices ======================================================== / matrices ======================================================== 1 2 3 */ 1 2 3 NB. outer product: multiplication table 1 2 3*/:1 2 3 /outer product: multiplication table 1 2 3 NB. same as */~ 1 2 3 (1 2 3 2 4 6 2 4 6 3 6 9 3 6 9) =/~i.3 NB. identity matrix, also =@i. (self-classify) {x=/:x}@!3 /identity matrix 1 0 0 NB. F~y <=> y F y (1 0 0 0 1 0 0 1 0 0 0 1 0 0 1) ]M=. i.2 3 NB. generate matrix 2 3#!6 /generate matrix 0 1 2 (0 1 2 3 4 5 3 4 5) 2 2$0 1 1 1 NB. reshape given vector to matrix 2 2#0 1 1 1 /reshape given vector to matrix 0 1 (0 1 1 1 1 1) NB. structural transforms =========================================== / structural transforms =========================================== ,N=: i.2 2 3 NB. ravel: list of atoms ,//N:2 2 3#!12 /ravel: list of atoms 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 ,"2 N NB. ravel each sub-matrix ,/'N /ravel each sub-matrix 0 1 2 3 4 5 (0 1 2 3 4 5 6 7 8 9 10 11 6 7 8 9 10 11) M:3 3#"ABC123!@#" /character matrix (]; |:; |.;|."1;1&|.) M=. 3 3$'ABC123!@#' NB. character matrix (:: ;+: ;|: ;|:'; 1!)@\:M +---+---+---+---+---+ NB. ] returns argument (("ABC" / :: returns argument |ABC|A1!|!@#|CBA|123| NB. |: transposes "123" |123|B2@|123|321|!@#| NB. |. reverses outer list "!@#") |!@#|C3#|ABC|#@!|ABC| NB. |."1 reverses inner list ("A1!" / +: transposes +---+---+---+---+---+ NB. 1|. rotates outer list "B2@" "C3#") ;:^:_1 ./) A NB. min and max over the list (&/;|/)@\:A /min and max over the list 0.0101683 0.57708 0.03505812 0.9046515 B i. 0 NB. first zero B?0 /first zero 3 3 (+/ % #) C-:"1 (?~"0) 10000#3 NB. method monte carlo {(+/x)%#x}C~/:10000{3_draw-3}\_n / method monte carlo 0.1637 NB. -: is list equality, F"n is rank modifier 0.1643836 %!3 NB. exact probability of 3 cards in given order %f 3 /exact probability of 3 cards in given order 0.166667 0.1666667 NB. unique elements ================================================= / unique elements ================================================= ]D=.~. S=. 'mississippi' NB. distinct (nub) :D:?S:"mississippi" /?: is unique misp "misp" ]K=. D i. S NB. key (index) :K:D?/:S /find (?) indexes 0 1 2 2 1 2 2 1 3 3 1 0 1 2 2 1 2 2 1 3 3 1 K