CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-datatable

Python package for manipulating 2-dimensional tabular data structures with emphasis on speed and big data support

Pending
Overview
Eval results
Files

mathematical-functions.mddocs/

Mathematical Functions

Comprehensive mathematical operations including arithmetic, trigonometric, logarithmic, and statistical functions for data analysis and transformations.

Capabilities

Basic Mathematical Functions

def abs(x):
    """Absolute value of x"""

def exp(x):
    """Exponential function (e^x)"""

def exp2(x):
    """Base-2 exponential function (2^x)"""

def expm1(x):
    """Exponential minus 1 (e^x - 1), accurate for small x"""

def log(x):
    """Natural logarithm (base e)"""

def log10(x):
    """Base-10 logarithm"""

def log2(x):
    """Base-2 logarithm"""

def log1p(x):
    """Logarithm of (1 + x), accurate for small x"""

def sqrt(x):
    """Square root"""

def cbrt(x):
    """Cube root"""

def square(x):
    """Square (x^2)"""

def pow(x, y):
    """x raised to power y"""

Trigonometric Functions

def sin(x):
    """Sine function"""

def cos(x):
    """Cosine function"""

def tan(x):
    """Tangent function"""

def arcsin(x):
    """Inverse sine (arcsine)"""

def arccos(x):
    """Inverse cosine (arccosine)"""

def arctan(x):
    """Inverse tangent (arctangent)"""

def atan2(y, x):
    """Two-argument arctangent"""

def sinh(x):
    """Hyperbolic sine"""

def cosh(x):
    """Hyperbolic cosine"""

def tanh(x):
    """Hyperbolic tangent"""

def arsinh(x):
    """Inverse hyperbolic sine"""

def arcosh(x):
    """Inverse hyperbolic cosine"""

def artanh(x):
    """Inverse hyperbolic tangent"""

Rounding and Comparison Functions

def ceil(x):
    """Ceiling (smallest integer >= x)"""

def floor(x):
    """Floor (largest integer <= x)"""

def round(x, ndigits=0):
    """Round to ndigits decimal places"""

def rint(x):
    """Round to nearest integer"""

def trunc(x):
    """Truncate to integer"""

def sign(x):
    """Sign of x (-1, 0, or 1)"""

def signbit(x):
    """True if sign bit is set"""

def copysign(x, y):
    """Return x with sign of y"""

def fabs(x):
    """Floating-point absolute value"""

def fmod(x, y):
    """Floating-point remainder of x/y"""

Special Functions

def gamma(x):
    """Gamma function"""

def lgamma(x):
    """Natural logarithm of gamma function"""

def erf(x):
    """Error function"""

def erfc(x):
    """Complementary error function"""

def hypot(x, y):
    """Euclidean distance sqrt(x^2 + y^2)"""

def ldexp(x, i):
    """x * 2^i"""

def logaddexp(x, y):
    """log(exp(x) + exp(y))"""

def logaddexp2(x, y):
    """log2(2^x + 2^y)"""

Testing and Utility Functions

def isna(x):
    """Test for missing values (NaN)"""

def isfinite(x):
    """Test for finite values"""

def isinf(x):
    """Test for infinite values"""

def isclose(x, y, rtol=1e-5, atol=1e-8):
    """Test for approximate equality"""

Conditional Functions

def ifelse(condition, x, y):
    """Return x where condition is True, y otherwise"""

Mathematical Constants

e: float        # Euler's number (2.718281828...)
pi: float       # Pi (3.141592653...)
tau: float      # Tau (2*pi, 6.283185307...)
golden: float   # Golden ratio (1.618033988...)
nan: float      # Not-a-Number
inf: float      # Positive infinity

Conversion Functions

def deg2rad(x):
    """Convert degrees to radians"""

def rad2deg(x):
    """Convert radians to degrees"""

Usage Examples

Basic Arithmetic Operations

import datatable as dt
from datatable import f

DT = dt.Frame({
    'x': [-2, -1, 0, 1, 2],
    'y': [0.5, 1.0, 1.5, 2.0, 2.5],
    'z': [4, 9, 16, 25, 36]
})

# Basic functions
result = DT[:, dt.update(
    abs_x=dt.math.abs(f.x),
    sqrt_z=dt.math.sqrt(f.z),
    square_y=dt.math.square(f.y),
    exp_x=dt.math.exp(f.x)
)]

# Logarithmic functions
result = DT[:, dt.update(
    log_y=dt.math.log(f.y),
    log10_z=dt.math.log10(f.z),
    log2_z=dt.math.log2(f.z)
)]

Trigonometric Calculations

import datatable as dt

# Create angles in radians
angles = dt.Frame({'radians': [0, dt.math.pi/6, dt.math.pi/4, dt.math.pi/3, dt.math.pi/2]})

# Trigonometric functions
trig_results = angles[:, dt.update(
    sin_val=dt.math.sin(f.radians),
    cos_val=dt.math.cos(f.radians),
    tan_val=dt.math.tan(f.radians)
)]

# Convert degrees to radians
degrees = dt.Frame({'degrees': [0, 30, 45, 60, 90]})
result = degrees[:, dt.update(
    radians=dt.math.deg2rad(f.degrees),
    sin_deg=dt.math.sin(dt.math.deg2rad(f.degrees))
)]

Rounding and Precision

DT = dt.Frame({
    'values': [1.234, 2.567, 3.891, 4.125, 5.999]
})

# Different rounding methods
result = DT[:, dt.update(
    rounded=dt.math.round(f.values, 2),
    ceiling=dt.math.ceil(f.values),
    floor=dt.math.floor(f.values),
    truncated=dt.math.trunc(f.values)
)]

# Sign operations
mixed_values = dt.Frame({'x': [-3.2, -0.1, 0, 0.1, 3.2]})
result = mixed_values[:, dt.update(
    sign=dt.math.sign(f.x),
    abs_val=dt.math.abs(f.x),
    signbit=dt.math.signbit(f.x)
)]

Statistical and Special Functions

# Probability distributions
x_vals = dt.Frame({'x': [-2, -1, 0, 1, 2]})
result = x_vals[:, dt.update(
    erf_x=dt.math.erf(f.x),
    erfc_x=dt.math.erfc(f.x),
    gamma_x=dt.math.gamma(f.x + 1)  # Avoid negative values for gamma
)]

# Distance calculations
points = dt.Frame({
    'x1': [0, 1, 3],
    'y1': [0, 2, 4],
    'x2': [3, 4, 0],
    'y2': [4, 6, 0]
})

distances = points[:, dt.update(
    euclidean=dt.math.hypot(f.x2 - f.x1, f.y2 - f.y1)
)]

Missing Value Handling

DT = dt.Frame({
    'a': [1, None, 3, 4, 5],
    'b': [2.5, 3.0, None, 4.5, 5.0]
})

# Mathematical functions with missing values
result = DT[:, dt.update(
    sqrt_a=dt.math.sqrt(f.a),        # sqrt(None) = None
    log_b=dt.math.log(f.b),          # log(None) = None
    is_missing_a=dt.math.isna(f.a),  # Check for missing values
    is_finite_b=dt.math.isfinite(f.b)  # Check for finite values
)]

# Conditional mathematical operations
result = DT[:, dt.update(
    safe_log=dt.ifelse(dt.math.isna(f.b) | (f.b <= 0), 
                      None, 
                      dt.math.log(f.b))
)]

Complex Mathematical Expressions

# Financial calculations
principal = dt.Frame({
    'P': [1000, 2000, 5000],      # Principal
    'r': [0.05, 0.03, 0.07],      # Interest rate
    't': [1, 2, 3]                # Time in years
})

# Compound interest: A = P(1 + r)^t
result = principal[:, dt.update(
    amount=f.P * dt.math.pow(1 + f.r, f.t),
    interest=f.P * dt.math.pow(1 + f.r, f.t) - f.P
)]

# Continuous compounding: A = Pe^(rt)
result = principal[:, dt.update(
    continuous_amount=f.P * dt.math.exp(f.r * f.t)
)]

Numerical Stability

# Stable calculations for small numbers
small_vals = dt.Frame({'x': [1e-10, 1e-15, 1e-20]})

# Use log1p for log(1 + x) when x is small
result = small_vals[:, dt.update(
    log1p_stable=dt.math.log1p(f.x),
    log_unstable=dt.math.log(1 + f.x)
)]

# Use expm1 for exp(x) - 1 when x is small  
result = small_vals[:, dt.update(
    expm1_stable=dt.math.exp(f.x) - 1,  # Note: expm1 not in API, using exp(x) - 1
    exp_minus_one=dt.math.exp(f.x) - 1
)]

Mathematical Constants Usage

# Using mathematical constants
circle = dt.Frame({'radius': [1, 2, 3, 4, 5]})

result = circle[:, dt.update(
    circumference=2 * dt.math.pi * f.radius,
    area=dt.math.pi * dt.math.square(f.radius),
    tau_circumference=dt.math.tau * f.radius  # Alternative using tau
)]

# Golden ratio applications
fibonacci = dt.Frame({'n': [1, 2, 3, 4, 5, 6, 7, 8]})
result = fibonacci[:, dt.update(
    approx_fib=dt.math.round(
        dt.math.pow(dt.math.golden, f.n) / dt.math.sqrt(5)
    )
)]

Comparison and Testing

# Approximate equality testing
values1 = dt.Frame({'a': [1.0, 2.0, 3.0]})
values2 = dt.Frame({'b': [1.0000001, 1.9999999, 3.0000001]})

comparison = dt.cbind(values1, values2)
result = comparison[:, dt.update(
    exactly_equal=(f.a == f.b),
    approximately_equal=dt.math.isclose(f.a, f.b, rtol=1e-6)
)]

# Testing for special values
test_vals = dt.Frame({'x': [1.0, dt.math.inf, -dt.math.inf, dt.math.nan, 0.0]})
result = test_vals[:, dt.update(
    is_finite=dt.math.isfinite(f.x),
    is_infinite=dt.math.isinf(f.x),
    is_nan=dt.math.isna(f.x)
)]

Install with Tessl CLI

npx tessl i tessl/pypi-datatable

docs

core-data-structures.md

data-manipulation.md

expression-system.md

file-io.md

index.md

mathematical-functions.md

reductions-aggregations.md

row-operations.md

set-operations.md

string-operations.md

time-operations.md

type-system.md

tile.json