-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
269 lines (222 loc) · 6.3 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
;;;;
;;;; Copyright (c) Ludger Solbach. All rights reserved.
;;;;
;;;; The use and distribution terms for this software are covered by the
;;;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;;;; which can be found in the file license.txt at the root of this distribution.
;;;; By using this software in any fashion, you are agreeing to be bound by
;;;; the terms of this license.
;;;;
;;;; You must not remove this notice, or any other, from this software.
;;;;
(ns org.soulspace.math.core
;(:refer-clojure :exclude [abs])
)
;;;
;;; Java Math implementations for Clojure
;;;
(set! *warn-on-reflection* true)
(def default-epsilon "Default tolerance (epsilon)." 0.00001)
(def default-dx "Default step size (delta x)." 0.0000001)
(def ^:const PI (Math/PI))
(def ^:const E (Math/E))
(def ^:const LN2 (Math/log 2))
(def ^:const DOUBLE-PI (* 2 PI))
(def ^:const HALF-PI (/ PI 2))
(comment
; use clojure.core/abs
(defn abs
"Calculates the absolute of x (with java.lang.Math)."
^double [^double x]
(Math/abs x))
)
(defn sign
"Calculates the sign of x (with java.lang.Math)."
^double [^double x]
(Math/signum x))
(defn floor
"Calculates the floor of x (with java.lang.Math)."
[^double x]
(Math/floor x))
(defn ceil
"Calculates the ceiling of x (with java.lang.Math)."
[^double x]
(Math/ceil x))
(defn sqrt
"Calculates the square root of x (with java.lang.Math)."
^double [^double x]
(Math/sqrt x))
(defn cbrt
"Calculates the cubic root of x (with java.lang.Math)."
^double [^double x]
(Math/cbrt x))
(defn pow
"Calculates x raised to the power of y (with java.lang.Math)."
^double [^double x ^double y]
(Math/pow x y))
(defn exp
"Calculates the exponential function of x (with java.lang.Math)."
^double [^double x]
(Math/exp x))
(defn expm1
"Calulates e to the power of x minus 1."
^double [^double x]
(Math/expm1 x))
(defn log
"Calculates the natural logarithm (with base e) of x (with java.lang.Math)."
^double [^double x]
(Math/log x))
(defn log-with-base
"Calculates the logarithm with base b of x."
^double [^double b ^double x]
(/ (Math/log x) (Math/log b)))
(defn log10
"Calculates the logarithm with base 10 of x (with java.lang.Math)."
^double [^double x]
(Math/log10 x))
(defn alog10
"Calculates the inverse of the logarithm with base 2 of x (with java.lang.Math)."
^double [^double x]
(Math/pow 10 x))
(defn log2
"Calculates the logarithm with base 2 of x (with java.lang.Math)."
^double [^double x]
(/ (Math/log x) LN2))
(defn alog2
"Calculates the inverse of the logarithm with base 2 of x (with java.lang.Math)."
^double [^double x]
(Math/pow 2 x))
(defn log1p
"Calculates the natural logarithm of the sum of x and 1."
^double [^double x]
(Math/log1p x))
; Trigonometrical functions
(defn cos
^double [^double x]
"Calculates the cosine of x (with java.lang.Math)."
(Math/cos x))
(defn sin
"Calculates the sine of x (with java.lang.Math)."
^double [^double x]
(Math/sin x))
(defn tan
"Calculates the tangens of x (with java.lang.Math)."
^double [^double x]
(Math/tan x))
(defn asin
"Calculates the arc sine of x (with java.lang.Math)."
^double [^double x]
(Math/asin x))
(defn acos
"Calculates the arc cosine of x (with java.lang.Math)."
^double [^double x]
(Math/acos x))
(defn atan
"Calculates the arc tangens of x (with java.lang.Math)."
^double [^double x]
(Math/atan x))
(defn atan2
"Calculates the arc tangens of a/b. Returns the result in the correct quadrant."
^double [^double a ^double b]
(let [r (atan (/ a b))] ; TODO handle b = 0 case
(cond
(< b 0) (if (< a 0)
(- r PI) ; adjust quadrant
(+ r PI)) ; adjust quadrant
:default r)))
(defn cosh
"Calculates the hyperbolic cosine of x (with java.lang.Math)."
^double [^double x]
(Math/cosh x))
(defn sinh
"Calculates the hyperbolic sine of x (with java.lang.Math)."
^double [^double x]
(Math/sinh x))
(defn tanh
"Calculates the hyperbolic tangens of x (with java.lang.Math)."
^double [^double x]
(Math/tanh x))
(defn hypot
"Calculates the hypothenuse of x and y (Pythagoras) (with java.lang.Math)."
^double [^double x ^double y]
(Math/hypot x y))
(defn deg-to-rad
"Converts degrees to radians (with java.lang.Math)."
^double [^double deg]
(Math/toRadians deg))
(defn rad-to-deg
"Converts radians to degrees (with java.lang.Math)."
^double [^double rad]
(Math/toDegrees rad))
;;
;; native implemented functions
;;
(defn sqr
"Calculates the square of x."
[x]
(* x x))
(defn cube
"Calculates the cube of x."
[x]
(* x x x))
(defn avg
"Calculates the avarage of x and y or of the values of coll."
([x y]
(/ (+ x y) 2))
([coll]
(/ (reduce + 0 coll) (count coll))))
(defn factorial
"Calculates the factorial of x."
^long [^long x]
(loop [curr (long x) fact (long 1)]
(cond
(<= curr 0) 0
(= curr 1) fact
:default (recur (dec curr) (long (*' curr fact))))))
(defn fibonacci
"Calculates the fibonacci number of x."
^long [^long x]
(loop [a (long 1) b (long 0) cnt (long x)]
(cond
(= cnt 0) b
:default (recur (long (+' a b)) (long a) (long (dec cnt))))))
;;
;; special trigonometric functions
;;
(defn hav
"Calculates the haversine function of the angle a."
^double [^double x]
(sqr (sin (/ x 2))))
(defn ahav
"Calculates the arc haversine function of the value v."
^double [^double x]
(* 2 (asin (sqrt x))))
;;
;; special functions
;;
(defn- tau-erf
^double [^double x]
(let [t (/ 1
(+ 1 (* 1/2 (abs x))))]
(- 1 (* t (exp (+ (* -1 x x)
-1.26551223
(* 1.00002368 t)
(* 0.37409196 t t)
(* 0.09678418 t t t)
(* -0.18628806 t t t t)
(* 0.27886807 t t t t t)
(* -1.13520398 t t t t t t)
(* 1.48851587 t t t t t t t)
(* -0.82215223 t t t t t t t t)
(* 0.17087277 t t t t t t t t t)))))))
(defn erf
"Calculates the gaussian error function."
^double [^double x]
(let [z (tau-erf x)]
(if (>= x 0)
z
(* -1 z))))
(defn erfc
"Calculates the complementary gaussian error function."
^double [^double x])
; TODO implement