The polynomial roots expressions in sympy do not seem to work for edge cases (when the coefficient for the higher degree is 0). Can anybody explain why? Can we use /rely on the expressions for the roots?
import sympy as sy
lam = sy.symbols("lambda")
a_0, a_1, a_2, a_3 = sy.symbols("a_0 a_1 a_2 a_3")
eq = sy.Eq(a_3 * (lam ** 3) + a_2 * (lam ** 2) + a_1 * (lam ** 1) + a_0, 0)
polynome_solutions = sy.solve(eq, lam)
print(polynome_solutions)
for solution in polynome_solutions:
print(solution.subs([(a_3, 0), (a_2, 1), (a_1, 0), (a_0, -1)]))
lam = sy.symbols("lambda")
a_0, a_1, a_2, a_3, a_4 = sy.symbols("a_0 a_1 a_2 a_3 a_4")
eq = sy.Eq(a_4 * (lam ** 4) + a_3 * (lam ** 3) + a_2 * (lam ** 2) + a_1 * (lam ** 1) + a_0, 0)
polynome_solutions = sy.solve(eq, lam)
print(polynome_solutions)
for solution in polynome_solutions:
print(solution.subs([(a_4, 1), (a_3, 0), (a_2, 0), (a_1, 0), (a_0, -1)]))
The results:
[-a_2/(3*a_3) - (-3*a_1/a_3 + a_2**2/a_3**2)/(3*(27*a_0/(2*a_3) - 9*a_1*a_2/(2*a_3**2) + a_2**3/a_3**3 + sqrt(-4*(-3*a_1/a_3 + a_2**2/a_3**2)**3 + (27*a_0/a_3 - 9*a_1*a_2/a_3**2 + 2*a_2**3/a_3**3)**2)/2)**(1/3)) - (27*a_0/(2*a_3) - 9*a_1*a_2/(2*a_3**2) + a_2**3/a_3**3 + sqrt(-4*(-3*a_1/a_3 + a_2**2/a_3**2)**3 + (27*a_0/a_3 - 9*a_1*a_2/a_3**2 + 2*a_2**3/a_3**3)**2)/2)**(1/3)/3, -a_2/(3*a_3) - (-3*a_1/a_3 + a_2**2/a_3**2)/(3*(-1/2 - sqrt(3)*I/2)*(27*a_0/(2*a_3) - 9*a_1*a_2/(2*a_3**2) + a_2**3/a_3**3 + sqrt(-4*(-3*a_1/a_3 + a_2**2/a_3**2)**3 + (27*a_0/a_3 - 9*a_1*a_2/a_3**2 + 2*a_2**3/a_3**3)**2)/2)**(1/3)) - (-1/2 - sqrt(3)*I/2)*(27*a_0/(2*a_3) - 9*a_1*a_2/(2*a_3**2) + a_2**3/a_3**3 + sqrt(-4*(-3*a_1/a_3 + a_2**2/a_3**2)**3 + (27*a_0/a_3 - 9*a_1*a_2/a_3**2 + 2*a_2**3/a_3**3)**2)/2)**(1/3)/3, -a_2/(3*a_3) - (-3*a_1/a_3 + a_2**2/a_3**2)/(3*(-1/2 + sqrt(3)*I/2)*(27*a_0/(2*a_3) - 9*a_1*a_2/(2*a_3**2) + a_2**3/a_3**3 + sqrt(-4*(-3*a_1/a_3 + a_2**2/a_3**2)**3 + (27*a_0/a_3 - 9*a_1*a_2/a_3**2 + 2*a_2**3/a_3**3)**2)/2)**(1/3)) - (-1/2 + sqrt(3)*I/2)*(27*a_0/(2*a_3) - 9*a_1*a_2/(2*a_3**2) + a_2**3/a_3**3 + sqrt(-4*(-3*a_1/a_3 + a_2**2/a_3**2)**3 + (27*a_0/a_3 - 9*a_1*a_2/a_3**2 + 2*a_2**3/a_3**3)**2)/2)**(1/3)/3]
nan
nan
nan
[Piecewise((-a_3/(4*a_4) - sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3))/2 - sqrt(-4*a_2/(3*a_4) + a_3**2/(2*a_4**2) + (2*a_1/a_4 + 2*a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)/sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3)) + 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3))/2, Eq(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12, 0)), (-a_3/(4*a_4) - sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3))/2 - sqrt(-4*a_2/(3*a_4) + a_3**2/(2*a_4**2) + (2*a_1/a_4 + 2*a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)/sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) - 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3))/2, True)), Piecewise((-a_3/(4*a_4) - sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3))/2 + sqrt(-4*a_2/(3*a_4) + a_3**2/(2*a_4**2) + (2*a_1/a_4 + 2*a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)/sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3)) + 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3))/2, Eq(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12, 0)), (-a_3/(4*a_4) - sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3))/2 + sqrt(-4*a_2/(3*a_4) + a_3**2/(2*a_4**2) + (2*a_1/a_4 + 2*a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)/sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) - 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3))/2, True)), Piecewise((-a_3/(4*a_4) + sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3))/2 - sqrt(-4*a_2/(3*a_4) + a_3**2/(2*a_4**2) - (2*a_1/a_4 + 2*a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)/sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3)) + 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3))/2, Eq(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12, 0)), (-a_3/(4*a_4) + sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3))/2 - sqrt(-4*a_2/(3*a_4) + a_3**2/(2*a_4**2) - (2*a_1/a_4 + 2*a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)/sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) - 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3))/2, True)), Piecewise((-a_3/(4*a_4) + sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3))/2 + sqrt(-4*a_2/(3*a_4) + a_3**2/(2*a_4**2) - (2*a_1/a_4 + 2*a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)/sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3)) + 2*((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**(1/3))/2, Eq(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12, 0)), (-a_3/(4*a_4) + sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3))/2 + sqrt(-4*a_2/(3*a_4) + a_3**2/(2*a_4**2) - (2*a_1/a_4 + 2*a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)/sqrt(-2*a_2/(3*a_4) + a_3**2/(4*a_4**2) - 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) + 2*(-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)/(3*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3)) - 2*(-(a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/6 + (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/16 + (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/216 + sqrt((-a_0/a_4 + a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**2/12)**3/27 + ((a_0/a_4 - a_3*(a_1/(4*a_4) + a_3*(-a_2/(16*a_4) + 3*a_3**2/(256*a_4**2))/a_4)/a_4)*(a_2/a_4 - 3*a_3**2/(8*a_4**2))/3 - (a_1/a_4 + a_3*(-a_2/(2*a_4) + a_3**2/(8*a_4**2))/a_4)**2/8 - (a_2/a_4 - 3*a_3**2/(8*a_4**2))**3/108)**2/4))**(1/3))/2, True))]
0
0
0
0
You seem to be solving a general third degree polynomial. One step of the solution normalizes the polynomial to start with 1, so dividing by a_3. The later substitution of a_3 by zero causes a divide by zero which is expressed as 'nan' ("not a number").
Answer to the edited question:
Well, there doesn't exist a general algebraic solution for the fourth degree polynomial. So, the best you can hope for, is to give sympy a specific higher degree polynomial and hope that one has a solution. Sympy just tries to do its best, and for that it has to make some assumptions, but it isn't always correct. No computational algebra system is perfect, and sympy certainly isn't the most sophisticated one for all purposes, but it's free and most importantly, open source.
Can we use /rely on the expressions for the roots?
Writing the equation with six unknowns, sympy doesn't differentiate between which one would be called an unknown and which would be called a parameter. Some assumptions are made to be able to make progress.
So, can we rely 100% on sympy's results? No, certainly not. Solutions need to be checked, and evaluated for fitness for the specific application. We can be pretty sure it works well for univariate polynomial equations. For more complicated cases, one often tops with the limitations of a computational algebra.
Looking at what Wolfram Alpha does with this specific equation, although I didn't check it, seems more appropriate, though very involved, and quite difficult to use in further calculations. Wolfram Alpha does seem to differentiate between parameters and unknown unknowns, which sometimes obliges you to use specific names for the parameters/unknowns. See for example what it does with the rewritten equation. Especially this rewrite doesn't return what you would hope for.
Your encouraged to test out your specific kind of equation in alternative systems and report cases in sympy's github for interesting examples where sympy doesn't perform as well as it should.
Related
I would like to use the output (object) as a attribute of another resources.
I have the module like below:
locals {
lb_domain = {
lb_public = {
domain = "dev.example.net"
},
lb_internal = {
domain = "dev.internal.example.net"
}
}
module "dev_acm" {
source = "terraform-aws-modules/acm/aws"
version = "3.5.0"
for_each = local.lb_domain
domain_name = each.value.domain
zone_id = data.aws_route53_zone.this.id
}
And output of this module. this is the object, I would like to use this for another resources module.dev_acm:
+ dev_acm = {
+ lb_internal = {
+ acm_certificate_arn = (known after apply)
+ acm_certificate_domain_validation_options = [
+ {
+ domain_name = "dev.internal.example.net"
+ resource_record_name = (known after apply)
+ resource_record_type = (known after apply)
+ resource_record_value = (known after apply)
},
]
+ acm_certificate_status = (known after apply)
+ acm_certificate_validation_emails = (known after apply)
+ distinct_domain_names = [
+ "dev.internal.example.net",
]
+ validation_domains = (known after apply)
+ validation_route53_record_fqdns = [
+ (known after apply),
]
}
+ lb_public = {
+ acm_certificate_arn = (known after apply)
+ acm_certificate_domain_validation_options = [
+ {
+ domain_name = "dev.example.net"
+ resource_record_name = (known after apply)
+ resource_record_type = (known after apply)
+ resource_record_value = (known after apply)
},
]
+ acm_certificate_status = (known after apply)
+ acm_certificate_validation_emails = (known after apply)
+ distinct_domain_names = [
+ "dev.example.net",
]
+ validation_domains = (known after apply)
+ validation_route53_record_fqdns = [
+ (known after apply),
]
}
}
How I can use acm_certificate_arn public lb_public in the output for another services?
Something like that: module.dev_acm.lb_internal.acm_certificate_arn
That's an extremely strange way of looking at a Terraform module output. I suggest looking at the documentation for the module you are using, instead of looking at the output that way. Not to mention what you are looking at doesn't indicate that certain things are created via for_each.
How I can use acm_certificate_arn in the output for another services?
Something like that: module.dev_acm.lb_internal.acm_certificate_arn
Taking the documantion I linked, and the documentation for referencing for_each instances, it would be:
module.dev_acm["lb_public"].acm_certificate_arn
or
module.dev_acm["lb_internal"].acm_certificate_arn
I have been working with textract for this past month using boto3, when I tried using the QUERIES feature this would throw an error saying that queries config is not a valid parameter so I decided to go with the amazon service API, but I get as a response;here my code, I;m following the docs to get the signature.
import requests
import boto3
import sys, os, base64, datetime, hashlib, hmac
import json
session = boto3.Session()
credentials = session.get_credentials()
credentials = credentials.get_frozen_credentials()
access_key = credentials.access_key
secret_key = credentials.secret_key
print()
method = 'POST'
service = 'textract'
amz_target = 'Textract.StartDocumentAnalysis'
host = 'textract.us-east-1.amazonaws.com'
region = 'us-east-1'
endpoint = 'https://textract.us-east-1.amazonaws.com'
canonical_uri = '/'
content_type = 'application/x-amz-json-1.1'
# request_parameters = 'Action=DescribeRegions&Version=2013-10-15'
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(key, dateStamp, regionName, serviceName):
kDate = sign(('AWS4' + key).encode('utf-8'), dateStamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
return kSigning
# Create a date for headers and the credential string
t = datetime.datetime.utcnow()
amz_date = t.strftime('%Y%m%dT%H%M%SZ')
date_stamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope
amz_target = 'Textract.'
canonical_headers = 'content-type:' + content_type + '\n' + 'host:' + host + '\n' + 'x-amz-date:' + amz_date + '\n' + 'x-amz-target:' + amz_target + '\n'
signed_headers = 'content-type;host;x-amz-date;x-amz-target'
canonical_querystring = ''
payload = {
"DocumentLocation": {
"S3Object": {
"Bucket": "",
"Name": "",
}
},
"FeatureTypes": [ "FORMS" ],
"JobTag": "123",
"NotificationChannel": {
"RoleArn": "",
"SNSTopicArn": ""
},
"QueriesConfig": {
"Queries": [
{
"Alias": "string",
"Pages": [ "1-4" ],
"Text": "what is the policy number ?"
}
]
}
}
payload_hash = hashlib.sha256(json.dumps(payload).encode('utf-8')).hexdigest()
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = date_stamp + '/' + region + '/' + service + '/' + 'aws4_request'
string_to_sign = algorithm + '\n' + amz_date + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
signing_key = getSignatureKey(secret_key, date_stamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
headers = {'Content-Type':content_type,
'X-Amz-Date':amz_date,
'X-Amz-Target':amz_target,
'Authorization':authorization_header}
print('Request URL = ' + endpoint)
r = requests.post("https://textract.us-east-1.amazonaws.com", data=json.dumps(payload), headers=headers)
print(r.text)
I have a powershell script that I am running through ExtendScript (Photoshop) in order to upload files to an s3 bucket.
I have this code to upload files to an aws s3 bucket.
However, it only works for smaller files (works on a 50mb file)
But does not work on a 140mb file. Shows no error but the file is not uploaded.
Any ideas?
$_rawfilename = 'C:/Users/DELL/AppData/Local/Temp/Filled_Albedo.exr'
$folder = 'seam-removal'
$filename = 'Filled_Albedo.exr'
$keyFile = ($folder+ '/' + $filename)
$service = 's3'
$bucket = '**'
$region = 'us-west-2'
$host1 = $bucket + '.s3' + '.amazonaws.com'
$access_key = '**'
$secret_key = '**'
$br = [regex]::Unescape('\u000a')
function HmacSHA256($message, $secret) {
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = $secret
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($message))
return $signature
}
function getSignatureKey($key, $dateStamp, $regionName, $serviceName) {
$kSecret = [Text.Encoding]::UTF8.GetBytes(('AWS4' + $key).toCharArray())
$kDate = HmacSHA256 $dateStamp $kSecret
$kRegion = HmacSHA256 $regionName $kDate
$kService = HmacSHA256 $serviceName $kRegion
$kSigning = HmacSHA256 'aws4_request' $kService
return $kSigning
}
function hash($request) {
$hasher = [System.Security.Cryptography.SHA256]::Create()
$content = [Text.Encoding]::UTF8.GetBytes($request)
$bytes = $hasher.ComputeHash($content)
return ($bytes | ForEach-Object ToString x2) -join ''
}
function requestBuilder($method, $key) {
$now = [DateTime]::UtcNow
$amz_date = $now.ToString('yyyyMMddTHHmmssZ')
$datestamp = $now.ToString('yyyyMMdd')
$signed_headers = 'host'
$credential_scope = $datestamp + '/' + $region + '/' + $service + '/' + 'aws4_request'
$canonical_querystring = ''
$canonical_querystring = 'X-Amz-Algorithm=AWS4-HMAC-SHA256'
$canonical_querystring += '&X-Amz-Credential=' + [uri]::EscapeDataString(($access_key + '/' + $credential_scope))
$canonical_querystring += '&X-Amz-Date=' + $amz_date
$canonical_querystring += '&X-Amz-Expires=86400'
$canonical_querystring += '&X-Amz-SignedHeaders=' + $signed_headers
$canonical_headers = 'host:' + $host1 + $br
$canonical_request = $method + $br
$canonical_request += '/' + $key + $br
$canonical_request += $canonical_querystring + $br
$canonical_request += $canonical_headers + $br
$canonical_request += $signed_headers + $br
$canonical_request += 'UNSIGNED-PAYLOAD'
$algorithm = 'AWS4-HMAC-SHA256'
$canonical_request_hash = hash -request $canonical_request
$string_to_sign = $algorithm + $br
$string_to_sign += $amz_date + $br
$string_to_sign += $credential_scope + $br
$string_to_sign += $canonical_request_hash
$signing_key = getSignatureKey $secret_key $datestamp $region $service
$signature = HmacSHA256 -secret $signing_key -message $string_to_sign
$signature = ($signature|ForEach-Object ToString x2) -join ''
$canonical_querystring += '&X-Amz-Signature=' + $signature
$request_url = 'http://' + $host1 + '/' + $key + '?' + $canonical_querystring
Write-Host $request_url
return $request_url
}
#C# class to create callback
$code = #"
public class SSLHandler
{
public static System.Net.Security.RemoteCertificateValidationCallback GetSSLHandler()
{
return new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });
}
}
"#
#compile the class
Add-Type -TypeDefinition $code
#disable checks using new class
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = [SSLHandler]::GetSSLHandler()
#do the request
try
{
Invoke-RestMethod -Method PUT -Uri (requestBuilder 'PUT' $keyFile) -InFile $_rawfilename
} catch {
# do something
} finally {
#enable checks again
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {true}
}
Started working after making the changes suggested in the above comments (verbose and write host).
I want to upload a file to AWS S3 with AES256 encryption however I am not allowed to install the AWS CLI. I have the code below which allows me to upload files to S3 using my secret keys however this does not work then encryption is required for the S3 bucket. How do I perform something similar to: aws s3api put-object --server-side-encryption=AES256 --bucket=<bucket_name> --key=<name_of_object_when_uploaded> --body=/<path>/<object_to_upload> with my code below?
# Config Parts
$_rawfilename = 'C:/<NAME>/SSP00001_RITM1304145.csv'
$folder = 'TestResults'
$filename = $_rawfilename.Split('/')[2]
$keyFile = ($folder+ '/' + $filename)
$service = 's3'
$bucket = '<BUCKET NAME>'
$region = 'us-east-1'
$host1 = $bucket + '.s3' + '.amazonaws.com'
$access_key = ''
$secret_key = ''
$br = [regex]::Unescape('\u000a')
function HmacSHA256($message, $secret) {
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = $secret
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($message))
return $signature
}
function getSignatureKey($key, $dateStamp, $regionName, $serviceName) {
$kSecret = [Text.Encoding]::UTF8.GetBytes(('AWS4' + $key).toCharArray())
$kDate = HmacSHA256 $dateStamp $kSecret
$kRegion = HmacSHA256 $regionName $kDate
$kService = HmacSHA256 $serviceName $kRegion
$kSigning = HmacSHA256 'aws4_request' $kService
return $kSigning
}
function hash($request) {
$hasher = [System.Security.Cryptography.SHA256]::Create()
$content = [Text.Encoding]::UTF8.GetBytes($request)
$bytes = $hasher.ComputeHash($content)
return ($bytes | ForEach-Object ToString x2) -join ''
}
function requestBuilder($method, $key) {
$now = [DateTime]::UtcNow
$amz_date = $now.ToString('yyyyMMddTHHmmssZ')
$datestamp = $now.ToString('yyyyMMdd')
$signed_headers = 'host'
$credential_scope = $datestamp + '/' + $region + '/' + $service + '/' + 'aws4_request'
$canonical_querystring = ''
$canonical_querystring = 'X-Amz-Algorithm=AWS4-HMAC-SHA256'
$canonical_querystring += '&X-Amz-Credential=' + [uri]::EscapeDataString(($access_key + '/' + $credential_scope))
$canonical_querystring += '&X-Amz-Date=' + $amz_date
$canonical_querystring += '&X-Amz-Expires=86400'
$canonical_querystring += '&X-Amz-SignedHeaders=' + $signed_headers
$canonical_headers = 'host:' + $host1 + $br
$canonical_request = $method + $br
$canonical_request += '/' + $key + $br
$canonical_request += $canonical_querystring + $br
$canonical_request += $canonical_headers + $br
$canonical_request += $signed_headers + $br
$canonical_request += 'UNSIGNED-PAYLOAD'
$algorithm = 'AWS4-HMAC-SHA256'
$canonical_request_hash = hash -request $canonical_request
$string_to_sign = $algorithm + $br
$string_to_sign += $amz_date + $br
$string_to_sign += $credential_scope + $br
$string_to_sign += $canonical_request_hash
$signing_key = getSignatureKey $secret_key $datestamp $region $service
$signature = HmacSHA256 -secret $signing_key -message $string_to_sign
$signature = ($signature|ForEach-Object ToString x2) -join ''
$canonical_querystring += '&X-Amz-Signature=' + $signature
$request_url = 'http://' + $host1 + '/' + $key + '?' + $canonical_querystring
Write-Host $request_url
return $request_url
}
# Where -InFile is Path/to/xlsx
Invoke-RestMethod -Method PUT -Uri (requestBuilder 'PUT' $keyFile) -InFile $_rawfilename
Start-Sleep -s 2
I tried adding $canonical_querystring += '&X-amz-server-side-encryption-customer-algorithm=AES256' to the code however it's still not working:
$canonical_querystring = ''
$canonical_querystring = 'X-Amz-Algorithm=AWS4-HMAC-SHA256'
$canonical_querystring += '&X-Amz-Credential=' + [uri]::EscapeDataString(($access_key + '/' + $credential_scope))
$canonical_querystring += '&X-Amz-Date=' + $amz_date
$canonical_querystring += '&X-Amz-Expires=86400'
**$canonical_querystring += '&X-amz-server-side-encryption-customer-algorithm=AES256'**
$canonical_querystring += '&X-Amz-SignedHeaders=' + $signed_headers
You have to add x-amz-server-side-encryption header to your request ($canonical_headers).
See:
https://docs.aws.amazon.com/AmazonS3/latest/userguide/specifying-s3-encryption.html
https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
I want to download a file from my AWS S3 bucket using Windows Powershell. I cannot install any AWS software and need to create an API to be able to access a file in AWS S3. I used Postman for testing that the file is accessible and it was successful.
Given this success I tried following AWS' guide which says that I need to create the following:
Create a canonical request.
Use the canonical request and additional metadata to create a string
for signing.
Derive a signing key from your AWS secret access key. Then use the
signing key, and the string from the previous step, to create a
signature.
Add the resulting signature to the HTTP request in a header or as a
query string parameter.
The closest I've seen is this example from https://forums.aws.amazon.com/thread.jspa?threadID=251722 by Abhaya however it is also unresolved. (The payload hash in this example is the payload hash for blank). I have gone through several AWS guides but these are very confusing when trying to apply them to powershell. https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
The url the code below generates a url that looks correct:http://SAMPLEBUCKETNAME HERE.s3-ap-southeast-1.amazonaws.com/test.xlsx?&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=%2F20190907%2Fap-southeast-1%2Fs3%2Faws4_request&X-Amz-Date=20190907T1644136560000Z&X-Amz-E0&X-Amz-SignedHeaders=host&X-Amz-Signature=HASH HERE
$method = 'GET'
$service = 's3'
$host1 = 'SAMPLES3BUCKETNAME.s3-ap-southeast-1.amazonaws.com'
$region = 'ap-southeast-1'
$endpoint = 'http://SAMPLES3BUCKETNAME.s3-ap-southeast-1.amazonaws.com/test.xlsx'
function HmacSHA256($message, $secret){
<#$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::UTF8.GetBytes($secret)
#$hmacsha.key = $secret
$signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($message))
$signature = [Convert]::ToBase64String($signature)
#>
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.Key = #($secret -split '(?<=\G..)(?=.)'|ForEach-Object {[byte]::Parse($_,'HexNumber')})
$sign = [BitConverter]::ToString($hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($message))).Replace('-','').ToLower()
return $sign
}
function getSignatureKey($key, $dateStamp, $regionName, $serviceName)
{
$kSecret = [Text.Encoding]::UTF8.GetBytes(("AWS4" + $key).toCharArray())
$kDate = HmacSHA256 $dateStamp $kSecret;
$kRegion = HmacSHA256 $regionName $kDate ;
$kService = HmacSHA256 $serviceName $kRegion ;
$kSigning = HmacSHA256 "aws4_request" $kService ;
return $kSigning;
}
$access_key = 'SAMPLEACCESSKEY'
$secret_key = 'SAMPLESECRETKEY'
$amz_date = [DateTime]::UtcNow.ToString('yyyyMMddTHHmmssfffffffZ')
$datestamp = [DateTime]::UtcNow.ToString('yyyyMMdd')
$canonical_uri = '/'
$canonical_headers = 'host:' + $host1 + "`n"
$signed_headers = 'host'
$algorithm = 'AWS4-HMAC-SHA256'
$credential_scope = $datestamp + '/' + $region + '/' + $service + '/' + 'aws4_request'
$canonical_querystring = ''
$canonical_querystring += '&X-Amz-Algorithm=AWS4-HMAC-SHA256'
$canonical_querystring += '&X-Amz-Credential=' + [uri]::EscapeDataString(($access_key + '/' + $credential_scope))
$canonical_querystring += '&X-Amz-Date=' + $amz_date
$canonical_querystring += '&X-Amz-Expires=86400'
$canonical_querystring += '&X-Amz-SignedHeaders=' + $signed_headers
$payload_hash = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
$canonical_request1 = $method + "`n" +$canonical_uri + "`n" + $canonical_querystring + "`n" + $canonical_headers + "`n" + $signed_headers + "`n" + $payload_hash
Write-Host $canonical_request1
function hash($request) {
$hasher = [System.Security.Cryptography.SHA256]::Create()
$content = [Text.Encoding]::UTF8.GetBytes($request)
$hash = [System.Convert]::ToBase64String($hasher.ComputeHash($content))
return $hash
}
$canonical_request = hash -request $canonical_request1
$string_to_sign = $algorithm + "`n" + $amz_date + "`n" + $credential_scope + "`n" + $canonical_request
$signing_key = getSignatureKey $secret_key $datestamp $region $service
$signature = HmacSHA256 -secret $signing_key -message $string_to_sign
$canonical_querystring += '&X-Amz-Signature=' + $signature
$request_url = $endpoint + "?" + $canonical_querystring
$request_url
I get the following error when I try accessing the url.
There were a few errors, notably how you were computing the signature, building the timestamp, and the error that you were seeing is because the parameters weren't properly being passed along.
Here's a version that corrects those issues:
$method = 'GET'
$service = 's3'
$bucket = "SAMPLES3BUCKETNAME"
$key = 'test.xlsx'
$region = 'ap-southeast-1'
$host1 = $bucket + '.s3-' + $region + '.amazonaws.com'
$access_key = 'SAMPLEACCESSKEY'
$secret_key = 'SAMPLESECRETKEY'
function HmacSHA256($message, $secret)
{
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = $secret
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($message))
return $signature
}
function getSignatureKey($key, $dateStamp, $regionName, $serviceName)
{
$kSecret = [Text.Encoding]::UTF8.GetBytes(("AWS4" + $key).toCharArray())
$kDate = HmacSHA256 $dateStamp $kSecret;
$kRegion = HmacSHA256 $regionName $kDate;
$kService = HmacSHA256 $serviceName $kRegion;
$kSigning = HmacSHA256 "aws4_request" $kService;
return $kSigning
}
function hash($request)
{
$hasher = [System.Security.Cryptography.SHA256]::Create()
$content = [Text.Encoding]::UTF8.GetBytes($request)
$bytes = $hasher.ComputeHash($content)
return ($bytes|ForEach-Object ToString x2) -join ''
}
$now = [DateTime]::UtcNow
$amz_date = $now.ToString('yyyyMMddTHHmmssZ')
$datestamp = $now.ToString('yyyyMMdd')
$signed_headers = 'host'
$credential_scope = $datestamp + '/' + $region + '/' + $service + '/' + 'aws4_request'
$canonical_querystring = 'X-Amz-Algorithm=AWS4-HMAC-SHA256'
$canonical_querystring += '&X-Amz-Credential=' + [uri]::EscapeDataString(($access_key + '/' + $credential_scope))
$canonical_querystring += '&X-Amz-Date=' + $amz_date
$canonical_querystring += '&X-Amz-Expires=86400'
$canonical_querystring += '&X-Amz-SignedHeaders=' + $signed_headers
$canonical_headers = 'host:' + $host1 + "`n"
$canonical_request = $method + "`n"
$canonical_request += "/" + $key + "`n"
$canonical_request += $canonical_querystring + "`n"
$canonical_request += $canonical_headers + "`n"
$canonical_request += $signed_headers + "`n"
$canonical_request += "UNSIGNED-PAYLOAD"
$algorithm = 'AWS4-HMAC-SHA256'
$canonical_request_hash = hash -request $canonical_request
$string_to_sign = $algorithm + "`n"
$string_to_sign += $amz_date + "`n"
$string_to_sign += $credential_scope + "`n"
$string_to_sign += $canonical_request_hash
$signing_key = getSignatureKey $secret_key $datestamp $region $service
$signature = HmacSHA256 -secret $signing_key -message $string_to_sign
$signature = ($signature|ForEach-Object ToString x2) -join ''
$canonical_querystring += '&X-Amz-Signature=' + $signature
$request_url = "http://" + $host1 + "/" + $key + "?" + $canonical_querystring
Write-Host $request_url