Simple arithmetic
q)2+2
4
q)2-3
-1
q)2*3+4
14
q)(2*3)+4
10
q)3%4
0.75
q){x*x}4
16
q)sqrt 4
2.0
q)reciprocal 4
0.25
Operations using lists
q)2*1 2 3
2 4 6
q)1 2 3%2 4 6
0.5 0.5 0.5
q)count 1 2 3
3
q)3#1
1 1 1
q)5#1 2
1 2 1 2 1
List items
q)first 1 2 3
1
q)last 1 2 3
3
q)1_1 2 3
2 3
q)-1_1 2 3
1 2
q)reverse 1 2 3
3 2 1
Indexing and sorting
q)1 2 3@1
2
q)1 2 3@1 0
2 1
q)til 3
0 1 2
q)2 4 6?4
1
q)iasc 2 1 6
1 0 2
q)asc 2 1 6
`s#1 2 6
List aggregation
q)1 2 3,10 20
1 2 3 10 20
q)1+2+3
6
q)sum 1 2 3
6
q)sums 1 2 3
1 3 6
q)1,(1+2),(1+2+3)
1 3 6
q){1_x+prev x}til 5
1 3 5 7
q)sum each{(2*til ceiling .5*count x)_x}1 2 3 4 5
3 7 5
q)(1 2;3 4 6;7 6)
(1 2;3 4 6;7 6)
q)first(3 4 6;7 6)
3 4 6
Function combinations
q){x+x*x}4
20
q)(sqrt;{x*x})@\:4
(2f;16)
q){x*x}sum 2 3
25
q)sum{x*x}2 3
13
q){sum(x*x),2*/x}2 3
25
q)sqrt sum{x*x}3 4
5f
User-defined functions and arguments
q)d1:- / binary projection
q)d2:{x-y} / explicit binary
q)m1:neg / unary projection
q)m2:0- / unary projection
q)m3:{neg x} / explicit unary
q)(m1;m2;m3)@\:4 / unary functions
-4 -4 -4
q)(d1;d2).\:3 4 / binary functions
-1 -1
Exponent and logarithm
q)(e;2*e;e*e:exp 1)
2.718282 5.436564 7.389056
q)exp 2
7.389056
q)2 xexp 16
65536.0
q)log exp 2
2.0
q)2 xlog 65536
16.0
Trigonometry
q)a:(pi;2*pi;pi*pi:acos -1)
3.141593 6.283185 9.869604
q)cos pi
-1.0
q)(t:sum{x*x}@(cos;sin)@\:)pi
1.0
q)t a
1 1 1.0
Matrixes
q)1 2 3*/:1 2 3
(1 2 3;2 4 6;3 6 9)
q){x=/:x}@til 3
(100b;010b;001b)
q)2 3#til 6
(0 1 2;3 4 5)
q)2 2#0 1 1 1
(0 1;1 1)
Structural transforms
q)show N:0 3_/:2 6#til 12
0 1 2 3 4 5
6 7 8 9 10 11
q)raze/[N]
0 1 2 3 4 5 6 7 8 9 10 11
q)raze each N
(0 1 2 3 4 5;6 7 8 9 10 11)
q)show M:3 3#"ABC123!@#"
"ABC"
"123"
"!@#"
q)(::;flip;reverse;reverse each;1 rotate)@\:M
"ABC" "123" "!@#"
"A1!" "B2@" "C3#"
"!@#" "123" "ABC"
"CBA" "321" "#@!"
"123" "!@#" "ABC"
q)M ./:/:f value group sum each f:n cross n:til 3
,"A"
"B1"
"C2!"
"3@"
,"#"
q)M ./:a,'a:til count M
"A2#"
Selection
q)N:((0 1 2;3 4 5);(6 7 8;9 10 11))
q)((N 1) 1) 1
10
q)3@[;1]/N
10
q)N[1;1;1]
10
q)N . 1 1 1
10
Factorial and binomial
q)each[f:{$[x<0;0;prd 1.+til x]}]1+til 5 / factorial
1 2 6 24 120.0
q)prds 1+til 5
1 2 6 24 120
q)(b:{til[x]{$[x<y;0;floor f[x]%f[y]*f x-y]}\:/:til x})5 / binomial coeff.
(1 1 1 1 1;0 1 2 3 4;0 0 1 3 6;0 0 0 1 4;0 0 0 0 1)
q)/ fibonacci: sum of second diagonal of binomial matrix
q)1_{sum b[x]./:flip(til x;reverse til x)}each til 16
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
Dot product
q)1 2 3 wsum 1 2 3
14f
q)1 2 3.$1 2 3.
14f
q)M:(0 1.;1 1.)
q)M$M
(1 1.;1 2.)
q)15$[M]/M / matrix to the power of 15, also fibonacci
(610 987.;987 1597.)
q)(14$[M]\M)[;0;1]
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610f
Randomness and probability
q)A:5?1.;A
0.03505812 0.7834427 0.7999031 0.9046515 0.2232866
q)B:10?2;B
1 1 1 0 1 0 1 1 0 0
q)B1:10?0b;B1
11110010101b
q)C:-3?3;C / deal 3 unique cards out of 3
1 0 2
q)(min;max)@\:A
0.03505812 0.9046515
q)B?0 / first zero
3
q)avg C~/:1_10000{-3?3}\() / method monte carlo
0.1643836
q)reciprocal f 3 / exact probability of 3 cards in given order
0.1666667
Unique elements
q)D:distinct S:"mississippi" / distinct items
"misp"
q)K:D?S;K / find (?) indexes
0 1 2 2 1 2 2 1 3 3 1
q)S value group K / group by key
(enlist"m";"iiii";"ssss";"pp")
q)count each group S / frequencies
"misp"!1 4 4 2
q)I:(til count S)in first each group S;I
11100000100b
q)S where I / filter by sieve to get D
"misp"
q)sum D=/:S / where items of D are in S
1 4 4 2
作者:Based on J by Example 06/11/2005 © Oleg Kobchenko