From: Kang, Pilsung
Subject: Newbie question - List Evaluation?
Date: 
Message-ID: <alv65i$qi9$1@geraldo.cc.utexas.edu>
Ok.

I made a function 'deriv' that performs symbolic differentiation to the
input equation 'form' with respect to the variable 'var'. That is,

(deriv '(* x x) 'x)  ==> (+ x x)

And now I want to define a function 'deriv-val' that returns numeric value
of a derivative, given a value for the 'var'. For example,

(deriv-val '(* x x) 'x 2) ==> 4

For this, I tried to use the output 'deriv' function, but I failed in every
way. Simply, I couldn't bind the 'var' in the output list of 'deriv' with
the input value. How can I do this? I know that there must be a very simple
way to do that, but alas, It's just 2 weeks since I began to learn
lisp.......

From: Paul F. Dietz
Subject: Re: Newbie question - List Evaluation?
Date: 
Message-ID: <3D831F82.7A213BF0@dls.net>
"Kang, Pilsung" wrote:

> (deriv-val '(* x x) 'x 2) ==> 4
> 
> For this, I tried to use the output 'deriv' function, but I failed in every
> way. Simply, I couldn't bind the 'var' in the output list of 'deriv' with
> the input value. How can I do this? I know that there must be a very simple
> way to do that, but alas, It's just 2 weeks since I began to learn
> lisp.......

You could:

  Build a let expression (or similar) and pass it to eval, or

  Substitute 4 for x in the derivative and pass to eval, or

  Eval the deriv expression inside a PROGV.

	Paul
From: Will Deakin
Subject: Re: Newbie question - List Evaluation?
Date: 
Message-ID: <alv7np$nr7$1@knossos.btinternet.com>
Kang, Pilsung wrote:
> For this, I tried to use the output 'deriv' function, but I failed in every
> way. Simply, I couldn't bind the 'var' in the output list of 'deriv' with
> the input value. How can I do this?
Are you working from a book? This kind of symbolic algebra stuff is 
covered in some considerable depth in PAIP[1].

However, if you don't have a copy to hand, it would help if you would 
give us more of a clue about what you've been upto by showing us the code.

:)w

[1] Paradigms of Artificial Intelligence Programming by Peter Norvig, 
December 1991, Morgan Kaufmann; ISBN: 1558601910
From: Kang, Pilsung
Subject: Re: Newbie question - List Evaluation?
Date: 
Message-ID: <alvbci$spo$1@geraldo.cc.utexas.edu>
"Will Deakin" <···········@hotmail.com> wrote in message
·················@knossos.btinternet.com...
> Kang, Pilsung wrote:
> > For this, I tried to use the output 'deriv' function, but I failed in
every
> > way. Simply, I couldn't bind the 'var' in the output list of 'deriv'
with
> > the input value. How can I do this?
> Are you working from a book? This kind of symbolic algebra stuff is
> covered in some considerable depth in PAIP[1].
>
> However, if you don't have a copy to hand, it would help if you would
> give us more of a clue about what you've been upto by showing us the code.
>

Ok. Here goes the code snippet.

;  Example: (deriv '(* 5 x) 'x)
(defun deriv (form var)
  (cond ((atom form)
          (if (eq form var) 1 0))
 ((eq (car form) '*) (derivtimes form var)) )

; Example: (deriv-val '(* 5 x) 'x 2)
(defun deriv-val (form var val)
  (let ((var val))
    (eval (deriv form var))))

Here, I defined 'deriv-val' using 'let', which is one of Paul Dietz's
suggestion in response to my message(Thank you, Paul). But in my clisp
environment,

1. Break [13]> (deriv '(* x x) 'x)
(+ X X)                 --> this is what I wanted, but

1. Break [13]> (deriv-val '(* x x) 'x 2)
0                          --> which is not I expected (4).

I traced 'deriv' and 'deriv-val' to see what's going wrong, and I found that
(deriv '(* x x) '2) is called instead of (deriv '(* x x) 'x). I guess that
means x gets bound to 2 prior to the evaluation of (deriv '(* x x) 'x).

So, how can I substitue 2 for x in the function 'deriv-val'?
At first, I thought this problem would be really easy one, and now I am at a
loss for more than 5 hours...

Actually, I have a copy of PAIP book and found sections on the symbolic
algebra topic. But I couldn't find any answer to this specific problem. I
think that this kind of problem is rather due to my shallow knowledge on
lisp language than on symblic algebra.

Would you enlighten me more on this?
Thanks!
From: Robert St. Amant
Subject: Re: Newbie question - List Evaluation?
Date: 
Message-ID: <lpnk7loo6jd.fsf@haeckel.csc.ncsu.edu>
"Kang, Pilsung" <········@cs.utexas.edu> writes:

> "Will Deakin" <···········@hotmail.com> wrote in message
> ·················@knossos.btinternet.com...
> > Kang, Pilsung wrote:
> > > For this, I tried to use the output 'deriv' function, but I failed in
> every
> > > way. Simply, I couldn't bind the 'var' in the output list of 'deriv'
> with
> > > the input value. How can I do this?
> > Are you working from a book? This kind of symbolic algebra stuff is
> > covered in some considerable depth in PAIP[1].
> >
> > However, if you don't have a copy to hand, it would help if you would
> > give us more of a clue about what you've been upto by showing us the code.
> >
> 
> Ok. Here goes the code snippet.
> 
> ;  Example: (deriv '(* 5 x) 'x)
> (defun deriv (form var)
>   (cond ((atom form)
>           (if (eq form var) 1 0))
>  ((eq (car form) '*) (derivtimes form var)) )
> 
> ; Example: (deriv-val '(* 5 x) 'x 2)
> (defun deriv-val (form var val)
>   (let ((var val))
>     (eval (deriv form var))))
> 
> Here, I defined 'deriv-val' using 'let', which is one of Paul Dietz's
> suggestion in response to my message(Thank you, Paul). But in my clisp
> environment,
> 
> 1. Break [13]> (deriv '(* x x) 'x)
> (+ X X)                 --> this is what I wanted, but
> 
> 1. Break [13]> (deriv-val '(* x x) 'x 2)
> 0                          --> which is not I expected (4).
> 
> I traced 'deriv' and 'deriv-val' to see what's going wrong, and I found that
> (deriv '(* x x) '2) is called instead of (deriv '(* x x) 'x). I guess that
> means x gets bound to 2 prior to the evaluation of (deriv '(* x x) 'x).
> 
> So, how can I substitue 2 for x in the function 'deriv-val'?
> At first, I thought this problem would be really easy one, and now I am at a
> loss for more than 5 hours...
> 
> Actually, I have a copy of PAIP book and found sections on the symbolic
> algebra topic. But I couldn't find any answer to this specific problem. I
> think that this kind of problem is rather due to my shallow knowledge on
> lisp language than on symblic algebra.

Take a look at the definition of match-if on page 186.  It shows how
to do what you want, using progv.  If the definition doesn't use progv
in your copy of the book (mine doesn't), you can find the correct
definition on the PAIP errata Web page:

               http://www.norvig.com/paip-errata.html

-- 
Rob St. Amant
http://www4.ncsu.edu/~stamant
From: Kang, Pilsung
Subject: Re: Newbie question - List Evaluation?
Date: 
Message-ID: <am1ten$eqi$1@geraldo.cc.utexas.edu>
>
> Take a look at the definition of match-if on page 186.  It shows how
> to do what you want, using progv.  If the definition doesn't use progv
> in your copy of the book (mine doesn't), you can find the correct
> definition on the PAIP errata Web page:
>
>                http://www.norvig.com/paip-errata.html

Fortunately, my book used progv and I tried it,

(defun deriv-val (form var val)
  (progv (cons var nil) (cons val nil) (eval (deriv form var))) )

and it worked! Many thanks.
From: Paul F. Dietz
Subject: Re: Newbie question - List Evaluation?
Date: 
Message-ID: <3D833CB0.E4EFF92E@dls.net>
"Kang, Pilsung" wrote:

> ; Example: (deriv-val '(* 5 x) 'x 2)
> (defun deriv-val (form var val)
>   (let ((var val))
>     (eval (deriv form var))))
> 
> Here, I defined 'deriv-val' using 'let', which is one of Paul Dietz's
> suggestion in response to my message(Thank you, Paul). 

This is not what I suggested, and it will not work
(this binds VAR before calling DERIV, passing '2' as
the second argument to DERIV.)

Instead I suggested building a let form, then passing
that form to EVAL.  Even simpler, use SUBST to replace
the varialbe with the valie in the expression returned
by DERIV before calling EVAL on it.

	Paul
From: Kang, Pilsung
Subject: Re: Newbie question - List Evaluation?
Date: 
Message-ID: <am1rnf$dus$1@geraldo.cc.utexas.edu>
> Instead I suggested building a let form, then passing
> that form to EVAL.  Even simpler, use SUBST to replace
> the varialbe with the valie in the expression returned
> by DERIV before calling EVAL on it.
>

Thanks!
I solved the problem by using SUBST like this,

(defun deriv-val (form var val)
  (eval (subst val var (deriv form var))) )

But I couldn't get what I wanted by using let form and eval. Can I ask and
bother you to show me some code if you have some time?

Anyway, thanks again.
From: Kaz Kylheku
Subject: Re: Newbie question - List Evaluation?
Date: 
Message-ID: <cf333042.0209141539.34c51175@posting.google.com>
"Kang, Pilsung" <········@cs.utexas.edu> wrote in message news:<············@geraldo.cc.utexas.edu>...
> Ok.
> 
> I made a function 'deriv' that performs symbolic differentiation to the
> input equation 'form' with respect to the variable 'var'. That is,
> 
> (deriv '(* x x) 'x)  ==> (+ x x)
> 
> And now I want to define a function 'deriv-val' that returns numeric value
> of a derivative, given a value for the 'var'. For example,
> 
> (deriv-val '(* x x) 'x 2) ==> 4
> 
> For this, I tried to use the output 'deriv' function, but I failed in every
> way. Simply, I couldn't bind the 'var' in the output list of 'deriv' with
> the input value. How can I do this? I know that there must be a very simple
> way to do that, but alas, It's just 2 weeks since I began to learn
> lisp.......

If I understand you, your problem is how to establish a binding
between the symbol X and the value 2. Perhaps what you are looking for
is the standard PROGV operator. Look this up in the Common Lisp
Hyperspec.

Note that PROGV establishes dynamic bindings, not lexical ones.

If you wanted something like (let ((x 2)) (deriv-val '(* x x))) to
work---in other words, to use lexical variables, your best bet would
be to make DERIV-VAL a macro, which does the derivation at
macro-expansion time. For that to work no matter how your Lisp program
is processed, you need to ensure that the DERIV function is available
at compile time, so wrap it like this:

 (eval-when (:compile-toplevel :load-toplevel :execute)
   (defun deriv (...) ...))

The macro is then written, quite simply:

 (defmacro deriv-val (expression w-r-t) `(deriv ',expression ',w-r-t))

when you write the macro invocation, you don't quote the list. Rather,
something like:

 (let ((x 2)) (deriv-val (* x x) x))

How it works is that when the DERIV-VAL macro is processed, calls
DERIV to compute the derivative, and then this derivative expression
is substituted into the program in place of the (deriv-val ...) call.
So in any decent implementation of Lisp, the derivative is computed
just once, and if your program is compiled, it is computed at compile
time.

By the way, is there a special reason why your deriver spits out (+ x
x) rather than (* 2 x)?
From: Kang, Pilsung
Subject: Re: Newbie question - List Evaluation?
Date: 
Message-ID: <am1ruk$e5d$1@geraldo.cc.utexas.edu>
Thanks for kind and long explanation, though I had no time to try your
suggestion. Actually, macros are also a little hard for my understandings on
lisp now.

>
> By the way, is there a special reason why your deriver spits out (+ x
> x) rather than (* 2 x)?

No. It's simply that I didn't spend much time on the simplification yet.
Thanks.