tinglingling - 1 year ago 82

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..

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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)
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**