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 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 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.

Constants

*

macro

(* x)(* x y)(* x y & rest)

A primitive math version of *

+

macro

(+ x)(+ x y)(+ x y & rest)

A primitive math version of +

-

macro

(- x)(- x y)(- x y & rest)

A primitive math version of -

/

macro

(/ x)(/ x y)(/ x y & rest)

A primitive math version of /

<

macro

(< x)(< x y)(< x y & rest)

A primitive math version of <

<<

macro

(<< x y)

fastmath.java.PrimitiveMath/shiftLeft function wrapped in macro.

<=

macro

(<= x)(<= x y)(<= x y & rest)

A primitive math version of <=

==

macro

(== x)(== x y)(== x y & rest)

Equality. See also eq for function version.

>

macro

(> x)(> x y)(> x y & rest)

A primitive math version of >

>=

macro

(>= x)(>= x y)(>= x y & rest)

A primitive math version of >=

>>

macro

(>> x y)

fastmath.java.PrimitiveMath/shiftRight function wrapped in macro.

>>>

macro

(>>> x y)

fastmath.java.PrimitiveMath/unsignedShiftRight function wrapped in macro.

abs

(abs x)

\(|x|\) - double version. See iabs.

Examples

Plot of abs

acos

macro

(acos x)

net.jafama.FastMath/acos function wrapped in macro.

Examples

Plot of acos

acosh

macro

(acosh x)

net.jafama.FastMath/acosh function wrapped in macro.

Examples

Plot of acosh

acot

(acot v)

Arccotangent

Examples

Plot of acot

acoth

(acoth v)

Area hyperbolic cotangent

Examples

Plot of acoth

acsc

(acsc v)

Arccosecant

Examples

Plot of acsc

acsch

(acsch v)

Area hyperbolic cosecant

Examples

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

asec

(asec v)

Arcsecant

Examples

Plot of asec

asech

(asech v)

Area hyperbolic secant

Examples

Plot of asech

asin

macro

(asin x)

net.jafama.FastMath/asin function wrapped in macro.

Examples

Plot of asin

asinh

macro

(asinh x)

net.jafama.FastMath/asinh function wrapped in macro.

Examples

Plot of asinh

atan

macro

(atan x)

net.jafama.FastMath/atan function wrapped in macro.

Examples

Plot of atan

atan2

macro

(atan2 x y)

net.jafama.FastMath/atan2 function wrapped in macro.

atanh

macro

(atanh x)

net.jafama.FastMath/atanh function wrapped in macro.

Examples

Plot of atanh

bessel-j

macro

(bessel-j x y)

Bessel J function value for given order and argument.

bit-and

macro

(bit-and x)(bit-and x y)(bit-and x y & rest)

A primitive math version of bit-and

bit-not

macro

(bit-not x)

fastmath.java.PrimitiveMath/bitNot function wrapped in macro.

bit-or

macro

(bit-or x)(bit-or x y)(bit-or x y & rest)

A primitive math version of bit-or

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-not

macro

(bool-not x)

fastmath.java.PrimitiveMath/not function wrapped in macro.

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

Plot of cbrt

ceil

(ceil x)

\(\lceil x \rceil\). See: qceil.

Examples

Plot of ceil

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.

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

macro

(cos x)

net.jafama.FastMath/cos function wrapped in macro.

Examples

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

Plot of cosh

cot

(cot v)

Cotangent

Examples

Plot of cot

coth

(coth v)

Hyperbolic cotangent

Examples

Plot of coth

csc

(csc v)

Cosecant

Examples

Plot of csc

csch

(csch v)

Hyperbolic cosecant

Examples

Plot of csch

dec

macro

(dec x)

fastmath.java.PrimitiveMath/dec function wrapped in macro.

deg-in-rad

const

;;=> 0.017453292519943295

\(\frac{\pi}{180}\)

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

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-array-type

const

;;=> class [D

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))

double-double-array-type

const

;;=> class [[D

E

const

;;=> 2.718281828459045

Value of \(e\)

EPSILON

const

;;=> 1.0E-10

Very small number \(\varepsilon\)

eq

(eq a)(eq a b)(eq a b c)(eq a b c d)

Primitive math equality function for doubles. See ==.

erf

macro

(erf x)(erf x y)

Error function. For two arguments return difference between (erf x) and (erf y).

Examples

Plot of erf

erfc

macro

(erfc x)

Complementary error function.

Examples

Plot of erfc

even?

macro

(even? x)

fastmath.java.PrimitiveMath/isEven function wrapped in macro.

exp

macro

(exp x)

net.jafama.FastMath/exp function wrapped in macro.

Examples

Plot of exp

floor

(floor x)

\(\lfloor x \rfloor\). See: qfloor.

Examples

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

Plot of frac

GAMMA

const

;;=> 4.7421875

Lanchos approximation g constant

gamma

macro

(gamma x)

Gamma function \(\Gamma(x)\)

Examples

Plot of gamma

gcd

(gcd a b)

Examples

Usage

(gcd 340 440)
;;=> 20
(gcd (* 123 5544331) (* 123 123))
;;=> 123
(gcd -234 -432)
;;=> 18

HALF_PI

const

;;=> 1.5707963267948966

Value of \(\frac{\pi}{2}\)

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 than 23.11. Lower exponent 1 gives lower value 9.

(high-exp 9 23.11)
;;=> 2

For x less than 1.0 gives negative exponent.

(high-exp 10 0.011)
;;=> -1

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}\). Should be faster than hypot.

iabs

(iabs x)

\(|x|\) - long version. See abs.

Examples

Plot of iabs

inc

macro

(inc x)

fastmath.java.PrimitiveMath/inc function wrapped in macro.

inv-erf

macro

(inv-erf x)

Inverse erf.

Examples

Plot of inv-erf

inv-erfc

macro

(inv-erfc x)

Inverse erfc.

Examples

Plot of inv-erfc

inv-gamma-1pm1

macro

(inv-gamma-1pm1 x)

\(\frac{1}{\Gamma(1+x)}\).

Examples

Plot of inv-gamma-1pm1

INV_LN2

const

;;=> 1.4426950408889634

\(\frac{1}{\ln{2}}\)

INV_LOG_HALF

const

;;=> -1.4426950408889634

\(\frac{1}{\ln{0.5}}\)

itrunc

(itrunc v)

Truncate fractional part, keep sign. Returns long.

Examples

Examples

(itrunc 1.234)
;;=> 1
(itrunc -1.544)
;;=> -1

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

Plot of ln

LN10

const

;;=> 2.302585092994046

\(\ln{10}\)

LN2

const

;;=> 0.6931471805599453

\(\ln{2}\)

LN2_2

const

;;=> 0.34657359027997264

\(\frac{\ln{2}}{2}\)

log

macro

(log x)

net.jafama.FastMath/log function wrapped in macro.

Examples

Plot of log

log-beta

macro

(log-beta x y)

Logarithm of Beta function.

log-gamma

macro

(log-gamma x)

Gamma function \(\ln\Gamma(x)\)

Examples

Plot of log-gamma

log-gamma-1p

macro

(log-gamma-1p x)

Gamma function \(\ln\Gamma(1+x)\)

Examples

Plot of log-gamma-1p

log10

macro

(log10 x)

\(\ln_{10}{x}\)

Examples

Plot of log10

LOG10E

const

;;=> 0.4342944819032518

\(\log_{10}{e}\)

log1p

macro

(log1p x)

net.jafama.FastMath/log1p function wrapped in macro.

Examples

Plot of log1p

log2

(log2 x)

Logarithm with base 2.

\(\ln_2{x}\)

Examples

Plot of log2

LOG2E

const

;;=> 1.4426950408889634

\(\log_{2}{e}\)

logb

(logb b x)

Logarithm with base b.

\(\ln_b{x}\)

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 than 23.11. Next exponent 2 gives greater value 82.

(low-exp 9 23.11)
;;=> 1

For x less than 1.0 gives negative exponent.

(low-exp 10 0.011)
;;=> -2

M_1_PI

const

;;=> 0.3183098861837907

\(\frac{1}{\pi}\)

M_2_PI

const

;;=> 0.6366197723675814

\(\frac{2}{\pi}\)

M_2_SQRTPI

const

;;=> 1.1283791670955126

\(\frac{2}{\sqrt\pi}\)

M_3PI_4

const

;;=> 2.356194490192345

\(\frac{3\pi}{4}\)

M_E

const

;;=> 2.718281828459045

\(e\)

M_INVLN2

const

;;=> 1.4426950408889634

\(\frac{1}{\ln{2}}\)

M_IVLN10

const

;;=> 0.43429448190325176

\(\frac{1}{\ln{10}}\)

M_LN10

const

;;=> 2.302585092994046

\(\ln{10}\)

M_LN2

const

;;=> 0.6931471805599453

\(\ln{2}\)

M_LOG10E

const

;;=> 0.4342944819032518

\(\log_{10}{e}\)

M_LOG2_E

const

;;=> 0.6931471805599453

\(\ln{2}\)

M_LOG2E

const

;;=> 1.4426950408889634

\(\log_{2}{e}\)

M_PI

const

;;=> 3.141592653589793

\(\pi\)

M_PI_2

const

;;=> 1.5707963267948966

\(\frac{\pi}{2}\)

M_PI_4

const

;;=> 0.7853981633974483

\(\frac{\pi}{4}\)

M_SQRT1_2

const

;;=> 0.7071067811865475

\(\frac{1}{\sqrt{2}}\)

M_SQRT2

const

;;=> 1.4142135623730951

\(\sqrt{2}\)

M_SQRT3

const

;;=> 1.7320508075688772

\(\sqrt{3}\)

M_SQRT_PI

const

;;=> 1.7724538509055159

\(\sqrt\pi\)

M_TWOPI

const

;;=> 6.283185307179586

\(2\pi\)

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]

max

macro

(max x)(max x y)(max x y & rest)

A primitive math version of max

min

macro

(min x)(min x y)(min x y & rest)

A primitive math version of min

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

neg?

macro

(neg? x)

fastmath.java.PrimitiveMath/isNeg function wrapped in macro.

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

not==

macro

(not== x)(not== x y)(not== x y & rest)

A primitive math version of not==

odd?

macro

(odd? x)

fastmath.java.PrimitiveMath/isOdd function wrapped in macro.

PHI

const

;;=> 1.618033988749895

Golden ratio \(\varphi\)

PI

const

;;=> 3.141592653589793

Value of \(\pi\)

pos?

macro

(pos? x)

fastmath.java.PrimitiveMath/isPos function wrapped in macro.

pow

macro

(pow x y)

net.jafama.FastMath/pow function wrapped in macro.

pow2

(pow2 x)

Same as sq. \(x^2\)

Examples

Plot of pow2

pow3

(pow3 x)

\(x^3\)

Examples

Plot of pow3

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

qceil

macro

(qceil x)

Fast version of ceil. Returns long. See: ceil.

qcos

macro

(qcos x)

Fast and less accurate cos.

Examples

Compare cos and qcos.

(cos 1.123)
;;=> 0.43298018843109504
(qcos 1.123)
;;=> 0.433093818853152

Plot of qcos

qdist

(qdist x1 y1 x2 y2)

Quick version of Euclidean distance between points. qsqrt is used instead of sqrt.

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

Compare exp and qexp.

(exp 1.123)
;;=> 3.07406257154899
(qexp 1.123)
;;=> 3.1542205810546875

Plot of qexp

qfloor

macro

(qfloor x)

Fast version of floor. Returns long. See: floor.

qlog

macro

(qlog x)

Fast and less accurate version of log.

Examples

Compare log and qlog.

(log 23.123)
;;=> 3.1408277931718307
(qlog 23.123)
;;=> 3.1407453502111626

Plot of qlog

qpow

macro

(qpow x y)

Fast and less accurate version of pow.

Examples

Compare pow and qpow.

(pow 1.23 43.3)
;;=> 7814.322402338249
(qpow 1.23 43.3)
;;=> 7808.951363383271

qround

macro

(qround x)

Fast version of round. Returns long. See: rint, round.

qsin

macro

(qsin x)

Fast and less accurate sin.

Examples

Compare sin and qsin.

(sin 1.123)
;;=> 0.901403437105813
(qsin 1.123)
;;=> 0.901348847046022

Plot of qsin

qsqrt

macro

(qsqrt x)

Approximated sqrt using binary operations with error 1.0E-2.

Examples

Compare sqrt and qsqrt.

(sqrt 23.123)
;;=> 4.808638060823459
(qsqrt 23.123)
;;=> 4.746781462931431

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

QUARTER_PI

const

;;=> 0.7853981633974483

Value of \(\frac{\pi}{4}\)

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

rad-in-deg

const

;;=> 57.29577951308232

\(\frac{180}{\pi}\)

radians

(radians deg)

Convert degrees into radians.

Examples

Let’s convert 180 degrees to radians.

(radians 180)
;;=> 3.141592653589793

regularized-beta

macro

(regularized-beta x y z)

Regularized Beta.

regularized-gamma-p

macro

(regularized-gamma-p x y)

Regularized gamma P

regularized-gamma-q

macro

(regularized-gamma-q x y)

Regularized gamma Q

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

Compare with rem and mod.

(remainder 3.123 0.2)
;;=> -0.07699999999999996
(rem 3.123 0.2)
;;=> 0.12300000000000005
(mod 3.123 0.2)
;;=> 0.12300000000000005

rint

(rint x)

Round to double. See round, qround.

Examples

Round to double.

(rint PI)
;;=> 3.0

Plot of rint

round

(round x)

Round to long. See: rint, qround.

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

rqsqrt

macro

(rqsqrt x)

Inversed version of qsqrt. Quick and less accurate.

Examples

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

Plot of safe-sqrt

sample

(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.

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)

sec

(sec v)

Secant

Examples

Plot of sec

sech

(sech v)

Hyperbolic secant

Examples

Plot of sech

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@6be9e443
(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@5f990556
(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

sigmoid

(sigmoid x)

Sigmoid function

Examples

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

Plot of signum

sin

macro

(sin x)

net.jafama.FastMath/sin function wrapped in macro.

Examples

Plot of sin

sinc

(sinc v)

Sinc function.

Examples

Plot of sinc

sinh

macro

(sinh x)

net.jafama.FastMath/sinh function wrapped in macro.

Examples

Plot of sinh

SIXTH

const

;;=> 0.16666666666666666

Value of \(\frac{1}{6}\)

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)

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

sq

(sq x)

Same as pow2. \(x^2\)

Examples

Plot of sq

sqrt

macro

(sqrt x)

\(\sqrt{x}\)

Examples

Plot of sqrt

SQRT2

const

;;=> 1.4142135623730951

\(\sqrt{2}\)

SQRT2_2

const

;;=> 0.7071067811865476

\(\frac{\sqrt{2}}{2}\)

SQRT2PI

const

;;=> 2.5066282746310002

\(\sqrt{2\pi}\)

SQRT3

const

;;=> 1.7320508075688772

\(\sqrt{3}\)

SQRT5

const

;;=> 2.23606797749979

\(\sqrt{5}\)

SQRTPI

const

;;=> 1.7724538509055159

\(\sqrt{\pi}\)

tan

macro

(tan x)

net.jafama.FastMath/tan function wrapped in macro.

Examples

Plot of tan

tanh

macro

(tanh x)

net.jafama.FastMath/tanh function wrapped in macro.

Examples

Plot of tanh

TAU

const

;;=> 6.283185307179586

Alias for TWO_PI

THIRD

const

;;=> 0.3333333333333333

Value of \(\frac{1}{3}\)

THIRD_PI

const

;;=> 1.0471975511965976

Value of \(\frac{\pi}{3}\)

trigamma

macro

(trigamma x)

Derivative of digamma.

Examples

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

Plot of trunc

TWO_PI

const

;;=> 6.283185307179586

Value of \(2 {\pi}\)

TWO_THIRD

const

;;=> 0.6666666666666666

Value of \(\frac{2}{3}\)

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

zero?

macro

(zero? x)

fastmath.java.PrimitiveMath/isZero function wrapped in macro.