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.
|
|
|
|
|
|
|
|
"""
|
2018-12-07 09:27:50 +00:00
|
|
|
Tokens representing polynomials functions
|
2018-12-07 07:10:15 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
from .token import Token
|
2018-12-13 09:44:52 +00:00
|
|
|
from ...core.MO import MO
|
2018-12-07 07:10:15 +00:00
|
|
|
|
|
|
|
__all__ = ["Polynomial", "Quadratic", "Linear"]
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-07 07:10:15 +00:00
|
|
|
class Polynomial(Token):
|
|
|
|
|
2019-07-15 07:57:19 +00:00
|
|
|
""" Token representing a polynomial
|
|
|
|
|
|
|
|
:examples:
|
|
|
|
>>> from ...core.MO.polynomial import MOpolynomial
|
|
|
|
>>> P = Polynomial(MOpolynomial('x', [1, 2, 3]))
|
|
|
|
>>> P
|
|
|
|
"""
|
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):
|
|
|
|
raise TypeError
|
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
else:
|
|
|
|
mo = a
|
2018-12-07 07:10:15 +00:00
|
|
|
|
2018-12-07 09:27:50 +00:00
|
|
|
Token.__init__(self, mo, name, ancestor)
|
2019-05-14 04:55:56 +00:00
|
|
|
self._mathtype = "polynome"
|
2018-12-07 07:10:15 +00:00
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
@classmethod
|
|
|
|
def from_mo(cls, mo, name="", ancestor=None):
|
|
|
|
|
|
|
|
return cls(mo, name, ancestor)
|
|
|
|
|
2018-12-07 07:10:15 +00:00
|
|
|
@classmethod
|
|
|
|
def random(cls):
|
|
|
|
raise NotImplemented
|
|
|
|
|
|
|
|
def __setitem__(self, key, item):
|
|
|
|
""" Use Polynomial like if they were a dictionnary to set coefficients """
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
""" Use Polynomial like if they were a dictionnary to get coefficients """
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __call__(self, value):
|
2019-07-15 07:57:19 +00:00
|
|
|
""" Call a Polynomial to evaluate itself on value
|
|
|
|
|
|
|
|
:examples:
|
|
|
|
>>> from ...core.MO.polynomial import MOpolynomial
|
|
|
|
>>> P = Polynomial(MOpolynomial('x', [1, 2, 3]))
|
|
|
|
>>> P(2)
|
|
|
|
"""
|
2018-12-07 07:10:15 +00:00
|
|
|
pass
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
class Linear(Polynomial):
|
2018-12-07 09:27:50 +00:00
|
|
|
|
|
|
|
""" Token representing a linear """
|
|
|
|
|
|
|
|
def __init__(self, mo, name="", ancestor=None):
|
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
Polynomial.__init__(self, mo, name, ancestor)
|
2019-05-14 04:55:56 +00:00
|
|
|
self._mathtype = "affine"
|
2018-12-07 09:27:50 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def random(cls):
|
|
|
|
raise NotImplemented
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
class Quadratic(Polynomial):
|
2018-12-07 09:27:50 +00:00
|
|
|
|
|
|
|
""" Token representing a quadratic """
|
|
|
|
|
|
|
|
def __init__(self, mo, name="", ancestor=None):
|
|
|
|
|
2018-12-13 09:44:52 +00:00
|
|
|
Polynomial.__init__(self, mo, name, ancestor)
|
2019-05-14 04:55:56 +00:00
|
|
|
self._mathtype = "polynome du 2nd degré"
|
2018-12-07 09:27:50 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def random(cls):
|
|
|
|
raise NotImplemented
|
|
|
|
|
2018-12-07 07:10:15 +00:00
|
|
|
|
2019-07-15 07:57:19 +00:00
|
|
|
def extract_variable(leaf):
|
|
|
|
try:
|
|
|
|
return leaf.variable
|
|
|
|
except AttributeError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def replace(leaf, origin, dest):
|
|
|
|
# map_on_leaf sur leaf?
|
|
|
|
if leaf == origin:
|
|
|
|
return dest
|
|
|
|
return leaf
|
2018-12-07 07:10:15 +00:00
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|