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
* 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
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
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__()
self._num = num
if denom == 0:
raise ZeroDivisionError("Can't create Fraction: division by zero")
self._denom = denom
self.isNumber = 1

View File

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

View File

@ -116,7 +116,6 @@ class Operator(str):
:*args: Operands for this operation
:returns: list with the operator surrounded by operands
# TODO: order doctest |lun. nov. 24 07:17:29 CET 2014
>>> op.mul.__p2i__(1,2)
[1, '*', 2]
>>> f = save_mainOp([2, op.add, 3],op.add)
@ -131,9 +130,7 @@ class Operator(str):
>>> op.sub1.__p2i__(f)
['-', '(', 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:
# 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])
ans = flatten_list([self, op1])
@ -149,7 +146,8 @@ class Operator(str):
""" Add parenthesis for left operand if necessary """
ans = opl
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
elif opl.mainOp.priority < self.priority:
ans = flatten_list(["(", opl, ")"])
@ -163,8 +161,7 @@ class Operator(str):
return ans
def r_parenthesis(self, op, str_join=False):
""" Add parenthesis for left operand if necessary """
# TODO: /!\ Parenthèses pour -2abc et l'opérateur * |lun. mars 9 19:02:32 CET 2015
""" Add parenthesis for rigth operand if necessary """
try:
if op.mainOp.priority < self.priority:
op = flatten_list(["(", op, ")"])
@ -212,6 +209,12 @@ def operatorize(fun):
def mod_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"])
for (attr, value) in ans.items():
if hasattr(value, '__call__'):
@ -558,6 +561,21 @@ class op(object):
""" Calling this operator performs the rigth calculus """
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 = {
"operator" : "^", \
"name" : "pw",\
@ -566,6 +584,7 @@ class op(object):
"actions" : ("__pow__",""), \
"txt" : "{op1} ^ {op2}",\
"tex" : "{op1}^{{ {op2} }}",\
"l_parenthesis": l_parenthesis,\
"_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('t') :" + str(op.can_be_operator('t')))
from .render import tex
print(tex([-2, 3, op.add ]))
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("-----------------")
print("op.sub.__dict__ -> ", op.sub.__dict__)
print(op.sub == op.sub1)
#import doctest
#doctest.testmod()

View File

@ -74,11 +74,17 @@ p2i = Render(p2i_render)
if __name__ == '__main__':
from .operator import op
exp = [ 2, 3, op.add, 4, op.mul]
print(exp)
print("txt(exp) :" + str(txt(exp)))
print("tex(exp) :" + str(tex(exp)))
print("p2i(exp) :" + str(p2i(exp)))
from itertools import permutations
from pymath import Polynom
from pymath import Expression
from pymath import Fraction
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)