$SPAD/src/input eval.input

Timothy Daly December 3, 2016

Abstract

Contents

2

1 License

— license —

--Copyright The Numerical Algorithms Group Limited 1991.

———-

— * —

)set break resume
)spool eval.output
)set message test on
)set message auto off
)clear all

———-

This file demonstrate the various eval’s available on EXPR, and the handling of formal derivatives. Lines starting with –** are comments indicating that the final syntax will be different.

— * —

--** This line will be optional interactively, since the a := f(x**2)
--** will prompt you if you don't declare f this way.
--S 1 of 23
f := operator 'f
--R
--R
--R   (1)  f
--R                                                          Type: BasicOperator
--E 1
--S 2 of 23
a := f(x**2)
--R
--R
--R           2
--R   (2)  f(x )
--R                                                    Type: Expression(Integer)
--E 2
--S 3 of 23
b := differentiate(a,x,2) + f 5
--R
--R
--R          2 ,,  2      ,  2
--R   (3)  4x f  (x ) + 2f (x ) + f(5)
--R
--R                                                    Type: Expression(Integer)

--E 3

———-

This is the ’variable’ evaluation, similar to what’s available on polynomials:

— * —

--S 4 of 23
eval(b, x = x + y)
--R
--R
--R           2            2  ,,  2           2      ,  2           2
--R   (4)  (4y  + 8x y + 4x )f  (y  + 2x y + x ) + 2f (y  + 2x y + x ) + f(5)
--R
--R                                                    Type: Expression(Integer)
--E 4

———-

This is the ’kernel’ evaluation, allowing you to specify special values. Only the specified value f 5 is affected, not the others:

— * —

--S 5 of 23
eval(b, f 5 = 1)
--R
--R
--R          2 ,,  2      ,  2
--R   (5)  4x f  (x ) + 2f (x ) + 1
--R
--R                                                    Type: Expression(Integer)
--E 5

———-

This is the ’operator’ evaluation, allowing you to specify an actual function for a formal one. ALL the values of f are affected.

— * —

--** will eventually use the +-> notation in the eval statement
--S 6 of 23
foo(u:EXPR INT):EXPR INT == exp u
--R
--R   Function declaration foo : Expression(Integer) -> Expression(Integer
--R      ) has been added to workspace.
--R                                                                   Type: Void
--E 6

———- So what is b if f were the exponential function? Notice that the formal derivatives will be computed properly now.

— * —

--S 7 of 23
c := eval(b, 'f, foo)
--R
--R   Compiling function foo with type Expression(Integer) -> Expression(
--R      Integer)
--R
--R                    2
--R           2       x      5
--R   (7)  (4x  + 2)%e   + %e
--R                                                    Type: Expression(Integer)
--E 7

———-

We can also use that evaluation on ’system’ operators, which allows us to replace an actual function by a formal one.

— * —

--S 8 of 23
oof(u:EXPR INT):EXPR INT == f u
--R
--R   Function declaration oof : Expression(Integer) -> Expression(Integer
--R      ) has been added to workspace.
--R                                                                   Type: Void
--E 8
--S 9 of 23
eval(c, 'exp, oof)
--R
--R   Compiling function oof with type Expression(Integer) -> Expression(
--R      Integer)
--R
--R           2        2
--R   (9)  (4x  + 2)f(x ) + f(5)
--R                                                    Type: Expression(Integer)
--E 9

———-

It is also possible to give f a derivative without replacing it by a ’concrete’ function. — * —

--S 10 of 23
f'(u:EXPR INT):EXPR INT == f u
--R
--R   Function declaration f' : Expression(Integer) -> Expression(Integer)

--R      has been added to workspace.
--R                                                                   Type: Void
--E 10

———-

This will make f differentiate like an exponential:

— * —

--S 11 of 23
derivative(f,f')
--R
--R   Compiling function f' with type Expression(Integer) -> Expression(
--R      Integer)
--R
--R   (11)  f
--R                                                          Type: BasicOperator
--E 11
--S 12 of 23
b
--R
--R
--R           2 ,,  2      ,  2
--R   (12)  4x f  (x ) + 2f (x ) + f(5)
--R
--R                                                    Type: Expression(Integer)
--E 12
--** The coercion is needed to avoid an interpreter bug.
--** This will just be eval(b) eventually:
--S 13 of 23
eval(b, x = x::(EXPR INT))
--R
--R
--R           2 ,,  2      ,  2
--R   (13)  4x f  (x ) + 2f (x ) + f(5)
--R
--R                                                    Type: Expression(Integer)
--E 13
--S 14 of 23
differentiate(%, x)
--R
--R
--R           3 ,,,  2        ,,  2
--R   (14)  8x f   (x ) + 12xf  (x )
--R
--R                                                    Type: Expression(Integer)
--E 14

———-

This is the ’operator/power’ evaluation: suppose that we know that f squared is the exponential, but we do not want to replace f(u) by sqrt(exp u). It is still possible to eliminate higher powers of f in the following way:

— * —

--S 15 of 23
a3 := a * a * a
--R
--R
--R            2 3
--R   (15)  f(x )
--R                                                    Type: Expression(Integer)
--E 15
--S 16 of 23
foo
--R
--R
--R   (16)  foo u == exp(u)
--R                                                    Type: FunctionCalled(foo)
--E 16
--S 17 of 23
eval(a3,'f,2,foo)
--R
--R
--R                 2
--R            2   x
--R   (17)  f(x )%e
--R                                                    Type: Expression(Integer)
--E 17

———-

Several ’operator’ evaluations can be carried out simultaneously:

— * —

--S 18 of 23
g := operator 'g
--R
--R
--R   (18)  g
--R                                                          Type: BasicOperator
--E 18
--S 19 of 23
bar(u:EXPR INT):EXPR INT == sin(u) + cos(2*u)

--R
--R   Function declaration bar : Expression(Integer) -> Expression(Integer
--R      ) has been added to workspace.
--R                                                                   Type: Void
--E 19
--S 20 of 23
a + g a
--R
--R
--R              2        2
--R   (20)  g(f(x )) + f(x )
--R                                                    Type: Expression(Integer)
--E 20
--S 21 of 23
eval(%,['f,'g],[foo,bar])
--R
--R   Compiling function bar with type Expression(Integer) -> Expression(
--R      Integer)
--R
--R                2            2       2
--R               x            x       x
--R   (21)  sin(%e  ) + cos(2%e  ) + %e
--R                                                    Type: Expression(Integer)
--E 21
--S 22 of 23
a3 + g a
--R
--R
--R              2        2 3
--R   (22)  g(f(x )) + f(x )
--R                                                    Type: Expression(Integer)
--E 22

———-

The grand finale: by now the effect of the following should be clear:

— * —

--S 23 of 23
eval(%,['f,'g],[2,1],[foo,bar])
--R
--R
--R                                            2
--R                2             2        2   x
--R   (23)  sin(f(x )) + cos(2f(x )) + f(x )%e
--R                                                    Type: Expression(Integer)
--E 23

)spool )lisp (bye) ———-

9

References^{[1] nothing}

10