# polynome.py

Created by cent20

Created on September 19, 2020

4 KB

Version 4ko. Beta à tester.Presque pep8. Version 01/01/2020 10h20

```# https://nsi.xyz , Arthur Jacquin, Kevin Fedyna, Vincent Robert.

from math import sqrt

a = b = c = d = 0
P, I = print, input

def entete():
global a
P("\n"*9)
P("Polynome (equation) degre 2"+"\nP(x)=ax^2+bx+c (=0)"*(a==0)+"\nP(x)={}{}{} (=0)".format(M(a, a="x^2", r=4), M(b, s=1, a="x", r=4, n=1), M(c, s=1, r=4, n=1))*(a!=0)+"\n")
if a == 0: menu1() ; entete()

entete()
P("1. Changer les valeurs a,b,c")
P("2. Discriminant d = ",M(d, r=4),"<"*(d<0)+">"*(d>0)+"="*(d==0)+" 0")
P("3. Racine"+"s"*(d!=0)+" reelle"*(d>=0)+"s"*(d>0)+" double"*(d==0)+" complexes conj"*(d<0)+" :"+" 1"*(d==0)+" 2"*(d!=0))
P("4. Signe : "+"-"*(d<0 and a<0)+"+"*(d<0 and a>0)+"- 0 -"*(d==0 and a<0)+"+ 0 +"*(d==0 and a>0)+"- 0 + 0 -"*(d>0 and a<0)+"+ 0 - 0 +"*(d>0 and a>0))
P("5. Factorisation dans "+"C"*(d<0)+"R"*(d>=0))
P("6. Quitter\n\n")
choix = In("Choix",1,6)

global a, b, c, d, e, nb, x, y, x1, x2
a = 0 ; P("\n"*3)
while a == 0:a = M(In("a"))
b, c = M(In("b")), M(In("c"))
d = M(float(b**2-4*a*c))
e = M(float(a*(-b/(2*a))**2+b*(-b/(2*a))+c))
nb = d and 2 or 1
x, y = M((-b)/(2*a)), M((sqrt(abs(d)))/(2 * a))
x1, x2 = M(min(x+y,x-y)), M(max(x-y,x+y))

P("2. Calcul du discriminant :\n")
P("d = b^2 - 4*a*c")
P("  = {}^2 - 4*{}*{}".format(M(b,p=1),M(a,p=1),M(c,p=1)))
P(M(-4*a*c,b="  = ")*(b==0 and c!=0)+("  = {}{}".format(M(b**2),M(-4*a*c,s=1,n=1)))*(not(b==0 and c!=0)))
P(M(d,b="d = ")+(" < "*(d<0)+" > "*(d>0)+"0")*(d!=0)+"\n\n")
I()

m="3. Calcul des racines :\n"
P(m,"d "+"<"*(d<0)+"="*(d==0)+">"*(d>0)+" 0 donc il existe")
P("2"*(d!=0)+"1"*(d==0)+" racine"+"s"*(d!=0)+" reelle"*(d>=0)+"s"*(d>0)+" double"*(d==0)+" complexes conjugees"*(d<0)+" :")
P("\n"+"z1"*(d<0)+"x1"*(d>=0)+" = x2"*(d==0)+" = -b/2a","+ sqrt(|d|)/2a"*(d!=0)+" i"*(d<0))
P("\n"+"z2"*(d<0)+"x2"*(d>0)+" = -b/2a"*(d!=0),"- sqrt(|d|)/2a"*(d!=0)+" i"*(d<0)+"\n")
I() ; entete() ; P(m)
s = ["+", "-"]
if d < 0:
for i in [0,1]:
P("z"+str(i+1)+" = -b/2a "+s[i]+" sqrt(|d|)/2a i")
P("   = (-{0})/(2*{1}) {3} sqrt(|{2}|)/(2*{1}) i".format(M(b, p=1, r=4),M(a, p=1,r=4),M(d, r=4),s[i]))
P("   = {} {} {} i".format(x,s[i],M(y,p=1)))
elif d == 0:
P("x1 = x2 = -b/2a")
P("   = (-{})/(2*{})".format(M(b, p=1, r=5), M(a, p=1, r=5)))
P(M(x,b="   = "))
P("\n"*2)
elif d > 0:
for i in [0, 1]:
P("x"+str(i+1)+" = (-b"+s[i-1*(a>0)]+"sqrt(d))/(2a)")
P("   = (-{}{}sqrt({}))/(2*{})".format(M(b, p=1, r=4), s[i-1*(a>0)], M(d, r=4), M(a, p=1, r=4)))
P(M(eval("x"+s[i-1*(a>0)]+"abs(y)"),b="   = "))
I()

if d <= 0:
P("  x |    -b/(2a)    |"+"\n"+"-"*21)
P("P(x)|  "+"+    m    +"*(a>0)+"-    M    -"*(a<0)+"  |\n"+"-"*21)
else:
P("  x |   x1  -b/(2a)  x2   |"+"\n"+"-"*27)
P("P(x)| "+"+ 0  -   m   -  0 +"*(a>0)+"- 0  +  M  +  0 -"*(a<0)+" |\n"+"-"*27)
P("Extremum :\n"+"  (", M(-b / (2 * a), r=4), ";", M(e, r=4), ")\n\n")
I()

if d >= 0:
P("P(x) = a (x-x1)^2"*(d==0)+"P(x) = a (x-x1) (x-x2)"*(d>0)+"\nAvec:")
P(M(a, "a = "))
P(M(-x1, "x-x1 = x", r=4, n=2, s=1))
P(M(-x2, "x-x2 = x", r=4, n=2, s=1)*(d!=0))
P("P(x) = "+str(a)*(a!=1)+("x^2"*(x1==0)+("(x"+M(-x1, s=1)+")^2")*(x1!=0))*(d==0)+("(x"+M(-x1, s=1, n=1)+")(x"+M(-x2, s=1, n=1)+")")*(d>0)+"\n"*2)
else:
P("P(z) = a(z-z1)(z-z2)\nAvec:")
P(M(a, "  a  = "))
P(M(-x, "z-z1 = z", M(y, a="i", r=4, n=1, s=1), r=4, n=2, s=1))
P(M(-x, "z-z2 = z", M(-y, a="i", r=4, n=1, s=1), r=4, n=2, s=1)+"\n"*3)
I()

def M(v, b="", a="", s=0, r=8, n=0, p=0):
# Magie ! value, before, after, signe, round, null, par
if not v and n in [1, 2, 3]: return (str(b)+str(a)*(n!=3))*(n!=1)
if v == int(v): v = int(v)
else: v = round(v, r)
if (b, a, s, p) == ("", "", 0, 0): return v
if s and v > 0: v = "+"+str(v)
if p and v < 0: v = "("+str(v)+")"
return str(b) + str(v) + str(a)

def In(t, m=-2**30, M=2**30):
# str, min, Max
r = v = 2**31
while not(m <= v <= M):
try: v = float(input("\t"+t+" = "))
except: v = r
return v