Ladies_Man

Символьное дифференцирование

Dec 26th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 4.05 KB | None | 0 0
  1. (define pi (acos -1))
  2.  
  3. (define ^ expt)
  4. (define (plus args x)
  5.   (define (iter S l)
  6.      (if (null? l)
  7.          S
  8.          (let ((k (dif (car l) x)))
  9.            (if (equal? k 0)
  10.                (if (and (null? S) (null? (cdr l)))
  11.                    (list 0)
  12.                    (iter S (cdr l)))
  13.                (iter (append S (list k)) (cdr l))) )))
  14.   (let ((l (iter '() args)))
  15.     (if (null? (cdr l))
  16.         (car l)
  17.         (cons '+ l))))
  18.  
  19. (define (minus args x)
  20.   (let ((l (if (not (null? (cdr args))) (dif (car args) x) 0))
  21.         (r (if (not (null? (cdr args))) (plus (cdr args) x) (dif (car args) x)))
  22.        )
  23.     (if (equal? l 0)
  24.         (if (equal? r 0)
  25.             0
  26.             (list '- r)
  27.         )
  28.         (if (equal? r 0)
  29.             l
  30.             (list '- l r)
  31.         )
  32.     )
  33.   )
  34. )
  35.  
  36. (define (mult args x)
  37.   (define (iter i S n)
  38.     (define (probeg j l S)
  39.       (if (> j n)
  40.           S
  41.           (let ((k (if (= i j)
  42.                        (dif (car l) x)
  43.                        (car l)
  44.                    )
  45.                ))
  46.             (case k (0 0)
  47.                     (1 (if (and (null? S) (null? (cdr l)))
  48.                            (list 1)
  49.                            (probeg (+ j 1) (cdr l) S)
  50.                        )
  51.                     )
  52.                     (else (probeg (+ j 1) (cdr l) (append S (list k))))
  53.             )
  54.           )
  55.       )                
  56.     )
  57.     (if (> i n)
  58.         S
  59.         (let ((k (probeg 1 args '())))
  60.           (if (equal? k 0)
  61.               (if (and (null? S) (= i n))
  62.                   (list 0)
  63.                   (iter (+ i 1) S n)
  64.               )
  65.               (if (= (length k) 1)
  66.                   (iter (+ i 1) (append S k) n)
  67.                   (iter (+ i 1) (append S (list (cons '* k))) n)
  68.               )
  69.           )
  70.         )
  71.     )
  72.   )
  73.   (let ((l (iter 1 '() (length args))))
  74.     (if (null? (cdr l))
  75.         (car l)
  76.         (cons '+ l)
  77.     )
  78.   )
  79. )
  80.  
  81. (define (div args x)
  82.   (let* ((l (if (not (null? (cdr args))) (dif (car args) x) 0))
  83.         (r (if (not (null? (cdr args))) (mult (cdr args) x) (dif (car args) x)))
  84.         (l (if (list? l) l (list '* l)))
  85.         (r (if (list? r) r (list '* r)))
  86.        )
  87.     (if (equal? l (list '* 0))
  88.         (if (equal? r (list '* 0))
  89.             0
  90.             (list '/ (list '- (append r (list (car args)))) (append (cons '^ (cdr args)) (list 2)))
  91.         )
  92.         (if (equal? r (list '* 0))
  93.             (list '/ (append l (cdr args)) (append (cons '^ (cdr args)) (list 2)))
  94.             (list '/ (list '- (append l (cdr args)) (append r (list (car args)))) (append (cons '^ (cdr args)) (list 2)))
  95.         )
  96.     )
  97.   )
  98. )
  99.  
  100. (define (exptdif args x)
  101.  
  102.   (let ((a (car args))
  103.         (b (cadr args))
  104.        )
  105.     (if (number? b)
  106.         (list '* b (dif a x) (list 'expt a (- b 1)))
  107.         (list '* (list 'expt a (list '- b 1)) (list '+ (list '* b (dif a x))
  108.                  (list '* a (list 'log a) (dif b x))))
  109.     )
  110.   )
  111. )
  112. (define (dif f x)
  113.   (cond ((equal? f x) 1)
  114.         ((number? f) 0)
  115.         ((list? f)
  116.           (let ((op (car f))
  117.                (args (cdr f))
  118.                )
  119.                (case op (+ (plus args x))
  120.                         (- (minus args x))
  121.                         (* (mult args x))
  122.                         (/ (div args x))
  123.                         (expt (exptdif args x))
  124.                         (^ (exptdif args x))
  125.                         (sin (list '* (cons 'cos args)  (dif (car args) x)))
  126.                         (cos (list '* (list '- (cons 'sin args)) (dif (car args) x)))
  127.                         (log (list '/ (dif (car args) x) (car args)))
  128.                         (else 'unknown)
  129.                )
  130.           )
  131.         )
  132.   )
  133. )
  134.  
  135. (define (derivative l)
  136.    (dif l 'x)
  137. )
  138.  
  139. (define (evaling l x value)
  140.   (eval
  141.    (list 'let (list (list x value)) l)
  142.    (interaction-environment)
  143.    )
  144. )
  145. (define (dift f x value)
  146.   (evaling (dif f x) x value)
  147. )
  148.  
  149. (define (tangent f x value)
  150.   (list '+ (list '* (dift f x value) (list '- 'x value)) (evaling f x value))
  151. )#include <stdio.h>
Advertisement
Add Comment
Please, Sign In to add comment