Аналитическое дифференцирование функций

  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
(define op car)
(define args cdr)
(define first-arg cadr)
(define second-arg caddr)
(define other-args cddr)
(define (bisection func array)
(cons (filter func array) (filter (lambda (el) (not (func el))) array)))
(define (has-0 L)
(cond ((null? L) #f)
((and (number? (car L)) (= (car L) 0)) #t)
(else (has-0 (cdr L)))))
(define (constant? exp var)
(and (not (pair? exp))
(not (eq? exp var))))
(define (same-var? exp var)
(and (not (pair? exp))
(eq? exp var)))
(define (sum? exp)
(and (pair? exp)
(eq? (op exp) '+)))
(define (make-sum . a)
(let ((a (filter (lambda (n) (not (and (number? n) (= n 0)))) a)))
(cond ((null? (cdr a)) (car a))
(else
(let ((numbers (bisection number? a)))
(cond ((= 0 (length (cdr numbers)))
(apply + (car numbers)))
(else
(let ((sums (bisection sum? a)))
(cons '+ (append (reduce append '() (map args (car sums))) (cdr sums)))))))))))
(define (diff? exp)
(and (pair? exp)
(eq? (op exp) '-)))
(define (make-diff a1 a2)
(cond ((and (number? a1) (number? a2))
(- a1 a2))
((and (number? a1) (= a1 0))
(make-product -1 a2))
((and (number? a2) (= a2 0))
a1)
(else (list '- a1 a2))))
(define (product? exp)
(and (pair? exp)
(eq? (op exp) '*)))
(define (make-product . m)
(let ((m (filter (lambda (n) (not (and (number? n) (= n 1)))) m)))
(cond ((null? m) 1)
((null? (cdr m)) (car m))
((has-0 m) 0)
(else
(let ((numbers (bisection number? m)))
(cond ((= 0 (length (cdr numbers)))
(apply * (car numbers)))
(else
(let ((products (bisection product? m)))
(cons '* (append (reduce append '() (map args (car products))) (cdr products)))))))))))
(define (division? exp)
(and (pair? exp)
(eq? (car exp) '/)))
(define (make-division d1 d2)
(cond ((and (number? d2) (= d2 1)) d1)
((and (number? d2) (= d1 0)) 0)
(else (list '/ d1 d2))))
(define (func? exp)
(not (or (not (pair? exp))
(eq? (op exp) '+)
(eq? (op exp) '-)
(eq? (op exp) '*)
(eq? (op exp) '/))))
(define (make-func f1 f2 var)
(make-product (derive f2 var)
(cond ((eq? f1 'sin)
(list 'cos f2))
((eq? f1 'cos)
(make-product -1 (list 'sin f2)))
((eq? f1 'ln)
(make-division 1 f2))
((eq? f1 'sqr)
(make-product 2 f2)))))
(define (derive exp var)
(cond ((constant? exp var) 0)
((same-var? exp var) 1)
((sum? exp)
(apply make-sum (map (lambda (exp) (derive exp var)) (args exp))))
((diff? exp)
(make-diff (derive (first-arg exp) var)
(derive (second-arg exp) var)))
((product? exp)
(cond ((not (pair? (cddr exp)))
(make-sum (make-product (first-arg exp) (derive (second-arg exp) var))
(make-product (second-arg exp) (derive (first-arg exp) var))))
(else
(make-sum (make-product (first-arg exp) (derive (apply make-product (other-args exp)) var))
(make-product (apply make-product (other-args exp)) (derive (first-arg exp) var))))))
((division? exp)
(make-division (make-diff (make-product (derive (first-arg exp) var)
(second-arg exp))
(make-product (derive (second-arg exp) var)
(first-arg exp)))
(make-func 'sqr (make-product (first-arg exp) (second-arg exp)) var)))
((func? exp)
(make-func (op exp) (first-arg exp) var))))
; testing
(define foo
'(sin (ln (sqr x))))
(define bar
'(+ x x x x x))
(define baz
'(/ (* x x x) (ln x)))
(derive baz 'x)