Skip to content
DeepForgeby svx
All problems
HardCalculusca-289

Complex-Step Polynomial Gradient

Problem

Complex-step differentiation evaluates a polynomial at x + i*h and reads the derivative from the imaginary part: f'(x) approximately imag(f(x + i*h)) / h, accurate to machine precision without cancellation.

Given polynomial coefficients (coeffs[i] is the coefficient of x^i), x, and h (default 1e-20), return the complex-step derivative.

Hint

Accumulate powers of z = x + ih and take the imaginary part divided by h.

Test cases

case 1
in: [[1,2,3],2]
expected: 13.999999999999998
case 2
in: [[0,0,0,1],3]
expected: 26.999999999999996
case 3
in: [[5],7]
expected: 0
Starter codePython
def complex_step_polynomial_gradient(coeffs, x, h=1e-20):
    # Your code here
    pass

3 test cases · run your code against them in the editor