# Mathematics and statistics


function                            | rank | ƒ | semantics
------------------------------------|------|---|--------------------------
[`+`](add.md)                | 2    | a | add
[`-`](subtract.md)           | 2    | a | subtract
[`*`](multiply.md)           | 2    | a | multiply
[`%`](divide.md)             | 2    | a | divide
[`$`](mmu.md)                | 2    | A | dot product, matrix multiply
[`&`](lesser.md)             | 2    | a | lesser
[`|`](greater.md)            | 2    | a | greater
[`abs`](abs.md)              | 1    | a | absolute value
[`acos`](cos.md)             | 1    | a | arccosine
[`asin`](sin.md)             | 1    | a | arcsine
[`atan`](tan.md)             | 1    | a | arctangent
[`avg`](avg.md#avg)          | 1    | A | arithmetic mean
[`avgs`](avg.md#avgs)        | 1    | u | arithmetic means
[`ceiling`](ceiling.md)      | 1    | a | round up to integer
[`cor`](cor.md)              | 2    | A | correlation
[`cos`](cos.md)              | 1    | a | cosine
[`cov`](cov.md)              | 2    | A | covariance
[`deltas`](deltas.md)        | 1    | u | differences
[`dev`](dev.md#dev)          | 1    | A | standard deviation
[`div`](div.md)              | 2    | a | integer division
[`ema`](ema.md)              | 2    | m | exponential moving average
[`exp`](exp.md#exp)          | 1    | a | _e_<sup>x</sup>
[`floor`](floor.md)          | 1    | a | round down to integer
[`inv`](inv.md)              | 1    | u | matrix inverse
[`log`](log.md#log)          | 1    | a | natural logarithm
[`lsq`](lsq.md)              | 2    |   | matrix divide
[`mavg`](avg.md#mavg)        | 2    | m | moving average
[`max`](max.md#max)          | 1    | A | greatest
[`maxs`](max.md#maxs)        | 1    | u | maximums
[`mcount`](count.md#mcount)  | 2    | m | moving count
[`mdev`](dev.md#mdev)        | 2    | m | moving deviation
[`med`](med.md)              | 1    | A | median
[`min`](min.md#min)          | 1    | A | least
[`mins`](min.md#mins)        | 1    | u | minimums
[`mmax`](max.md#mmax)        | 2    | m | moving maximum
[`mmin`](min.md#mmin)        | 2    | m | moving minimum
[`mmu`](mmu.md)              | 2    |   | matrix multiply
[`mod`](mod.md)              | 2    | a | modulo
[`msum`](sum.md#msum)        | 2    | m | moving sum
[`prd`](prd.md)              | 1    | A | product
[`prds`](prd.md#prds)        | 1    | u | products
[`ratios`](ratios.md)        | 1    | u | ratios
[`reciprocal`](reciprocal.md)| 1    | a | reciprocal
[`scov`](cov.md#scov)        | 2    | A | statistical covariance
[`sdev`](dev.md#sdev)        | 1    | A | statistical standard deviation
[`signum`](signum.md)        | 1    | a | sign
[`sin`](sin.md)              | 1    | a | sine
[`sqrt`](sqrt.md)            | 1    | a | square root
[`sum`](sum.md)              | 1    | A | sum
[`sums`](sum.md#sums)        | 1    | u | sums
[`svar`](var.md#svar)        | 1    | A | statistical variance
[`tan`](tan.md)              | 1    | a | tangent
[`til`](til.md)              | 1    |   | natural numbers till
[`var`](var.md#var)          | 1    | A | variance
[`wavg`](avg.md#wavg)        | 2    | A | weighted average
[`wsum`](sum.md#wsum)        | 2    | A | weighted sum
[`xbar`](xbar.md)            | 2    | A | round down
[`xexp`](exp.md#xexp)        | 2    | a | x<sup>y</sup>
[`xlog`](log.md#xlog)        | 2    | a | base-x logarithm of y


ƒ – a: atomic; u: uniform; A: aggregate; m: moving


## Domains and ranges

The domains and ranges of the mathematical functions have boolean, numeric, and temporal datatypes.
```q
q)2+3 4 5
5 6 7
q)2012.05 2012.06m-2
2012.03 2012.04m
q)3.3 4.4 5.5*1b
3.3 4.4 5.5
```

Individual function articles tabulate non-obvious domain and range datatypes.


## Dictionaries and tables

The domains and ranges also extend to:

-   **dictionaries** where the [`value`](value.md) of the dictionary is in the domain
    ```q
    q)3+`a`b`c!(42;2012.09.15;1b)
    a| 45
    b| 2012.09.18
    c| 4
    ```
-   **simple tables** where the [`value`](value.md) of the [`flip`](flip.md) of the table is in the domain
    ```q
    q)3%([]b:1 2 3;c:45 46 47)
    b   c
    --------------
    3   0.06666667
    1.5 0.06521739
    1   0.06382979
    ```
-   **keyed tables** where the [`value`](value.md) of the table is in the domain
    ```q
    q)show v:([sym:`ibm`goog`msoft]qty:1000 2000 3000;p:1550 375 98)
    sym  | qty  p
    -----| ---------
    ibm  | 1000 1550
    goog | 2000 375
    msoft| 3000 98
    q)v+5
    sym  | qty  p
    -----| ---------
    ibm  | 1005 1555
    goog | 2005 380
    msoft| 3005 103
    ```

Exceptions to the above:
```txt
cor                  scov
cov                  sdev
dev                  svar
div  (tables)        til
ema                  var
inv                  wavg (tables)
lsq                  wsum (tables)
mmu                  xbar (tables)
mod  (tables)        xexp (tables)
```


## Mathematics with temporals

Temporal datatypes (timestamp, month, date, datetime, timespan, minute, second, time) are encoded as integer or float offsets from 2000.01.01 or 00:00.

Mathematical functions on temporals are applied to the underlying numerics. See domain/range tables for individual functions for the result datatypes.

> **Warning — Beyond addition and subtraction**
>
> Results for addition and subtraction are generally intuitive and useful; not always for other arithmetic functions.
> ```q
> q)2017.12.31+0 1 2
> 2017.12.31 2018.01.01 2018.01.02
> q)2017.12m-0 1 2
> 2017.12 2017.11 2017.10m
> q)2017.12m*0 1 2
> 2000.01 2017.12 2035.11m
> q)2017.12m% 1 2 3
> 215 107.5 71.66667
> q)00:10%2
> 5f
> q)00:10:00%2
> 300f
> q)00:10:00.000%2
> 300000f
> q)00:10:00.000000000%2
> 3e+11
> ```
>
>
## Aggregating nulls

`avg`, `min`, `max` and `sum` are special: they ignore nulls, in order to be similar to SQL92.

But for nested `x` these functions preserve the nulls.
```q
q)avg (1 2;0N 4)
0n 3
```
