def test_intdif_random(self) : for dt in [float, complex] : shp = (9, 10, 11) c = np.random.randint(low=-10, high=10, size=shp) p = MVPolyCube(c, dtype=dt) expected = p.coef obtained = p.int(1, 1, 2).diff(1, 1, 2).coef self.assertTrue((np.abs(expected - obtained) < 1e-10).all(), "bad integrate-differentiate \n{0!s}\n{1!s}".format(repr(obtained), repr(expected)))
def rudin_shapiro(cls, n, **kwargs): """ Return the output of :meth:`mvpoly.cube.MVPolyCube.rudin_shapiro` converted to other polynomial subclasess. """ from mvpoly.cube import MVPolyCube return cls(MVPolyCube.rudin_shapiro(n, **kwargs), **kwargs)
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 lehmer(cls, **kwargs): """ Return the output of :meth:`mvpoly.cube.MVPolyCube.lehmer` converted to other polynomial subclasess. """ from mvpoly.cube import MVPolyCube return cls(MVPolyCube.lehmer(**kwargs), **kwargs)
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 wendland(cls, d, k, **kwargs): """ Return the output of :meth:`mvpoly.cube.MVPolyCube.wendland` converted to other polynomial subclasess. """ from mvpoly.cube import MVPolyCube return cls(MVPolyCube.wendland(d, k, **kwargs), **kwargs)
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_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_regression_broadcast(self) : # regression in __setitem__() : when passed an empty # tuple the value was broadcast to the whole array, # we don't want that coef = np.zeros((3,3), dtype=int) p = MVPolyCube(coef, dtype=int) p[()] = 1 q = MVPolyCube.one(dtype=int) self.assertTrue(p == q, "setitem broadcast regression")
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_monomials_linear(self) : L = MVPolyCube.monomials(2, 1) self.assertTrue(len(L) == 3) self.assertTrue((L[0].coef == [[1]]).all(), "{0!s}".format(repr(L[0].coef))) self.assertTrue((L[1].coef == [[0, 0], [1, 0]]).all(), "{0!s}".format(repr(L[1].coef))) self.assertTrue((L[2].coef == [[0, 1], [0, 0]]).all(), "{0!s}".format(repr(L[2].coef)))
def test_monomials_quadratic(self) : L = MVPolyCube.monomials(2, 2) self.assertTrue(len(L) == 6) self.assertTrue((L[0].coef == [[1]]).all(), "{0!s}".format(repr(L[0].coef))) self.assertTrue((L[1].coef == [[0, 0], [1, 0]]).all(), "{0!s}".format(repr(L[1].coef))) self.assertTrue((L[2].coef == [[0, 1], [0, 0]]).all(), "{0!s}".format(repr(L[2].coef))) self.assertTrue((L[3].coef == [[0, 0, 0], [0, 0, 0], [1, 0, 0]]).all(), "{0!s}".format(repr(L[3].coef))) self.assertTrue((L[4].coef == [[0, 0, 0], [0, 1, 0], [0, 0, 0]]).all(), "{0!s}".format(repr(L[4].coef))) self.assertTrue((L[5].coef == [[0, 0, 1], [0, 0, 0], [0, 0, 0]]).all(), "{0!s}".format(repr(L[5].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_wendland_dim5_order0(self) : r = self.r expected = (1 - r)**3 obtained = MVPolyCube.wendland(5, 0) self.assertTrue(expected == obtained, "{0!s}".format((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_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_maxmodnb_fifomax(self) : x, y = MVPolyCube.variables(2, dtype=complex) p = x**2 + 1 with self.assertRaises(RuntimeError) : p.maxmodnb(fifomax = 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_wendland_dim5_order2(self) : r = self.r expected = (16*r*r + 7*r + 1) * (1 - r)**7 obtained = MVPolyCube.wendland(5, 2) self.assertEqualUpToConstant(expected, obtained)
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_wendland_dim5_order1(self) : r = self.r expected = (5*r + 1) * (1 - r)**5 obtained = MVPolyCube.wendland(5, 1) self.assertEqualUpToConstant(expected, obtained)
def test_wendland_dim3_order3(self) : r = self.r expected = (32*r*r*r + 25*r*r + 8*r + 1) * (1 - r)**8 obtained = MVPolyCube.wendland(3, 3) self.assertEqualUpToConstant(expected, obtained)
def test_wendland_dim3_order2(self) : r = self.r expected = (35*r*r + 18*r + 3) * (1 - r)**6 obtained = MVPolyCube.wendland(3, 2) self.assertEqualUpToConstant(expected, obtained)
def setUp(self) : self.r, = MVPolyCube.variables(1, dtype = float)
def test_variables_count(self) : for n in [2,3,4] : M = MVPolyCube.variables(n) self.assertTrue(len(M) == n)
def wendland(self): return MVPolyCube.wendland(self.dim, self.n)
def test_wendland_dim1_order2(self) : r = self.r expected = (8*r*r + 5*r + 1) * (1 - r)**5 obtained = MVPolyCube.wendland(1, 2) self.assertEqualUpToConstant(expected, obtained)