diff --git a/CHANGELOG.md b/CHANGELOG.md index a72efd7e..6e1cd504 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,16 @@ # Change Log All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). +## [1.1.0] + +### Added + +* `fastmath.clustering` namespace + +### Changed + +* removed `k-means` from `fastmath.stats` + ## [1.0.3] ### Fixed diff --git a/README.md b/README.md index 6c8e8250..871e53a7 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Originally it was a part of generative art/glich [Clojure2d](https://github.com/ ## Installation ```clojure -[generateme/fastmath "1.0.3"] +[generateme/fastmath "1.1.0"] ``` ## Documentation @@ -98,6 +98,10 @@ Several easing functions (in, out, in-out) Great collection (100+) of R^2->R^2 functions. +### Clustering + +SMILE bindings for clustering + ### Other Plenty of constant values diff --git a/docs/fastmath.clustering.html b/docs/fastmath.clustering.html new file mode 100755 index 00000000..36c532db --- /dev/null +++ b/docs/fastmath.clustering.html @@ -0,0 +1,306 @@ + +fastmath.clustering documentation

fastmath.clustering

Clustering algorithms.

+

Various clustering algrorithms backed by SMILE library.

+

Currently implemented: only partition clustering.

+

Input data

+

It’s always sequence of n-sized samples as sequences.

+

For example, 2d samples [[1 2] [2 2] [3 3] ...]

+

For 1d data you can pass sequence of numbers of sequence of 1d seqs of numbers

+
[1 2 3]
+;; or
+[[1] [2] [3]]
+
+

Distances

+

Some of the methods use distance functions, currently supported are:

+
    +
  • :euclidean
  • +
  • :manhattan
  • +
  • :chebyshev
  • +
+

Output

+

Every function returns record which contains:

+
    +
  • :type - name of the method used
  • +
  • :data - input data
  • +
  • :clustering - sequence of cluster ids
  • +
  • :sizes - sizes of clusters
  • +
  • :clusters - number of clusters
  • +
  • :predict - predicting function (see below), qualify additional sample
  • +
  • :representatives - list of centroids or medoids if available
  • +
  • :info - additional statistics for your samples (like distortion)
  • +
  • :obj - SMILE object
  • +
+

Cluster id is a integer ranging from 0 to the number of clusters minus 1. Some methods mark outliers with outlier-id.

+

Record acts as function and can qualify additonal sample by calling :predict function, for example (data is sequence of 3d samples):

+
(let [cl (k-means data 10)] (cl [0 1 2]))
+
+

See k-means

+

Regrouping

+

Clustering record can be regroupped to the list of individual clusters. Call regroup and get list of maps with following structure:

+
    +
  • :key - cluster id
  • +
  • :data - samples which belong to the cluster
  • +
  • :outliers? - does it contain outliers or not
  • +
  • :representative - centroid/medoid or average vector if the former is not available
  • +
  • :size - size of cluster
  • +

Constants

clarans

(clarans data clusters)(clarans data dist clusters)(clarans data dist clusters max-neighbor)(clarans data dist clusters max-neighbor num-local)

Clustering Large Applications based upon RANdomized Search algorithm.

+

Input:

+
    +
  • data - sequence of samples
  • +
  • clusters - numbe of clusters
  • +
+

Optional:

+
    +
  • dist - distance method, default :euclidean
  • +
  • max-neighbor - maximum number of neighbors checked during random search
  • +
  • num-local - the number of local minima to search for
  • +
+

See more in SMILE doc

Examples

Usage

(dissoc (clarans (repeatedly
+                  1000
+                  (fn* []
+                    (r/randval 0.1 (r/irand -10 10) (r/irand 100 150))))
+                 :chebyshev
+                 2)
+        :data :clustering
+        :obj :predict)
+;;=> {:clusters 2,
+;;=>  :info {:distortion 11686.0, :maxneighbor 100, :numlocalminima 4},
+;;=>  :representatives ((126.0) (0.0)),
+;;=>  :sizes (897 103),
+;;=>  :type :clarans}

clustering-methods-list

List of clustering methods.

Examples

List of methods

clustering-methods-list
+;;=> (:dbscan :k-means :mec
+;;=>          :clarans :g-means
+;;=>          :neural-gas :x-means
+;;=>          :deterministic-annealing :denclue)

dbscan

(dbscan data min-pts radius)(dbscan data dist min-pts radius)

Density-Based Spatial Clustering of Applications with Noise algorithm.

+

Input:

+
    +
  • data - sequence of samples
  • +
  • dist (optional) - distance method, default :euclidean
  • +
  • min-pts - minimum number of neighbors
  • +
  • radius - the neighborhood radius
  • +
+

See more in SMILE doc

Examples

3d vectors

(dissoc (dbscan
+         (repeatedly
+          5000
+          (fn* []
+            (vector (r/randval 0.1 (r/irand -10 10) (r/irand 100 150))
+                    (r/randval (r/irand -10 10) (r/irand 100 150))
+                    (r/randval (r/irand -10 10) (r/irand 100 150)))))
+         10
+         20)
+        :data :clustering
+        :obj :predict)
+;;=> {:clusters 8,
+;;=>  :info {:minpts 10.0, :radius 20.0},
+;;=>  :representatives nil,
+;;=>  :sizes (1110 1154 1158 1042 139 139 115 143 0),
+;;=>  :type :dbscan}

denclue

(denclue data sigma m)

DENsity CLUstering algorithm.

+

Input:

+
    +
  • data - sequence of samples
  • +
  • sigma - gaussian kernel parameter
  • +
  • m - number of selected samples, much slower than number of all samples
  • +
+

See more in SMILE doc

Examples

Expect 2 clusters, uniform distribution.

((juxt :clusters :sizes :representatives)
+ (denclue (repeatedly 100 (fn* [] (r/randval (r/drand) (r/drand 5 6))))
+          1
+          10))
+;;=> [2 (51 49) nil]
+(map
+ (fn [m] (dissoc m :data))
+ (regroup
+  (denclue (repeatedly 1000
+                       (fn* [] (r/randval 0.1 (r/drand) (r/drand 5 6))))
+           1
+           10)))
+;;=> ({:key 0,
+;;=>   :outliers? false,
+;;=>   :representative (5.4851294023675425),
+;;=>   :size 901}
+;;=>  {:key 1,
+;;=>   :outliers? false,
+;;=>   :representative (0.5107267960829602),
+;;=>   :size 99})

deterministic-annealing

(deterministic-annealing data max-clusters)(deterministic-annealing data max-clusters alpha)

Deterministic Annealing algorithm.

+

Input:

+
    +
  • data - sequence of samples
  • +
  • max-clusters - number of clusters
  • +
  • alpha (optional) - temperature decreasing factor (valued from 0 to 1)
  • +
+

See more in SMILE doc

Examples

Usage

(map (fn [m] (dissoc m :data))
+     (-> (repeatedly 1000
+                     (fn* []
+                       (vector (r/randval (r/grand) (r/grand 5 1.0))
+                               (r/randval (r/grand) (r/grand 5 1.0)))))
+         (deterministic-annealing 4 0.5)
+         (regroup)))
+;;=> ({:key 1,
+;;=>   :outliers? false,
+;;=>   :representative (5.023101333705685 2.506790529242708),
+;;=>   :size 514}
+;;=>  {:key 0,
+;;=>   :outliers? false,
+;;=>   :representative (0.027958552996607915 2.5469299537709693),
+;;=>   :size 486})

distances-list

List of distances used in some clustring methods.

Examples

List of distances

distances-list
+;;=> (:chebyshev :euclidean :manhattan)

g-means

(g-means data max-clusters)

G-Means algorithm.

+

Input:

+
    +
  • data - sequence of samples
  • +
  • max-clusters - maximum number of clusters
  • +
+

See more in SMILE doc

Examples

Expect 2 clusters, uniform distribution.

((juxt :clusters :sizes :representatives)
+ (g-means (repeatedly 100 (fn* [] (r/randval (r/drand) (r/drand 5 6))))
+          4))
+;;=> [2 (41 59) ((0.4846521086121508) (5.593876148318546))]

k-means

(k-means data clusters)(k-means data clusters max-iter)(k-means data clusters max-iter runs)

K-Means++ algorithm.

+

Input:

+
    +
  • data - sequence of samples
  • +
  • clusters - number of clusters
  • +
  • max-iter (optional) - maximum number of iterations
  • +
  • runs (optional) - maximum number of runs
  • +
+

See more in SMILE doc

Examples

Usage

(k-means [1 2 3 -1 -1 2 -1 11 111] 4)
+;;=> #fastmath.clustering/ClusteringResult
+;;=>  {:clustering (3 3 3 0 0 3 0 2 1),
+;;=>   :clusters 4,
+;;=>   :data [1 2 3 -1 -1 2 -1 11 111],
+;;=>   :info {:distortion 2.0000000000000004},
+;;=>   :obj
+;;=>   #object[smile.clustering.KMeans 0x4dbb6e75 "K-Means distortion: 2,00000\r\nClusters of 9 data points of dimension 1:\r\n  0\t    3 (33.3%)\r\n  1\t    1 (11.1%)\r\n  2\t    1 (11.1%)\r\n  3\t    4 (44.4%)\r\n"],
+;;=>   :predict #,
+;;=>   :representatives ((-1.0) (111.0) (11.0) (2.0)),
+;;=>   :sizes (3 1 1 4),
+;;=>   :type :k-means}

Clusters group into separate maps.

(regroup (k-means [1 2 3 -1 -1 2 -1 11 111] 4))
+;;=> ({:data (1 2 3 2),
+;;=>   :key 1,
+;;=>   :outliers? false,
+;;=>   :representative (2.0),
+;;=>   :size 4}
+;;=>  {:data (-1 -1 -1),
+;;=>   :key 2,
+;;=>   :outliers? false,
+;;=>   :representative (-1.0),
+;;=>   :size 3}
+;;=>  {:data (11), :key 3, :outliers? false, :representative (11.0), :size 1}
+;;=>  {:data (111),
+;;=>   :key 0,
+;;=>   :outliers? false,
+;;=>   :representative (111.0),
+;;=>   :size 1})

Use as predictor

(let [cl (k-means [1 2 3 -1 -1 2 -1 11 111] 4)]
+  [(cl -1) (cl 10) (cl 100) (cl 1) (cl -1000) (cl 1000)])
+;;=> [2 3 1 0 2 1]

mec

(mec data max-clusters radius)(mec data dist max-clusters radius)

Nonparametric Minimum Conditional Entropy Clustering algorithm.

+

Input:

+
    +
  • data - sequence of samples
  • +
  • dist (optional) - distance method, default :euclidean
  • +
  • max-clusters - maximum number of clusters
  • +
  • radius - the neighborhood radius
  • +
+

See more in SMILE doc

Examples

2d vectors

(dissoc (mec (repeatedly
+              5000
+              (fn* []
+                (vector
+                 (r/randval 0.1 (r/irand -10 10) (r/irand 100 150))
+                 (r/randval (r/irand -10 10) (r/irand 100 150)))))
+             :manhattan
+             8
+             20)
+        :data :clustering
+        :obj :predict)
+;;=> {:clusters 5,
+;;=>  :info {:entropy 246.23389628629226},
+;;=>  :representatives nil,
+;;=>  :sizes (1006 1244 244 252 2254),
+;;=>  :type :mec}

neural-gas

(neural-gas data clusters)(neural-gas data clusters lambda-i lambda-f eps-i eps-f steps)

Neural Gas algorithm.

+

Input:

+
    +
  • data - sequence of samples
  • +
  • clusters - number of clusters
  • +
+

Optional:

+
    +
  • lambda-i - intial lambda value (soft learning radius/rate)
  • +
  • lambda-f - final lambda value
  • +
  • eps-i - initial epsilon value (learning rate)
  • +
  • eps-f - final epsilon value
  • +
  • steps - number of iterations
  • +
+

See more in SMILE doc

Examples

Usage

(neural-gas [1 2 3 -1 -1 2 -1 11 111] 4)
+;;=> #fastmath.clustering/ClusteringResult
+;;=>  {:clustering (0 0 0 0 0 0 0 3 1),
+;;=>   :clusters 4,
+;;=>   :data [1 2 3 -1 -1 2 -1 11 111],
+;;=>   :info {:distortion 100.21135168578684},
+;;=>   :obj
+;;=>   #object[smile.vq.NeuralGas 0x64bcf1a3 "Neural Gas distortion: 100,21135\r\nClusters of 9 data points of dimension 1:\r\n  0\t    7 (77.8%)\r\n  1\t    1 (11.1%)\r\n  2\t    0 ( 0.0%)\r\n  3\t    1 (11.1%)\r\n"],
+;;=>   :predict #,
+;;=>   :representatives ((0.6767057694450506)
+;;=>                     (102.02680736489556)
+;;=>                     (38.047386026088205)
+;;=>                     (9.498431345311419)),
+;;=>   :sizes (7 1 0 1),
+;;=>   :type :neural-gas}

Clusters group into separate maps.

(regroup (neural-gas [1 2 3 -1 -1 2 -1 11 111] 4))
+;;=> ({:data (1 2 3 -1 -1 2 -1),
+;;=>   :key 2,
+;;=>   :outliers? false,
+;;=>   :representative (0.6767057694246992),
+;;=>   :size 7}
+;;=>  {:data (11),
+;;=>   :key 3,
+;;=>   :outliers? false,
+;;=>   :representative (9.498429886559977),
+;;=>   :size 1}
+;;=>  {:data (111),
+;;=>   :key 1,
+;;=>   :outliers? false,
+;;=>   :representative (102.02680736489556),
+;;=>   :size 1})

outlier-id

const

;;=> 2147483647

Id of the cluster which contain outliers.

regroup

(regroup clustered-data)

Transform clusterig result into list of clusters as separate maps.

+

Every map contain:

+
    +
  • :key - cluster id
  • +
  • :data - samples which belong to the cluster
  • +
  • :outliers? - does it contain outliers or not
  • +
  • :representative - centroid/medoid or average vector if the former is not available
  • +
  • :size - size of cluster
  • +
+

Representative is always a n-dimensional sequence even if input is a list of numbers.

+

Empty clusters are skipped.

Examples

Result of clustering with regrouping

(k-means [1 2 3 -1 -1 2 -1 11 111] 7)
+;;=> #fastmath.clustering/ClusteringResult
+;;=>  {:clustering (4 0 5 3 3 0 3 2 1),
+;;=>   :clusters 7,
+;;=>   :data [1 2 3 -1 -1 2 -1 11 111],
+;;=>   :info {:distortion 0.0},
+;;=>   :obj
+;;=>   #object[smile.clustering.KMeans 0x15c3ebea "K-Means distortion: 0,00000\r\nClusters of 9 data points of dimension 1:\r\n  0\t    2 (22.2%)\r\n  1\t    1 (11.1%)\r\n  2\t    1 (11.1%)\r\n  3\t    3 (33.3%)\r\n  4\t    1 (11.1%)\r\n  5\t    1 (11.1%)\r\n  6\t    0 ( 0.0%)\r\n"],
+;;=>   :predict #,
+;;=>   :representatives ((2.0) (111.0) (11.0) (-1.0) (1.0) (3.0) (##NaN)),
+;;=>   :sizes (2 1 1 3 1 1 0),
+;;=>   :type :k-means}
+(regroup (k-means [1 2 3 -1 -1 2 -1 11 111] 7))
+;;=> ({:data (1), :key 4, :outliers? false, :representative (1.0), :size 1}
+;;=>  {:data (2 2), :key 5, :outliers? false, :representative (2.0), :size 2}
+;;=>  {:data (3), :key 0, :outliers? false, :representative (3.0), :size 1}
+;;=>  {:data (-1 -1 -1),
+;;=>   :key 3,
+;;=>   :outliers? false,
+;;=>   :representative (-1.0),
+;;=>   :size 3}
+;;=>  {:data (11), :key 2, :outliers? false, :representative (11.0), :size 1}
+;;=>  {:data (111),
+;;=>   :key 1,
+;;=>   :outliers? false,
+;;=>   :representative (111.0),
+;;=>   :size 1})
+(count (regroup (k-means [1 2 3 -1 -1 2 -1 11 111] 7)))
+;;=> 6

x-means

(x-means data max-clusters)

X-Means algorithm.

+

Input:

+
    +
  • data - sequence of samples
  • +
  • max-clusters - number of clusters
  • +
+

See more in SMILE doc

Examples

Expect 2 clusters, gaussian distribution.

((juxt :clusters :sizes :representatives)
+ (x-means (repeatedly 10000
+                      (fn* [] (r/randval (r/grand) (r/grand 5 1.0))))
+          4))
+;;=> [2 (5033 4967) ((5.00854129653856) (0.0032155072522262163))]
\ No newline at end of file diff --git a/docs/fastmath.complex.html b/docs/fastmath.complex.html index 02cd2249..5026b441 100755 --- a/docs/fastmath.complex.html +++ b/docs/fastmath.complex.html @@ -1,6 +1,6 @@ -fastmath.complex documentation

fastmath.complex

Complex numbers functions.

+fastmath.complex documentation

fastmath.complex

Complex numbers functions.

Complex number is represented as Vec2 type (from clojure2d.math.vector namespace).

To create complex number use complex, vec2 or ->Vec2.

Simplified implementation based on Apache Commons Math. Functions don’t check NaNs or INF values.

diff --git a/docs/fastmath.core.html b/docs/fastmath.core.html index 0bbe9472..eae9252a 100755 --- a/docs/fastmath.core.html +++ b/docs/fastmath.core.html @@ -1,7 +1,7 @@ -fastmath.core documentation

fastmath.core

Collection of fast math functions and plethora of constants known from other math libraries.

-

Primitive math operators

+fastmath.core documentation

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:

@@ -16,9 +16,9 @@

Prim
  • 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

    +

    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

    +

    Other functions

    Additionally namespace contains functions which are common in frameworks like OpenFrameworks and Processing.

    • For random/noise functions check fastmath.random namespace.
    • @@ -44,7 +44,7 @@

      Other functions

      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)
    +;;=> ((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
    @@ -195,15 +195,17 @@ 

    Other functions

    (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@7773054a
    +;;=> [D@28e6d65f
     (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)

    seq->double-double-array

    (seq->double-double-array vss)

    Convert sequence to double-array of double-arrays.

    +;;=> (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@22f144b1
    +;;=> [[D@32c886e6
     (double-double-array->seq (seq->double-double-array [[1 2] [3 4]]))
    -;;=> ((1.0 2.0) (3.0 4.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)
    +;;=> ((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.

    @@ -215,7 +217,7 @@

    Other functions

    ;;=> 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

    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.

    \ No newline at end of file diff --git a/docs/fastmath.easings.html b/docs/fastmath.easings.html index cdca3054..49b90b85 100755 --- a/docs/fastmath.easings.html +++ b/docs/fastmath.easings.html @@ -1,6 +1,6 @@ -fastmath.easings documentation

    fastmath.easings

    Easing functions.

    +fastmath.easings documentation

    fastmath.easings

    Easing functions.

    List of all are in easings-list.

    Code snippets

    Save incanter graph

    (defn save-graph
       [f params & opts]
       (let [fname (str "images/e/" (first opts) ".png")]
    diff --git a/docs/fastmath.fields.html b/docs/fastmath.fields.html
    index 81b02189..ec18206c 100755
    --- a/docs/fastmath.fields.html
    +++ b/docs/fastmath.fields.html
    @@ -1,13 +1,13 @@
     
    -fastmath.fields documentation

    fastmath.fields

    Vector field functions.

    +fastmath.fields documentation

    fastmath.fields

    Vector field functions.

    Vector fields are functions R^2->R^2.

    Names are taken from fractal flames world where such fields are call variations. Most implementations are taken from JWildfire software.

    -

    Creation

    +

    Creation

    To create vector field call field multimethod with name of the field as keyword.

    Some of the vector fields require additional configuration as a map of parameters as keywords and values. Call parametrization to create random one or to merge with provided.

    Additionally you can provide amount parameter which is scaling factor for vector field (default: 1.0).

    -

    Derived fields

    +

    Derived fields

    You can use several method to derive new vector field from the other one(s). Possible options are:

    -

    Scalar fields

    +

    Scalar fields

    You can derive scalar fields from given vector field(s):

    • jacobian - determinant of jacobian matrix
    • @@ -25,7 +25,7 @@

      Scalar fields

    • dot - dot product
    • angle-between - angle between vectors from fields.
    -

    Combinations

    +

    Combinations

    The other option is to create vector field using some of the above possibilities. Combination is a tree of field operations with parametrizations. Functions:

    • combine - create vector field randomly of from given parametrization.
    • @@ -70,7 +70,7 @@

      Combinations

    • :angles - vector field from angles

    See random-configuration for example.

    Examples

    Create random combination

    (let [f (combine)] (f (v/vec2 -0.5 0.5)))
    -;;=> #vec2 [0.5290833818481148, -1.1001869390540726]

    Create combination for given configuration

    (let [conf {:type :operation,
    +;;=> #vec2 [-0.11529403328101333, -0.08097036071848729]

    Create combination for given configuration

    (let [conf {:type :operation,
                 :name :comp,
                 :var1 {:type :variation,
                        :name :blocky,
    @@ -99,22 +99,22 @@ 

    Combinations

    ;;=> 1.5302948024685854

    field

    multimethod

    Return vector field for given name and options: amount (scaling factor) and parametrization.

    Default scaling factor is 1.0, default parametrization is random.

    Resulting function operates on Vec2 type.

    Examples

    Get vector field by name

    (field :sinusoidal)
    -;;=> fastmath.fields$make_sinusoidal$fn__10993@c790b46
    +;;=> fastmath.fields$make_sinusoidal$fn__30674@376aeed
     ((field :sinusoidal) (v/vec2 m/HALF_PI m/HALF_PI))
     ;;=> #vec2 [1.0, 1.0]

    Get vector field by name and scale

    (field :sinusoidal 0.5)
    -;;=> fastmath.fields$make_sinusoidal$fn__10993@4e10067d
    +;;=> fastmath.fields$make_sinusoidal$fn__30674@b48be28
     ((field :sinusoidal 0.5) (v/vec2 m/HALF_PI m/HALF_PI))
     ;;=> #vec2 [0.5, 0.5]

    Apply parametrization

    (let [params (parametrization :cpow3)
           f (field :cpow3 1.0 params)]
       {:parametrization params, :value (f (v/vec2 -1.0 1.0))})
    -;;=> {:parametrization {:a -1.027883317179492,
    -;;=>                    :discrete-spread 0.7910279968110224,
    -;;=>                    :divisor 0.28827117514856804,
    -;;=>                    :offset2 -1.855418094213078,
    -;;=>                    :r 1.3910205700392493,
    -;;=>                    :spread -0.6958560972015344,
    -;;=>                    :spread2 -1.6226069692931797},
    -;;=>  :value #vec2 [-83948.09092212954, -95296.9692904463]}

    fields-list

    Examples

    List of all vector field names.

    (sort fields-list)
    +;;=> {:parametrization {:a 1.9970530148378383,
    +;;=>                    :discrete-spread 1.4597411339770134,
    +;;=>                    :divisor 0.6960262227580584,
    +;;=>                    :offset2 1.2960719259024356,
    +;;=>                    :r 1.8665653821917503,
    +;;=>                    :spread 1.3312992716874263,
    +;;=>                    :spread2 -1.0345115562258163},
    +;;=>  :value #vec2 [0.40325016867201424, 0.1353507183943091]}

    fields-list

    Examples

    List of all vector field names.

    (sort fields-list)
     ;;=> (:arch
     ;;=>  :asteria
     ;;=>  :atan :auger
    @@ -277,55 +277,86 @@ 

    Combinations

    Optinally you can pass part of the parametrization. In this case function will add remaining keys with randomly generated values.

    If field doesn’t have parametrization, empty map will be returned.

    See field.

    Examples

    Get random parametrization for given field

    (parametrization :auger)
    -;;=> {:freq -3.0859586681349525,
    -;;=>  :scale -0.9587085563264008,
    -;;=>  :sym 1.834501264037586,
    -;;=>  :weight -0.005060306756689448}

    Add lacking fields

    (parametrization :auger {:scale 1.0, :freq 1.0})
    +;;=> {:freq 3.2229094212774427,
    +;;=>  :scale 1.4291359982941958,
    +;;=>  :sym 0.8620662941401442,
    +;;=>  :weight 0.5438593560884912}

    Add lacking fields

    (parametrization :auger {:scale 1.0, :freq 1.0})
     ;;=> {:freq 1.0,
     ;;=>  :scale 1.0,
    -;;=>  :sym -0.0010320305160465004,
    -;;=>  :weight 0.3690021900469187}

    Returns empty map when field doesn’t have parametrization

    (parametrization :sinusoidal)
    +;;=>  :sym -0.5118174623638403,
    +;;=>  :weight -0.24666666442573715}

    Returns empty map when field doesn’t have parametrization

    (parametrization :sinusoidal)
     ;;=> {}

    random-configuration

    (random-configuration)(random-configuration depth)(random-configuration depth f)

    Create random configuration for combine function. Optionally with depth (0 = only root is created).

    See combine for structure.

    Bind *skip-random-fields* to true to exclude fields which are random.

    Examples

    Generate random configuration

    (random-configuration)
    -;;=> {:amount 1.0, :config {}, :name :cylinder, :type :variation}

    One node configuration

    (random-configuration 0)
    -;;=> {:amount 1.0, :config {}, :name :foucaut, :type :variation}

    Configuration with depth 2

    (random-configuration 2)
    -;;=> {:amount 1.0,
    -;;=>  :name :comp,
    +;;=> {:amount 0.7412632758103176,
    +;;=>  :name :add,
     ;;=>  :type :operation,
    -;;=>  :var1 {:amount 1.0,
    +;;=>  :var1 {:amount 0.05603520263329731,
     ;;=>         :name :add,
     ;;=>         :type :operation,
    -;;=>         :var1 {:amount -0.38896511052921534,
    -;;=>                :config {},
    -;;=>                :name :loonie,
    +;;=>         :var1 {:amount -0.5099973215388385,
    +;;=>                :name :comp,
    +;;=>                :type :operation,
    +;;=>                :var1 {:amount 1.0,
    +;;=>                       :config {:distort -0.568695763777221,
    +;;=>                                :multiplier 0.5651337111380562},
    +;;=>                       :name :stwin,
    +;;=>                       :type :variation},
    +;;=>                :var2 {:amount 1.0,
    +;;=>                       :config {},
    +;;=>                       :name :hemisphere,
    +;;=>                       :type :variation}},
    +;;=>         :var2 {:amount -1.5355275407147722,
    +;;=>                :config {:distance 0.9437575560105342,
    +;;=>                         :radius 1.705237632727735},
    +;;=>                :name :bmod,
    +;;=>                :type :variation}},
    +;;=>  :var2 {:amount 1.2930131485167613,
    +;;=>         :config {:Dens1 0.07882714218202802,
    +;;=>                  :Dens2 0.5868500132153693,
    +;;=>                  :K -1.7997295389810266,
    +;;=>                  :Reverse -0.9695878806367173,
    +;;=>                  :Sc 0.4300620755940313,
    +;;=>                  :Seed 689712544,
    +;;=>                  :X 18.951169603712987,
    +;;=>                  :Y 2.5662719327495},
    +;;=>         :name :circlelinear,
    +;;=>         :type :variation}}

    One node configuration

    (random-configuration 0)
    +;;=> {:amount 1.0, :config {}, :name :erf, :type :variation}

    Configuration with depth 2

    (random-configuration 2)
    +;;=> {:amount 0.3654909885423369,
    +;;=>  :name :add,
    +;;=>  :type :operation,
    +;;=>  :var1 {:amount 0.9785045015332838,
    +;;=>         :name :add,
    +;;=>         :type :operation,
    +;;=>         :var1 {:amount 1.031199463268642,
    +;;=>                :config {:high 1.2140517032656555,
    +;;=>                         :low -0.25398734004904844,
    +;;=>                         :waves 1.382558839998536},
    +;;=>                :name :blob,
     ;;=>                :type :variation},
    -;;=>         :var2 {:amount -1.978326319628585,
    +;;=>         :var2 {:amount 0.6854382080226196,
     ;;=>                :config {},
    -;;=>                :name :noise,
    +;;=>                :name :cross,
     ;;=>                :type :variation}},
    -;;=>  :var2 {:amount 1.0,
    -;;=>         :config {:correctd -0.10570874404742536,
    -;;=>                  :correctn -1.49107210788639,
    -;;=>                  :denominator -16.870277795143114,
    -;;=>                  :numerator 3.1738725224288515,
    -;;=>                  :root -3.790084551225446},
    -;;=>         :name :powblock,
    +;;=>  :var2 {:amount -1.7575410682582797,
    +;;=>         :config {},
    +;;=>         :name :sinusoidal,
     ;;=>         :type :variation}}

    randomize-configuration

    (randomize-configuration f)

    Randomize values for given configuration. Keeps structure untouched.

    Examples

    Usage

    (let [conf {:type :variation,
                 :name :blocky,
                 :amount 1.0,
                 :config {:x -1.4, :y 0.9, :mp 2.6}}]
       [(randomize-configuration conf) (randomize-configuration conf)])
     ;;=> [{:amount 1.0,
    -;;=>   :config {:mp -5.319779837039107,
    -;;=>            :x 1.192112272968585,
    -;;=>            :y -0.5659895082838441},
    +;;=>   :config {:mp -4.368966809437412,
    +;;=>            :x 0.6704656447146687,
    +;;=>            :y -0.7977613495062006},
     ;;=>   :name :blocky,
     ;;=>   :type :variation}
     ;;=>  {:amount 1.0,
    -;;=>   :config {:mp -3.1133839905285705,
    -;;=>            :x 0.781652073973997,
    -;;=>            :y 1.2051764264026015},
    +;;=>   :config {:mp -5.283987859033081,
    +;;=>            :x 1.2840517487451941,
    +;;=>            :y 1.1404939211797904},
     ;;=>   :name :blocky,
     ;;=>   :type :variation}]

    scalar->vector-field

    (scalar->vector-field scalar f)(scalar->vector-field scalar f1 f2)

    Returns vector field build from scalar fields of the input vector and result of the vector field.

    Examples

    Usage

    (let [f (scalar->vector-field v/heading (field :sinusoidal))]
       (v/applyf (f (v/vec2 m/HALF_PI m/HALF_PI)) m/degrees))
    diff --git a/docs/fastmath.interpolation.html b/docs/fastmath.interpolation.html
    index c73a274a..1d35e8ed 100755
    --- a/docs/fastmath.interpolation.html
    +++ b/docs/fastmath.interpolation.html
    @@ -1,6 +1,6 @@
     
    -fastmath.interpolation documentation

    fastmath.interpolation

    1d, 2d interpolation functions.

    +fastmath.interpolation documentation

    fastmath.interpolation

    1d, 2d interpolation functions.

    See more:

    One

    Two

    Three

    random-noise-fn

    (random-noise-fn cfg)(random-noise-fn)

    Create random noise function from all possible options.

    Optionally provide own configuration cfg. In this case one of 4 different blending methods will be selected.

    randval

    macro

    (randval v1 v2)(randval prob v1 v2)

    Retrun value with given probability (default 0.5)

    Examples

    Usage

    (randval :val-one :val-two)
     ;;=> :val-two
     (randval 0.001 :low-probability :high-probability)
    @@ -217,9 +217,9 @@ 

    Integer di {:octaves 3, :lacunarity 2.1, :gain 0.7, :noise-type :simplex})] (n 0.5 1.1 -1.3)) ;;=> 0.5497357888943046

    2d noise

    rng

    multimethod

    Create RNG for given name (as keyword) and optional seed. Return object enhanced with RNGProto. See: rngs-list for names.

    Examples

    Creating

    (rng :mersenne)
    -;;=> org.apache.commons.math3.random.MersenneTwister@18f67db4
    +;;=> org.apache.commons.math3.random.MersenneTwister@1f09259f
     (rng :isaac 1234)
    -;;=> org.apache.commons.math3.random.ISAACRandom@5b63eae

    Using

    (irandom (rng :mersenne 999) 15 25)
    +;;=> org.apache.commons.math3.random.ISAACRandom@37e3802a

    Using

    (irandom (rng :mersenne 999) 15 25)
     ;;=> 17

    RNGProto

    protocol

    Defines set of random functions for different RNGs or distributions returning primitive values.

    members

    ->seq

    (->seq t)(->seq t n)

    Returns sequence of random samples limited to optional n values.

    Examples

    Sequence of random values from distribution

    (->seq (distribution :gamma) 5)
     ;;=> (1.3985071121468868
     ;;=>  1.9713574080234095
    @@ -227,28 +227,28 @@ 

    Integer di ;;=> 1.5751629265513674 ;;=> 3.0898296399038285)

    brandom

    (brandom t)(brandom t thr)

    Boolean random. Returns true or false with equal probability. You can set probability for true setting thr (from [0-1] range).

    See brand.

    Examples

    boolean

    (rngproto-snippet brandom ...)
    -;;=> false

    drandom

    (drandom t)(drandom t mx)(drandom t mn mx)

    Random double.

    +;;=> true

    drandom

    (drandom t)(drandom t mx)(drandom t mn mx)

    Random double.

    For RNGs: As default returns random double from [0,1) range. When mx is passed, range is set to [0, mx). When mn is passed, range is set to [mn, mx).

    See drand.

    For distributions, just returns random double (call without parameters).

    Examples

    double

    (rngproto-snippet drandom ...)
    -;;=> 0.3485525632755664

    Double random value from distribution

    (drandom (distribution :gamma))
    +;;=> 0.12280377553069832

    Double random value from distribution

    (drandom (distribution :gamma))
     ;;=> 1.7209575348841526

    frandom

    (frandom t)(frandom t mx)(frandom t mn mx)

    Random float.

    For RNGs: As default returns random float from [0,1) range. When mx is passed, range is set to [0, mx). When mn is passed, range is set to [mn, mx).

    See frand.

    For distributions, just returns random float (call without parameters).

    Examples

    float

    (rngproto-snippet frandom ...)
    -;;=> 0.31610963

    Float random value from distribution (sample cast to float)

    (frandom (distribution :gamma))
    +;;=> 0.8633272

    Float random value from distribution (sample cast to float)

    (frandom (distribution :gamma))
     ;;=> 3.6556783

    grandom

    (grandom t)(grandom t std)(grandom t mean std)

    Random double from gaussian distribution. As default returns random double from N(0,1). When std is passed, N(0,std) is used. When mean is passed, distribution is set to N(mean, std).

    See grand.

    Examples

    gaussian double

    (rngproto-snippet grandom ...)
    -;;=> 1.071352745701417

    irandom

    (irandom t)(irandom t mx)(irandom t mn mx)

    Random integer.

    +;;=> 1.9672689953905065

    irandom

    (irandom t)(irandom t mx)(irandom t mn mx)

    Random integer.

    For RNGs: As default returns random integer from full integer range. When mx is passed, range is set to [0, mx). When mn is passed, range is set to [mn, mx).

    See irand.

    For distributions, just returns random integer (call without parameters).

    Examples

    integer

    (rngproto-snippet irandom ...)
    -;;=> -1779834783

    Integer random value from distribution (sample cast to int)

    (irandom (distribution :gamma))
    +;;=> 814805834

    Integer random value from distribution (sample cast to int)

    (irandom (distribution :gamma))
     ;;=> 4

    lrandom

    (lrandom t)(lrandom t mx)(lrandom t mn mx)

    Random long.

    For RNGs: As default returns random long from full long range. When mx is passed, range is set to [0, mx). When mn is passed, range is set to [mn, mx).

    See lrand.

    For distributions, just returns random long (call without parameters).

    Examples

    long

    (rngproto-snippet lrandom ...)
    -;;=> -2415189781520483255

    Long random value from distribution (sample cast to long)

    (lrandom (distribution :gamma))
    +;;=> -5797707407805087982

    Long random value from distribution (sample cast to long)

    (lrandom (distribution :gamma))
     ;;=> 0

    set-seed!

    (set-seed! t v)

    Sets seed. Returns RNG or distribution itself.

    Examples

    Set seed for the RNG object

    (let [rng (rng :isaac)]
       (set-seed! rng 1234)
       (irandom rng 10 15))
    @@ -272,15 +272,15 @@ 

    Integer di ;;=> #vec2 [0.125, 0.8888888888888888])

    Usage (1d)

    (let [gen (sequence-generator :sobol 1)] (take 5 (gen)))
     ;;=> (0.0 0.5 0.75 0.25 0.375)

    Halton plot (1000 samples)

    Sobol plot (1000 samples)

    Sphere plot (1000 samples)

    Gaussian plot (1000 samples)

    Default plot (1000 samples)

    sequence-generators-list

    List of random sequence generator. See sequence-generator.

    Examples

    Generator names.

    (sort sequence-generators-list)
     ;;=> (:default :gaussian :halton :sobol :sphere)

    simplex

    (simplex x)(simplex x y)(simplex x y z)

    Create Simplex noise. 6 octaves.

    Examples

    Usage

    (simplex 3.3)
    -;;=> 0.5096442877980952
    +;;=> 0.43967244782476217
     (simplex 3.3 1.1)
    -;;=> 0.5751061746072044
    +;;=> 0.5700423801993916
     (simplex 3.3 0.0 -0.1)
    -;;=> 0.6730292363968908

    2d noise

    single-noise

    Examples

    Usage

    (let [n (single-noise {:interpolation :linear})] (n 0.5 1.1 -1.3))
    +;;=> 0.40732770657782974

    2d noise

    single-noise

    Examples

    Usage

    (let [n (single-noise {:interpolation :linear})] (n 0.5 1.1 -1.3))
     ;;=> 0.627

    2d noise

    vnoise

    (vnoise x)(vnoise x y)(vnoise x y z)

    Value Noise.

    6 octaves, Hermite interpolation (cubic, h01).

    Examples

    Usage

    (vnoise 3.3)
    -;;=> 0.27846446202769165
    +;;=> 0.6987089983668293
     (vnoise 3.3 1.1)
    -;;=> 0.6497716338792594
    +;;=> 0.3744086136400674
     (vnoise 3.3 0.0 -0.1)
    -;;=> 0.6150280086062385

    2d noise

    \ No newline at end of file +;;=> 0.5490152343472502

    2d noise

    \ No newline at end of file diff --git a/docs/fastmath.rbf.html b/docs/fastmath.rbf.html index bffa0b58..3f519315 100755 --- a/docs/fastmath.rbf.html +++ b/docs/fastmath.rbf.html @@ -1,6 +1,6 @@ -fastmath.rbf documentation

    fastmath.rbf

    Radial Basis Function

    +fastmath.rbf documentation

    fastmath.rbf

    Radial Basis Function

    Create with multifunction rbf.

    All of them accept scaling factor scale. Only polyharmonic is defined with integer exponent k. See rbfs-list for all names.

    rbf-obj returns SMILE library object for defined function.

    Categories

    Code snippets

    Save graph

    (defn save-graph
    diff --git a/docs/fastmath.stats.html b/docs/fastmath.stats.html
    index 486e6094..8f6e1e03 100755
    --- a/docs/fastmath.stats.html
    +++ b/docs/fastmath.stats.html
    @@ -1,6 +1,6 @@
     
    -fastmath.stats documentation

    fastmath.stats

    Statistics functions.

    +fastmath.stats documentation

    fastmath.stats

    Statistics functions.

    • Descriptive statistics for sequence.
    • Correlation / covariance of two sequences.
    • @@ -47,7 +47,7 @@

      Other

      Normalize samples to have mean=0 and standard deviation = 1 with standardize.

      -

      histogram to count samples in evenly spaced ranges.

    adjacent-values

    (adjacent-values vs)(adjacent-values vs estimation-strategy)(adjacent-values vs q1 q3)

    adjacent-values

    (adjacent-values vs)(adjacent-values vs estimation-strategy)(adjacent-values vs q1 q3)

    Lower and upper adjacent values (LAV and UAV).

    Let Q1 is 25-percentile and Q3 is 75-percentile. IQR is (- Q3 Q1).

    • LAV is smallest value which is greater or equal to the LIF = (- Q1 (* 1.5 IQR)).
    • @@ -68,13 +68,13 @@

      Other

    • :step - distance between bins
    • :bins - list of pairs of range lower value and number of hits

    Examples

    3 bins from uniform distribution.

    (histogram (repeatedly 1000 rand) 3)
    -;;=> {:bins ([9.993947203562614E-4 330]
    -;;=>         [0.33322076626006886 323]
    -;;=>         [0.6654421377997815 347]),
    +;;=> {:bins ([8.212692395922483E-4 303]
    +;;=>         [0.3331197753070451 362]
    +;;=>         [0.665418281374498 335]),
     ;;=>  :size 3,
    -;;=>  :step 0.3322213715397126}

    3 bins from uniform distribution for given range.

    (histogram (repeatedly 10000 rand) 3 [0.1 0.5])
    +;;=>  :step 0.33229850606745287}

    3 bins from uniform distribution for given range.

    (histogram (repeatedly 10000 rand) 3 [0.1 0.5])
     ;;=> {:bins
    -;;=>  ([0.1 1315] [0.23333333333333334 1370] [0.3666666666666667 1325]),
    +;;=>  ([0.1 1331] [0.23333333333333334 1301] [0.3666666666666667 1323]),
     ;;=>  :size 3,
     ;;=>  :step 0.13333333333333333}

    5 bins from normal distribution.

    (histogram (repeatedly 10000 r/grand) 5)
     ;;=> {:bins ([-3.8255442971705595 104]
    @@ -85,15 +85,14 @@ 

    Other

    ;;=> :size 5, ;;=> :step 1.506064781845698}

    jensen-shannon-divergence

    (jensen-shannon-divergence vs1 vs2)

    Jensen-Shannon divergence of two sequences.

    Examples

    Jensen-Shannon divergence

    (jensen-shannon-divergence (repeatedly 100 (fn* [] (r/irand 100)))
                                (repeatedly 100 (fn* [] (r/irand 100))))
    -;;=> 498.62287946302877

    k-means

    (k-means k vs)

    k-means clustering

    Examples

    Reduce to 4 values.

    (k-means 4 [1 2 3 -1 -1 2 -1 11 111])
    -;;=> (-1.0 2.0 11.0 111.0)

    kendall-correlation

    (kendall-correlation vs1 vs2)

    Kendall’s correlation of two sequences.

    Examples

    Kendall’s correlation of uniform and gaussian distribution samples.

    (kendall-correlation (repeatedly 100000 (partial r/grand 1.0 10.0))
    +;;=> 498.62287946302877

    kendall-correlation

    (kendall-correlation vs1 vs2)

    Kendall’s correlation of two sequences.

    Examples

    Kendall’s correlation of uniform and gaussian distribution samples.

    (kendall-correlation (repeatedly 100000 (partial r/grand 1.0 10.0))
                          (repeatedly 100000 (partial r/drand -10.0 -5.0)))
     ;;=> 8.147273472734727E-4

    kullback-leibler-divergence

    (kullback-leibler-divergence vs1 vs2)

    Kullback-Leibler divergence of two sequences.

    Examples

    Kullback-Leibler divergence.

    (kullback-leibler-divergence (repeatedly 100 (fn* [] (r/irand 100)))
                                  (repeatedly 100 (fn* [] (r/irand 100))))
     ;;=> 2025.8557601936284

    kurtosis

    (kurtosis vs)

    Calculate kurtosis from sequence.

    Examples

    Kurtosis

    (kurtosis [1 2 3 -1 -1 2 -1 11 111])
     ;;=> 8.732515263272099

    maximum

    (maximum vs)

    Maximum value from sequence.

    Examples

    Maximum value

    (maximum [1 2 3 -1 -1 2 -1 11 111])
    -;;=> 111.0

    mean

    (mean vs)

    Calculate mean of a list

    Examples

    Mean (average value)

    (mean [1 2 3 -1 -1 2 -1 11 111])
    -;;=> 14.111111111111109

    median

    (median vs)

    Calculate median of a list. See median-3.

    Examples

    Median (percentile 50%).

    (median [1 2 3 -1 -1 2 -1 11 111])
    +;;=> 111.0

    mean

    (mean vs)

    Calculate mean of vs

    Examples

    Mean (average value)

    (mean [1 2 3 -1 -1 2 -1 11 111])
    +;;=> 14.111111111111109

    median

    (median vs)

    Calculate median of vs. See median-3.

    Examples

    Median (percentile 50%).

    (median [1 2 3 -1 -1 2 -1 11 111])
     ;;=> 2.0

    For three elements use faster median-3.

    (median [7 1 4])
     ;;=> 4.0

    median-3

    (median-3 a b c)

    Median of three values. See median.

    Examples

    Median of [7 1 4]

    (median-3 7 1 4)
     ;;=> 4.0

    median-absolute-deviation

    (median-absolute-deviation vs)

    Calculate MAD

    Examples

    MAD

    (median-absolute-deviation [1 2 3 -1 -1 2 -1 11 111])
    @@ -151,9 +150,9 @@ 

    Other

    (percentile [1 2 3 -1 -1 2 -1 11 111] 85.0 :r8) ;;=> 37.66666666666675 (percentile [1 2 3 -1 -1 2 -1 11 111] 85.0 :r9) -;;=> 34.75000000000007

    population-stddev

    (population-stddev vs)(population-stddev vs u)

    Calculate population standard deviation of a list.

    +;;=> 34.75000000000007

    population-stddev

    (population-stddev vs)(population-stddev vs u)

    Calculate population standard deviation of vs.

    See stddev.

    Examples

    Population standard deviation.

    (population-stddev [1 2 3 -1 -1 2 -1 11 111])
    -;;=> 34.4333315406403

    population-variance

    (population-variance vs)(population-variance vs u)

    Calculate population variance of a list.

    +;;=> 34.4333315406403

    population-variance

    (population-variance vs)(population-variance vs u)

    Calculate population variance of vs.

    See variance.

    Examples

    Population variance

    (population-variance [1 2 3 -1 -1 2 -1 11 111])
     ;;=> 1185.6543209876543

    quantile

    (quantile vs p)(quantile vs p estimation-strategy)

    Calculate quantile of a vs.

    Percentile p is from range 0.0-1.0.

    @@ -196,7 +195,7 @@

    Other

    ;;=> -0.33161081278713267 ;;=> -0.4137529407252298 ;;=> -0.08518442897284138 -;;=> 2.652886502297062)

    stats-map

    (stats-map vs)(stats-map vs estimation-strategy)

    Calculate several statistics from the list and return as map.

    +;;=> 2.652886502297062)

    stats-map

    (stats-map vs)(stats-map vs estimation-strategy)

    Calculate several statistics of vs and return as map.

    Optional estimation-strategy argument can be set to change quantile calculations estimation type. See estimation-strategies.

    Examples

    Stats

    (stats-map [1 2 3 -1 -1 2 -1 11 111])
     ;;=> {:IQR 8.0,
     ;;=>  :Kurtosis 8.732515263272099,
    @@ -220,9 +219,9 @@ 

    Other

    ;;=> :Total 127.0, ;;=> :UAV 11.0, ;;=> :UIF 19.0, -;;=> :UOF 31.0}

    stddev

    (stddev vs)(stddev vs u)

    Calculate population standard deviation of a list.

    +;;=> :UOF 31.0}

    stddev

    (stddev vs)(stddev vs u)

    Calculate standard deviation of vs.

    See population-stddev.

    Examples

    Standard deviation.

    (stddev [1 2 3 -1 -1 2 -1 11 111])
     ;;=> 36.522063346847084

    sum

    (sum vs)

    Sum of all vs values.

    Examples

    Sum

    (sum [1 2 3 -1 -1 2 -1 11 111])
    -;;=> 127.0

    variance

    (variance vs)(variance vs u)

    Calculate variance of a list.

    +;;=> 127.0

    variance

    (variance vs)(variance vs u)

    Calculate variance of vs.

    See population-variance.

    Examples

    Variance.

    (variance [1 2 3 -1 -1 2 -1 11 111])
     ;;=> 1333.861111111111
    \ No newline at end of file diff --git a/docs/fastmath.transform.html b/docs/fastmath.transform.html index 838c7396..3fa43975 100755 --- a/docs/fastmath.transform.html +++ b/docs/fastmath.transform.html @@ -1,13 +1,13 @@ -fastmath.transform documentation

    fastmath.transform

    Transforms.

    +fastmath.transform documentation

    fastmath.transform

    Transforms.

    See transformer and TransformProto for details.

    -

    Wavelet

    +

    Wavelet

    Based on JWave library.

    Be aware that some of the wavelet types doesn’t work properly. :battle-23, :cdf-53, :cdf-97.

    -

    Cos/Sin/Hadamard

    +

    Cos/Sin/Hadamard

    Orthogonal or standard fast sine/cosine/hadamard 1d transforms.

    -

    Fourier

    +

    Fourier

    DFT.

    compress

    (compress mag xs)

    Compress transformed signal xs with given magnitude mag.

    Examples

    Compress 1d

    (let [t (transformer :fast :symlet-5)]
       (->> [1 2 3 4]
            (forward-1d t)
    @@ -47,7 +47,7 @@ 

    Fourier

    • :standard :dft - 1d Discrete Fourier Transform - returns double-array where even elements are real part, odd elements are imaginary part.

    Examples

    Usage

    (transformer :packet :discrete-mayer)
    -;;=> jwave.transforms.WaveletPacketTransform@1b4f6b6d

    TransformProto

    protocol

    Transformer functions.

    members

    forward-1d

    (forward-1d t xs)

    Forward transform of sequence or array. Returns double array.

    Examples

    Usage

    (seq (forward-1d (transformer :packet :haar-orthogonal) [-1 8 7 6]))
    +;;=> jwave.transforms.WaveletPacketTransform@33b5af2b

    TransformProto

    protocol

    Transformer functions.

    members

    forward-1d

    (forward-1d t xs)

    Forward transform of sequence or array. Returns double array.

    Examples

    Usage

    (seq (forward-1d (transformer :packet :haar-orthogonal) [-1 8 7 6]))
     ;;=> (20.0 -6.0 -8.0 -10.0)
     (seq (forward-1d (transformer :fast :haar-orthogonal) [-1 8 7 6]))
     ;;=> (20.0 -6.0 -9.0 1.0)
    diff --git a/docs/fastmath.vector.html b/docs/fastmath.vector.html
    index ff24e19e..2fba6d5a 100755
    --- a/docs/fastmath.vector.html
    +++ b/docs/fastmath.vector.html
    @@ -1,6 +1,6 @@
     
    -fastmath.vector documentation

    array->vec2

    (array->vec2 a)

    Doubles array to Vec2

    Examples

    Usage

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

    array->vec2

    (array->vec2 a)

    Doubles array to Vec2

    Examples

    Usage

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

    array->vec3

    (array->vec3 a)

    Doubles array to Vec3

    Examples

    Usage

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

    array->vec4

    (array->vec4 a)

    Doubles array to Vec4

    Examples

    Usage

    (array->vec4 (double-array [11 22 33 44 55]))
     ;;=> #vec4 [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])
    @@ -80,13 +80,13 @@ 

    Types

    ;;=> #vec2 [1.0, 1.0]

    generate-vec2

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

    Generate Vec2 with fn(s)

    Examples

    Usage

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

    generate-vec3

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

    Generate Vec3 with fn(s)

    Examples

    Usage

    (generate-vec3 rand)
    -;;=> #vec3 [0.4691580762211698, 0.252494638791968, 0.4653813945662143]
    +;;=> #vec2 [0.5700176814860464, 1.0]

    generate-vec3

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

    Generate Vec3 with fn(s)

    Examples

    Usage

    (generate-vec3 rand)
    +;;=> #vec3 [0.9523171468596539, 0.5970067567859844, 0.6220458820427102]
     (generate-vec3 rand (constantly 1) (constantly 2))
    -;;=> #vec3 [0.010902894119597217, 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)
    -;;=> #vec4 [0.7046959186663563, 0.06544269007247161, 0.7800565032840674, 0.34281544310127676]
    +;;=> #vec3 [0.16411800556131229, 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)
    +;;=> #vec4 [0.8916846880009579, 0.9723287704524494, 0.9934444288783233, 0.9625900856968701]
     (generate-vec4 rand rand (constantly 1) (constantly 2))
    -;;=> #vec4 [0.4745994985798181, 0.159660905804342, 1.0, 2.0]

    limit

    (limit v len)

    Limit length of the vector by given value

    Examples

    Usage

    (limit (vec3 1.0 1.0 1.0) 1.0)
    +;;=> #vec4 [0.3739156932075838, 0.45244106878901014, 1.0, 2.0]

    limit

    (limit v len)

    Limit length of the vector by given value

    Examples

    Usage

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

    normalize

    (normalize v)

    Normalize vector (set length = 1.0)

    Examples

    Usage

    (normalize (vec2 1.0 -1.0))
    @@ -96,13 +96,13 @@ 

    Types

    ;;=> 90.0 (m/degrees (relative-angle-between (vec (repeatedly 50 rand)) (vec (repeatedly 50 rand)))) -;;=> -0.09850367659286136

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

    set-mag

    (set-mag v len)

    Set length of the vector

    Examples

    Usage

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

    TOLERANCE

    const

    ;;=> 1.0E-6

    Tolerance used in is-near-zero?. Values less than this value are treated as zero.

    vec2

    (vec2 x y)

    Make 2d vector

    Examples

    Usage

    (vec2 0.5 -0.5)
     ;;=> #vec2 [0.5, -0.5]

    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__31632#] (* 2.0 p1__31632#)) (vec2 1 2))
    +;;=> [11.0 22.0]

    As sequence

    (map (fn* [p1__32505#] (* 2.0 p1__32505#)) (vec2 1 2))
     ;;=> (2.0 4.0)
     (reduce clojure.core/+ (vec2 6 4))
     ;;=> 10.0
    @@ -191,12 +191,12 @@ 

    Types

    ;; Test: ok.

    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)])
    -;;=> [39 98]
    +;;=> [74 98]
     (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)])
    -;;=> [13 -99]
    +;;=> [6 -96]
     (mindim (vec3 1 2 3))
     ;;=> 0

    mn

    (mn v1)

    Minimum value of vector elements

    Examples

    Usage

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

    mult

    (mult v1 v)

    Multiply vector by number v.

    Examples

    Usage

    (mult (vec4 5 4 3 5) 4.0)
    diff --git a/docs/images/i/1f8ee3a5402be0b14ee9916aecd6074b.png b/docs/images/i/1f8ee3a5402be0b14ee9916aecd6074b.png
    index cb50fdea..7fd54b17 100755
    Binary files a/docs/images/i/1f8ee3a5402be0b14ee9916aecd6074b.png and b/docs/images/i/1f8ee3a5402be0b14ee9916aecd6074b.png differ
    diff --git a/docs/index.html b/docs/index.html
    index cc4acf5a..44eab397 100755
    --- a/docs/index.html
    +++ b/docs/index.html
    @@ -1,3 +1,3 @@
     
    -Fastmath 1.0.3

    Fastmath 1.0.3

    Released under the Unlicence

    Fast and primitive math library.

    Installation

    To install, add the following dependency to your project or build file:

    [generateme/fastmath "1.0.3"]

    Namespaces

    fastmath.complex

    Complex numbers functions.

    Categories

    Other vars: abs add arg complex conjugate div mult neg reciprocal sq sqrt sqrt1z sub

    fastmath.rbf

    Radial Basis Function

    Categories

    \ No newline at end of file +Fastmath 1.1.0

    Fastmath 1.1.0

    Released under the Unlicence

    Fast and primitive math library.

    Installation

    To install, add the following dependency to your project or build file:

    [generateme/fastmath "1.1.0"]

    Namespaces

    fastmath.complex

    Complex numbers functions.

    Categories

    Other vars: abs add arg complex conjugate div mult neg reciprocal sq sqrt sqrt1z sub

    fastmath.rbf

    Radial Basis Function

    Categories

    \ No newline at end of file diff --git a/example/fastmath/clustering_example.clj b/example/fastmath/clustering_example.clj new file mode 100644 index 00000000..e5f70680 --- /dev/null +++ b/example/fastmath/clustering_example.clj @@ -0,0 +1,59 @@ +(ns fastmath.clustering-example + (:require [fastmath.clustering :refer :all] + [metadoc.examples :refer :all] + [fastmath.random :as r])) + +(add-examples clustering-methods-list + (example "List of methods" clustering-methods-list)) + +(add-examples distances-list + (example "List of distances" distances-list)) + +(add-examples k-means + (example (k-means [1 2 3 -1 -1 2 -1 11 111] 4)) + (example "Clusters group into separate maps." (regroup (k-means [1 2 3 -1 -1 2 -1 11 111] 4))) + (example "Use as predictor" (let [cl (k-means [1 2 3 -1 -1 2 -1 11 111] 4)] + [(cl -1) (cl 10) (cl 100) (cl 1) (cl -1000) (cl 1000)]))) + +(add-examples g-means + (example "Expect 2 clusters, uniform distribution." + ((juxt :clusters :sizes :representatives) (g-means (repeatedly 100 #(r/randval (r/drand) (r/drand 5 6))) 4)))) + +(add-examples x-means + (example "Expect 2 clusters, gaussian distribution." + ((juxt :clusters :sizes :representatives) (x-means (repeatedly 10000 #(r/randval (r/grand) (r/grand 5 1.0))) 4)))) + +(add-examples deterministic-annealing + (example (map (fn [m] (dissoc m :data)) + (-> (repeatedly 1000 #(vector (r/randval (r/grand) (r/grand 5 1.0)) + (r/randval (r/grand) (r/grand 5 1.0)))) + (deterministic-annealing 4 0.5) + (regroup))))) + +(add-examples neural-gas + (example (neural-gas [1 2 3 -1 -1 2 -1 11 111] 4)) + (example "Clusters group into separate maps." (regroup (neural-gas [1 2 3 -1 -1 2 -1 11 111] 4)))) + +(add-examples denclue + (example-session "Expect 2 clusters, uniform distribution." + ((juxt :clusters :sizes :representatives) (denclue (repeatedly 100 #(r/randval (r/drand) (r/drand 5 6))) 1 10)) + (map (fn [m] (dissoc m :data)) (regroup (denclue (repeatedly 1000 #(r/randval 0.1 (r/drand) (r/drand 5 6))) 1 10))))) + +(add-examples clarans + (example (dissoc (clarans (repeatedly 1000 #(r/randval 0.1 (r/irand -10 10) (r/irand 100 150))) :chebyshev 2) :data :clustering :obj :predict))) + +(add-examples dbscan + (example "3d vectors" (dissoc (dbscan (repeatedly 5000 #(vector (r/randval 0.1 (r/irand -10 10) (r/irand 100 150)) + (r/randval (r/irand -10 10) (r/irand 100 150)) + (r/randval (r/irand -10 10) (r/irand 100 150)))) 10 20) :data :clustering :obj :predict))) + +(add-examples mec + (example "2d vectors" (dissoc (mec (repeatedly 5000 #(vector (r/randval 0.1 (r/irand -10 10) (r/irand 100 150)) + (r/randval (r/irand -10 10) (r/irand 100 150)))) :manhattan 8 20) :data :clustering :obj :predict))) + +(add-examples regroup + (example-session "Result of clustering with regrouping" + (k-means [1 2 3 -1 -1 2 -1 11 111] 7) + (regroup (k-means [1 2 3 -1 -1 2 -1 11 111] 7)) + (count (regroup (k-means [1 2 3 -1 -1 2 -1 11 111] 7))))) + diff --git a/example/fastmath/core_examples.clj b/example/fastmath/core_examples.clj index 523ee333..a6276f02 100644 --- a/example/fastmath/core_examples.clj +++ b/example/fastmath/core_examples.clj @@ -194,7 +194,8 @@ (example-session "Convert" (seq->double-array [1 2 3]) (seq (seq->double-array [1 2 3])) - (double-array->seq (seq->double-array [1 2 3])))) + (double-array->seq (seq->double-array [1 2 3]))) + (example "Also works on number (treated as one element list)." (seq (seq->double-array 1)))) (add-examples double-double-array->seq (example "Convert" (double-double-array->seq (seq->double-double-array [[4 3 2] (double-array [1 2 3])])))) @@ -202,8 +203,8 @@ (add-examples seq->double-double-array (example-session "Convert" (seq->double-double-array [[1 2] [3 4]]) - (double-double-array->seq (seq->double-double-array [[1 2] [3 4]])))) - + (double-double-array->seq (seq->double-double-array [[1 2] [3 4]]))) + (example "Also works on seq of numbers" (seq (second (seq->double-double-array [1 2 3]))))) (def single-list `(sin cos tan cot sec csc asin acos atan acot asec acsc sinh cosh tanh coth sech csch asinh acosh atanh acoth asech acsch diff --git a/example/fastmath/stats_examples.clj b/example/fastmath/stats_examples.clj index b1c60c31..1c8f55cd 100644 --- a/example/fastmath/stats_examples.clj +++ b/example/fastmath/stats_examples.clj @@ -90,8 +90,6 @@ (add-examples kullback-leibler-divergence (example "Kullback-Leibler divergence." (kullback-leibler-divergence (repeatedly 100 #(r/irand 100)) (repeatedly 100 #(r/irand 100))))) (add-examples jensen-shannon-divergence (example "Jensen-Shannon divergence" (jensen-shannon-divergence (repeatedly 100 #(r/irand 100)) (repeatedly 100 #(r/irand 100))))) -(add-examples k-means (example "Reduce to 4 values." (k-means 4 [1 2 3 -1 -1 2 -1 11 111]))) - (add-examples histogram (example "3 bins from uniform distribution." (histogram (repeatedly 1000 rand) 3)) (example "3 bins from uniform distribution for given range." (histogram (repeatedly 10000 rand) 3 [0.1 0.5])) diff --git a/project.clj b/project.clj index e96f0c70..b9025565 100755 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject generateme/fastmath "1.0.3" +(defproject generateme/fastmath "1.1.0" :description "Fast and primitive math library" :url "https://github.com/generateme/fastmath" :license {:name "The Unlicence" @@ -7,7 +7,7 @@ [net.jafama/jafama "2.3.1"] [org.apache.commons/commons-math3 "3.6.1"] [com.github.haifengl/smile-interpolation "1.5.1"] - [com.github.haifengl/smile-math "1.5.1"] + [com.github.haifengl/smile-core "1.5.1"] [org.slf4j/slf4j-simple "1.7.25"] [de.sciss/jwave "1.0.3"]] :resource-path "resources/" @@ -22,7 +22,7 @@ [lein-codox "0.10.3"]] :source-paths ["example"] :dependencies [[codox-theme-rdash "0.1.2"] - [clojure2d "1.0.0-RC2"] + [clojure2d "1.0.0"] [criterium "0.4.4"] [incanter/incanter-charts "1.9.2"] [incanter/incanter-core "1.9.2"] diff --git a/src/fastmath/clustering.clj b/src/fastmath/clustering.clj new file mode 100644 index 00000000..05411dca --- /dev/null +++ b/src/fastmath/clustering.clj @@ -0,0 +1,287 @@ +(ns fastmath.clustering + "Clustering algorithms. + + Various clustering algrorithms backed by SMILE library. + + Currently implemented: only partition clustering. + + ### Input data + + It's always sequence of n-sized samples as sequences. + + For example, 2d samples `[[1 2] [2 2] [3 3] ...]` + + For 1d data you can pass sequence of numbers of sequence of 1d seqs of numbers + + ```clojure + [1 2 3] + ;; or + [[1] [2] [3]] + ``` + + ### Distances + + Some of the methods use distance functions, currently supported are: + + * `:euclidean` + * `:manhattan` + * `:chebyshev` + + ### Output + + Every function returns record which contains: + + * `:type` - name of the method used + * `:data` - input data + * `:clustering` - sequence of cluster ids + * `:sizes` - sizes of clusters + * `:clusters` - number of clusters + * `:predict` - predicting function (see below), qualify additional sample + * `:representatives` - list of centroids or medoids if available + * `:info` - additional statistics for your samples (like distortion) + * `:obj` - SMILE object + + Cluster id is a integer ranging from 0 to the number of clusters minus 1. Some methods mark outliers with [[outlier-id]]. + + Record acts as function and can qualify additonal sample by calling `:predict` function, for example (`data` is sequence of 3d samples): + + ```clojure + (let [cl (k-means data 10)] (cl [0 1 2])) + ``` + + See [[k-means]] + + #### Regrouping + + Clustering record can be regroupped to the list of individual clusters. Call [[regroup]] and get list of maps with following structure: + + * `:key` - cluster id + * `:data` - samples which belong to the cluster + * `:outliers?` - does it contain outliers or not + * `:representative` - centroid/medoid or average vector if the former is not available + * `:size` - size of cluster" + (:require [fastmath.core :as m] + [fastmath.vector :as v] + [clojure.string :as s] + [fastmath.stats :as stat]) + (:import [smile.clustering Clustering KMeans GMeans XMeans DeterministicAnnealing DENCLUE CLARANS DBScan MEC] + [smile.math.distance ChebyshevDistance EuclideanDistance ManhattanDistance CorrelationDistance JensenShannonDistance] + [smile.vq NeuralGas] + [clojure.lang IFn])) + +(def ^:const ^{:doc "Id of the cluster which contain outliers."} outlier-id Clustering/OUTLIER) + +(defrecord ClusteringResult [type data clustering sizes clusters predict representatives info obj] + IFn + (invoke [_ in] (predict in))) + +(defn- structurize + "Pack result of the clustering function into the structure" + [type data in repr info] + (->ClusteringResult type + data + (seq (.getClusterLabel in)) + (seq (.getClusterSize in)) + (.getNumClusters in) + (fn [x] (.predict in (m/seq->double-array x))) + (when repr (m/double-double-array->seq (repr in))) + (into {} (map (fn [[k v]] [k (v in)]) info)) + in)) + +(def ^:private clustering-classes {:k-means ['KMeans 'centroids 'distortion] + :g-means ['GMeans 'centroids 'distortion] + :x-means ['XMeans 'centroids 'distortion] + :deterministic-annealing ['DeterministicAnnealing 'centroids 'distortion 'getAlpha] + :neural-gas ['NeuralGas 'centroids 'distortion] + :denclue ['DENCLUE nil 'getSigma] + :clarans ['CLARANS 'medoids 'distortion 'getMaxNeighbor 'getNumLocalMinima] + :dbscan ['DBScan nil 'getMinPts 'getRadius] + :mec ['MEC nil 'entropy]}) + +(def ^{:doc "List of clustering methods."} clustering-methods-list (keys clustering-classes)) + +(def ^:private distances {:chebyshev (ChebyshevDistance.) + :euclidean (EuclideanDistance.) + :manhattan (ManhattanDistance.)}) + +(def ^{:doc "List of distances used in some clustring methods."} distances-list (keys distances)) + +(defn- symbol->keyword + "Convert java method into the keyword." + [s] + (let [s (name s)] + (-> (if (= "get" (subs s 0 3)) (subs s 3) s) + (s/lower-case) + (keyword)))) + +(defmacro ^:private clustering + "Analyze clustering method and pack into the structure." + [clustering-method data & params] + (let [[nm repr & rest] (clustering-classes clustering-method) + repr (when repr `(fn [obj#] (. obj# ~repr))) + info (mapv #(vector (symbol->keyword %) `(fn [obj#] (. obj# ~%))) rest)] + `(structurize ~clustering-method ~data (new ~nm (m/seq->double-double-array ~data) ~@params) ~repr ~info))) + +(defn k-means + "K-Means++ algorithm. + + Input: + + * data - sequence of samples + * clusters - number of clusters + * max-iter (optional) - maximum number of iterations + * runs (optional) - maximum number of runs + + See more in [SMILE doc](https://haifengl.github.io/smile/api/java/smile/clustering/KMeans.html)" + ([data clusters] (clustering :k-means data clusters)) + ([data clusters max-iter] (clustering :k-means data clusters max-iter)) + ([data clusters max-iter runs] (clustering :k-means data clusters max-iter runs))) + +(defn g-means + "G-Means algorithm. + + Input: + + * data - sequence of samples + * max-clusters - maximum number of clusters + + See more in [SMILE doc](https://haifengl.github.io/smile/api/java/smile/clustering/GMeans.html)" + [data max-clusters] (clustering :g-means data max-clusters)) + +(defn x-means + "X-Means algorithm. + + Input: + + * data - sequence of samples + * max-clusters - number of clusters + + See more in [SMILE doc](https://haifengl.github.io/smile/api/java/smile/clustering/XMeans.html)" + [data max-clusters] (clustering :x-means data max-clusters)) + +(defn deterministic-annealing + "Deterministic Annealing algorithm. + + Input: + + * data - sequence of samples + * max-clusters - number of clusters + * alpha (optional) - temperature decreasing factor (valued from 0 to 1) + + See more in [SMILE doc](https://haifengl.github.io/smile/api/java/smile/clustering/DeterministicAnnealing.html)" + ([data max-clusters] (clustering :deterministic-annealing data max-clusters)) + ([data max-clusters alpha] (clustering :deterministic-annealing data max-clusters alpha))) + +(defn neural-gas + "Neural Gas algorithm. + + Input: + + * data - sequence of samples + * clusters - number of clusters + + Optional: + + * lambda-i - intial lambda value (soft learning radius/rate) + * lambda-f - final lambda value + * eps-i - initial epsilon value (learning rate) + * eps-f - final epsilon value + * steps - number of iterations + + See more in [SMILE doc](https://haifengl.github.io/smile/api/java/smile/vq/NeuralGas.html)" + ([data clusters] (clustering :neural-gas data clusters)) + ([data clusters lambda-i lambda-f eps-i eps-f steps] (clustering :neural-gas data clusters lambda-i lambda-f eps-i eps-f steps))) + +(defn denclue + "DENsity CLUstering algorithm. + + Input: + + * data - sequence of samples + * sigma - gaussian kernel parameter + * m - number of selected samples, much slower than number of all samples + + See more in [SMILE doc](https://haifengl.github.io/smile/api/java/smile/clustering/DENCLUE.html)" + [data sigma m] (clustering :denclue data sigma m)) + +(defn clarans + "Clustering Large Applications based upon RANdomized Search algorithm. + + Input: + + * data - sequence of samples + * clusters - numbe of clusters + + Optional: + + * dist - distance method, default `:euclidean` + * max-neighbor - maximum number of neighbors checked during random search + * num-local - the number of local minima to search for + + See more in [SMILE doc](https://haifengl.github.io/smile/api/java/smile/clustering/CLARANS.html)" + ([data clusters] (clarans data :euclidean clusters)) + ([data dist clusters] (clustering :clarans data (distances dist) clusters)) + ([data dist clusters max-neighbor] (clustering :clarans data (distances dist) clusters max-neighbor)) + ([data dist clusters max-neighbor num-local] (clustering :clarans data (distances dist) clusters max-neighbor num-local))) + +(defn dbscan + "Density-Based Spatial Clustering of Applications with Noise algorithm. + + Input: + + * data - sequence of samples + * dist (optional) - distance method, default `:euclidean` + * min-pts - minimum number of neighbors + * radius - the neighborhood radius + + See more in [SMILE doc](https://haifengl.github.io/smile/api/java/smile/clustering/DBScan.html)" + ([data min-pts radius] (dbscan data :euclidean min-pts radius)) + ([data dist min-pts ^double radius] (clustering :dbscan data (distances dist) (int min-pts) radius))) + +(defn mec + "Nonparametric Minimum Conditional Entropy Clustering algorithm. + + Input: + + * data - sequence of samples + * dist (optional) - distance method, default `:euclidean` + * max-clusters - maximum number of clusters + * radius - the neighborhood radius + + See more in [SMILE doc](https://haifengl.github.io/smile/api/java/smile/clustering/MEC.html)" + ([data max-clusters radius] (mec data :euclidean max-clusters radius)) + ([data dist max-clusters ^double radius] (clustering :mec data (distances dist) max-clusters radius))) + +(defn regroup + "Transform clusterig result into list of clusters as separate maps. + + Every map contain: + + * `:key` - cluster id + * `:data` - samples which belong to the cluster + * `:outliers?` - does it contain outliers or not + * `:representative` - centroid/medoid or average vector if the former is not available + * `:size` - size of cluster + + Representative is always a n-dimensional sequence even if input is a list of numbers. + + Empty clusters are skipped." + [clustered-data] + (let [mvector? (satisfies? v/VectorProto (first (:data clustered-data))) ;;required to fix missing representative + mseqable? (seqable? (first (:data clustered-data)))] + (for [[k lst] (group-by first (map vector (:clustering clustered-data) (:data clustered-data))) + :let [d (map second lst) + outliers? (== k outlier-id) + r (:representatives clustered-data)]] + {:key k + :outliers? outliers? + :data d + :representative (if (and r (not outliers?)) + (nth r k) + (cond ;; if representative is missing, calculate average + mvector? (v/average-vectors d) + mseqable? (v/average-vectors (map vec d)) + :else (list (stat/mean d)))) + :size (if outliers? + (count d) + (nth (:sizes clustered-data) k))}))) diff --git a/src/fastmath/core.clj b/src/fastmath/core.clj index 42784047..3ca1138c 100755 --- a/src/fastmath/core.clj +++ b/src/fastmath/core.clj @@ -11,7 +11,7 @@ (ns fastmath.core "Collection of fast math functions and plethora of constants known from other math libraries. - #### Primitive math operators + ### 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: @@ -31,11 +31,11 @@ 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 + ### 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 + ### Other functions Additionally namespace contains functions which are common in frameworks like OpenFrameworks and Processing. @@ -746,9 +746,11 @@ where n is the mathematical integer closest to dividend/divisor. Returned value ^doubles [vs] (if (= (type vs) double-array-type) vs - (double-array vs))) - -(declare seq->double-double-array) + (if (seqable? vs) + (double-array vs) + (let [arr (double-array 1)] ;; not a sequence? maybe number... + (aset arr 0 (double vs)) + arr)))) (defn double-double-array->seq "Convert double array of double arrays into sequence of sequences." diff --git a/src/fastmath/fields.clj b/src/fastmath/fields.clj index 222d577e..7addeba0 100644 --- a/src/fastmath/fields.clj +++ b/src/fastmath/fields.clj @@ -5,7 +5,7 @@ Names are taken from fractal flames world where such fields are call `variations`. Most implementations are taken from [JWildfire](http://jwildfire.org/) software. - ## Creation + ### Creation To create vector field call [[field]] multimethod with name of the field as keyword. @@ -13,7 +13,7 @@ Additionally you can provide `amount` parameter which is scaling factor for vector field (default: `1.0`). - ## Derived fields + ### Derived fields You can use several method to derive new vector field from the other one(s). Possible options are: @@ -23,7 +23,7 @@ * [[composition]] - composition of the fields * [[angles]] - angles of the field vectors - ## Scalar fields + ### Scalar fields You can derive scalar fields from given vector field(s): @@ -33,7 +33,7 @@ * [[dot]] - dot product * [[angle-between]] - angle between vectors from fields. - ## Combinations + ### Combinations The other option is to create vector field using some of the above possibilities. Combination is a tree of field operations with parametrizations. Functions: diff --git a/src/fastmath/random.clj b/src/fastmath/random.clj index 2816b8bf..42a9be79 100644 --- a/src/fastmath/random.clj +++ b/src/fastmath/random.clj @@ -3,7 +3,7 @@ Namespace defines various random number generators (RNGs), different types of random functions, sequence generators and noise functions. - #### RNGs + ### RNGs You can use a selection of various RNGs defined in [Apache Commons Math](http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/random/package-summary.html) library. @@ -34,7 +34,7 @@ Check individual function for parameters description. - #### Random Vector Sequences + ### Random Vector Sequences Couple of functions to generate sequences of numbers or vectors. You can generate sequence of `double`, [[Vec2]], [[Vec3]] or [[Vec4]] types. Just pass the size to creator function. @@ -50,7 +50,7 @@ After creation you get function equivalent to `repeatedly`. - #### Noise + ### Noise List of continuous noise functions (1d, 2d and 3d): @@ -83,11 +83,11 @@ * [[vnoise]] - Value Noise (as in Processing, 6 octaves, hermite interpolation) * [[simplex]] - Simpled Noise (6 octaves) - ##### Discrete Noise + #### Discrete Noise [[discrete-noise]] is a 1d or 2d hash function for given integers. Returns double from `[0,1]` range. - #### Distribution + ### Distribution Various real and integer distributions. See [[DistributionProto]] and [[RNGProto]] for functions. diff --git a/src/fastmath/stats.clj b/src/fastmath/stats.clj index ad4fa7b8..15968982 100644 --- a/src/fastmath/stats.clj +++ b/src/fastmath/stats.clj @@ -131,7 +131,7 @@ (percentile vs (m/constrain (* p 100.0) 0.0 100.0) estimation-strategy))) (defn median - "Calculate median of a list. See [[median-3]]." + "Calculate median of `vs`. See [[median-3]]." {:metadoc/categories #{:stat}} ^double [vs] (percentile vs 50.0)) @@ -143,12 +143,12 @@ (m/max (m/min a b) (m/min (m/max a b) c))) (defn mean - "Calculate mean of a list" + "Calculate mean of `vs`" {:metadoc/categories #{:stat}} (^double [vs] (StatUtils/mean (m/seq->double-array vs)))) (defn population-variance - "Calculate population variance of a list. + "Calculate population variance of `vs`. See [[variance]]." {:metadoc/categories #{:stat}} @@ -158,7 +158,7 @@ (StatUtils/populationVariance (m/seq->double-array vs) u))) (defn variance - "Calculate variance of a list. + "Calculate variance of `vs`. See [[population-variance]]." {:metadoc/categories #{:stat}} @@ -168,7 +168,7 @@ (StatUtils/variance (m/seq->double-array vs) u))) (defn population-stddev - "Calculate population standard deviation of a list. + "Calculate population standard deviation of `vs`. See [[stddev]]." {:metadoc/categories #{:stat}} @@ -178,7 +178,7 @@ (m/sqrt (population-variance vs u)))) (defn stddev - "Calculate population standard deviation of a list. + "Calculate standard deviation of `vs`. See [[population-stddev]]." {:metadoc/categories #{:stat}} @@ -304,7 +304,7 @@ (.evaluate k (m/seq->double-array vs)))) (defn stats-map - "Calculate several statistics from the list and return as map. + "Calculate several statistics of `vs` and return as map. Optional `estimation-strategy` argument can be set to change quantile calculations estimation type. See [[estimation-strategies]]." {:metadoc/categories #{:stat}} @@ -423,23 +423,3 @@ {:size bins :step step :bins (map vector search-array buff)}))) - -;; TODO - replace with native (SMILE or Apache Commens) algorithms - -(defn- closest-mean-fn - [means] - (fn [^double v] (reduce (partial min-key #(m/sq (- v ^double %))) means))) - -;; `(k-means 4 '(1 2 3 -1 -1 2 -1 11 111)) => (-1.0 2.0 11.0 111.0)` -(defn k-means - "k-means clustering" - [^long k vs] - (let [vs (map double vs) - svs (set vs)] - (if (> k (count svs)) - (sort svs) - (loop [mns (sort (take k (shuffle svs))) - pmns (repeat k Double/NaN)] - (if (= mns pmns) - mns - (recur (sort (map mean (vals (group-by (closest-mean-fn mns) vs)))) mns)))))) diff --git a/src/fastmath/transform.clj b/src/fastmath/transform.clj index 13fe29bd..e46e4747 100644 --- a/src/fastmath/transform.clj +++ b/src/fastmath/transform.clj @@ -3,17 +3,17 @@ See [[transformer]] and [[TransformProto]] for details. - #### Wavelet + ### Wavelet Based on [JWave](https://github.com/cscheiblich/JWave/) library. Be aware that some of the wavelet types doesn't work properly. `:battle-23`, `:cdf-53`, `:cdf-97`. - #### Cos/Sin/Hadamard + ### Cos/Sin/Hadamard Orthogonal or standard fast sine/cosine/hadamard 1d transforms. - #### Fourier + ### Fourier DFT." {:metadoc/categories {:w "Transform"