Merge branch 'dev' into 2nd_deg
This commit is contained in:
@@ -1,63 +1,77 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
""" Testing Expression """
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from pymath.expression import Expression
|
||||
from pymath.fraction import Fraction
|
||||
from pymath.generic import first_elem
|
||||
from pymath.render import txt, tex
|
||||
|
||||
|
||||
class TestExpression(unittest.TestCase):
|
||||
"""Testing functions from pymath.expression"""
|
||||
|
||||
def setup(self):
|
||||
Expression.set_render(txt)
|
||||
|
||||
def test_init_from_str(self):
|
||||
def test_init_from_str():
|
||||
exp = Expression("2 + 3")
|
||||
self.assertEqual(exp.postfix_tokens, [2, 3, "+"])
|
||||
assert exp.postfix_tokens == [2, 3, "+"]
|
||||
|
||||
def test_init_from_exp(self):
|
||||
pass
|
||||
def test_init_from_exp():
|
||||
pass
|
||||
|
||||
def test_list(self):
|
||||
exp = Expression([2, 3, "+"])
|
||||
self.assertEqual(exp.postfix_tokens, [2, 3, "+"])
|
||||
def test_init_list():
|
||||
exp = Expression([2, 3, "+"])
|
||||
assert exp.postfix_tokens == [2, 3, "+"]
|
||||
|
||||
def test_simplify_frac(self):
|
||||
exp = Expression("1/2 - 4")
|
||||
steps = ['\\frac{ 1 }{ 2 } - 4', \
|
||||
'\\frac{ 1 \\times 1 }{ 2 \\times 1 } - \\frac{ 4 \\times 2 }{ 1 \\times 2 }',\
|
||||
'\\frac{ 1 }{ 2 } - \\frac{ 8 }{ 2 }',\
|
||||
'\\frac{ 1 - 8 }{ 2 }',\
|
||||
'\\frac{ -7 }{ 2 }']
|
||||
self.assertEqual(steps, list(exp.simplify()))
|
||||
Expression.set_render(txt)
|
||||
def test_init_one_element_int_from_str():
|
||||
exp = Expression("1")
|
||||
|
||||
def test_add_exp(self):
|
||||
e = Expression("12- 4")
|
||||
f = Expression("4 + 1")
|
||||
g = e + f
|
||||
self.assertEqual(g.postfix_tokens, [12, 4, '-', 4, 1, "+", "+"])
|
||||
def test_init_one_element_int_from_list():
|
||||
exp = Expression([1])
|
||||
|
||||
def test_mul_exp(self):
|
||||
e = Expression("12- 4")
|
||||
f = Expression("4 + 1")
|
||||
g = e * f
|
||||
self.assertEqual(g.postfix_tokens, [12, 4, '-', 4, 1, "+", "*"])
|
||||
#def test_init_one_element_str_from_str():
|
||||
# exp = Expression("x")
|
||||
#
|
||||
#def test_init_one_element_str_from_list():
|
||||
# exp = Expression(["x"])
|
||||
|
||||
def test_neg_exp(self):
|
||||
e = Expression("12- 4")
|
||||
g = -e
|
||||
self.assertEqual(g.postfix_tokens, [12, 4, '-', '-'])
|
||||
def test_simplify_exp():
|
||||
exp = Expression("1 + 2 * 3")
|
||||
simplified = exp.simplify()
|
||||
ans = Expression("7")
|
||||
assert ans == simplified
|
||||
|
||||
#def test_simplify_frac():
|
||||
# exp = Expression("1/2 - 4")
|
||||
# simplified = exp.simplify()
|
||||
# ans = Expression("-7/2")
|
||||
# assert simplified == ans
|
||||
#
|
||||
#def test_explain_frac():
|
||||
# exp = Expression("1/2 - 4")
|
||||
# simplified = exp.simplify()
|
||||
#
|
||||
# steps = ['\\frac{ 1 }{ 2 } - 4', \
|
||||
# '\\frac{ 1 \\times 1 }{ 2 \\times 1 } - \\frac{ 4 \\times 2 }{ 1 \\times 2 }',\
|
||||
# '\\frac{ 1 }{ 2 } - \\frac{ 8 }{ 2 }',\
|
||||
# '\\frac{ 1 - 8 }{ 2 }',\
|
||||
# '\\frac{ -7 }{ 2 }']
|
||||
# assert simplified.steps == list(exp.simplify())
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
def test_add_exp():
|
||||
e = Expression("12- 4")
|
||||
f = Expression("4 + 1")
|
||||
g = e + f
|
||||
assert g.postfix_tokens == [12, 4, '-', 4, 1, "+", "+"]
|
||||
|
||||
def test_mul_exp():
|
||||
e = Expression("12- 4")
|
||||
f = Expression("4 + 1")
|
||||
g = e * f
|
||||
assert g.postfix_tokens == [12, 4, '-', 4, 1, "+", "*"]
|
||||
|
||||
def test_neg_exp():
|
||||
e = Expression("12- 4")
|
||||
g = -e
|
||||
assert g.postfix_tokens == [12, 4, '-', '-']
|
||||
|
||||
|
||||
# -----------------------------
|
||||
|
@@ -22,43 +22,21 @@ class TestFraction(unittest.TestCase):
|
||||
ans = [[Fraction(2, 3), 1, Fraction(17, 15), 0, 0, Fraction(4,3)], \
|
||||
[Fraction(4,3), Fraction(5,3), Fraction(9,5), Fraction(2,3), Fraction(2,3), 2] \
|
||||
]
|
||||
# TODO: Bug pour 1 + 1/-3 |sam. févr. 22 07:01:29 CET 2014
|
||||
|
||||
for (i, f1) in enumerate(self.listFrom):
|
||||
for (j, f2) in enumerate(self.listAgainst):
|
||||
res = f1 + f2
|
||||
|
||||
#print("-----------")
|
||||
#print("f1 : ", f1)
|
||||
#print("f2 : ", f2)
|
||||
#print(res)
|
||||
|
||||
# On est obligé de faire ça pour gérer le cas de 1+1 qui ne passe pas par la classe Fraction
|
||||
if type(res) == list:
|
||||
self.assertEqual(res[-1], ans[i][j])
|
||||
else:
|
||||
self.assertEqual(res, ans[i][j])
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_sub(self):
|
||||
ans = [[0, Fraction(-1,3), Fraction(-7, 15), Fraction(2,3), Fraction(2,3), Fraction(-2,3)], \
|
||||
[Fraction(2,3), Fraction(1,3), Fraction(1,5), Fraction(4,3), Fraction(4,3), 0] \
|
||||
]
|
||||
# TODO: bug pour 1 - 1/-3 |sam. févr. 22 07:05:15 CET 2014
|
||||
|
||||
for (i, f1) in enumerate(self.listFrom):
|
||||
for (j, f2) in enumerate(self.listAgainst):
|
||||
res = f1 - f2
|
||||
|
||||
#print("-----------")
|
||||
#print("f1 : ", f1)
|
||||
#print("f2 : ", f2)
|
||||
#print(res)
|
||||
|
||||
# On est obligé de faire ça pour gérer le cas de 1-1 qui ne passe pas par la classe Fraction
|
||||
if type(res) == list:
|
||||
self.assertEqual(res[-1], ans[i][j])
|
||||
else:
|
||||
self.assertEqual(res, ans[i][j])
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_neg(self):
|
||||
ans = [ Fraction(-1,3), \
|
||||
@@ -70,10 +48,7 @@ class TestFraction(unittest.TestCase):
|
||||
]
|
||||
for (j, f) in enumerate(self.listAgainst):
|
||||
res = -f
|
||||
if type(res) == list:
|
||||
self.assertEqual(res[-1], ans[j])
|
||||
else:
|
||||
self.assertEqual(res, ans[j])
|
||||
self.assertEqual(res, ans[j])
|
||||
|
||||
def test_mul(self):
|
||||
ans = [[Fraction(1, 9), Fraction(2,9), Fraction(4, 15), Fraction(-1,9), Fraction(-1,9), Fraction(1,3)], \
|
||||
@@ -83,17 +58,7 @@ class TestFraction(unittest.TestCase):
|
||||
for (i, f1) in enumerate(self.listFrom):
|
||||
for (j, f2) in enumerate(self.listAgainst):
|
||||
res = f1 * f2
|
||||
|
||||
#print("-----------")
|
||||
#print("f1 : ", f1)
|
||||
#print("f2 : ", f2)
|
||||
#print(res)
|
||||
|
||||
# On est obligé de faire ça pour gérer le cas de 1*1 qui ne passe pas par la classe Fraction
|
||||
if type(res) == list:
|
||||
self.assertEqual(res[-1], ans[i][j])
|
||||
else:
|
||||
self.assertEqual(res, ans[i][j])
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_truediv(self):
|
||||
ans = [[1, Fraction(1,2), Fraction(5, 12), -1, -1, Fraction(1,3)], \
|
||||
@@ -103,17 +68,7 @@ class TestFraction(unittest.TestCase):
|
||||
for (i, f1) in enumerate(self.listFrom):
|
||||
for (j, f2) in enumerate(self.listAgainst):
|
||||
res = f1 / f2
|
||||
|
||||
#print("-----------")
|
||||
#print("f1 : ", f1)
|
||||
#print("f2 : ", f2)
|
||||
#print(res)
|
||||
|
||||
# On est obligé de faire ça pour gérer le cas de 1/1 qui ne passe pas par la classe Fraction
|
||||
if type(res) == list:
|
||||
self.assertEqual(res[-1], ans[i][j])
|
||||
else:
|
||||
self.assertEqual(res, ans[i][j])
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_lt(self):
|
||||
pass
|
||||
@@ -130,8 +85,6 @@ class TestFraction(unittest.TestCase):
|
||||
f = Fraction(2, 3)
|
||||
ans = "2 / 3"
|
||||
self.assertEqual(f.__txt__(), ans)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
@@ -33,19 +33,20 @@ class TestPolynom(unittest.TestCase):
|
||||
|
||||
def test_eval_base(self):
|
||||
p = Polynom([1, 2])
|
||||
self.assertEqual(p(3).simplified(), 7)
|
||||
self.assertEqual(p(3).simplify(), 7)
|
||||
|
||||
def test_eval_const(self):
|
||||
p = Polynom([1])
|
||||
self.assertEqual(p(3).simplified(), 1)
|
||||
self.assertEqual(p(3).simplify(), 1)
|
||||
|
||||
def test_eval_const_neg(self):
|
||||
p = Polynom([-1])
|
||||
self.assertEqual(p(3).simplified(), -1)
|
||||
self.assertEqual(p(3).simplify(), -1)
|
||||
|
||||
def test_eval_poly(self):
|
||||
p = Polynom([1, 2])
|
||||
self.assertEqual(p("1+h").simplified(), Polynom([3,2], "h"))
|
||||
hp1 = Expression("h+1")
|
||||
self.assertEqual(p(hp1).simplify(), Polynom([3,2], "h"))
|
||||
|
||||
#def test_print(self):
|
||||
# p = Polynom([1,2,3])
|
||||
@@ -71,97 +72,104 @@ class TestPolynom(unittest.TestCase):
|
||||
p = Polynom([1,2,3])
|
||||
#ans = [1, 2, "x", "*", "+", 3, "x", 2, "^", "*", "+"]
|
||||
ans = [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+']
|
||||
self.assertEqual(ans, p.postfix)
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_monom(self):
|
||||
p = Polynom([0,2])
|
||||
ans = [2, "x", "*"]
|
||||
self.assertEqual(ans, p.postfix)
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_0_coef(self):
|
||||
p = Polynom([0,2,0,4])
|
||||
#ans = [2, "x", "*", 4, "x", 3, "^", "*", "+"]
|
||||
ans = [4, 'x', 3, '^', '*', 2, 'x', '*', '+']
|
||||
self.assertEqual(ans, p.postfix)
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_1_coef(self):
|
||||
p = Polynom([0,1,1])
|
||||
#ans = ["x", "x", 2, "^", "+"]
|
||||
ans = ["x", 2, "^", "x", "+"]
|
||||
self.assertEqual(ans, p.postfix)
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_neg_coef(self):
|
||||
# TODO: Choix arbitraire (vis à vis des + et des -) il faudra faire en fonction de render |sam. juin 14 09:45:55 CEST 2014
|
||||
p = Polynom([-1,-2,-3])
|
||||
#ans = [-1, -2, "x", "*", "+", -3, "x", 2, "^", "*", "+"]
|
||||
ans = [3, 'x', 2, '^', '*', '-', 2, 'x', '*', '-', 1, '-']
|
||||
self.assertEqual(ans, p.postfix)
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_multi_coef(self):
|
||||
p = Polynom([1,[2, 3],4])
|
||||
#ans = [1, 2, "x", "*", "+", 3, "x", "*", "+", 4, "x", 2, "^", "*", "+"]
|
||||
ans = [4, 'x', 2, '^', '*', 2, 'x', '*', '+', 3, 'x', '*', '+', 1, '+']
|
||||
self.assertEqual(ans, p.postfix)
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_arithm_coef(self):
|
||||
p = Polynom([1,Expression([2, 3, "+"]),4])
|
||||
#ans = [1, 2, 3, "+", "x", "*", "+", 4, "x", 2, "^", "*", "+"]
|
||||
ans = [4, 'x', 2, '^', '*', 2, 3, '+', 'x', '*', '+', 1, '+']
|
||||
self.assertEqual(ans, p.postfix)
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
#def test_reduce_nilpo(self):
|
||||
# p = Polynom([1, 2, 3])
|
||||
# self.assertEqual(p, p.reduce()[-1])
|
||||
def test_reduce_nilpo(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
self.assertEqual(p, p.reduce())
|
||||
|
||||
def test_reduce(self):
|
||||
p = Polynom([1, [2, 3], 4])
|
||||
self.assertEqual(str(p.reduce()[-1]),'4 x ^ 2 + 5 x + 1')
|
||||
ans = '4 x^{ 2 } + 5 x + 1'
|
||||
self.assertEqual(str(p.reduce()), ans)
|
||||
|
||||
def test_add_int(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
q = (p + 2)[-1]
|
||||
self.assertEqual(str(q), '3 x ^ 2 + 2 x + 3')
|
||||
q = p + 2
|
||||
ans = '3 x^{ 2 } + 2 x + 3'
|
||||
self.assertEqual(str(q), ans)
|
||||
|
||||
def test_add_frac(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
f = Fraction(1, 2)
|
||||
q = (p + f)[-1]
|
||||
#ans = repr(Polynom([Expression(Fraction(3, 2)), Expression(2), Expression(3)]))
|
||||
self.assertEqual(str(q),'3 x ^ 2 + 2 x + 3 / 2')
|
||||
q = p + f
|
||||
ans = '3 x^{ 2 } + 2 x + \\frac{ 3 }{ 2 }'
|
||||
self.assertEqual(str(q),ans)
|
||||
|
||||
def test_add_poly(self):
|
||||
p = Polynom([1, 0, 3])
|
||||
q = Polynom([0, 2, 3])
|
||||
r = (p + q)[-1]
|
||||
self.assertEqual(str(r), '6 x ^ 2 + 2 x + 1')
|
||||
r = p + q
|
||||
ans = '6 x^{ 2 } + 2 x + 1'
|
||||
self.assertEqual(str(r), ans)
|
||||
|
||||
def test_sub_int(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
q = (p - 2)[-1]
|
||||
self.assertEqual(str(q), '3 x ^ 2 + 2 x - 1')
|
||||
q = p - 2
|
||||
ans = '3 x^{ 2 } + 2 x - 1'
|
||||
self.assertEqual(str(q),ans )
|
||||
|
||||
def test_sub_frac(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
f = Fraction(1, 2)
|
||||
q = (p - f)[-1]
|
||||
#ans = repr(Polynom([Expression(Fraction(3, 2)), Expression(2), Expression(3)]))
|
||||
self.assertEqual(str(q),'3 x ^ 2 + 2 x + 1 / 2')
|
||||
q = p - f
|
||||
ans = '3 x^{ 2 } + 2 x + \\frac{ 1 }{ 2 }'
|
||||
self.assertEqual(str(q), ans)
|
||||
|
||||
def test_sub_poly(self):
|
||||
p = Polynom([1, 0, 2])
|
||||
q = Polynom([0, 2, 3])
|
||||
r = (p - q)[-1]
|
||||
self.assertEqual(str(r), '- x ^ 2 - 2 x + 1')
|
||||
r = p - q
|
||||
ans = '- x^{ 2 } - 2 x + 1'
|
||||
self.assertEqual(str(r), ans)
|
||||
|
||||
def test_pow_monome(self):
|
||||
p = Polynom([0,-2])
|
||||
r = (p**3)[-1]
|
||||
self.assertEqual(str(r), '- 8 x ^ 3')
|
||||
r = p**3
|
||||
ans = '- 8 x^{ 3 }'
|
||||
self.assertEqual(str(r), ans)
|
||||
|
||||
def test_pow2_monome(self):
|
||||
p = Polynom([0,-2])
|
||||
r = (p^3)[-1]
|
||||
self.assertEqual(str(r), '- 8 x ^ 3')
|
||||
r = p^3
|
||||
ans = '- 8 x^{ 3 }'
|
||||
self.assertEqual(str(r), ans)
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user