def global_variables(doctest_namespace, monkeypatch): """Ensure certain variables are available during tests.""" doctest_namespace["numpy"] = numpy doctest_namespace["numpoly"] = numpoly environ = os.environ.copy() environ["NUMPOLY_DEBUG"] = True monkeypatch.setattr("os.environ", environ) with numpoly.global_options(**numpoly.get_options(defaults=True)): yield
def test_aspolynomial(): poly = 2 * X - Y + 1 assert poly == numpoly.aspolynomial(poly) assert poly == numpoly.aspolynomial(poly, names=XY) assert poly == numpoly.aspolynomial(poly.todict(), names=XY) assert poly == numpoly.aspolynomial(poly, names=("q0", "q1")) with numpoly.global_options(varname_filter=r"\w+", force_number_suffix=False): assert numpy.all( numpoly.symbols("Z:2") == numpoly.aspolynomial(XY, names="Z")) assert poly == numpoly.aspolynomial(poly.todict(), names=("q0", "q1")) assert poly != numpoly.aspolynomial(poly.todict(), names=("q1", "q0")) assert X == numpoly.aspolynomial(Y, names="q0") assert poly != numpoly.aspolynomial(poly.todict(), names="q0") assert isinstance(numpoly.aspolynomial([1, 2, 3]), numpoly.ndpoly) assert numpy.all(numpoly.aspolynomial([1, 2, 3]) == [1, 2, 3])
def test_ndpoly(): poly = numpoly.ndpoly(exponents=[(1, )], shape=(), names="q0") poly["<"] = 1 assert poly == X poly = numpoly.ndpoly(exponents=[(1, )], shape=(), names=X) poly["<"] = 1 assert poly == X poly = numpoly.ndpoly(exponents=[(1, 0), (0, 1)], shape=(), names=("q0", "q1")) poly["<;"] = 2 poly[";<"] = 3 assert poly == 2 * X + 3 * Y with numpoly.global_options(varname_filter=r"Q\d+"): poly = numpoly.ndpoly(exponents=[(1, 0), (0, 1)], shape=(2, ), names="Q:2") poly["<;"] = [1, 0] poly[";<"] = [0, 1] assert numpy.all(poly == numpoly.symbols("Q0 Q1"))
def test_polynomial(): assert numpoly.polynomial() == 0 assert numpoly.polynomial({(0, ): 4}) == 4 assert numpoly.polynomial({(1, ): 5}, names="q0") == 5 * X assert numpoly.polynomial({ (0, 1): 2, (1, 0): 3 }, names=("q0", "q1")) == 3 * X + 2 * Y with numpoly.global_options(varname_filter=r"\w+"): assert numpy.all( numpoly.polynomial({ (0, 1): [0, 1], (1, 0): [1, 0] }, names="Q") == numpoly.symbols("Q0 Q1")) assert numpoly.polynomial(X) == X assert numpoly.polynomial(numpy.array((3, ), dtype=[(";", int)])) == 3 assert numpoly.polynomial(5.5) == 5.5 assert numpoly.polynomial(sympy.symbols("q0")) == X assert numpy.all(numpoly.polynomial([1, 2, 3]) == [1, 2, 3]) assert numpy.all(numpoly.polynomial([[1, 2], [3, 4]]) == [[1, 2], [3, 4]]) assert numpy.all( numpoly.polynomial(numpy.array([[1, 2], [3, 4]])) == [[1, 2], [3, 4]])
def test_display_order(display_config): """Ensure string output changes with various display options.""" expected_output = display_config.pop("expected_output") polynomial = numpy.sum(numpoly.monomial(3, dimensions=("q0", "q1"))) with numpoly.global_options(**display_config): assert str(polynomial) == expected_output