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
Warning: All bool-
evaluate all parameters.
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.
Categories
- Bitwise: << >> >>> bit-and bit-not bit-or bit-shift-left bit-shift-right bit-xor unsigned-bit-shift-right
- Boolean: between-? between? bool-and bool-not bool-or bool-xor inf? invalid-double? nan? neg-inf? pos-inf? valid-double?
- Comparison: < <= == > >= between-? between? eq even? inf? invalid-double? nan? neg-inf? neg? not== odd? one? pos-inf? pos? valid-double? 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: gcd lcm mod quot rem remainder
- Powers / logarithms: cb cbrt exp expm1 fpow high-2-exp high-exp ln log log10 log1p log1pexp log2 logb logit low-2-exp low-exp pow pow2 pow3 qexp qlog qpow qsqrt rqsqrt safe-sqrt sigmoid sq sqrt
- Primitive: * + - / dec fast* fast+ fast- fast-identity fast-max fast-min inc max min unuse-primitive-operators use-primitive-operators
- Rank: order rank
- Rounding: abs approx approx-eq ceil floor frac iabs itrunc qceil qfloor qround rint round round-even round-up-pow2 sfrac trunc
- Sampling: sample
- Primitive <-> Seq converters: double-array->seq double-double-array->seq seq->double-array seq->double-double-array
- 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
- Trigonometry: acos acosh acot acoth acsc acsch asec asech asin asinh atan atan2 atanh cos cosh cot coth csc csch haversine haversine-dist qcos qsin sec sech sin sinc sinh tan tanh
Other vars: co-intervals cut group-by-intervals mnorm next-double prev-double slice-range
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.
abs
(abs x)
\(|x|\) - double
version. See iabs.
Examples
Usage
(fastmath.core/abs 1.0)
;;=> 1.0
Plot of abs
acos
macro
(acos x)
net.jafama.FastMath/acos function wrapped in macro.
Examples
Usage
(fastmath.core/acos 1.0)
;;=> 0.0
Plot of acos
acosh
macro
(acosh x)
net.jafama.FastMath/acosh function wrapped in macro.
Examples
Usage
(fastmath.core/acosh 1.0)
;;=> 0.0
Plot of acosh
acot
(acot v)
Arccotangent
Examples
Usage
(fastmath.core/acot 1.0)
;;=> 0.7853981633974483
Plot of acot
acoth
(acoth v)
Area hyperbolic cotangent
Examples
Usage
(fastmath.core/acoth 1.0)
;;=> Infinity
Plot of acoth
acsc
(acsc v)
Arccosecant
Examples
Usage
(fastmath.core/acsc 1.0)
;;=> 1.5707963267948966
Plot of acsc
acsch
(acsch v)
Area hyperbolic cosecant
Examples
Usage
(fastmath.core/acsch 1.0)
;;=> 0.8813735870195429
Plot of acsch
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
asech
(asech v)
Area hyperbolic secant
Examples
Usage
(fastmath.core/asech 1.0)
;;=> 0.0
Plot of asech
asin
macro
(asin x)
net.jafama.FastMath/asin function wrapped in macro.
Examples
Usage
(fastmath.core/asin 1.0)
;;=> 1.5707963267948966
Plot of asin
asinh
macro
(asinh x)
net.jafama.FastMath/asinh function wrapped in macro.
Examples
Usage
(fastmath.core/asinh 1.0)
;;=> 0.8813735870195429
Plot of asinh
atan
macro
(atan x)
net.jafama.FastMath/atan function wrapped in macro.
Examples
Usage
(fastmath.core/atan 1.0)
;;=> 0.7853981633974483
Plot of atan
atan2
macro
(atan2 x y)
net.jafama.FastMath/atan2 function wrapped in macro.
Examples
atan2 values
(degrees (atan2 1 1))
;;=> 45.0
(degrees (atan2 0 1))
;;=> 0.0
(degrees (atan2 0 -1))
;;=> 180.0
Plot of atan2
atanh
macro
(atanh x)
net.jafama.FastMath/atanh function wrapped in macro.
Examples
Usage
(fastmath.core/atanh 1.0)
;;=> Infinity
Plot of atanh
bessel-j
macro
(bessel-j x y)
Bessel J function value for given order and argument.
Examples
bessel-j values
(bessel-j 1 1)
;;=> 0.44005058574493355
(bessel-j 0 1)
;;=> 0.7651976865579666
(bessel-j 0 10)
;;=> -0.24593576445134827
Plot of bessel-j
between-?
(between-? [x y] v)
(between-? x y v)
Check if given number is within the range (x,y].
between?
(between? [x y] v)
(between? x y v)
Check if given number is within the range [x,y].
Examples
Examples
(between? [1 4] 2)
;;=> true
(between? [1 4] 5)
;;=> false
(between? [1 4] -2)
;;=> false
(between? [1 4] 1)
;;=> true
(between? 1 4 2)
;;=> true
(between? 1 4 5)
;;=> false
(between? 1 4 -2)
;;=> false
(between? 1 4 1)
;;=> true
;; Test: ok.
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
cbrt
macro
(cbrt x)
\(\sqrt[3]{x}\)
Examples
Usage
(fastmath.core/cbrt 1.0)
;;=> 1.0
Plot of cbrt
ceil
(ceil x)
\(\lceil x \rceil\). See: qceil.
Examples
Usage
(fastmath.core/ceil 1.0)
;;=> 1.0
Plot of ceil
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
co-intervals
(co-intervals data)
(co-intervals data number)
(co-intervals data number overlap)
Divide sequence to overlaping intervals containing similar number of values. Same as R’s co.intervals()
Examples
Usage
(co-intervals [1 2 3 1 2 3 4 5 6 1 2 3 4 5 6 7 1 1 1 1 -1])
;;=> ([-1.5 1.5] [0.5 2.5] [0.5 3.5] [1.5 4.5] [2.5 5.5] [3.5 7.5])
Higher overlap
(co-intervals [1 2 3 1 2 3 4 5 6 1 2 3 4 5 6 7 1 1 1 1 -1] 5 0.95)
;;=> ([-1.5 5.5] [0.5 5.5] [0.5 6.5] [0.5 7.5])
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
macro
(cos x)
net.jafama.FastMath/cos function wrapped in macro.
Examples
Usage
(fastmath.core/cos 1.0)
;;=> 0.5403023058681398
Plot of cos
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
cosh
macro
(cosh x)
net.jafama.FastMath/cosh function wrapped in macro.
Examples
Usage
(fastmath.core/cosh 1.0)
;;=> 1.543080634815244
Plot of cosh
cot
(cot v)
Cotangent
Examples
Usage
(fastmath.core/cot 1.0)
;;=> 0.6420926159343308
Plot of cot
coth
(coth v)
Hyperbolic cotangent
Examples
Usage
(fastmath.core/coth 1.0)
;;=> 1.3130352854993315
Plot of coth
csc
(csc v)
Cosecant
Examples
Usage
(fastmath.core/csc 1.0)
;;=> 1.1883951057781215
Plot of csc
csch
(csch v)
Hyperbolic cosecant
Examples
Usage
(fastmath.core/csch 1.0)
;;=> 0.8509181282393214
Plot of csch
degrees
(degrees rad)
Convert radians into degrees.
Examples
Let’s convert \(\pi\) radians to degrees.
(degrees PI)
;;=> 180.0
digamma
macro
(digamma x)
Logarithmic derivative of \(\Gamma\).
Examples
Usage
(fastmath.core/digamma 1.0)
;;=> -0.5772156677920679
Plot of digamma
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
Usage
(fastmath.core/erf 1.0)
;;=> 0.8427007929497151
Plot of erf
Plort of 2d erf
erfc
macro
(erfc x)
Complementary error function.
Examples
Usage
(fastmath.core/erfc 1.0)
;;=> 0.15729920705028488
Plot of erfc
exp
macro
(exp x)
net.jafama.FastMath/exp function wrapped in macro.
Examples
Usage
(fastmath.core/exp 1.0)
;;=> 2.7182818284590455
Plot of exp
expm1
macro
(expm1 x)
net.jafama.FastMath/expm1 function wrapped in macro.
Examples
Usage
(fastmath.core/expm1 1.0)
;;=> 1.7182818284590455
Plot of expm1
floor
(floor x)
\(\lfloor x \rfloor\). See: qfloor.
Examples
Usage
(fastmath.core/floor 1.0)
;;=> 1.0
Plot of floor
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
Usage
(fastmath.core/frac 1.0)
;;=> 0.0
Plot of frac
gamma
macro
(gamma x)
Gamma function \(\Gamma(x)\)
Examples
Usage
(fastmath.core/gamma 1.0)
;;=> 1.0
Plot of gamma
gcd
(gcd a b)
Fast binary greatest common divisor (Stein’s algorithm)
Examples
Usage
(gcd 340 440)
;;=> 20
(gcd (* 123 5544331) (* 123 123))
;;=> 123
(gcd -234 -432)
;;=> 18
group-by-intervals
(group-by-intervals coll)
(group-by-intervals intervals coll)
Group sequence of values into given intervals.
If intervals
are missing, use co-intervals to find some.
Examples
Examples
(group-by-intervals [1 2 3 4 1 2 3 4 5 1 -2])
;;=> {[-2.5 1.5] (1 1 1 -2),
;;=> [0.5 2.5] (1 2 1 2 1),
;;=> [1.5 3.5] (2 3 2 3),
;;=> [2.5 4.5] (3 4 3 4),
;;=> [3.5 5.5] (4 4 5)}
(group-by-intervals [[1 2] [3 4]] [1 2 3 4 1 2 3 4 5 1 -2])
;;=> {[1 2] (2 2), [3 4] (4 4)}
haversine
(haversine v)
(haversine [lat1 lon1] [lat2 lon2])
(haversine lat1 lon1 lat2 lon2)
Haversine formula
Examples
Usage
(haversine 10)
;;=> 0.9195357645382263
(haversine [10 20] [30 10])
;;=> 0.17694529423473296
(haversine 10 20 30 10)
;;=> 0.17694529423473296
Usage
(fastmath.core/haversine 1.0)
;;=> 0.22984884706593012
Plot of haversine
haversine-dist
(haversine-dist [lat1 lon1] [lat2 lon2])
(haversine-dist lat1 lon1 lat2 lon2)
Haversine distance d
for r=1
Examples
Usage
(haversine-dist [10 20] [30 10])
;;=> 0.8683203900985386
(haversine-dist 10 20 30 10)
;;=> 0.8683203900985386
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
Usage
(fastmath.core/high-2-exp 1.0)
;;=> 0
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.
Examples
Plot of hypot-sqrt
iabs
(iabs x)
\(|x|\) - long
version. See abs.
Examples
Usage
(fastmath.core/iabs 1.0)
;;=> 1
Plot of iabs
inf?
(inf? v)
Check if number is infinite
Examples
Usage
(inf? ##Inf)
;;=> true
(inf? (/ 0))
;;=> true
(inf? 1)
;;=> false
(inf? ##-Inf)
;;=> true
(inf? ##NaN)
;;=> false
;; Test: ok.
inv-erf
macro
(inv-erf x)
Inverse erf.
Examples
Usage
(fastmath.core/inv-erf 1.0)
;;=> Infinity
Plot of inv-erf
inv-erfc
macro
(inv-erfc x)
Inverse erfc.
Examples
Usage
(fastmath.core/inv-erfc 1.0)
;;=> 0.0
Plot of inv-erfc
inv-gamma-1pm1
macro
(inv-gamma-1pm1 x)
\(\frac{1}{\Gamma(1+x)}\).
Examples
Usage
(fastmath.core/inv-gamma-1pm1 1.0)
;;=> -0.0
Plot of inv-gamma-1pm1
invalid-double?
(invalid-double? v)
Check if number is invalid
Examples
Usage
(invalid-double? ##Inf)
;;=> true
(invalid-double? (/ 0))
;;=> true
(invalid-double? 1)
;;=> false
(invalid-double? ##-Inf)
;;=> true
(invalid-double? ##NaN)
;;=> true
;; Test: ok.
itrunc
(itrunc v)
Truncate fractional part, keep sign. Returns long
.
Examples
Examples
(itrunc 1.234)
;;=> 1
(itrunc -1.544)
;;=> -1
Usage
(fastmath.core/itrunc 1.0)
;;=> 1
Plot of itrunc
lcm
(lcm a b)
Fast binary least common multiplier.
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
ln
macro
(ln x)
net.jafama.FastMath/log function wrapped in macro.
Examples
Usage
(fastmath.core/ln 1.0)
;;=> 0.0
Plot of ln
log
macro
(log x)
net.jafama.FastMath/log function wrapped in macro.
Examples
Usage
(fastmath.core/log 1.0)
;;=> 0.0
Plot of log
log-gamma
macro
(log-gamma x)
Gamma function \(\ln\Gamma(x)\)
Examples
Usage
(fastmath.core/log-gamma 1.0)
;;=> -0.0
Plot of log-gamma
log-gamma-1p
macro
(log-gamma-1p x)
Gamma function \(\ln\Gamma(1+x)\)
Examples
Usage
(fastmath.core/log-gamma-1p 1.0)
;;=> 0.0
Plot of log-gamma-1p
log10
macro
(log10 x)
\(\ln_{10}{x}\)
Examples
Usage
(fastmath.core/log10 1.0)
;;=> 0.0
Plot of log10
log1p
macro
(log1p x)
net.jafama.FastMath/log1p function wrapped in macro.
Examples
Usage
(fastmath.core/log1p 1.0)
;;=> 0.6931471805599453
Plot of log1p
log1pexp
(log1pexp x)
log(1+exp(x))
Examples
Usage
(fastmath.core/log1pexp 1.0)
;;=> 1.3132616875182228
Plot of log1pexp
log2
(log2 x)
Logarithm with base 2.
\(\ln_2{x}\)
Examples
Usage
(fastmath.core/log2 1.0)
;;=> 0.0
Plot of log2
logit
(logit x)
Logit function
Examples
Usage
(fastmath.core/logit 1.0)
;;=> Infinity
Plot of logit
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
Usage
(fastmath.core/low-2-exp 1.0)
;;=> 0
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
mnorm
macro
(mnorm v start stop)
(mnorm v start1 stop1 start2 stop2)
Macro version of norm.
Examples
Normalize from [1,-1] to [0,1]
(mnorm 0.234 -1.0 1.0)
;;=> 0.617
Normalize from [-1,1] to [0,1]
(mnorm 0.234 1.0 -1.0)
;;=> 0.383
Normalize cos() to [0,255]
(mnorm (cos HALF_PI) -1.0 1.0 0.0 255.0)
;;=> 127.5
Normalize cos() to [255,0]
(mnorm (cos HALF_PI) -1.0 1.0 255.0 0.0)
;;=> 127.5
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
nan?
(nan? v)
Check if number is NaN
Examples
Usage
(nan? ##Inf)
;;=> false
(nan? (/ 0))
;;=> false
(nan? 1)
;;=> false
(nan? ##-Inf)
;;=> false
(nan? ##NaN)
;;=> true
;; Test: ok.
neg-inf?
(neg-inf? v)
Check if number is negatively infinite
Examples
Usage
(neg-inf? ##Inf)
;;=> false
(neg-inf? (/ 0))
;;=> false
(neg-inf? 1)
;;=> false
(neg-inf? ##-Inf)
;;=> true
(neg-inf? ##NaN)
;;=> false
;; Test: ok.
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
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
order
(order vs)
(order vs decreasing?)
Ordering permutation. See R docs
Order uses 0 based indexing.
Examples
Usage
(order [1 1 3 2 1 1 2 3 4 3])
;;=> (0 1 4 5 3 6 2 7 9 8)
;; Test: ok.
Reverse order
(order [1 1 3 2 1 1 2 3 4 3] true)
;;=> (8 2 7 9 3 6 0 1 4 5)
;; Test: ok.
pos-inf?
(pos-inf? v)
Check if number is positively infinite
Examples
Usage
(pos-inf? ##Inf)
;;=> true
(pos-inf? (/ 0))
;;=> true
(pos-inf? 1)
;;=> false
(pos-inf? ##-Inf)
;;=> false
(pos-inf? ##NaN)
;;=> false
;; Test: ok.
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
qcos
macro
(qcos x)
Fast and less accurate cos.
Examples
Usage
(fastmath.core/qcos 1.0)
;;=> 0.540171472729893
Plot of qcos
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
qexp
macro
(qexp x)
Quick and less accurate version of exp.
Examples
Usage
(fastmath.core/qexp 1.0)
;;=> 2.799318313598633
Plot of qexp
qlog
macro
(qlog x)
Fast and less accurate version of log.
Examples
Usage
(fastmath.core/qlog 1.0)
;;=> 0.0
Plot of qlog
qsin
macro
(qsin x)
Fast and less accurate sin.
Examples
Usage
(fastmath.core/qsin 1.0)
;;=> 0.8415549774368984
Plot of qsin
qsqrt
macro
(qsqrt x)
Approximated sqrt using binary operations with error 1.0E-2
.
Examples
Usage
(fastmath.core/qsqrt 1.0)
;;=> 0.982050807866429
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
rank
(rank vs)
(rank vs ties)
Sample ranks. See R docs.
Rank uses 0 based indexing.
Possible tie strategies: :average
, :first
, :last
, :random
, :min
, :max
, :dense
.
:dense
is the same as in data.table::frank
from R
Examples
Usage
(rank [3 1 4 1 5 9 2 6 5 3 5])
;;=> (3.5 0.5 5.0 0.5 7.0 10.0 2.0 9.0 7.0 3.5 7.0)
(rank [3 1 4 1 5 9 2 6 5 3 5] :average)
;;=> (3.5 0.5 5.0 0.5 7.0 10.0 2.0 9.0 7.0 3.5 7.0)
(rank [3 1 4 1 5 9 2 6 5 3 5] :first)
;;=> [3 0 5 1 6 10 2 9 7 4 8]
(rank [3 1 4 1 5 9 2 6 5 3 5] :last)
;;=> [4 1 5 0 8 10 2 9 7 3 6]
(rank [3 1 4 1 5 9 2 6 5 3 5] :min)
;;=> (3 0 5 0 6 10 2 9 6 3 6)
(rank [3 1 4 1 5 9 2 6 5 3 5] :max)
;;=> (4 1 5 1 8 10 2 9 8 4 8)
(rank [3 1 4 1 5 9 2 6 5 3 5] :dense)
;;=> (2 0 3 0 4 6 1 5 4 2 4)
;; Test: ok.
Random ties
(rank [3 1 4 1 5 9 2 6 5 3 5] :random)
;;=> [4 1 5 0 6 10 2 9 7 3 8]
(rank [3 1 4 1 5 9 2 6 5 3 5] :random)
;;=> [4 1 5 0 8 10 2 9 7 3 6]
(rank [3 1 4 1 5 9 2 6 5 3 5] :random)
;;=> [4 1 5 0 6 10 2 9 8 3 7]
(rank [3 1 4 1 5 9 2 6 5 3 5] :random)
;;=> [3 1 5 0 6 10 2 9 8 4 7]
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
Usage
(fastmath.core/rint 1.0)
;;=> 1.0
Plot of rint
round
(round x)
Examples
Round to long.
(round PI)
;;=> 3
Usage
(fastmath.core/round 1.0)
;;=> 1
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
Usage
(fastmath.core/round-up-pow2 1.0)
;;=> 1
Plot of round-up-pow2
rqsqrt
macro
(rqsqrt x)
Inversed version of qsqrt. Quick and less accurate.
Examples
Usage
(fastmath.core/rqsqrt 1.0)
;;=> 0.9662250423950712
Plot of rqsqrt
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
Usage
(fastmath.core/safe-sqrt 1.0)
;;=> 1.0
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])
sech
(sech v)
Hyperbolic secant
Examples
Usage
(fastmath.core/sech 1.0)
;;=> 0.6480542736638853
Plot of sech
seq->double-array
(seq->double-array vs)
Convert sequence to double array. Returns input if vs
is double array already.
Examples
Convert
(seq->double-array [1 2 3])
;;=> [D@123561f2
(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 returns vss
Examples
Convert
(seq->double-double-array [[1 2] [3 4]])
;;=> [[D@42f3dac6
(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
Usage
(fastmath.core/sfrac 1.0)
;;=> 0.0
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
Usage
(fastmath.core/sgn 1.0)
;;=> 1.0
Plot of sgn
sigmoid
(sigmoid x)
Sigmoid function
Examples
Usage
(fastmath.core/sigmoid 1.0)
;;=> 0.7310585786300049
Plot of sigmoid
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
Usage
(fastmath.core/signum 1.0)
;;=> 1.0
Plot of signum
sin
macro
(sin x)
net.jafama.FastMath/sin function wrapped in macro.
Examples
Usage
(fastmath.core/sin 1.0)
;;=> 0.8414709848078964
Plot of sin
sinc
(sinc v)
Sinc function.
Examples
Usage
(fastmath.core/sinc 1.0)
;;=> 3.898171832295186E-17
Plot of sinc
sinh
macro
(sinh x)
net.jafama.FastMath/sinh function wrapped in macro.
Examples
Usage
(fastmath.core/sinh 1.0)
;;=> 1.1752011936438016
Plot of sinh
slice-range
(slice-range start end cnt)
(slice-range cnt)
Slice range to get cnt
number of points evenly distanced.
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
tan
macro
(tan x)
net.jafama.FastMath/tan function wrapped in macro.
Examples
Usage
(fastmath.core/tan 1.0)
;;=> 1.5574077246549018
Plot of tan
tanh
macro
(tanh x)
net.jafama.FastMath/tanh function wrapped in macro.
Examples
Usage
(fastmath.core/tanh 1.0)
;;=> 0.7615941559557649
Plot of tanh
trigamma
macro
(trigamma x)
Derivative of digamma.
Examples
Usage
(fastmath.core/trigamma 1.0)
;;=> 1.6449340668481562
Plot of trigamma
trunc
(trunc v)
Truncate fractional part, keep sign. Returns double
.
Examples
Examples
(trunc 1.234)
;;=> 1.0
(trunc -1.544)
;;=> -1.0
Usage
(fastmath.core/trunc 1.0)
;;=> 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.
valid-double?
(valid-double? v)
Check if number is invalid
Examples
Usage
(valid-double? ##Inf)
;;=> false
(valid-double? (/ 0))
;;=> false
(valid-double? 1)
;;=> true
(valid-double? ##-Inf)
;;=> false
(valid-double? ##NaN)
;;=> false
;; Test: ok.
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