Debian Codegraph @

wxgeometrie/mathlib/tests/test_internal_functions.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 # -*- coding: utf-8 -*-
2 import os, sys
3 TOPDIR = os.path.abspath(os.path.join(os.path.dirname(__file__),"../.."))
4 sys.path.insert(0, TOPDIR)
5
6 from wxgeometrie.mathlib.internal_functions import poly_factor
7 from sympy import Symbol, sqrt, S, I as i, expand, Integer
8
9 x = Symbol('x')
10
•11 import tools.unittests
12
•13 class MathlibTest(tools.unittests.TestCase):
14
15 def assert_factor(self,poly, *args, **kw):
16 def convert(obj):
17 if isinstance(obj, int):
18 return Integer(obj)
19 return obj
20 var = kw.get("var", x)
21 facteurs = poly_factor(poly, var)
22 poly_args = facteurs.args
23 s = set()
24 s.update(convert(arg) for arg in poly_args)
25 s.update(convert(-arg) for arg in poly_args)
26 TEST1 = set(convert(arg) for arg in args).issubset(s)
27 if not TEST1:
28 print("ERREUR: ", set(args), " n'est pas inclus dans ", s)
29 print("Difference: ", set(args).difference(s))
30 print([type(obj) for obj in set(args).difference(s)])
31 TEST2 = (expand(facteurs) == expand(poly))
32 if not TEST2:
33 print("ERREUR: ", expand(facteurs), " != ", expand(poly))
34 self.assertTrue(TEST1)
35 self.assertTrue(TEST2)
36
37 def test_factor(self):
38 self.assert_factor(x**2 - 1, x-1, x+1)
39 self.assert_factor(x**2 - 2, x - sqrt(2), x + sqrt(2))
40 self.assert_factor(2*x**2 - 2, 2, x-1, x+1)
41 self.assert_factor(1 + 2*x + 2*x**2 + x**3, x+1, x**2+x+1)
42 self.assert_factor(2*x, 2, x)
43 self.assert_factor(x**2-i, x + sqrt(i), x - sqrt(i))