Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
lafrite 2015-05-14 10:25:48 +02:00
commit a79f5e98c0
6 changed files with 53 additions and 28 deletions

1
TODO
View File

@ -9,3 +9,4 @@
* Expression parents class and his children: Numerical_exp, toGenerate_exp and formal expression * Expression parents class and his children: Numerical_exp, toGenerate_exp and formal expression
* Create tbl sgn and variation render * Create tbl sgn and variation render
* Make less verbose fractions operations

View File

@ -83,6 +83,18 @@ mathématiques. Voici ce qu'il est capable de faire:
( 6 + 10 ) / 15 ( 6 + 10 ) / 15
16 / 15 16 / 15
Le rendu latex permet ensuite d'être directement compiler et par exemple d'avoir le rendu suivant
.. math::
:nowrap:
\frac{ 2 }{ 5 } + \frac{ 2 }{ 3 } \\
\frac{ 2 \times 3 }{ 5 \times 3 } + \frac{ 2 \times 5 }{ 3 \times 5 } \\
\frac{ 6 }{ 15 } + \frac{ 10 }{ 15 } \\
\frac{ 6 + 10 }{ 15 } \\
\frac{ 16 }{ 15 }
Ce module a pour but d'être un outil pour faciliter la construction Ce module a pour but d'être un outil pour faciliter la construction
d'exercices et leurs correction. Il a pour but d'être le plus simple d'exercices et leurs correction. Il a pour but d'être le plus simple

View File

@ -24,6 +24,8 @@ class Fraction(Explicable):
""" """
super(Fraction, self).__init__() super(Fraction, self).__init__()
self._num = num self._num = num
if denom == 0:
raise ZeroDivisionError("Can't create Fraction: division by zero")
self._denom = denom self._denom = denom
self.isNumber = 1 self.isNumber = 1

View File

@ -8,7 +8,6 @@ class Stack(object):
"""Docstring for Stack """ """Docstring for Stack """
def __init__(self): def __init__(self):
"""@todo: to be defined1 """
self.items = [] self.items = []
def pushFromList(self, list): def pushFromList(self, list):
@ -21,7 +20,6 @@ class Stack(object):
def isEmpty(self): def isEmpty(self):
""" Says if the stack is empty """ Says if the stack is empty
:returns: @todo
""" """
return self.items == [] return self.items == []
@ -29,9 +27,6 @@ class Stack(object):
def push(self, item): def push(self, item):
"""Push an item in the stack """Push an item in the stack
:param item: @todo
:returns: @todo
""" """
self.items.append(item) self.items.append(item)

View File

@ -116,7 +116,6 @@ class Operator(str):
:*args: Operands for this operation :*args: Operands for this operation
:returns: list with the operator surrounded by operands :returns: list with the operator surrounded by operands
# TODO: order doctest |lun. nov. 24 07:17:29 CET 2014
>>> op.mul.__p2i__(1,2) >>> op.mul.__p2i__(1,2)
[1, '*', 2] [1, '*', 2]
>>> f = save_mainOp([2, op.add, 3],op.add) >>> f = save_mainOp([2, op.add, 3],op.add)
@ -131,9 +130,7 @@ class Operator(str):
>>> op.sub1.__p2i__(f) >>> op.sub1.__p2i__(f)
['-', '(', 2, '+', 3, ')'] ['-', '(', 2, '+', 3, ')']
""" """
# TODO: Attention à gestion des fractions qui se comportent chelou avec les parenthèses |dim. nov. 9 09:21:52 CET 2014
if self.arity == 1: if self.arity == 1:
# TODO: Marche juste avec -, il faudra voir quand il y aura d'autres operateurs unitaires |dim. nov. 9 09:24:53 CET 2014
op1 = self.l_parenthesis(args[0]) op1 = self.l_parenthesis(args[0])
ans = flatten_list([self, op1]) ans = flatten_list([self, op1])
@ -149,7 +146,8 @@ class Operator(str):
""" Add parenthesis for left operand if necessary """ """ Add parenthesis for left operand if necessary """
ans = opl ans = opl
try: try:
if opl.mainOp == op.sub1: # TODO: Je pige pas pourquoi quand on enlève .name ça marche plus... |lun. avril 27 19:07:24 CEST 2015
if opl.mainOp.name == op.sub1.name:
ans = opl ans = opl
elif opl.mainOp.priority < self.priority: elif opl.mainOp.priority < self.priority:
ans = flatten_list(["(", opl, ")"]) ans = flatten_list(["(", opl, ")"])
@ -163,8 +161,7 @@ class Operator(str):
return ans return ans
def r_parenthesis(self, op, str_join=False): def r_parenthesis(self, op, str_join=False):
""" Add parenthesis for left operand if necessary """ """ Add parenthesis for rigth operand if necessary """
# TODO: /!\ Parenthèses pour -2abc et l'opérateur * |lun. mars 9 19:02:32 CET 2015
try: try:
if op.mainOp.priority < self.priority: if op.mainOp.priority < self.priority:
op = flatten_list(["(", op, ")"]) op = flatten_list(["(", op, ")"])
@ -212,6 +209,12 @@ def operatorize(fun):
def mod_fun(self, *args): def mod_fun(self, *args):
ans = fun(self, *args) ans = fun(self, *args)
def _eq(op1, op2):
""" op1 == op2 """
return op1.name == op2.name == name and op1.arity == op2.arity
ans["__eq__"] = _eq
new_op = Operator(ans["operator"]) new_op = Operator(ans["operator"])
for (attr, value) in ans.items(): for (attr, value) in ans.items():
if hasattr(value, '__call__'): if hasattr(value, '__call__'):
@ -558,6 +561,21 @@ class op(object):
""" Calling this operator performs the rigth calculus """ """ Calling this operator performs the rigth calculus """
return getattr(op1, "__pow__")(op2) return getattr(op1, "__pow__")(op2)
def l_parenthesis(self, opl, str_join=False):
""" Add parenthesis for left operand if necessary """
ans = opl
try:
if opl.mainOp.priority < self.priority:
ans = flatten_list(["(", opl, ")"])
except AttributeError as e:
# op has not the attribute priority
pass
ans = flatten_list([ans])
if str_join:
ans = ' '.join([str(i) for i in ans])
return ans
caract = { caract = {
"operator" : "^", \ "operator" : "^", \
"name" : "pw",\ "name" : "pw",\
@ -566,6 +584,7 @@ class op(object):
"actions" : ("__pow__",""), \ "actions" : ("__pow__",""), \
"txt" : "{op1} ^ {op2}",\ "txt" : "{op1} ^ {op2}",\
"tex" : "{op1}^{{ {op2} }}",\ "tex" : "{op1}^{{ {op2} }}",\
"l_parenthesis": l_parenthesis,\
"_call":_call,\ "_call":_call,\
} }
@ -603,18 +622,8 @@ if __name__ == '__main__':
#print("\t op.can_be_operator('+') :" + str(op.can_be_operator('+'))) #print("\t op.can_be_operator('+') :" + str(op.can_be_operator('+')))
#print("\t op.can_be_operator('t') :" + str(op.can_be_operator('t'))) #print("\t op.can_be_operator('t') :" + str(op.can_be_operator('t')))
from .render import tex print("op.sub.__dict__ -> ", op.sub.__dict__)
print(tex([-2, 3, op.add ])) print(op.sub == op.sub1)
print("-----------------")
print(tex([-2, 3, op.mul ]))
print("-----------------")
from .polynom import Polynom
print(tex([Polynom([1,2,3]), 2, op.mul]))
print("-----------------")
from .fraction import Fraction
print(tex([2, Fraction(1,2), op.mul]))
print("-----------------")
#import doctest #import doctest
#doctest.testmod() #doctest.testmod()

View File

@ -74,11 +74,17 @@ p2i = Render(p2i_render)
if __name__ == '__main__': if __name__ == '__main__':
from .operator import op from .operator import op
exp = [ 2, 3, op.add, 4, op.mul] from itertools import permutations
print(exp) from pymath import Polynom
print("txt(exp) :" + str(txt(exp))) from pymath import Expression
print("tex(exp) :" + str(tex(exp))) from pymath import Fraction
print("p2i(exp) :" + str(p2i(exp))) coefs_p = [[(i-2),(j-2)] for i,j in permutations(range(5),2)]
coefs_q = [[2*(i-2),2*(j-2)] for i,j in permutations(range(5),2)]
l_p = [Polynom(i) for i in coefs_p]
l_q = [Fraction(i,j) for i,j in coefs_q if j!=0]
operations = [Expression([l_p[i],l_q[j],op.mul]) for i,j in permutations(range(len(l_q)),2)]
for i in operations:
print(i)