-
Notifications
You must be signed in to change notification settings - Fork 9
/
core.clj
1662 lines (1415 loc) · 66.8 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; # Namespace scope
;;
;; Collection of math function:
;;
;; * Several constants from Java, C, Processing, etc.
;; * Functions based on FastMath exposed as macros or functions (trigonometry, powers/logarithms/roots, rounding)
;; * Primitive operators (as in primitive-math package)
;; * Additional math functions (signum, constrain, interpolation)
;; * Statistics
(ns 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](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:
Known from Clojure: `*` `+` `-` `/` `>` `<` `>=` `<=` `==` `rem` `quot` `mod` `bit-or` `bit-and` `bit-xor` `bit-and-not` `bit-set` `bit-clear` `bit-test` `bit-flip` `bit-not` `bit-shift-left` `bit-shift-right` `unsigned-bit-shift-right` `inc` `dec` `zero?` `neg?` `pos?` `min` `max` `even?` `odd?` `abs`
And additionally:
* `<<` - bit shift left
* `>>` - signed bit shift right
* `>>>` - unsigned bit shift right
* `not==` - not equal
Warning: All `bool-` evaluate all parameters.
To turn on primitive math on your namespace call [[use-primitive-operators]].
To turn off and revert original versions call [[unuse-primitive-operators]]
### Fast Math
Almost all math functions are backed by [FastMath](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
Additionally namespace contains functions which are common in frameworks like OpenFrameworks and Processing."
(:refer-clojure
:exclude [* + - / > < >= <= == rem quot mod bit-or bit-and bit-and-not bit-set bit-clear bit-test bit-flip bit-xor bit-not bit-shift-left bit-shift-right unsigned-bit-shift-right inc dec zero? neg? pos? min max even? odd? abs])
(:import [net.jafama FastMath]
[fastmath.java PrimitiveMath]
[org.apache.commons.math3.util Precision]
[org.apache.commons.math3.special Gamma]))
(set! *unchecked-math* :warn-on-boxed)
;; which java?
(def ^{:const true :tag 'long} jvm-version
(->> (System/getProperty "java.version")
(re-seq #"\d+")
(first)
(Long/parseLong)))
;; ## Macros
(defmacro ^:private javaclass-proxy
"Wrapps operation into macro"
([class arity alt-name name]
(let [cf (str class "/" name)
f (symbol cf)
x (symbol "x")
y (symbol "y")
z (symbol "z")
doc (or (:doc (meta alt-name)) (str cf " function wrapped in macro."))
arity-1 `([~x] (list '~f ~x))
arity-2 `([~x ~y] (list '~f ~x ~y))
arity-3 `([~x ~y ~z] (list '~f ~x ~y ~z))]
(condp = arity
:two `(defmacro ~alt-name ~doc ~arity-2)
:onetwo `(defmacro ~alt-name ~doc ~arity-1 ~arity-2)
:three `(defmacro ~alt-name ~doc ~arity-3)
`(defmacro ~alt-name ~doc ~arity-1))))
([class arity name]
`(javaclass-proxy ~class ~arity ~name ~name)))
(defmacro ^:private fastmath-proxy [& rest] `(javaclass-proxy "net.jafama.FastMath" ~@rest))
(defmacro ^:private primitivemath-proxy [& rest] `(javaclass-proxy "fastmath.java.PrimitiveMath" ~@rest))
(defmacro ^:private erf-proxy [& rest] `(javaclass-proxy "org.apache.commons.math3.special.Erf" ~@rest))
(defmacro ^:private gamma-proxy [& rest] `(javaclass-proxy "org.apache.commons.math3.special.Gamma" ~@rest))
(defmacro ^:private beta-proxy [& rest] `(javaclass-proxy "org.apache.commons.math3.special.Beta" ~@rest))
(defmacro ^:private besselj-proxy [& rest] `(javaclass-proxy "org.apache.commons.math3.special.BesselJ" ~@rest))
(defmacro ^:private variadic-proxy
"Creates left-associative variadic forms for any operator.
https://github.com/ztellman/primitive-math/blob/master/src/primitive_math.clj#L10"
([name]
`(variadic-proxy ~name ~name))
([name fn]
`(variadic-proxy ~name ~fn identity))
([name fn single-arg-form]
(let [x (symbol "x")
y (symbol "y")
rest (symbol "rest")
fname (symbol (str "fastmath.java.PrimitiveMath/" fn))
qname (symbol (str "fastmath.core/" name))
doc (or (:doc (meta name)) (str "A primitive math version of `" name "`"))]
`(defmacro ~name
~doc
([~x]
~((eval single-arg-form) x))
([~x ~y]
(list '~fname ~x ~y))
([~x ~y ~'& ~rest]
(list* '~qname (list '~fname ~x ~y) ~rest))))))
(defmacro ^:private variadic-predicate-proxy
"Turns variadic predicates into multiple pair-wise comparisons.
https://github.com/ztellman/primitive-math/blob/master/src/primitive_math.clj#L27"
([name]
`(variadic-predicate-proxy ~name ~name))
([name fn]
`(variadic-predicate-proxy ~name ~fn (constantly true)))
([name fn single-arg-form]
(let [x (symbol "x")
y (symbol "y")
rest (symbol "rest")
fname (symbol (str "fastmath.java.PrimitiveMath/" fn))
qname (symbol (str "fastmath.core/" name))
doc (or (:doc (meta name)) (str "A primitive math version of `" name "`"))]
`(defmacro ~name
~doc
([~x]
~((eval single-arg-form) x))
([~x ~y]
(list '~fname ~x ~y))
([~x ~y ~'& ~rest]
(list 'fastmath.java.PrimitiveMath/and (list '~fname ~x ~y) (list* '~qname ~y ~rest)))))))
;; ## Basic operations
(variadic-proxy + add)
(variadic-proxy - subtract (fn [x] `(list 'fastmath.java.PrimitiveMath/negate ~x)))
(variadic-proxy * multiply)
(variadic-proxy / divide (fn [x] `(list 'fastmath.java.PrimitiveMath/reciprocal ~x)))
(primitivemath-proxy :one inc)
(primitivemath-proxy :one dec)
(primitivemath-proxy :two rem remainder)
(primitivemath-proxy :two quot quotient)
(primitivemath-proxy :two mod modulus)
(variadic-proxy bit-and bitAnd)
(variadic-proxy bit-nand bitNand)
(variadic-proxy bit-and-not bitAndNot)
(variadic-proxy bit-or bitOr)
(variadic-proxy bit-nor bitNor)
(variadic-proxy bit-xor bitXor)
(primitivemath-proxy :one bit-not bitNot)
(primitivemath-proxy :two bit-set bitSet)
(primitivemath-proxy :two bit-clear bitClear)
(primitivemath-proxy :two bit-flip bitFlip)
(primitivemath-proxy :two bit-test bitTest)
(variadic-proxy ^{:deprecated true} bool-and and)
(variadic-proxy ^{:deprecated true} bool-or or)
(variadic-proxy ^{:deprecated true} bool-xor xor)
(primitivemath-proxy :one bool-not not)
(variadic-proxy min)
(variadic-proxy max)
(primitivemath-proxy :one zero? isZero)
(primitivemath-proxy :one one? isOne)
(primitivemath-proxy :one neg? isNeg)
(primitivemath-proxy :one pos? isPos)
(primitivemath-proxy :one not-neg? isNNeg)
(primitivemath-proxy :one not-pos? isNPos)
(primitivemath-proxy :one even? isEven)
(primitivemath-proxy :one odd? isOdd)
(primitivemath-proxy :two << shiftLeft)
(primitivemath-proxy :two >> shiftRight)
(primitivemath-proxy :two >>> unsignedShiftRight)
(primitivemath-proxy :two bit-shift-left shiftLeft)
(primitivemath-proxy :two bit-shift-right shiftRight)
(primitivemath-proxy :two unsigned-bit-shift-right unsignedShiftRight)
(variadic-predicate-proxy < lt)
(variadic-predicate-proxy > gt)
(variadic-predicate-proxy <= lte)
(variadic-predicate-proxy >= gte)
(variadic-predicate-proxy ^{:doc "Equality. See also [[eq]] for function version."} == eq)
(variadic-predicate-proxy not== neq)
(defn negative-zero?
"Check if zero is negative, ie. -0.0"
[^double x]
(== (Double/doubleToLongBits x) -9223372036854775808)) ;; -0.0
(defn fast+
{:inline (fn [x y] `(+ ~x ~y)) :inline-arities #{2}
:doc "Primitive `+` for two doubles as function."}
^double [^double a ^double b] (+ a b))
(defn fast-
{:inline (fn [x y] `(- ~x ~y)) :inline-arities #{2}
:doc "Primitive `-` for two doubles as function."}
^double [^double a ^double b] (- a b))
(defn fast*
{:inline (fn [x y] `(* ~x ~y)) :inline-arities #{2}
:doc "Primitive `*` for two doubles as function."}
^double [^double a ^double b] (* a b))
(defn fast-max
{:inline (fn [x y] `(max ~x ~y)) :inline-arities #{2}
:doc "Primitive `max` for two doubles as function."}
^double [^double a ^double b] (max a b))
(defn fast-min
{:inline (fn [x y] `(min ~x ~y)) :inline-arities #{2}
:doc "Primitive `min` for two doubles as function"}
^double [^double a ^double b] (min a b))
(defn fast-identity
{:inline (fn [x] `~x) :inline-arities #{1}
:doc "Identity on double."}
^double [^double a] a)
;; Primitive math eq
(defn eq
"Primitive math equality function for doubles. See [[==]]."
([_] true)
([^double a ^double b]
(== a b))
([^double a ^double b ^double c]
(and (== a b) (== b c)))
([^double a ^double b ^double c ^double d]
(and (== a b) (== b c) (== c d))))
;; macros for polynomials
(defmacro muladd
"`[x y z]` -> `(+ z (* x y))` or `Math/fma` for java 9+"
[x y z]
(if (< jvm-version 9)
`(+ ~z (* ~x ~y))
`(Math/fma ~x ~y ~z)))
(defmacro fma
"`[x y z]` -> `(+ z (* x y))` or `Math/fma` for java 9+"
[x y z]
`(muladd ~x ~y ~z))
(defmacro negmuladd
"`[x y z]` -> `(+ z (* -1.0 x y)`"
[x y z]
`(muladd (- ~x) ~y ~z))
(defmacro mevalpoly
"Evaluate polynomial macro version in the form coeffs[0]+coeffs[1]*x+coeffs[2]*x^2+...."
[x & coeffs]
(let [cnt (count coeffs)]
(condp clojure.core/= cnt
0 `0.0
1 `~(first coeffs)
2 (let [[z y] coeffs]
`(muladd ~x ~y ~z))
`(muladd ~x (mevalpoly ~x ~@(rest coeffs)) ~(first coeffs)))))
(defn evalpoly
"Evaluate polynomial"
[x & coeffs]
(if-not (seq coeffs)
0.0
(let [rc (reverse coeffs)]
(loop [rcoeffs (rest rc)
^double ex (first rc)]
(if-not (seq rcoeffs)
ex
(recur (rest rcoeffs)
(muladd ^double x ex ^double (first rcoeffs))))))))
(defn makepoly
"Create polynomial function for given coefficients"
[coeffs]
(cond
(not (seq coeffs)) (constantly 0.0)
(= 1 (count coeffs)) (constantly (first coeffs))
:else (let [rc (reverse coeffs)]
(fn [^double x]
(loop [rcoeffs (rest rc)
^double ex (first rc)]
(if-not (seq rcoeffs)
ex
(recur (rest rcoeffs)
(muladd x ex ^double (first rcoeffs)))))))))
;; some stuff from pbrt
(defn difference-of-products
"Kahan's algorithm for (a*b)-(c*d) to avoid catastrophic cancellation."
^double [^double a ^double b ^double c ^double d]
(let [cd (* c d)]
(+ (fma a b (- cd)) (fma (- c) d cd))))
(defn sum-of-products
"Kahan's algorithm for (a*b)+(c*d) to avoid catastrophic cancellation."
^double [^double a ^double b ^double c ^double d]
(let [cd (* c d)]
(+ (fma a b cd) (fma c d (- cd)))))
;; Processing math constants
(def ^{:const true :tag 'double :doc "Value of \\\\(\\pi\\\\)"} PI Math/PI)
(def ^{:const true :tag 'double :doc "Value of \\\\(\\frac{\\pi}{2}\\\\)"} HALF_PI (* PI 0.5))
(def ^{:const true :tag 'double :doc "Value of \\\\(\\frac{\\pi}{3}\\\\)"} THIRD_PI (/ PI 3.0))
(def ^{:const true :tag 'double :doc "Value of \\\\(\\frac{\\pi}{4}\\\\)"} QUARTER_PI (* PI 0.25))
(def ^{:const true :tag 'double :doc "Value of \\\\(2 {\\pi}\\\\)"} TWO_PI (+ PI PI))
(def ^{:const true :tag 'double :doc "Alias for [[TWO_PI]]"} TAU TWO_PI)
(def ^{:const true :tag 'double :doc "Value of \\\\(e\\\\)"} E Math/E)
(def ^{:const true :tag 'double :doc "Value of \\\\(-\\pi\\\\)"} -PI (- Math/PI))
(def ^{:const true :tag 'double :doc "Value of \\\\(-\\frac{\\pi}{2}\\\\)"} -HALF_PI (* PI -0.5))
(def ^{:const true :tag 'double :doc "Value of \\\\(-\\frac{\\pi}{3}\\\\)"} -THIRD_PI (/ -PI -3.0))
(def ^{:const true :tag 'double :doc "Value of \\\\(-\\frac{\\pi}{4}\\\\)"} -QUARTER_PI (* PI -0.25))
(def ^{:const true :tag 'double :doc "Value of \\\\(-2 {\\pi}\\\\)"} -TWO_PI (- TWO_PI))
(def ^{:const true :tag 'double :doc "Alias for [[TWO_PI-]]"} -TAU -TWO_PI)
(def ^{:const true :tag 'double :doc "Value of \\\\(-e\\\\)"} -E (- Math/E))
(def ^{:const true :tag 'double :doc "Value of \\\\(\\frac{1}{\\pi}\\\\)"} INV_PI (/ PI))
(def ^{:const true :tag 'double :doc "Value of \\\\(\\frac{2}{\\pi}\\\\)"} TWO_INV_PI (/ 2.0 PI))
(def ^{:const true :tag 'double :doc "Value of \\\\(\\frac{4}{\\pi}\\\\)"} FOUR_INV_PI (/ 4.0 PI))
(def ^{:const true :tag 'double :doc "Value of \\\\(\\frac{1}{2 \\pi}\\\\)"} INV_TWO_PI (/ TWO_PI))
(def ^{:const true :tag 'double :doc "Value of \\\\(\\frac{1}{4 \\pi}\\\\)"} INV_FOUR_PI (/ (* 2.0 TWO_PI)))
(def ^{:const true :tag 'double :doc "Very small number \\\\(\\varepsilon\\\\)"} EPSILON 1.0e-10)
(def ^{:const true :tag 'double :doc "Euler-Mascheroni constant"} GAMMA Gamma/GAMMA)
(def ^{:const true :tag 'double :doc "Lanchos approximation `g` constant"} LANCZOS_G Gamma/LANCZOS_G)
(def ^{:const true :tag 'double :doc "Catalan G"} CATALAN_G 0.915965594177219015054603514932384110774)
(defonce ^{:const true :tag 'double :doc "Smallest machine number. Value is calculated during evaluation and may differ on different processors."}
MACHINE-EPSILON (* 0.5 (double (loop [d (double 1.0)]
(if (not== 1.0 (+ 1.0 (* d 0.5)))
(recur (* d 0.5))
d)))))
(def ^{:const true :tag 'double :doc "Value of \\\\(\\frac{1}{3}\\\\)"} THIRD (/ 3.0))
(def ^{:const true :tag 'double :doc "Value of \\\\(\\frac{2}{3}\\\\)"} TWO_THIRD (/ 2.0 3.0))
(def ^{:const true :tag 'double :doc "Value of \\\\(\\frac{1}{6}\\\\)"} SIXTH (/ 6.0))
;; Trigonometry
(fastmath-proxy :one sin)
(fastmath-proxy :one cos)
(fastmath-proxy :one tan)
(fastmath-proxy :one asin)
(fastmath-proxy :one acos)
(fastmath-proxy :one atan)
(fastmath-proxy :one sinh)
(fastmath-proxy :one cosh)
(fastmath-proxy :one tanh)
(fastmath-proxy :one asinh)
(fastmath-proxy :one acosh)
(fastmath-proxy :one atanh)
(fastmath-proxy :one ^{:doc "Fast and less accurate [[sin]]."} qsin sinQuick)
(fastmath-proxy :one ^{:doc "Fast and less accurate [[cos]]."} qcos cosQuick)
;; Additional trigonometry functions
(defn cot "Cotangent" ^double [^double v] (/ (FastMath/tan v)
#_(FastMath/tan (- HALF_PI v))))
(defn sec "Secant" ^double [^double v] (/ (FastMath/cos v)))
(defn csc "Cosecant" ^double [^double v] (/ (FastMath/sin v)))
;; Additional cyclometric functions
(defn acot "Arccotangent" ^double [^double v] (- HALF_PI (FastMath/atan v)))
(defn asec "Arcsecant" ^double [^double v] (FastMath/acos (/ 1.0 v)))
(defn acsc "Arcosecant" ^double [^double v] (FastMath/asin (/ 1.0 v)))
(fastmath-proxy :two atan2)
;; Additional hyperbolic functions
(defn coth "Hyperbolic cotangent"^double [^double v] (/ (FastMath/tanh v)))
(defn sech "Hyperbolic secant" ^double [^double v] (/ (FastMath/cosh v)))
(defn csch "Hyperbilic cosecant" ^double [^double v] (/ (FastMath/sinh v)))
;; Additional inverse hyperbolic functions
(defn acoth "Area hyperbolic cotangent" ^double [^double v] (FastMath/atanh (/ v)))
(defn asech "Area hyperbolic secant" ^double [^double v] (FastMath/acosh (/ v)))
(defn acsch "Area hyperbolic cosecant" ^double [^double v] (FastMath/asinh (/ v)))
;; historical
(defn crd "Chord" ^double [^double v] (* 2.0 (FastMath/sin (* 0.5 v))))
(defn acrd "Inverse chord" ^double [^double v] (* 2.0 (FastMath/asin (* 0.5 v))))
(defn versin "Versine" ^double [^double v] (- 1.0 (FastMath/cos v)))
(defn coversin "Coversine" ^double [^double v] (- 1.0 (FastMath/sin v)))
(defn vercos "Vercosine" ^double [^double v] (inc (FastMath/cos v)))
(defn covercos "Covercosine" ^double [^double v] (inc (FastMath/sin v)))
(defn aversin "Arc versine" ^double [^double v] (FastMath/acos (- 1.0 v)))
(defn acoversin "Arc coversine" ^double [^double v] (FastMath/asin (- 1.0 v)))
(defn avercos "Arc vecosine" ^double [^double v] (FastMath/acos (dec v)))
(defn acovercos "Arc covercosine" ^double [^double v] (FastMath/asin (dec v)))
(defn haversin
"Haversine formula for value or lattitude and longitude pairs."
(^double [^double v] (* 0.5 (- 1.0 (FastMath/cos v))))
(^double [[^double lat1 ^double lon1] [^double lat2 ^double lon2]]
(haversin lat1 lon1 lat2 lon2))
(^double [^double lat1 ^double lon1 ^double lat2 ^double lon2]
(+ (haversin (- lat2 lat1))
(* (FastMath/cos lat1)
(FastMath/cos lat2)
(haversin (- lon2 lon1))))))
(def ^{:doc "Haversine ([[haversin]] alias)"} haversine haversin)
(defn hacoversin "Hacoversine" ^double [^double v] (* 0.5 (- 1.0 (FastMath/sin v))))
(defn havercos "Havercosine" ^double [^double v] (* 0.5 (inc (FastMath/cos v))))
(defn hacovercos "Hacovercosine" ^double [^double v] (* 0.5 (inc (FastMath/sin v))))
(defn ahaversin "Arc haversine" ^double [^double v] (FastMath/acos (- 1.0 (* 2.0 v))))
(defn ahacoversin "Arc hacoversine" ^double [^double v] (FastMath/asin (- 1.0 (* 2.0 v))))
(defn ahavercos "Arc havecosine" ^double [^double v] (FastMath/acos (dec (* 2.0 v))))
(defn ahacovercos "Arc hacovercosine" ^double [^double v] (FastMath/asin (dec (* 2.0 v))))
(defn exsec "Exsecant" ^double [^double v] (dec (sec v)))
(defn excsc "Excosecant" ^double [^double v] (dec (csc v)))
(defn aexsec "Arc exsecant" ^double [^double v] (asec (inc v)))
(defn aexcsc "Arc excosecant" ^double [^double v] (acsc (inc v)))
(defn haversine-dist
"Haversine distance `d` for `r=1`"
(^double [[^double lat1 ^double lon1] [^double lat2 ^double lon2]]
(haversine-dist lat1 lon1 lat2 lon2))
(^double [^double lat1 ^double lon1 ^double lat2 ^double lon2]
(* 2.0 (FastMath/asin (FastMath/sqrt (haversin lat1 lon1 lat2 lon2))))))
;; exp and log
(fastmath-proxy :one exp)
(fastmath-proxy :one log)
(fastmath-proxy :one ^{:doc "\\\\(\\ln_{10}{x}\\\\)"} log10)
;; Alias for natural logarithm
(fastmath-proxy :one ln log)
(fastmath-proxy :one log1p)
(fastmath-proxy :one expm1)
(def ^{:const true :tag 'double :doc "\\\\(\\ln{2}\\\\)"} LN2 (log 2.0))
(def ^{:const true :tag 'double :doc "\\\\(\\frac{1}{\\ln{2}}\\\\)"} INV_LN2 (/ LN2))
(def ^{:const true :tag 'double :doc "\\\\(\\frac{\\ln{2}}{2}\\\\)"} LN2_2 (* 0.5 LN2))
(def ^{:const true :tag 'double :doc "\\\\(\\ln{10}\\\\)"} LN10 (log 10.0))
(def ^{:const true :tag 'double :doc "\\\\(\\frac{1}{\\ln{0.5}}\\\\)"} INV_LOG_HALF (/ (log 0.5)))
(def ^{:const true :tag 'double :doc "\\\\(\\ln{0.5}\\\\)"} LOG_HALF (log 0.5))
(def ^{:const true :tag 'double :doc "\\\\(\\ln{\\pi}\\\\)"} LOG_PI (log PI))
(def ^{:const true :tag 'double :doc "\\\\(\\ln{2 \\pi}\\\\)"} LOG_TWO_PI (log TWO_PI))
(defn log1pexp
"log(1+exp(x))"
^double [^double x]
(cond
(< x -745.1332191019412) 0.0
(< x -36.7368005696771) (FastMath/exp x)
(< x 18.021826694558577) (FastMath/log1p (FastMath/exp x))
(< x 33.23111882352963) (+ x (FastMath/exp (- x)))
:else x))
(defn log1mexp
"log(1-exp(x))"
^double [^double x]
(if (< x LOG_HALF)
(FastMath/log1p (- (FastMath/exp x)))
(FastMath/log (- (FastMath/expm1 x)))))
(defn log2mexp
"log(2-exp(x))"
^double [^double x]
(FastMath/log1p (- (FastMath/expm1 x))))
(defn log1psq
"log(1+x^2))"
^double [^double x]
(if (< x 9007199254740992)
(FastMath/log1p (* x x))
(* 2.0 (log x))))
(defn logexpm1
"log(exp(x)-1))"
^double [^double x] (FastMath/log (FastMath/expm1 x)))
;; from julia
(defn- log1pmx-ker
^double [^double x]
(let [r (/ x (+ 2.0 x))
t (* r r)
w (mevalpoly t 6.66666666666666667e-1 4.00000000000000000e-1 2.85714285714285714e-1 2.22222222222222222e-1
1.81818181818181818e-1 1.53846153846153846e-1 1.33333333333333333e-1 1.17647058823529412e-1)
hxsq (* 0.5 x x)]
(- (* r (+ hxsq (* w t))) hxsq)))
(defn log1pmx
"log(1+x)-x"
^double [^double x]
(cond
(not (< -0.7 x 0.9)) (- (FastMath/log1p x) x)
(> x 0.315) (let [u (/ (- x 0.5) 1.5)]
(- (log1pmx-ker u) 9.45348918918356180e-2 (* 0.5 u)))
(> x -0.227) (log1pmx-ker x)
(> x -0.4) (let [u (/ (+ x 0.25) 0.75)]
(+ (log1pmx-ker u) -3.76820724517809274e-2 (* 0.25 u)))
(> x -0.6) (let [u (* (+ x 0.5) 2.0)]
(+ (log1pmx-ker u) -1.93147180559945309e-1 (* 0.5 u)))
:else (let [u (/ (+ x 0.625) 0.375)]
(+ (log1pmx-ker u) -3.55829253011726237e-1 (* 0.625 u)))))
(defn logmxp1
"log(x)-x+1"
^double [^double x]
(cond
(<= x 0.3) (- (inc (FastMath/log x)) x)
(<= x 0.4) (let [u (/ (- x 0.375) 0.375)]
(+ (log1pmx-ker u) -3.55829253011726237e-1 (* 0.625 u)))
(<= x 0.6) (let [u (* (- x 0.5) 2.0)]
(+ (log1pmx-ker u) -1.93147180559945309e-1 (* 0.5 u)))
:else (log1pmx (dec x))))
(defn logaddexp
"log(exp(x)+exp(y))"
^double [^double x ^double y]
(if (< x y)
(+ y (log1pexp (- x y)))
(+ (if-not (Double/isNaN y) x y)
(log1pexp (- y x)))))
(defn logsubexp
"log(abs(exp(x)-exp(y)))"
^double [^double x ^double y]
(+ (max x y)
(log1mexp (- (if (and (== x y)
(or (Double/isFinite x) (neg? x))) 0.0 (FastMath/abs (- x y)))))))
(defn logsumexp
"log(exp(x1)+...+exp(xn))"
^double [xs]
(loop [[^double x & rst] xs
r 0.0
alpha ##-Inf]
(if (<= x alpha)
(let [nr (+ r (FastMath/exp (- x alpha)))]
(if-not (seq rst)
(+ (FastMath/log nr) alpha)
(recur rst nr alpha)))
(let [nr (inc (* r (FastMath/exp (- alpha x))))]
(if-not (seq rst)
(+ (FastMath/log nr) x)
(recur rst nr (double x)))))))
(defn xlogx
"x * log(x)"
^double [^double x]
(if (zero? x) 0.0 (* x (FastMath/log x))))
(defn xlogy
"x * log(y)"
^double [^double x ^double y]
(if (and (zero? x)
(not (Double/isNaN y))) 0.0 (* x (log y))))
(defn xlog1py
"x * log(1+y)"
^double [^double x ^double y]
(if (and (zero? x)
(not (Double/isNaN y))) 0.0 (* x (log1p y))))
(defn cloglog
"log(-log(1-x))"
^double [^double x]
(FastMath/log (- (FastMath/log1p (- x)))))
(defn xexpx
"x * exp(x)"
^double [^double x]
(let [expx (exp x)]
(if (zero? expx) 0.0 (* x expx))))
(defn xexpy
"x * exp(x)"
^double [^double x ^double y]
(let [expy (exp y)]
(if (and (zero? expy)
(not (Double/isNaN x))) 0.0 (* x expy))))
(defn cexpexp
"1-exp(-exp(x))"
^double [^double x]
(- (FastMath/expm1 (- (FastMath/exp x)))))
;; Quick logarithm
(fastmath-proxy :one ^{:doc "Fast and less accurate version of [[log]]."} qlog logQuick)
;; Roots (square and cubic)
(fastmath-proxy :one ^{:doc "\\\\(\\sqrt{x}\\\\)"} sqrt)
(fastmath-proxy :one ^{:doc "\\\\(\\sqrt[3]{x}\\\\)"} cbrt)
;; Quick version of exponential \\(e^x\\)
(fastmath-proxy :one ^{:doc "Quick and less accurate version of [[exp]]."} qexp expQuick)
;; Radians to degrees (and opposite) conversions
(def ^{:const true :tag 'double :doc "\\\\(\\frac{180}{\\pi}\\\\)"} rad-in-deg (/ 180.0 PI))
(def ^{:const true :tag 'double :doc "\\\\(\\frac{\\pi}{180}\\\\)"} deg-in-rad (/ PI 180.0))
(defn radians "Convert degrees into radians." ^double [^double deg] (* deg-in-rad deg))
(defn degrees "Convert radians into degrees." ^double [^double rad] (* rad-in-deg rad))
;; Erf
(erf-proxy :onetwo ^{:doc "Error function. For two arguments return difference between `(erf x)` and `(erf y)`."} erf)
(erf-proxy :one ^{:doc "Complementary error function."} erfc)
(erf-proxy :one ^{:doc "Inverse [[erf]]."} inv-erf erfInv)
(erf-proxy :one ^{:doc "Inverse [[erfc]]."} inv-erfc erfcInv)
;; Gamma
(gamma-proxy :one ^{:doc "Gamma function \\\\(\\Gamma(x)\\\\)"} gamma gamma)
(gamma-proxy :one ^{:doc "Log of Gamma function \\\\(\\ln\\Gamma(x)\\\\)"} log-gamma logGamma)
(gamma-proxy :one ^{:doc "Log of Gamma function \\\\(\\ln\\Gamma(1+x)\\\\)"} log-gamma-1p logGamma1p)
(gamma-proxy :one ^{:doc "Logarithmic derivative of \\\\(\\Gamma\\\\)."} digamma)
(gamma-proxy :one ^{:doc "Derivative of [[digamma]]."} trigamma)
(gamma-proxy :one ^{:doc "\\\\(\\frac{1}{\\Gamma(1+x)}-1\\\\)."} inv-gamma-1pm1 invGamma1pm1)
(gamma-proxy :two ^{:doc "Regularized `gamma` P"} regularized-gamma-p regularizedGammaP)
(gamma-proxy :two ^{:doc "Regularized `gamma` Q"} regularized-gamma-q regularizedGammaQ)
(defn minkowski
"Minkowski's question mark function ?(x)"
^double [^double x]
(loop [it (long 0) p 0.0 q 1.0 r 1.0 s 1.0 d 1.0 y 0.0]
(if (< it 20)
(let [d (* d 0.5)
m (+ p r)
n (+ q s)
p? (< x (/ m n))]
(recur (inc it)
(if p? p m)
(if p? q n)
(if p? m r)
(if p? n s)
d
(if p? y (+ y d))))
(+ y d))))
;; Beta
(beta-proxy :two ^{:doc "Logarithm of Beta function."} log-beta logBeta)
(beta-proxy :three ^{:doc "Regularized `Beta`."} regularized-beta regularizedBeta)
;; BesselJ
(besselj-proxy :two ^{:doc "Bessel J function value for given order and argument."} bessel-j value)
;; jinc-c4 (/ (* PI PI PI PI) 192.0)
;; jinc-c2 (/ (* PI PI) -8.0)
(defn jinc
"Besselj1 devided by `x`"
^double [^double x]
(if (< (FastMath/abs x) 0.002)
(let [x2 (* x x)]
(mevalpoly x2 1.0 -1.2337005501361697 0.5073390158020964))
(let [pix (* PI x)]
(* 2.0 (/ (bessel-j 1 pix) pix)))))
;; I0
(defn I0
"Modified Bessel function of the first kind, order 0."
^double [^double x]
(let [x2 (* x x)
;; i=1
val (inc (/ x2 4))
x2i (* x2 x2)
;; i=2
val (+ val (/ x2i 64))
x2i (* x2i x2)
;; i=3
val (+ val (/ x2i 2304))
x2i (* x2i x2)
;; i=4
val (+ val (/ x2i 147456))
x2i (* x2i x2)
;; i=5
val (+ val (/ x2i 14745600))
x2i (* x2i x2)
;; i=6
val (+ val (/ x2i 2123366400))
x2i (* x2i x2)
;; i=7
val (+ val (/ x2i 416179814400))
x2i (* x2i x2)
;; i=8
val (+ val (/ x2i 106542032486400))
x2i (* x2i x2)]
(+ val (/ x2i 34519618525593600))))
(defn log-I0
"Log of [[I0]]."
^double [^double x]
(FastMath/log (I0 x)))
;; Sinc
(defn sinc
"Sinc function."
^double [^double v]
(let [x (* PI (FastMath/abs v))]
(if (< x 1.0e-5) 1.0
(/ (FastMath/sin x) x))))
;;
(defn sigmoid
"Sigmoid function"
^double [^double x]
(/ (inc (FastMath/exp (- x)))))
(def ^{:doc "Alias for [[sigmoid]]"} logistic sigmoid)
(defn logit
"Logit function"
^double [^double x]
(FastMath/log (/ x (- 1.0 x))))
(defn log2
"Logarithm with base 2.
\\\\(\\ln_2{x}\\\\)"
^double [^double x]
(* (FastMath/log x) INV_LN2))
;; \\(\log_b x\\)
(defn logb
"Logarithm with base `b`.
\\\\(\\ln_b{x}\\\\)"
^double [^double b ^double x]
(/ (FastMath/log x) (FastMath/log b)))
(defn logcosh
"log(cosh(x))"
^double [^double x]
(let [absx (FastMath/abs x)]
(- (+ absx (log1pexp (* -2.0 absx))) LN2)))
;; \\(\log_2 e\\)
(def ^{:const true :tag 'double :doc "\\\\(\\log_{2}{e}\\\\)"} LOG2E (log2 E))
;; \\(\log_{10} e\\)
(def ^{:const true :tag 'double :doc "\\\\(\\log_{10}{e}\\\\)"} LOG10E (log10 E))
;; Powers (normal, quick)
(fastmath-proxy :two pow)
(fastmath-proxy :two ^{:doc "Fast and less accurate version of [[pow]]."} qpow powQuick)
;; Fast version of power, second parameter should be integer
(fastmath-proxy :two ^{:doc "Fast version of pow where exponent is integer."} fpow powFast)
(def ^:private factorial20-table [1 1 2 6 24 120 720 5040 40320 362880 3628800 39916800 479001600
6227020800 87178291200 1307674368000 20922789888000
355687428096000 6402373705728000 121645100408832000
2432902008176640000])
(defn factorial20
"Factorial table up to 20!"
^long [^long n]
(factorial20-table n))
(defn factorial "Factorial"
^double [^long n]
(if (< n 21)
(factorial20-table n)
(exp (log-gamma (double (inc n))))))
(defn ^{:doc "Log factorial, alias to log-gamma"} log-factorial
^double [^long x] (log-gamma (double (inc x))))
(defn combinations
"Binomial coefficient (n choose k)"
^double [^long n ^long k]
(let [k (min k (- n k))]
(cond
(neg? k) 0.0
(zero? k) 1.0
(< k 30) (loop [j (long 2)
r (double n)]
(if (> j k)
r
(recur (inc j) (* r (/ (inc (- n j)) (double j))))))
:else (exp (- (- (ln (inc n)))
(log-beta (inc (- n k)) (inc k)))))))
(defn log-combinations
"Log of binomial coefficient (n choose k)"
^double [^long n ^long k]
(let [k (min k (- n k))]
(cond
(neg? k) ##-Inf
(zero? k) 0.0
(one? k) (ln n)
(< n k) ##-Inf
(== n k) 0.0
:else (- (- (ln (inc n)))
(log-beta (inc (- n k)) (inc k))))))
;; Square and cubic
(defn sq "Same as [[pow2]]. \\\\(x^2\\\\)" ^double [^double x] (* x x))
(defn pow2 "Same as [[sq]]. \\\\(x^2\\\\)" ^double [^double x] (* x x))
(defn pow3 "\\\\(x^3\\\\)" ^double [^double x] (* x (* x x)))
(defn cb "\\\\(x^3\\\\)" ^double [^double x] (* x (* x x)))
(defn safe-sqrt
"Safe sqrt, for value <= 0 result is 0.
\\\\(
\\left\\\\{
\\begin{array}{lr}
0 & : x \\leq 0\\\\\\\\
\\sqrt{x} & : x > 0
\\end{array}
\\\\right.
\\\\)"
^double [^double value]
(if (neg? value) 0.0 (FastMath/sqrt value)))
;; Approximated sqrt via binary operations (error 1.0E-2)
(fastmath-proxy :one ^{:doc "Approximated [[sqrt]] using binary operations with error `1.0E-2`."} qsqrt sqrtQuick)
(fastmath-proxy :one ^{:doc "Inversed version of [[qsqrt]]. Quick and less accurate."} rqsqrt invSqrtQuick)
(defn hypot
"Hypot.
See also [[hypot-sqrt]]."
(^double [^double x ^double y]
(FastMath/hypot x y))
(^double [^double x ^double y ^double z]
(FastMath/hypot x y z)))
(defn hypot-sqrt
"Hypot, sqrt version: \\\\(\\sqrt{x^2+y^2}\\\\) or \\\\(\\sqrt{x^2+y^2+z^2}\\\\).
Should be faster than [[hypot]]."
(^double [^double x ^double y]
(FastMath/sqrt (+ (* x x) (* y y))))
(^double [^double x ^double y ^double z]
(FastMath/sqrt (+ (* x x) (* y y) (* z z)))))
;; distance
(defn dist
"Euclidean distance between points `(x1,y1)` and `(x2,y2)`. See [[fastmath.vector]] namespace to see other metrics which work on vectors."
(^double [[^double x1 ^double y1] [^double x2 ^double y2]] (dist x1 y1 x2 y2))
(^double [^double x1 ^double y1 ^double x2 ^double y2]
(FastMath/sqrt (+ (sq (- x2 x1)) (sq (- y2 y1))))))
(defn qdist
"Quick version of Euclidean distance between points. [[qsqrt]] is used instead of [[sqrt]]."
(^double [[^double x1 ^double y1] [^double x2 ^double y2]] (qdist x1 y1 x2 y2))
(^double [^double x1 ^double y1 ^double x2 ^double y2]
(FastMath/sqrtQuick (+ (sq (- x2 x1)) (sq (- y2 y1))))))
;; Rounding functions
(defn floor
"\\\\(\\lfloor x \\rfloor\\\\). See: [[qfloor]].
Rounding is done to a multiply of scale value (when provided)."
(^double [^double x] (FastMath/floor x))
(^double [^double x ^double scale] (* (FastMath/floor (/ x scale)) scale)))
(defn ceil
"\\\\(\\lceil x \\rceil\\\\). See: [[qceil]].
Rounding is done to a multiply of scale value (when provided)."
(^double [^double x] (FastMath/ceil x))
(^double [^double x ^double scale] (* (FastMath/ceil (/ x scale)) scale)))
(defn round "Round to `long`. See: [[rint]], [[qround]]." ^long [^double x] (FastMath/round x))
(defn rint
"Round to `double`. See [[round]], [[qround]].
Rounding is done to a multiply of scale value (when provided)."
(^double [^double x] (FastMath/rint x))
(^double [^double x ^double scale] (* (FastMath/rint (/ x scale)) scale)))
(defn round-even
"Round evenly (like in round in R), IEEE / IEC rounding"
^long [^double x] (FastMath/roundEven x))
(primitivemath-proxy :one ^{:doc "Fast version of [[floor]]. Returns `long`. See: [[floor]]."} qfloor fastFloor)
(primitivemath-proxy :one ^{:doc "Fast version of [[ceil]]. Returns `long`. See: [[ceil]]."} qceil fastCeil)
(primitivemath-proxy :one ^{:doc "Fast version of [[round]]. Returns `long`. See: [[rint]], [[round]]."} qround fastRound)
(fastmath-proxy :two ^{:doc "From `FastMath` doc: returns dividend - divisor * n,
where n is the mathematical integer closest to dividend/divisor. Returned value in `[-|divisor|/2,|divisor|/2]`"} remainder)
(defn abs "\\\\(|x|\\\\) - `double` version. See [[iabs]]." ^double [^double x] (FastMath/abs x))
(defn iabs "\\\\(|x|\\\\) - `long` version. See [[abs]]." ^long [^long x] (if (neg? x) (- x) x))
(defn trunc
"Truncate fractional part, keep sign. Returns `double`."
^double [^double v] (if (neg? v) (ceil v) (floor v)))
(defn itrunc
"Truncate fractional part, keep sign. Returns `long`."
^long [^double v] (if (neg? v) (qceil v) (qfloor v)))
;; return approximate value
(defn approx
"Round `v` to specified (default: 2) decimal places. Be aware of `double` number accuracy."
(^double [^double v] (Precision/round v (int 2)))
(^double [^double v ^long digits] (Precision/round v (int digits))))
(defn approx-eq
"Checks equality approximately. See [[approx]]."
([^double a ^double b] (== (approx a) (approx b)))
([^double a ^double b ^long digits] (== (approx a digits)
(approx b digits))))
(defn delta-eq
"Checks equality for given absolute accuracy (default `1.0e-6`).
Version with 4-arity accepts absolute and relative accuracy."
([^double a ^double b] (delta-eq a b 1.0e-6))
([^double a ^double b ^double accuracy]
(< (abs (- a b)) accuracy))
([^double a ^double b ^double abs-tol ^double rel-tol]
(< (abs (- a b)) (max abs-tol (* rel-tol (max (abs a) (abs b)))))))
(def ^{:doc "Alias for [[approx-eq]]"} approx= approx-eq)
(def ^{:doc "Alias for [[delta-eq]]"} delta= delta-eq)
(defn near-zero?
"Checks if given value is near zero with absolute (default: `1.0e-6`) and/or relative (default `0.0`) tolerance."
([^double x] (near-zero? x 1.0e-6))
([^double x ^double abs-tol] (< (abs x) abs-tol))
([^double x ^double abs-tol ^double rel-tol]
(let [ax (abs x)] (< ax (max abs-tol (* rel-tol ax)) ))))
(defn frac
"Fractional part, always returns values from 0.0 to 1.0 (exclusive). See [[sfrac]] for signed version."
^double [^double v] (abs (- v (unchecked-long v))))
(defn sfrac
"Fractional part, always returns values from -1.0 to 1.0 (exclusive). See [[frac]] for unsigned version."
^double [^double v] (- v (trunc v)))
;; Find power of 2 exponent for double number where
;; \\(2^(n-1)\leq x\leq 2^n\\)
;; where n-1 is result of `low-2-exp` and n is result of `high-2-exp`
;; `(low-2-exp TWO_PI) => 2` \\(2^2\eq 4\leq 6.28\\)
;; `(high-2-exp TWO_PI) => 3` \\(6.28\leq 2^3\eq 8\\)
(defn low-2-exp
"Find greatest exponent (power of 2) which is lower or equal `x`. See [[high-2-exp]]."
^long [^double x] (-> x log2 floor unchecked-long))
(defn high-2-exp
"Find lowest exponent (power of 2) which is greater or equal `x`. See [[low-2-exp]]."
^long [^double v] (-> v log2 ceil unchecked-long))
(defn low-exp
"Find greatest exponent for base `b` which is lower or equal `x`. See also [[high-exp]]."
^long [^double b ^double x] (->> x (logb b) floor unchecked-long))
(defn high-exp
"Find lowest exponent for base `b` which is higher or equal`x`. See also [[low-exp]]."
^long [^double b ^double x] (->> x (logb b) ceil unchecked-long))
(defn round-up-pow2
"Round long to the next power of 2"
^long [^long v]
(as-> (dec v) v
(bit-or v (>> v 1))
(bit-or v (>> v 2))
(bit-or v (>> v 4))
(bit-or v (>> v 8))
(bit-or v (>> v 16))
(bit-or v (>> v 32))
(inc v)))
(defn next-double
"Next double value. Optional value `delta` sets step amount."
(^double [^double v]
(FastMath/nextUp v))
(^double [^double v ^long delta]
(nth (iterate next-double v) delta)))
(defn prev-double
"Next double value. Optional value `delta` sets step amount."
(^double [^double v]
(FastMath/nextDown v))
(^double [^double v ^long delta]
(nth (iterate prev-double v) delta)))
(defn double-high-bits
"Returns high word from double as bits"
^long [^double v]
(bit-and (>>> (Double/doubleToRawLongBits v) 32) 0xffffffff))
(defn double-low-bits
"Returns low word from double as bits"
^long [^double v]
(bit-and (Double/doubleToRawLongBits v) 0xffffffff))
(defn double-bits
"Returns double as 64-bits (long)"
^long [^double v]
(Double/doubleToRawLongBits v))
(defn bits->double
"Convert 64 bits to double"
^double [^long v]
(Double/longBitsToDouble v))
(defn double-exponent
"Extract exponent information from double"
^long [^double v]
(FastMath/getExponent v))
(defn double-significand
"Extract significand from double"
^long [^double v]