formal (which should be called polynom...) are ok.No mult yet

This commit is contained in:
lafrite 2014-01-17 20:17:49 +01:00 committed by Lafrite
parent b0a60f706b
commit 2ffce67867
3 changed files with 148 additions and 13 deletions

View File

@ -1,10 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
from generic import Stack, flatten_list, expand_list from .generic import Stack, flatten_list, expand_list
from fraction import Fraction from .fraction import Fraction
from renders import txt_render, post2in_fix, tex_render from .render import txt_render, post2in_fix, tex_render
from formal import FormalExp from .formal import FormalExp
class Expression(object): class Expression(object):
"""A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown""" """A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown"""
@ -324,11 +324,7 @@ class Expression(object):
""" """
return type(exp) == int or \ return type(exp) == int or \
type(exp) == Fraction or \ type(exp) == Fraction or \
<<<<<<< HEAD
type(exp) == FormalExp type(exp) == FormalExp
=======
exp.isalpha()
>>>>>>> Render ~work with letters still some bugs
@staticmethod @staticmethod
def isOperator(exp): def isOperator(exp):
@ -354,6 +350,9 @@ if __name__ == '__main__':
#exp = "2 ^ 3 * 5" #exp = "2 ^ 3 * 5"
#test(exp) #test(exp)
#exp = "1 + 3 * 5"
#test(exp)
#exp = "2 * 3 * 3 * 5" #exp = "2 * 3 * 3 * 5"
#test(exp) #test(exp)
@ -404,15 +403,22 @@ if __name__ == '__main__':
#exp="-2*b+a(12 + 1)(3-12)" #exp="-2*b+a(12 + 1)(3-12)"
#test(exp) #test(exp)
#exp="(-2+5)/(3*4)+1/12+5*5"
#test(exp)
# TODO: The next one doesn't work |ven. janv. 17 14:56:58 CET 2014 # TODO: The next one doesn't work |ven. janv. 17 14:56:58 CET 2014
#exp="-2*(-a)(12 + 1)(3-12)" #exp="-2*(-a)(12 + 1)(3-12)"
#e = Expression(exp) #e = Expression(exp)
#print(e) #print(e)
exp="-2*a(12 + 1)(3-12)" exp="-2+a+(12 + 1)(3-12) + 34a"
test(exp)
e = Expression(exp) e = Expression(exp)
print(e) print(e)
#exp="-2*b+a(12 + 1)(3-12)"
#test(exp)
# TODO: The next one doesn't work |ven. janv. 17 14:56:58 CET 2014 # TODO: The next one doesn't work |ven. janv. 17 14:56:58 CET 2014
#exp="-2*(-a)(12 + 1)(3-12)" #exp="-2*(-a)(12 + 1)(3-12)"
#e = Expression(exp) #e = Expression(exp)

128
pymath/formal.py Normal file
View File

@ -0,0 +1,128 @@
#!/usr/bin/env python
# encoding: utf-8
from .fraction import Fraction
from .generic import add_in_dict, remove_in_dict
import re
class FormalExp(object):
"""A formal expression (similare to Symbol in Sympy"""
def __init__(self, coef = {}, letter = ""):
"""Initiat the formal expression
:param coef: the dictionary representing the expression
:param letter: minimum expression, a letter
"""
if coef != {} and letter != "":
raise ValueError("A FormalExp can't be initiate with dict_exp and a letter")
elif letter != "":
self._letter = letter
self._coef = {letter: 1}
elif coef != {}:
self._coef = coef
else:
raise ValueError("FormalExp needs a letter or dictionary of coeficients")
if len(self) != 1:
self.mainOp = "+"
def master_coef(self):
"""Return the master coefficient
/!\ may not work pretty well if there is more than one indeterminate
:returns: a_n
"""
pattern = "\w\*\*(\d*)"
finder = re.compile(pattern)
power = {}
for (k,v) in self._coef.items():
if k=="":
power[0] = v
else:
p = finder.findall(k)
if p == []:
power[1] = v
else:
power[int(p[0])] = v
m_power = max(power)
return power[m_power]
def __add__(self, other):
if type(other) in [int, Fraction]:
d = {"":other}
elif type(other) == FormalExp:
d = other._coef
else:
raise ValueError("Can't add {type} with FormalExp".format(type=type(other)))
d = add_in_dict(self._coef, d)
d = remove_in_dict(d)
if list(d.keys()) == ['']:
return [d['']]
else:
return [FormalExp(d)]
def __radd__(self, other):
return self + other
def __sub__(self, other):
o_tmp = -other
return self + o_tmp
def __neg__(self):
d = {}
for k,v in self._coef.items():
d[k] = -v
return FormalExp(d)
def __mul__(self, other):
pass
def __rmul__(self, other):
pass
def __div__(self, other):
pass
def __pow__(self, other):
pass
def __len__(self):
return len(list(self._coef.keys()))
def __str__(self):
return " + ".join([str(v) + str(k) for k,v in self._coef.items()])
if __name__ == '__main__':
#fe1 = FormalExp({"x": 1, "":2})
#print(fe1)
#fe2 = FormalExp({"x**12": 5, "":2})
#print(fe2)
#fe3 = fe1 + fe2
#for s in fe3:
# print(s)
#fe4 = fe1 + 2
#for s in fe4:
# print(s)
#print(fe1.master_coef())
#print(fe2.master_coef())
#print(fe3[0].master_coef())
#print(fe4[0].master_coef())
fe = FormalExp(letter = "a")
fe_ = -2 + fe
print(fe_[0])
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@ -1,10 +1,11 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
from generic import Stack,flatten_list from .generic import Stack,flatten_list
from fraction import Fraction from .fraction import Fraction
from formal import FormalExp from .formal import FormalExp
class Render(object): class Render(object):
"""A class which aims to create render functions from three dictionnaries: """A class which aims to create render functions from three dictionnaries:
- op_infix: dict of caracters - op_infix: dict of caracters
@ -15,7 +16,7 @@ class Render(object):
PRIORITY = {"^": 4,"*" : 3, "/": 3, ":": 3, "+": 2, "-":2, "(": 1} PRIORITY = {"^": 4,"*" : 3, "/": 3, ":": 3, "+": 2, "-":2, "(": 1}
def __init__(self, op_infix = {}, op_postfix = {}, other = {}, join = " ", type_render = {int: str, Fraction: str, str: str}): def __init__(self, op_infix = {}, op_postfix = {}, other = {}, join = " ", type_render = {int: str, Fraction: str, FormalExp: str}):
"""Initiate the render """Initiate the render
@param op_infix: the dictionnary of infix operator with how they have to be render @param op_infix: the dictionnary of infix operator with how they have to be render