# kdb Math Functions

kdb provides the following math functions:

## Math Function List

sym | description | syntax | eg |
---|---|---|---|

abs | Returns the absolute value of a number. i.e. the positive value | abs X | abs -2 -1 0 1 |

exp | This 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 | |

flip | transposes its argument, which may be a list of lists, a dictionary or a table. | flip (`a`b`c; 1 2 3) | |

inv | inv computes the inverse of a non-singular floating point matrix. | r:inv x | inv (3 3#2 4 8 3 5 6 0 7 1f) |

max | The 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 L | max 1 2 3 10 3 2 1 |

maxs | The 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 L | maxs 1 2 3 10 3 2 1 |

mcount | The 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 L | 3 mcount 0N 1 2 3 0N 5 |

med | Computes the median of a numeric list. | r:med L | med 10 34 23 123 5 56 |

mmu | mmu 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) |

mod | x 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 |

neg | The 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 X | neg -1 0 1 2 |

prd | Aggregation 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 L | prd 2 4 5 6 |

rand | If X is an atom 0, it returns a random value of the same type in the range of that type: | r: rand 0 | rand each 3#0h |

rand | If X is a positive number, it returns a random number of the same type in the range [0,X) | r: rand a | rand 100 |

rand | If X is a list, it returns a random element from the list: | r:rand L | rand 1 30 45 32 |

ratios | The uniform function ratios returns the ratio of consecutive pairs. It applies to all numeric data types. | r:ratios L | ratios 1 2 4 6 7 10 |

reciprocal | Returns the reciprocal of its argument. The argument is first cast to float, and the result is float. | r:reciprocal X | reciprocal 0 0w 0n 3 10 |

signum | The 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 X | signum -2 0 1 3 |

sqrt | Return the square toor of an atom or list. | sqrt 4 9 16 | |

sum | Return the total sum value of a list | Sum 5 5 10 | |

var | Aggregation 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 X | var 10 343 232 55 |

where | Where the argument is a boolean list, this returns the indices of the 1's | where 21 2 5 11 9>15 | |

wsum | The 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 | |

xexp | This is the dyadic power function. | r:xexp[X;Y] | 2 xexp 3 |

## kdb Moving Functions - Moving Sum, Moving Maximum..

sym | description | syntax | eg |
---|---|---|---|

mdev | The 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 L | 2 mdev 1 2 3 5 7 10 |

mmax | The 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 L | 3 mmax 2 7 1 3 5 2 8 |

mmin | The moving minimum. | r:N mmin L | 2 mmin 2 7 1 3 5 2 8 |

msum | The 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 L | 3 msum 1 2 3 5 7 11 |

## kdb Set Operations

sym | description | syntax | eg |
---|---|---|---|

all | Function 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 A | all 1 2 3=1 2 4 |

cross | Returns the cross product (i.e. all possible combinations) of its arguments. | R:X cross Y | |

except | Returns all elements of its left argument that are not in its right argument. | x except y | 1 3 2 4 except 1 2 |

inter | Returns all elements common to both arguments | x inter y | 1 3 2 4 inter 1 2 6 7 |

union | Dyadic 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 Y | 1 2 3 union 2 4 6 8 |