我有一个工作函数,该函数针对给定变量查找给定函数的导数。我也有一个简化函数(define(简化expr)),该函数接受微分函数(define(diff x expr))的输出并将其简化为易于阅读的形式。我正在尝试实现一个允许用户输入的系统他们想要为其找到导数并返回简化导数的表达式。为了使这项工作有效,我需要在第六行代码中进行哪些更改?
#racket
(display "Please enter an expression that you would like to differntiate: ")
(define expr (read))
(display expr)
(display " the derivative is ")
(display (simplify (diff 'x expr)))
(newline)
我的simplify
和diff
函数
#lang racket/base
(define (diff x expr)
; differentiation function which gives derivative given a variable
(cond ((not (list? expr)) (if (equal? x expr) 1 0)) ; d/dx(x) = 1, d/dx(c) = 0
(else
(let ((operation (car expr)) ; expr is (operation u v)
(u (cadr expr))
(v (caddr expr)))
(case operation
((+) (list '+ (diff x u) (diff x v))) ; d/dx (u+v) = d/dx(u) + d/dx(v)
((-) (list '- (diff x u) (diff x v))) ; d/dx (u-v) = d/dx(u) - d/dx(v)
((*) (list '+ ; d/dx (u*v) = u * d/dxuv) + v * d/dx(u)
(list '* u (diff x v))
(list '* v (diff x u))))
((/) (list '/ (list '- ; power rule
(list '* v (diff x u))
(list '* u (diff x v)))))
((^) (list '* v (list '*
(list '^ u (- v 1))
(diff x u)))))))))
(define (simplify expr)
(if (not (list? expr))
expr
(let ((operation (car expr))
(a (simplify (cadr expr)))
(b (simplify (caddr expr))))
(case operation
((+) (cond ((and (number? a) (number? b)) (+ a b)) ;; use Racket's ability to add
((number? a)
(cond ((zero? a) (simplify b))
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else (list operation (simplify a) (simplify b)))))
((-) (cond ((and (number? a) (number? b)) (- a b)) ;; use Racket's ability to subtract
((number? a)
(cond ((zero? a) (- (simplify b)))
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else (list operation (simplify a) (simplify b)))))
((*) (cond ((and (number? a) (number? b)) (* a b)) ;; use Racket's ability to mulitpy
((number? a)
(cond ((zero? a) 0)
((= a 1) (simplify b))
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) 0)
((= b 1) (simplify a))
(else (list operation (simplify a)(simplify b)))))
(else (list operation (simplify a) (simplify b)))))
((/) (cond ((and (number? a) (number? b)) (/ a b)) ;; use Racket's ability to divide
((number? a)
(cond ((zero? a) 0)
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) (error "Divison by 0, statement undefined!"))
((= b 1) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else
(list operation (simplify a) (simplify b)))))
((^) (cond ((and (number? a) (number? b)) (expt a b)) ;; use Racket's ability to exponentiate
((number? a)
(cond ((= a 1) 1) ;; ((zero? a) 0) ;; depends on b [b < 0 then undefined]
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) 1) ;; depends on a [a = 0 then undefined]
((= b 1) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else (list operation (simplify a) (simplify b)))))))))