2014-11-11 09:45:45 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from pymath.polynom import Polynom
|
|
|
|
from pymath.fraction import Fraction
|
2014-11-15 09:16:59 +00:00
|
|
|
from pymath.expression import Expression
|
2014-12-21 18:13:34 +00:00
|
|
|
from pymath.render import txt
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestPolynom(unittest.TestCase):
|
|
|
|
"""Testing functions from pymath.polynom"""
|
|
|
|
|
2014-12-21 18:13:34 +00:00
|
|
|
def setup(self):
|
|
|
|
Expression.set_render(txt)
|
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
def test_init(self):
|
|
|
|
p = Polynom([1, 2, 3], "x")
|
|
|
|
|
|
|
|
def test_init_multi(self):
|
|
|
|
p = Polynom([1, [2, 3], 4], "x")
|
|
|
|
|
2014-11-11 20:51:03 +00:00
|
|
|
#def test_init_arith(self):
|
|
|
|
# p = Polynom([1, [2, 3, "+"], 4], "x")
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-11 20:51:03 +00:00
|
|
|
#def test_init_arith_2(self):
|
|
|
|
# p = Polynom([1, [[2, 3, "*"],3], 4], "x")
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def test_deg(self):
|
|
|
|
pass
|
|
|
|
|
2014-12-21 18:13:34 +00:00
|
|
|
def test_eval_base(self):
|
|
|
|
p = Polynom([1, 2])
|
2015-03-07 08:15:21 +00:00
|
|
|
self.assertEqual(p(3), 7)
|
2014-12-21 18:13:34 +00:00
|
|
|
|
|
|
|
def test_eval_const(self):
|
|
|
|
p = Polynom([1])
|
2015-03-07 08:15:21 +00:00
|
|
|
self.assertEqual(p(3), 1)
|
2014-12-21 18:13:34 +00:00
|
|
|
|
|
|
|
def test_eval_const_neg(self):
|
|
|
|
p = Polynom([-1])
|
2015-03-07 08:15:21 +00:00
|
|
|
self.assertEqual(p(3), -1)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-12-22 14:32:17 +00:00
|
|
|
def test_eval_poly(self):
|
|
|
|
p = Polynom([1, 2])
|
2015-03-07 08:15:21 +00:00
|
|
|
self.assertEqual(p("h+1"), Polynom([3,2], "h"))
|
2014-12-22 09:13:20 +00:00
|
|
|
|
2014-11-11 09:45:45 +00:00
|
|
|
#def test_print(self):
|
|
|
|
# p = Polynom([1,2,3])
|
|
|
|
# ans = "1 + 2 x + 3 x^2"
|
|
|
|
# self.assertEqual(ans, str(p))
|
|
|
|
|
|
|
|
#def test_print_monom(self):
|
|
|
|
# p = Polynom([0,2])
|
|
|
|
# ans = "2 x"
|
|
|
|
# self.assertEqual(ans, str(p))
|
|
|
|
|
|
|
|
#def test_print_0_coef(self):
|
|
|
|
# p = Polynom([0,1,3])
|
|
|
|
# ans = "x + 3 x^2"
|
|
|
|
# self.assertEqual(ans, str(p))
|
|
|
|
|
|
|
|
#def test_print_multi_coef(self):
|
|
|
|
# p = Polynom([1,[2, -2],3])
|
|
|
|
# ans = "1 + 2 x - 2 x + 3 x^2"
|
|
|
|
# self.assertEqual(ans, str(p))
|
|
|
|
|
2014-11-11 20:51:03 +00:00
|
|
|
def test_postfix(self):
|
2014-11-11 09:45:45 +00:00
|
|
|
p = Polynom([1,2,3])
|
|
|
|
#ans = [1, 2, "x", "*", "+", 3, "x", 2, "^", "*", "+"]
|
|
|
|
ans = [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+']
|
2015-02-28 21:52:28 +00:00
|
|
|
self.assertEqual(ans, p.postfix_tokens)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-11 20:51:03 +00:00
|
|
|
def test_postfix_monom(self):
|
2014-11-11 09:45:45 +00:00
|
|
|
p = Polynom([0,2])
|
|
|
|
ans = [2, "x", "*"]
|
2015-02-28 21:52:28 +00:00
|
|
|
self.assertEqual(ans, p.postfix_tokens)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-11 20:51:03 +00:00
|
|
|
def test_postfix_0_coef(self):
|
2014-11-11 09:45:45 +00:00
|
|
|
p = Polynom([0,2,0,4])
|
|
|
|
#ans = [2, "x", "*", 4, "x", 3, "^", "*", "+"]
|
|
|
|
ans = [4, 'x', 3, '^', '*', 2, 'x', '*', '+']
|
2015-02-28 21:52:28 +00:00
|
|
|
self.assertEqual(ans, p.postfix_tokens)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-11 20:51:03 +00:00
|
|
|
def test_postfix_1_coef(self):
|
2014-11-11 09:45:45 +00:00
|
|
|
p = Polynom([0,1,1])
|
|
|
|
#ans = ["x", "x", 2, "^", "+"]
|
|
|
|
ans = ["x", 2, "^", "x", "+"]
|
2015-02-28 21:52:28 +00:00
|
|
|
self.assertEqual(ans, p.postfix_tokens)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-11 20:51:03 +00:00
|
|
|
def test_postfix_neg_coef(self):
|
2014-11-11 09:45:45 +00:00
|
|
|
# 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, "^", "*", "+"]
|
2014-12-21 07:53:10 +00:00
|
|
|
ans = [3, 'x', 2, '^', '*', '-', 2, 'x', '*', '-', 1, '-']
|
2015-02-28 21:52:28 +00:00
|
|
|
self.assertEqual(ans, p.postfix_tokens)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-11 20:51:03 +00:00
|
|
|
def test_postfix_multi_coef(self):
|
2014-11-11 09:45:45 +00:00
|
|
|
p = Polynom([1,[2, 3],4])
|
|
|
|
#ans = [1, 2, "x", "*", "+", 3, "x", "*", "+", 4, "x", 2, "^", "*", "+"]
|
|
|
|
ans = [4, 'x', 2, '^', '*', 2, 'x', '*', '+', 3, 'x', '*', '+', 1, '+']
|
2015-02-28 21:52:28 +00:00
|
|
|
self.assertEqual(ans, p.postfix_tokens)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-11-11 20:51:03 +00:00
|
|
|
def test_postfix_arithm_coef(self):
|
2014-11-15 09:16:59 +00:00
|
|
|
p = Polynom([1,Expression([2, 3, "+"]),4])
|
2014-11-11 09:45:45 +00:00
|
|
|
#ans = [1, 2, 3, "+", "x", "*", "+", 4, "x", 2, "^", "*", "+"]
|
|
|
|
ans = [4, 'x', 2, '^', '*', 2, 3, '+', 'x', '*', '+', 1, '+']
|
2015-02-28 21:52:28 +00:00
|
|
|
self.assertEqual(ans, p.postfix_tokens)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2015-02-28 21:52:28 +00:00
|
|
|
def test_reduce_nilpo(self):
|
|
|
|
p = Polynom([1, 2, 3])
|
|
|
|
self.assertEqual(p, p.reduce())
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def test_reduce(self):
|
|
|
|
p = Polynom([1, [2, 3], 4])
|
2015-02-28 21:52:28 +00:00
|
|
|
ans = '4 x^{ 2 } + 5 x + 1'
|
|
|
|
self.assertEqual(str(p.reduce()), ans)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def test_add_int(self):
|
|
|
|
p = Polynom([1, 2, 3])
|
2015-02-28 21:52:28 +00:00
|
|
|
q = p + 2
|
|
|
|
ans = '3 x^{ 2 } + 2 x + 3'
|
|
|
|
self.assertEqual(str(q), ans)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def test_add_frac(self):
|
|
|
|
p = Polynom([1, 2, 3])
|
|
|
|
f = Fraction(1, 2)
|
2015-02-28 21:52:28 +00:00
|
|
|
q = p + f
|
|
|
|
ans = '3 x^{ 2 } + 2 x + \\frac{ 3 }{ 2 }'
|
|
|
|
self.assertEqual(str(q),ans)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
def test_add_poly(self):
|
|
|
|
p = Polynom([1, 0, 3])
|
|
|
|
q = Polynom([0, 2, 3])
|
2015-02-28 21:52:28 +00:00
|
|
|
r = p + q
|
|
|
|
ans = '6 x^{ 2 } + 2 x + 1'
|
|
|
|
self.assertEqual(str(r), ans)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
2014-12-21 07:53:10 +00:00
|
|
|
def test_sub_int(self):
|
|
|
|
p = Polynom([1, 2, 3])
|
2015-02-28 21:52:28 +00:00
|
|
|
q = p - 2
|
|
|
|
ans = '3 x^{ 2 } + 2 x - 1'
|
|
|
|
self.assertEqual(str(q),ans )
|
2014-12-21 07:53:10 +00:00
|
|
|
|
|
|
|
def test_sub_frac(self):
|
|
|
|
p = Polynom([1, 2, 3])
|
|
|
|
f = Fraction(1, 2)
|
2015-02-28 21:52:28 +00:00
|
|
|
q = p - f
|
|
|
|
ans = '3 x^{ 2 } + 2 x + \\frac{ 1 }{ 2 }'
|
|
|
|
self.assertEqual(str(q), ans)
|
2014-12-21 07:53:10 +00:00
|
|
|
|
|
|
|
def test_sub_poly(self):
|
|
|
|
p = Polynom([1, 0, 2])
|
|
|
|
q = Polynom([0, 2, 3])
|
2015-02-28 21:52:28 +00:00
|
|
|
r = p - q
|
|
|
|
ans = '- x^{ 2 } - 2 x + 1'
|
|
|
|
self.assertEqual(str(r), ans)
|
2014-12-21 07:53:10 +00:00
|
|
|
|
2014-12-22 14:54:07 +00:00
|
|
|
def test_pow_monome(self):
|
|
|
|
p = Polynom([0,-2])
|
2015-02-28 21:52:28 +00:00
|
|
|
r = p**3
|
|
|
|
ans = '- 8 x^{ 3 }'
|
|
|
|
self.assertEqual(str(r), ans)
|
2014-12-21 07:53:10 +00:00
|
|
|
|
2014-12-22 14:54:07 +00:00
|
|
|
def test_pow2_monome(self):
|
|
|
|
p = Polynom([0,-2])
|
2015-02-28 21:52:28 +00:00
|
|
|
r = p^3
|
|
|
|
ans = '- 8 x^{ 3 }'
|
|
|
|
self.assertEqual(str(r), ans)
|
2014-11-11 09:45:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|
|
|
|
|