Mapytex/mapytex/calculus/core/MO/fraction.py

83 lines
2.1 KiB
Python
Raw Normal View History

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# Distributed under terms of the MIT license.
from mapytex.calculus.core.tree import Tree
from .mo import MO, MOError
__all__ = ["MOFraction"]
class MOFraction(MO):
""" Fraction math object"""
def __init__(self, numerator, denominator, negative=False):
""" Initiate the MOFraction
It can't be indempotent.
:param numerator: Numerator of the Fraction
:param denominator: Denominator of the Fraction
:param negative: Is the fraction negative (not concidering sign of
numerator or denominator.
>>> f = MOFraction(2, 3)
>>> f
<MOFraction 2 / 3>
>>> f = MOFraction(2, 3, negative = True)
>>> f
<MOFraction - 2 / 3>
"""
value = Tree("/",
MO.factory(numerator),
MO.factory(denominator),
)
MO.__init__(self, value)
self._numerator = numerator
self._denominator = denominator
self.negative = negative
@property
def __txt__(self):
# TODO: fonctionnement temporaire. Il faudrait utilser un moteur de rendu plus fin. |jeu. mars 8 15:26:49 EAT 2018
try:
numerator = self._numerator.__txt__
except AttributeError:
numerator = str(self._numerator)
try:
denominator = self._denominator.__txt__
except AttributeError:
denominator = str(self._denominator)
return "- "*self.negative + f"{numerator} / {denominator}"
def __add__(self, other):
""" Overload * for MOFraction
:param other: any other MO
:yields: calculus steps and the final yielded value is a MO.
>>> f = MOFraction(1, 2)
>>> g = MOFraction(3, 2)
"""
raise NotImplemented
def __mul__(self, other):
raise NotImplemented
def __truediv__(self, other):
raise NotImplemented
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del