Re: Turning Derivative into Function (Newbie Question)
- To: mathgroup at smc.vnet.net
- Subject: [mg115463] Re: Turning Derivative into Function (Newbie Question)
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Tue, 11 Jan 2011 19:22:19 -0500 (EST)
Hi, In your first example, you are mixing math with programming. When you define g:=f'[x], you define g as an expression, not a function. Therefore, it will always use <x>, and can not be passed arguments. You can do this, however: In[3]:= g /. x -> 2 Out[3]= 4 But this doesn't make g a function. What you could also do is this: In[4]:= Clear[g]; g = f'; In[6]:= g[2] Out[6]= 4 Which is probably what you wanted. What happened here is that f' has been converted into a pure function ( http://reference.wolfram.com/mathematica/ref/Function.html), and that pure function has been assigned to g. You second example would work, had you removed the initial definition for <g>. The reason for the error message is a bit subtle (while assignment operator Set ( = ) does not evaluate its l.h.s. in its entirety, it evaluates its head, which is g, which is why the it attempted to assign to 2x[y_] and barked). Anyway, after you clear g, your code works: In[8]:= Clear[g]; g[x_] := f'[x]; g[2] Out[10]= 4 As for the references to functions and variables: there are 2 main types of functions in Mathematica - pure functions (which I already mentioned), and functions defined by patterns (like your function). The majority of user-defined functions are of the second type, while pure functions are typically (but not always) used for somewhat different purposes (as building blocks for functional programs). For the functions defined by patterns, one important thing to know is that they are actually not functions in the sense of more traditional programming languages, but transformation rules. Therefore, if you want to better understand Mathematica functions, you need to understand transformation rules. You could read on them more at http://reference.wolfram.com/mathematica/tutorial/TransformationRulesAndDefinitionsOverview.html and for functions also here: http://reference.wolfram.com/mathematica/tutorial/FunctionsAndProgramsOverview.html You may also find useful a chapter in my book where I discuss these topics: http://www.mathprogramming-intro.org/book/chapterNode5.html Hope this helps. Regards, Leonid On Tue, Jan 11, 2011 at 2:58 PM, Just A Stranger < forpeopleidontknow at gmail.com> wrote: > When I apply a function that outputs another function, how come I can not > pass arguments to the new function? Am I missing some principle about how > Mathematica functions work? Could someone please point me to the relevant > documentation? > > > A simple example to illustrate my confusion: > > In[1]: f[x_] := x^2 > In[2]: g := f'[x] > > So now I have: > > In[3]: g > out[3]: 2x > > as expected > > So how come I get the following when trying to pass an argument to g: > > In[4]: g[2] > Out[4]: 2x[2] > > Instead of the output I want: > > *Ou[4]: 4 > > > I tried > > In: g[x_] := f'[x] > > But it seems to think I'm trying to assign a function to Times. > "SetDelayed::write: Tag Times in (2 x)[y_] is Protected. >>" > > Thank you very much for any help :) > > >