fastmath.core
Collection of fast math functions and plethora of constants known from other math libraries.
Primitive math operators
Based on Primitive Math by Zach Tellman several operators are introduced and replace clojure.core
functions. All operators are macros and can’t be used as functions. List includes:
Known from Clojure: *
+
-
/
>
<
>=
<=
==
rem
quot
mod
bit-or
bit-and
bit-xor
bit-not
bit-shift-left
bit-shift-right
unsigned-bit-shift-right
inc
dec
zero?
neg?
pos?
min
max
even?
odd?
And additionally:
bool-and
-and
working on booleansbool-or
- booleanor
bool-xor
- booleanxor
bool-not
- booleannot
<<
- bit shift left>>
- signed bit shift right>>>
- unsigned bit shift rightnot==
- not equal
To turn on primitive math on your namespace call use-primitive-operators. To turn off and revert original versions call unuse-primitive-operators
Fast Math
Almost all math functions are backed by FastMath library. Most of them are macros. Some of them are wrapped in Clojure functions. Almost all operates on primitive double
and returns double
(with an exception round or qround which returns long
).
Other functions
Additionally namespace contains functions which are common in frameworks like OpenFrameworks and Processing.
- For random/noise functions check fastmath.random namespace.
- fastmath.vector contains vector (2,3,4 dim. + double array + clojure vector) protocol and implementations.
- fastmath.complex contains complex number operations.
Categories
- Bitwise: << >> >>> bit-and bit-not bit-or bit-shift-left bit-shift-right bit-xor unsigned-bit-shift-right
- Boolean: bool-and bool-not bool-or bool-xor
- Comparison: < <= == > >= eq even? neg? not== odd? pos? zero?
- Conversions: cnorm constrain cos-interpolation degrees lerp make-norm mlerp norm quad-interpolation radians smooth-interpolation smoothstep wrap
- Distance: dist hypot hypot-sqrt qdist
- Error: erf erfc inv-erf inv-erfc
- Mod: mod quot rem remainder
- Powers / logarithms: cbrt exp fpow high-2-exp high-exp ln log log10 log1p log2 logb low-2-exp low-exp pow pow2 pow3 qexp qlog qpow qsqrt rqsqrt safe-sqrt sigmoid sq sqrt
- primitive: * + - / dec inc
- Rounding: abs approx approx-eq ceil floor frac iabs itrunc qceil qfloor qround rint round round-up-pow2 sfrac trunc
- Sign: sgn signum
- Special functions: bessel-j digamma gamma inv-gamma-1pm1 log-beta log-gamma log-gamma-1p regularized-beta regularized-gamma-p regularized-gamma-q trigamma
- stat: max min
- Trigonometry: acos acosh acot acoth acsc acsch asec asech asin asinh atan atan2 atanh cos cosh cot coth csc csch qcos qsin sec sech sin sinc sinh tan tanh
Other vars: double-array->seq double-double-array->seq gcd lcm next-double prev-double sample seq->double-array seq->double-double-array unuse-primitive-operators use-primitive-operators
Constants
- -E =
-2.718281828459045
- -HALF_PI =
-1.5707963267948966
- -PI =
-3.141592653589793
- -QUARTER_PI =
-0.7853981633974483
- -TAU =
-6.283185307179586
- -THIRD_PI =
-1.0471975511965976
- -TWO_PI =
-6.283185307179586
- E =
2.718281828459045
- EPSILON =
1.0E-10
- GAMMA =
0.5772156649015329
- HALF_PI =
1.5707963267948966
- INV_LN2 =
1.4426950408889634
- INV_LOG_HALF =
-1.4426950408889634
- LANCZOS_G =
4.7421875
- LN10 =
2.302585092994046
- LN2 =
0.6931471805599453
- LN2_2 =
0.34657359027997264
- LOG10E =
0.4342944819032518
- LOG2E =
1.4426950408889634
- MACHINE-EPSILON =
1.1102230246251565E-16
- M_1_PI =
0.3183098861837907
- M_2_PI =
0.6366197723675814
- M_2_SQRTPI =
1.1283791670955126
- M_3PI_4 =
2.356194490192345
- M_E =
2.718281828459045
- M_INVLN2 =
1.4426950408889634
- M_IVLN10 =
0.43429448190325176
- M_LN10 =
2.302585092994046
- M_LN2 =
0.6931471805599453
- M_LOG10E =
0.4342944819032518
- M_LOG2E =
1.4426950408889634
- M_LOG2_E =
0.6931471805599453
- M_PI =
3.141592653589793
- M_PI_2 =
1.5707963267948966
- M_PI_4 =
0.7853981633974483
- M_SQRT1_2 =
0.7071067811865475
- M_SQRT2 =
1.4142135623730951
- M_SQRT3 =
1.7320508075688772
- M_SQRT_PI =
1.7724538509055159
- M_TWOPI =
6.283185307179586
- PHI =
1.618033988749895
- PI =
3.141592653589793
- QUARTER_PI =
0.7853981633974483
- SIXTH =
0.16666666666666666
- SQRT2 =
1.4142135623730951
- SQRT2PI =
2.5066282746310002
- SQRT2_2 =
0.7071067811865476
- SQRT3 =
1.7320508075688772
- SQRT3_2 =
0.8660254037844386
- SQRT3_3 =
0.5773502691896257
- SQRT5 =
2.23606797749979
- SQRTPI =
1.7724538509055159
- TAU =
6.283185307179586
- THIRD =
0.3333333333333333
- THIRD_PI =
1.0471975511965976
- TWO_PI =
6.283185307179586
- TWO_THIRD =
0.6666666666666666
- deg-in-rad =
0.017453292519943295
- double-array-type =
class [D
- double-double-array-type =
class [[D
- rad-in-deg =
57.29577951308232
>>>
macro
(>>> x y)
fastmath.java.PrimitiveMath/unsignedShiftRight function wrapped in macro.
acosh
macro
(acosh x)
net.jafama.FastMath/acosh function wrapped in macro.
Examples
Plot of acosh

approx
(approx v)
(approx v digits)
Round v
to specified (default: 2) decimal places. Be aware of double
number accuracy.
Examples
Default rounding (2 digits).
(approx 1.232323)
;;=> 1.23
Rounding up to 4 digits.
(approx 1.232323 4)
;;=> 1.2323
approx-eq
(approx-eq a b)
(approx-eq a b digits)
Checks equality approximately. See approx.
Examples
Default rounding (2 digits).
(approx-eq 1.232323 1.231999)
;;=> true
Rounding up to 4 digits.
(approx-eq 1.232323 1.23231999 4)
;;=> true
Keep an eye on rounding
(approx-eq 1.2349 1.235)
;;=> false
asinh
macro
(asinh x)
net.jafama.FastMath/asinh function wrapped in macro.
Examples
Plot of asinh

atanh
macro
(atanh x)
net.jafama.FastMath/atanh function wrapped in macro.
Examples
Plot of atanh

bit-and
macro
(bit-and x)
(bit-and x y)
(bit-and x y & rest)
A primitive math version of bit-and
bit-shift-left
macro
(bit-shift-left x y)
fastmath.java.PrimitiveMath/shiftLeft function wrapped in macro.
bit-shift-right
macro
(bit-shift-right x y)
fastmath.java.PrimitiveMath/shiftRight function wrapped in macro.
bit-xor
macro
(bit-xor x)
(bit-xor x y)
(bit-xor x y & rest)
A primitive math version of bit-xor
bool-and
macro
(bool-and x)
(bool-and x y)
(bool-and x y & rest)
A primitive math version of bool-and
bool-or
macro
(bool-or x)
(bool-or x y)
(bool-or x y & rest)
A primitive math version of bool-or
bool-xor
macro
(bool-xor x)
(bool-xor x y)
(bool-xor x y & rest)
A primitive math version of bool-xor
cnorm
(cnorm v start1 stop1 start2 stop2)
(cnorm v start stop)
Examples
Constrain result of norm.
(cnorm 1.5 0 1 100 200)
;;=> 200.0
(cnorm 555 200 500)
;;=> 1.0
constrain
macro
(constrain value mn mx)
Clamp value
to the range [mn,mx]
.
Examples
Examples
(constrain 0.5 1 2)
;;=> 1.0
(constrain 1.5 1 2)
;;=> 1.5
(constrain 2.5 1 2)
;;=> 2.0
cos-interpolation
(cos-interpolation start stop t)
oF interpolateCosine interpolation. See also lerp/mlerp, quad-interpolation or smooth-interpolation.
Examples
Example
(cos-interpolation 0.0 1.0 0.123)
;;=> 0.03686712449046664
Plot of cos-interpolation

degrees
(degrees rad)
Convert radians into degrees.
Examples
Let’s convert \(\pi\) radians to degrees.
(degrees PI)
;;=> 180.0
dist
(dist x1 y1 x2 y2)
Euclidean distance between points (x1,y1)
and (x2,y2)
. See fastmath.vector namespace to see other metrics which work on vectors.
Examples
Distance between two points.
(dist 1 3 -2 10)
;;=> 7.615773105863909
double-array->seq
Convert double array into sequence.
Alias for seq
.
Examples
Convert
(double-array->seq (seq->double-array [4 3 2]))
;;=> (4.0 3.0 2.0)
double-double-array->seq
(double-double-array->seq res)
Convert double array of double arrays into sequence of sequences.
Examples
Convert
(double-double-array->seq (seq->double-double-array
[[4 3 2] (double-array [1 2 3])]))
;;=> ((4.0 3.0 2.0) (1.0 2.0 3.0))
erf
macro
(erf x)
(erf x y)
Error function. For two arguments return difference between (erf x)
and (erf y)
.
Examples
Plot of erf

fpow
macro
(fpow x y)
Fast version of pow where exponent is integer.
Examples
Example
(fpow 1.23 4)
;;=> 2.28886641
(fpow 1.23 4.123)
;;=> 2.28886641
(fpow 1.23 4.999)
;;=> 2.28886641
(fpow 1.23 5)
;;=> 2.8153056842999997
(fpow 1.23 -2)
;;=> 0.6609822195782934
frac
(frac v)
Fractional part, always returns values from 0.0 to 1.0 (exclusive). See sfrac for signed version.
Examples
Examples
(frac 0.555)
;;=> 0.555
(frac -0.555)
;;=> 0.555
Plot of frac

gcd
(gcd a b)
Examples
Usage
(gcd 340 440)
;;=> 20
(gcd (* 123 5544331) (* 123 123))
;;=> 123
(gcd -234 -432)
;;=> 18
high-2-exp
(high-2-exp v)
Find lowest exponent (power of 2) which is greater or equal x
. See low-2-exp.
Examples
Result 5 means, that \(2^5=32\) is greater than 23.11. Lower exponent (4) gives lower value (16).
(high-2-exp 23.11)
;;=> 5
For
x
less than 1.0 gives negative exponent.
(high-2-exp 0.11)
;;=> -3
Plot of high-2-exp

high-exp
(high-exp b x)
Find lowest exponent for base b
which is higher or equalx
. See also low-exp.
Examples
Result
2
means, that \(9^2=81\) is greater than23.11
. Lower exponent1
gives lower value9
.
(high-exp 9 23.11)
;;=> 2
For
x
less than 1.0 gives negative exponent.
(high-exp 10 0.011)
;;=> -1
hypot-sqrt
(hypot-sqrt x y)
(hypot-sqrt x y z)
Hypot, sqrt version: \(\sqrt{x^2+y^2}\) or \(\sqrt{x^2+y^2+z^2}\). Should be faster than hypot.
inv-gamma-1pm1
macro
(inv-gamma-1pm1 x)
\(\frac{1}{\Gamma(1+x)}\).
Examples
Plot of inv-gamma-1pm1

itrunc
(itrunc v)
Truncate fractional part, keep sign. Returns long
.
Examples
Examples
(itrunc 1.234)
;;=> 1
(itrunc -1.544)
;;=> -1
lcm
(lcm a b)
Examples
Usage
(lcm 340 440)
;;=> 7480
(lcm (* 123 331) (* 123 123))
;;=> 5007699
(lcm 331 (* 123 123))
;;=> 5007699
(lcm -234 -432)
;;=> 5616
lerp
(lerp start stop t)
Linear interpolation between start
and stop
for amount t
. See also mlerp, cos-interpolation, quad-interpolation or smooth-interpolation.
Examples
Examples
(lerp 0.0 1.0 0.123)
;;=> 0.123
(lerp 0.0 100.0 0.123)
;;=> 12.3
(lerp 100 200 0.5)
;;=> 150.0
Interpolate outside given range.
(lerp -1.0 1.0 1.5)
;;=> 2.0
Plot of lerp

log-gamma-1p
macro
(log-gamma-1p x)
Gamma function \(\ln\Gamma(1+x)\)
Examples
Plot of log-gamma-1p

log1p
macro
(log1p x)
net.jafama.FastMath/log1p function wrapped in macro.
Examples
Plot of log1p

low-2-exp
(low-2-exp x)
Find greatest exponent (power of 2) which is lower or equal x
. See high-2-exp.
Examples
Result 4 means, that \(2^4=16\) is lower than 23.11. Next exponent (5) gives greater value (32).
(low-2-exp 23.11)
;;=> 4
For
x
less than 1.0 gives negative exponent.
(low-2-exp 0.11)
;;=> -4
Plot of low-2-exp

low-exp
(low-exp b x)
Find greatest exponent for base b
which is lower or equal x
. See also high-exp.
Examples
Result
1
means, that \(9^1=9\) is lower than23.11
. Next exponent2
gives greater value82
.
(low-exp 9 23.11)
;;=> 1
For
x
less than1.0
gives negative exponent.
(low-exp 10 0.011)
;;=> -2
MACHINE-EPSILON
const
;;=> 1.1102230246251565E-16
Smallest machine number. Value is calculated during evaluation and may differ on different processors.
make-norm
(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
.
Examples
Make cos() normalizer from [-1.0,1.0] to [0.0, 1.0].
(let [norm-cos (make-norm -1.0 1.0 0.0 1.0)] (norm-cos (cos 2.0)))
;;=> 0.29192658172642894
Make normalizer from [0,255] to any range.
(let [norm-0-255 (make-norm 0 255)]
[(norm-0-255 123 -10 -20) (norm-0-255 123 20 10)])
;;=> [-14.823529411764707 15.176470588235293]
mlerp
macro
(mlerp start stop t)
lerp as macro. For inline code. See also lerp, cos-interpolation, quad-interpolation or smooth-interpolation.
Examples
Examples
(mlerp 0.0 1.0 0.123)
;;=> 0.123
(mlerp 0.0 100.0 0.123)
;;=> 12.3
(mlerp 100 200 0.5)
;;=> 150.0
Interpolate outside given range.
(mlerp -1.0 1.0 1.5)
;;=> 2.0
mod
macro
(mod x y)
fastmath.java.PrimitiveMath/modulus function wrapped in macro.
Examples
Modulus (compared to
clojure.core
version).
(mod 3.123 0.333)
;;=> 0.12600000000000006
(mod -3.123 0.333)
;;=> 0.20699999999999996
(mod -3.123 -0.333)
;;=> -0.12600000000000006
(mod 3.123 -0.333)
;;=> -0.20699999999999996
(clojure.core/mod 3.123 0.333)
;;=> 0.1259999999999999
(clojure.core/mod -3.123 0.333)
;;=> 0.20700000000000013
(clojure.core/mod -3.123 -0.333)
;;=> -0.1259999999999999
(clojure.core/mod 3.123 -0.333)
;;=> -0.20700000000000013
next-double
(next-double v)
(next-double v delta)
Next double value. Optional value delta
sets step amount.
Examples
Next double.
(next-double 1234.56789)
;;=> 1234.5678900000003
Next double with delta.
(next-double 1234.56789 1000)
;;=> 1234.5678900002274
Plot of next-double

norm
(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.
Examples
Normalize from [1,-1] to [0,1]
(norm 0.234 -1.0 1.0)
;;=> 0.617
Normalize from [-1,1] to [0,1]
(norm 0.234 1.0 -1.0)
;;=> 0.383
Normalize cos() to [0,255]
(norm (cos HALF_PI) -1.0 1.0 0.0 255.0)
;;=> 127.5
Normalize cos() to [255,0]
(norm (cos HALF_PI) -1.0 1.0 255.0 0.0)
;;=> 127.5
prev-double
(prev-double v)
(prev-double v delta)
Previous double value. Optional value delta
sets step amount.
Examples
Prev double.
(prev-double 1234.56789)
;;=> 1234.5678899999998
Prev double with delta.
(prev-double 1234.56789 1000)
;;=> 1234.5678899997727
Plot of prev-double

qdist
(qdist x1 y1 x2 y2)
Examples
Distance between two points (quick version).
(qdist 1 3 -2 10)
;;=> 7.481406462931432
Distance between two points (accurate version).
(dist 1 3 -2 10)
;;=> 7.615773105863909
qsqrt
macro
(qsqrt x)
Approximated sqrt using binary operations with error 1.0E-2
.
Examples
Plot of qsqrt

quad-interpolation
(quad-interpolation start stop t)
Quad interpolation. See also lerp/mlerp, cos-interpolation or smooth-interpolation.
Examples
Example
(quad-interpolation 0.0 1.0 0.123)
;;=> 0.030258
Plot of quad-interpolation

quot
macro
(quot x y)
fastmath.java.PrimitiveMath/quotient function wrapped in macro.
Examples
Quotient (compared to
clojure.core
version).
(quot 3.123 0.333)
;;=> 9.0
(quot -3.123 0.333)
;;=> -9.0
(quot -3.123 -0.333)
;;=> 9.0
(quot 3.123 -0.333)
;;=> -9.0
(clojure.core/quot 3.123 0.333)
;;=> 9.0
(clojure.core/quot -3.123 0.333)
;;=> -9.0
(clojure.core/quot -3.123 -0.333)
;;=> 9.0
(clojure.core/quot 3.123 -0.333)
;;=> -9.0
radians
(radians deg)
Convert degrees into radians.
Examples
Let’s convert 180 degrees to radians.
(radians 180)
;;=> 3.141592653589793
rem
macro
(rem x y)
fastmath.java.PrimitiveMath/remainder function wrapped in macro.
Examples
Remainder (compared to
clojure.core
version).
(rem 3.123 0.333)
;;=> 0.12600000000000006
(rem -3.123 0.333)
;;=> -0.12600000000000006
(rem -3.123 -0.333)
;;=> -0.12600000000000006
(rem 3.123 -0.333)
;;=> 0.12600000000000006
(clojure.core/rem 3.123 0.333)
;;=> 0.1259999999999999
(clojure.core/rem -3.123 0.333)
;;=> -0.1259999999999999
(clojure.core/rem -3.123 -0.333)
;;=> -0.1259999999999999
(clojure.core/rem 3.123 -0.333)
;;=> 0.1259999999999999
remainder
macro
(remainder x y)
From FastMath
doc: returns dividend - divisor * n, where n is the mathematical integer closest to dividend/divisor. Returned value in [-|divisor|/2,|divisor|/2]
Examples
rint
(rint x)
Examples
Round to double.
(rint PI)
;;=> 3.0
Plot of rint

round
(round x)
Examples
Round to long.
(round PI)
;;=> 3
Plot of round

round-up-pow2
(round-up-pow2 v)
Round long to the next power of 2
Examples
Examples
(round-up-pow2 1023)
;;=> 1024
(round-up-pow2 1024)
;;=> 1024
(round-up-pow2 1025)
;;=> 2048
Plot of round-up-pow2

safe-sqrt
(safe-sqrt value)
Safe sqrt, for value <= 0 result is 0.
\( \left\{ \begin{array}{lr} 0 & : x \leq 0\\ \sqrt{x} & : x > 0 \end{array} \right. \)
Examples
Plot of safe-sqrt

sample
(sample f number-of-values)
(sample f number-of-values domain?)
(sample f range-min range-max number-of-values)
(sample f range-min range-max number-of-values domain?)
Sample function f
and return sequence of values.
range-min
defaults to 0.0, range-max
to 1.0.
Range is inclusive.
When optional domain?
is set to true (default: false) function returns pairs [x,(f x)]
.
Examples
Usage
(sample identity 10)
;;=> (0.0
;;=> 0.1111111111111111
;;=> 0.2222222222222222
;;=> 0.3333333333333333
;;=> 0.4444444444444444
;;=> 0.5555555555555556
;;=> 0.6666666666666666
;;=> 0.7777777777777778
;;=> 0.8888888888888888
;;=> 1.0)
(sample identity -11 22 5)
;;=> (-11.0 -2.75 5.5 13.75 22.0)
(sample sq 1 5 5)
;;=> (1.0 4.0 9.0 16.0 25.0)
(sample sq 1 5 5 true)
;;=> ([1.0 1.0] [2.0 4.0] [3.0 9.0] [4.0 16.0] [5.0 25.0])
seq->double-array
(seq->double-array vs)
Convert sequence to double-array.
If sequence is double-array do not convert.
Examples
Convert
(seq->double-array [1 2 3])
;;=> [D@5b820a87
(seq (seq->double-array [1 2 3]))
;;=> (1.0 2.0 3.0)
(double-array->seq (seq->double-array [1 2 3]))
;;=> (1.0 2.0 3.0)
Also works on number (treated as one element list).
(seq (seq->double-array 1))
;;=> (1.0)
seq->double-double-array
(seq->double-double-array vss)
Convert sequence to double-array of double-arrays.
If sequence is double-array of double-arrays do not convert
Examples
Convert
(seq->double-double-array [[1 2] [3 4]])
;;=> [[D@4f34e348
(double-double-array->seq (seq->double-double-array [[1 2] [3 4]]))
;;=> ((1.0 2.0) (3.0 4.0))
Also works on seq of numbers
(seq (second (seq->double-double-array [1 2 3])))
;;=> (2.0)
sfrac
(sfrac v)
Fractional part, always returns values from -1.0 to 1.0 (exclusive). See frac for unsigned version.
Examples
Examples
(sfrac 0.555)
;;=> 0.555
(sfrac -0.555)
;;=> -0.555
Plot of sfrac

sgn
(sgn value)
Return -1 when value
is negative, 1 otherwise. See also signum.
\( \left\{ \begin{array}{lr} 1.0 & : x \geq 0\\ -1.0 & : x < 0\\ \end{array} \right. \)
Examples
Plot of sgn

signum
(signum value)
Return 1 if value
is > 0, 0 if it is 0, -1 otherwise. See also sgn.
\( \left\{ \begin{array}{lr} 1.0 & : x > 0\\ -1.0 & : x < 0\\ 0.0 & : x = 0 \end{array} \right. \)
Examples
Plot of signum

smooth-interpolation
(smooth-interpolation start stop t)
Smoothstep based interpolation. See also lerp/mlerp, quad-interpolation or cos-interpolation.
Examples
Example
(smooth-interpolation 0.0 1.0 0.123)
;;=> 0.041665266
Plot of smooth-interpolation

smoothstep
(smoothstep edge0 edge1 x)
GL smoothstep.
Examples
x from range.
(smoothstep 100 200 120)
;;=> 0.10400000000000002
corner case (< x edge0)
(smoothstep 100 200 50)
;;=> 0.0
corner case (> x edge1)
(smoothstep 100 200 250)
;;=> 1.0
trunc
(trunc v)
Truncate fractional part, keep sign. Returns double
.
Examples
Examples
(trunc 1.234)
;;=> 1.0
(trunc -1.544)
;;=> -1.0
Plot of trunc

unsigned-bit-shift-right
macro
(unsigned-bit-shift-right x y)
fastmath.java.PrimitiveMath/unsignedShiftRight function wrapped in macro.
unuse-primitive-operators
(unuse-primitive-operators)
Undoes the work of use-primitive-operators. This is idempotent.
use-primitive-operators
(use-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.
wrap
(wrap start stop value)
Wrap overflowed value into the range, similar to ofWrap.
Examples
Example 1
(wrap 0 -1 1)
;;=> -1.0
Example 2 (value outside range)
(wrap -1.1 -1 1.5)
;;=> -1.0000000000000022
Example 3 (reversed range)
(wrap 0.7 0.5 1.0)
;;=> 0.6000000000000001
Plot of wrap
