function.py

Created by emrio

Created on October 13, 2019

591 Bytes

reduce(reduce, list, start): Implémentation de la fonction reduce de Python

growthRate(f, a, dx): Calcule la dérivé/taux de variation de la fonction f en a (dx est optionnel)

compose(f1, f2, f3, ...): Renvoie une fonction qui est la composition de toutes les fonctions en entrée où f1 est la dernière fonction appliquée

dichotomie(f, a, b, steps): Tente de trouver un point dans l’intervalle [a, b] où la fonction f s’annule (steps est optionnel)

newton(f, a, steps, dx): Tente de trouver un point où la fonction f s’annule en commençant au point a (steps et dx sont optionnels)


def reduce(f,l,s):
  for e in l:
    s=f(s,e)
  return s

def growthRate(f,a,dx=0.001):
  return(f(a+dx)-f(a-dx))/(dx*2)

def compose(*functions):
  functions=reversed(functions)
  return lambda x:reduce(lambda y,f:f(y),functions,x)

def dichotomie(f,a,b,steps=500):
  a,b=sorted([a,b])
  for _ in range(abs(steps)+1):
    c=a+(b-a)/2
    fc=f(c)#avoid losing time if f is difficult to compute
    if fc==0:
      break
    a,b=[c,b]if fc<0 else[a,c]
  return c

def newton(f,a,steps=500,dx=0.001):
  for _ in range(abs(steps)+1):
    a=-f(a)/growthRate(f,a,dx)+a
  return a

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.