def __pow__(self, autre): """ exponentiation """ if isinstance(autre, int): autre = fraction(po.polynome(mo.monome(ra.rationnel(autre))), \ po.polynome_un()) if isinstance(autre, fraction): if (self.__valide) and (autre.__valide): a = self.__num b = self.__denom p = autre.__num q = autre.__denom pv = p.valuation().lire_num().lire_valeur() qv = q.valuation() if (p.degre() == 0) and (q.degre() == 0): if pv < 0: a, b = b, a p = -p if not qv.est_un(): return fraction(po.polynome_err(), po.polynome_un()) return fraction(a**p, b**p) return fraction(po.polynome_err(), po.polynome_un())
def test_unitaire_8(visible=False): print("*** fraction: test_unitaire_8 ***") a = po.polynome(mo.monome(ra.rationnel(1), "a")) a = a.joindre(mo.monome(ra.rationnel(1), "b")) # if visible: print(a) b = po.polynome(mo.monome(ra.rationnel(3))) # if visible: print(b) c = po.polynome(mo.monome(ra.rationnel(5))) # if visible: print(c) f = frac.fraction(a, b) if visible: print(f) g = frac.fraction(c, po.polynome_un()) if visible: print(g) r = f**g if visible: print(r) print(r.joli()) ok = (r.lire_num().valuation() == ra.rationnel(1, 243)) return ok
def __init__(self, num =po.polynome_nul(), denom =po.polynome_un(), valide =True): """ constructeur """ valide = valide and (num.est_valide() and denom.est_valide()) if not valide: # normaliser les param. num = po.polynome_err() denom = po.polynome_un() if valide: if denom.est_polynome_nul(): valide = False num = po.polynome_err() denom = po.polynome_un() self.__num = num self.__denom = denom self.__valide = valide
def __truediv__(self, autre): """ quotient """ if isinstance(autre, fraction): if (self.__valide) and (autre.__valide): a = self.__num b = self.__denom p = autre.__num q = autre.__denom if p.est_polynome_nul(): return fraction(po.polynome_err(), po.polynome_un()) return fraction(a*q, b*p) return fraction(po.polynome_err(), po.polynome_un())
def test_unitaire_1(visible=False): print("*** fraction: test_unitaire_1 ***") f = frac.fraction(po.polynome_nul(), po.polynome_un(), False) if visible: print(f) ok = not f.est_valide() return ok
def fraction_depuis_lettre(lettre): """ construire une fraction rationnelle depuis une lettre """ if lettre in string.ascii_letters: p = po.polynome() p = p.joindre(mo.monome(ra.rationnel(1), str(lettre))) return fraction(p, po.polynome_un()) return fraction_err()
def __neg__(self): """ polynome oppose (inverse pour l'addition) """ if self.__valide: a = self.__num b = self.__denom return fraction(-a, b) return fraction(po.polynome_err(), po.polynome_un())
def __mul__(self, autre): """ produit """ if isinstance(autre, fraction): if (self.__valide) and (autre.__valide): a = self.__num b = self.__denom p = autre.__num q = autre.__denom return fraction(a*p, b*q) return fraction(po.polynome_err(), po.polynome_un())
def __add__(self, autre): """ somme """ if isinstance(autre, fraction): if self.__valide and autre.__valide: a = self.__num b = self.__denom p = autre.__num q = autre.__denom return fraction(a*q + b*p, b*q) return fraction(po.polynome_err(), po.polynome_un())
def reduire(self): """ reduction des coefficients """ if self.__valide: n = self.__num.ppcm_denominateurs() m = self.__denom.ppcm_denominateurs() d = ent.pgcd_entiers(n, m) r = ra.rationnel((m * n) // d) p = po.polynome() for k in self.__num.liste_decroissante_monomes(): c = k.lire_coeff() c *= r s = k.lire_indet() p = p.joindre(mo.monome(c, s)) q = po.polynome() for k in self.__denom.liste_decroissante_monomes(): c = k.lire_coeff() c *= r s = k.lire_indet() q = q.joindre(mo.monome(c, s)) if q.degre() == 0: n = q.valuation().lire_num().lire_valeur() m = q.valuation().lire_denom().lire_valeur() r = ra.rationnel(m, n) t = po.polynome() for k in p.liste_decroissante_monomes(): c = k.lire_coeff() c *= r s = k.lire_indet() t = t.joindre(mo.monome(c, s)) p = t q = po.polynome_un() self.__num = p self.__denom = q self.simplifier_coefficients()
def fraction_depuis_naturel(n): """ construire une fraction rationnelle depuis un entier naturel """ p = po.polynome() p = p.joindre(mo.monome(ra.rationnel(n))) return fraction(p, po.polynome_un())