def test_PolynomialRing_from_FractionField(): x = DMF(([1, 0, 1], [1, 1]), ZZ) y = DMF(([1, 0, 1], [1]), ZZ) assert ZZ['x'].from_FractionField(x, ZZ['x']) is None assert ZZ['x'].from_FractionField(y, ZZ['x']) == DMP( [ZZ(1), ZZ(0), ZZ(1)], ZZ)
def test_DMF_properties(): assert DMF([[]], ZZ).is_zero == True assert DMF([[]], ZZ).is_one == False assert DMF([[1]], ZZ).is_zero == False assert DMF([[1]], ZZ).is_one == True assert DMF(([[1]], [[2]]), ZZ).is_one == False
def test___hash__(): # Issue 2472 # Make sure int vs. long doesn't affect hashing with Python ground types assert DMP([[1, 2], [3]], ZZ) == DMP([[1l, 2l], [3l]], ZZ) assert hash(DMP([[1, 2], [3]], ZZ)) == hash(DMP([[1l, 2l], [3l]], ZZ)) assert DMF(([[1, 2], [3]], [[1]]), ZZ) == DMF(([[1L, 2L], [3L]], [[1L]]), ZZ) assert hash(DMF(([[1, 2], [3]], [[1]]), ZZ)) == hash(DMF(([[1L, 2L], [3L]], [[1L]]), ZZ)) assert ANP([1, 1], [1, 0, 1], ZZ) == ANP([1l, 1l], [1l, 0l, 1l], ZZ) assert hash(ANP([1, 1], [1, 0, 1], ZZ)) == hash(ANP([1l, 1l], [1l, 0l, 1l], ZZ))
def test___hash__(): # issue 5571 # Make sure int vs. long doesn't affect hashing with Python ground types assert DMP([[1, 2], [3]], ZZ) == DMP([[long(1), long(2)], [long(3)]], ZZ) assert hash(DMP([[1, 2], [3]], ZZ)) == hash(DMP([[long(1), long(2)], [long(3)]], ZZ)) assert DMF( ([[1, 2], [3]], [[1]]), ZZ) == DMF(([[long(1), long(2)], [long(3)]], [[long(1)]]), ZZ) assert hash(DMF(([[1, 2], [3]], [[1]]), ZZ)) == hash(DMF(([[long(1), long(2)], [long(3)]], [[long(1)]]), ZZ)) assert ANP([1, 1], [1, 0, 1], ZZ) == ANP([long(1), long(1)], [long(1), long(0), long(1)], ZZ) assert hash( ANP([1, 1], [1, 0, 1], ZZ)) == hash(ANP([long(1), long(1)], [long(1), long(0), long(1)], ZZ))
def test_polys(): x = Symbol("x") f = Poly(x, x) g = lambda x: x ZZ = ZZ_python() QQ = QQ_sympy() for c in (Poly, Poly(x, x)): check(c) for c in (GFP, GFP([ZZ(1), ZZ(2), ZZ(3)], ZZ(7), ZZ)): check(c) for c in (DUP, DUP([ZZ(1), ZZ(2), ZZ(3)], ZZ(7), ZZ)): check(c) for c in (DMP, DMP([ZZ(1), ZZ(2), ZZ(3)], 0, ZZ)): check(c) for c in (DMF, DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(3)], ZZ))): check(c) for c in (ANP, ANP([QQ(1), QQ(2)], [QQ(1), QQ(2), QQ(3)], QQ)): check(c) for c in (ZZ_python, ZZ_python()): check(c) for c in (ZZ_sympy, ZZ_sympy()): check(c) for c in (QQ_sympy, QQ_sympy()): check(c) for c in (PolynomialRing, PolynomialRing(ZZ, 'x', 'y')): check(c) for c in (FractionField, FractionField(ZZ, 'x', 'y')): check(c) for c in (ExpressionDomain, ExpressionDomain()): check(c) try: from sympy.polys.algebratools import QQ_python for c in (QQ_python, QQ_python()): check(c) except ImportError: pass try: from sympy.polys.algebratools import QQ_python for c in (ZZ_gmpy, ZZ_gmpy()): check(c) for c in (QQ_gmpy, QQ_gmpy()): check(c) except ImportError: pass for c in (RootOf, RootOf(f, 0), RootsOf, RootsOf(x, x), RootSum, RootSum(g, f)): check(c)
def test_pickling_polys_polyclasses(): from sympy.polys.polyclasses import DMP, DMF, ANP for c in (DMP, DMP([[ZZ(1)], [ZZ(2)], [ZZ(3)]], ZZ)): check(c) for c in (DMF, DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(3)]), ZZ)): check(c) for c in (ANP, ANP([QQ(1), QQ(2)], [QQ(1), QQ(2), QQ(3)], QQ)): check(c)
def test_polys(): x = Symbol("x") ZZ = PythonIntegerRing() QQ = SymPyRationalField() for c in (Poly, Poly(x, x)): check(c) for c in (GFP, GFP([ZZ(1), ZZ(2), ZZ(3)], ZZ(7), ZZ)): check(c) for c in (DMP, DMP([ZZ(1), ZZ(2), ZZ(3)], 0, ZZ)): check(c) for c in (DMF, DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(3)], ZZ))): check(c) for c in (ANP, ANP([QQ(1), QQ(2)], [QQ(1), QQ(2), QQ(3)], QQ)): check(c) for c in (PythonIntegerRing, PythonIntegerRing()): check(c) for c in (SymPyIntegerRing, SymPyIntegerRing()): check(c) for c in (SymPyRationalField, SymPyRationalField()): check(c) for c in (PolynomialRing, PolynomialRing(ZZ, 'x', 'y')): check(c) for c in (FractionField, FractionField(ZZ, 'x', 'y')): check(c) for c in (ExpressionDomain, ExpressionDomain()): check(c) from sympy.polys.domains import HAS_FRACTION, HAS_GMPY if HAS_FRACTION: from sympy.polys.domains import PythonRationalField for c in (PythonRationalField, PythonRationalField()): check(c) if HAS_GMPY: from sympy.polys.domains import GMPYIntegerRing, GMPYRationalField for c in (GMPYIntegerRing, GMPYIntegerRing()): check(c) for c in (GMPYRationalField, GMPYRationalField()): check(c) f = x**3 + x + 3 g = lambda x: x for c in (RootOf, RootOf(f, 0), RootSum, RootSum(f, g)): check(c)
def test_polys(): x = Symbol("X") ZZ = PythonIntegerRing() QQ = PythonRationalField() for c in (Poly, Poly(x, x)): check(c) for c in (DMP, DMP([[ZZ(1)], [ZZ(2)], [ZZ(3)]], ZZ)): check(c) for c in (DMF, DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(3)]), ZZ)): check(c) for c in (ANP, ANP([QQ(1), QQ(2)], [QQ(1), QQ(2), QQ(3)], QQ)): check(c) for c in (PythonIntegerRing, PythonIntegerRing()): check(c) for c in (PythonRationalField, PythonRationalField()): check(c) for c in (PolynomialRing, PolynomialRing(ZZ, 'x', 'y')): check(c) for c in (FractionField, FractionField(ZZ, 'x', 'y')): check(c) for c in (ExpressionDomain, ExpressionDomain()): check(c) from sympy.core.compatibility import HAS_GMPY if HAS_GMPY: from sympy.polys.domains import GMPYIntegerRing, GMPYRationalField for c in (GMPYIntegerRing, GMPYIntegerRing()): check(c) for c in (GMPYRationalField, GMPYRationalField()): check(c) f = x**3 + x + 3 g = exp for c in (RootOf, RootOf(f, 0), RootSum, RootSum(f, g)): check(c)
def test_DMF_arithmetics(): f = DMF([[7],[-9]], ZZ) g = DMF([[-7],[9]], ZZ) assert f.neg() == -f == g f = DMF(([[1]], [[1],[]]), ZZ) g = DMF(([[1]], [[1,0]]), ZZ) h = DMF(([[1],[1,0]], [[1,0],[]]), ZZ) assert f.add(g) == f + g == h assert g.add(f) == g + f == h h = DMF(([[-1],[1,0]], [[1,0],[]]), ZZ) assert f.sub(g) == f - g == h h = DMF(([[1]], [[1,0],[]]), ZZ) assert f.mul(g) == f*g == h assert g.mul(f) == g*f == h h = DMF(([[1,0]], [[1],[]]), ZZ) assert f.quo(g) == f/g == h h = DMF(([[1]], [[1],[],[],[]]), ZZ) assert f.pow(3) == f**3 == h h = DMF(([[1]], [[1,0,0,0]]), ZZ) assert g.pow(3) == g**3 == h
def test_PolynomialRing__from_FractionField(): f = DMF(([1, 0, 1], [1, 1]), ZZ) g = DMF(([1, 0, 1], [1]), ZZ) assert ZZ[x].from_FractionField(f, ZZ[x]) is None assert ZZ[x].from_FractionField(g, ZZ[x]) == DMP([ZZ(1), ZZ(0), ZZ(1)], ZZ)
def test_DMF__bool__(): assert bool(DMF([[]], ZZ)) == False assert bool(DMF([[1]], ZZ)) == True
def test_DMF__init__(): f = DMF(([[0],[],[0,1,2],[3]], [[1,2,3]]), ZZ) assert f.num == [[1,2],[3]] assert f.den == [[1,2,3]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[1,2],[3]], [[1,2,3]]), ZZ, 1) assert f.num == [[1,2],[3]] assert f.den == [[1,2,3]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[-1],[-2]],[[3],[-4]]), ZZ) assert f.num == [[-1],[-2]] assert f.den == [[3],[-4]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[1],[2]],[[-3],[4]]), ZZ) assert f.num == [[-1],[-2]] assert f.den == [[3],[-4]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[1],[2]],[[-3],[4]]), ZZ) assert f.num == [[-1],[-2]] assert f.den == [[3],[-4]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[]],[[-3],[4]]), ZZ) assert f.num == [[]] assert f.den == [[1]] assert f.lev == 1 assert f.dom == ZZ f = DMF(17, ZZ, 1) assert f.num == [[17]] assert f.den == [[1]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[1],[2]]), ZZ) assert f.num == [[1],[2]] assert f.den == [[1]] assert f.lev == 1 assert f.dom == ZZ f = DMF([[0],[],[0,1,2],[3]], ZZ) assert f.num == [[1,2],[3]] assert f.den == [[1]] assert f.lev == 1 assert f.dom == ZZ f = DMF({(1,1): 1, (0,0): 2}, ZZ, 1) assert f.num == [[1,0],[2]] assert f.den == [[1]] assert f.lev == 1 assert f.dom == ZZ raises(ValueError, "DMF(([1], [[1]]), ZZ)") raises(ZeroDivisionError, "DMF(([1], []), ZZ)")
def test_DMF__init__(): f = DMF(([[0], [], [0, 1, 2], [3]], [[1, 2, 3]]), ZZ) assert f.num == [[1, 2], [3]] assert f.den == [[1, 2, 3]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[1, 2], [3]], [[1, 2, 3]]), ZZ, 1) assert f.num == [[1, 2], [3]] assert f.den == [[1, 2, 3]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[-1], [-2]], [[3], [-4]]), ZZ) assert f.num == [[-1], [-2]] assert f.den == [[3], [-4]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[1], [2]], [[-3], [4]]), ZZ) assert f.num == [[-1], [-2]] assert f.den == [[3], [-4]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[1], [2]], [[-3], [4]]), ZZ) assert f.num == [[-1], [-2]] assert f.den == [[3], [-4]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[]], [[-3], [4]]), ZZ) assert f.num == [[]] assert f.den == [[1]] assert f.lev == 1 assert f.dom == ZZ f = DMF(17, ZZ, 1) assert f.num == [[17]] assert f.den == [[1]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[1], [2]]), ZZ) assert f.num == [[1], [2]] assert f.den == [[1]] assert f.lev == 1 assert f.dom == ZZ f = DMF([[0], [], [0, 1, 2], [3]], ZZ) assert f.num == [[1, 2], [3]] assert f.den == [[1]] assert f.lev == 1 assert f.dom == ZZ f = DMF({(1, 1): 1, (0, 0): 2}, ZZ, 1) assert f.num == [[1, 0], [2]] assert f.den == [[1]] assert f.lev == 1 assert f.dom == ZZ f = DMF(([[QQ(1)], [QQ(2)]], [[-QQ(3)], [QQ(4)]]), QQ) assert f.num == [[-QQ(1)], [-QQ(2)]] assert f.den == [[QQ(3)], [-QQ(4)]] assert f.lev == 1 assert f.dom == QQ f = DMF(([[QQ(1, 5)], [QQ(2, 5)]], [[-QQ(3, 7)], [QQ(4, 7)]]), QQ) assert f.num == [[-QQ(7)], [-QQ(14)]] assert f.den == [[QQ(15)], [-QQ(20)]] assert f.lev == 1 assert f.dom == QQ raises(ValueError, lambda: DMF(([1], [[1]]), ZZ)) raises(ZeroDivisionError, lambda: DMF(([1], []), ZZ))
def __call__(self, a): """Construct an element of `self` domain from `a`. """ return DMF(a, self.dom, len(self.gens)-1, ring=self)