Skip to content

Instantly share code, notes, and snippets.

@jmlon
Created November 5, 2021 20:28
Show Gist options
  • Save jmlon/9f8faba289cba624edb4012efc892402 to your computer and use it in GitHub Desktop.
Save jmlon/9f8faba289cba624edb4012efc892402 to your computer and use it in GitHub Desktop.
# Módulo para funciones matemáticas en los complejos
# https://docs.python.org/3/library/cmath.html
import cmath
# Ingresar complejos en la forma 1+2j
a = complex(input("Ingrese a: "))
b = complex(input("Ingrese b: "))
c = complex(input("Ingrese c: "))
# En este caso no hay precondiciones con respecto a los valores a,b,c
x1 = (-b+cmath.sqrt(b**2-4*a*c))/(2*a)
x2 = (-b-cmath.sqrt(b**2-4*a*c))/(2*a)
print('Primera raiz ', x1)
print('Segunda raiz ', x2)
# Comprobar las postcondiciones
(r1,phi1) = cmath.polar(a*x1**2 + b*x1 + c)
assert r1 < 0.000000001, "La primera raiz no cumple"
(r2,phi2) = cmath.polar(a*x2**2 + b*x2 + c)
assert r2 < 0.000000001, "La segunda raiz no cumple"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment