New York kdb Training 2019
Spaces still available. Register now to learn Kdb+.

kdb Math Functions

kdb provides the following math functions:

Math Function List

symdescriptionsyntaxeg
absReturns the absolute value of a number. i.e. the positive valueabs Xabs -2 -1 0 1
expThis function computes e to the power of x, where e is the base of the natural logarithms. Null is returned if the argument is null.R:exp 1 
fliptransposes its argument, which may be a list of lists, a dictionary or a table. flip (`a`b`c; 1 2 3)
invinv computes the inverse of a non-singular floating point matrix.r:inv xinv (3 3#2 4 8 3 5 6 0 7 1f)
maxThe max function returns the maximum of its argument. If the argument is a list, it returns the maximum of the list. The list may be any datatype except symbol. Nulls are ignored, except that if the argument has only nulls, the result is negative infinity. max Lmax 1 2 3 10 3 2 1
maxsThe maxs function returns the maximums of the prefixes of its argument. If the argument is a list, it returns the maximums of the prefixes of the list. The list may be any datatype except symbol. Nulls are ignored, except that initial nulls are returned as negative infinity. maxs Lmaxs 1 2 3 10 3 2 1
mcountThe mcount verb returns the N-item moving count of the non-null items of its numeric right argument. The first N items of the result are the counts so far, and thereafter the result is the moving count.r:N mcount L3 mcount 0N 1 2 3 0N 5
medComputes the median of a numeric list.r:med Lmed 10 34 23 123 5 56
mmummu computes the matrix multiplication of floating point matrices. The arguments must be floating point and must conform in the usual way, i.e. the columns of x must equal the rows of y.r:x mmu y(2 4#2 4 8 3 5 6 0 7f) mmu (4 3#`float$til 12)
modx mod y returns the remainder of x%y. Applies to numeric types, and gives type error on sym, char and temporal types.r:L mod N-3 -2 -1 0 1 2 3 4 mod 3
negThe function neg negates its argument, e.g. neg 3 is -3. Applies to all data types except sym and char. Applies item-wise to lists, dict values and table columns. r:neg Xneg -1 0 1 2
prdAggregation function, also called multiply over, applies to all numeric data types. It returns a type error with symbol, character and temporal types. prd always returns an atom and in the case of application to an atom returns the argument.ra:prd Lprd 2 4 5 6
randIf X is an atom 0, it returns a random value of the same type in the range of that type:r: rand 0rand each 3#0h
randIf X is a positive number, it returns a random number of the same type in the range [0,X)r: rand arand 100
randIf X is a list, it returns a random element from the list:r:rand Lrand 1 30 45 32
ratiosThe uniform function ratios returns the ratio of consecutive pairs. It applies to all numeric data types.r:ratios Lratios 1 2 4 6 7 10
reciprocalReturns the reciprocal of its argument. The argument is first cast to float, and the result is float.r:reciprocal Xreciprocal 0 0w 0n 3 10
signumThe function signum returns -1, 0 or 1 if the argument is negative, zero or positive respectively. Applies item-wise to lists, dictionaries and tables, and to all data types except symbol.r:signum Xsignum -2 0 1 3
sqrtReturn the square toor of an atom or list.sqrt 4 9 16 
sumReturn the total sum value of a listSum 5 5 10 
varAggregation function which applies to a list of numerical types and returns the variance of the list. Again for the usual reason it works on the temporal types.r:var Xvar 10 343 232 55
whereWhere the argument is a boolean list, this returns the indices of the 1's where 21 2 5 11 9>15
wsumThe weighted sum aggregation function wsum produces the sum of the items of its right argument weighted by the items of its left argument. The left argument can be a scalar, or a list of equal length to the right argument. When both arguments are integer lists, they are converted to floating point. Any null elements in the arguments are excluded from the calculation. 2 3 4 wsum 1 2 4
xexpThis is the dyadic power function.r:xexp[X;Y]2 xexp 3

kdb Moving Functions - Moving Sum, Moving Maximum..

symdescriptionsyntaxeg
mdevThe mdev verb returns the N-item moving deviation of its numeric right argument, with any nulls after the first element replaced by zero. The first N items of the result are the deviations of the terms so far, and thereafter the result is the moving deviation. The result is floating point.r:N mdev L2 mdev 1 2 3 5 7 10
mmaxThe mmax verb returns the N-item moving maximum of its numeric right argument, with nulls after the first replaced by the preceding maximum. The first N items of the result are the maximums of the terms so far, and thereafter the result is the moving maximum.r:N mmax L3 mmax 2 7 1 3 5 2 8
mminThe moving minimum.r:N mmin L2 mmin 2 7 1 3 5 2 8
msumThe msum verb returns the N-item moving sum of its numeric right argument, with nulls replaced by zero. The first N items of the result are the sums of the terms so far, and thereafter the result is the moving sum.r:N msum L3 msum 1 2 3 5 7 11

kdb Set Operations

symdescriptionsyntaxeg
allFunction all returns a boolean atom 1b if all values in its argument are non-zero, and otherwise 0b.It applies to all data types except symbol, first converting the type to boolean if necessary. r:all Aall 1 2 3=1 2 4
crossReturns the cross product (i.e. all possible combinations) of its arguments.R:X cross Y 
exceptReturns all elements of its left argument that are not in its right argument. x except y1 3 2 4 except 1 2
interReturns all elements common to both argumentsx inter y1 3 2 4 inter 1 2 6 7
unionDyadic function which returns the union of its arguments, i.e. returns the distinct elements of the combined lists respecting the order of the union.R:X union Y1 2 3 union 2 4 6 8