tinglingling - 3 months ago 27

Python Question

`def horner(x,coeffs):`

result = 0

deriv = 0

for a in coeffs:

result = x*result+a

deriv = x*deriv+result

return result,deriv

this is what I have got. But the value of deriv is not correct which I dont know why..

Answer

I use this function to differenciate:

```
def differenciate(coefficients):
return [c * (len(coefficients) - i) for i, c in enumerate(coefficients[:-1])]
```

and this function to evaluate a polinomial:

```
def call(coefficients, x):
return sum(x ** (len(coefficients) - i - 1) * c for i, c in enumerate(coefficients))
```

There might be no need to do both in one function. You can do

```
call(differenciate([1, 2, 3, 4]), 4)
```