def test_dup_decompose(): assert dup_decompose([1], ZZ) == [[1]] assert dup_decompose([1, 0], ZZ) == [[1, 0]] assert dup_decompose([1, 0, 0, 0], ZZ) == [[1, 0, 0, 0]] assert dup_decompose([1, 0, 0, 0, 0], ZZ) == [[1, 0, 0], [1, 0, 0]] assert dup_decompose([1, 0, 0, 0, 0, 0, 0], ZZ) == [[1, 0, 0, 0], [1, 0, 0]] assert dup_decompose([7, 0, 0, 0, 1], ZZ) == [[7, 0, 1], [1, 0, 0]] assert dup_decompose([4, 0, 3, 0, 2], ZZ) == [[4, 3, 2], [1, 0, 0]] f = [1, 0, 20, 0, 150, 0, 500, 0, 625, -2, 0, -10, 9] assert dup_decompose(f, ZZ) == [[1, 0, 0, -2, 9], [1, 0, 5, 0]] f = [2, 0, 40, 0, 300, 0, 1000, 0, 1250, -4, 0, -20, 18] assert dup_decompose(f, ZZ) == [[2, 0, 0, -4, 18], [1, 0, 5, 0]] f = [1, 0, 20, -8, 150, -120, 524, -600, 865, -1034, 600, -170, 29] assert dup_decompose(f, ZZ) == [[1, -8, 24, -34, 29], [1, 0, 5, 0]] f = [ DMP([6, 0, -42], ZZ), DMP([48, 0, 96], ZZ), DMP([144, 648, 288], ZZ), DMP([624, 864, 384], ZZ), DMP([108, 312, 432, 192], ZZ) ] assert dup_decompose(f, ZZ['a']) == [f]
def test_dmp_convert(): K0, K1 = ZZ['x'], ZZ f = [[DMP([1], ZZ)],[DMP([2], ZZ)],[],[DMP([3], ZZ)]] assert dmp_convert(f, 1, K0, K1) == \ [[ZZ(1)],[ZZ(2)],[],[ZZ(3)]]
def test_DMP_exclude(): f = [[[[[[[[[[[[[[[[[[[[[[[[[[1]], [[]]]]]]]]]]]]]]]]]]]]]]]]]] J = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 25] assert DMP(f, ZZ).exclude() == (J, DMP([1, 0], ZZ)) assert DMP([[1], [1, 0]], ZZ).exclude() == ([], DMP([[1], [1, 0]], ZZ))
def test_to_algebraic_integer(): a = AlgebraicNumber(sqrt(3), gen=x).to_algebraic_integer() assert a.minpoly == x**2 - 3 assert a.root == sqrt(3) assert a.rep == DMP([QQ(1), QQ(0)], QQ) a = AlgebraicNumber(2 * sqrt(3), gen=x).to_algebraic_integer() assert a.minpoly == x**2 - 12 assert a.root == 2 * sqrt(3) assert a.rep == DMP([QQ(1), QQ(0)], QQ) a = AlgebraicNumber(sqrt(3) / 2, gen=x).to_algebraic_integer() assert a.minpoly == x**2 - 12 assert a.root == 2 * sqrt(3) assert a.rep == DMP([QQ(1), QQ(0)], QQ) a = AlgebraicNumber(sqrt(3) / 2, [S(7) / 19, 3], gen=x).to_algebraic_integer() assert a.minpoly == x**2 - 12 assert a.root == 2 * sqrt(3) assert a.rep == DMP([QQ(7, 19), QQ(3)], QQ)
def test_dup_convert(): K0, K1 = ZZ['x'], ZZ f = [DMP([1], ZZ),DMP([2], ZZ),DMP([], ZZ),DMP([3], ZZ)] assert dup_convert(f, K0, K1) == \ [ZZ(1),ZZ(2),ZZ(0),ZZ(3)]
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_DMP___eq__(): assert DMP([[ZZ(1),ZZ(2)],[ZZ(3)]], ZZ) == \ DMP([[ZZ(1),ZZ(2)],[ZZ(3)]], ZZ) assert DMP([[ZZ(1),ZZ(2)],[ZZ(3)]], ZZ) == \ DMP([[QQ(1),QQ(2)],[QQ(3)]], QQ) assert DMP([[QQ(1),QQ(2)],[QQ(3)]], QQ) == \ DMP([[ZZ(1),ZZ(2)],[ZZ(3)]], ZZ) assert DMP([[[ZZ(1)]]], ZZ) != DMP([[ZZ(1)]], ZZ) assert DMP([[ZZ(1)]], ZZ) != DMP([[[ZZ(1)]]], 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 legendre_poly(n, x=None, polys=False): """Generates Legendre polynomial of degree `n` in `x`. Parameters ========== n : int `n` decides the degree of polynomial x : optional polys : bool, optional ``polys=True`` returns an expression, otherwise (default) returns an expression. """ if n < 0: raise ValueError("Cannot generate Legendre polynomial of degree %s" % n) poly = DMP(dup_legendre(int(n), QQ), QQ) if x is not None: poly = Poly.new(poly, x) else: poly = PurePoly.new(poly, Dummy('x')) return poly if polys else poly.as_expr()
def laguerre_poly(n, x=None, alpha=None, polys=False): """Generates Laguerre polynomial of degree `n` in `x`. Parameters ========== n : int `n` decides the degree of polynomial x : optional alpha Decides minimal domain for the list of coefficients. polys : bool, optional ``polys=True`` returns an expression, otherwise (default) returns an expression. """ if n < 0: raise ValueError("can't generate Laguerre polynomial of degree %s" % n) if alpha is not None: K, alpha = construct_domain( alpha, field=True) # XXX: ground_field=True else: K, alpha = QQ, QQ(0) poly = DMP(dup_laguerre(int(n), alpha, K), K) if x is not None: poly = Poly.new(poly, x) else: poly = PurePoly.new(poly, Dummy('x')) return poly if polys else poly.as_expr()
def test_DUP_to_dict(): f = DMP([[3],[],[2],[],[8]], ZZ) assert f.to_dict() == \ {(4, 0): 3, (2, 0): 2, (0, 0): 8} assert f.to_sympy_dict() == \ {(4, 0): ZZ.to_sympy(3), (2, 0): ZZ.to_sympy(2), (0, 0): ZZ.to_sympy(8)}
def chebyshevu_poly(n, x=None, polys=False): """Generates Chebyshev polynomial of the second kind of degree `n` in `x`. Parameters ========== n : int `n` decides the degree of polynomial x : optional polys : bool, optional ``polys=True`` returns an expression, otherwise (default) returns an expression. """ if n < 0: raise ValueError( "can't generate 2nd kind Chebyshev polynomial of degree %s" % n) poly = DMP(dup_chebyshevu(int(n), ZZ), ZZ) if x is not None: poly = Poly.new(poly, x) else: poly = PurePoly.new(poly, Dummy('x')) return poly if polys else poly.as_expr()
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 jacobi_poly(n, a, b, x=None, polys=False): """Generates Jacobi polynomial of degree `n` in `x`. Parameters ========== n : int `n` decides the degree of polynomial a Lower limit of minimal domain for the list of coefficients. b Upper limit of minimal domain for the list of coefficients. x : optional polys : bool, optional ``polys=True`` returns an expression, otherwise (default) returns an expression. """ if n < 0: raise ValueError("can't generate Jacobi polynomial of degree %s" % n) K, v = construct_domain([a, b], field=True) poly = DMP(dup_jacobi(int(n), v[0], v[1], K), K) if x is not None: poly = Poly.new(poly, x) else: poly = PurePoly.new(poly, Dummy('x')) return poly if polys else poly.as_expr()
def gegenbauer_poly(n, a, x=None, polys=False): """Generates Gegenbauer polynomial of degree `n` in `x`. Parameters ========== n : int `n` decides the degree of polynomial x : optional a Decides minimal domain for the list of coefficients. polys : bool, optional ``polys=True`` returns an expression, otherwise (default) returns an expression. """ if n < 0: raise ValueError( "can't generate Gegenbauer polynomial of degree %s" % n) K, a = construct_domain(a, field=True) poly = DMP(dup_gegenbauer(int(n), a, K), K) if x is not None: poly = Poly.new(poly, x) else: poly = PurePoly.new(poly, Dummy('x')) return poly if polys else poly.as_expr()
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_DMP_properties(): assert DMP([[]], ZZ).is_zero == True assert DMP([[1]], ZZ).is_zero == False assert DMP([[1]], ZZ).is_one == True assert DMP([[2]], ZZ).is_one == False assert DUP([[1]], ZZ).is_ground == True assert DUP([[1],[2],[1]], ZZ).is_ground == False assert DMP([[1],[2,0],[1,0]], ZZ).is_sqf == True assert DMP([[1],[2,0],[1,0,0]], ZZ).is_sqf == False assert DMP([[1,2],[3]], ZZ).is_monic == True assert DMP([[2,2],[3]], ZZ).is_monic == False assert DMP([[1,2],[3]], ZZ).is_primitive == True assert DMP([[2,4],[6]], ZZ).is_primitive == False
def test_DMP___init__(): f = DMP([[0],[],[0,1,2],[3]], ZZ) assert f.rep == [[1,2],[3]] assert f.dom == ZZ assert f.lev == 1 f = DMP([[1,2],[3]], ZZ, 1) assert f.rep == [[1,2],[3]] assert f.dom == ZZ assert f.lev == 1 f = DMP({(1,1): 1, (0,0): 2}, ZZ, 1) assert f.rep == [[1,0],[2]] assert f.dom == ZZ assert f.lev == 1
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 spherical_bessel_fn(n, x=None, polys=False): """ Coefficients for the spherical Bessel functions. Those are only needed in the jn() function. The coefficients are calculated from: fn(0, z) = 1/z fn(1, z) = 1/z**2 fn(n-1, z) + fn(n+1, z) == (2*n+1)/z * fn(n, z) Parameters ========== n : int `n` decides the degree of polynomial x : optional polys : bool, optional ``polys=True`` returns an expression, otherwise (default) returns an expression. Examples ======== >>> from sympy.polys.orthopolys import spherical_bessel_fn as fn >>> from sympy import Symbol >>> z = Symbol("z") >>> fn(1, z) z**(-2) >>> fn(2, z) -1/z + 3/z**3 >>> fn(3, z) -6/z**2 + 15/z**4 >>> fn(4, z) 1/z - 45/z**3 + 105/z**5 """ if n < 0: dup = dup_spherical_bessel_fn_minus(-int(n), ZZ) else: dup = dup_spherical_bessel_fn(int(n), ZZ) poly = DMP(dup, ZZ) if x is not None: poly = Poly.new(poly, 1/x) else: poly = PurePoly.new(poly, 1/Dummy('x')) return poly if polys else poly.as_expr()
def hermite_poly(n, x=None, **args): """Generates Hermite polynomial of degree `n` in `x`. """ if n < 0: raise ValueError("can't generate Hermite polynomial of degree %s" % n) if x is not None: x = sympify(x) else: x = Symbol('x', dummy=True) poly = Poly(DMP(dup_hermite(int(n), ZZ), ZZ), x) if not args.get('polys', False): return poly.as_basic() else: return poly
def cyclotomic_poly(n, x=None, **args): """Generates cyclotomic polynomial of order `n` in `x`. """ if n <= 0: raise ValueError("can't generate cyclotomic polynomial of order %s" % n) if x is not None: x = sympify(x) else: x = Dummy('x') poly = Poly(DMP(dup_zz_cyclotomic_poly(int(n), ZZ), ZZ), x) if not args.get('polys', False): return poly.as_basic() else: return poly
def laguerre_poly(n, x=None, **args): """Generates Laguerre polynomial of degree `n` in `x`. """ if n < 0: raise ValueError("can't generate Laguerre polynomial of degree %s" % n) if x is not None: x = sympify(x) else: x = Dummy('x') poly = Poly.new(DMP(dup_laguerre(int(n), QQ), QQ), x) if not args.get('polys', False): return poly.as_expr() else: return poly
def chebyshevu_poly(n, x=None, **args): """Generates Chebyshev polynomial of the second kind of degree `n` in `x`. """ if n < 0: raise ValueError("can't generate 2nd kind Chebyshev polynomial of degree %s" % n) if x is not None: x = sympify(x) else: x = Dummy('x') poly = Poly(DMP(dup_chebyshevu(int(n), ZZ), ZZ), x) if not args.get('polys', False): return poly.as_basic() else: return poly
def legendre_poly(n, x=None, **args): """Generates Legendre polynomial of degree `n` in `x`. """ if n < 0: raise ValueError("can't generate Legendre polynomial of degree %s" % n) poly = DMP(dup_legendre(int(n), QQ), QQ) if x is not None: poly = Poly.new(poly, x) else: poly = PurePoly.new(poly, Dummy('x')) if not args.get('polys', False): return poly.as_expr() else: return poly
def jacobi_poly(n, a, b, x=None, **args): """Generates Jacobi polynomial of degree `n` in `x`. """ if n < 0: raise ValueError("can't generate Jacobi polynomial of degree %s" % n) K, v = construct_domain([a, b], field=True) poly = DMP(dup_jacobi(int(n), v[0], v[1], K), K) if x is not None: poly = Poly.new(poly, x) else: poly = PurePoly.new(poly, Dummy('x')) if not args.get('polys', False): return poly.as_expr() else: return poly
def cyclotomic_poly(n, x=None, **args): """Generates cyclotomic polynomial of order `n` in `x`. """ if n <= 0: raise ValueError("can't generate cyclotomic polynomial of order %s" % n) poly = DMP(dup_zz_cyclotomic_poly(int(n), ZZ), ZZ) if x is not None: poly = Poly.new(poly, x) else: poly = PurePoly.new(poly, Dummy('x')) if not args.get('polys', False): return poly.as_expr() else: return poly
def chebyshevu_poly(n, x=None, **args): """Generates Chebyshev polynomial of the second kind of degree `n` in `x`. """ if n < 0: raise ValueError( "can't generate 2nd kind Chebyshev polynomial of degree %s" % n) poly = DMP(dup_chebyshevu(int(n), ZZ), ZZ) if x is not None: poly = Poly.new(poly, x) else: poly = PurePoly.new(poly, Dummy('x')) if not args.get('polys', False): return poly.as_expr() else: return poly
def chebyshevt_poly(n, x=None, **args): """Generates Chebyshev polynomial of the first kind of degree `n` in `x`. """ if n < 0: raise ValueError( "can't generate 1st kind Chebyshev polynomial of degree %s" % n) if x is not None: x = sympify(x) else: x = Symbol('x', dummy=True) poly = Poly(DMP(dup_chebyshevt(int(n), ZZ), ZZ), x) if not args.get('polys', False): return poly.as_basic() else: return poly