Odp: Calculating Derivatives?

Tomek Lisowski Lisowski.Tomasz at sssa.nospam.pl
Mon Mar 27 08:41:48 CEST 2000


U¿ytkownik carbon <prodos8 at yahoo.com> w wiadomooci do grup dyskusyjnych
napisa³:260320001926276702%prodos8 at yahoo.com...
> Is there any python source that relates to calculating derivatives?
> I'd like to try writing software that does this (for polynomials), but
> I don't know where to start.  any resources or help would be greatly
> appreciated.  thanks.

At least for polynomials there is an analytic solution:

Let's take a coefficient list coeff, whose elements coeff[n] just stand by
the term x**n

Then the function (assumes, that coeff is not en empty list):

def PolyDerivative(coeff, x):
  clen = len(coeff)
  if clen == 1:
    return 0.0
  else:
    n = clen-1
    sum = n*coeff[n]
    while n>1:
      n = n-1
      sum = sum*x +n*coeff[n]
  return sum

or perhaps better yet:

def DerivativeCoeffs(coeff):
  dcoeff, n = [], 1
  if len(coeff) == 1: return [0]
  else:
    for c in coeff[1:]:
      dcoeff.append(n*c)
      n = n+1
    return dcoeff

which is called once, and then use many times the normal polynomial value
calculation algorithm:

def PolyValue(coeff, x):
  n = len(coeff)-1
  sum = coeff[n]
  if n>0:
    for i in range(n-1,-1,-1):
      sum = sum*x + coeff[i]
  return sum

I have heard about some Polynomial class module, but I don't have it, so
these functions are simply my training in numerical methods :-)

Tomasz Lisowski




More information about the Python-list mailing list