Mapytex/mapytex/calculus/test/test_polynom.py

191 lines
5.1 KiB
Python

#!/usr/bin/env python
# encoding: utf-8
import unittest
from mapytex.calculus.polynom import Polynom
from mapytex.calculus.fraction import Fraction
from mapytex.calculus.expression import Expression
from mapytex.calculus.render import txt
from mapytex.calculus.operator import op
class TestPolynom(unittest.TestCase):
"""Testing functions from mapytex.calculus.polynom"""
def setup(self):
Expression.set_render(txt)
def test_init(self):
p = Polynom([1, 2, 3], "x")
def test_init_multi(self):
p = Polynom([1, [2, 3], 4], "x")
# def test_init_arith(self):
# p = Polynom([1, [2, 3, "+"], 4], "x")
# def test_init_arith_2(self):
# p = Polynom([1, [[2, 3, "*"],3], 4], "x")
def test_deg(self):
pass
def test_eval_base(self):
p = Polynom([1, 2])
self.assertEqual(p(3), 7)
def test_eval_const(self):
p = Polynom([1])
self.assertEqual(p(3), 1)
def test_eval_const_neg(self):
p = Polynom([-1])
self.assertEqual(p(3), -1)
def test_eval_poly(self):
p = Polynom([1, 2])
self.assertEqual(p("h+1"), Polynom([3, 2], "h"))
def test_postfix(self):
p = Polynom([1, 2, 3])
ans = [3, 'x', 2, op.pw, op.mul, 2, 'x', op.mul, op.add, 1, op.add]
self.assertEqual(ans, p.postfix_tokens)
def test_postfix_monom(self):
p = Polynom([0, 2])
ans = [2, "x", op.mul]
self.assertEqual(ans, p.postfix_tokens)
def test_postfix_0_coef(self):
p = Polynom([0, 2, 0, 4])
ans = [4, 'x', 3, op.pw, op.mul, 2, 'x', op.mul, op.add]
self.assertEqual(ans, p.postfix_tokens)
def test_postfix_1_coef(self):
p = Polynom([0, 1, 1])
ans = ["x", 2, op.pw, "x", op.add]
self.assertEqual(ans, p.postfix_tokens)
def test_postfix_neg_coef(self):
p = Polynom([-1, -2, -3])
ans = [-3, 'x', 2, op.pw, op.mul, -2, 'x', op.mul, op.add, -1, op.add]
self.assertEqual(ans, p.postfix_tokens)
def test_postfix_multi_coef(self):
p = Polynom([1, [2, 3], 4])
ans = [4, 'x', 2, op.pw, op.mul, 2, 'x', op.mul, op.add, 3, 'x', op.mul, op.add, 1, op.add]
self.assertEqual(ans, p.postfix_tokens)
def test_postfix_exp_coef(self):
p = Polynom([1, Expression([2, 3, "+"]), 4])
ans = [4, 'x', 2, op.pw, op.mul, 2, 3, op.add, 'x', op.mul, op.add, 1, op.add]
self.assertEqual(ans, p.postfix_tokens)
def test_str(self):
p = Polynom([1, 2, 3])
ans = '3 x^{ 2 } + 2 x + 1'
self.assertEqual(ans, str(p))
def test_str_monom(self):
p = Polynom([0, 2])
ans = '2 x'
self.assertEqual(ans, str(p))
def test_str_0_coef(self):
p = Polynom([0, 2, 0, 4])
ans = '4 x^{ 3 } + 2 x'
self.assertEqual(ans, str(p))
def test_str_1_coef(self):
p = Polynom([0, 1, 1])
ans = 'x^{ 2 } + x'
self.assertEqual(ans, str(p))
def test_str_neg_coef(self):
p = Polynom([-1, -2, -3])
ans = '-3 x^{ 2 } - 2 x - 1'
self.assertEqual(ans, str(p))
def test_str_multi_coef(self):
p = Polynom([1, [2, 3], 4])
ans = '4 x^{ 2 } + 2 x + 3 x + 1'
self.assertEqual(ans, str(p))
def test_str_exp_coef(self):
p = Polynom([1, Expression([2, 3, "+"]), 4])
ans = '4 x^{ 2 } + ( 2 + 3 ) x + 1'
self.assertEqual(ans, str(p))
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])
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
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
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
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
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
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
ans = '- x^{ 2 } - 2 x + 1'
self.assertEqual(str(r), ans)
def test_pow_monome(self):
p = Polynom([0, -2])
r = p**3
ans = '-8 x^{ 3 }'
self.assertEqual(str(r), ans)
def test_pow2_monome(self):
p = Polynom([0, -2])
r = p ^ 3
ans = '-8 x^{ 3 }'
self.assertEqual(str(r), ans)
if __name__ == '__main__':
unittest.main()
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del