Dynamic polynomial function
The snippet can be accessed without any authentication.
Authored by
Schneider, Michael
Returns the result of a polynomial sum of x**i * a[i]. The order of the polynomial is determined by the length of a. This also works for fitting function like scipy curve_fit py providing a list of starting parameters. The length of the list determines the polynomial order.
import pandas as pd
def poly(x, *a):
"""
Returns the result of a polynomial sum of x**i * a[i]. The order of the polynomial is determined by the
length of a. This also works for fitting function like scipy curve_fit py providing a list of starting
parameters. The length of the list determines the polynomial order.
:param x: x value of polynomial function.
:param a: List of polynomial parameters.
:return: Result of polynomial function.
"""
if type(x) == pd.Series:
x = x.values * 1.0 # Seriously, in the Series, the numbers were integers, causing some issues here...
order = len(a) - 1
out = 0.0
for i in range(0, order + 1):
out += x ** i * a[i]
return out
Please register or sign in to comment