packages feed

egison-3.6.0: lib/math/analysis/derivative.egi

;;;;;
;;;;;
;;;;; Differentiation
;;;;;
;;;;;

(define $d/d
  (lambda [$f $x]
    (match f math-expr
      {[?simple-term?
        (match [x f] [symbol-expr symbol-expr]
          {[[<symbol $name> <symbol !,name>] 0]
           [[<symbol $name> <symbol ,name>] 1]
           [[_ (,exp $g)] (* (exp g) (d/d g x))]
           [[_ (,** $g $h)] (* f (d/d (* (log g) h) x))]
           [[_ (,log $g)] (* (/ 1 g) (d/d g x))]
           [[_ (,cos $g)] (* (* -1 (sin g)) (d/d g x))]
           [[_ (,sin $g)] (* (cos g) (d/d g x))]
           [[_ (,sqrt $g)] (* (/ 1 (* 2 (sqrt g))) (d/d g x))]
           })]
       [?term?
        (match f term-expr
          {[<term _ <nil>> 0]
           [<term ,1 <ncons $n $fx <nil>>> (* n (** fx (- n 1)) (d/d fx x))]
           [<term $a <ncons $n $fx $ts>>
            (+ (* a
                  (d/d (** fx n) x)
                  (foldl *' 1 (map 2#(**' %1 %2) ts)))
               (* a
                  (** fx n)
                  (d/d (foldl *' 1 (map 2#(**' %1 %2) ts)) x)))]
           })]
       [?polynomial?
        (match f poly-expr
          {[<plus $ts> (sum (map (d/d $ x) ts))]})]
       [_
        (match f math-expr
          {[<div $p1 $p2>
            (let {[$p1' (d/d p1 x)]
                  [$p2' (d/d p2 x)]}
              (/ (- (* p1' p2) (* p2' p1)) (** p2 2)))]
           })]
       })))

(define $d/dx (d/d $ x)) ; just a syntax sugar
(define $d/dy (d/d $ y)) ; just a syntax sugar
(define $d/dz (d/d $ z)) ; just a syntax sugar

(define $taylor-expansion
  (lambda [$f $x $a]
    (map2 *
          (map 1#(/ (** (- x a) %1) (fact %1)) nats0)
          (map (substitute {[x a]} $) (iterate (d/d $ x) f)))))

(define $maclaurin-expansion (taylor-expansion $ $ 0))