def test_nonzero_enumerate(self) : x, y = MVPolyCube.variables(2) p = 3*x + y**2 + 7 obt = p.nonzero exp = [((0, 0), 7.0), ((1, 0), 3.0), ((0, 2), 1.0)] self.assertTrue(sorted(obt) == sorted(exp), "nonzero")
def test_multiply_complex(self) : x, y = MVPolyCube.variables(2, dtype=complex) p1 = (x + y)*(x + 1j*y) p2 = x**2 + (1 + 1j)*x*y + 1j*y**2 self.assertTrue((p1.coef == p2.coef).all(), "bad multiply:\n{0!s}\n{1!s}".format(repr(p1.coef), repr(p2.coef)))
def test_multiply_arithmetic(self) : x, y = MVPolyCube.variables(2, dtype=int) p1 = (x + y)*(2*x - y) p2 = 2*x**2 + x*y - y**2 self.assertTrue((p1.coef == p2.coef).all(), "bad multiply:\n{0!s}\n{1!s}".format(repr(p1.coef), repr(p2.coef)))
def test_maxmodnb_simple(self) : eps = 1e-10 x, y = MVPolyCube.variables(2, dtype=complex) p = x**2 + 1 expected = 2.0 obtained = p.maxmodnb(eps = eps)[0] self.assertTrue(abs(expected - obtained) < eps*expected, "bad maxmodnb {0!s}".format(repr(obtained)))
def test_variables_dtype(self) : x, y = MVPolyCube.variables(2, dtype=int) p = 2*x**2 + 3*x*y + 1 for m in (x, y, p) : self.assertTrue(m.dtype == int, "bad type: \n{0!s}".format(repr(m.dtype))) self.assertTrue(m.coef.dtype == int, "bad type: \n{0!s}".format(repr(m.coef.dtype)))
def test_variables_create(self) : x, y, z = MVPolyCube.variables(3) self.assertTrue((x.coef == [[[0]],[[1]]]).all(), "bad x variable: \n{0!s}".format(repr(x.coef))) self.assertTrue((y.coef == [[[0],[1]]]).all(), "bad y variable: \n{0!s}".format(repr(y.coef))) self.assertTrue((z.coef == [[[0, 1]]]).all(), "bad z variable: \n{0!s}".format(repr(z.coef)))
def test_diff_invariant(self) : x, y = MVPolyCube.variables(2, dtype=int) p = x + 2*y expected = p.coef.copy() q = p.diff(1,0) obtained = p.coef self.assertTrue((expected == obtained).all(), "polynomial modified by diff {0!s}".format( \ (repr(obtained))))
def test_negation(self) : x, y = MVPolyCube.variables(2) p = 2*x**2 - 3*x*y + 1 obtained = (-p).coef expected = [[-1, 0], [ 0, 3], [-2, 0]] self.assertTrue((obtained == expected).all(), "bad negation:\n{0!s}".format(repr(obtained)))
def test_maxmodnb_no_positional_args(self) : x, y = MVPolyCube.variables(2, dtype=complex) p = x**2 + 1 with self.assertRaises(TypeError) : p.maxmodnb(3)
def test_maxmodnb_unknown_keyword(self) : x, y = MVPolyCube.variables(2, dtype=complex) p = x**2 + 1 with self.assertRaises(ValueError) : p.maxmodnb(nosuchvar = 3)
def test_maxmodnb_fifomax(self) : x, y = MVPolyCube.variables(2, dtype=complex) p = x**2 + 1 with self.assertRaises(RuntimeError) : p.maxmodnb(fifomax = 3)
def setUp(self) : self.r, = MVPolyCube.variables(1, dtype = float)
def setUp(self) : x, y = MVPolyCube.variables(2, dtype=int) self.p = self.makep(x, y) self.x = [0, 1, -1, 0, 7, 3, -3, 1] self.y = [0, 0, 0, 3, -1, 2, -10, 2] self.n = len(self.x)
def test_subtract(self) : x, y = MVPolyCube.variables(2) p = 1 - x q = -(x - 1) self.assertTrue((p.coef == q.coef).all(), "bad subtract:\n{0!s}\n{1!s}".format(repr(p.coef), repr(q.coef)))
def test_variables_build(self) : x, y = MVPolyCube.variables(2) p = 2*x**2 + 3*x*y + 1 self.assertTrue((p.coef == [[1, 0], [0, 3], [2, 0]]).all(), "bad build: \n{0!s}".format(repr(p.coef)))
def test_variables_count(self) : for n in [2,3,4] : M = MVPolyCube.variables(n) self.assertTrue(len(M) == n)