Skip to content

math

Generated by sema doc from the compiler’s authoritative native-signature registry.

Scientific stdlib namespace — bring it in with import math (LANGUAGE §5.32).

Cosine of x.

  • domain: finite real x (radians)
  • shape: elementwise
  • returns: float | Tensor
  • example: math.cos(0.5)

Sine of x.

  • domain: finite real x (radians)
  • shape: elementwise
  • returns: float | Tensor
  • example: math.sin(0.5)

Tangent of x.

  • domain: finite real x (radians)
  • shape: elementwise
  • returns: float | Tensor
  • example: math.tan(0.5)

Arc cosine, in radians.

  • domain: x in [-1, 1]
  • shape: elementwise
  • returns: float | Tensor
  • example: math.acos(0.5)

Arc sine, in radians.

  • domain: x in [-1, 1]
  • shape: elementwise
  • returns: float | Tensor
  • example: math.asin(0.5)

Arc tangent, in radians.

  • domain: finite real x
  • shape: elementwise
  • returns: float | Tensor
  • example: math.atan(0.5)

Hyperbolic cosine.

  • domain: finite real x; result must stay finite
  • shape: elementwise
  • returns: float | Tensor
  • example: math.cosh(0.5)

Hyperbolic sine.

  • domain: finite real x; result must stay finite
  • shape: elementwise
  • returns: float | Tensor
  • example: math.sinh(0.5)

Hyperbolic tangent.

  • domain: finite real x
  • shape: elementwise
  • returns: float | Tensor
  • example: math.tanh(0.5)

Inverse hyperbolic cosine.

  • domain: x >= 1
  • shape: elementwise
  • returns: float | Tensor
  • example: math.acosh(1.5)

Inverse hyperbolic sine.

  • domain: finite real x
  • shape: elementwise
  • returns: float | Tensor
  • example: math.asinh(0.5)

Inverse hyperbolic tangent.

  • domain: x in (-1, 1)
  • shape: elementwise
  • returns: float | Tensor
  • example: math.atanh(0.5)

e raised to x.

  • domain: finite real x; result must stay finite
  • shape: elementwise
  • returns: float | Tensor
  • example: math.exp(1.0)

2 raised to x.

  • domain: finite real x; result must stay finite
  • shape: elementwise
  • returns: float | Tensor
  • example: math.exp2(3.0)

exp(x) - 1, accurate near zero.

  • domain: finite real x; result must stay finite
  • shape: elementwise
  • returns: float | Tensor
  • example: math.expm1(0.5)

Natural logarithm.

  • domain: x > 0
  • shape: elementwise
  • returns: float | Tensor
  • example: math.ln(2.0)

Base-2 logarithm.

  • domain: x > 0
  • shape: elementwise
  • returns: float | Tensor
  • example: math.log2(8.0)

Base-10 logarithm.

  • domain: x > 0
  • shape: elementwise
  • returns: float | Tensor
  • example: math.log10(100.0)

ln(1 + x), accurate near zero.

  • domain: x > -1
  • shape: elementwise
  • returns: float | Tensor
  • example: math.log1p(0.5)

Square root.

  • domain: x >= 0
  • shape: elementwise
  • returns: float | Tensor
  • example: math.sqrt(2.0)

Cube root (defined for negative x).

  • domain: finite real x
  • shape: elementwise
  • returns: float | Tensor
  • example: math.cbrt(-8.0)

Absolute value (float kernel).

  • domain: finite real x
  • shape: elementwise
  • returns: float | Tensor
  • example: math.abs(-2.5)

Absolute value (float kernel; alias of abs).

  • domain: finite real x
  • shape: elementwise
  • returns: float | Tensor
  • example: math.fabs(-2.5)

Fractional part (x - trunc(x)).

  • domain: finite real x
  • shape: elementwise
  • returns: float | Tensor
  • example: math.fract(2.75)

Radians to degrees.

  • domain: finite real x (radians)
  • shape: elementwise
  • returns: float | Tensor
  • example: math.degrees(1.0)

Radians to degrees (alias of degrees).

  • domain: finite real x (radians)
  • shape: elementwise
  • returns: float | Tensor
  • example: math.rad2deg(1.0)

Degrees to radians.

  • domain: finite real x (degrees)
  • shape: elementwise
  • returns: float | Tensor
  • example: math.radians(180.0)

Degrees to radians (alias of radians).

  • domain: finite real x (degrees)
  • shape: elementwise
  • returns: float | Tensor
  • example: math.deg2rad(180.0)

Reciprocal 1/x.

  • domain: x != 0
  • shape: elementwise
  • returns: float | Tensor
  • example: math.recip(4.0)

Sign of x as -1.0, 0.0 (signed zero), or 1.0.

  • domain: finite real x
  • shape: elementwise
  • returns: float | Tensor
  • example: math.sign(-3.5)

Sign of x (alias of sign).

  • domain: finite real x
  • shape: elementwise
  • returns: float | Tensor
  • example: math.sgn(-3.5)

Gauss error function.

  • domain: finite real x
  • shape: elementwise
  • returns: float | Tensor
  • example: math.erf(0.5)

Complementary error function 1 - erf(x).

  • domain: finite real x
  • shape: elementwise
  • returns: float | Tensor
  • example: math.erfc(0.5)

Gamma function Γ(x).

  • domain: finite real x, not a nonpositive integer; result must stay finite
  • shape: elementwise
  • returns: float | Tensor
  • example: math.gamma(4.5)

Natural log of |Γ(x)|.

  • domain: finite real x, not a nonpositive integer
  • shape: elementwise
  • returns: float | Tensor
  • example: math.lgamma(4.5)

Natural logarithm, or the base-base logarithm with two arguments.

  • domain: x > 0; base > 0 and base != 1 when given
  • shape: elementwise
  • returns: float | Tensor
  • example: math.log(81.0, 3.0)

Arc tangent of y/x using both signs to pick the quadrant.

  • domain: finite reals (signed zeros respected)
  • shape: elementwise (binary broadcast)
  • returns: float | Tensor
  • example: math.atan2(0.75, -0.25)

Euclidean norm sqrt(x² + y²) without intermediate overflow.

  • domain: finite reals; result must stay finite
  • shape: elementwise (binary broadcast)
  • returns: float | Tensor
  • example: math.hypot(3.0, 4.0)

math.copysign(magnitude, sign) -> float | Tensor

Section titled “math.copysign(magnitude, sign) -> float | Tensor”

Magnitude of the first argument with the sign of the second.

  • domain: finite reals
  • shape: elementwise (binary broadcast)
  • returns: float | Tensor
  • example: math.copysign(3.0, -0.0)

math.pow(base, exponent) -> float | Tensor

Section titled “math.pow(base, exponent) -> float | Tensor”

base raised to exponent (float power).

  • domain: finite reals; negative base needs an integral exponent; result must stay finite
  • shape: elementwise (binary broadcast)
  • returns: float | Tensor
  • example: math.pow(2.0, 10.5)

C-style remainder with the sign of x.

  • domain: finite reals, y != 0
  • shape: elementwise (binary broadcast)
  • returns: float | Tensor
  • example: math.fmod(-7.5, 2.0)

IEEE 754 remainder (nearest-multiple, ties to even).

  • domain: finite reals, y != 0
  • shape: elementwise (binary broadcast)
  • returns: float | Tensor
  • example: math.remainder(7.0, 2.0)

math.nextafter(x, toward) -> float | Tensor

Section titled “math.nextafter(x, toward) -> float | Tensor”

Next representable f64 after x toward the second argument.

  • domain: finite reals
  • shape: elementwise (binary broadcast)
  • returns: float | Tensor
  • example: math.nextafter(1.0, 2.0)

n! with checked i64 overflow.

  • domain: int 0 <= n <= 20 (i64 result)
  • shape: scalar
  • returns: int
  • example: math.factorial(12)

Binomial coefficient C(n, k) with checked i64 overflow.

  • domain: ints n >= 0, k >= 0 (0 when k > n)
  • shape: scalar
  • returns: int
  • example: math.comb(10, 3)

Partial permutations P(n, k) with checked i64 overflow.

  • domain: ints n >= 0, k >= 0 (k defaults to n; 0 when k > n)
  • shape: scalar
  • returns: int
  • example: math.perm(10, 3)

Greatest common divisor of the arguments.

  • domain: any i64 ints (absolute values; 0 with no arguments)
  • shape: scalar
  • returns: int
  • example: math.gcd(12, 18, 30)

Least common multiple of the arguments with checked i64 overflow.

  • domain: any i64 ints (0 if any argument is 0); checked overflow
  • shape: scalar
  • returns: int
  • example: math.lcm(4, 6)

Integer square root: floor of the exact square root.

  • domain: int n >= 0
  • shape: scalar
  • returns: int
  • example: math.isqrt(17)

Largest integer <= x.

  • domain: finite scalar (exact for int/rational; i64-checked result) or tensor (stays f64)
  • shape: elementwise
  • returns: int | Tensor
  • example: math.floor(2.75)

Smallest integer >= x.

  • domain: finite scalar (exact for int/rational; i64-checked result) or tensor (stays f64)
  • shape: elementwise
  • returns: int | Tensor
  • example: math.ceil(2.25)

Integer part of x (toward zero).

  • domain: finite scalar (exact for int/rational; i64-checked result) or tensor (stays f64)
  • shape: elementwise
  • returns: int | Tensor
  • example: math.trunc(-2.75)

Nearest integer, ties to even.

  • domain: finite scalar (exact for int/rational; i64-checked result) or tensor (stays f64)
  • shape: elementwise
  • returns: int | Tensor
  • example: math.round(2.5)

Build a typed Tensor from rectangular nested data.

  • domain: uniform nested numeric or bool data with optional matching f64/bool dtype; bounded rank/elements
  • shape: constructor
  • returns: Tensor
  • keywords: dtype
  • example: math.tensor([1.0, 2.0], dtype="f64")

Matrix product (or matrix·vector), shape-checked.

  • domain: 2-D shapes (m,k)·(k,n), or matrix·vector; typed ShapeError otherwise
  • shape: contraction
  • returns: Tensor
  • example: math.matmul(math.eye(2), math.ones([2, 2]))

Dot product of two vectors.

  • domain: two equal-length 1-D vectors (bounded reduction work)
  • shape: reduction
  • returns: float
  • example: math.dot(math.tensor([1.0, 2.0]), math.tensor([3.0, 4.0]))

Tensor of zeros with the given shape.

  • domain: nonnegative exact int or list/tuple of them (bounded elements)
  • shape: constructor
  • returns: Tensor
  • example: math.zeros([2, 3])

Tensor of ones with the given shape.

  • domain: nonnegative exact int or list/tuple of them (bounded elements)
  • shape: constructor
  • returns: Tensor
  • example: math.ones(3)

n×n identity matrix.

  • domain: one nonnegative exact int dimension (bounded elements)
  • shape: constructor
  • returns: Tensor
  • example: math.eye(2)

1-D tensor of 0.0..stop-1.

  • domain: exact integer stop (negative yields an empty tensor; bounded elements)
  • shape: constructor
  • returns: Tensor
  • example: math.arange(4)

math.mean(values) -> int | rational | float | Tensor

Section titled “math.mean(values) -> int | rational | float | Tensor”

Mean; exact rational for exact inputs, float once any float enters, or deterministic complex tensor reduction.

  • domain: non-empty iterable, or finite f64/complex tensor with optional integer axis/keepdims
  • shape: reduction
  • returns: int | rational | float | Tensor
  • keywords: axis, keepdims
  • example: math.mean([1.0, 2.0, 4.0])

math.sum(values, start?) -> int | rational | float | Tensor

Section titled “math.sum(values, start?) -> int | rational | float | Tensor”

Sum; exact for exact inputs, float once any float enters, or deterministic complex tensor reduction.

  • domain: iterable plus optional start, or finite f64/complex tensor with optional integer axis/keepdims
  • shape: reduction
  • returns: int | rational | float | Tensor
  • keywords: start, axis, keepdims
  • example: math.sum([1, 2, 3], start=4)

The circle constant π.

  • domain: attribute read; not callable
  • shape: scalar
  • returns: float
  • example: math.pi

Euler’s number e.

  • domain: attribute read; not callable
  • shape: scalar
  • returns: float
  • example: math.e

The circle constant τ = 2π.

  • domain: attribute read; not callable
  • shape: scalar
  • returns: float
  • example: math.tau

Positive infinity (f64).

  • domain: attribute read; not callable
  • shape: scalar
  • returns: float
  • example: math.inf

Quiet NaN (f64).

  • domain: attribute read; not callable
  • shape: scalar
  • returns: float
  • example: math.nan