I'm trying to shorten
to
but the output does not stay the same. What am I doing wrong? Is it something with the direction variable?
Code:
if f_1 == 0:
roots.append(critical_points[-1])
elif f_1 > 0:
x_0 = critical_points[-1]
x_1 = x_0 + 1
f_1 = f(polynomial, x_1)
if f_1 < f_0:
while f_1 > 0:
x_1 += 1
f_1 = f(polynomial, x_1)
if f_1 == 0:
roots.append(x_1)
else:
roots.append(bisection(polynomial, x_0, x_1, d))
else:
x_0 = critical_points[-1]
x_1 = x_0 + 1
f_1 = f(polynomial, x_1)
if f_1 > f_0:
while f_1 < 0:
x_1 += 1
f_1 = f(polynomial, x_1)
if f_1 == 0:
roots.append(x_1)
else:
roots.append(bisection(polynomial, x_0, x_1, d))
to
Code:
if f_1 == 0:
roots.append(critical_points[-1])
else:
direction = 1 if f_1 > 0 else -1
x_0 = critical_points[-1]
x_1 = x_0 + 1
f_1 = f(polynomial, x_1)
if f_1 * direction < f_0:
while f_1 * direction > 0:
x_1 += 1
f_1 = f(polynomial, x_1)
if f_1 == 0:
roots.append(x_1)
else:
roots.append(bisection(polynomial, x_0, x_1, d))
but the output does not stay the same. What am I doing wrong? Is it something with the direction variable?