예제 #1
0
파일: Racine.py 프로젝트: Azixx/pyromaths
    def __add__(self, other):
        if isinstance(other, RacineDegre2):
            if self.radicande == other.radicande or self.radicande == 0 or other.radicande == 0:
                radicande = max(self.radicande, other.radicande)
                premier, second = self, other
            else:
                premier = self.simplifie()
                second = other.simplifie()
                if self.radicande == other.radicande or self.radicande == 0 or other.radicande == 0:
                    radicande = max(self.radicande, other.radicande)
                else:
                    return NotImplemented

            denominateur = ppcm(premier.denominateur, second.denominateur)
            facteur1 = denominateur / premier.denominateur
            facteur2 = denominateur / second.denominateur
            # if self.radicande==other.radicande:
            coeff = premier.coeff * facteur1 * (
                premier.radicande !=
                0) + second.coeff * facteur2 * (second.radicande != 0)
            if coeff == 0:
                radicande = 0
            return RacineDegre2(
                premier.numerateur * facteur1 + second.numerateur * facteur2,
                denominateur, coeff, radicande)
        elif isinstance(other, int):
            return self + RacineDegre2(other)
        elif isinstance(other, Fraction):
            return self + RacineDegre2(other.numerateur, other.denominateur)
예제 #2
0
파일: Racine.py 프로젝트: Lafrite/pyromaths
    def __add__(self, other):
        if isinstance(other, RacineDegre2):
            if self.radicande == other.radicande or self.radicande == 0 or other.radicande == 0:
                radicande = max(self.radicande, other.radicande)
                premier, second = self, other
            else:
                premier = self.simplifie()
                second = other.simplifie()
                if self.radicande == other.radicande or self.radicande == 0 or other.radicande == 0:
                    radicande = max(self.radicande, other.radicande)
                else:
                    return NotImplemented

            denominateur = ppcm(premier.denominateur, second.denominateur)
            facteur1 = denominateur / premier.denominateur
            facteur2 = denominateur / second.denominateur
            # if self.radicande==other.radicande:
            coeff = premier.coeff * facteur1 * (premier.radicande != 0) + second.coeff * facteur2 * (second.radicande != 0)
            if coeff == 0:
                radicande = 0
            return RacineDegre2(premier.numerateur * facteur1 + second.numerateur * facteur2,
                                denominateur,
                                coeff,
                                radicande)
        elif isinstance(other, int):
            return self +RacineDegre2(other)
        elif isinstance(other, Fraction):
            return self +RacineDegre2(other.numerateur, other.denominateur)
예제 #3
0
    def __sub__(self, *others):
        """*object*\ .\ **__sub__**\ (*other*)

        ``p.__sub__(q)`` est équivalent à ``p - q``  calcule la différence de deux fractions.

        *other* peut être une chaîne représentant une fraction, un entier ou un réel.

        Pour plus de détails, voir :py:func:`__add__`"""
        from pyromaths.classes.PolynomesCollege import Polynome
        lother, lden, traiter = [], [], False
        if self.code: traiter = True
        details, var = True, ''
        for other in others:
            if other == 0:
                pass
            elif isinstance(other, Polynome):
                var = other.var
                if details != 0 : details = min(details, other.details)
                else: details = other.details
                lother.append(other)
            elif isinstance(other, (int, float)):
                    lother.append(Fraction(other))
                    lden.append(1)
            elif isinstance(other, Fraction):
                lother.append(other)
                if other.code: traiter = True
                try: lden.append(eval(other.d))
                except TypeError: lden.append(other.d)
            else:
                raise ValueError(u'Format incorrect : %s' % (other))
        if var:
            self = Polynome([[self, 0]], var, details)
            for i in range(len(lother)):
                if not isinstance(lother[i], Polynome): lother[i] = Polynome([[lother[i], 0]], var, details)
            return Polynome.__sub__(self, *lother)

        if traiter:
            lfrac = [repr(self.traitement())]
            for other in lother:
                lfrac.append(repr(other.traitement()))
            return "-".join(lfrac)
        try: self.d = eval(self.d)
        except TypeError: pass
        if not lother: return self  # On a ajouté 0
        leppcm = ppcm(self.d, *lden)
        if self.d == leppcm:
            # Vérifions si toutes les fractions ont le même dénominateur
            d = Counter(lden)
            if d[leppcm] == len(lden):
                try: num = eval(self.n)
                except TypeError: num = self.n
                for other in lother:
                    try: num += -eval(other.n)
                    except TypeError: num += -other.n
                return Fraction(num, leppcm)
        if self.n: lfrac = [repr(self.choix_denominateur(leppcm))]
        else: lfrac = []
        for other in lother:
            if lfrac: lfrac.append(repr(other.choix_denominateur(leppcm)))
            else: lfrac.append(repr(-other.choix_denominateur(leppcm)))
            # On effectue Fraction(0,3)-Fraction(2,3), il faut donc prendre l'opposé de Fraction(2,3), puisque Fraction(0,3) a été supprimée
        if lfrac: return "-".join(lfrac)
        else: return "0"
예제 #4
0
    def __add__(self, *others):
        """*object*\ .\ **__add__**\ (*other*)

        ``p.__add__(q)`` est équivalent à ``p + q``  calcule la somme de deux fractions.

        *other* peut être une chaîne représentant une fraction, un entier ou un réel.

        >>> from pyromaths.classes.Fractions import Fraction
        >>> Fraction(2,5) + Fraction(2,10)
        'Fraction("2*2", "5*2", "r")+Fraction(2, 10)'
        >>> Fraction(2,20) + Fraction(2,10)
        'Fraction(2, 20)+Fraction("2*2", "10*2", "r")'
        >>> Fraction(5,10) + Fraction(2,10)
        Fraction(7, 10)
        >>> Fraction(5,7) + Fraction(2,10)
        'Fraction("5*10", "7*10", "r")+Fraction("2*7", "10*7", "r")'

        :param: other
        :type: Fraction ou string
        :rtype: Fraction ou string

        **TODO :** Attention, 1+3/4 donne 1*4/1*4 + 3/4 à la place de 4/4+3/4. À corriger
        """
        from pyromaths.classes.PolynomesCollege import Polynome
        lother, lden, traiter = [], [], False
        if self.code: traiter = True
        details, var = '', ''
        for other in others:
            if other == 0:
                pass
            elif isinstance(other, Polynome):
                var = other.var
                if details != 0  : details = min(details, other.details)
                else: details = other.details
                lother.append(other)
            elif isinstance(other, (int, float)):
                    lother.append(Fraction(other))
                    lden.append(1)
            elif isinstance(other, Fraction):
                lother.append(other)
                if other.code: traiter = True
                try: lden.append(eval(other.d))
                except TypeError: lden.append(other.d)
            else:
                raise ValueError(u'Format incorrect : %s' % (other))
        if var:
            self = Polynome([[self, 0]], var, details)
            for i in range(len(lother)):
                if not isinstance(lother[i], Polynome): lother[i] = Polynome([[lother[i], 0]], var, details)
            return Polynome.__add__(self, *lother)

        if traiter:
            lfrac = [repr(self.traitement())]
            for other in lother:
                lfrac.append(repr(other.traitement()))
            return "+".join(lfrac)
        try: self.d = eval(self.d)
        except TypeError: pass
        if not lother: return self  # On a ajouté 0
        leppcm = ppcm(self.d, *lden)
        if self.d == leppcm:
            # Vérifions si toutes les fractions ont le même dénominateur
            d = Counter(lden)
            if d[leppcm] == len(lden):
                try: num = eval(self.n)
                except TypeError: num = self.n
                for other in lother:
                    try: num += eval(other.n)
                    except TypeError: num += other.n
                return Fraction(num, leppcm)
        if self.n: lfrac = [repr(self.choix_denominateur(leppcm))]
        else: lfrac = []
        for other in lother: lfrac.append(repr(other.choix_denominateur(leppcm)))
        if lfrac: return "+".join(lfrac)
        else: return "0"
예제 #5
0
    def __sub__(self, *others):
        """*object*\ .\ **__sub__**\ (*other*)

        ``p.__sub__(q)`` est équivalent à ``p - q``  calcule la différence de deux fractions.

        *other* peut être une chaîne représentant une fraction, un entier ou un réel.

        Pour plus de détails, voir :py:func:`__add__`"""
        from pyromaths.classes.PolynomesCollege import Polynome
        lother, lden, traiter = [], [], False
        if self.code: traiter = True
        details, var = True, ''
        for other in others:
            if other == 0:
                pass
            elif isinstance(other, Polynome):
                var = other.var
                if details != 0: details = min(details, other.details)
                else: details = other.details
                lother.append(other)
            elif isinstance(other, (int, float)):
                lother.append(Fraction(other))
                lden.append(1)
            elif isinstance(other, Fraction):
                lother.append(other)
                if other.code: traiter = True
                try:
                    lden.append(eval(other.d))
                except TypeError:
                    lden.append(other.d)
            else:
                raise ValueError(u'Format incorrect : %s' % (other))
        if var:
            self = Polynome([[self, 0]], var, details)
            for i in range(len(lother)):
                if not isinstance(lother[i], Polynome):
                    lother[i] = Polynome([[lother[i], 0]], var, details)
            return Polynome.__sub__(self, *lother)

        if traiter:
            lfrac = [repr(self.traitement())]
            for other in lother:
                lfrac.append(repr(other.traitement()))
            return "-".join(lfrac)
        try:
            self.d = eval(self.d)
        except TypeError:
            pass
        if not lother: return self  # On a ajouté 0
        leppcm = ppcm(self.d, *lden)
        if self.d == leppcm:
            # Vérifions si toutes les fractions ont le même dénominateur
            d = Counter(lden)
            if d[leppcm] == len(lden):
                try:
                    num = eval(self.n)
                except TypeError:
                    num = self.n
                for other in lother:
                    try:
                        num += -eval(other.n)
                    except TypeError:
                        num += -other.n
                return Fraction(num, leppcm)
        if self.n: lfrac = [repr(self.choix_denominateur(leppcm))]
        else: lfrac = []
        for other in lother:
            if lfrac: lfrac.append(repr(other.choix_denominateur(leppcm)))
            else: lfrac.append(repr(-other.choix_denominateur(leppcm)))
            # On effectue Fraction(0,3)-Fraction(2,3), il faut donc prendre l'opposé de Fraction(2,3), puisque Fraction(0,3) a été supprimée
        if lfrac: return "-".join(lfrac)
        else: return "0"
예제 #6
0
    def __add__(self, *others):
        """*object*\ .\ **__add__**\ (*other*)

        ``p.__add__(q)`` est équivalent à ``p + q``  calcule la somme de deux fractions.

        *other* peut être une chaîne représentant une fraction, un entier ou un réel.

        >>> from pyromaths.classes.Fractions import Fraction
        >>> Fraction(2,5) + Fraction(2,10)
        Fraction("2*2", "5*2", "r")+Fraction(2, 10)
        >>> Fraction(2,20) + Fraction(2,10)
        Fraction(2, 20)+Fraction("2*2", "10*2", "r")
        >>> repr(Fraction(5,10) + Fraction(2,10))
        Fraction(7, 10)
        >>> Fraction(5,7) + Fraction(2,10)
        Fraction("5*10", "7*10", "r")+Fraction("2*7", "10*7", "r")

        :param: other
        :type: Fraction ou string
        :rtype: Fraction ou string

        **TODO :** Attention, 1+3/4 donne 1*4/1*4 + 3/4 à la place de 4/4+3/4. À corriger
        """
        from pyromaths.classes.PolynomesCollege import Polynome
        lother, lden, traiter = [], [], False
        if self.code: traiter = True
        details, var = '', ''
        for other in others:
            if other == 0:
                pass
            elif isinstance(other, Polynome):
                var = other.var
                if details != 0: details = min(details, other.details)
                else: details = other.details
                lother.append(other)
            elif isinstance(other, (int, float)):
                lother.append(Fraction(other))
                lden.append(1)
            elif isinstance(other, Fraction):
                lother.append(other)
                if other.code: traiter = True
                try:
                    lden.append(eval(other.d))
                except TypeError:
                    lden.append(other.d)
            else:
                raise ValueError(u'Format incorrect : %s' % (other))
        if var:
            self = Polynome([[self, 0]], var, details)
            for i in range(len(lother)):
                if not isinstance(lother[i], Polynome):
                    lother[i] = Polynome([[lother[i], 0]], var, details)
            return Polynome.__add__(self, *lother)

        if traiter:
            lfrac = [repr(self.traitement())]
            for other in lother:
                lfrac.append(repr(other.traitement()))
            return "+".join(lfrac)
        try:
            self.d = eval(self.d)
        except TypeError:
            pass
        if not lother: return self  # On a ajouté 0
        leppcm = ppcm(self.d, *lden)
        if self.d == leppcm:
            # Vérifions si toutes les fractions ont le même dénominateur
            d = Counter(lden)
            if d[leppcm] == len(lden):
                try:
                    num = eval(self.n)
                except TypeError:
                    num = self.n
                for other in lother:
                    try:
                        num += eval(other.n)
                    except TypeError:
                        num += other.n
                return Fraction(num, leppcm)
        if self.n: lfrac = [repr(self.choix_denominateur(leppcm))]
        else: lfrac = []
        for other in lother:
            lfrac.append(repr(other.choix_denominateur(leppcm)))
        if lfrac: return "+".join(lfrac)
        else: return "0"
예제 #7
0
def den_com0(a, b):  # renvoie un tuple contenant les 2 nombres par lesquels multiplier les deux denominateurs pour obtenir leur ppcm
    c = ppcm(a[1], b[1])
    return (abs(c // a[1]), abs(c // b[1]))