Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Generate polynomial of specified degree

  • To: mathgroup at
  • Subject: [mg60757] Re: Generate polynomial of specified degree
  • From: Maxim <ab_def at>
  • Date: Tue, 27 Sep 2005 03:45:28 -0400 (EDT)
  • References: <dgtj9q$28g$> <dgtuja$6ni$> <dh0f27$qaf$>
  • Sender: owner-wri-mathgroup at

On Fri, 23 Sep 2005 08:40:39 +0000 (UTC), Paul Abbott  
<paul at> wrote:

> In article <dgtuja$6ni$1 at>, dh <dh at> 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 .

Maxim Rytin
m.r at

  • Prev by Date: Re: WORD document
  • Next by Date: Re: WORD document
  • Previous by thread: Re: Generate polynomial of specified degree
  • Next by thread: Re: 5.2 Install issues