Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: thi-ng/math
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: WhimsicalCode/thi.ng.math
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: temporary-fork
Choose a head ref
  • 4 commits
  • 13 files changed
  • 1 contributor

Commits on Dec 1, 2022

  1. Exclude abs from :refer-clojure

    This is now provided in Clojure/ClojureScript so prints a warning.
    danielcompton committed Dec 1, 2022
    Copy the full SHA
    990cac1 View commit details
  2. Add a deps.edn

    danielcompton committed Dec 1, 2022
    Copy the full SHA
    b3f1806 View commit details
  3. Add generated source

    ./tangle.sh src/*.org test/*.org
    danielcompton committed Dec 1, 2022
    Copy the full SHA
    9560cd3 View commit details
  4. Copy the full SHA
    36907f2 View commit details
2 changes: 2 additions & 0 deletions babel/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:paths ["src"]
:deps { thi.ng/typedarrays {:mvn/version "0.1.7"}}}
9 changes: 9 additions & 0 deletions babel/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title> test</title>
</head>
<body>
<script type="text/javascript" src="target/math-0.3.1.js"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions babel/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(defproject thi.ng/math "0.3.1"
:description "CLJ/CLJS math functions, macros & utilities"
:url "http://thi.ng/math"
:license {:name "Apache Software License 2.0"
:url "http://www.apache.org/licenses/LICENSE-2.0"
:distribution :repo}
:scm {:name "git"
:url "git@github.com:thi-ng/math.git"}

:min-lein-vesion "2.4.0"

:dependencies [[org.clojure/clojure "1.11.1"]
[org.clojure/clojurescript "1.11.4"]
[thi.ng/typedarrays "0.1.7"]]

:profiles {:dev {:dependencies [[criterium "0.4.6"]]
:plugins [[lein-cljsbuild "1.1.8"]
[com.cemerick/clojurescript.test "0.3.3"]]
:global-vars {*warn-on-reflection* true}
:jvm-opts ^:replace ["-Dclojure.compiler.direct-linking=true"]
:aliases {"cleantest" ["do" "clean," "test," "cljsbuild" "test"]}}}

:cljsbuild {:builds [{:id "simple"
:source-paths ["src" "test"]
:compiler {:output-to "target/math-0.3.1.js"
:optimizations :whitespace
:pretty-print true}}]
:test-commands {"unit-tests" ["phantomjs" :runner "target/math-0.3.1.js"]}}

:pom-addition [:developers
[:developer
[:name "Karsten Schmidt"]
[:url "https://thi.ng"]
[:timezone "0"]]])
153 changes: 153 additions & 0 deletions babel/src/thi/ng/math/bits.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
(ns thi.ng.math.bits
(:refer-clojure :exclude [abs bit-count]))

(def ^:const INT-BITS 32)
(def ^:const INT-BITS1 (dec INT-BITS))
(def ^:const INT-MAX 0x7fffffff)
(def ^:const INT-MIN (bit-shift-left -1 INT-BITS1))

(def ^:const LOG2 (Math/log 2.0))

(defn abs
[x]
(let [mask (bit-shift-right x INT-BITS1)]
(- (bit-xor x mask) mask)))

(defn log2?
[x] (and (zero? (bit-and x (dec x))) (not (zero? x))))

(defn log2
[x]
(let [[x r] (if (> x 0xffff) [(unsigned-bit-shift-right x 16) 16] [x 0])
[x r] (if (> x 0xff) [(unsigned-bit-shift-right x 8) (bit-or r 8)] [x r])
[x r] (if (> x 0xf) [(unsigned-bit-shift-right x 4) (bit-or r 4)] [x r])
[x r] (if (> x 0x3) [(unsigned-bit-shift-right x 2) (bit-or r 2)] [x r])]
(bit-or r (bit-shift-right x 1))))

(defn log10
[x]
(if (>= x 1000000000)
9 (if (>= x 100000000)
8 (if (>= x 10000000)
7 (if (>= x 1000000)
6 (if (>= x 100000)
5 (if (>= x 10000)
4 (if (>= x 1000)
3 (if (>= x 100)
2 (if (>= x 10)
1 0))))))))))

(defn trailing-zeros
[x]
(let [x (bit-and x (- x))
c (if (pos? x) 31 32)
c (if (pos? (bit-and x 0x0000ffff)) (- c 16) c)
c (if (pos? (bit-and x 0x00ff00ff)) (- c 8) c)
c (if (pos? (bit-and x 0x0f0f0f0f)) (- c 4) c)
c (if (pos? (bit-and x 0x33333333)) (- c 2) c)]
(if (pos? (bit-and x 0x55555555)) (dec c) c)))

(defn ceil-pow2
[x]
(if (zero? x)
1
(let [x (dec x)
x (bit-or x (unsigned-bit-shift-right x 1))
x (bit-or x (unsigned-bit-shift-right x 2))
x (bit-or x (unsigned-bit-shift-right x 4))
x (bit-or x (unsigned-bit-shift-right x 8))
x (bit-or x (unsigned-bit-shift-right x 16))]
(inc x))))

(defn floor-pow2
[x]
(let [x (bit-or x (unsigned-bit-shift-right x 1))
x (bit-or x (unsigned-bit-shift-right x 2))
x (bit-or x (unsigned-bit-shift-right x 4))
x (bit-or x (unsigned-bit-shift-right x 8))
x (bit-or x (unsigned-bit-shift-right x 16))]
(- x (unsigned-bit-shift-right x 1))))

(defn bit-count
[x]
(let [x (- x (bit-and (bit-shift-right x 1) 0x55555555))
x (+ (bit-and x 0x33333333) (bit-and (bit-shift-right x 2) 0x33333333))]
(bit-shift-right
(* (bit-and (+ x (bit-shift-right x 4)) 0xf0f0f0f)
0x1010101) 24)))

(defn parity
[x]
(let [x (bit-xor x (unsigned-bit-shift-right x 16))
x (bit-xor x (unsigned-bit-shift-right x 8))
x (bit-xor x (unsigned-bit-shift-right x 4))]
(bit-and (unsigned-bit-shift-right 0x6996 (bit-and x 0xf)) 1)))

(defn even-parity? [x] (zero? (parity x)))
(defn odd-parity? [x] (pos? (parity x)))

(defn- interleave2*
[x]
(let [x (bit-and x 0xffff)
x (bit-and (bit-or x (bit-shift-left x 8)) 0x00ff00ff)
x (bit-and (bit-or x (bit-shift-left x 4)) 0x0f0f0f0f)
x (bit-and (bit-or x (bit-shift-left x 2)) 0x33333333)]
(bit-and (bit-or x (bit-shift-left x 1)) 0x55555555)))

(defn- interleave3*
[x]
(let [x (bit-and x 0x3fff)
x (bit-and (bit-or x (bit-shift-left x 16)) 0xff0000ff)
x (bit-and (bit-or x (bit-shift-left x 8)) 0xf00f00f)
x (bit-and (bit-or x (bit-shift-left x 4)) 0xc30c30c3)]
(bit-and (bit-or x (bit-shift-left x 2)) 0x49249249)))

(defn interleave2
[x y]
(bit-or (interleave2* x) (bit-shift-left (interleave2* y) 1)))

(defn interleave3
[x y z]
(bit-or
(bit-or
(interleave3* x)
(bit-shift-left (interleave3* y) 1))
(bit-shift-left (interleave3* z) 2)))

(defn deinterleave2-nth
[x i]
(let [x (bit-and (unsigned-bit-shift-right x i) 0x55555555)
x (bit-and (bit-or x (unsigned-bit-shift-right x 1)) 0x33333333)
x (bit-and (bit-or x (unsigned-bit-shift-right x 2)) 0x0f0f0f0f)
x (bit-and (bit-or x (unsigned-bit-shift-right x 4)) 0x00ff00ff)
x (bit-and (bit-or x (unsigned-bit-shift-right x 16)) 0x000ffff)]
(bit-shift-right (bit-shift-left x 16) 16)))

(defn deinterleave2
[x]
[(deinterleave2-nth x 0)
(deinterleave2-nth x 1)])

(defn deinterleave3-nth
[x i]
(let [x (bit-and (unsigned-bit-shift-right x i) 0x49249249)
x (bit-and (bit-or x (unsigned-bit-shift-right x 2)) 0xc30c30c3)
x (bit-and (bit-or x (unsigned-bit-shift-right x 4)) 0xf00f00f)
x (bit-and (bit-or x (unsigned-bit-shift-right x 8)) 0xff0000ff)
x (bit-and (bit-or x (unsigned-bit-shift-right x 16)) 0x3ff)]
(bit-shift-right (bit-shift-left x 22) 22)))

(defn deinterleave3
[x]
[(deinterleave3-nth x 0)
(deinterleave3-nth x 1)
(deinterleave3-nth x 2)])

(defn next-combination
[x]
(let [t (bit-or x (dec x))]
(bit-or
(inc t)
(unsigned-bit-shift-right
(dec (bit-and (bit-not t) (- (bit-not t))))
(inc (trailing-zeros x))))))
473 changes: 473 additions & 0 deletions babel/src/thi/ng/math/core.cljc

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions babel/src/thi/ng/math/gamma.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
(ns thi.ng.math.gamma
(:require [thi.ng.math.core :as m :refer [PI TWO_PI]]))

(def P
[0.99999999999980993
676.5203681218851
-1259.1392167224028
771.32342877765313
-176.61502916214059
12.507343278686905
-0.13857109526572012
9.9843695780195716e-6
1.5056327351493116e-7])

(def PLN
[0.99999999999999709182
57.156235665862923517
-59.597960355475491248
14.136097974741747174
-0.49191381609762019978
0.33994649984811888699e-4
0.46523628927048575665e-4
-0.98374475304879564677e-4
0.15808870322491248884e-3
-0.21026444172410488319e-3
0.21743961811521264320e-3
-0.16431810653676389022e-3
0.84418223983852743293e-4
-0.26190838401581408670e-4
0.36899182659531622704e-5])

(def GLN (/ 607.0 128))

(defn p-sum
[idx n z]
(transduce
(map #(/ (idx %) (+ z %)))
+ (first idx) (range 1 n)))

(defn log
[z]
(if (>= z 0.0)
(let [x (p-sum PLN (count PLN) z)
z' (+ z 0.5)
t (+ z' GLN)]
(- (+ (- (+ (* 0.5 (Math/log TWO_PI))
(* z' (Math/log t)))
t)
(Math/log x))
(Math/log z)))
0.0))

(defn gamma
[z]
(cond
(< z 0.5) (* (/ PI (Math/sin (* z PI))) (gamma (- 1 z)))
(> z 100) (Math/exp (log z))
:else (let [z (dec z)
z' (+ z 0.5)
t (+ z' 7)]
(* (* (* (Math/sqrt TWO_PI)
(Math/pow t z'))
(Math/exp (- t)))
(p-sum P 9 z)))))
136 changes: 136 additions & 0 deletions babel/src/thi/ng/math/macros.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
(ns thi.ng.math.macros
(:refer-clojure :exclude [min max]))

(defmacro defmathop
"Constructs macro to build inlined nested expressions which when
call will apply f successively to all args. Supports arities 2-8."
[name f]
`(defmacro ~name
([a# b#]
`(~~f ~a# ~b#))
([a# b# c#]
`(~~f (~~f ~a# ~b#) ~c#))
([a# b# c# d#]
`(~~f (~~f (~~f ~a# ~b#) ~c#) ~d#))
([a# b# c# d# e#]
`(~~f (~~f (~~f (~~f ~a# ~b#) ~c#) ~d#) ~e#))
([a# b# c# d# e# f#]
`(~~f (~~f (~~f (~~f (~~f ~a# ~b#) ~c#) ~d#) ~e#) ~f#))
([a# b# c# d# e# f# g#]
`(~~f (~~f (~~f (~~f (~~f (~~f ~a# ~b#) ~c#) ~d#) ~e#) ~f#) ~g#))
([a# b# c# d# e# f# g# h#]
`(~~f (~~f (~~f (~~f (~~f (~~f (~~f ~a# ~b#) ~c#) ~d#) ~e#) ~f#) ~g#) ~h#))))

(defmacro defmathop2
"Constructs macro to build inlined nested expressions which when
call will apply f to inner pairs and f2 to combine results."
[name f f2]
`(defmacro ~name
([a# b# c#]
`(~~f2 (~~f ~a# ~b#) ~c#))
([a# b# c# d#]
`(~~f2 (~~f ~a# ~b#) (~~f ~c# ~d#)))
([a# b# c# d# e#]
`(~~f2 (~~f2 (~~f ~a# ~b#) (~~f ~c# ~d#)) ~e#))
([a# b# c# d# e# f#]
`(~~f2 (~~f2 (~~f ~a# ~b#) (~~f ~c# ~d#)) (~~f ~e# ~f#)))
([a# b# c# d# e# f# g#]
`(~~f2 (~~f2 (~~f2 (~~f ~a# ~b#) (~~f ~c# ~d#)) (~~f ~e# ~f#)) ~g#))
([a# b# c# d# e# f# g# h#]
`(~~f2 (~~f2 (~~f2 (~~f ~a# ~b#) (~~f ~c# ~d#)) (~~f ~e# ~f#)) (~~f ~g# ~h#)))))

(defmacro defmathop3
"Takes f, f2 & f3 as syntax-quoted symbols. Constructs a macro which
when called, applies f to all but the last 1 or 2 args. The
remaining arg(s) are combined with the first result using f2.
Furthermore, for arities 6 and 8, f3 is first applied to the last
two args are before the final application of f2. For example:
(defmathop* maddsub `madd `- `*)
(maddsub 2 3 4 5) => (- (madd 2 3 4) 5)
(maddsub 2 3 4 5 6) => (- (madd 2 3 4) (* 5 6))"
[name f f2 f3]
`(defmacro ~name
([a# b# c# d#]
`(~~f2 (~~f ~a# ~b# ~c#) ~d#))
([a# b# c# d# e#]
`(~~f2 (~~f ~a# ~b# ~c# ~d#) ~e#))
([a# b# c# d# e# f#]
`(~~f2 (~~f ~a# ~b# ~c# ~d#) (~~f3 ~e# ~f#)))
([a# b# c# d# e# f# g#]
`(~~f2 (~~f ~a# ~b# ~c# ~d# ~e# ~f#) ~g#))
([a# b# c# d# e# f# g# h#]
`(~~f2 (~~f ~a# ~b# ~c# ~d# ~e# ~f#) (~~f3 ~g# ~h#)))))

(defmathop add `+)
(defmathop sub `-)
(defmathop mul `*)
(defmathop div `/)
(defmathop2 madd `* `+)
(defmathop2 msub `* `-)
(defmathop2 addm `+ `*)
(defmathop2 subm `- `*)
(defmathop2 adddiv `+ `/)
(defmathop2 subdiv `- `/)
(defmathop3 maddsub `madd `- `*)
(defmathop3 addmsub `addm `- `*)
(defmathop3 msubadd `msub `+ `*)
(defmathop3 submadd `subm `+ `*)

(defmacro if*
"Returns y if x pred returns truthy, else 0"
[pred x y] `(if (~pred ~x) ~y 0))

(defmacro bitmask
"Constructs a bit mask from given values & predicate fn applied to
each. If pred returns truthy value the value's related bit is set.
Bit values start at 1 and double for successive args (max 8)."
([pred a]
`(if* ~pred ~a 0x01))
([pred a b]
`(bit-or (bitmask ~pred ~a) (if* ~pred ~b 0x02)))
([pred a b c]
`(bit-or (bitmask ~pred ~a ~b) (if* ~pred ~c 0x04)))
([pred a b c d]
`(bit-or (bitmask ~pred ~a ~b ~c) (if* ~pred ~d 0x08)))
([pred a b c d e]
`(bit-or (bitmask ~pred ~a ~b ~c ~d) (if* ~pred ~e 0x10)))
([pred a b c d e f]
`(bit-or (bitmask ~pred ~a ~b ~c ~d ~e) (if* ~pred ~f 0x20)))
([pred a b c d e f g]
`(bit-or (bitmask ~pred ~a ~b ~c ~d ~e ~f) (if* ~pred ~g 0x40)))
([pred a b c d e f g h]
`(bit-or (bitmask ~pred ~a ~b ~c ~d ~e ~f ~g) (if* ~pred ~h 0x80))))

(defmacro mix
"Linear, bi-linear & tri-linear interpolation"
([a b t]
`(let [a# ~a]
(submadd ~b a# ~t a#)))
([a b c d u v]
`(let [u# ~u
a# (mix ~a ~b u#)]
(submadd (mix ~c ~d u#) a# ~v a#)))
([a b c d e f g h u v w]
`(let [u# ~u
v# ~v
a# (mix ~a ~b ~c ~d u# v#)]
(submadd (mix ~e ~f ~g ~h u# v#) a# ~w a#))))

(defmacro min
([a b] `(let [a# ~a b# ~b] (if (<= a# b#) a# b#)))
([a b c] `(min (min ~a ~b) ~c))
([a b c d] `(min (min (min ~a ~b) ~c) ~d))
([a b c d e] `(min (min (min (min ~a ~b) ~c) ~d) ~e))
([a b c d e f] `(min (min (min (min (min ~a ~b) ~c) ~d) ~e) ~f))
([a b c d e f g] `(min (min (min (min (min (min ~a ~b) ~c) ~d) ~e) ~f) ~g))
([a b c d e f g h] `(min (min (min (min (min (min (min ~a ~b) ~c) ~d) ~e) ~f) ~g) ~h)))

(defmacro max
([a b] `(let [a# ~a b# ~b] (if (>= a# b#) a# b#)))
([a b c] `(max (max ~a ~b) ~c))
([a b c d] `(max (max (max ~a ~b) ~c) ~d))
([a b c d e] `(max (max (max (max ~a ~b) ~c) ~d) ~e))
([a b c d e f] `(max (max (max (max (max ~a ~b) ~c) ~d) ~e) ~f))
([a b c d e f g] `(max (max (max (max (max (max ~a ~b) ~c) ~d) ~e) ~f) ~g))
([a b c d e f g h] `(max (max (max (max (max (max (max ~a ~b) ~c) ~d) ~e) ~f) ~g) ~h)))
118 changes: 118 additions & 0 deletions babel/src/thi/ng/math/noise.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
(ns thi.ng.math.noise
#?(:cljs (:require-macros [thi.ng.math.macros :as mm]))
(:require
#?(:clj [thi.ng.math.macros :as mm]
:cljs [thi.ng.typedarrays.core :as ta])
[thi.ng.math.core :as m]))

;; Permutation table

(def P
(->> [151 160 137 91 90 15 131 13 201 95 96 53 194 233 7 225 140 36
103 30 69 142 8 99 37 240 21 10 23 190 6 148 247 120 234 75 0
26 197 62 94 252 219 203 117 35 11 32 57 177 33 88 237 149 56
87 174 20 125 136 171 168 68 175 74 165 71 134 139 48 27 166
77 146 158 231 83 111 229 122 60 211 133 230 220 105 92 41 55
46 245 40 244 102 143 54 65 25 63 161 1 216 80 73 209 76 132
187 208 89 18 169 200 196 135 130 116 188 159 86 164 100 109
198 173 186 3 64 52 217 226 250 124 123 5 202 38 147 118 126
255 82 85 212 207 206 59 227 47 16 58 17 182 189 28 42 223 183
170 213 119 248 152 2 44 154 163 70 221 153 101 155 167 43 172
9 129 22 39 253 19 98 108 110 79 113 224 232 178 185 112 104
218 246 97 228 251 34 242 193 238 210 144 12 191 179 162 241
81 51 145 235 249 14 239 107 49 192 214 31 181 199 106 157 184
84 204 176 115 121 50 45 127 4 150 254 138 236 205 93 222 114
67 29 24 72 243 141 128 195 78 66 215 61 156 180]
(repeat 2)
(apply concat)
(#?(:clj int-array :cljs ta/uint8))))

(def G
(#?(:clj double-array :cljs ta/float32)
[1.0 1.0 0.0 0.0
-1.0 1.0 0.0 0.0
1.0 -1.0 0.0 0.0
-1.0 -1.0 0.0 0.0
1.0 0.0 1.0 0.0
-1.0 0.0 1.0 0.0
1.0 0.0 -1.0 0.0
-1.0 0.0 -1.0 0.0
0.0 1.0 1.0 0.0
0.0 -1.0 1.0 0.0
0.0 1.0 -1.0 0.0
0.0 -1.0 -1.0 0.0
1.0 1.0 0.0 0.0
-1.0 1.0 0.0 0.0
0.0 -1.0 1.0 0.0
0.0 -1.0 -1.0 0.0]))

(defn smooth
[^double t]
(* t (* t (* t (+ (* t (- (* t 6.0) 15.0)) 10.0)))))

(defn gradient1
[i ^double x]
(* x (aget ^doubles G (bit-shift-left (bit-and (aget ^ints P i) 15) 2))))

(defn gradient2
[i j ^double x ^double y]
(let [idx (-> (aget ^ints P (unchecked-add-int i (aget ^ints P j)))
(bit-and 15)
(bit-shift-left 2))]
(mm/madd
x (aget ^doubles G idx)
y (aget ^doubles G (unchecked-inc idx)))))

(defn gradient3
[i j k x y z]
(let [idx (-> (aget ^ints P (unchecked-add-int i (aget ^ints P (unchecked-add-int j (aget ^ints P k)))))
(bit-and 15)
(bit-shift-left 2))]
(mm/madd
x (aget ^doubles G idx)
y (aget ^doubles G (unchecked-inc idx))
z (aget ^doubles G (unchecked-add-int idx 2)))))

(defn noise1
[x]
(let [xf (m/floor x)
f (- x xf)
ix (bit-and xf 255)]
(* (mm/mix
(gradient1 ix f)
(gradient1 (inc ix) (dec f))
(smooth f))
2.0)))

(defn noise2
[x y]
(let [pfx (m/floor x) pfy (m/floor y)
fpx (- x pfx) fpy (- y pfy)
ipx (bit-and pfx 255) ipy (bit-and pfy 255)
ipx' (inc ipx) ipy' (inc ipy)
fpx' (dec fpx) fpy' (dec fpy)]
(mm/mix
(gradient2 ipx ipy fpx fpy)
(gradient2 ipx' ipy fpx' fpy)
(gradient2 ipx ipy' fpx fpy')
(gradient2 ipx' ipy' fpx' fpy')
(smooth fpx) (smooth fpy))))

(defn noise3
[x y z]
(let [pfx (m/floor x) pfy (m/floor y) pfz (m/floor z)
fpx (- x pfx) fpy (- y pfy) fpz (- z pfz)
ipx (bit-and pfx 255) ipy (bit-and pfy 255) ipz (bit-and pfz 255)
ipx' (inc ipx) ipy' (inc ipy) ipz' (inc ipz)
fpx' (dec fpx) fpy' (dec fpy) fpz' (dec fpz)
t (smooth fpx) t2 (smooth fpy)]
(mm/mix
(gradient3 ipx ipy ipz fpx fpy fpz) ; 000
(gradient3 ipx' ipy ipz fpx' fpy fpz) ; 100
(gradient3 ipx ipy' ipz fpx fpy' fpz) ; 010
(gradient3 ipx' ipy' ipz fpx' fpy' fpz) ; 110
(gradient3 ipx ipy ipz' fpx fpy fpz') ; 001
(gradient3 ipx' ipy ipz' fpx' fpy fpz') ; 101
(gradient3 ipx ipy' ipz' fpx fpy' fpz') ; 011
(gradient3 ipx' ipy' ipz' fpx' fpy' fpz') ; 111
t t2 (smooth fpz))))
3 changes: 3 additions & 0 deletions babel/src/thi/ng/math/version.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(ns thi.ng.math.version)

(def version "0.3.1")
46 changes: 46 additions & 0 deletions babel/test/thi/ng/math/test/core.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(ns thi.ng.math.test.core
(:require
[thi.ng.math.core :as m :refer [*eps*]]
#?(:clj
[clojure.test :refer :all]
:cljs
[cemerick.cljs.test :refer-macros [is deftest]])))

(deftest test-delta=
(let [e (* *eps* 0.9)]
(is (m/delta= (int 1) (int 1)))
(is (m/delta= (int 1) (int 2) 1))
(is (not (m/delta= (int 1) (int 2))))
(is (m/delta= (long 1) (long 1)))
(is (m/delta= (long 1) (long 2) 1))
(is (not (m/delta= (long 1) (long 2))))
(is (m/delta= (float 1) (float 1)))
(is (m/delta= (float 1) (float 2) 1))
(is (m/delta= (float 1) (float 1.00000001)))
(is (not (m/delta= (float 1) (float 1.01))))
(is (m/delta= (double 1) (double 1)))
(is (m/delta= (double 1) (double 2) 1))
(is (m/delta= (double 1) (double 1.00000001)))
(is (not (m/delta= (double 1) (double 1.01))))
(is (m/delta= 1.0 1.0))
(is (m/delta= 0 0.0))
(is (m/delta= nil nil))
(is (m/delta= nil nil 1))
(is (m/delta= [nil] [nil]))
(is (not (m/delta= nil 1)))
(is (not (m/delta= nil 1 1)))
(is (not (m/delta= 1 nil)))
(is (not (m/delta= 1 nil 1)))
(is (not (m/delta= [1 2 3] nil)))
(is (not (m/delta= '(1 2 3) nil)))
(is (not (m/delta= [[1 2 3] 4] [[1 2 3] nil])))
(is (m/delta= [[1 2 3] [(+ 1.0 e) (+ 2.0 e) (+ 3.0 e)]]
(list (list (+ 1.0 e) (+ 2.0 e) (+ 3.0 e)) '(1 2 3))))
(is (m/delta= (list (list (+ 1.0 e) (+ 2.0 e) (+ 3.0 e)) '(1 2 3))
[[1 2 3] [(+ 1.0 e) (+ 2.0 e) (+ 3.0 e)]]))
(is (m/delta= (lazy-seq [1 2 3]) [1 2 3]))
(is (m/delta= (seq [1 2 3]) [1 2 3]))
(is (not (m/delta= [1 2 3] [1 2])))
(is (not (m/delta= '(1 2 3) [1 2])))
(is (not (m/delta= (map identity [1 2 3]) [1 2]))))
)
137 changes: 137 additions & 0 deletions babel/test/thi/ng/math/test/macros.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
(ns thi.ng.math.test.macros
#?@(:clj
[(:require
[clojure.test :refer :all]
[thi.ng.math.macros :as mm])]
:cljs
[(:require-macros
[thi.ng.math.macros :as mm])
(:require
[cemerick.cljs.test :refer-macros [is deftest]])]))

(deftest test-add
(is (== 5 (mm/add 2.0 3.0)))
(is (== 9 (mm/add 2.0 3.0 4.0)))
(is (== 14 (mm/add 2.0 3.0 4.0 5.0)))
(is (== 20 (mm/add 2.0 3.0 4.0 5.0 6.0)))
(is (== 27 (mm/add 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== 35 (mm/add 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== 44 (mm/add 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-sub
(is (== -1 (mm/sub 2.0 3.0)))
(is (== -5 (mm/sub 2.0 3.0 4.0)))
(is (== -10 (mm/sub 2.0 3.0 4.0 5.0)))
(is (== -16 (mm/sub 2.0 3.0 4.0 5.0 6.0)))
(is (== -23 (mm/sub 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== -31 (mm/sub 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== -40 (mm/sub 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-mul
(is (== 6 (mm/mul 2.0 3.0)))
(is (== 24 (mm/mul 2.0 3.0 4.0)))
(is (== 120 (mm/mul 2.0 3.0 4.0 5.0)))
(is (== 720 (mm/mul 2.0 3.0 4.0 5.0 6.0)))
(is (== 5040 (mm/mul 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== 40320 (mm/mul 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== 362880 (mm/mul 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-div
(is (== (/ 2.0 3.0) (mm/div 2.0 3.0)))
(is (== (/ 2.0 3.0 4.0) (mm/div 2.0 3.0 4.0)))
(is (== (/ 2.0 3.0 4.0 5.0) (mm/div 2.0 3.0 4.0 5.0)))
(is (== (/ 2.0 3.0 4.0 5.0 6.0) (mm/div 2.0 3.0 4.0 5.0 6.0)))
(is (== (/ 2.0 3.0 4.0 5.0 6.0 7.0) (mm/div 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== (/ 2.0 3.0 4.0 5.0 6.0 7.0 8.0) (mm/div 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== (/ 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0) (mm/div 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-madd
(is (== 10 (mm/madd 2.0 3.0 4.0)))
(is (== 26 (mm/madd 2.0 3.0 4.0 5.0)))
(is (== 32 (mm/madd 2.0 3.0 4.0 5.0 6.0)))
(is (== 68 (mm/madd 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== 76 (mm/madd 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== 140 (mm/madd 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-msub
(is (== 2 (mm/msub 2.0 3.0 4.0)))
(is (== -14 (mm/msub 2.0 3.0 4.0 5.0)))
(is (== -20 (mm/msub 2.0 3.0 4.0 5.0 6.0)))
(is (== -56 (mm/msub 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== -64 (mm/msub 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== -128 (mm/msub 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-addm
(is (== 20 (mm/addm 2.0 3.0 4.0)))
(is (== 45 (mm/addm 2.0 3.0 4.0 5.0)))
(is (== 270 (mm/addm 2.0 3.0 4.0 5.0 6.0)))
(is (== 585 (mm/addm 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== 4680 (mm/addm 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== 9945 (mm/addm 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-adddiv
(is (== (/ 5.0 4.0) (mm/adddiv 2.0 3.0 4.0)))
(is (== (/ 5.0 9.0) (mm/adddiv 2.0 3.0 4.0 5.0)))
(is (== (/ 5.0 9.0 6.0) (mm/adddiv 2.0 3.0 4.0 5.0 6.0)))
(is (== (/ 5.0 9.0 13.0) (mm/adddiv 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== (/ 5.0 9.0 13.0 8.0) (mm/adddiv 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== (/ 5.0 9.0 13.0 17.0) (mm/adddiv 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-subm
(is (== -4 (mm/subm 2.0 3.0 4.0)))
(is (== 1 (mm/subm 2.0 3.0 4.0 5.0)))
(is (== 6 (mm/subm 2.0 3.0 4.0 5.0 6.0)))
(is (== -1 (mm/subm 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== -8 (mm/subm 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== 1 (mm/subm 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-subdiv
(is (== (/ -1.0 4.0) (mm/subdiv 2.0 3.0 4.0)))
(is (== (/ -1.0 -1.0) (mm/subdiv 2.0 3.0 4.0 5.0)))
(is (== (/ -1.0 -1.0 6.0) (mm/subdiv 2.0 3.0 4.0 5.0 6.0)))
(is (== (/ -1.0 -1.0 -1.0) (mm/subdiv 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== (/ -1.0 -1.0 -1.0 8.0) (mm/subdiv 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== (/ -1.0 -1.0 -1.0 -1.0) (mm/subdiv 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-maddsub
(is (== 5 (mm/maddsub 2.0 3.0 4.0 5.0)))
(is (== 20 (mm/maddsub 2.0 3.0 4.0 5.0 6.0)))
(is (== -16 (mm/maddsub 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== 60 (mm/maddsub 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== -4 (mm/maddsub 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-msubadd
(is (== 7 (mm/msubadd 2.0 3.0 4.0 5.0)))
(is (== -8 (mm/msubadd 2.0 3.0 4.0 5.0 6.0)))
(is (== 28 (mm/msubadd 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== -48 (mm/msubadd 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== 16 (mm/msubadd 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-addmsub
(is (== 15 (mm/addmsub 2.0 3.0 4.0 5.0)))
(is (== 39 (mm/addmsub 2.0 3.0 4.0 5.0 6.0)))
(is (== 3 (mm/addmsub 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== 577 (mm/addmsub 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== 513 (mm/addmsub 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))

(deftest test-submadd
(is (== 1 (mm/submadd 2.0 3.0 4.0 5.0)))
(is (== 7 (mm/submadd 2.0 3.0 4.0 5.0 6.0)))
(is (== 43 (mm/submadd 2.0 3.0 4.0 5.0 6.0 7.0)))
(is (== 7 (mm/submadd 2.0 3.0 4.0 5.0 6.0 7.0 8.0)))
(is (== 71 (mm/submadd 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0))))
(deftest bitmask
(is (= 0x01 (mm/bitmask pos? 1)))
(is (= 0x03 (mm/bitmask pos? 1 1)))
(is (= 0x07 (mm/bitmask pos? 1 1 1)))
(is (= 0x0f (mm/bitmask pos? 1 1 1 1)))
(is (= 0x1f (mm/bitmask pos? 1 1 1 1 1)))
(is (= 0x3f (mm/bitmask pos? 1 1 1 1 1 1)))
(is (= 0x7f (mm/bitmask pos? 1 1 1 1 1 1 1)))
(is (= 0xff (mm/bitmask pos? 1 1 1 1 1 1 1 1)))
(is (= 0x55 (mm/bitmask pos? 1 0 1 0 1 0 1 0)))
(is (= 0xaa (mm/bitmask pos? 0 1 0 1 0 1 0 1))))
(deftest mix
(is (= 0.5 (mm/mix 0 1 0.5)))
(is (= 1.5 (mm/mix 0 1 2 3 0.5 0.5)))
(is (= 3.5 (mm/mix 0 1 2 3 4 5 6 7 0.5 0.5 0.5))))
2 changes: 1 addition & 1 deletion src/bits.org
Original file line number Diff line number Diff line change
@@ -177,7 +177,7 @@ http://graphics.stanford.edu/~seander/bithacks.html

#+BEGIN_SRC clojure :tangle ../babel/src/thi/ng/math/bits.cljc :noweb yes :mkdirp yes :padline no
(ns thi.ng.math.bits
(:refer-clojure :exclude [bit-count]))
(:refer-clojure :exclude [abs bit-count]))

<<const>>

2 changes: 1 addition & 1 deletion src/core.org
Original file line number Diff line number Diff line change
@@ -604,7 +604,7 @@ to math operations across several other [[http://thi.ng/][thi.ng]] projects, mos

#+BEGIN_SRC clojure :tangle ../babel/src/thi/ng/math/core.cljc :noweb yes :mkdirp yes :padline no
(ns thi.ng.math.core
(:refer-clojure :exclude [+ - * min max bit-count])
(:refer-clojure :exclude [+ - * abs min max bit-count])
#?(:clj
(:require [thi.ng.math.macros :as mm])
:cljs