maths_tableau_variation.py

Created by antarctus

Created on March 13, 2022

3.5 KB

pour les fonctions trinômiales


from math import *

class Trinome():
  def __init__(self,a,b,c):
    self.a=a
    self.b=b
    self.c=c

    self.sx=-b/(2*a)
    self.sy=self.f(self.sx)

    self.alpha=-b/(2*a)
    self.beta =self.f(self.alpha)
    self.delta=b**2-4*a*c
    self.nub_solution=self.delta>=0+self.delta>0

    self.str={
    "a"    :get_fract(str(self.a    )),
    "b"    :get_fract(str(self.b    )),
    "c"    :get_fract(str(self.c    )),
    "sx"   :get_fract(str(self.sx   )),
    "sy"   :get_fract(str(self.sy   )),
    "alpha":get_fract(str(self.alpha)),
    "beta" :get_fract(str(self.beta )),
    "delta":get_fract(str(self.delta)),
    }

    self.lenght=0
  
    if len(self.str["sy"])>len(self.str["sx"]):
      self.lenght=len(self.str["sy"])
    else:
      self.lenght=len(self.str["sx"])

  def f(self,x):
    return self.a*x**2+self.b*x+self.c

  def get_ante(self,num=0):
    c=self.c+num
    delta=self.b**2+-4*self.a*c
    nub_solution=((delta>=0)+(delta>0))

    if nub_solution==0: return []
    if nub_solution==1: return [self.alpha,]
    else: return [(-self.b-delta**0.5)/(2*self.a),(-self.b+delta**0.5)/(2*self.a)]

  def print_f(self):
    print("f(x)= {}x**2+ {}x+ {}".format(self.str["a"],self.str["b"],self.str["c"]))

  def print_canonique(self):
    print("f(x)={}(x-{})**2+{}".format(self.str["a"],self.str["sx"],self.str["sy"]))

  def tab(self):

    print()
    print("--Tableau:--")
    self.print_f()

    text=""" x  | -inf  {}  +inf
----|-------{}------"""

    if self.a>0:
      text+="""
    | \     {}    />
f(x)|  \    {}   /  
    |   \>  {}  /   """

    else:
      text+="""
    |   />  {}  \  
f(x)|  /    {}   \  
    | /     {}    \>"""

    print(text.format(
    self.str["sx"]+" "*(self.lenght-len(self.str["sx"])),
    "-"*self.lenght,
    " "*self.lenght,
    self.str["sy"]+" "*(self.lenght-len(self.str["sy"])),
    " "*self.lenght))

  def preuve(self):
    print()
    print("--Preuve:--")
    print("f(x) est un trinome avec a={}, b={} et c={}".format(self.str["a"],self.str["b"],self.str["c"]))
    if self.a>0:
      print("·la fonction f est oriente vers le haut car a>0")
    elif self.a<0:
      print("·la fonction f est oriente vers le bas car a<0")

    print("·-b/2a = -{}/(2*{}) = {}".format(self.str["a"],self.str["b"],self.str["sx"]))
    print("·f({}) = {}*x**2 + {}*x + {} = {}".format( self.str["sx"], self.str["a"], self.str["b"], self.str["c"], self.str["sy"]))

  def caract(self):
    print()
    print("--Caracteristiques:--")

    print("Sommet: ( {}; {})".format(self.str["sx"],self.str["sy"]))
    print("alpha: {} beta:{}".format(self.str["alpha"],self.str["beta"]))
    print("forme canonique:")
    self.print_canonique()
    print("antecedent de 0:")
    txt="0"
    while txt!="":
      ante=self.get_ante(eval(txt))
      if len(ante)==0:
        print("l'equation n'admet aucune solution")
      elif len(ante)==1:
        print("l'equation n'admet qu'une solution")
      elif len(ante)==2:
        print("l'equation admet deux solutions")
      for i in range(len(ante)):
        print("x{} = {}".format(i+1,get_fract(str(ante[i]))))
      print("Voulez-vous une autre recherche d'antecedent ?")
      txt=input("(ne mettez rien si non) : ")


def get_fract(txt):
  a=float(eval(txt))
  for i in range(1,5000):
    if round(a*i,0)==round(a*i,5):
      if i==1:
        return str(int(a))
      return "{}/{}".format(int(round(a*i,0)),i)
  return txt

a=float(eval(input("a:")))
b=float(eval(input("b:")))
c=float(eval(input("c:")))


f=Trinome(a,b,c)
f.preuve()
f.tab()
f.caract()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.