fastmath.vector

Mathematical vector operations.

Types

  • Fixed size (custom types):
    • Number - 1d vector
    • Vec2 - 2d vector, creator vec2
    • Vec3 - 3d vector, creator vec3
    • Vec4 - 4d vector, creator vec4
    • ArrayVec - fixed size double array wrapper, n-dimensional, creator array-vec
  • Fixed size
    • doubles - double array itself
  • Variable size:
    • Clojure’s IPersistentVector, creator []
    • Clojure’s ISeq

VectorProto defines most of the functions.

Vectors implements also:

  • Sequable
  • Sequencial
  • IFn
  • Counted
  • Reversible
  • Indexed
  • ILookup
  • equals and toString from Object
  • IPersistentVector
  • Associative
  • clojure.core.matrix.protocols
  • IReduce and IReduceInit

That means that vectors can be destructured, treated as sequence or called as a function. See vec2 for examples.

abs

(abs v)

Absolute value of vector elements

Examples

Usage

(abs (array-vec [-1 2 -2 1]))
;;=> [1.0 2.0 2.0 1.0]

acos

(acos vector)

Apply acos to vector elements.

Examples

Usage

(acos (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.0471975511965976 ##NaN ##NaN 1.5707963267948966]

acosh

(acosh vector)

Apply acosh to vector elements.

Examples

Usage

(acosh (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [##NaN ##NaN 1.37285914424258 ##NaN]

acot

(acot vector)

Apply acot to vector elements.

Examples

Usage

(acot (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.1071487177940904 2.5535900500422257 0.44441920990109884
;;=>  1.5707963267948966]

acoth

(acoth vector)

Apply acoth to vector elements.

Examples

Usage

(acoth (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [##NaN -0.8047189562170501 0.5180459658433878 ##NaN]

acsc

(acsc vector)

Apply acsc to vector elements.

Examples

Usage

(acsc (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [##NaN -0.7297276562269663 0.49631736212546657 ##NaN]

acsch

(acsch vector)

Apply acsch to vector elements.

add

(add v)(add v1 v2)

Sum of two vectors.

Examples

Usage

(add (vec2 1 2) (vec2 -1 -2))
;;=> [0.0 0.0]

aligned?

(aligned? v1 v2 tol)(aligned? v1 v2)

Are vectors aligned (have the same direction)?

Examples

Usage

(aligned? (vec2 1.0 1.0) (vec2 2.0 2.000001))
;;=> true
(aligned? (vec2 1.0 1.0) (vec2 2.0 2.00001))
;;=> false
(aligned? (vec2 1.0 1.0) (vec2 2.0 2.00001) 0.001)
;;=> true

;; Test: ok.

angle-between

(angle-between v1 v2)

Angle between two vectors

See also relative-angle-between.

Examples

Usage

(m/degrees (angle-between (vec3 1.0 0.0 0.0) (vec3 0.0 1.0 0.0)))
;;=> 90.0
(m/degrees (angle-between (vec (repeatedly 50 rand))
                          (vec (repeatedly 50 rand))))
;;=> 35.464943700722095

applyf

deprecated in v1.3.0

Same as fmap. Deprecated.

Examples

Usage

(applyf [5 3] (fn [v] (m/sin v)))
;;=> [-0.9589242746631386 0.1411200080598671]

approx

(approx v)(approx v d)

Round to 2 (or d) decimal places

Examples

Usage

(approx (vec2 (m/sin -1) (m/cos 1)))
;;=> [-0.84 0.54]
(approx (vec2 (m/sin -1) (m/cos 1)) 6)
;;=> [-0.841471 0.540302]

array->vec2

(array->vec2 arr)

Doubles array to Vec2

Examples

Usage

(array->vec2 (double-array [11 22 33 44 55]))
;;=> [11.0 22.0]

array->vec3

(array->vec3 arr)

Doubles array to Vec3

Examples

Usage

(array->vec3 (double-array [11 22 33 44 55]))
;;=> [11.0 22.0 33.0]

array->vec4

(array->vec4 arr)

Doubles array to Vec4

Examples

Usage

(array->vec4 (double-array [11 22 33 44 55]))
;;=> [11.0 22.0 33.0 44.0]

array-vec

(array-vec xs)

Make ArrayVec type based on provided sequence xs.

Examples

Usage

(array-vec [1 2 3 4 5 6 7])
;;=> [1.0 2.0 3.0 4.0 5.0 6.0 7.0]
(array-vec (range 0.0 1.0 0.25))
;;=> [0.0 0.25 0.5 0.75]
(array-vec 11)
;;=> [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]

Operations

(nth (array-vec [9 8 7 6]) 2)
;;=> 7.0
(count (array-vec (range 0.1 1.0 0.05)))
;;=> 18
(seq (array-vec [1 2]))
;;=> (1.0 2.0)

as-vec

(as-vec v)(as-vec v xs)

Create vector from sequence as given type. If there is no sequence fill with 0.0.

Examples

Create vectors

(as-vec [1 2 3])
;;=> [0.0 0.0 0.0]
(as-vec [3 2 3] (repeat -10))
;;=> [-10 -10 -10]
(as-vec (vec3 1 2 3) (repeat -10))
;;=> [-10.0 -10.0 -10.0]
(type (as-vec (array-vec [1 1 11 2 3]) (repeat -10)))
;;=> class fastmath.vector.ArrayVec

asec

(asec vector)

Apply asec to vector elements.

Examples

Usage

(asec (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [##NaN 2.300523983021863 1.07447896466943 ##NaN]

asech

(asech vector)

Apply asech to vector elements.

Examples

Usage

(asech (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.3169578969248166 ##NaN ##NaN ##Inf]

asin

(asin vector)

Apply asin to vector elements.

Examples

Usage

(asin (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.5235987755982989 ##NaN ##NaN 0.0]

asinh

(asinh vector)

Apply asinh to vector elements.

Examples

Usage

(asinh (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.48121182505960347 -1.1947632172871094 1.4874828366412711 0.0]

atan

(atan vector)

Apply atan to vector elements.

Examples

Usage

(atan (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.4636476090008061 -0.9827937232473292 1.1263771168937977 0.0]

atanh

(atanh vector)

Apply atanh to vector elements.

Examples

Usage

(atanh (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.5493061443340548 ##NaN ##NaN 0.0]

average

(average v)(average v weights)

Mean or weighted average of the vector

average-vectors

(average-vectors init vs)(average-vectors vs)

Average / centroid of vectors. Input: initial vector (optional), list of vectors

Examples

Usage

(average-vectors [[1 2] [0 1] [3 4] [1 2] [4 -1]])
;;=> [1.8 1.6]
(average-vectors (vec2 0 0) [(vec2 1 1) (vec2 1 1) (vec2 1 1)])
;;=> [0.75 0.75]

axis-rotate

(axis-rotate v angle axis)(axis-rotate v angle axis pivot)

Rotate vector. Only for Vec3 types

Examples

Usage

(axis-rotate (vec3 1.0 0.0 0.0) m/HALF_PI (vec3 0.0 1.0 0.0))
;;=> [6.12323399538461E-17 0.0 -1.0]
(axis-rotate (vec3 1.0 0.0 0.0)
             m/HALF_PI
             (vec3 0.0 1.0 0.0)
             (vec3 1.0 1.0 1.0))
;;=> [0.0 0.0 0.9999999999999999]

base-from

(base-from v)

List of perpendicular vectors (basis). Works only for Vec2 and Vec3 types.

Examples

Usage

(base-from (vec3 0.1 1.0 -1.0))
;;=> [[0.1 1.0 -1.0] [0.0 -0.7071067811865475 -0.7071067811865475]
;;=>  [-1.414213562373095 0.07071067811865475 -0.07071067811865475]]
(base-from (vec2 1.0 0.0))
;;=> [[1.0 0.0] [-0.0 1.0]]

cb

(cb vector)

Apply cb to vector elements.

cbrt

(cbrt vector)

Apply cbrt to vector elements.

Examples

Usage

(cbrt (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.7937005259840997 -1.1447142425533319 1.2805791649874945 0.0]

ceil

(ceil vector)

Apply ceil to vector elements.

Examples

Usage

(ceil (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.0 -1.0 3.0 0.0]

clamp

(clamp v mn mx)(clamp v)

Clamp elements.

Examples

Clamp

(clamp [-1 -2 -3 1 2 3])
;;=> [0.0 0.0 0.0 1.0 2.0 3.0]
(clamp [-1 -2 -3 1 2 3] -2.5 2.5)
;;=> [-1.0 -2.0 -2.5 1.0 2.0 2.5]

cos

(cos vector)

Apply cos to vector elements.

Examples

Usage

(cos (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.8775825618903728 0.07073720166770284 -0.5048461045998573 1.0]

cosh

(cosh vector)

Apply cosh to vector elements.

Examples

Usage

(cosh (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.1276259652063807 2.3524096152432477 4.1443131704103155 1.0]

cot

(cot vector)

Apply cot to vector elements.

Examples

Usage

(cot (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.830487721712452 -0.07091484430265245 -0.5848478064594645 ##Inf]

coth

(coth vector)

Apply coth to vector elements.

Examples

Usage

(coth (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [2.163953413738653 -1.104791392982512 1.0304477349900076 ##Inf]

cross

(cross v1 v2)

Cross product

Examples

Usage

(cross (vec3 1 2 3) (vec3 4 3 2))
;;=> [-5.0 10.0 -5.0]
(cross (vec2 1 2) (vec2 -1 2))
;;=> 4.0

csc

(csc vector)

Apply csc to vector elements.

Examples

Usage

(csc (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [2.0858296429334886 -1.0025113042467249 1.1584675035237058 ##Inf]

csch

(csch vector)

Apply csch to vector elements.

Examples

Usage

(csch (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.9190347513349437 -0.46964244059522453 0.24864137738123349 ##Inf]

Usage

(csch (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.9190347513349437 -0.46964244059522453 0.24864137738123349 ##Inf]

degrees

(degrees vector)

Apply degrees to vector elements.

Examples

Usage

(degrees (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [28.64788975654116 -85.94366926962348 120.32113697747289 0.0]

delta-eq

(delta-eq v1 v2)(delta-eq v1 v2 abs-tol)(delta-eq v1 v2 abs-tol rel-tol)

Equality with given absolute (and/or relative) toleance.

dhash-code

(dhash-code state a)(dhash-code a)

double hashcode

dist

(dist v1 v2)

Euclidean distance between vectors

Examples

Usage

(dist (vec4 1.0 2.0 -1.0 -2.0) (vec4 1.0 -2.0 3.0 4.0))
;;=> 8.246211251235321
(dist [9 8 7 6 5 4 3 2 1] [9 8 7 6 5 5 5 5 5])
;;=> 5.477225575051661

dist-abs

(dist-abs v1 v2)

Manhattan distance between vectors

Examples

Usage

(dist-abs (vec4 1.0 2.0 -1.0 -2.0) (vec4 1.0 -2.0 3.0 4.0))
;;=> 14.0
(dist-abs [9 8 7 6 5 4 3 2 1] [9 8 7 6 5 5 5 5 5])
;;=> 10.0

dist-ang

(dist-ang v1 v2)

Angular distance

Examples

Usage

(dist-ang (vec4 1.0 2.0 -1.0 -2.0) (vec4 1.0 -2.0 3.0 4.0))
;;=> 0.7996068408289091
(dist-ang [9 8 7 6 5 4 3 2 1] [9 8 7 6 5 5 5 5 5])
;;=> 0.09160164480902667

dist-canberra

(dist-canberra v1 v2)

Canberra distance

Examples

Usage

(dist-canberra (vec4 1.0 2.0 -1.0 -2.0) (vec4 1.0 -2.0 3.0 4.0))
;;=> 3.0
(dist-canberra [9 8 7 6 5 4 3 2 1] [9 8 7 6 5 5 5 5 5])
;;=> 1.4563492063492063

dist-cheb

(dist-cheb v1 v2)

Chebyshev distance between 2d vectors

Examples

Usage

(dist-cheb (vec4 1.0 2.0 -1.0 -2.0) (vec4 1.0 -2.0 3.0 4.0))
;;=> 6.0
(dist-cheb [9 8 7 6 5 4 3 2 1] [9 8 7 6 5 5 5 5 5])
;;=> 4.0

dist-discrete

(dist-discrete v1 v2)

Discrete distance between 2d vectors

Examples

Usage

(dist-discrete (vec4 1.0 2.0 -1.0 -2.0) (vec4 1.0 -2.0 3.0 4.0))
;;=> 3.0
(dist-discrete [9 8 7 6 5 4 3 2 1] [9 8 7 6 5 5 5 5 5])
;;=> 4.0

dist-emd

(dist-emd v1 v2)

Earth Mover’s Distance

Examples

Usage

(dist-emd (vec4 1.0 2.0 -1.0 -2.0) (vec4 1.0 -2.0 3.0 4.0))
;;=> 4.0
(dist-emd [9 8 7 6 5 4 3 2 1] [9 8 7 6 5 5 5 5 5])
;;=> 10.0

dist-sq

(dist-sq v1 v2)

Squared Euclidean distance between vectors

Examples

Usage

(dist-sq (vec4 1.0 2.0 -1.0 -2.0) (vec4 1.0 -2.0 3.0 4.0))
;;=> 68.0
(dist-sq [9 8 7 6 5 4 3 2 1] [9 8 7 6 5 5 5 5 5])
;;=> 30.0

distances

Examples

List of distances

(sort (keys distances))
;;=> (:abs :angular :canberra :cheb :discrete :emd :euclid :euclid-sq)

div

(div v1 v)(div v1)

Vector division or reciprocal.

Examples

Usage

(div [5 4 3 5] 4.0)
;;=> [1.25 1.0 0.75 1.25]

Applied to vector is reciprocal

(div [1 2 4 -5])
;;=> [1.0 0.5 0.25 -0.2]

dot

(dot v1 v2)

Dot product of two vectors.

Examples

Usage

(dot (vec4 1 0 0 0) (vec4 -1 0 -1 0))
;;=> -1.0

econstrain

(econstrain v mn mx)

Element-wise constrain

Examples

Usage

(econstrain (vec3 2 0 -2) -1 1)
;;=> [1.0 0.0 -1.0]

ediv

(ediv v1 v2)

Element-wise division of two vectors.

Examples

Usage

(ediv (vec2 1 3) (vec2 2 4))
;;=> [0.5 0.75]

einterpolate

(einterpolate v1 v2 v)(einterpolate v1 v2 v f)

Interpolate vector selement-wise, optionally set interpolation fn (default: lerp)

Examples

Usage

(einterpolate (vec2 -1 -1) (vec2 1 1) (vec2 0.25 0.75))
;;=> [-0.5 0.5]
(einterpolate (vec2 -1 -1)
              (vec2 1 1)
              (vec2 0.25 0.75)
              m/smooth-interpolation)
;;=> [-0.6875 0.6875]

emn

(emn v1 v2)

Element-wise min from two vectors.

Examples

Usage

(emn [-1 2] [1 -2])
;;=> [-1.0 -2.0]

emult

(emult v1 v2)

Element-wise vector multiplication (Hadamard product).

Examples

Usage

(emult (vec3 1 2 3) (vec3 9 9 9))
;;=> [9.0 18.0 27.0]

emx

(emx v1 v2)

Element-wise max from two vectors.

Examples

Usage

(emx [-1 2] [1 -2])
;;=> [1.0 2.0]

exp

(exp vector)

Apply exp to vector elements.

Examples

Usage

(exp (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.6487212707001282 0.22313016014842985 8.16616991256765 1.0]

expm1

(expm1 vector)

Apply expm1 to vector elements.

Examples

Usage

(expm1 (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.6487212707001282 -0.7768698398515701 7.16616991256765 0.0]

faceforward

(faceforward n v)

Flip normal n to match the same direction as v.

Examples

Usage

(faceforward (vec2 1.0 1.0) (vec2 -1.0 -3.0))
;;=> [-1.0 -1.0]
(faceforward (vec2 1.0 1.0) (vec2 1.0 0.0))
;;=> [1.0 1.0]

floor

(floor vector)

Apply floor to vector elements.

Examples

Usage

(floor (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.0 -2.0 2.0 0.0]

fmap

(fmap v f)

Apply function to all vector values (like map but returns the same type).

Examples

Usage

(fmap [5 3] (fn [v] (m/sin v)))
;;=> [-0.9589242746631386 0.1411200080598671]

frac

(frac vector)

Apply frac to vector elements.

Examples

Usage

(frac (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.5 0.5 0.10000000000000009 0.0]

from-polar

(from-polar v)

From polar coordinates (2d, 3d only)

Examples

Usage

(from-polar (vec2 1.0 (m/radians 90)))
;;=> [6.12323399538461E-17 1.0]
(from-polar (vec3 1.0 (m/radians 90) (m/radians 45)))
;;=> [0.7071067811865476 0.7071067811865475 6.12323399538461E-17]

generate-vec2

(generate-vec2 f1 f2)(generate-vec2 f)

Generate Vec2 with fn(s)

Examples

Usage

(generate-vec2 (constantly 2))
;;=> [2.0 2.0]
(generate-vec2 rand (constantly 1))
;;=> [0.5194820369938931 1.0]

generate-vec3

(generate-vec3 f1 f2 f3)(generate-vec3 f)

Generate Vec3 with fn(s)

Examples

Usage

(generate-vec3 rand)
;;=> [0.501112308699315 0.7207155719934696 0.16218937948219891]
(generate-vec3 rand (constantly 1) (constantly 2))
;;=> [0.7430096964207699 1.0 2.0]

generate-vec4

(generate-vec4 f1 f2 f3 f4)(generate-vec4 f)

Generate Vec4 with fn(s)

Examples

Usage

(generate-vec4 rand)
;;=> [0.6783152954910473 0.15646776453752909 0.9084675455754365
;;=>  0.9154446597284284]
(generate-vec4 rand rand (constantly 1) (constantly 2))
;;=> [0.01421826328123399 0.17205623293395933 1.0 2.0]

heading

(heading v)

Angle between vector and unit vector [1,0,...]

Examples

Usage

(m/degrees (heading (vec2 1.0 1.0)))
;;=> 45.0
(m/degrees (heading (vec3 1.0 0.0 1.0)))
;;=> 45.0
(m/degrees (heading (vec4 1.0 -1.0 1.0 -1.0)))
;;=> 59.99999999999999
(heading [1 2 3 4 5 6 7 8 9])
;;=> 1.5115267439240436
(heading (array-vec [1 2 3 4 5 6 7 8 9]))
;;=> 1.5115267439240436

interpolate

(interpolate v1 v2 t)(interpolate v1 v2 t f)

Interpolate vectors, optionally set interpolation fn (default: lerp)

Examples

Usage

(interpolate (vec2 -1 -1) (vec2 1 1) 0.25)
;;=> [-0.5 -0.5]
(interpolate (vec2 -1 -1) (vec2 1 1) 0.25 m/smooth-interpolation)
;;=> [-0.6875 -0.6875]

is-near-zero?

(is-near-zero? v)(is-near-zero? v abs-tol)(is-near-zero? v abs-tol rel-tol)

Equality to zero 0 with given absolute (and/or relative) toleance.

Examples

Usage

(is-near-zero? [0 1.0E-7])
;;=> true
(is-near-zero? [0 1.0E-6])
;;=> false
(is-near-zero? [0 1.0E-6] 1.0E-4)
;;=> true

;; Test: ok.

is-zero?

(is-zero? v)

Is vector zero?

Examples

Usage

(is-zero? [0 1.0E-7])
;;=> false
(is-zero? [0 0.0])
;;=> true

jinc

(jinc vector)

Apply jinc to vector elements.

lerp

(lerp v1 v2 t)

Linear interpolation of vectors

limit

(limit v len)

Limit length of the vector by given value

Examples

Usage

(limit (vec3 1.0 1.0 1.0) 1.0)
;;=> [0.5773502691896258 0.5773502691896258 0.5773502691896258]
(limit (vec3 1.0 1.0 1.0) 2.0)
;;=> [1.0 1.0 1.0]

ln

(ln vector)

Apply ln to vector elements.

Examples

Usage

(ln (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [-0.6931471805599453 ##NaN 0.7419373447293773 ##-Inf]

log

(log vector)

Apply log to vector elements.

Examples

Usage

(log (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [-0.6931471805599453 ##NaN 0.7419373447293773 ##-Inf]

log10

(log10 vector)

Apply log10 to vector elements.

Examples

Usage

(log10 (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [-0.3010299956639812 ##NaN 0.3222192947339193 ##-Inf]

log1mexp

(log1mexp vector)

Apply log1mexp to vector elements.

log1p

(log1p vector)

Apply log1p to vector elements.

Examples

Usage

(log1p (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.4054651081081644 ##NaN 1.1314021114911006 0.0]

log1pexp

(log1pexp vector)

Apply log1pexp to vector elements.

log1pmx

(log1pmx vector)

Apply log1pmx to vector elements.

log1psq

(log1psq vector)

Apply log1psq to vector elements.

log2

(log2 vector)

Apply log2 to vector elements.

Examples

Usage

(log2 (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [-1.0 ##NaN 1.070389327891398 ##-Inf]

logexpm1

(logexpm1 vector)

Apply logexpm1 to vector elements.

logit

(logit vector)

Apply logit to vector elements.

logmeanexp

(logmeanexp v)

logmxp1

(logmxp1 vector)

Apply logmxp1 to vector elements.

logsoftmax

(logsoftmax v)

logsumexp

(logsumexp v)

mag

(mag v)

Length of the vector.

Examples

Length of the vector

(mag (vec2 1 1))
;;=> 1.4142135623730951

;; Test: ok.

magsq

(magsq v)

Length of the vector squared.

Examples

Length of the vector, squared

(magsq [1 2 1 2])
;;=> 10.0

;; Test: ok.

make-vector

(make-vector dims xs)(make-vector dims)

Returns fixed size vector for given number of dimensions.

Proper type is used.

Examples

Usage

(make-vector 2)
;;=> [0.0 0.0]
(make-vector 2 (cycle [1 2 3]))
;;=> [1.0 2.0]
(make-vector 3)
;;=> [0.0 0.0 0.0]
(make-vector 3 (cycle [1 2 3]))
;;=> [1.0 2.0 3.0]
(make-vector 4)
;;=> [0.0 0.0 0.0 0.0]
(make-vector 4 (cycle [1 2 3]))
;;=> [1.0 2.0 3.0 1.0]
(make-vector 6)
;;=> [0.0 0.0 0.0 0.0 0.0 0.0]
(make-vector 6 (cycle [1 2 3]))
;;=> [1.0 2.0 3.0 1.0 2.0 3.0]
(type (make-vector 6))
;;=> class fastmath.vector.ArrayVec

maxdim

(maxdim v)

Index of maximum value.

Examples

Usage

(let [v (vec (repeatedly 100 (fn [] (- (int (rand-int 200)) 100))))
      mdim (maxdim v)]
  [mdim (v mdim)])
;;=> [24 99]
(maxdim (vec3 1 2 3))
;;=> 2

mindim

(mindim v)

Index of minimum value.

Examples

Usage

(let [v (vec (repeatedly 100 (fn [] (- (int (rand-int 200)) 100))))
      mdim (mindim v)]
  [mdim (v mdim)])
;;=> [1 -100]
(mindim (vec3 1 2 3))
;;=> 0

mn

(mn v)

Minimum value of vector elements

Examples

Usage

(mn (vec4 -1 -2 3 4))
;;=> -2.0

mult

(mult v x)

Multiply vector by number x.

Examples

Usage

(mult (vec4 5 4 3 5) 4.0)
;;=> [20.0 16.0 12.0 20.0]

mx

(mx v)

Maximum value of vector elements

Examples

Usage

(mx (vec4 -1 -2 3 4))
;;=> 4.0

near-zero?

(near-zero? v)(near-zero? v abs-tol)(near-zero? v abs-tol rel-tol)

Equality to zero 0 with given absolute (and/or relative) toleance.

nonzero-count

(nonzero-count v)

Count non zero velues in vector

Examples

Usage

(nonzero-count [1 2 3 -1 0 2 0 0])
;;=> 5

normalize

(normalize v)

Normalize vector (set length = 1.0)

Examples

Usage

(normalize (vec2 1.0 -1.0))
;;=> [0.7071067811865475 -0.7071067811865475]

permute

(permute v idxs)

Permute vector elements with given indices.

Examples

Usage

(permute (vec4 1 2 3 4) (vec4 0 3 2 1))
;;=> [1.0 4.0 3.0 2.0]

perpendicular

(perpendicular v)(perpendicular v1 v2)

Perpendicular vector. Only for Vec2 and Vec3 types.

Examples

Usage

(perpendicular (vec2 -4 0))
;;=> [-0.0 -1.0]
(perpendicular (vec3 1.0 0.0 0.0) (vec3 0.0 -1.0 0.0))
;;=> [0.0 0.0 -1.0]

prod

(prod v)

Product of elements

project

(project v1 v2)

Project v1 onto v2

radians

(radians vector)

Apply radians to vector elements.

Examples

Usage

(radians (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.008726646259971648 -0.026179938779914945 0.03665191429188092 0.0]

reciprocal

(reciprocal v)

Reciprocal of elements.

Examples

Usage

(reciprocal (vec3 1 2 5))
;;=> [1.0 0.5 0.2]

relative-angle-between

(relative-angle-between v1 v2)

Angle between two vectors relative to each other.

See also angle-between.

Examples

Usage

(m/degrees (relative-angle-between (vec3 1.0 0.0 0.0)
                                   (vec3 0.0 1.0 0.0)))
;;=> 90.0
(m/degrees (relative-angle-between (vec (repeatedly 50 rand))
                                   (vec (repeatedly 50 rand))))
;;=> 0.5814907572232303

rint

(rint vector)

Apply rint to vector elements.

Examples

Usage

(rint (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.0 -2.0 2.0 0.0]

rotate

(rotate v angle)(rotate v angle-x angle-y angle-z)

Rotate vector. Only for Vec2 and Vec3 types.

Examples

Usage

(rotate (vec2 1 2) (m/radians 90))
;;=> [-2.0 1.0000000000000002]
(rotate (vec3 1 2 3) (m/radians 90) 0 0)
;;=> [1.0 -3.0 2.0]

round

(round vector)

Apply round to vector elements.

Examples

Usage

(round (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.0 -1.0 2.0 0.0]

safe-sqrt

(safe-sqrt vector)

Apply safe-sqrt to vector elements.

Examples

Usage

(safe-sqrt (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.7071067811865476 0.0 1.449137674618944 0.0]

sec

(sec vector)

Apply sec to vector elements.

Examples

Usage

(sec (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.139493927324549 14.136832902969918 -1.9808016559672244 1.0]

sech

(sech vector)

Apply sech to vector elements.

Examples

Usage

(sech (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.886818883970074 0.4250960349422804 0.2412945062018547 1.0]

seq->vec2

(seq->vec2 xs)

Any seq to Vec2

Examples

Usage

(seq->vec2 [11 22 33 44 55])
;;=> [11.0 22.0]

seq->vec3

(seq->vec3 xs)

Any seq to Vec3

Examples

Usage

(seq->vec3 (lazy-seq (quote (1 2))))
;;=> [1.0 2.0 0.0]

seq->vec4

(seq->vec4 xs)

Any seq to Vec4

Examples

Usage

(seq->vec4 (double-array [11 22 33 44 55]))
;;=> [11.0 22.0 33.0 44.0]

set-mag

(set-mag v len)

Set length of the vector

Examples

Usage

(set-mag (vec2 0.22 0.22) (m/sqrt 2.0))
;;=> [1.0000000000000002 1.0000000000000002]
(set-mag (vec2 1.0 1.0) 0.0)
;;=> [0.0 0.0]

sfrac

(sfrac vector)

Apply sfrac to vector elements.

Examples

Usage

(sfrac (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.5 -0.5 0.10000000000000009 0.0]

sgn

(sgn vector)

Apply sgn to vector elements.

Examples

Usage

(sgn (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.0 -1.0 1.0 1.0]

shift

(shift v)(shift v x)

Add value to every vector element.

sigmoid

(sigmoid vector)

Apply sigmoid to vector elements.

Examples

Usage

(sigmoid (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.6224593312018546 0.18242552380635632 0.8909031788043871 0.5]

signum

(signum vector)

Apply signum to vector elements.

Examples

Usage

(signum (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [1.0 -1.0 1.0 0.0]

sim-cos

(sim-cos v1 v2)

Cosine similarity

Examples

Usage

(sim-cos (vec4 1.0 2.0 -1.0 -2.0) (vec4 1.0 -2.0 3.0 4.0))
;;=> -0.808290376865476
(sim-cos [9 8 7 6 5 4 3 2 1] [9 8 7 6 5 5 5 5 5])
;;=> 0.9588777312999299

sin

(sin vector)

Apply sin to vector elements.

Examples

Usage

(sin (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.47942553860420295 -0.9974949866040544 0.8632093666488738 0.0]

sinc

(sinc vector)

Apply sinc to vector elements.

Examples

Usage

(sinc (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.6366197723675814 -0.2122065907891938 0.04683960205159357 1.0]

sinh

(sinh vector)

Apply sinh to vector elements.

Examples

Usage

(sinh (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.5210953054937474 -2.1292794550948178 4.021856742157334 0.0]

softmax

(softmax v)

sq

(sq vector)

Apply sq to vector elements.

Examples

Usage

(sq (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.25 2.25 4.41 0.0]

sqrt

(sqrt vector)

Apply sqrt to vector elements.

Examples

Usage

(sqrt (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.7071067811865476 ##NaN 1.449137674618944 0.0]

sub

(sub v)(sub v1 v2)

Subtraction of two vectors.

Examples

Usage

(sub (vec2 1 2) (vec2 -1 -2))
;;=> [2.0 4.0]

sum

(sum v)

Sum of elements

Examples

Usage

(sum (vec (range 1000)))
;;=> 499500.0

tan

(tan vector)

Apply tan to vector elements.

Examples

Usage

(tan (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.5463024898437905 -14.101419947171719 -1.7098465429045075 0.0]

tanh

(tanh vector)

Apply tanh to vector elements.

Examples

Usage

(tanh (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.46211715726000974 -0.9051482536448664 0.9704519366134539 0.0]

to-polar

(to-polar v)

To polar coordinates (2d, 3d only), first element is length, the rest angle.

Examples

Usage

(to-polar (vec2 1.0 1.0))
;;=> [1.4142135623730951 0.7853981633974483]
(to-polar (vec3 1.0 0.0 1.0))
;;=> [1.4142135623730951 0.7853981633974483 0.0]

to-vec

deprecated in v1.5.0

Same as vec->Vec. Deprecated.

transform

(transform v o vx vy)(transform v o vx vy vz)

Transform vector; map point to coordinate system defined by origin, vx and vy (as bases), Only for Vec2 and Vec3 types.

Examples

Usage

(transform (vec2 1 1) (vec2 -1 -1) (vec2 1.0 0.0) (vec2 0.0 1.0))
;;=> [0.0 0.0]
(transform (vec3 1 1 1)
           (vec3 -1 -1 0)
           (vec3 1.0 0.0 1.0)
           (vec3 0.0 1.0 0.0)
           (vec3 0.0 1.0 1.0))
;;=> [0.0 1.0 2.0]

triple-product

(triple-product a b c)

a o (b x c)

trunc

(trunc vector)

Apply trunc to vector elements.

Examples

Usage

(trunc (fastmath.vector/vec4 0.5 -1.5 2.1 0.0))
;;=> [0.0 -1.0 2.0 0.0]

vec->array

(vec->array v)

Convert to double array

vec->RealVector

(vec->RealVector v)

Convert to Apache Commons Math RealVector

vec->Vec

(vec->Vec v)

Convert to Clojure primitive vector Vec.

Examples

Check types

(type (vec->Vec [1 2 3]))
;;=> class clojure.core.Vec
(type (vec->Vec (vec2 1 2)))
;;=> class clojure.core.Vec
(type (vec->Vec (vec3 1 2 3)))
;;=> class clojure.core.Vec
(type (vec->Vec (vec4 1 2 3 4)))
;;=> class clojure.core.Vec
(type (vec->Vec (array-vec 1)))
;;=> class clojure.core.Vec

vec2

(vec2 x y)(vec2)

Make 2d vector.

Examples

Usage

(vec2 0.5 -0.5)
;;=> [0.5 -0.5]
(vec2)
;;=> [0.0 0.0]

Destructuring

(let [[x y] (vec2 4.3 2.2)] (+ x y))
;;=> 6.5

As function

[((vec2 11 22) 0) ((vec2 11 22) 1)]
;;=> [11.0 22.0]

As sequence

(map (fn* [p1__37717#] (* 2.0 p1__37717#)) (vec2 1 2))
;;=> (2.0 4.0)
(reduce clojure.core/+ (vec2 6 4))
;;=> 10.0
(cons 6.0 (vec2 3 4))
;;=> (6.0 3.0 4.0)
(first (vec2 3 4))
;;=> 3.0
(second (vec2 3 4))
;;=> 4.0
(last (vec2 3 4))
;;=> 4.0
(nth (vec2 3 4) 1)
;;=> 4.0
(filter clojure.core/neg? (vec2 -1 2))
;;=> (-1.0)

Count

(count (vec2 4 3))
;;=> 2

vec3

(vec3 x y z)(vec3 v z)(vec3)

Make Vec2 vector

Examples

Usage

(vec3)
;;=> [0.0 0.0 0.0]
(vec3 0.5 -0.5 1.0)
;;=> [0.5 -0.5 1.0]
(let [v (vec2 1 2)] (vec3 v -1.0))
;;=> [1.0 2.0 -1.0]

vec4

(vec4 x y z w)(vec4 v w)(vec4 v z w)(vec4)

Make Vec4 vector

Examples

Usage

(vec4)
;;=> [0.0 0.0 0.0 0.0]
(vec4 0.5 -0.5 1.0 -1.0)
;;=> [0.5 -0.5 1.0 -1.0]
(let [v (vec2 1 2)] (vec4 v -1.0 0.1))
;;=> [1.0 2.0 -1.0 0.1]
(let [v (vec3 0 1 2)] (vec4 v 0.1))
;;=> [0.0 1.0 2.0 0.1]

xlogx

(xlogx vector)

Apply xlogx to vector elements.

zero-count

(zero-count v)

Count zeros in vector

Examples

Usage

(zero-count [1 2 3 -1 0 2 0 0])
;;=> 3

zero?

(zero? v)

Is vector zero?