math module

datatable provides the similar set of mathematical functions, as Python’s standard math module, or numpy math functions. Below is the comparison table showing which functions are available:

math numpy datatable
Trigonometric/hyperbolic functions
sin(x) sin(x) sin(x)
cos(x) cos(x) cos(x)
tan(x) tan(x) tan(x)
asin(x) arcsin(x) arcsin(x)
acos(x) arccos(x) arccos(x)
atan(x) arctan(x) arctan(x)
atan2(y, x) arctan2(y, x) atan2(y, x)
sinh(x) sinh(x) sinh(x)
cosh(x) cosh(x) cosh(x)
tanh(x) tanh(x) tanh(x)
asinh(x) arcsinh(x) arsinh(x)
acosh(x) arccosh(x) arcosh(x)
atanh(x) arctanh(x) artanh(x)
hypot(x, y) hypot(x, y) hypot(x, y)
radians(x) deg2rad(x) deg2rad(x)
degrees(x) rad2deg(x) rad2deg(x)
Exponential/logarithmic/power functions
exp(x) exp(x) exp(x)
exp2(x) exp2(x)
expm1(x) expm1(x) expm1(x)
log(x) log(x) log(x)
log10(x) log10(x) log10(x)
log1p(x) log1p(x) log1p(x)
log2(x) log2(x) log2(x)
logaddexp(x, y) logaddexp(x, y)
logaddexp2(x, y) logaddexp2(x, y)
cbrt(x) cbrt(x)
pow(x, a) power(x, a) pow(x, a)
sqrt(x) sqrt(x) sqrt(x)
square(x) square(x)
Special mathematical functions
erf(x)   erf(x)
erfc(x)   erfc(x)
gamma(x)   gamma(x)
heaviside(x)  
i0(x)  
lgamma(x)   lgamma(x)
sinc(x)  
Floating-point functions
abs(x) abs(x) abs(x)
ceil(x) ceil(x) ceil(x)
copysign(x, y) copysign(x, y) copysign(x, y)
fabs(x) fabs(x) fabs(x)
floor(x) floor(x) floor(x)
fmod(x, y) fmod(x, y) fmod(x)
frexp(x) frexp(x)  
isclose(x, y) isclose(x, y) isclose(x, y)
isfinite(x) isfinite(x) isfinite(x)
isinf(x) isinf(x) isinf(x)
isnan(x) isnan(x) isna(x)
ldexp(x, n) ldexp(x, n) ldexp(x, n)
modf(x) modf(x)  
nextafter(x, y)  
rint(x) rint(x)
sign(x) sign(x)
signbit(x) signbit(x)
spacing(x)  
trunc(x) trunc(x) trunc(x)
Miscellaneous
clip(x, a, b)  
divmod(x, y)  
factorial(n)    
gcd(a, b) gcd(a, b)  
maximum(x, y)  
minimum(x, y)  
Mathematical constants
e e e
golden
inf inf inf
nan nan nan
pi pi pi
tau   tau

Trigonometric/hyperbolic functions

sin(x)

Compute the trigonometric sine of angle x measured in radians.

This function can only be applied to numeric columns (real, integer, or boolean), and produces a float64 result, except when the argument x is float32, in which case the result is float32 as well.

cos(x)

Compute the trigonometric cosine of angle x measured in radians.

This function can only be applied to numeric columns (real, integer, or boolean), and produces a float64 result, except when the argument x is float32, in which case the result is float32 as well.

tan(x)

Compute the trigonometric tangent of x, which is the ratio sin(x)/cos(x).

This function can only be applied to numeric columns (real, integer, or boolean), and produces a float64 result, except when the argument x is float32, in which case the result is float32 as well.

arcsin(x)

The inverse trigonometric sine of x. In mathematics, this may also be written as \(\sin^{-1}x\). This function satisfies the property that sin(arcsin(x)) == x for all x in the interval [-1, 1].

For the values of x that are greater than 1 in magnitude, the function arc-sine produces NA values.

arccos(x)

The inverse trigonometric cosine of x. In mathematics, this may also be written as \(\cos^{-1}x\). This function satisfies the property that cos(arccos(x)) == x for all x in the interval [-1, 1].

For the values of x that are greater than 1 in magnitude, the function arc-sine produces NA values.

arctan(x)

The inverse trigonometric tangent of x. This function satisfies the property that tan(arctan(x)) == x.

atan2(y, x)

The inverse trigonometric tangent of y/x, taking into account the signs of x and y to produce the correct result.

If (x,y) is a point in a Cartesian plane, then arctan2(y, x) returns the radian measure of an angle formed by 2 rays: one starting at the origin and passing through point (0,1), and the other starting at the origin and passing through point (x,y). The angle is assumed positive if the rotation from the first ray to the second occurs counter-clockwise, and negative otherwise.

As a special case, arctan2(0, 0) == 0, and arctan2(0, -1) == tau/2..

sinh(x)

The hyperbolic sine of x, defined as \(\sinh(x) = \frac12(e^x - e^{-x})\).

cosh(x)

The hyperbolic cosine of x, defined as \(\cosh(x) = \frac12(e^x + e^{-x})\).

tanh(x)

The hyperbolic tangent of x, defines as \(\tanh(x) = \frac{\sinh x}{\cosh x} = \frac{e^x-e^{-x}}{e^x+e^{-x}}\).

arsinh(x)

The inverse hyperbolic sine of x. This function satisfies the property that sinh(arcsinh(x)) == x. Alternatively, this function can also be computed as \(\sinh^{-1}(x) = \ln(x + \sqrt{x^2 + 1})\).

arcosh(x)

The inverse hyperbolic cosine of x. This function satisfies the property that cosh(arccosh(x)) == x. Alternatively, this function can also be computed as \(\cosh^{-1}(x) = \ln(x + \sqrt{x^2 - 1})\).

artanh(x)

The inverse hyperbolic tangent of x. This function satisfies the property that sinh(arcsinh(x)) == x. Alternatively, this function can also be computed as \(\tanh^{-1}(x) = \frac12\ln\frac{1+x}{1-x}\).

hypot(x, y)

The length of a hypotenuse in a right triangle with sides x and y, i.e. \(\operatorname{hypot}(x, y) = \sqrt{x^2 + y^2}\).

deg2rad(x)

Convert an angle measured in degrees into radians: \(\operatorname{deg2rad}(x) = x\cdot\frac{\tau}{360}\).

rad2deg(x)

Convert an angle measured in radians into degrees: \(\operatorname{rad2deg}(x) = x\cdot\frac{360}{\tau}\).

Exponential/logarithmic functions

exp(x)

The exponent of x, i.e. Euler’s number e raised to the power x.

exp2(x)

Two raised to the power x.

expm1(x)

Computes \(e^x - 1\), however offering a better precision than exp(x) - 1 for small values of x.

log(x)

The natural logarithm of x. This function is the inverse of exp(x): exp(log(x)) == x.

log10(x)

The base-10 logarithm of x, also denoted as \(\lg(x)\) in mathematics. This function is the inverse of power(10, x).

log1p(x)

The natural logarithm of 1 plus x, i.e. \(\ln(1 + x)\).

log2(x)

The base-2 logarithm of x, this function is the inverse of exp2(x).

logaddexp(x, y)

Logarithm of the sum of exponents of x and y: \(\ln(e^x + e^y)\). The result avoids loss of precision from exponentiating small numbers.

logaddexp2(x, y)

Binary logarithm of the sum of binary exponents of x and y: \(\log_2(2^x + 2^y)\). The result avoids loss of precision from exponentiating small numbers.

cbrt(x)

Compute the cubic root of x, i.e. \(\sqrt[3]{x}\).

pow(x, a)

Raise x to the power a, i.e. calculate \(x^a\).

sqrt(x)

The square root of x, i.e. \(\sqrt{x}\).

square(x)

The square of x, i.e. \(x^2\).

Special mathemetical functions

erf(x)

The error function \(\operatorname{erf}(x)\).

This function is defined as the integral \(\operatorname{erf}(x) = \frac{2}{\sqrt{\pi}} \int^x_0 e^{-t^2}dt\). This function is used in computing probabilities arising from the normal distribution.

erfc(x)

The complementary error function \(\operatorname{erfc}(x) = 1 - \operatorname{erf}(x)\).

This function is defined as the integral \(\operatorname{erfc}(x) = \frac{2}{\sqrt{\pi}} \int^{\infty}_x e^{-t^2}dt\).

For large values of x this function computes the result much more precisely than 1 - erf(x).

gamma(x)

Euler gamma function of x.

The gamma function is defined for all x except for the negative integers. For positive x it can be computed via the integral \(\Gamma(x) = \int_0^\infty t^{x-1}e^{-t}dt\). For negative x it can be computed as \(\Gamma(x) = \frac{\Gamma(x + k)}{x(x+1)\cdot...\cdot(x+k-1)}\), where \(k\) is any integer such that \(x+k\) is positive.

If x is a positive integer, then \(\Gamma(x) = (x - 1)!\).

lgamma(x)

Natural logarithm of the absolute value of gamma function of x.

Floating-point functions

abs(x)

Return the absolute value of x. This function can only be applied to numeric arguments (i.e. boolean, integer, or real).

The argument x can be one of the following:

  • a Frame, in which case the function is applied to all elements of the frame, and returns a new frame with the same shape and stypes as x. An error will be raised if any columns in x are not numeric.
  • a column-expression, in which case abs(x) is also a column-expression that, when applied to some frame DT, will evaluate to a column with the absolute values of x. The stype of the resulting column will be the same as the stype of x.
  • an int, or a float, in which case abs(x) returns the absolute value of that number, similar to the python built-in function abs().

Examples:

DT = dt.Frame(A=[-3, 2, 4, -17, 0])
DT[:, abs(f.A)]
C0
03
12
24
317
40

See also

ceil(x)

The smallest integer not less than x.

copysign(x, y)

Return a float with the magnitude of x and the sign of y.

fabs(x)

The absolute value of x, returned as a float.

floor(x)

The largest integer not greater than x.

fmod(x, y)

Return the remainder of a floating-point division x/y.

isclose(x, y, *, rtol=1e-5, atol=1e-8)

Return True if x y, and False otherwise.

The comparison is done using the relative tolerance rtol and the absolute tolerance atol parameters. The numbers x and y are considered close if \(|x-y| \le atol + rtol|y|\). Note that this relationship is not symmetric: it is possible to have x “close” to y, while y not “close” to x.

isfinite(x)

Returns True if x is a finite value, and False if x is a positive/negative infinity of NA.

isinf(x)

Returns True if x is a positive or negative infinity, and False otherwise.

isna(x)

Returns True if x is an NA value, and False otherwise.

  • If x is a Frame, the function is applied separately to each element in the frame. The result is a new Frame where all columns are boolean, and with the same shape as x. Each element in this new frame is a boolean indicator of whether the corresponding element in x is an NA value or not.
  • If x is a column-expression, then isna(x) is also an expression. The argument column x can be of any stype, and the result is a column with stype bool8. When evaluated within DT[i, j, ...], the expression isna(x) produces a column where each element is an indicator of whether the corresponding value in x is NA or not.
  • When x is a python integer, isna(x) returns False.
  • When x is a python float, isna(x) returns False for all values of x except for the float nan value.
  • isna(None) produces True.
ldexp(x, n)

Multiply x by 2 raised to the power y, i.e. compute \(x \cdot 2^y\). Column x is expected to be float, and y integer.

rint(x)

Round x to the nearest integer.

sign(x)

The sign of x, returned as float.

This function returns 1.0 if x is positive (including positive infinity), -1.0 if x is negative, 0.0 if x is zero, and NA if x is NA.

signbit(x)

Returns True if x is negative (its sign bit is set), and False if x is positive. This function is able to distinguish between -0.0 and +0.0, returning True/False respectively. If x is an NA value, this function will also return NA.

trunc(x)

The nearest integer value not greater than x in magnitude.

If x is integer or boolean, then trunc() will return this value converted to float64. If x is floating-point, then trunc(x) acts as floor(x) for positive values of x, and as ceil(x) for negative values of x. This rounding mode is also called rounding towards zero.

Miscellaneous functions

Mathematical constants

e

The base of the natural logarithm, also known as the Euler’s number. Its value is 2.718281828459045.

golden

The golden ratio \(\varphi = (1 + \sqrt{5})/2\). The value is 1.618033988749895.

inf

Positive infinity.

nan

Not-a-number, a special floating-point constant that denotes a missing number. In most datatable functions you can use None instead of nan.

pi

Mathematical constant \(\pi = \frac12\tau\), the area of a circle with unit radius. The constant is stored with float64 precision, and its value is 3.141592653589793.

tau

Mathematical constant \(\tau = 2\pi\), the circumference of a circle with unit radius. Some mathematicians believe that \(\tau\) is the true circle constant, and \(\pi\) is an impostor. The value of \(\tau\) is 6.283185307179586.