• Aucun résultat trouvé

25 Symbolic Differentiation

Dans le document 2 The Atom Table and the Number Table (Page 52-58)

The rules for differentiation are explicitly recursive, so it is easy to use LISP to compute the symbolic derivative of a real-valued function of real number arguments. The biggest difficulty is the problem of input and output notation. If we agree to accept the LISP prefix notation for algebraic expressions, so that we enter 1 +sin(x+y)/xas(PLUS 1 (QUOTIENT (APPLY SIN ((PLUS x y))) x))for example, then the task of differentiation is truly simple. We shall instead demand that the input and output be in traditional infix form, however, so the job is more complicated.

We shall present a collection of LISP functions and special forms below which provide the ability to define, differentiate, and print-out elementary real-valued functions of real arguments. These LISP functions and special forms include FSET,DIFF, and FPRINT.

We will use the special form FSET to define a real-valued function of real arguments by typing

(FSET G (X Y . . .) (f)), whereGis an ordinary atom, (X Y . . .)is a list of ordinary atoms denoting the formal arguments of the functionGbeing defined, andf is aninfixform which is the body that defines G,e.g.,(FSET G1 (X Y) (X + Y * EXP(X))).

Note that an infix form here is a kind of pun; it denotes the intended algebraic expression, and also it is a particular LISP S-expression. The only caveat that must be remembered is that blanks must delimit operators in infix forms to insure that they will be interpreted as atoms within the S-expression.

Such a function can be differentiated with respect to a variable which may be either a free variable or a formal argument. The derivative is itself a function which has the same formal arguments.

The derivative of such anFSET-defined functionGwith respect toXis denotedG#X. The LISP function

DIFF will be defined to compute the derivative of such a real-valued function, G, with respect to a specified symbol X, and store the resulting function as the value of the created atom G#X. This is done for a function Gby typing (DIFF G X).

In order to construct atoms with names like G#Xwhich depend upon the names of other atoms, we will use the builtin LISP function MKATOMwhich makes atoms whose names are composed from the names of other atoms. MKATOMis defined such thatv[(MKATOM x y)] = the ordinary atom whose name is the string formed by concatenating the names of the ordinary atomsv[x] and v[y].

Functions defined byFSETor byDIFFmay be printed-out for examination by using the LISP function

FPRINT defined below.

52 25 SYMBOLIC DIFFERENTIATION A functionh defined by FSET orDIFF may be evaluated at the point (x1, x2, . . .), where x1,x2,. . . are numbers, by usingAPPLY. Thus typing(APPLY h (x1 x2 . . .))causes the numberh(x1, x2, . . .) to be typed-out. Of course, all the subsidiary functions called inh must be defined as well.

The differentiation rules used by DIFF are as follows. The symbol D denotes differentiation with respect to a specific understood ordinary atom variable name x, called thesymbol of D.

D(A+B) = D(A) +D(B)

D(AB) = D(A)−D(B)

D(−A) = −D(A)

D(AB) = AD(B) +BD(A)

D(A/B) = D(A)/B+AD(B↑(−1))

D(AB) = BA↑(B−1)∗D(A) +LOG(A)∗ABD(B) (↑ denotes exponentiation)

D(LOG(A)) = D(A)/A

IfF(X Y . . .) has been defined via FSET, then D(F(A B . . .)) =F#X(A B . . .)∗D(A) +F#Y(A B . . .)∗D(B) +. . . .

IfA is an ordinary atom variable thenD(A) = 1 if Ais the symbol ofD, and D(A) = 0 otherwise.

IfA is a number, thenD(A) = 0.

The infix forms used to define functions withFSET are translated into corresponding S-expressions which will be called E-expressions in order to distinguish them. E-expressions are made up of evaluatable prefix S-expressions involving numbers and ordinary atoms and other E-expressions.

The basic LISP functional forms used in E-expressions are:

(MINUS a),

(PLUS a b),

(DIFFERENCE a b),

(TIMES a b),

(QUOTIENT a b),(POWER a b), where aand bare E-expressions, and

(APPLY h (x1 x2 . . .)), wherex1,x2,. . . are E-expressions, andh is anFSET-defined orDIFF-defined function.

The definitions ofFSET,DIFF, and FPRINTfollow.

• FSET – special form

v[(FSET G (X Y . . .) (e))] =v[G] whereGis an ordinary atom whose name does not begin with

“$”, and (X Y . . .) is a list of atoms which are the formal arguments to the functionF being defined. eis an infix functional form which is used to defineG(X,Y, . . .).

FSET is defined by

(SETQ FSET (SPECIAL ($G $L $Q)

(SET $G (EVAL (LIST ’LAMBDA $L (ELIST $Q))))))

The function SEThas been defined above. Here we redefine SETby:

(SETQ SET (LAMBDA ($X $Y)

(EVAL (CONS ’SETQ (CONS $X (CONS ’$Y NIL))))))

Exercise 25.1: Why are such strange formal argument names used in defining FSET and

SET? Hint: think about using SETQ to assign values to ordinary atoms which are active formal arguments. In particular, suppose the $-signs were dropped, and we executed (FSET Y (X) X)? Exercise 25.2: Can you think of a way to allow the user to write(FSET Y (X) = e)? Note the third argument here is “=”.

ELIST constructs the E-expression corresponding to its argument by using the Bauer-Samelson-Ershov postfix Polish translation algorithm. ELISTis also used to process the argument of a unary minus operation.

Exercise 25.3: Use a library to find out aboutJan L-ukasiewiczand about “Polish notation”.

(SETQ ELIST (LAMBDA (L)

(COND ((ATOM L) L)

((EQ (CDR L) NIL) (ELIST (CAR L))) (T (EP NIL NIL (DLIST L)))))) (SETQ DLIST (LAMBDA (L)

(COND ((ATOM L) L)

((NULL (CDR L)) (DLIST (CAR L))) (T L))))

(EP OP P L)is the result of completing the polish translation of Lusing the stacksOP and P.

(SETQ EP (LAMBDA (OP P L)

(COND ((NULL L) (COND ((NULL OP) (DLIST P)) (T (BUILD OP P L)))) (T (POLISH OP P (CAR L) (CDR L)))))) (SETQ CADR (LAMBDA (L) (CAR (CDR L))))

(BUILD OP P L)puts the phrase h(CAR OP) P 2 P

1i on the Pstack in place of P

1 and P

2, and goes back toEP to continue the translation.

(SETQ BUILD (LAMBDA (OP P L)

(EP (CDR OP) (CONS (LIST (NAME (CAR OP)) (CADR P) (CAR P)) (CDR (CDR P))) L)))

54 25 SYMBOLIC DIFFERENTIATION In(POLISH OP P B L),Bis the current input symbol, andLis the remaining infix input. EitherBis to be pushed on OP, or else Bis used to make a phrase reduction and a corresponding entry is placed on the Pstack.

(SETQ POLISH (LAMBDA (OP P B L) (COND ((EQ B ’-)

(H V L) pushes V onto theP stack and goes back to EP. It also changes a binary minus-sign at the front of Lto the unambiguous internal code “:”.

(SETQ H (LAMBDA (V L)

v[(FPRINT G)] = NIL, and the E-expression value of the function-valued atom G is printed-out in infix form.

FPRINT is defined by:

(SETQ FPRINT (SPECIAL ($G) (T (LIST (HP (CADR G)) (OPSYMBOL (CAR G))

(HP (CADR (CDR G)))))))) function and Xis an ordinary atom.

DIFF is defined by:

(SETQ DIFF (SPECIAL ($G X)

(SET (MKATOM $G (MKATOM ’# X))

(EVAL (LIST ’LAMBDA (CAR (BODY (EVAL $G))) (DI (CDR (BODY (EVAL $G))) X))))))

(DI G X) is the E-expression form derivative of the E-expression Gwith respect to the atomX.

(SETQ DI (LAMBDA (E X) ((EQ OP ’DIFFERENCE) (LIST OP (DI (CAR L) X)

(DI (CADR L) X)))

56 25 SYMBOLIC DIFFERENTIATION

(LIST ’QUOTIENT (DI (CADR L) X) (CADR L))) (T (CHAIN (CAR L) (CAR (BODY (EVAL (CAR L))))

(LIST ’APPLY (MKATOM F (MKATOM ’# (CAR A))) B))))

Exercise 25.4: Would it be okay to defineTERM as

(LAMBDA (A R) (LIST ’TIMES (DI (CAR R) X)

(LIST ’APPLY (MKATOM F (MKATOM ’# (CAR A))) B))),

and appropriately modify the associated calls?

Exercise 25.5: Is it necessary to use APPLY to compute functions at particular points? Is it necessary to use APPLY embedded in the bodies of functions created byFSET and DIFFat all?

Solution 25.5: The functions built by FSETand DIFF are full-fledged LISP functions. The use of APPLYcould be dispensed with, except that we then need to provide the functionLOG which is used inDIFF. Also, this would avoid the need to watch out for the formal argument names used in the special form APPLY in order to avoid the special form functional binding problem. Using

APPLY is convenient, however, since we then do not have to distinguish user function names from operator names such as PLUS and TIMES.

Exercise 25.6: The function DF allows products with 1 and sums and differences with 0 to be created. Write a modified version of DF which uses some auxilliary functions to simplify the E-expressions being formed so as to avoid such needless operations.

Exercise 25.7: Introduce the elementary functions EXP, SIN, and COS, and implement the explicit differentiation rules which apply to them.

Dans le document 2 The Atom Table and the Number Table (Page 52-58)

Documents relatifs