-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathad_test.clj
141 lines (101 loc) · 3.3 KB
/
ad_test.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
(ns examples.ad-test
(:require [geex.common :as lib]
[geex.core :as core]
[geex.java :as java]
[clojure.test :refer :all]))
;;;------- Constructing ad numbers -------
;; Forward-mode automatic differentiation using dual numbers
(defn ad [x dx]
{:x x :dx dx})
;; An automatically differentiable number representing the variable
;; we are differentiating. Its derivative is 1.0
(defn variable [x]
(ad x 1.0))
;; A constant has derivative 0.0
(defn constant [x]
(ad x 0.0))
;;;------- Common operations -------
;; These functions take automatically differentiable numbers
;; and return new automatically differentiable numbers
(defn add [a b]
{:x (lib/+ (:x a) (:x b))
:dx (lib/+ (:dx a) (:dx b))})
(defn sub [a b]
{:x (lib/- (:x a) (:x b))
:dx (lib/- (:dx a) (:dx b))})
(defn mul [a b]
{:x (lib/* (:x a) (:x b))
:dx (lib/+ (lib/* (:x a) (:dx b))
(lib/* (:dx a) (:x b)))})
(defn sqr [x]
(mul x x))
(defn div [a b]
{:x (lib// (:x a) (:x b))
:dx (lib// (lib/- (lib/* (:dx a) (:x b))
(lib/* (:x a) (:dx b)))
(lib/sqr (:x b)))})
(defn pow [x n]
{:pre [(number? n)]}
{:x (lib/pow (:x x) n)
:dx (lib/* n (lib/pow (:x x) (dec n)))})
;;;------- Basic tests of operations -------
(deftest test-the-ops
(is (= {:x 7 :dx 2.0}
(java/eval (add (variable 3) (variable 4)))))
(is (= {:x -1 :dx 0.0}
(java/eval
(sub (variable 3) (variable 4)))))
(is (= (java/eval (pow (variable 3.0) 2.0))
(java/eval (sqr (variable 3.0)))))
(is (= {:x 9
:dx 6.0}
(java/eval
(sqr (variable 3))))))
;;;------- Computing the square root -------
;; using Newton-Raphson
;; Let f(x) = x^2 - k
;; The square root of k is th solution of f(x) = 0
;; f'(x) = 2x
;; We will do this: x_{n+1} = x_{n} - (x_{n}^2 - k)/2*x_{n}
;; This is a single iteration in the Newton-Raphson algorithm
(defn sqrt-iteration [k x]
(sub x (div (sub (sqr x) k)
(mul (constant 2) x))))
;; This performs n iterations
(defn iterate-sqrt [n k x]
(last (take n (iterate (partial sqrt-iteration k) x))))
;; This is a loop-unrolled implementation of the square root
(java/typed-defn sqrt-with-derivative [Double/TYPE x]
(iterate-sqrt 10 (variable x) (constant x)))
;; This is a loop-based implementation of the square root
(defn iterate-sqrt2 [n k x]
(lib/iterate-while
[(lib/wrap 0) (add x (variable (lib/wrap 0.0)))]
(fn [[counter est]]
[(lib/inc counter)
(sqrt-iteration k est)])
(fn [[counter _]]
(lib/< counter 10))))
;; Using a loop
(java/typed-defn
sqrt-with-derivative2 [Double/TYPE x]
;;(core/set-flag! :disp :disp-time)
(second
(iterate-sqrt2 13 (variable x) (constant x))))
(defn expected-sqrt [x]
{:x (Math/sqrt x)
:dx (/ 0.5 (Math/sqrt x))})
(defn aeq [a b]
(< (Math/abs (- a b)) 1.0e-6))
(defn almost-eq [a b]
(and (aeq (:x a) (:x b))
(aeq (:dx a) (:dx b))))
(deftest sqrt-test
(is (aeq 3.34 3.340000003))
(is (not (aeq 3.34 3.3403)))
(is (map? (java/eval (sqrt-iteration (variable 2)
(constant 3)))))
(is (almost-eq (sqrt-with-derivative 2.0)
(expected-sqrt 2.0)))
(is (almost-eq (sqrt-with-derivative2 2.0)
(expected-sqrt 2.0))))