Core
Collection of type hinted math macros and functions. Partially backed by Java static functions and exposed as macros. They are prepared to accept primitive long or double arguments and return long or double only.
There is a possibility to replace clojure.core
functions with a selection of fastmath.core
macros. Call:
(m/use-primitive-operators)
to replace functions with macros(m/unuse-primitive-operators)
to revert replacement.
Be aware that there are some differences and fastmath.core
versions shoudn’t be treated as a drop-in replacement for clojure.core
versions. Also, since Clojure 1.12, always call unuse-primitive-operators
at the end of the namespace.
Here is the complete list of replaced functions:
* + - /
> < >= <= ==
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
require '[fastmath.core :as m]) (
Basic operations
Basic math operations.
When used in an expression oprations are inlined and can accept mixture of long
and double
values. If all values are of long
primitive type, long
is returned, double
otherwise.
When used in higher order function, double
is returned always. To operate on long
primitive type, reach for long-
versions.
+
,-
,*
,/
,quot
inc
,dec
min
,max
,smooth-max
rem
,mod
,remainder
,wrap
abs
long
versions
long-add
,long-sub
,long-mult
,long-div
,long-quot
long-inc
,long-dec
long-min
,long-max
long-rem
,long-quot
,long-mod
long-abs
Arithmetics
- addition, incrementation
- subtraction, decrementation
- multiplication
- division
Please note that there some differences between division in fastmath
and clojure.core
- when called with one argument (
double
orlong
)m//
always returns reciprocal (clojure.core//
returns a ratio) - when called on
long
arguments,m//
is a long division (clojure.core//
returns a ratio) m//
for twolong
arguments is equivalent tom/quot
Addition
;; => 0.0
(m/+) 1 2 3 4) ;; => 10
(m/+ 1.0 2.5 3 4) ;; => 10.5
(m/+ reduce m/+ [1 2 3]) ;; => 6.0 (
;; => 0
(m/long-add) 1 2 3 4) ;; => 10
(m/long-add 1.0 2.5 3 4) ;; => 10
(m/long-add reduce m/long-add [1 2 3.5]) ;; => 6 (
Subtraction
1) (m/- 1.0)] ;; => [-1 -1.0]
[(m/- 1 2 3 4) ;; => -8
(m/- 1.0 2.5 3 4) ;; => -8.5
(m/- reduce m/- [1 2 3]) ;; => -4.0 (
1) ;; => -1
(m/long-sub 1 2 3 4) ;; => -8
(m/long-sub 1.0 2.5 3 4) ;; => -8
(m/long-sub reduce m/long-sub [1 2 3.5]) ;; => -4 (
Multiplication
;; => 1.0
(m/*) 1 2 3 4) ;; => 24
(m/* 1.0 2.5 3 4) ;; => 30.0
(m/* reduce m/* [1 2 3]) ;; => 6.0 (
;; => 1
(m/long-mult) 1 2 3 4) ;; => 24
(m/long-mult 1.0 2.5 3 4) ;; => 24
(m/long-mult reduce m/long-mult [1 2 3.5]) ;; => 6 (
Division
2) (m// 2) (/ 2)] ;; => [0.5 0.5 1/2]
[(m// 1 2 3 4) ;; => 0
(m// 1.0 2.5 3 4) ;; => 0.03333333333333333
(m// reduce m// [1 2 3]) ;; => 0.16666666666666666
(10.5 -3) ;; => -3.0 (m/quot
2) ;; => 0.5
(m/long-div 100 5 3) ;; => 6
(m/long-div 100.5 2.5 3) ;; => 16
(m/long-div reduce m/long-div [100 2 3.5]) ;; => 16
(10 -3) ;; => -3 (m/long-quot
Increment and decrement
4) ;; => 5
(m/inc 4.5) ;; => 5.5
(m/inc 4) ;; => 3
(m/dec 4.5) ;; => 3.5
(m/dec map m/inc [1 2 3.5 4.5]) ;; => (2.0 3.0 4.5 5.5) (
4) ;; => 5
(m/long-inc 4.5) ;; => 5
(m/long-inc 4) ;; => 3
(m/long-dec 4.5) ;; => 3
(m/long-dec map m/long-inc [1 2 3.5 4.5]) ;; => (2 3 4 5) (
Remainders
rem
andmod
are the same as inclojure.core
,remainder
returns \(dividend - divisor * n\), where \(n\) is the mathematical integer closest to \(\frac{dividend}{divisor}\). Returned value is inside the \([\frac{-|divisor|}{2},\frac{|divisor|}{2}]\) range.wrap
wraps the value to be within given interval (right open) \([a,b)\) `
10 4) ;; => 2
(m/mod 10.25 4.0) ;; => 1.75
(m/mod -10.25 -4.0) ;; => -1.75
(m/mod 10.25 -4.0) ;; => -2.25
(m/mod -10 4) ;; => 2
(m/rem 10.25 4.0) ;; => -2.25
(m/rem -10.25 -4.0) ;; => 2.25
(m/rem 10.25 -4.0) ;; => -2.25
(m/rem -10 4) ;; => 2.0
(m/remainder 10.25 4.0) ;; => 1.75
(m/remainder -10.25 -4.0) ;; => -1.75
(m/remainder 10.25 -4.0) ;; => 1.75
(m/remainder -1.25 1.25 1.0) ;; => 1.0
(m/wrap -1.25 1.25 1.35) ;; => -1.15
(m/wrap -1.25 1.25 -1.25) ;; => -1.25
(m/wrap -1.25 1.25 1.25) ;; => -1.25
(m/wrap -1.25 1.25] -1.35) ;; => 1.15 (m/wrap [-
Min, max, abs
1 2 -3) ;; => -3
(m/min 1.0 2 -3) ;; => -3.0
(m/min 1 2 -3) ;; => 2
(m/max 1.0 2 -3) ;; => 2.0
(m/max 3) ;; => 3
(m/abs -3) ;; => 3
(m/long-abs -3.5) ;; => 3.5
(m/abs -3.5) ;; => 3 (m/long-abs -
Smooth maximum
Smooth maximum is a family of functions \(\max_\alpha(xs)\) for which \(\lim_{\alpha\to\infty}\max_\alpha(xs)=\max(xs)\).
Five types of smooth maximum are defined (see wikipedia for formulas):
:lse
- LogSumExp (default):boltzmann
- Boltzmann operator, works for small alpha values:mellowmax
:p-norm
:smu
- smooth maximum unit, \(\epsilon=\frac{1}{\alpha}\)
:lse
, :boltzmann
and :mellowmax
are also smooth minimum for negative \(\alpha\) values.
The following plots show value of the smooth max for different \(\alpha\) and set of the numbers equal to [-3.5 -2 -1 0.1 3 4]
. Blue dashed horizontal lines are minimum (-3.5) and maximum values (4.0).
The following plots are defined only for positive \(\alpha\).
3.5 -2 -1 0.1 3 4] 4.0 :lse) ;; => 4.004537523710555
(m/smooth-max [-3.5 -2 -1 0.1 3 4] -4.0 :lse) ;; => -3.500630381944282
(m/smooth-max [-3.5 -2 -1 0.1 3 4] 4.0 :boltzmann) ;; => 3.9820131397304284
(m/smooth-max [-3.5 -2 -1 0.1 3 4] -4.0 :boltzmann) ;; => -3.496176019710726
(m/smooth-max [-3.5 -2 -1 0.1 3 4] 4.0 :mellowmax) ;; => 3.5565976564035413
(m/smooth-max [-3.5 -2 -1 0.1 3 4] -4.0 :mellowmax) ;; => -3.0526905146372685
(m/smooth-max [-3.5 -2 -1 0.1 3 4] 4.0 :p-norm) ;; => 4.738284340366858
(m/smooth-max [-3.5 -2 -1 0.1 3 4] 4.0 :smu) ;; => 4.060190281957045 (m/smooth-max [-
fma
Fused multiply-add \(fma(a,b,c)=a+b*c\) is the operation implemented with better accuracy in Java 9+ and as one instruction (see more here and here). When Java 8 is used fma
is replaced with direct a+b*c
formula.
fma
,muladd
,negmuladd
difference-of-products
,sum-of-products
\[\operatorname{fma}(a,b,c)=\operatorname{muladd}(a,b,c)=a+bc\] \[\operatorname{negmuladd}(a,b,c)=\operatorname{fma}(-a,b,c)\]
difference-of-products
(dop) and sum-of-products
(sop) are using Kahan’s algorithm to avoid catastrophic cancellation.
\[\operatorname{dop}(a,b,c,d)=ab-cd=\operatorname{fma}(a,b,-cd)+\operatorname{fma}(-c,d,cd)\] \[\operatorname{sop}(a,b,c,d)=ab+cd=\operatorname{fma}(a,b,cd)+\operatorname{fma}(c,d,-cd)\]
The following example shows that \(x^2-y^2\) differs from the best floating point approximation which is equal 1.8626451518330422e-9
.
let [x (m/inc (m/pow 2 -29))
(2 -30))]
y (m/inc (m/pow :proper-value (m/difference-of-products x x y y)
{:wrong-value (m/- (m/* x x) (m/* y y))})
:proper-value 1.8626451518330422E-9, :wrong-value 1.862645149230957E-9} {
3 4 5) ;; => 17.0
(m/fma 3 4 5) ;; => 17.0
(m/muladd 3 4 5) ;; => -7.0
(m/negmuladd 3 3 4 4) ;; => -7.0
(m/difference-of-products 3 3 4 4) ;; => 25.0 (m/sum-of-products
Rounding
Various rounding functions.
floor
,ceil
andrint
accept additional argument,scale
, which allows to round to the nearest multiple of scale.round
returnslong
whilerint
returnsdouble
approx
rounds number to the given number of digits, usesbigdec
trunc
returns integer part of a number,frac
returns fractional parttrunc
returnsdouble
whileitrunc
returns longsfrac
keeps sign of the argumentqfloor
,qceil
andqround
are implemented using casting tolong
floor
,ceil
round
,round-even
,rint
,approx
,trunc
,itrunc
qfloor
,qceil
,qround
frac
,sfrac
round-up-pow2
map m/floor [-10.5 10.5]) ;; => (-11.0 10.0)
(10.5 4.0) ;; => 8.0
(m/floor map m/ceil [-10.5 10.5]) ;; => (-10.0 11.0)
(10.5 4.0) ;; => 12.0
(m/ceil map m/rint [-10.51 -10.5 -10.49 10.49 10.5 10.51]) ;; => (-11.0 -10.0 -10.0 10.0 10.0 11.0)
(10.5 4.0) ;; => 12.0
(m/rint 10.591 0.1) ;; => 10.600000000000001
(m/rint map m/round [-10.51 -10.5 -10.49 10.49 10.5 10.51]) ;; => (-11 -10 -10 10 11 11)
(map m/round-even [-10.51 -10.5 -10.49 10.49 10.5 10.51]) ;; => (-11 -10 -10 10 10 11)
(map m/qfloor [-10.5 10.5]) ;; => (-11 10)
(map m/qceil [-10.5 10.5]) ;; => (-10 11)
(map m/qround [-10.51 -10.5 -10.49 10.49 10.5 10.51]) ;; => (-11 -11 -10 10 11 11)
(map m/trunc [-10.591 10.591]) ;; => (-10.0 10.0)
(map m/itrunc [-10.591 10.591]) ;; => (-10 10)
(10.591) ;; => 10.59
(m/approx 10.591 1) ;; => 10.6
(m/approx 10.591 0) ;; => 11.0
(m/approx 10.591) ;; => -10.59
(m/approx -10.591 1) ;; => -10.6
(m/approx -10.591 0) ;; => -11.0
(m/approx -map m/frac [-10.591 10.591]) ;; => (0.5909999999999993 0.5909999999999993)
(map m/sfrac [-10.591 10.591]) ;; => (-0.5909999999999993 0.5909999999999993)
(map m/round-up-pow2 (range 10)) ;; => (0 1 2 4 4 8 8 8 8 16) (
The difference between rint
and round
. round
is bounded by minimum and maximum long
values.
1.23456789E30) ;; => 1.23456789E30
(m/rint 1.23456789E30) ;; => 9223372036854775807 (m/round
Sign
Sign of the number.
signum
andsgn
copy-sign
\[\operatorname{signum}(x)=\begin{cases} -1 & x<0 \\ 1 & x>0 \\ 0 & x=0 \end{cases}\]
\[\operatorname{sgn}(x)=\begin{cases} -1 & x<0 \\ 1 & x\geq 0 \end{cases}\]
copy-sign
sets the sign of the second argument to the first. Please note that -0.0
is negative and 0.0
is positive.
\[\operatorname{copy-sign}(x,y)=\begin{cases} |x| & y>0 \lor y=0.0\\ -|x| & y<0 \lor y=-0.0 \end{cases}\]
2.5) ;; => -1.0
(m/signum -2.5) ;; => 1.0
(m/signum 2.5) ;; => -1.0
(m/sgn -2.5) ;; => 1.0
(m/sgn 0) ;; => 0.0
(m/signum 0) ;; => 1.0
(m/sgn 123 -10) ;; => -123.0
(m/copy-sign 123 10) ;; => 123.0
(m/copy-sign -123 -0.0) ;; => -123.0
(m/copy-sign 123 0.0) ;; => 123.0 (m/copy-sign -
Predicates
Trigonometry
Power and logarithms
Bitwise operations
Floating point
Other
GCD and LCM
Combinatorics
Rank and order
Sampling
Interpolation and mapping
Constants
Reference
fastmath.core
Collection of basic math functions and constants.
Contains:
- Basic math functions
- Predicates
- Bitwise operations
- Trigonometry
- Log/power
- Floating point format operations
- Factorial, combinations, gcd/lcd
- Additional functions: sampling, rank, lerp
Almost all math functions are backed by FastMath library. Almost all operates on primitive double
and returns double
or long
and are inlined.
#### Primitive math operators
Inlined function operating on double/longs as a replacement of Clojure numerical tower: *
+
-
/
>
<
>=
<=
==
rem
quot
mod
bit-or
bit-and
bit-xor
bit-and-not
bit-set
bit-clear
bit-test
bit-flip
bit-not
bit-shift-left
bit-shift-right
unsigned-bit-shift-right
inc
dec
zero?
neg?
pos?
min
max
even?
odd?
abs
And additionally:
<<
- 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 which is recomended when Clojure 1.12+ is used.
*
(*)
(* a)
(* a b)
(* a b c)
(* a b c d)
(* a b c d & r)
Primitive and inlined *
.
+
(+)
(+ a)
(+ a b)
(+ a b c)
(+ a b c d)
(+ a b c d & r)
Primitive and inlined +
.
-
(- a)
(- a b)
(- a b c)
(- a b c d)
(- a b c d & r)
Primitive and inlined -
.
-E CONST
-E = -2.718281828459045
Value of \(-e\)
-HALF_PI CONST
-HALF_PI = -1.5707963267948966
Value of \(-\frac{\pi}{2}\)
-PI CONST
-PI = -3.141592653589793
Value of \(-\pi\)
-QUARTER_PI CONST
-QUARTER_PI = -0.7853981633974483
Value of \(-\frac{\pi}{4}\)
-TAU CONST
-TAU = -6.283185307179586
Alias for TWO_PI-
-THIRD_PI CONST
-THIRD_PI = 1.0471975511965976
Value of \(-\frac{\pi}{3}\)
-TWO_PI CONST
-TWO_PI = -6.283185307179586
Value of \(-2 {\pi}\)
/
(/ a)
(/ a b)
(/ a b c)
(/ a b c d)
(/ a b c d & r)
Primitive and inlined /
.
<
(< _)
(< a b)
(< a b & r)
Primitive math less-then function.
<<
(<< x shift)
Shift bits left
<=
(<= _)
(<= a b)
(<= a b & r)
Primitive math less-and-equal function.
==
(== _)
(== a b)
(== a b & r)
Primitive math equality function.
>
(> _)
(> a b)
(> a b & r)
Primitive math greater-than function.
>=
(>= _)
(>= a b)
(>= a b & r)
Primitive math greater-and-equal function.
>>
(>> x shift)
Shift bits right and keep most significant bit unchanged
>>>
(>>> x shift)
Shift bits right and set most significant bit to 0
CATALAN_G CONST
CATALAN_G = 0.915965594177219
Catalan G
E CONST
E = 2.718281828459045
Value of \(e\)
EPSILON CONST
EPSILON = 1.0E-10
Very small number \(\varepsilon\)
FOUR_INV_PI CONST
FOUR_INV_PI = 1.2732395447351628
Value of \(\frac{4}{\pi}\)
GAMMA CONST
GAMMA = 0.5772156649015329
Euler-Mascheroni constant
HALF_PI CONST
HALF_PI = 1.5707963267948966
Value of \(\frac{\pi}{2}\)
INV_FOUR_PI CONST
INV_FOUR_PI = 0.07957747154594767
Value of \(\frac{1}{4 \pi}\)
INV_LN2 CONST
INV_LN2 = 1.4426950408889634
\(\frac{1}{\ln{2}}\)
INV_LOG_HALF CONST
INV_LOG_HALF = -1.4426950408889634
\(\frac{1}{\ln{0.5}}\)
INV_PI CONST
INV_PI = 0.3183098861837907
Value of \(\frac{1}{\pi}\)
INV_SQRT2PI CONST
INV_SQRT2PI = 0.3989422804014327
\(\frac{1}{\sqrt{2\pi}}\)
INV_SQRTPI CONST
INV_SQRTPI = 0.5641895835477563
\(\frac{1}{\sqrt\pi}\)
INV_SQRT_2 CONST
INV_SQRT_2 = 0.7071067811865475
\(\frac{1}{\sqrt{2}}\)
INV_TWO_PI CONST
INV_TWO_PI = 0.15915494309189535
Value of \(\frac{1}{2 \pi}\)
LANCZOS_G CONST
LANCZOS_G = 4.7421875
Lanchos approximation g
constant
LN10 CONST
LN10 = 2.302585092994046
\(\ln{10}\)
LN2 CONST
LN2 = 0.6931471805599453
\(\ln{2}\)
LN2_2 CONST
LN2_2 = 0.34657359027997264
\(\frac{\ln{2}}{2}\)
LOG10E CONST
LOG10E = 0.4342944819032518
\(\log_{10}{e}\)
LOG2E CONST
LOG2E = 1.4426950408889634
\(\log_{2}{e}\)
LOG_HALF CONST
LOG_HALF = -0.6931471805599453
\(\ln{0.5}\)
LOG_PI CONST
LOG_PI = 1.1447298858494002
\(\ln{\pi}\)
LOG_TWO_PI CONST
LOG_TWO_PI = 1.8378770664093453
\(\ln{2 \pi}\)
MACHINE-EPSILON CONST
MACHINE-EPSILON = 1.1102230246251565E-16
ULP(1)/2. Half of the smallest difference between 1.0 and next possible double floating point number.
MACHINE-EPSILON10 CONST
MACHINE-EPSILON10 = 1.1102230246251565E-15
5*ULP(1)
M_1_PI CONST
M_1_PI = 0.3183098861837907
\(\frac{1}{\pi}\)
M_2_PI CONST
M_2_PI = 0.6366197723675814
\(\frac{2}{\pi}\)
M_2_SQRTPI CONST
M_2_SQRTPI = 1.1283791670955126
\(\frac{2}{\sqrt\pi}\)
M_3PI_4 CONST
M_3PI_4 = 2.356194490192345
\(\frac{3\pi}{4}\)
M_E CONST
M_E = 2.718281828459045
\(e\)
M_INVLN2 CONST
M_INVLN2 = 1.4426950408889634
\(\frac{1}{\ln{2}}\)
M_IVLN10 CONST
M_IVLN10 = 0.43429448190325176
\(\frac{1}{\ln{10}}\)
M_LN10 CONST
M_LN10 = 2.302585092994046
\(\ln{10}\)
M_LN2 CONST
M_LN2 = 0.6931471805599453
\(\ln{2}\)
M_LOG10E CONST
M_LOG10E = 0.4342944819032518
\(\log_{10}{e}\)
M_LOG2E CONST
M_LOG2E = 1.4426950408889634
\(\log_{2}{e}\)
M_LOG2_E CONST
M_LOG2_E = 0.6931471805599453
\(\ln{2}\)
M_PI CONST
M_PI = 3.141592653589793
\(\pi\)
M_PI_2 CONST
M_PI_2 = 1.5707963267948966
\(\frac{\pi}{2}\)
M_PI_4 CONST
M_PI_4 = 0.7853981633974483
\(\frac{\pi}{4}\)
M_SQRT1_2 CONST
M_SQRT1_2 = 0.7071067811865475
\(\frac{1}{\sqrt{2}}\)
M_SQRT2 CONST
M_SQRT2 = 1.4142135623730951
\(\sqrt{2}\)
M_SQRT3 CONST
M_SQRT3 = 1.7320508075688772
\(\sqrt{3}\)
M_SQRT_PI CONST
M_SQRT_PI = 1.7724538509055159
\(\sqrt\pi\)
M_TWOPI CONST
M_TWOPI = 6.283185307179586
\(2\pi\)
ONE_SIXTH CONST
ONE_SIXTH = 0.16666666666666666
Value of \(\frac{1}{6}\)
ONE_THIRD CONST
ONE_THIRD = 0.3333333333333333
Value of \(\frac{1}{3}\)
PHI CONST
PHI = 1.618033988749895
Golden ratio \(\phi\)
PI CONST
PI = 3.141592653589793
Value of \(\pi\)
QUARTER_PI CONST
QUARTER_PI = 0.7853981633974483
Value of \(\frac{\pi}{4}\)
SILVER CONST
SILVER = 2.414213562373095
Silver ratio \(\delta_S\)
SIXTH CONST
SIXTH = 0.16666666666666666
Value of \(\frac{1}{6}\)
SQRT2 CONST
SQRT2 = 1.4142135623730951
\(\sqrt{2}\)
SQRT2PI CONST
SQRT2PI = 2.5066282746310002
\(\sqrt{2\pi}\)
SQRT2_2 CONST
SQRT2_2 = 0.7071067811865476
\(\frac{\sqrt{2}}{2}\)
SQRT3 CONST
SQRT3 = 1.7320508075688772
\(\sqrt{3}\)
SQRT3_2 CONST
SQRT3_2 = 0.8660254037844386
\(\frac{\sqrt{3}}{2}\)
SQRT3_3 CONST
SQRT3_3 = 0.5773502691896257
\(\frac{\sqrt{3}}{3}\)
SQRT3_4 CONST
SQRT3_4 = 0.4330127018922193
\(\frac{\sqrt{3}}{4}\)
SQRT5 CONST
SQRT5 = 2.23606797749979
\(\sqrt{5}\)
SQRTPI CONST
SQRTPI = 1.7724538509055159
\(\sqrt{\pi}\)
SQRT_2_PI CONST
SQRT_2_PI = 0.7978845608028654
\(\sqrt{\frac{2}{\pi}}\)
SQRT_HALFPI CONST
SQRT_HALFPI = 1.2533141373155001
\(\sqrt{\frac{1}{2}\pi}\)
TAU CONST
TAU = 6.283185307179586
Alias for TWO_PI
THIRD CONST
THIRD = 0.3333333333333333
Value of \(\frac{1}{3}\)
THIRD_PI CONST
THIRD_PI = 1.0471975511965976
Value of \(\frac{\pi}{3}\)
TWO_INV_PI CONST
TWO_INV_PI = 0.6366197723675814
Value of \(\frac{2}{\pi}\)
TWO_PI CONST
TWO_PI = 6.283185307179586
Value of \(2 {\pi}\)
TWO_THIRD CONST
TWO_THIRD = 0.6666666666666666
Value of \(\frac{2}{3}\)
TWO_THIRDS CONST
TWO_THIRDS = 0.6666666666666666
Value of \(\frac{2}{3}\)
abs
(abs x)
Primitive and inlined version of abs.
acos
(acos x)
acos(x)
acosh
(acosh x)
acosh(x)
acot
(acot x)
acot(x)
acoth
(acoth x)
Area hyperbolic cotangent
acovercos
(acovercos x)
Arc covercosine
acoversin
(acoversin x)
Arc coversine
acrd
(acrd x)
Inverse chord
acsc
(acsc x)
acsc(x)
acsch
(acsch v)
Area hyperbolic cosecant
aexcsc
(aexcsc x)
Arc excosecant
aexsec
(aexsec x)
Arc exsecant
ahacovercos
(ahacovercos x)
Arc hacovercosine
ahacoversin
(ahacoversin x)
Arc hacoversine
ahavercos
(ahavercos x)
Arc havecosine
ahaversin
(ahaversin x)
Arc haversine
approx
(approx v)
(approx v digits)
Round v
to specified (default: 2) decimal places. Be aware of floating point number accuracy.
approx-eq
(approx-eq a b)
(approx-eq a b digits)
Checks equality approximately. See approx.
approx=
Alias for approx-eq
asec
(asec x)
asec(x)
asech
(asech x)
Area hyperbolic secant
asin
(asin x)
asin(x)
asinh
(asinh x)
asinh(x)
atan
(atan x)
atan(x)
atan2
(atan2 x y)
atan2(x,y)
atanh
(atanh x)
atanh(x)
avercos
(avercos x)
Arc vecosine
aversin
(aversin x)
Arc versine
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].
bit-and
(bit-and x)
(bit-and x y)
(bit-and x y & r)
x ∧ y - bitwise AND
bit-and-not
(bit-and-not x)
(bit-and-not x y)
(bit-and-not x y & r)
x ∧ ~y - bitwise AND (with complement second argument)
bit-clear
(bit-clear x bit)
Clear bit (set to 0
).
bit-flip
(bit-flip x bit)
Flip bit (set to 0
when 1
or to 1
when 0
).
bit-nand
(bit-nand x)
(bit-nand x y)
(bit-nand x y & r)
~(x ∧ y) - bitwise NAND
bit-nor
(bit-nor x)
(bit-nor x y)
(bit-nor x y & r)
~(x ∨ y) - bitwise NOR
bit-not
(bit-not x)
~x - bitwise NOT
bit-or
(bit-or x)
(bit-or x y)
(bit-or x y & r)
x ∨ y - bitwise OR
bit-set
(bit-set x bit)
Set bit (set to 1
).
bit-shift-left
(bit-shift-left x shift)
Shift bits left
bit-shift-right
(bit-shift-right x shift)
Shift bits right and keep most significant bit unchanged
bit-test
(bit-test x bit)
Test bit (return to true
when 1
or false
when 0
).
bit-xnor
(bit-xnor x)
(bit-xnor x y)
(bit-xnor x y & r)
~(x⊕y) - bitwise XNOR
bit-xor
(bit-xor x)
(bit-xor x y)
(bit-xor x y & r)
x⊕y - bitwise XOR
bits->double
(bits->double v)
Convert 64 bits to double
bool-not
(bool-not x)
Primitive boolean not
bool-xor
(bool-xor x y)
(bool-xor x y & r)
Primitive boolean xor
cb
(cb x)
\(x^3\)
cbrt
(cbrt x)
\(\sqrt[3]{x}\)
ceil
(ceil x)
(ceil x scale)
\(\lceil x \rceil\). See: qceil.
Rounding is done to a multiply of scale value (when provided).
cexpexp
(cexpexp x)
1-exp(-exp(x))
cloglog
(cloglog x)
log(-log(1-x))
cnorm
(cnorm v start1 stop1 start2 stop2)
(cnorm v start stop)
Constrained version of norm. Result of norm is applied to constrain to [0,1]
or [start2,stop2]
ranges.
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()
combinations
(combinations n k)
Binomial coefficient (n choose k)
constrain MACRO
(constrain value mn mx)
Clamp value
to the range [mn,mx]
.
copy-sign
(copy-sign magnitude sign)
Returns a value with a magnitude of first argument and sign of second.
cos
(cos x)
cos(x)
cos-interpolation
(cos-interpolation start stop t)
oF interpolateCosine interpolation. See also lerp/mlerp, quad-interpolation or smooth-interpolation.
cosh
(cosh x)
cosh(x)
cospi
(cospi x)
cos(pi*x)
cot
(cot x)
cot(x)
coth
(coth x)
Hyperbolic cotangent
cotpi
(cotpi x)
cot(pi*x)
covercos
(covercos x)
Covercosine
coversin
(coversin x)
Coversine
crd
(crd x)
Chord
csc
(csc x)
csc(x)
csch
(csch x)
Hyperbolic cosecant
cscpi
(cscpi x)
csc(pi*x)
cut
(cut data breaks)
(cut x1 x2 breaks)
Cut range or sequence into intervals
dec
(dec x)
Primitive and inlined dec
deg-in-rad CONST
deg-in-rad = 0.017453292519943295
\(\frac{\pi}{180}\)
degrees
(degrees rad)
Convert radians into degrees.
delta-eq
(delta-eq a b)
(delta-eq a b accuracy)
(delta-eq a b abs-tol rel-tol)
Checks equality for given absolute accuracy (default 1.0e-6
).
Version with 4-arity accepts absolute and relative accuracy.
delta=
Alias for delta-eq
difference-of-products
(difference-of-products a b c d)
Kahan’s algorithm for (ab)-(cd) to avoid catastrophic cancellation.
dist
(dist [x1 y1] [x2 y2])
(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.
double-array->seq
Convert double array into sequence.
Alias for seq
.
double-array-type
double-bits
(double-bits v)
Returns double as 64-bits (long)
double-double-array->seq
(double-double-array->seq res)
Convert double array of double arrays into sequence of sequences.
double-double-array-type
double-exponent
(double-exponent v)
Extract exponent information from double
double-high-bits
(double-high-bits v)
Returns high word from double as bits
double-low-bits
(double-low-bits v)
Returns low word from double as bits
double-one-minus-epsilon CONST
double-one-minus-epsilon = 0.9999999999999999
Value of 0x1.fffffffffffffp-1d = 0.(9)
double-significand
(double-significand v)
Extract significand from double
eq
(eq _)
(eq a b)
(eq a b & r)
Primitive math equality function.
even?
(even? x)
Primitive and inlined even?
excsc
(excsc x)
Excosecant
exp
(exp x)
exp(x) = e^x
expexp
(expexp x)
exp(-exp(-x))
expm1
(expm1 x)
exp(x)-1 for small x
exsec
(exsec x)
Exsecant
factorial
(factorial n)
Factorial
factorial20
(factorial20 n)
Factorial table up to 20!
falling-factorial
(falling-factorial n x)
Falling (descending) factorial.
falling-factorial-int
(falling-factorial-int n x)
Falling (descending) factorial for integer n.
fast* DEPRECATED
Deprecated: Use *
instead
(fast*)
(fast* a)
(fast* a b)
(fast* a b & r)
Primitive and inlined *
as a function
fast+ DEPRECATED
Deprecated: Use +
instead
(fast+)
(fast+ a)
(fast+ a b)
(fast+ a b & r)
Primitive and inlined +
as a function
fast- DEPRECATED
Deprecated: Use -
instead
(fast-)
(fast- a)
(fast- a b)
(fast- a b & r)
Primitive and inlined -
as a function
fast-div DEPRECATED
Deprecated: Use /
instead
(fast-div)
(fast-div a)
(fast-div a b)
(fast-div a b & r)
Primitive and inlined /
as a function
fast-identity DEPRECATED
Deprecated: Use identity-double
instead
(fast-identity a)
Identity on double.
fast-max DEPRECATED
Deprecated: Use max
instead
(fast-max a)
(fast-max a b)
(fast-max a b & r)
Primitive and inlined max
as a function
fast-min DEPRECATED
Deprecated: Use min
instead
(fast-min a)
(fast-min a b)
(fast-min a b & r)
Primitive and inlined min
as a function
floor
(floor x)
(floor x scale)
\(\lfloor x \rfloor\). See: qfloor.
Rounding is done to a multiply of scale value (when provided).
fma
(fma x y z)
(x y z)
-> (+ z (* x y))
or Math/fma
for java 9+
fpow
(fpow x exponent)
Fast version of pow where exponent is integer.
frac
(frac v)
Fractional part, always returns values from 0.0 to 1.0 (exclusive). See sfrac for signed version.
gcd
(gcd a b)
Fast binary greatest common divisor (Stein’s algorithm)
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.
hacovercos
(hacovercos x)
Hacovercosine
hacoversin
(hacoversin x)
Hacoversine
havercos
(havercos x)
Havercosine
haversin
(haversin x)
(haversin [lat1 lon1] [lat2 lon2])
(haversin lat1 lon1 lat2 lon2)
Haversine formula for value or lattitude and longitude pairs.
haversine
Haversine (haversin alias)
haversine-dist
(haversine-dist [lat1 lon1] [lat2 lon2])
(haversine-dist lat1 lon1 lat2 lon2)
Haversine distance d
for r=1
high-2-exp
(high-2-exp x)
Find lowest exponent (power of 2) which is greater or equal x
. See low-2-exp.
high-exp
(high-exp b x)
Find lowest exponent for base b
which is higher or equalx
. See also low-exp.
hypot
(hypot x y)
(hypot x y z)
Hypot. See also hypot-sqrt.
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}\).
iabs DEPRECATED
Deprecated: Use long-abs.
(iabs x)
\(|x|\) - long
version. See abs.
identity-double
(identity-double a)
Identity on double.
identity-long
(identity-long a)
Identity on double.
inc
(inc x)
Primitive and inlined inc
inf?
(inf? v)
Check if a number is an infinite (positive or negative).
integer?
(integer? v)
Check if given real number is an integer.
inv-factorial
(inv-factorial n)
Inverse of factorial, 1/n!
invalid-double?
(invalid-double? v)
Check if a number is not finite double (NaN or ±Inf).
itrunc
(itrunc v)
Truncate fractional part, keep sign. Returns long
.
jvm-version CONST
jvm-version = 21
lcm
(lcm a b)
Fast binary least common multiplier.
lerp
(lerp start stop t)
Linear interpolation between start
and stop
for amount t
. See also mlerp, cos-interpolation, quad-interpolation or smooth-interpolation.
ln
(ln x)
log(x)=ln(x)
log
(log x)
(log base x)
log(x)=ln(x) or logarithm with given base
.
log-combinations
(log-combinations n k)
Log of binomial coefficient (n choose k)
log-factorial
(log-factorial x)
Log factorial, alias to log-gamma
log10
(log10 x)
log_10(x)
log1mexp
(log1mexp x)
log(1-exp(x)), x<0
log1p
(log1p x)
log(1+x) for small x
log1pexp
(log1pexp x)
log(1+exp(x))
log1pmx
(log1pmx x)
log(1+x)-x
log1psq
(log1psq x)
log(1+x^2))
log2
(log2 x)
Logarithm with base 2.
\(\ln_2{x}\)
log2int
(log2int v)
Fast and integer version of log2, returns long
log2mexp
(log2mexp x)
log(2-exp(x))
logaddexp
(logaddexp x y)
log(exp(x)+exp(y))
logb
(logb b x)
Logarithm with base b
.
\(\ln_b{x}\)
logcosh
(logcosh x)
log(cosh(x))
logexpm1
(logexpm1 x)
log(exp(x)-1))
logistic
Alias for sigmoid
logit
(logit x)
Logit function
loglog
(loglog x)
-log(-log(x))
logmxp1
(logmxp1 x)
log(x)-x+1
logsubexp
(logsubexp x y)
log(abs(exp(x)-exp(y)))
logsumexp
(logsumexp xs)
log(exp(x1)+…+exp(xn))
long-abs
(long-abs x)
\(|x|\) - long
version. See abs.
long-add
(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.
long-dec
(long-dec x)
Primitive and inlined dec
coerced to a long
long-div
(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.
long-inc
(long-inc x)
Primitive and inlined inc
coerced to a long
long-max
(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.
long-min
(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.
long-mod
(long-mod x y)
Primitive and inlined mod
coerced to longs
long-mult
(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.
long-quot
(long-quot x y)
Primitive and inlined quot
coerced to longs
long-rem
(long-rem x y)
Primitive and inlined rem
coerced to longs
long-sub
(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.
low-2-exp
(low-2-exp x)
Find greatest exponent (power of 2) which is lower or equal x
. See high-2-exp.
low-exp
(low-exp b x)
Find greatest exponent for base b
which is lower or equal x
. See also high-exp.
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
.
max
(max a)
(max a b)
(max a b c)
(max a b c d)
(max a b c d & r)
Primitive and inlined max
.
min
(min a)
(min a b)
(min a b c)
(min a b c d)
(min a b c d & r)
Primitive and inlined min
.
mlerp MACRO
(mlerp start stop t)
lerp as macro. For inline code. See also lerp, cos-interpolation, quad-interpolation or smooth-interpolation.
mnorm MACRO
(mnorm v start stop)
(mnorm v start1 stop1 start2 stop2)
Macro version of norm.
mod
(mod x y)
Primitive and inlined mod
muladd
(muladd x y z)
(x y z)
-> (+ z (* x y))
or Math/fma
for java 9+
nan?
(nan? v)
Check if a number is a NaN
near-zero?
(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.
neg-inf?
(neg-inf? v)
Check if a number is negatively infinite.
neg?
(neg? x)
Primitive and inlined neg?
negative-zero?
(negative-zero? x)
Check if zero is negative, ie. -0.0
negmuladd
(negmuladd x y z)
(x y z)
-> (+ z (* -x y))
or Math/fma
for java 9+
next-double
(next-double v)
(next-double v delta)
Next double value. Optional value delta
sets step amount.
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.
not-neg?
(not-neg? x)
Primitive and inlined not-neg?
(x>=0.0)
not-pos?
(not-pos? x)
Primitive and inlined not-pos?
(x<=0.0)
not==
(not== _)
(not== a b)
(not== a b & r)
Not equality. For more than two arguments, pairwise not equality is checked.
(not== 1 2 1)
=== (and (not= 1 2) (not= 2 1))
odd?
(odd? x)
Primitive and inlined odd?
one?
(one? x)
Primitive and inlined one?
(x==1.0)
order
(order vs)
(order vs decreasing?)
Ordering permutation. See R docs
Order uses 0 based indexing.
pos-inf?
(pos-inf? v)
Check if a number is positively infinite.
pos?
(pos? x)
Primitive and inlined pos?
pow
(pow x exponent)
Power of a number
pow2
(pow2 x)
Same as sq. \(x^2\)
pow3
(pow3 x)
\(x^3\)
prev-double
(prev-double v)
(prev-double v delta)
Next double value. Optional value delta
sets step amount.
qceil
(qceil x)
Fast version of ceil. Returns long
.
qcos
(qcos x)
Fast and less accurate cos(x)
qdist
(qdist [x1 y1] [x2 y2])
(qdist x1 y1 x2 y2)
Quick version of Euclidean distance between points. qsqrt is used instead of sqrt.
qexp
(qexp x)
Quick and less accurate version of exp.
qfloor
(qfloor x)
Fast version of floor. Returns long
.
qlog
(qlog x)
Fast and less accurate version of log.
qpow
(qpow x exponent)
Fast and less accurate version of pow.
qround
(qround x)
Fast version of round. Returns long
qsin
(qsin x)
Fast and less accurate sin(x)
qsqrt
(qsqrt x)
Approximated sqrt using binary operations with error 1.0E-2
.
quad-interpolation
(quad-interpolation start stop t)
Quad interpolation. See also lerp/mlerp, cos-interpolation or smooth-interpolation.
quot
(quot x y)
Primitive and inlined quot
rad-in-deg CONST
rad-in-deg = 57.29577951308232
\(\frac{180}{\pi}\)
radians
(radians deg)
Convert degrees into radians.
rank
(rank vs)
(rank vs ties)
(rank vs ties desc?)
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
rank1
rem
(rem x y)
Primitive and inlined rem
remainder
(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]
rint
(rint x)
(rint x scale)
Round to double
. See round, qround.
Rounding is done to a multiply of scale value (when provided).
rising-factorial
(rising-factorial n x)
Rising (Pochhammer) factorial.
rising-factorial-int
(rising-factorial-int n x)
Rising (Pochhammer) factorial for integer n.
round
(round x)
round-even
(round-even x)
Round evenly (like in round in R), IEEE / IEC rounding
round-up-pow2
(round-up-pow2 v)
Round long to the next power of 2
rqsqrt
(rqsqrt x)
Reciprocal of qsqrt. Quick and less accurate.
safe-sqrt
(safe-sqrt value)
Safe sqrt, for value <= 0 result is 0.
$ \{ \[\begin{array}{lr} 0 & : x \leq 0\\\\ \sqrt{x} & : x > 0 \end{array}\]\right. $
sample
(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?)
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)]
.
sec
(sec x)
sec(x)
sech
(sech x)
Hyperbolic secant
secpi
(secpi x)
sec(pi*x)
seq->double-array
(seq->double-array vs)
Convert sequence to double array. Returns input if vs
is double array already.
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
sfrac
(sfrac v)
Fractional part, always returns values from -1.0 to 1.0 (exclusive). See frac for unsigned version.
sgn
(sgn value)
Return -1 when value
is negative, 1 otherwise. See also signum.
sigmoid
(sigmoid x)
Sigmoid function
signum
(signum value)
Return 1 if value
is > 0, 0 if it is 0, -1 otherwise. See also sgn.
sin
(sin x)
sin(x)
sinc
(sinc v)
Sinc function.
sinh
(sinh x)
sinh(x)
sinpi
(sinpi x)
sin(pi*x)
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.
smooth-max
(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 > 0
smoothstep
(smoothstep edge0 edge1 x)
GL smoothstep.
sq
(sq x)
Same as pow2. \(x^2\)
sqrt
(sqrt x)
\(\sqrt{x}\)
sum-of-products
(sum-of-products a b c d)
Kahan’s algorithm for (ab)+(cd) to avoid catastrophic cancellation.
tan
(tan x)
tan(x)
tanh
(tanh x)
tanh(x)
tanpi
(tanpi x)
tan(pi*x)
trunc
(trunc v)
Truncate fractional part, keep sign. Returns double
.
ulp
(ulp x)
Unit in the Last Place, distance between next value larger than x
and x
unsigned-bit-shift-right
(unsigned-bit-shift-right x shift)
Shift bits right and set most significant bit to 0
unuse-primitive-operators
(unuse-primitive-operators)
(unuse-primitive-operators skip-set)
Undoes the work of use-primitive-operators. This is idempotent.
use-primitive-operators
(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.
valid-double?
(valid-double? v)
Check if a number is finite double.
vercos
(vercos x)
Vercosine
versin
(versin x)
Versine
wrap
(wrap [start stop] value)
(wrap start stop value)
Wrap overflowed value into the range, similar to ofWrap.
xexpx
(xexpx x)
x * exp(x)
xexpy
(xexpy x y)
x * exp(x)
xlog1py
(xlog1py x y)
x * log(1+y)
xlogx
(xlogx x)
x * log(x)
xlogy
(xlogy x y)
x * log(y)
xor
(xor x y)
(xor x y & r)
Primitive boolean xor
zero?
(zero? x)
Primitive and inlined zero?
source: clay/core.clj