Re: Generate polynomial of specified degree
- To: mathgroup at smc.vnet.net
- Subject: [mg60757] Re: Generate polynomial of specified degree
- From: Maxim <ab_def at prontomail.com>
- Date: Tue, 27 Sep 2005 03:45:28 -0400 (EDT)
- References: <dgtj9q$28g$1@smc.vnet.net> <dgtuja$6ni$1@smc.vnet.net> <dh0f27$qaf$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Fri, 23 Sep 2005 08:40:39 +0000 (UTC), Paul Abbott
<paul at physics.uwa.edu.au> wrote:
> In article <dgtuja$6ni$1 at smc.vnet.net>, dh <dh at metrohm.ch> wrote:
>
>> it is not too hard:
>>
>> f[x_Symbol,n_Integer]:= Array[c,{n+1},0].x^Range[0,n]
>
> Personally, I think it is better to separate parameters from variables,
> and also to localise the coefficient name:
>
> f[n_Integer,c_:c][x_Symbol]:= With[{p = Range[0, n]}, (c /@ p) . x^p]
> f[0, c_:c][x_Symbol]:= c[0]
>
> Now try
>
> f[0][x]
> f[3][x]
> f[3,a][x]
>
> Sometimes using pure functions is advantageous:
>
> Clear[f]
> f[n_Integer,c_:c] := Function[x, (c /@ Range[0, n]) . x^Range[0, n]]
> f[0, c_:c]:= Function[x, c[0]]
>
> As a particular advantage of this syntax, try
>
> f[2]'[x]
>
> Cheers,
> Paul
>
>
>
Differentiation of pure functions in Mathematica is more like a lottery:
In[1]:= Function[x, {1, 1}.x^{1, 2}]'[x]
Out[1]= {3, 3*x}
In[2]:= Function[x, Total[x^{1, 2}]]'[x]
Out[2]= {{0, 0}, {0, 0}}
Note that the functions are scalar-valued (and equivalent) for scalar x.
It probably would be better if Mathematica always evaluated f' as
Module[{var}, Function @@ {var, D[f[var], var]}]
That would get the two above examples right. Mathematica seems to do
roughly that if the function is defined as f[x_] : = ..., except that it
uses Slot instead of creating unique formal parameters. This means that
there may be a conflict if the definition of f already contains a Function
without named formal parameters:
In[3]:= f[x_] := x + x^2&[1]; f'[x]
Out[3]= 0
This time it's the other way around: the result would be correct if f were
defined in a pure function form as Function[x, x + x^2&[1]]. Also see
http://forums.wolfram.com/mathgroup/archive/2005/Jun/msg00059.html .
Maxim Rytin
m.r at inbox.ru