Simplification of Expression

I have this code for calculating derivatives:

(define (diff x expr)
  (if  (not (list? expr))
    (if  (equal? x expr) 1 0)
    (let ((u (cadr expr)) (v (caddr expr)))
     (case (car expr)
       ((+) (list '+ (diff x u) (diff x v)))
       ((-) (list '- (diff x u) (diff x v)))
       ((*) (list '+
                   (list  '* u (diff x v))
                   (list  '* v (diff x u))))
        ((/) (list ‘div (list '-
                  (list  '* v (diff x u))
                  (list  '* u (diff x v)))
                  (list  '* u v)))
))))

How can I simplify algebraic expressions?

instead x + xshow2x

and

instead x * xshowx^2

+3
source share
4 answers

Simplification of algebraic expressions is rather difficult, especially in comparison with the calculation of derivatives. Simplification must be done recursively. First, you simplify the most secret expressions. Do not try too much at a time. I would start with the simplest simplifications, for example:

 0 + x -> x
 0 * x -> 0
 1 * x -> x
 x ^ 0 -> 1
 x ^ 1 -> x

Replace subtraction with addition and division by multiplication

 x - y -> x + (-1)*x
 x / y -> x ^ (-1)

It may not look so simple, but it will simplify your code. You can always undo this step at the end.

. , ( , )

 (x * y) * z -> x * (y * z)
 x * 2 -> 2 * x
 2 * (x * 3) -> 2 * (3 * x)

  (x ^ y) ^ z -> x^(y * z)

.

 2 * (3 * x) -> 6 * x
 2 + (3 + x) -> 5 + x

, .

+3

PAIP . Common Lisp, . simp.

, .

. PAIP Macsyma, , 1960- , Mathematica, Maple ( Matlab) .

0

:

(define (diff x expr) ...) - (define (simp expr) ...).

x + x, -

(case (car expr)
  ((+) (if (equal u v)    ; check for identical subexpressions
         `(* ,(simp u) 2) ; if u==v, simplify 2u
         `(+ ,(simp u) ,(simp v))))
  ...)

x * x . if cond, .

, , eliben, , .

0

, , ( ) . , . " " , .

, . ; . - .

, .

0

Source: https://habr.com/ru/post/1708702/


All Articles