packages feed

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

;;;;;
;;;;;
;;;;; Integration
;;;;;
;;;;;

(define $Sd
  (lambda [$x $f]
    (match f math-expr
      {[?simple-term?
        (match [x f] [symbol-expr symbol-expr]
          {[[<symbol $name> <symbol !,name>] (* f x)]
           [[<symbol $name> <symbol ,name>] (* (/ 1 2) x^2)]
           [[_ (,exp ,x)] (exp x)]
           [[_ (,cos ,x)] (sin x)]
           [[_ (,sin ,x)] (* -1 (cos x))]
           [[_ (,log ,x)] (multSd x 1 (log x))]
           [[_ ($f $y)] (substitute {[tmpvar y]} (Sd tmpvar (* (f tmpvar) (d/d (inverse tmpvar y x) tmpvar))))]
           [[_ (,** $a ,x)] (/ (** a x) (log a))]
           [[_ (,** $a $y)] (substitute {[tmpvar y]} (Sd tmpvar (* (** a tmpvar) (d/d (inverse tmpvar y x) tmpvar))))]
           [[_ _] (Sd' x f)]
           })]
       [?term?
        (match f term-expr
          {[<term $a <ncons $n ,x $ts>>
            (let {[$b (foldl *' a (map 2#(**' %1 %2) ts))]}
              (if (contain-symbol? x b)
                (Sd' x f)
                (/ (* b (** x (+ n 1))) (+ n 1))))]
           [<term $a $ts>
            (let {[[$fxs $cs] (partition (contain-symbol? x $) (from-assoc ts))]}
              (match fxs (list math-expr)
                {[<nil> (* f x)]
                 [<cons $fx <nil>> (* a (Sd x fx) (foldl *' 1 cs))]
                 [_ (Sd' x f)]}))]})]
       [?polynomial?
        (match f poly-expr
          {[<plus $ts> (sum (map (Sd x $) ts))]})]
       [_
        (match f math-expr
          {[<div $p1 $p2>
            (match (coefficients p2 x) (list math-expr)
              {[<cons $a <nil>> (/ (Sd x p1) a)]
               [_ (Sd' x f)]})]})]
       })))

(define $multSd
  (lambda [$x $f $g]
    (let {[$F (Sd x f)]}
      (- (* F g)
         (Sd x (* F (d/d g x)))))))

(define $Sd'
  (lambda [$x $f]
    (to-math-expr <Apply Sd (map from-math-expr {x f})>)))

(define $dSd
  (lambda [$x $a $b $f]
    (let {[$F (Sd x f)]}
      (- (substitute {[x b]} F)
         (substitute {[x a]} F)))))