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
xmeasured in radians.This function can only be applied to numeric columns (real, integer, or boolean), and produces a float64 result, except when the argument
xis float32, in which case the result is float32 as well.
-
cos(x)¶ Compute the trigonometric cosine of angle
xmeasured in radians.This function can only be applied to numeric columns (real, integer, or boolean), and produces a float64 result, except when the argument
xis float32, in which case the result is float32 as well.
-
tan(x)¶ Compute the trigonometric tangent of
x, which is the ratiosin(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
xis 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 thatsin(arcsin(x)) == xfor allxin the interval[-1, 1].For the values of
xthat 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 thatcos(arccos(x)) == xfor allxin the interval[-1, 1].For the values of
xthat 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 thattan(arctan(x)) == x.
-
atan2(y, x)¶ The inverse trigonometric tangent of
y/x, taking into account the signs ofxandyto produce the correct result.If
(x,y)is a point in a Cartesian plane, thenarctan2(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, andarctan2(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 thatsinh(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 thatcosh(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 thatsinh(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
xandy, 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¶
-
exp2(x)¶ Two raised to the power
x.
-
expm1(x)¶ Computes \(e^x - 1\), however offering a better precision than
exp(x) - 1for small values ofx.
-
log(x)¶ The natural logarithm of
x. This function is the inverse ofexp(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 ofpower(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 ofexp2(x).
-
logaddexp(x, y)¶ Logarithm of the sum of exponents of
xandy: \(\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
xandy: \(\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
xto the powera, 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
xthis function computes the result much more precisely than1 - erf(x).
-
gamma(x)¶ Euler gamma function of
x.The gamma function is defined for all
xexcept for the negative integers. For positivexit can be computed via the integral \(\Gamma(x) = \int_0^\infty t^{x-1}e^{-t}dt\). For negativexit 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
xcan 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 asx. An error will be raised if any columns inxare not numeric. - a column-expression, in which case
abs(x)is also a column-expression that, when applied to some frameDT, will evaluate to a column with the absolute values ofx. The stype of the resulting column will be the same as the stype ofx. - an
int, or afloat, in which caseabs(x)returns the absolute value of that number, similar to the python built-in functionabs().
Examples:
DT = dt.Frame(A=[-3, 2, 4, -17, 0]) DT[:, abs(f.A)]
C0 ▪ 0 3 1 2 2 4 3 17 4 0 See also
- a
-
ceil(x)¶ The smallest integer not less than
x.
-
copysign(x, y)¶ Return a float with the magnitude of
xand the sign ofy.
-
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
rtoland the absolute toleranceatolparameters. The numbersxandyare considered close if \(|x-y| \le atol + rtol|y|\). Note that this relationship is not symmetric: it is possible to havex“close” toy, whileynot “close” tox.
-
isfinite(x)¶ Returns True if
xis a finite value, and False ifxis a positive/negative infinity of NA.
-
isinf(x)¶ Returns True if
xis a positive or negative infinity, and False otherwise.
-
isna(x)¶ Returns True if
xis an NA value, and False otherwise.- If
xis aFrame, 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 asx. Each element in this new frame is a boolean indicator of whether the corresponding element inxis an NA value or not. - If
xis a column-expression, thenisna(x)is also an expression. The argument columnxcan be of any stype, and the result is a column with stype bool8. When evaluated withinDT[i, j, ...], the expressionisna(x)produces a column where each element is an indicator of whether the corresponding value inxis NA or not. - When
xis a python integer,isna(x)returns False. - When
xis a python float,isna(x)returns False for all values ofxexcept for the floatnanvalue. isna(None)produces True.
- If
-
ldexp(x, n)¶ Multiply
xby 2 raised to the powery, i.e. compute \(x \cdot 2^y\). Columnxis expected to be float, andyinteger.
-
rint(x)¶ Round
xto the nearest integer.
-
sign(x)¶ The sign of
x, returned as float.This function returns 1.0 if
xis positive (including positive infinity), -1.0 ifxis negative, 0.0 ifxis zero, and NA ifxis NA.
-
signbit(x)¶ Returns True if
xis negative (its sign bit is set), and False ifxis positive. This function is able to distinguish between -0.0 and +0.0, returning True/False respectively. Ifxis an NA value, this function will also return NA.
-
trunc(x)¶ The nearest integer value not greater than
xin magnitude.If
xis integer or boolean, thentrunc()will return this value converted to float64. Ifxis floating-point, thentrunc(x)acts asfloor(x)for positive values ofx, and asceil(x)for negative values ofx. 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
datatablefunctions you can useNoneinstead ofnan.
-
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.