-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsec1.3.4.clj
120 lines (85 loc) · 2.2 KB
/
sec1.3.4.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
;; Ex. 1.40
(defn cubic [a b c]
(fn [x] (+ (* x x x)
(* a x x)
(* b x)
c)))
;; Ex. 1.41
(defn dbl [f] (fn [x] (f (f x))))
(((dbl (dbl dbl)) inc) 5) ; => 21
;; Ex. 1.42
(defn compose [f g]
(fn [x] (f (g x))))
;; Ex. 1.43
(defn repeated [f n]
(fn [x]
(loop [n n res x]
(if (zero? n)
res
(recur (dec n) (f res))))))
(defn repeated [f n]
(if (zero? n)
identity
(comp f (repeated f (dec n)))))
(defn repeated [f n]
(accumulate comp
identity
(constantly f)
1
inc
n))
(defn repeated [f n] #(nth (iterate f %) n))
;; Ex. 1.44
(def dx 0.0001)
(defn average [& nums]
(/ (apply + nums) (count nums)))
(defn smooth [f]
(fn [x]
(average (f (+ x dx))
(f x)
(f (- x dx)))))
(defn n-fold-smooth [f n]
((repeated smooth n) f))
;; Ex. 1.45
; Experimenting with the required number of average-damps shows, that the
; n-th root requires log_2(n) repeated applications of AVERAGE_DAMP.
(use 'clojure.contrib.generic.math-functions)
(def tolerance 0.00001)
(defn fixed-point [f initial-guess]
(letfn [(close-enough? [a b]
(< (abs (- a b)) tolerance))]
(loop [guess initial-guess]
(let [nxt (f guess)]
(if (close-enough? guess nxt)
nxt
(recur nxt))))))
(defn fixed-point-of-transform [g transform guess]
(fixed-point (transform g) guess))
(defn average-damp [f]
(fn [x] (average x (f x))))
(defn log2 [x]
(/ (log x) (log 2)))
(defn nth-root [n x]
(fixed-point-of-transform
(fn [y] (/ x (pow y (dec n))))
(repeated average-damp (log2 n))
1.0))
;; Ex. 1.46
(defn iterative-improve [good-enough? improve]
(fn [guess]
(let [nxt (improve guess)]
(if (good-enough? guess nxt)
nxt
(recur nxt)))))
(defn sqrt [x]
((iterative-improve (fn [g1 g2]
(< 0.999 (/ g1 g2) 1.001))
(fn [g]
(average g (/ x g))))
1.0))
(defn fixed-point [f guess]
((iterative-improve (fn [x y]
(< (abs (- x y))
tolerance))
f)
guess))