Re: Evaulate an arithmetic expression
From: David Sletten (david_at_slytobias.com)
Date: 02/09/05
- Next message: Barry Wilkes: "Re: demonic problem descriptions"
- Previous message: William D Clinger: "Re: Continuations in Common Lisp (with apologies)"
- In reply to: fix: "Re: Evaulate an arithmetic expression"
- Next in thread: mmcconnell17704_at_yahoo.com: "Re: Evaulate an arithmetic expression"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 09 Feb 2005 07:05:22 GMT
fix wrote:
>>>> This could be even simpler:
>>>> (defun deriv-eval (expr var val)
>>>> (setf (symbol-value var) val)
>>>> (eval (deriv expr val)))
>>>>
>> In the terser example, we simply use the derivative directly rather
>> than storing it temporarily. The fact that we've already assigned a
>> value to X is irrelevant. Your derivative code isn't evaluating
>> variables, is it? It's simply looking for the symbol with respect to
>> which you are differentiating right? In other words, differentiating
>> 3x^2 - 4x wrt x is 6x - 4 regardless of the value of x. Even if you
>> know ahead of time that x = 3 you can't evaluate 3(3^2) - 4(3) and
>> then take the derivative.
>
>
> Do you mean "3" will be the symbol if you set x=3?
> It is just messing up my mind, I can't decide when it is a variable and
> when it is a number.
> What if I deriv (* 3 x) and substitute x=3? The "3" in (* 3 x) would not
> be regarded as a variable and be differentiated?
>
The process of differentiation of polynomials is simply a formal
transformation. Regardless of whether the variable is x or y or z, we
take the pattern <var>^2 and produce 2*<var>. This is what your code is
doing in DERIV. It takes the expression (* x x) and produces (+ x x). It
would transform the expression (* z z) into (+ z z), right? This process
does not involve evaluating variables--it simply transforms one
expression into another. It's only when we ask Lisp to EVAL the
derivative expression that we need concern ourselves with what X (or Z)
represents. So in the shorter version above, the assignment of a value
to X is independent of the production of the derivative expression. As
long as we do the SETF _sometime_ before we call EVAL we're OK. Because
EVAL sees the derivative expression, (+ x x), for instance and has to
determine "what is the value of X?".
>> In any case, take a look at Alan's example. He has a better approach.
>
>
> Alan's?
Take a look at the example Alan Crowe presented elsewhere in this
thread. It uses PROGV to limit the scope of the variables you evaluate
in your derivatives.
David Sletten
- Next message: Barry Wilkes: "Re: demonic problem descriptions"
- Previous message: William D Clinger: "Re: Continuations in Common Lisp (with apologies)"
- In reply to: fix: "Re: Evaulate an arithmetic expression"
- Next in thread: mmcconnell17704_at_yahoo.com: "Re: Evaulate an arithmetic expression"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|