[数式処理][Scheme] 微分する

複雑な微分をするときは計算機に任せた方が良いように思う。

Scheme で微分するには次のようにする。

(define (deriv exp var)
  (cond ((eq? exp var) 1)
	((not (pair? exp)) 0)
	((eq? (car exp) '-)
	 `(- ,(deriv (cadr exp) var)))
	((eq? (car exp) '+)
	 `(+ ,(deriv (cadr exp) var) ,(deriv (caddr exp) var)))
	((eq? (car exp) '*)
	 `(+ (* ,(deriv (cadr exp) var)
		,(caddr exp))
	     (* ,(cadr exp)
		,(deriv (caddr exp) var))))
	((eq? (car exp) 'expt)
	 (let ((g (cadr exp))
	       (n (caddr exp)))
	   `(* (* ,n (expt ,g ,(- n 1))) ,(deriv g var))))
	((eq? (car exp) 'sin)
	 `(* (cos ,(cadr exp)) ,(deriv (cadr exp) var)))
	((eq? (car exp) 'cos)
	 `(* (- (sin ,(cadr exp))) ,(deriv (cadr exp) var)))
        ((eq? (car exp) 'tan)
         `(* (+ 1 (expt ,exp 2)) ,(deriv (cadr exp) var)))
	(else (error "not yet."))))
> (deriv '(expt (* (sin x) (cos x)) 1/2)) 'x)
=> (* (* 1/2
         (expt (* (sin x) (cos x)) -1/2))
      (+ (* (* (cos x) 1)
	    (cos x))
         (* (sin x)
	    (* (- (sin x)) 1))))

`-‘ 演算子は単項演算子としてのみ使える。


Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/category.php on line 383
This entry was posted in コンピュータ and tagged , , by bugyo. Bookmark the permalink.

Deprecated: Creation of dynamic property WP_Query::$comments_by_type is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/comment-template.php on line 1528

Leave a Reply

Your email address will not be published. Required fields are marked *

*