-
Notifications
You must be signed in to change notification settings - Fork 5
/
derivative.sls
71 lines (42 loc) · 1.33 KB
/
derivative.sls
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
#!r6rs
(library (mpl derivative)
(export derivative)
(import (mpl rnrs-sans)
(mpl misc)
(mpl arithmetic)
(mpl sin)
(mpl cos)
(mpl tan)
(mpl log)
(mpl contains))
(define (derivative u x)
(cond ( (equal? u x) 1 )
( (power? u)
(let ((v (base u))
(w (exponent u)))
(+ (* w
(^ v (- w 1))
(derivative v x))
(* (derivative w x)
(^ v w)
(log v)))) )
( (sum? u)
(let ( (v (list-ref u 1)) )
(let ( (w (- u v)) )
(+ (derivative v x) (derivative w x)))) )
( (product? u)
(let ( (v (list-ref u 1)) )
(let ( (w (/ u v)) )
(+ (* (derivative v x) w)
(* v (derivative w x))))) )
( (sin? u)
(let ( (v (list-ref u 1)) )
(* (cos v) (derivative v x))) )
( (cos? u)
(let ( (v (list-ref u 1)) )
(* (- (sin v)) (derivative v x))) )
( (tan? u)
(let ( (v (list-ref u 1)) )
(* (^ `(sec ,v) 2) (derivative v x))) )
( (free? u x) 0 )
( else `(derivative ,u ,x) ))))