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) |
arctan2(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) |
||
logaddexp2(x, y) |
||
cbrt(x) |
cbrt(x) |
|
pow(x, a) |
power(x, a) |
power(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 | ||
ceil(x) |
ceil(x) |
ceil(x) |
copysign(x, y) |
copysign(x, y) |
|
fabs(x) |
fabs(x) |
fabs(x) |
floor(x) |
floor(x) |
floor(x) |
frexp(x) |
frexp(x) |
|
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) |
|
nextafter(x) |
||
rint(x) |
||
sign(x) |
sign(x) |
|
spacing(x) |
||
signbit(x) |
||
trunc(x) |
trunc(x) |
trunc(x) |
Miscellaneous | ||
abs(x) |
abs(x) |
abs(x) |
clip(x, a, b) |
||
divmod(x, y) |
||
factorial(n) |
||
fmod(x, y) |
fmod(x, y) |
|
gcd(a, b) |
gcd(a, b) |
|
maximum(x, y) |
||
minimum(x, y) |
||
modf(x) |
modf(x) |
|
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 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
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 thatsin(arcsin(x)) == x
for allx
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 thatcos(arccos(x)) == x
for allx
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 thattan(arctan(x)) == x
.
-
arctan2
(y, x)¶ The inverse trigonometric tangent of
y/x
, taking into account the signs ofx
andy
to 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
x
andy
, 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) - 1
for 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)
.
-
cbrt
(x)¶ Compute the cubic root of
x
, i.e. \(\sqrt[3]{x}\).
-
power
(x, a)¶ Raise
x
to 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
x
this function computes the result much more precisely than1 - erf(x)
.
-
gamma
(x)¶ Euler gamma function of
x
.The gamma function is defined for all
x
except for the negative integers. For positivex
it can be computed via the integral \(\Gamma(x) = \int_0^\infty t^{x-1}e^{-t}dt\). For negativex
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¶
-
isna
(x)¶ Returns True if
x
is an NA value, and False otherwise.- If
x
is 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 inx
is an NA value or not. - If
x
is a column-expression, thenisna(x)
is also an expression. The argument columnx
can 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 inx
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 ofx
except for the floatnan
value. isna(None)
produces True.
- If
-
isfinite
(x)¶ Returns True if
x
is a finite value, and False ifx
is a positive/negative infinity of NA.
-
isinf
(x)¶ Returns True if
x
is a positive or negative infinity, and False otherwise.
Miscellaneous 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 asx
. An error will be raised if any columns inx
are 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
fabs()
- a
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 useNone
instead 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
.