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-12-21 10:26:37 +00:00
|
|
|
from .mo import Molecule, MO
|
2019-07-15 15:48:59 +00:00
|
|
|
from .atoms import MOnumber
|
2021-09-26 06:29:07 +00:00
|
|
|
from decimal import Decimal
|
2021-09-26 06:58:42 +00:00
|
|
|
from ..arithmetic import gcd
|
2018-03-09 16:31:46 +00:00
|
|
|
|
|
|
|
__all__ = ["MOFraction"]
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
class MOFraction(Molecule):
|
2018-03-09 16:31:46 +00:00
|
|
|
|
|
|
|
""" Fraction math object"""
|
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
MAINOP = "/"
|
|
|
|
|
2018-03-09 16:31:46 +00:00
|
|
|
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>
|
2020-12-12 22:14:44 +00:00
|
|
|
>>> print(f.tree)
|
|
|
|
/
|
|
|
|
> 2
|
|
|
|
> 3
|
2018-12-21 10:26:37 +00:00
|
|
|
>>> print(f)
|
2018-12-21 11:20:13 +00:00
|
|
|
2 / 3
|
2018-03-09 16:31:46 +00:00
|
|
|
>>> 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)
|
2020-12-12 22:14:44 +00:00
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
base_tree = Tree("/", _numerator, _denominator)
|
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
|
2020-12-12 22:14:44 +00:00
|
|
|
|
2018-12-21 10:26:37 +00:00
|
|
|
Molecule.__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
|
|
|
|
|
2021-09-26 06:29:07 +00:00
|
|
|
@property
|
|
|
|
def _value(self):
|
|
|
|
return Decimal(self._numerator._value) / Decimal(self._denominator._value)
|
|
|
|
|
2018-03-12 04:34:26 +00:00
|
|
|
def inverse(self):
|
|
|
|
""" return the inverse fraction """
|
2019-05-14 04:55:56 +00:00
|
|
|
return MOFraction(self._denominator, self._numerator, self.negative)
|
|
|
|
|
2019-07-15 15:48:59 +00:00
|
|
|
def differentiate(self):
|
|
|
|
""" differentiate a fraction and get something!
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = MOFraction(2, 3)
|
|
|
|
>>> a.differentiate()
|
|
|
|
<MOnumber 0>
|
|
|
|
"""
|
|
|
|
d_num = self.numerator.differentiate()
|
|
|
|
d_denom = self.denominator.differentiate()
|
|
|
|
|
|
|
|
if d_num == 0 and d_denom == 0:
|
|
|
|
return MOnumber(0)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2021-09-26 06:58:42 +00:00
|
|
|
def simplified(self):
|
|
|
|
""" Simplified version of self
|
|
|
|
|
|
|
|
:examplex
|
|
|
|
>>> f = MOFraction(2, 3)
|
|
|
|
>>> f
|
|
|
|
<MOFraction 2 / 3>
|
|
|
|
>>> f.simplified()
|
|
|
|
<MOFraction 2 / 3>
|
|
|
|
>>> f = MOFraction(2, 6)
|
|
|
|
>>> f
|
|
|
|
<MOFraction 2 / 6>
|
|
|
|
>>> f.simplified()
|
|
|
|
<MOFraction 1 / 3>
|
|
|
|
>>> f = MOFraction(32, 24)
|
|
|
|
>>> f.simplified()
|
|
|
|
<MOFraction 4 / 3>
|
|
|
|
>>> f = MOFraction(32, 8)
|
|
|
|
>>> f.simplified()
|
|
|
|
<MOnumber 4>
|
|
|
|
"""
|
|
|
|
frac_gcd = gcd(self.numerator._value, self.denominator._value)
|
|
|
|
new_num = self.numerator._value / frac_gcd
|
|
|
|
new_denom = self.denominator._value / frac_gcd
|
|
|
|
|
|
|
|
if new_denom == 1:
|
|
|
|
return MOnumber(new_num)
|
|
|
|
|
|
|
|
return MOFraction(new_num, new_denom)
|
|
|
|
|
|
|
|
|
2018-03-09 16:31:46 +00:00
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|