Collection of fast math functions and plethora of constants known from other math libraries.
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
- boolean or
bool-xor
- boolean xor
bool-not
- boolean not
<<
- bit shift left>>
- signed bit shift right>>>
- unsigned bit shift rightnot==
- not equalTo turn on primitive math on your namespace call use-primitive-operators
.
To turn off and revert original versions call unuse-primitive-operators
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
).
Additionally namespace contains functions which are common in frameworks like OpenFrameworks and Processing.
fastmath.random
namespace.fastmath.vector
contains vector (2,3,4 dim. + double array + clojure vector) protocol and implementations.fastmath.complex
contains complex number operations.Collection of fast math functions and plethora of constants known from other math libraries. #### Primitive math operators Based on [Primitive Math by Zach Tellman](https://github.com/ztellman/primitive-math) 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 booleans * `bool-or` - boolean `or` * `bool-xor` - boolean `xor` * `bool-not` - boolean `not` * `<<` - bit shift left * `>>` - signed bit shift right * `>>>` - unsigned bit shift right * `not==` - 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](https://github.com/jeffhain/jafama) 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.
(* x)
(* x y)
(* x y & rest)
A primitive math version of *
A primitive math version of `*`
(+ x)
(+ x y)
(+ x y & rest)
A primitive math version of +
A primitive math version of `+`
(- x)
(- x y)
(- x y & rest)
A primitive math version of -
A primitive math version of `-`
(/ x)
(/ x y)
(/ x y & rest)
A primitive math version of /
A primitive math version of `/`
(< x)
(< x y)
(< x y & rest)
A primitive math version of <
A primitive math version of `<`
(<< x y)
fastmath.java.PrimitiveMath/shiftLeft function wrapped in macro.
fastmath.java.PrimitiveMath/shiftLeft function wrapped in macro.
(<= x)
(<= x y)
(<= x y & rest)
A primitive math version of <=
A primitive math version of `<=`
(== x)
(== x y)
(== x y & rest)
Equality. See also eq
for function version.
Equality. See also [[eq]] for function version.
(> x)
(> x y)
(> x y & rest)
A primitive math version of >
A primitive math version of `>`
(>= x)
(>= x y)
(>= x y & rest)
A primitive math version of >=
A primitive math version of `>=`
(>> x y)
fastmath.java.PrimitiveMath/shiftRight function wrapped in macro.
fastmath.java.PrimitiveMath/shiftRight function wrapped in macro.
(>>> x y)
fastmath.java.PrimitiveMath/unsignedShiftRight function wrapped in macro.
fastmath.java.PrimitiveMath/unsignedShiftRight function wrapped in macro.
(abs x)
\(|x|\) - double
version. See iabs
.
\\(|x|\\) - `double` version. See [[iabs]].
(acos x)
net.jafama.FastMath/acos function wrapped in macro.
net.jafama.FastMath/acos function wrapped in macro.
(acosh x)
net.jafama.FastMath/acosh function wrapped in macro.
net.jafama.FastMath/acosh function wrapped in macro.
(approx v)
(approx v digits)
Round v
to specified (default: 2) decimal places. Be aware of double
number accuracy.
Round `v` to specified (default: 2) decimal places. Be aware of `double` number accuracy.
(approx-eq a b)
(approx-eq a b digits)
Checks equality approximately. See approx
.
Checks equality approximately. See [[approx]].
(asin x)
net.jafama.FastMath/asin function wrapped in macro.
net.jafama.FastMath/asin function wrapped in macro.
(asinh x)
net.jafama.FastMath/asinh function wrapped in macro.
net.jafama.FastMath/asinh function wrapped in macro.
(atan x)
net.jafama.FastMath/atan function wrapped in macro.
net.jafama.FastMath/atan function wrapped in macro.
(atan2 x y)
net.jafama.FastMath/atan2 function wrapped in macro.
net.jafama.FastMath/atan2 function wrapped in macro.
(atanh x)
net.jafama.FastMath/atanh function wrapped in macro.
net.jafama.FastMath/atanh function wrapped in macro.
(bessel-j x y)
Bessel J function value for given order and argument.
Bessel J function value for given order and argument.
(bit-and x)
(bit-and x y)
(bit-and x y & rest)
A primitive math version of bit-and
A primitive math version of `bit-and`
(bit-not x)
fastmath.java.PrimitiveMath/bitNot function wrapped in macro.
fastmath.java.PrimitiveMath/bitNot function wrapped in macro.
(bit-or x)
(bit-or x y)
(bit-or x y & rest)
A primitive math version of bit-or
A primitive math version of `bit-or`
(bit-shift-left x y)
fastmath.java.PrimitiveMath/shiftLeft function wrapped in macro.
fastmath.java.PrimitiveMath/shiftLeft function wrapped in macro.
(bit-shift-right x y)
fastmath.java.PrimitiveMath/shiftRight function wrapped in macro.
fastmath.java.PrimitiveMath/shiftRight function wrapped in macro.
(bit-xor x)
(bit-xor x y)
(bit-xor x y & rest)
A primitive math version of bit-xor
A primitive math version of `bit-xor`
(bool-and x)
(bool-and x y)
(bool-and x y & rest)
A primitive math version of bool-and
A primitive math version of `bool-and`
(bool-not x)
fastmath.java.PrimitiveMath/not function wrapped in macro.
fastmath.java.PrimitiveMath/not function wrapped in macro.
(bool-or x)
(bool-or x y)
(bool-or x y & rest)
A primitive math version of bool-or
A primitive math version of `bool-or`
(bool-xor x)
(bool-xor x y)
(bool-xor x y & rest)
A primitive math version of bool-xor
A primitive math version of `bool-xor`
(ceil x)
\(\lceil x \rceil\). See: qceil
.
\\(\lceil x \rceil\\). See: [[qceil]].
(constrain value mn mx)
Clamp value
to the range [mn,mx]
.
Clamp `value` to the range `[mn,mx]`.
(cos x)
net.jafama.FastMath/cos function wrapped in macro.
net.jafama.FastMath/cos function wrapped in macro.
(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]].
(cosh x)
net.jafama.FastMath/cosh function wrapped in macro.
net.jafama.FastMath/cosh function wrapped in macro.
(dec x)
fastmath.java.PrimitiveMath/dec function wrapped in macro.
fastmath.java.PrimitiveMath/dec function wrapped in macro.
(degrees rad)
Convert radians into degrees.
Convert radians into degrees.
(digamma x)
Logarithmic derivative of \(\Gamma\).
Logarithmic derivative of \\(\Gamma\\).
(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.
Euclidean distance between points `(x1,y1)` and `(x2,y2)`. See [[fastmath.vector]] namespace to see other metrics which work on vectors.
Convert double array into sequence.
Alias for seq
.
Convert double array into sequence. Alias for `seq`.
(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.
(eq a)
(eq a b)
(eq a b c)
(eq a b c d)
Primitive math equality function for doubles. See ==
.
Primitive math equality function for doubles. See [[==]].
(erf x)
(erf x y)
Error function. For two arguments return difference between (erf x)
and (erf y)
.
Error function. For two arguments return difference between `(erf x)` and `(erf y)`.
(erfc x)
Complementary error function.
Complementary error function.
(even? x)
fastmath.java.PrimitiveMath/isEven function wrapped in macro.
fastmath.java.PrimitiveMath/isEven function wrapped in macro.
(exp x)
net.jafama.FastMath/exp function wrapped in macro.
net.jafama.FastMath/exp function wrapped in macro.
(floor x)
\(\lfloor x \rfloor\). See: qfloor
.
\\(\lfloor x \rfloor\\). See: [[qfloor]].
(fpow x y)
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.
(gamma x)
Gamma function \(\Gamma(x)\)
Gamma function \\(\Gamma(x)\\)
(high-2-exp v)
Find lowest exponent (power of 2) which is greater or equal x
. See low-2-exp
.
Find lowest exponent (power of 2) which is greater or equal `x`. See [[low-2-exp]].
(high-exp b x)
Find lowest exponent for base b
which is higher or equalx
. See also low-exp
.
Find lowest exponent for base `b` which is higher or equal`x`. See also [[low-exp]].
(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
.
Hypot, sqrt version: \\(\sqrt{x^2+y^2}\\) or \\(\sqrt{x^2+y^2+z^2}\\). Should be faster than [[hypot]].
(iabs x)
\(|x|\) - long
version. See abs
.
\\(|x|\\) - `long` version. See [[abs]].
(inc x)
fastmath.java.PrimitiveMath/inc function wrapped in macro.
fastmath.java.PrimitiveMath/inc function wrapped in macro.
(inv-gamma-1pm1 x)
\(\frac{1}{\Gamma(1+x)}\).
\\(\frac{1}{\Gamma(1+x)}\\).
(itrunc v)
Truncate fractional part, keep sign. Returns long
.
Truncate fractional part, keep sign. Returns `long`.
(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]].
(ln x)
net.jafama.FastMath/log function wrapped in macro.
net.jafama.FastMath/log function wrapped in macro.
(log x)
net.jafama.FastMath/log function wrapped in macro.
net.jafama.FastMath/log function wrapped in macro.
(log-beta x y)
Logarithm of Beta function.
Logarithm of Beta function.
(log-gamma x)
Gamma function \(\ln\Gamma(x)\)
Gamma function \\(\ln\Gamma(x)\\)
(log-gamma-1p x)
Gamma function \(\ln\Gamma(1+x)\)
Gamma function \\(\ln\Gamma(1+x)\\)
(log1p x)
net.jafama.FastMath/log1p function wrapped in macro.
net.jafama.FastMath/log1p function wrapped in macro.
(log2 x)
Logarithm with base 2.
\(\ln_2{x}\)
Logarithm with base 2. \\(\ln_2{x}\\)
(logb b x)
Logarithm with base b
.
\(\ln_b{x}\)
Logarithm with base `b`. \\(\ln_b{x}\\)
(low-2-exp x)
Find greatest exponent (power of 2) which is lower or equal x
. See high-2-exp
.
Find greatest exponent (power of 2) which is lower or equal `x`. See [[high-2-exp]].
(low-exp b x)
Find greatest exponent for base b
which is lower or equal x
. See also high-exp
.
Find greatest exponent for base `b` which is lower or equal `x`. See also [[high-exp]].
Smallest machine number. Value is calculated during evaluation and may differ on different processors.
Smallest machine number. Value is calculated during evaluation and may differ on different processors.
(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 x)
(max x y)
(max x y & rest)
A primitive math version of max
A primitive math version of `max`
(min x)
(min x y)
(min x y & rest)
A primitive math version of min
A primitive math version of `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]].
(mod x y)
fastmath.java.PrimitiveMath/modulus function wrapped in macro.
fastmath.java.PrimitiveMath/modulus function wrapped in macro.
(neg? x)
fastmath.java.PrimitiveMath/isNeg function wrapped in macro.
fastmath.java.PrimitiveMath/isNeg function wrapped in macro.
(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== x)
(not== x y)
(not== x y & rest)
A primitive math version of not==
A primitive math version of `not==`
(odd? x)
fastmath.java.PrimitiveMath/isOdd function wrapped in macro.
fastmath.java.PrimitiveMath/isOdd function wrapped in macro.
(pos? x)
fastmath.java.PrimitiveMath/isPos function wrapped in macro.
fastmath.java.PrimitiveMath/isPos function wrapped in macro.
(pow x y)
net.jafama.FastMath/pow function wrapped in macro.
net.jafama.FastMath/pow function wrapped in macro.
(prev-double v)
(prev-double v delta)
Previous double value. Optional value delta
sets step amount.
Previous double value. Optional value `delta` sets step amount.
(qcos x)
Fast and less accurate cos
.
Fast and less accurate [[cos]].
(qexp x)
Quick and less accurate version of exp
.
Quick and less accurate version of [[exp]].
(qlog x)
Fast and less accurate version of log
.
Fast and less accurate version of [[log]].
(qpow x y)
Fast and less accurate version of pow
.
Fast and less accurate version of [[pow]].
(qsin x)
Fast and less accurate sin
.
Fast and less accurate [[sin]].
(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]].
(quot x y)
fastmath.java.PrimitiveMath/quotient function wrapped in macro.
fastmath.java.PrimitiveMath/quotient function wrapped in macro.
(radians deg)
Convert degrees into radians.
Convert degrees into radians.
(regularized-beta x y z)
Regularized Beta
.
Regularized `Beta`.
(regularized-gamma-p x y)
Regularized gamma
P
Regularized `gamma` P
(regularized-gamma-q x y)
Regularized gamma
Q
Regularized `gamma` Q
(rem x y)
fastmath.java.PrimitiveMath/remainder function wrapped in macro.
fastmath.java.PrimitiveMath/remainder function wrapped in 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]
From `FastMath` doc: returns dividend - divisor * n, where n is the mathematical integer closest to dividend/divisor. Returned value in `[-|divisor|/2,|divisor|/2]`
(round-up-pow2 v)
Round long to the next power of 2
Round long to the next power of 2
(rqsqrt x)
Inversed version of qsqrt
. Quick and less accurate.
Inversed version of [[qsqrt]]. Quick and less accurate.
(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. \)
Safe sqrt, for value <= 0 result is 0. \\( \left\\{ \begin{array}{lr} 0 & : x \leq 0\\\\ \sqrt{x} & : x > 0 \end{array} \\right. \\)
(sample f number-of-values)
(sample f range-min range-max number-of-values)
Sample function f
and return sequence of values.
range-min
defaults to 0.0, range-max
to 1.0.
Range is inclusive.
Sample function `f` and return sequence of values. `range-min` defaults to 0.0, `range-max` to 1.0. Range is inclusive.
(seq->double-array vs)
Convert sequence to double-array.
If sequence is double-array do not convert.
Convert sequence to double-array. If sequence is double-array do not convert.
(seq->double-double-array vss)
Convert sequence to double-array of double-arrays.
If sequence is double-array of double-arrays do not convert
Convert sequence to double-array of double-arrays. If sequence is double-array of double-arrays do not convert
(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)
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. \)
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. \\)
(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. \)
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. \\)
(sin x)
net.jafama.FastMath/sin function wrapped in macro.
net.jafama.FastMath/sin function wrapped in macro.
(sinh x)
net.jafama.FastMath/sinh function wrapped in macro.
net.jafama.FastMath/sinh function wrapped in macro.
(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]].
(smoothstep edge0 edge1 x)
GL smoothstep.
GL [smoothstep](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/smoothstep.xhtml).
(tan x)
net.jafama.FastMath/tan function wrapped in macro.
net.jafama.FastMath/tan function wrapped in macro.
(tanh x)
net.jafama.FastMath/tanh function wrapped in macro.
net.jafama.FastMath/tanh function wrapped in macro.
(trunc v)
Truncate fractional part, keep sign. Returns double
.
Truncate fractional part, keep sign. Returns `double`.
(unsigned-bit-shift-right x y)
fastmath.java.PrimitiveMath/unsignedShiftRight function wrapped in macro.
fastmath.java.PrimitiveMath/unsignedShiftRight function wrapped in macro.
(unuse-primitive-operators)
Undoes the work of use-primitive-operators
. This is idempotent.
Undoes the work of [[use-primitive-operators]]. This is idempotent.
(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
.
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 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).
(zero? x)
fastmath.java.PrimitiveMath/isZero function wrapped in macro.
fastmath.java.PrimitiveMath/isZero function wrapped in macro.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close