2018-12-07 07:10:15 +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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Tokens representing interger and decimal
|
|
|
|
|
|
|
|
"""
|
2018-12-21 16:03:12 +00:00
|
|
|
from decimal import Decimal as _Decimal
|
2018-12-07 07:10:15 +00:00
|
|
|
from .token import Token
|
2018-12-21 16:03:12 +00:00
|
|
|
from ...core.arithmetic import gcd
|
2021-09-30 12:52:08 +00:00
|
|
|
#from ...core.random.int_gene import filter_random
|
2018-12-21 10:36:40 +00:00
|
|
|
from ...core.MO import MO, MOnumber
|
2018-12-07 09:27:50 +00:00
|
|
|
from ...core.MO.fraction import MOFraction
|
2019-10-14 20:26:51 +00:00
|
|
|
from random import random
|
2018-12-07 07:10:15 +00:00
|
|
|
|
2020-12-15 14:02:39 +00:00
|
|
|
__all__ = ["Integer", "Decimal", "Fraction"]
|
2018-12-07 07:10:15 +00:00
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-07 07:10:15 +00:00
|
|
|
class Integer(Token):
|
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
""" Token representing a integer
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> Integer(4)
|
|
|
|
<Integer 4>
|
|
|
|
>>> a = MOnumber(4)
|
|
|
|
>>> Integer.from_mo(a)
|
|
|
|
<Integer 4>
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, a, name="", ancestor=None):
|
|
|
|
if not isinstance(a, MO):
|
|
|
|
if not isinstance(a, int):
|
|
|
|
raise TypeError
|
|
|
|
mo = MOnumber(a)
|
|
|
|
else:
|
|
|
|
mo = a
|
2018-12-07 07:10:15 +00:00
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
Token.__init__(self, mo, name, ancestor)
|
2019-05-14 04:55:56 +00:00
|
|
|
self._mathtype = "entier"
|
2018-12-13 09:44:52 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_mo(cls, mo, name="", ancestor=None):
|
2018-12-07 07:10:15 +00:00
|
|
|
if not isinstance(mo, MOnumber):
|
|
|
|
raise TypeError
|
2018-12-13 09:44:52 +00:00
|
|
|
if not isinstance(mo.value, int):
|
2018-12-07 07:10:15 +00:00
|
|
|
raise TypeError
|
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
return cls(mo, name, ancestor)
|
2018-12-07 07:10:15 +00:00
|
|
|
|
|
|
|
@classmethod
|
2019-05-14 04:55:56 +00:00
|
|
|
def random(
|
|
|
|
cls, name="", min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks=[]
|
|
|
|
):
|
2018-12-21 16:03:12 +00:00
|
|
|
""" Generate a random Integer
|
|
|
|
|
|
|
|
:param name: name of the Integer
|
|
|
|
:param min_value: minimum value
|
|
|
|
:param max_value: maximum value
|
|
|
|
:param rejected: rejected values
|
2018-12-22 07:42:31 +00:00
|
|
|
:param accept_callbacks: list of function for value acceptation
|
2018-12-21 16:03:12 +00:00
|
|
|
|
|
|
|
"""
|
2019-05-14 04:55:56 +00:00
|
|
|
candidate = filter_random(min_value, max_value, rejected, accept_callbacks)
|
2018-12-21 16:03:12 +00:00
|
|
|
|
|
|
|
return Integer(candidate, name)
|
2018-12-07 07:10:15 +00:00
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-07 07:10:15 +00:00
|
|
|
class Decimal(Token):
|
|
|
|
|
2018-12-21 16:03:12 +00:00
|
|
|
""" Token representing a decimal
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> Decimal("4.3")
|
|
|
|
<Decimal 4.3>
|
|
|
|
>>> Decimal(3.3)
|
|
|
|
<Decimal 3.29999999999999982236431605997495353221893310546875>
|
|
|
|
>>> Decimal(_Decimal("2.3"))
|
|
|
|
<Decimal 2.3>
|
|
|
|
"""
|
2018-12-07 07:10:15 +00:00
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
def __init__(self, a, name="", ancestor=None):
|
|
|
|
if not isinstance(a, MO):
|
2018-12-21 16:03:12 +00:00
|
|
|
if isinstance(a, _Decimal):
|
|
|
|
mo = MOnumber(a)
|
|
|
|
elif isinstance(a, (str, float)):
|
|
|
|
mo = MOnumber(_Decimal(a))
|
2018-12-13 09:44:52 +00:00
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
else:
|
|
|
|
mo = a
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
self._mathtype = "décimal"
|
2018-12-13 09:44:52 +00:00
|
|
|
Token.__init__(self, mo, name, ancestor)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_mo(cls, mo, name="", ancestor=None):
|
2018-12-07 07:10:15 +00:00
|
|
|
if not isinstance(mo, MOnumber):
|
|
|
|
raise TypeError
|
2019-05-14 04:55:56 +00:00
|
|
|
if not isinstance(mo.value, _Decimal):
|
2018-12-07 07:10:15 +00:00
|
|
|
raise TypeError
|
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
return cls(mo, name, ancestor)
|
2018-12-07 07:10:15 +00:00
|
|
|
|
|
|
|
@classmethod
|
2019-05-14 04:55:56 +00:00
|
|
|
def random(
|
|
|
|
cls,
|
|
|
|
name="",
|
|
|
|
min_value=-10,
|
|
|
|
max_value=10,
|
|
|
|
digits=2,
|
|
|
|
rejected=[0, 1],
|
|
|
|
reject_callbacks=[],
|
|
|
|
):
|
2018-12-21 16:03:12 +00:00
|
|
|
""" Generate a random Decimal
|
|
|
|
|
|
|
|
:param name: name of the Integer
|
|
|
|
:param min_value: minimum value
|
|
|
|
:param max_value: maximum value
|
|
|
|
:param digits: digits after comas
|
|
|
|
:param rejected: rejected values
|
|
|
|
:param reject_callbacks: list of function for value rejection
|
|
|
|
|
|
|
|
"""
|
|
|
|
conditions = [lambda x: x in rejected] + reject_callbacks
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
float_cand = (max_value - min_value) * random() + min_value
|
2018-12-21 16:03:12 +00:00
|
|
|
candidate = _Decimal(f"{float_cand:.{digits}f}")
|
|
|
|
while any(c(candidate) for c in conditions):
|
2019-05-14 04:55:56 +00:00
|
|
|
float_cand = (max_value - min_value) * random() + min_value
|
2018-12-21 16:03:12 +00:00
|
|
|
candidate = _Decimal(f"{float_cand:.{digits}f}")
|
|
|
|
|
|
|
|
return Decimal(candidate, name)
|
|
|
|
|
2018-12-07 07:10:15 +00:00
|
|
|
|
|
|
|
class Fraction(Token):
|
|
|
|
|
2021-09-25 16:05:05 +00:00
|
|
|
""" Token representing a fraction of numbers
|
2018-12-21 16:03:12 +00:00
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> Fraction("3/4")
|
|
|
|
<Fraction 3 / 4>
|
|
|
|
"""
|
2018-12-07 07:10:15 +00:00
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
def __init__(self, a, name="", ancestor=None):
|
|
|
|
if not isinstance(a, MO):
|
|
|
|
if isinstance(a, str):
|
2019-05-14 04:55:56 +00:00
|
|
|
num, denom = a.split("/")
|
2018-12-13 12:45:39 +00:00
|
|
|
mo = MOFraction(int(num), int(denom))
|
2018-12-13 09:44:52 +00:00
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
else:
|
|
|
|
mo = a
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
self._mathtype = "fraction"
|
2018-12-13 09:44:52 +00:00
|
|
|
Token.__init__(self, mo, name, ancestor)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_mo(cls, mo, name="", ancestor=None):
|
2018-12-07 07:10:15 +00:00
|
|
|
if not isinstance(mo, MOFraction):
|
|
|
|
raise TypeError
|
2019-05-14 04:55:56 +00:00
|
|
|
if not isinstance(mo._numerator, MOnumber):
|
2018-12-07 07:10:15 +00:00
|
|
|
raise TypeError
|
2019-05-14 04:55:56 +00:00
|
|
|
if not isinstance(mo._denominator, MOnumber):
|
2018-12-07 07:10:15 +00:00
|
|
|
raise TypeError
|
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
return cls(mo, name, ancestor)
|
2018-12-07 07:10:15 +00:00
|
|
|
|
|
|
|
@classmethod
|
2019-05-14 04:55:56 +00:00
|
|
|
def random(
|
|
|
|
cls,
|
|
|
|
name="",
|
|
|
|
fix_num="",
|
|
|
|
min_num=-10,
|
|
|
|
max_num=10,
|
|
|
|
rejected_num=[0],
|
|
|
|
accept_num_callbacks=[],
|
|
|
|
fix_denom="",
|
|
|
|
min_denom=-10,
|
|
|
|
max_denom=10,
|
|
|
|
rejected_denom=[0, 1, -1],
|
|
|
|
accept_denom_callbacks=[],
|
|
|
|
irreductible=False,
|
|
|
|
not_integer=True,
|
|
|
|
):
|
2018-12-21 16:03:12 +00:00
|
|
|
""" Generate a random Fraction
|
|
|
|
|
|
|
|
:param name: Name of the fraction
|
|
|
|
:param fix_num: if set, the numerator will get this value
|
|
|
|
:param min_num: minimum value for the numerator
|
|
|
|
:param max_num: maximum value for the numerator
|
|
|
|
:param rejected_num: rejected values for the numerator
|
2018-12-22 07:42:31 +00:00
|
|
|
:param accept_num_callbacks: list of function for numerator rejection
|
2018-12-21 16:03:12 +00:00
|
|
|
:param fix_denom: if set, the denomerator will get this value
|
|
|
|
:param min_denom: minimum value for the denominator
|
|
|
|
:param max_denom: maximum value for the denominator
|
|
|
|
:param rejected_denom: rejected values for the denominator
|
2018-12-22 07:42:31 +00:00
|
|
|
:param accept_denom_callbacks: list of function for denomerator rejection
|
2018-12-21 16:03:12 +00:00
|
|
|
:param irreductible: is the generated fraction necessary irreductible
|
|
|
|
:param not_integer: can the generated fraction be egal to an interger
|
|
|
|
"""
|
|
|
|
if fix_num == "":
|
2019-05-14 04:55:56 +00:00
|
|
|
num = filter_random(min_num, max_num, rejected_num, accept_num_callbacks)
|
2018-12-21 16:03:12 +00:00
|
|
|
else:
|
|
|
|
num = fix_num
|
|
|
|
|
|
|
|
if fix_denom == "":
|
2018-12-22 07:42:31 +00:00
|
|
|
accept_callbacks = accept_denom_callbacks
|
2018-12-21 16:03:12 +00:00
|
|
|
|
|
|
|
if irreductible:
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-22 07:42:31 +00:00
|
|
|
def prime_with_num(denom):
|
|
|
|
return gcd(num, denom) == 1
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-22 07:42:31 +00:00
|
|
|
accept_callbacks.append(prime_with_num)
|
2018-12-21 16:03:12 +00:00
|
|
|
if not_integer:
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-22 07:42:31 +00:00
|
|
|
def not_divise_num(denom):
|
|
|
|
return num % denom != 0
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-22 07:42:31 +00:00
|
|
|
accept_callbacks.append(not_divise_num)
|
2018-12-21 16:03:12 +00:00
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
denom = filter_random(
|
|
|
|
min_denom, max_denom, rejected_denom, accept_callbacks
|
|
|
|
)
|
2018-12-21 16:03:12 +00:00
|
|
|
else:
|
|
|
|
denom = fix_denom
|
|
|
|
|
|
|
|
frac = MOFraction(num, denom)
|
|
|
|
return cls(frac, name)
|
2018-12-07 07:10:15 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def numerator(self):
|
2020-12-15 14:02:39 +00:00
|
|
|
""" Get numerator of the fraction
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = Fraction("3/4")
|
|
|
|
>>> a.numerator
|
|
|
|
<Integer 3>
|
|
|
|
"""
|
|
|
|
return Integer(self._mo.numerator)
|
2018-12-07 07:10:15 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def denominator(self):
|
2020-12-15 14:02:39 +00:00
|
|
|
""" Get denominator of the fraction
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = Fraction("3/4")
|
|
|
|
>>> a.denominator
|
|
|
|
<Integer 4>
|
|
|
|
"""
|
|
|
|
return Integer(self._mo.denominator)
|
2018-12-07 07:10:15 +00:00
|
|
|
|
2019-10-14 20:26:51 +00:00
|
|
|
@property
|
|
|
|
def decimal(self):
|
|
|
|
""" return decimal approximation of the fraction
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> f = Fraction("3/4")
|
|
|
|
>>> f.decimal
|
|
|
|
<Decimal 0.75>
|
|
|
|
>>> f = Fraction("1/3")
|
|
|
|
>>> f.decimal
|
|
|
|
<Decimal 0.3333333333333333333333333333>
|
|
|
|
"""
|
2021-09-26 06:29:07 +00:00
|
|
|
return Decimal(self._mo._value)
|
|
|
|
|
2021-09-29 13:34:14 +00:00
|
|
|
@property
|
2021-09-26 06:58:42 +00:00
|
|
|
def simplified(self):
|
|
|
|
""" Get the irreductible version of self
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> f = Fraction("3/4")
|
2021-09-29 13:34:14 +00:00
|
|
|
>>> f.simplified
|
2021-09-26 06:58:42 +00:00
|
|
|
<Fraction 3 / 4>
|
|
|
|
>>> f = Fraction("12/9")
|
2021-09-29 13:34:14 +00:00
|
|
|
>>> f.simplified
|
2021-09-26 06:58:42 +00:00
|
|
|
<Fraction 4 / 3>
|
|
|
|
>>> f = Fraction("12/4")
|
2021-09-29 13:34:14 +00:00
|
|
|
>>> f.simplified
|
2021-09-26 06:58:42 +00:00
|
|
|
<Integer 3>
|
|
|
|
|
|
|
|
"""
|
|
|
|
simplified = self._mo.simplified()
|
|
|
|
|
|
|
|
if isinstance(simplified, MOnumber):
|
|
|
|
return Integer(simplified)
|
|
|
|
|
|
|
|
return Fraction(simplified)
|
|
|
|
|
2021-09-29 13:34:14 +00:00
|
|
|
def simplify(self):
|
|
|
|
""" Itself or its simplified version
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> f = Fraction("12/8")
|
|
|
|
>>> fs = f.simplify()
|
|
|
|
>>> for i in fs.explain():
|
|
|
|
... print(i)
|
|
|
|
12 / 8
|
|
|
|
3 / 2
|
|
|
|
>>> f = Fraction("5/8")
|
|
|
|
>>> fs = f.simplify()
|
|
|
|
>>> for i in fs.explain():
|
|
|
|
... print(i)
|
|
|
|
5 / 8
|
|
|
|
"""
|
|
|
|
simplified = self.simplified
|
|
|
|
try:
|
|
|
|
if self.numerator == simplified.numerator:
|
|
|
|
return self
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
simplified._ancestor = self
|
|
|
|
return simplified
|
|
|
|
|
2021-09-26 06:58:42 +00:00
|
|
|
|
2019-10-14 20:26:51 +00:00
|
|
|
|
2018-12-07 07:10:15 +00:00
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|