High-performance mathematical functions and constants for Clojure,
optimized for primitive double
and long
types.
Key features:
double
and long
) and are inlined for performance.clojure.core
's standard numerical operators with primitive-specialized macros.This namespace contains functions for:
+
, -
, *
, /
, etc.)==
, <
, zero?
, pos?
, etc.)Primitive Math Operators:
A set of inlined macros designed to replace selected clojure.core
arithmetic, comparison, and bitwise operators for potential performance gains with primitive arguments. These macros operate on double
and long
primitives and generally return primitive values.
Replaced operators:
* + - / > < >= <= == rem quot mod
bit-or bit-and bit-xor bit-not bit-and-not
bit-shift-left bit-shift-right unsigned-bit-shift-right
bit-set bit-clear bit-flip bit-test
inc dec
zero? neg? pos? even? odd?
min max
abs
<< >> >>> not==
To enable these primitive operators in your namespace, call use-primitive-operators
.
To revert to the original clojure.core
functions, call unuse-primitive-operators
.
Note that the fastmath.core
versions are not a complete drop-in replacement due to their primitive-specific behavior (e.g., return types), and calling unuse-primitive-operators
at the end of the namespace is recommended, especially in Clojure 1.12+.
High-performance mathematical functions and constants for Clojure, optimized for primitive `double` and `long` types. Key features: - Most functions are specialized for primitive types (`double` and `long`) and are inlined for performance. - Primarily backed by the [FastMath](https://github.com/jeffhain/jafama) library and custom primitive implementations. - Provides an option to replace `clojure.core`'s standard numerical operators with primitive-specialized macros. This namespace contains functions for: - Basic arithmetic (`+`, `-`, `*`, `/`, etc.) - Comparisons and predicates (`==`, `<`, `zero?`, `pos?`, etc.) - Bitwise operations - Trigonometric and hyperbolic functions - Exponents, logarithms, and powers - Floating-point specific operations (ulp, bits manipulation) - Combinatorics (factorial, combinations) - Distance calculations - Interpolation and mapping - Utility functions (gcd, lcm, error calculation, etc.) Primitive Math Operators: A set of inlined macros designed to replace selected `clojure.core` arithmetic, comparison, and bitwise operators for potential performance gains with primitive arguments. These macros operate on `double` and `long` primitives and generally return primitive values. Replaced operators: - `* + - / > < >= <= == rem quot mod` - `bit-or bit-and bit-xor bit-not bit-and-not` - `bit-shift-left bit-shift-right unsigned-bit-shift-right` - `bit-set bit-clear bit-flip bit-test` - `inc dec` - `zero? neg? pos? even? odd?` - `min max` - `abs` - Additionally: `<< >> >>> not==` To enable these primitive operators in your namespace, call [[use-primitive-operators]]. To revert to the original `clojure.core` functions, call [[unuse-primitive-operators]]. Note that the `fastmath.core` versions are not a complete drop-in replacement due to their primitive-specific behavior (e.g., return types), and calling `unuse-primitive-operators` at the end of the namespace is recommended, especially in Clojure 1.12+.
(*)
(* a)
(* a b)
(* a b c)
(* a b c d)
(* a b c d & r)
Primitive and inlined *
.
Primitive and inlined `*`.
(+)
(+ a)
(+ a b)
(+ a b c)
(+ a b c d)
(+ a b c d & r)
Primitive and inlined +
.
Primitive and inlined `+`.
(- a)
(- a b)
(- a b c)
(- a b c d)
(- a b c d & r)
Primitive and inlined -
.
Primitive and inlined `-`.
(/ a)
(/ a b)
(/ a b c)
(/ a b c d)
(/ a b c d & r)
Primitive and inlined /
.
Primitive and inlined `/`.
(< _)
(< a b)
(< a b & r)
Primitive math less-then function.
Primitive math less-then function.
(<= _)
(<= a b)
(<= a b & r)
Primitive math less-and-equal function.
Primitive math less-and-equal function.
(== _)
(== a b)
(== a b & r)
Primitive math equality function.
Primitive math equality function.
(> _)
(> a b)
(> a b & r)
Primitive math greater-than function.
Primitive math greater-than function.
(>= _)
(>= a b)
(>= a b & r)
Primitive math greater-and-equal function.
Primitive math greater-and-equal function.
(>> x shift)
Shift bits right and keep most significant bit unchanged
Shift bits right and keep most significant bit unchanged
(>>> x shift)
Shift bits right and set most significant bit to 0
Shift bits right and set most significant bit to `0`
(absolute-error v v-approx)
Absolute error between two values
Absolute error between two values
(approx v)
(approx v digits)
Round v
to specified (default: 2) decimal places. Be aware of floating point number accuracy.
Round `v` to specified (default: 2) decimal places. Be aware of floating point number accuracy.
(approx-eq a b)
(approx-eq a b digits)
Checks equality approximately up to selected number of digits (2 by default).
It can be innacurate due to the algorithm used. Use delta-eq
instead.
See approx
.
Checks equality approximately up to selected number of digits (2 by default). It can be innacurate due to the algorithm used. Use [[delta-eq]] instead. See [[approx]].
(between-? [x y] v)
(between-? x y v)
Check if given number is within the range (x,y].
Check if given number is within the range (x,y].
(between? [x y] v)
(between? x y v)
Check if given number is within the range [x,y].
Check if given number is within the range [x,y].
(bit-and x)
(bit-and x y)
(bit-and x y & r)
x ∧ y - bitwise AND
x ∧ y - bitwise AND
(bit-and-not x)
(bit-and-not x y)
(bit-and-not x y & r)
x ∧ ~y - bitwise AND (with complement second argument)
x ∧ ~y - bitwise AND (with complement second argument)
(bit-flip x bit)
Flip bit (set to 0
when 1
or to 1
when 0
).
Flip bit (set to `0` when `1` or to `1` when `0`).
(bit-nand x)
(bit-nand x y)
(bit-nand x y & r)
~(x ∧ y) - bitwise NAND
~(x ∧ y) - bitwise NAND
(bit-nor x)
(bit-nor x y)
(bit-nor x y & r)
~(x ∨ y) - bitwise NOR
~(x ∨ y) - bitwise NOR
(bit-or x)
(bit-or x y)
(bit-or x y & r)
x ∨ y - bitwise OR
x ∨ y - bitwise OR
(bit-shift-right x shift)
Shift bits right and keep most significant bit unchanged
Shift bits right and keep most significant bit unchanged
(bit-test x bit)
Test bit (return to true
when 1
or false
when 0
).
Test bit (return to `true` when `1` or `false` when `0`).
(bit-xnor x)
(bit-xnor x y)
(bit-xnor x y & r)
~(x⊕y) - bitwise XNOR
~(x⊕y) - bitwise XNOR
(bit-xor x)
(bit-xor x y)
(bit-xor x y & r)
x⊕y - bitwise XOR
x⊕y - bitwise XOR
(bits->double v)
Convert 64 bits to double
Convert 64 bits to double
(bool-xor x y)
(bool-xor x y & r)
Primitive boolean xor
Primitive boolean xor
(ceil x)
(ceil x scale)
Calculates the ceiling of a number.
With a scale
argument, rounds to the nearest multiple of scale
towards positive infinity.
See also: qceil
.
Calculates the ceiling of a number. With a `scale` argument, rounds to the nearest multiple of `scale` towards positive infinity. See also: [[qceil]].
(co-intervals data)
(co-intervals data number)
(co-intervals data number overlap)
Divides a sequence of numerical data
into number
(default 6) overlapping intervals.
The intervals are constructed such that each contains a similar number of values from the sorted data, aiming to replicate the behavior of R's co.intervals()
function. Invalid doubles (NaN, infinite) are removed from the input data before processing.
Arguments:
data
: A collection of numbers.number
: The desired number of intervals (default 6, long).overlap
: The desired overlap proportion between consecutive intervals (default 0.5, double).Returns a sequence of intervals, where each interval is represented as a 2-element vector [lower-bound upper-bound]
.
Divides a sequence of numerical `data` into `number` (default 6) overlapping intervals. The intervals are constructed such that each contains a similar number of values from the sorted data, aiming to replicate the behavior of R's `co.intervals()` function. Invalid doubles (NaN, infinite) are removed from the input data before processing. Arguments: - `data`: A collection of numbers. - `number`: The desired number of intervals (default 6, long). - `overlap`: The desired overlap proportion between consecutive intervals (default 0.5, double). Returns a sequence of intervals, where each interval is represented as a 2-element vector `[lower-bound upper-bound]`.
(combinations n k)
Binomial coefficient (n choose k)
Binomial coefficient (n choose k)
(constrain value mn mx)
Clamp value
to the range [mn,mx]
.
Clamp `value` to the range `[mn,mx]`.
(copy-sign magnitude sign)
Returns a value with a magnitude of first argument and sign of second.
Returns a value with a magnitude of first argument and sign of second.
(cos-interpolation start stop t)
oF interpolateCosine interpolation. See also lerp
/mlerp
, quad-interpolation
or smooth-interpolation
.
oF interpolateCosine interpolation. See also [[lerp]]/[[mlerp]], [[quad-interpolation]] or [[smooth-interpolation]].
(cut data breaks)
(cut x1 x2 breaks)
Divides a numerical range or a sequence of numbers into a specified number of equally spaced intervals.
Given data
and breaks
, the range is determined by the minimum and maximum finite values in data
. Invalid doubles (NaN, infinite) are ignored.
Given x1
, x2
, and breaks
, the range is explicitly [x1, x2]
.
The function generates breaks + 1
equally spaced points within the range using slice-range
, and then forms breaks
intervals from these points.
The intervals are returned as a sequence of 2-element vectors [lower-bound upper-bound]
.
Specifically, if the generated points are p0, p1, ..., p_breaks
, the intervals are formed as:
[[(prev-double p0), p1], [p1, p2], ..., [p_breaks-1, p_breaks]]
.
This means intervals are generally closed on the right, [a, b]
, with the first interval's lower bound slightly adjusted downwards to ensure inclusion of the exact minimum value if necessary due to floating point precision.
Arguments:
data
: A collection of numbers (used to determine range).x1
, x2
: The start and end points of the range.breaks
: The desired number of intervals (a positive long).Divides a numerical range or a sequence of numbers into a specified number of equally spaced intervals. Given `data` and `breaks`, the range is determined by the minimum and maximum finite values in `data`. Invalid doubles (NaN, infinite) are ignored. Given `x1`, `x2`, and `breaks`, the range is explicitly `[x1, x2]`. The function generates `breaks + 1` equally spaced points within the range using [[slice-range]], and then forms `breaks` intervals from these points. The intervals are returned as a sequence of 2-element vectors `[lower-bound upper-bound]`. Specifically, if the generated points are `p0, p1, ..., p_breaks`, the intervals are formed as: `[[(prev-double p0), p1], [p1, p2], ..., [p_breaks-1, p_breaks]]`. This means intervals are generally closed on the right, `[a, b]`, with the first interval's lower bound slightly adjusted downwards to ensure inclusion of the exact minimum value if necessary due to floating point precision. Arguments: - `data`: A collection of numbers (used to determine range). - `x1`, `x2`: The start and end points of the range. - `breaks`: The desired number of intervals (a positive long).
(degrees rad)
Convert radians into degrees.
Convert radians into degrees.
(delta-eq a b)
(delta-eq a b accuracy)
(delta-eq a b abs-tol rel-tol)
Checks if two floating-point numbers a
and b
are approximately equal within given tolerances.
The check returns true if abs(a - b)
is less than a combined tolerance, or if (== a b)
.
(delta-eq a b)
: Uses a default absolute tolerance of 1.0e-6
.(delta-eq a b abs-tol)
: Uses the provided absolute tolerance abs-tol
.(delta-eq a b abs-tol rel-tol)
: Uses both absolute and relative tolerances. The combined tolerance is max(abs-tol, rel-tol * max(abs(a), abs(b)))
.This function is useful for comparing floating-point numbers where exact equality checks (==
) may fail due to precision issues.
Checks if two floating-point numbers `a` and `b` are approximately equal within given tolerances. The check returns true if `abs(a - b)` is less than a combined tolerance, or if `(== a b)`. - 2-arity `(delta-eq a b)`: Uses a default absolute tolerance of `1.0e-6`. - 3-arity `(delta-eq a b abs-tol)`: Uses the provided absolute tolerance `abs-tol`. - 4-arity `(delta-eq a b abs-tol rel-tol)`: Uses both absolute and relative tolerances. The combined tolerance is `max(abs-tol, rel-tol * max(abs(a), abs(b)))`. This function is useful for comparing floating-point numbers where exact equality checks (`==`) may fail due to precision issues.
(difference-of-products a b c d)
Kahan's algorithm for (ab)-(cd) to avoid catastrophic cancellation.
Kahan's algorithm for (a*b)-(c*d) to avoid catastrophic cancellation.
(dist [x1 y1] [x2 y2])
(dist x1 y1 x2 y2)
Euclidean distance between points (x1,y1)
and (x2,y2)
.
Euclidean distance between points `(x1,y1)` and `(x2,y2)`.
Convert double array into sequence.
Alias for seq
.
Convert double array into sequence. Alias for `seq`.
(double-bits v)
Returns double as 64-bits (long)
Returns double as 64-bits (long)
(double-double-array->seq res)
Convert double array of double arrays into sequence of sequences.
Convert double array of double arrays into sequence of sequences.
(double-exponent v)
Extract exponent information from double
Extract exponent information from double
(double-high-bits v)
Returns high word from double as bits
Returns high word from double as bits
(double-low-bits v)
Returns low word from double as bits
Returns low word from double as bits
Value of 0x1.fffffffffffffp-1d = 0.(9)
Value of 0x1.fffffffffffffp-1d = 0.(9)
(double-significand v)
Extract significand from double
Extract significand from double
(eq _)
(eq a b)
(eq a b & r)
Primitive math equality function.
Primitive math equality function.
(factorial20 n)
Factorial table up to 20!
Factorial table up to 20!
(falling-factorial n x)
Falling (descending) factorial.
Falling (descending) factorial.
(falling-factorial-int n x)
Falling (descending) factorial for integer n.
Falling (descending) factorial for integer n.
(fast*)
(fast* a)
(fast* a b)
(fast* a b & r)
Primitive and inlined *
as a function
Primitive and inlined `*` as a function
(fast+)
(fast+ a)
(fast+ a b)
(fast+ a b & r)
Primitive and inlined +
as a function
Primitive and inlined `+` as a function
(fast-)
(fast- a)
(fast- a b)
(fast- a b & r)
Primitive and inlined -
as a function
Primitive and inlined `-` as a function
(fast-div)
(fast-div a)
(fast-div a b)
(fast-div a b & r)
Primitive and inlined /
as a function
Primitive and inlined `/` as a function
(fast-identity a)
Identity on double.
Identity on double.
(fast-max a)
(fast-max a b)
(fast-max a b & r)
Primitive and inlined max
as a function
Primitive and inlined `max` as a function
(fast-min a)
(fast-min a b)
(fast-min a b & r)
Primitive and inlined min
as a function
Primitive and inlined `min` as a function
(floor x)
(floor x scale)
Calculates the floor of a number.
With a scale
argument, rounds to the nearest multiple of scale
towards negative infinity.
See also: qfloor
.
Calculates the floor of a number. With a `scale` argument, rounds to the nearest multiple of `scale` towards negative infinity. See also: [[qfloor]].
(fma x y z)
(x y z)
-> (+ z (* x y))
or Math/fma
for java 9+
`(x y z)` -> `(+ z (* x y))` or `Math/fma` for java 9+
(fpow x exponent)
Fast version of pow where exponent is integer.
Fast version of pow where exponent is integer.
(frac v)
Fractional part, always returns values from 0.0 to 1.0 (exclusive). See sfrac
for signed version.
Fractional part, always returns values from 0.0 to 1.0 (exclusive). See [[sfrac]] for signed version.
(gcd a b)
Fast binary greatest common divisor (Stein's algorithm)
Fast binary greatest common divisor (Stein's algorithm)
(group-by-intervals coll)
(group-by-intervals intervals coll)
Groups values from a sequence coll
into specified intervals.
The function partitions the values in coll
based on which interval they fall into.
Each interval is a 2-element vector [lower upper]
. Values are included in an interval if they are strictly greater than the lower
bound and less than or equal to the upper
bound, using between-?
.
Arguments:
intervals
: A sequence of 2-element vectors representing the intervals [lower upper]
.coll
: A collection of numerical values to be grouped.If intervals
are not provided, the function first calculates overlapping intervals using co-intervals
from the values in coll
, and then groups the values into these generated intervals.
Returns a map where keys are the interval vectors and values are sequences of the numbers from coll
that fall within that interval.
Groups values from a sequence `coll` into specified intervals. The function partitions the values in `coll` based on which interval they fall into. Each interval is a 2-element vector `[lower upper]`. Values are included in an interval if they are strictly greater than the `lower` bound and less than or equal to the `upper` bound, using [[between-?]]. Arguments: - `intervals`: A sequence of 2-element vectors representing the intervals `[lower upper]`. - `coll`: A collection of numerical values to be grouped. If `intervals` are not provided, the function first calculates overlapping intervals using [[co-intervals]] from the values in `coll`, and then groups the values into these generated intervals. Returns a map where keys are the interval vectors and values are sequences of the numbers from `coll` that fall within that interval.
(haversin x)
(haversin [lat1 lon1] [lat2 lon2])
(haversin lat1 lon1 lat2 lon2)
Haversine formula for value or lattitude and longitude pairs.
Haversine formula for value or lattitude and longitude pairs.
(haversine-dist [lat1 lon1] [lat2 lon2])
(haversine-dist lat1 lon1 lat2 lon2)
Haversine distance d
for r=1
Haversine distance `d` for `r=1`
(high-2-exp x)
Finds the smallest integer n
such that 2^n >= |x|
. See low-2-exp
.
Finds the smallest integer `n` such that `2^n >= |x|`. See [[low-2-exp]].
(high-exp b x)
Finds the smallest integer n
such that b^n >= |x|
. See also low-exp
.
Finds the smallest integer `n` such that `b^n >= |x|`. See also [[low-exp]].
(hypot x y)
(hypot x y z)
Calculates the Euclidean norm (distance from the origin) for 2 or 3 arguments.
It uses a numerically stable algorithm to avoid intermediate overflow or underflow.
See also hypot-sqrt
for the direct calculation.
Calculates the Euclidean norm (distance from the origin) for 2 or 3 arguments. It uses a numerically stable algorithm to avoid intermediate overflow or underflow. See also [[hypot-sqrt]] for the direct calculation.
(hypot-sqrt x y)
(hypot-sqrt x y z)
Calculates the Euclidean norm (distance from the origin) using a direct sqrt of sum of squares.
Note: This method can be less numerically stable than hypot
for inputs with vastly different magnitudes.
Calculates the Euclidean norm (distance from the origin) using a direct sqrt of sum of squares. Note: This method can be less numerically stable than [[hypot]] for inputs with vastly different magnitudes.
(iabs x)
Absolute value, long
version. See abs
.
Absolute value, `long` version. See [[abs]].
(inf? v)
Check if a number is an infinite (positive or negative).
Check if a number is an infinite (positive or negative).
(integer? v)
Check if given real number is an integer.
Check if given real number is an integer.
(inv-factorial n)
Inverse of factorial, 1/n!
Inverse of factorial, 1/n!
Value of $\frac{1}{\ln{\frac{1}{2}}}$
Value of $\frac{1}{\ln{\frac{1}{2}}}$
(invalid-double? v)
Check if a number is not finite double (NaN or ±Inf).
Check if a number is not finite double (NaN or ±Inf).
(itrunc v)
Truncate fractional part, keep sign. Returns long
.
Truncate fractional part, keep sign. Returns `long`.
Lanchos approximation of g
constant
Lanchos approximation of `g` constant
(lcm a b)
Fast binary least common multiplier.
Fast binary least common multiplier.
(lerp start stop t)
Linear interpolation between start
and stop
for amount t
. See also mlerp
, cos-interpolation
, quad-interpolation
or smooth-interpolation
.
Linear interpolation between `start` and `stop` for amount `t`. See also [[mlerp]], [[cos-interpolation]], [[quad-interpolation]] or [[smooth-interpolation]].
(log x)
(log base x)
log(x)=ln(x) or logarithm with given base
.
log(x)=ln(x) or logarithm with given `base`.
(log-combinations n k)
Log of binomial coefficient (n choose k)
Log of binomial coefficient (n choose k)
(log-factorial x)
Log factorial, alias to log-gamma
Log factorial, alias to log-gamma
(log2 x)
Logarithm with base 2.
\(\ln_2{x}\)
Logarithm with base 2. \\(\ln_2{x}\\)
(log2int v)
Fast and integer version of log2, returns long
Fast and integer version of log2, returns long
(logb b x)
Logarithm with base b
.
\(\ln_b{x}\)
Logarithm with base `b`. \\(\ln_b{x}\\)
(long-abs x)
Absolut value, long
version. See abs
.
Absolut value, `long` version. See [[abs]].
(long-add)
(long-add a)
(long-add a b)
(long-add a b c)
(long-add a b c d)
(long-add a b c d & r)
Primitive and inlined +
. Coerces arguments and returned value to a long.
Primitive and inlined `+`. Coerces arguments and returned value to a long.
(long-dec x)
Primitive and inlined dec
coerced to a long
Primitive and inlined `dec` coerced to a long
(long-div a)
(long-div a b)
(long-div a b c)
(long-div a b c d)
(long-div a b c d & r)
Primitive and inlined /
. Coerces to arguments and returned value to a long.
Primitive and inlined `/`. Coerces to arguments and returned value to a long.
(long-inc x)
Primitive and inlined inc
coerced to a long
Primitive and inlined `inc` coerced to a long
(long-max a)
(long-max a b)
(long-max a b c)
(long-max a b c d)
(long-max a b c d & r)
Primitive and inlined max
. Coerces arguments and returned values to longs.
Primitive and inlined `max`. Coerces arguments and returned values to longs.
(long-min a)
(long-min a b)
(long-min a b c)
(long-min a b c d)
(long-min a b c d & r)
Primitive and inlined min
. Coerces arguments and returned values to longs.
Primitive and inlined `min`. Coerces arguments and returned values to longs.
(long-mod x y)
Primitive and inlined mod
coerced to longs
Primitive and inlined `mod` coerced to longs
(long-mult)
(long-mult a)
(long-mult a b)
(long-mult a b c)
(long-mult a b c d)
(long-mult a b c d & r)
Primitive and inlined *
. Coerces arguments and returned value to a long.
Primitive and inlined `*`. Coerces arguments and returned value to a long.
(long-quot x y)
Primitive and inlined quot
coerced to longs
Primitive and inlined `quot` coerced to longs
(long-rem x y)
Primitive and inlined rem
coerced to longs
Primitive and inlined `rem` coerced to longs
(long-sub a)
(long-sub a b)
(long-sub a b c)
(long-sub a b c d)
(long-sub a b c d & r)
Primitive and inlined -
. Coerces arguments and returned value to a long.
Primitive and inlined `-`. Coerces arguments and returned value to a long.
(low-2-exp x)
Finds the greatest integer n
such that 2^n <= |x|
. See high-2-exp
.
Finds the greatest integer `n` such that `2^n <= |x|`. See [[high-2-exp]].
(low-exp b x)
Finds the greatest integer n
such that b^n <= |x|
. See also high-exp
.
Finds the greatest integer `n` such that `b^n <= |x|`. See also [[high-exp]].
(make-norm start stop)
(make-norm start stop dstart dstop)
Make norm
function for given range. Resulting function accepts double
value (with optional target [dstart,dstop]
range) and returns double
.
Make [[norm]] function for given range. Resulting function accepts `double` value (with optional target `[dstart,dstop]` range) and returns `double`.
(max a)
(max a b)
(max a b c)
(max a b c d)
(max a b c d & r)
Primitive and inlined max
.
Primitive and inlined `max`.
(min a)
(min a b)
(min a b c)
(min a b c d)
(min a b c d & r)
Primitive and inlined min
.
Primitive and inlined `min`.
(mlerp start stop t)
lerp
as macro. For inline code. See also lerp
, cos-interpolation
, quad-interpolation
or smooth-interpolation
.
[[lerp]] as macro. For inline code. See also [[lerp]], [[cos-interpolation]], [[quad-interpolation]] or [[smooth-interpolation]].
(mnorm v start stop)
(mnorm v start1 stop1 start2 stop2)
Macro version of norm
.
Macro version of [[norm]].
(mpow x e m)
Calculates the modular exponentiation $(x^e \pmod m)$.
Computes the remainder of x
raised to the power of e
, when divided by m
.
Uses binary exponentiation.
x
: Base (long).
e
: Exponent (long, non-negative).
m
: Modulus (long, positive).
Calculates the modular exponentiation $(x^e \pmod m)$. Computes the remainder of `x` raised to the power of `e`, when divided by `m`. Uses binary exponentiation. `x`: Base (long). `e`: Exponent (long, non-negative). `m`: Modulus (long, positive).
(muladd x y z)
(x y z)
-> (+ z (* x y))
or Math/fma
for java 9+
`(x y z)` -> `(+ z (* x y))` or `Math/fma` for java 9+
(near-zero? x)
(near-zero? x abs-tol)
(near-zero? x abs-tol rel-tol)
Checks if given value is near zero with absolute (default: 1.0e-6
) and/or relative (default 0.0
) tolerance.
Checks if given value is near zero with absolute (default: `1.0e-6`) and/or relative (default `0.0`) tolerance.
(neg-inf? v)
Check if a number is negatively infinite.
Check if a number is negatively infinite.
(negative-zero? x)
Check if zero is negative, ie. -0.0
Check if zero is negative, ie. -0.0
(negmuladd x y z)
(x y z)
-> (+ z (* -x y))
or Math/fma
for java 9+
`(x y z)` -> `(+ z (* -x y))` or `Math/fma` for java 9+
(next-double v)
(next-double v delta)
Next double value. Optional value delta
sets step amount.
Next double value. Optional value `delta` sets step amount.
(norm v start stop)
(norm v start1 stop1 start2 stop2)
Normalize v
from the range [start,stop]
to the range [0,1]
or map v
from the range [start1,stop1]
to the range [start2,stop2]
. See also make-norm
.
Normalize `v` from the range `[start,stop]` to the range `[0,1]` or map `v` from the range `[start1,stop1]` to the range `[start2,stop2]`. See also [[make-norm]].
(not-neg? x)
Primitive and inlined not-neg?
(x>=0.0)
Primitive and inlined `not-neg?` (x>=0.0)
(not-pos? x)
Primitive and inlined not-pos?
(x<=0.0)
Primitive and inlined `not-pos?` (x<=0.0)
(not-zero? x)
Primitive and inlined x<>0.0
Primitive and inlined x<>0.0
(not== _)
(not== a b)
(not== a b & r)
Not equality. For more than two arguments, returns true
when all values are unique.
(not== 1 2 1)
=== (and (not= 1 1) (not= 1 2))
Not equality. For more than two arguments, returns `true` when all values are unique. `(not== 1 2 1)` === `(and (not= 1 1) (not= 1 2))`
(one? x)
Primitive and inlined one?
(x==1.0)
Primitive and inlined `one?` (x==1.0)
(order vs)
(order vs decreasing?)
Computes the permutation of indices that would sort the input collection vs
.
The result is a sequence of 0-based indices such that applying them to the original collection
using (map #(nth vs %) result)
yields a sorted sequence.
Arguments:
vs
: A collection of comparable values.decreasing?
: Optional boolean (default false). If true, the indices permute for a descending sort.Returns: A sequence of 0-based indices.
Computes the permutation of indices that would sort the input collection `vs`. The result is a sequence of 0-based indices such that applying them to the original collection using `(map #(nth vs %) result)` yields a sorted sequence. Arguments: - `vs`: A collection of comparable values. - `decreasing?`: Optional boolean (default false). If true, the indices permute for a descending sort. Returns: A sequence of 0-based indices.
(pos-inf? v)
Check if a number is positively infinite.
Check if a number is positively infinite.
(prev-double v)
(prev-double v delta)
Next double value. Optional value delta
sets step amount.
Next double value. Optional value `delta` sets step amount.
(qceil x)
Fast version of ceil
. Returns long
.
Fast version of [[ceil]]. Returns `long`.
(qexp x)
Quick and less accurate version of exp
.
Quick and less accurate version of [[exp]].
(qfloor x)
Fast version of floor
. Returns long
.
Fast version of [[floor]]. Returns `long`.
(qlog x)
Fast and less accurate version of log
.
Fast and less accurate version of [[log]].
(qpow x exponent)
Fast and less accurate version of pow
.
Fast and less accurate version of [[pow]].
(qround x)
Fast version of round
. Returns long
Fast version of [[round]]. Returns `long`
(qsqrt x)
Approximated sqrt
using binary operations with error 1.0E-2
.
Approximated [[sqrt]] using binary operations with error `1.0E-2`.
(quad-interpolation start stop t)
Quad interpolation. See also lerp
/mlerp
, cos-interpolation
or smooth-interpolation
.
Quad interpolation. See also [[lerp]]/[[mlerp]], [[cos-interpolation]] or [[smooth-interpolation]].
(radians deg)
Convert degrees into radians.
Convert degrees into radians.
(rank vs)
(rank vs ties)
(rank vs ties desc?)
Assigns ranks to values in a collection, handling ties according to a specified strategy. Ranks are 0-based indices indicating the position of each element in the sorted collection.
Arguments:
vs
: A collection of comparable values.ties
: The tie-breaking strategy (keyword, optional, default :average
).
Supported strategies:
:average
: Assign the average rank to all tied values.:first
: Assign ranks based on their appearance order in the input.:last
: Assign ranks based on their appearance order in the input (reverse of :first
).:random
: Assign random ranks to tied values.:min
: Assign the minimum rank to all tied values.:max
: Assign the maximum rank to all tied values.:dense
: Assign consecutive ranks without gaps (like data.table::frank
in R).desc?
: If true, rank in descending order (boolean, optional, default false
).Returns a sequence of rank values corresponding to the input elements.
Assigns ranks to values in a collection, handling ties according to a specified strategy. Ranks are 0-based indices indicating the position of each element in the sorted collection. Arguments: - `vs`: A collection of comparable values. - `ties`: The tie-breaking strategy (keyword, optional, default `:average`). Supported strategies: - `:average`: Assign the average rank to all tied values. - `:first`: Assign ranks based on their appearance order in the input. - `:last`: Assign ranks based on their appearance order in the input (reverse of `:first`). - `:random`: Assign random ranks to tied values. - `:min`: Assign the minimum rank to all tied values. - `:max`: Assign the maximum rank to all tied values. - `:dense`: Assign consecutive ranks without gaps (like `data.table::frank` in R). - `desc?`: If true, rank in descending order (boolean, optional, default `false`). Returns a sequence of rank values corresponding to the input elements.
(relative-error v v-approx)
Relative error between two values
Relative error between two values
(remainder dividend divisor)
From FastMath
doc: returns dividend - divisor * n,
where n is the mathematical integer closest to dividend/divisor. Returned value in [-|divisor|/2,|divisor|/2]
From `FastMath` doc: returns dividend - divisor * n, where n is the mathematical integer closest to dividend/divisor. Returned value in `[-|divisor|/2,|divisor|/2]`
(rising-factorial n x)
Rising (Pochhammer) factorial.
Rising (Pochhammer) factorial.
(rising-factorial-int n x)
Rising (Pochhammer) factorial for integer n.
Rising (Pochhammer) factorial for integer n.
(round-even x)
Round evenly, IEEE / IEC rounding
Round evenly, IEEE / IEC rounding
(round-up-pow2 v)
Rounds a positive long
integer up to the smallest power of 2 greater than or equal to the input value.
Rounds a positive `long` integer up to the smallest power of 2 greater than or equal to the input value.
(rqsqrt x)
Reciprocal of qsqrt
. Quick and less accurate.
Reciprocal of [[qsqrt]]. Quick and less accurate.
(safe-sqrt value)
Safe sqrt, for value <= 0 result is 0.
Safe sqrt, for value <= 0 result is 0.
(sample f number-of-values)
(sample f number-of-values domain?)
(sample f domain-min domain-max number-of-values)
(sample f domain-min domain-max number-of-values domain?)
Samples a function f
by evaluating it at evenly spaced points within a numerical range.
Generates number-of-values
points in the specified range [domain-min, domain-max]
(inclusive) and applies f
to each point.
Arguments:
f
: The function to sample. Should accept a single double
argument.number-of-values
: The total number of points to generate (a positive long
).domain-min
: The lower bound of the sampling range (a double
). Defaults to 0.0
.domain-max
: The upper bound of the sampling range (a double
). Defaults to 1.0
.domain?
: A boolean flag. If true
, returns pairs [x, (f x)]
. If false
(default), returns just (f x)
.Arities:
[f number-of-values]
: Samples f
in [0.0, 1.0]
. Returns (f x)
values.[f number-of-values domain?]
: Samples f
in [0.0, 1.0]
. Returns [x, (f x)]
pairs if domain?
is true, otherwise (f x)
values.[f domain-min domain-max number-of-values]
: Samples f
in [domain-min, domain-max]
. Returns (f x)
values.[f domain-min domain-max number-of-values domain?]
: Samples f
in [domain-min, domain-max]
. Returns [x, (f x)]
pairs if domain?
is true, otherwise (f x)
values.The points are generated linearly from domain-min
to domain-max
. If number-of-values
is 1, it samples only the midpoint of the range.
Returns a sequence of double
values or vectors [double, double]
depending on domain?
.
Samples a function `f` by evaluating it at evenly spaced points within a numerical range. Generates `number-of-values` points in the specified range `[domain-min, domain-max]` (inclusive) and applies `f` to each point. Arguments: - `f`: The function to sample. Should accept a single `double` argument. - `number-of-values`: The total number of points to generate (a positive `long`). - `domain-min`: The lower bound of the sampling range (a `double`). Defaults to `0.0`. - `domain-max`: The upper bound of the sampling range (a `double`). Defaults to `1.0`. - `domain?`: A boolean flag. If `true`, returns pairs `[x, (f x)]`. If `false` (default), returns just `(f x)`. Arities: - `[f number-of-values]`: Samples `f` in `[0.0, 1.0]`. Returns `(f x)` values. - `[f number-of-values domain?]`: Samples `f` in `[0.0, 1.0]`. Returns `[x, (f x)]` pairs if `domain?` is true, otherwise `(f x)` values. - `[f domain-min domain-max number-of-values]`: Samples `f` in `[domain-min, domain-max]`. Returns `(f x)` values. - `[f domain-min domain-max number-of-values domain?]`: Samples `f` in `[domain-min, domain-max]`. Returns `[x, (f x)]` pairs if `domain?` is true, otherwise `(f x)` values. The points are generated linearly from `domain-min` to `domain-max`. If `number-of-values` is 1, it samples only the midpoint of the range. Returns a sequence of `double` values or vectors `[double, double]` depending on `domain?`.
(seq->double-array vs)
Convert sequence to double array. Returns input if vs
is double array already.
Convert sequence to double array. Returns input if `vs` is double array already.
(seq->double-double-array vss)
Convert sequence to double-array of double-arrays.
If sequence is double-array of double-arrays returns vss
Convert sequence to double-array of double-arrays. If sequence is double-array of double-arrays returns `vss`
(sfrac v)
Fractional part, always returns values from -1.0 to 1.0 (exclusive). See frac
for unsigned version.
Fractional part, always returns values from -1.0 to 1.0 (exclusive). See [[frac]] for unsigned version.
(sgn value)
Returns -1 when value
is negative, 1 otherwise. See also signum
.
Returns -1 when `value` is negative, 1 otherwise. See also [[signum]].
(signum value)
Returns 1 if value
is > 0, 0 if it is 0, -1 otherwise. See also sgn
.
Returns 1 if `value` is > 0, 0 if it is 0, -1 otherwise. See also [[sgn]].
(slice-range cnt)
(slice-range data cnt)
(slice-range start end cnt)
Generates cnt
evenly spaced points within a numerical range.
(slice-range cnt)
: Generates cnt
points in the range [0.0, 1.0]
.(slice-range data cnt)
: Generates cnt
points in the range [(min data), (max data)]
.(slice-range start end cnt)
: Generates cnt
points in the range [start, end]
.The range is inclusive, meaning the first point is start
and the last is end
(unless cnt
is 1).
If cnt
is 1, it returns a single value which is the midpoint (start + end) / 2
of the range.
Returns a sequence of cnt
double values.
Generates `cnt` evenly spaced points within a numerical range. - `(slice-range cnt)`: Generates `cnt` points in the range `[0.0, 1.0]`. - `(slice-range data cnt)`: Generates `cnt` points in the range `[(min data), (max data)]`. - `(slice-range start end cnt)`: Generates `cnt` points in the range `[start, end]`. The range is inclusive, meaning the first point is `start` and the last is `end` (unless `cnt` is 1). If `cnt` is 1, it returns a single value which is the midpoint `(start + end) / 2` of the range. Returns a sequence of `cnt` double values.
(smooth-interpolation start stop t)
Smoothstep based interpolation. See also lerp
/mlerp
, quad-interpolation
or cos-interpolation
.
Smoothstep based interpolation. See also [[lerp]]/[[mlerp]], [[quad-interpolation]] or [[cos-interpolation]].
(smooth-max xs)
(smooth-max xs alpha)
(smooth-max xs alpha family)
Smooth maximum function.
A smooth function with alpha
argument. When alpha
goes to infinity, function returns maximum value of xs
.
Family:
:lse
- LogSumExp (default):boltzmann
- Boltzmann operator, works for small alpha values:mellowmax
:p-norm
:smu
- smooth maximum unit, epsilon = 1/alpha > 0Smooth maximum function. A smooth function with `alpha` argument. When `alpha` goes to infinity, function returns maximum value of `xs`. Family: * `:lse` - LogSumExp (default) * `:boltzmann` - Boltzmann operator, works for small alpha values * `:mellowmax` * `:p-norm` * `:smu` - smooth maximum unit, epsilon = 1/alpha > 0
(smoothstep edge0 edge1 x)
GL smoothstep.
GL [smoothstep](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/smoothstep.xhtml).
(spow x exponent)
Symmetric power of a number (keeping a sign of the argument.
Symmetric power of a number (keeping a sign of the argument.
(sum-of-products a b c d)
Kahan's algorithm for (ab)+(cd) to avoid catastrophic cancellation.
Kahan's algorithm for (a*b)+(c*d) to avoid catastrophic cancellation.
(tpow x exponent)
(tpow x exponent shift)
Calculates truncated power.
For x >= shift returns (x-shift)^exponent, 0.0 otherwise.
Calculates truncated power. For x >= shift returns (x-shift)^exponent, 0.0 otherwise.
(trunc v)
Truncate fractional part, keep sign. Returns double
.
Truncate fractional part, keep sign. Returns `double`.
(ulp x)
Unit in the Last Place, distance between next value larger than x
and x
Unit in the Last Place, distance between next value larger than `x` and `x`
(unsigned-bit-shift-right x shift)
Shift bits right and set most significant bit to 0
Shift bits right and set most significant bit to `0`
(unuse-primitive-operators)
(unuse-primitive-operators skip-set)
Undoes the work of use-primitive-operators
. This is idempotent.
Undoes the work of [[use-primitive-operators]]. This is idempotent.
(use-primitive-operators)
(use-primitive-operators skip-set)
Replaces Clojure's arithmetic and number coercion functions with primitive equivalents. These are
defined as macros, so they cannot be used as higher-order functions. This is an idempotent operation. Undo with unuse-primitive-operators
.
Replaces Clojure's arithmetic and number coercion functions with primitive equivalents. These are defined as macros, so they cannot be used as higher-order functions. This is an idempotent operation. Undo with [[unuse-primitive-operators]].
(valid-double? v)
Check if a number is finite double.
Check if a number is finite double.
(wrap [start stop] value)
(wrap start stop value)
Wrap overflowed value into the range, similar to ofWrap.
Wrap overflowed value into the range, similar to [ofWrap](http://openframeworks.cc/documentation/math/ofMath/#!show_ofWrap).
cljdoc builds & hosts documentation for Clojure/Script libraries
Ctrl+k | Jump to recent docs |
← | Move to previous article |
→ | Move to next article |
Ctrl+/ | Jump to the search field |