2018-03-09 16:31:46 +00:00
|
|
|
#! /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
|
2018-03-13 11:43:48 +00:00
|
|
|
from .mo import MO
|
2018-03-09 16:31:46 +00:00
|
|
|
|
|
|
|
__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>
|
|
|
|
"""
|
2018-11-13 08:14:50 +00:00
|
|
|
_numerator = MO.factory(numerator)
|
|
|
|
_denominator = MO.factory(denominator)
|
|
|
|
base_tree = Tree("/",
|
|
|
|
_numerator,
|
|
|
|
_denominator,
|
2018-03-13 11:43:48 +00:00
|
|
|
)
|
2018-03-10 05:44:01 +00:00
|
|
|
if negative:
|
2018-11-13 08:14:50 +00:00
|
|
|
tree = Tree("-", None, base_tree)
|
2018-03-10 05:44:01 +00:00
|
|
|
else:
|
2018-11-13 08:14:50 +00:00
|
|
|
tree = base_tree
|
|
|
|
MO.__init__(self, tree)
|
2018-03-09 16:31:46 +00:00
|
|
|
|
2018-11-13 08:14:50 +00:00
|
|
|
self._numerator = _numerator
|
|
|
|
self._denominator = _denominator
|
2018-03-09 16:31:46 +00:00
|
|
|
self.negative = negative
|
|
|
|
|
2018-03-12 04:34:26 +00:00
|
|
|
@property
|
|
|
|
def numerator(self):
|
|
|
|
""" Get the numerator. If self is negative, getting the opposite of the _numerator
|
|
|
|
"""
|
|
|
|
if self.negative:
|
|
|
|
return Tree("-", None, self._numerator)
|
|
|
|
|
|
|
|
return self._numerator
|
|
|
|
|
|
|
|
@property
|
|
|
|
def denominator(self):
|
|
|
|
return self._denominator
|
|
|
|
|
|
|
|
def inverse(self):
|
|
|
|
""" return the inverse fraction """
|
|
|
|
return MOFraction(self._denominator,
|
|
|
|
self._numerator,
|
|
|
|
self.negative)
|
2018-03-09 16:31:46 +00:00
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|