Re: Using substitution rules to define a function
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Using substitution rules to define a function
- From: Richard Mercer <richard at rmercer.wright.edu>
- Date: Thu, 31 Mar 1994 08:31:11 -0500
Jean Peccoud writes: > could anybody explain me what happens here ? Sol is a > solution of an equation > > In[32]:= > > sol > > Out[32]= > 2 2 > 2 a + b w + Sqrt[b] w Sqrt[4 a + b w ] {{x -> > ---------------------------------------}, > 2 > > 2 2 > 2 a + b w - Sqrt[b] w Sqrt[4 a + b w ] {x -> > ---------------------------------------}} > 2 > > Here I want to define a function of w which returns the > first solution. > > In[34]:= > > invf1[w_]:=sol[[1,1,2]] invf1[x] > > Out[34]= > > 2 2 > 2 a + b w + Sqrt[b] w Sqrt[4 a + b w ] > --------------------------------------- > 2 > > but when I want to compute the value of this function > for x, w remains. There must be a basic trick that I > did not undestand well in using rules or patterns. Thank > you. There are three solutions to your problem: (1) invf1[w_] = sol[[1,1,2]]; without the colon. (2) invf1[w_]:= Evaluate[sol[[1,1,2]]]; (3) In notebooks front ends, use the "Copy Output From Above" menu item to place the previous output in your function definition, then edit. Each of these will establish the functional relationship you desire. The problem is in understanding the "delayed assignment" operator :=, which is not a sacred part of function definition (as is the underscored variable), but rather just a suggestion that works best in most cases. But not here! The effect of delayed assignment in your example is that the value of invf1[w_] is set to literally 's[[1,1,2]]', which has no w's in it, and therefore no functional relationship is established. With the direct assignment(1) or the delayed assignment with forced evaluation(2), the expression s[[1,1,2]] is evaluated before the function is defined, and so invf1[w_] is set to 2*a + b*w^2 + b^(1/2)*w*(4*a + b*w^2)^(1/2) as intended. ********************************************************************* Richard Mercer 513-873-2191 office Department of Mathematics and Statistics 513-873-2785 message Wright State University 513-873-3301 fax Dayton, OH 45435 NeXTmail welcome! richard at rmercer.wright.edu or rmercer at desire.wright.edu *********************************************************************