def swinnerton_dyer_poly(n, x=None, **args): """Generates n-th Swinnerton-Dyer polynomial in `x`. """ from .numberfields import minimal_polynomial if n <= 0: raise ValueError( "can't generate Swinnerton-Dyer polynomial of order %s" % n) if x is not None: sympify(x) else: x = Dummy('x') if n > 3: p = 2 a = [sqrt(2)] for i in range(2, n + 1): p = nextprime(p) a.append(sqrt(p)) return minimal_polynomial(Add(*a), x, polys=args.get('polys', False)) if n == 1: ex = x**2 - 2 elif n == 2: ex = x**4 - 10 * x**2 + 1 elif n == 3: ex = x**8 - 40 * x**6 + 352 * x**4 - 960 * x**2 + 576 if not args.get('polys', False): return ex else: return PurePoly(ex, x)
def test_sympyissue_3449(): # test if powers are simplified correctly # see also issue sympy/sympy#3995 x = Symbol('x') assert ((x**Rational(1, 3))**Rational(2)) == x**Rational(2, 3) assert ( (x**Rational(3))**Rational(2, 5)) == (x**Rational(3))**Rational(2, 5) a = Symbol('a', extended_real=True) b = Symbol('b', extended_real=True) assert (a**2)**b == (abs(a)**b)**2 assert sqrt(1/a) != 1/sqrt(a) # e.g. for a = -1 assert (a**3)**Rational(1, 3) != a assert (x**a)**b != x**(a*b) # e.g. x = -1, a=2, b=1/2 assert (x**.5)**b == x**(.5*b) assert (x**.5)**.5 == x**.25 assert (x**2.5)**.5 != x**1.25 # e.g. for x = 5*I k = Symbol('k', integer=True) m = Symbol('m', integer=True) assert (x**k)**m == x**(k*m) assert Number(5)**Rational(2, 3) == Number(25)**Rational(1, 3) assert (x**.5)**2 == x**1.0 assert (x**2)**k == (x**k)**2 == x**(2*k) a = Symbol('a', positive=True) assert (a**3)**Rational(2, 5) == a**Rational(6, 5) assert (a**2)**b == (a**b)**2 assert (a**Rational(2, 3))**x == (a**(2*x/3)) != (a**x)**Rational(2, 3)
def test_sympyissue_3449(): # test if powers are simplified correctly # see also issue sympy/sympy#3995 assert cbrt(x)**2 == x**Rational(2, 3) assert (x**3)**Rational(2, 5) == Pow(x**3, Rational(2, 5), evaluate=False) a = Symbol('a', extended_real=True) b = Symbol('b', extended_real=True) assert (a**2)**b == (abs(a)**b)**2 assert sqrt(1/a) != 1/sqrt(a) # e.g. for a = -1 assert cbrt(a**3) != a assert (x**a)**b != x**(a*b) # e.g. x = -1, a=2, b=1/2 assert (x**.5)**b == x**(.5*b) assert (x**.5)**.5 == x**.25 assert (x**2.5)**.5 != x**1.25 # e.g. for x = 5*I k = Symbol('k', integer=True) m = Symbol('m', integer=True) assert (x**k)**m == x**(k*m) assert Number(5)**Rational(2, 3) == cbrt(Number(25)) assert (x**.5)**2 == x**1.0 assert (x**2)**k == (x**k)**2 == x**(2*k) a = Symbol('a', positive=True) assert (a**3)**Rational(2, 5) == a**Rational(6, 5) assert (a**2)**b == (a**b)**2 assert (a**Rational(2, 3))**x == (a**(2*x/3)) != (a**x)**Rational(2, 3)
def test_sympyissue_6990(): x = Symbol('x') a = Symbol('a') b = Symbol('b') assert (sqrt(a + b*x + x**2)).series(x, 0, 3).removeO() == \ b*x/(2*sqrt(a)) + x**2*(1/(2*sqrt(a)) - b**2/(8*a**Rational(3, 2))) + sqrt(a)
def test_root(): from diofant.abc import x n = Symbol('n', integer=True) k = Symbol('k', integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == 2**Rational(1, 3) assert root(2, 3) == cbrt(2) assert root(2, -5) == 2**Rational(4, 5) / 2 assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2) * I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == x**Rational(1, 3) assert root(x, 3) == cbrt(x) assert root(x, -5) == x**Rational(-1, 5) assert root(x, n) == x**(1 / n) assert root(x, -n) == x**(-1 / n) assert root(x, n, k) == x**(1 / n) * (-1)**(2 * k / n)
def eval(cls, arg): if arg.is_Number: if arg is S.Infinity: return S.Infinity elif arg.is_Integer: if arg.is_positive: return factorial(arg - 1) else: return S.ComplexInfinity elif arg.is_Rational: if arg.q == 2: n = abs(arg.p) // arg.q if arg.is_positive: k, coeff = n, S.One else: n = k = n + 1 if n & 1 == 0: coeff = S.One else: coeff = S.NegativeOne for i in range(3, 2 * k, 2): coeff *= i if arg.is_positive: return coeff * sqrt(S.Pi) / 2**n else: return 2**n * sqrt(S.Pi) / coeff if arg.is_integer and arg.is_nonpositive: return S.ComplexInfinity
def as_real_imag(self, deep=True, **hints): # TODO: Handle deep and hints n, m, theta, phi = self.args re = (sqrt((2*n + 1)/(4*pi) * factorial(n - m)/factorial(n + m)) * cos(m*phi) * assoc_legendre(n, m, cos(theta))) im = (sqrt((2*n + 1)/(4*pi) * factorial(n - m)/factorial(n + m)) * sin(m*phi) * assoc_legendre(n, m, cos(theta))) return re, im
def as_real_imag(self, deep=True, **hints): x, y = self._as_real_imag(deep=deep, **hints) sq = -y**2 / x**2 re = S.Half * (self.func(x + x * sqrt(sq)) + self.func(x - x * sqrt(sq))) im = x / (2 * y) * sqrt(sq) * (self.func(x - x * sqrt(sq)) - self.func(x + x * sqrt(sq))) return re, im
def eval(cls, n, m, theta, phi): n, m, th, ph = [sympify(x) for x in (n, m, theta, phi)] if m.is_positive: zz = (Ynm(n, m, th, ph) + Ynm_c(n, m, th, ph)) / sqrt(2) return zz elif m.is_zero: return Ynm(n, m, th, ph) elif m.is_negative: zz = (Ynm(n, m, th, ph) - Ynm_c(n, m, th, ph)) / (sqrt(2)*I) return zz
def _eval_rewrite_as_besseli(self, z): ot = Rational(1, 3) tt = Rational(2, 3) a = tt * Pow(z, Rational(3, 2)) if re(z).is_positive: return z / sqrt(3) * (besseli(-tt, a) + besseli(tt, a)) else: a = Pow(z, Rational(3, 2)) b = Pow(a, tt) c = Pow(a, -tt) return sqrt(ot) * (b * besseli(-tt, tt * a) + z**2 * c * besseli(tt, tt * a))
def _eval_rewrite_as_besseli(self, z): ot = Rational(1, 3) tt = Rational(2, 3) a = Pow(z, Rational(3, 2)) if re(z).is_positive: return sqrt(z) / sqrt(3) * (besseli(-ot, tt * a) + besseli(ot, tt * a)) else: b = Pow(a, ot) c = Pow(a, -ot) return sqrt(ot) * (b * besseli(-ot, tt * a) + z * c * besseli(ot, tt * a))
def test_rational(): a = Rational(1, 5) r = sqrt(5)/5 assert sqrt(a) == r assert 2*sqrt(a) == 2*r r = a*a**Rational(1, 2) assert a**Rational(3, 2) == r assert 2*a**Rational(3, 2) == 2*r r = a**5*a**Rational(2, 3) assert a**Rational(17, 3) == r assert 2 * a**Rational(17, 3) == 2*r
def test_rational(): a = Rational(1, 5) r = sqrt(5)/5 assert sqrt(a) == r assert 2*sqrt(a) == 2*r r = a*sqrt(a) assert a**Rational(3, 2) == r assert 2*a**Rational(3, 2) == 2*r r = a**5*a**Rational(2, 3) assert a**Rational(17, 3) == r assert 2 * a**Rational(17, 3) == 2*r
def test_sympyissue_6208(): assert sqrt(33**(9*I/10)) == -33**(9*I/20) assert root((6*I)**(2*I), 3).as_base_exp()[1] == Rational(1, 3) # != 2*I/3 assert root((6*I)**(I/3), 3).as_base_exp()[1] == I/9 assert sqrt(exp(3*I)) == exp(3*I/2) assert sqrt(-sqrt(3)*(1 + 2*I)) == sqrt(sqrt(3))*sqrt(-1 - 2*I) assert sqrt(exp(5*I)) == -exp(5*I/2) assert root(exp(5*I), 3).exp == Rational(1, 3)
def test_sympyissue_6208(): assert sqrt(33**(9*I/10)) == -33**(9*I/20) assert root((6*I)**(2*I), 3).as_base_exp()[1] == Rational(1, 3) # != 2*I/3 assert root((6*I)**(I/3), 3).as_base_exp()[1] == I/9 assert sqrt(exp(3*I)) == exp(3*I/2) assert sqrt(-sqrt(3)*(1 + 2*I)) == sqrt(sqrt(3))*sqrt(-1 - 2*I) assert sqrt(exp(5*I)) == -exp(5*I/2) assert root(exp(5*I), 3).exp == Rational(1, 3)
def _eval_expand_func(self, **hints): arg = self.args[0] symbs = arg.free_symbols if len(symbs) == 1: z = symbs.pop() c = Wild("c", exclude=[z]) d = Wild("d", exclude=[z]) m = Wild("m", exclude=[z]) n = Wild("n", exclude=[z]) M = arg.match(c * (d * z**n)**m) if M is not None: m = M[m] # The transformation is in principle # given by 03.08.16.0001.01 but note # that there is an error in this formule. # http://functions.wolfram.com/Bessel-TypeFunctions/AiryBiPrime/16/01/01/0001/ if (3 * m).is_integer: c = M[c] d = M[d] n = M[n] pf = (d**m * z**(n * m)) / (d * z**n)**m newarg = c * d**m * z**(n * m) return S.Half * (sqrt(3) * (pf - S.One) * airyaiprime(newarg) + (pf + S.One) * airybiprime(newarg))
def _cholesky_sparse(self): """Algorithm for numeric Cholesky factorization of a sparse matrix.""" Crowstruc = self.row_structure_symbolic_cholesky() C = self.zeros(self.rows) for i in range(len(Crowstruc)): for j in Crowstruc[i]: if i != j: C[i, j] = self[i, j] summ = 0 for p1 in Crowstruc[i]: if p1 < j: for p2 in Crowstruc[j]: if p2 < j: if p1 == p2: summ += C[i, p1] * C[j, p1] else: break else: break C[i, j] -= summ C[i, j] /= C[j, j] else: C[j, j] = self[j, j] summ = 0 for k in Crowstruc[j]: if k < j: summ += C[j, k]**2 else: break C[j, j] -= summ C[j, j] = sqrt(C[j, j]) return C
def fdiff(self, argindex=1): if len(self.args) == 3: n, z, m = self.args fm, fn = sqrt(1 - m * sin(z)**2), 1 - n * sin(z)**2 if argindex == 1: return (elliptic_e(z, m) + (m - n) * elliptic_f(z, m) / n + (n**2 - m) * elliptic_pi(n, z, m) / n - n * fm * sin(2 * z) / (2 * fn)) / (2 * (m - n) * (n - 1)) elif argindex == 2: return 1 / (fm * fn) elif argindex == 3: return (elliptic_e(z, m) / (m - 1) + elliptic_pi(n, z, m) - m * sin(2 * z) / (2 * (m - 1) * fm)) / (2 * (n - m)) else: n, m = self.args if argindex == 1: return (elliptic_e(m) + (m - n) * elliptic_k(m) / n + (n**2 - m) * elliptic_pi(n, m) / n) / (2 * (m - n) * (n - 1)) elif argindex == 2: return (elliptic_e(m) / (m - 1) + elliptic_pi(n, m)) / (2 * (n - m)) raise ArgumentIndexError(self, argindex)
def distance(self, p): """The Euclidean distance from self to point p. Parameters ========== p : Point Returns ======= distance : number or symbolic expression. See Also ======== diofant.geometry.line.Segment.length Examples ======== >>> from diofant.geometry import Point >>> p1, p2 = Point(1, 1), Point(4, 5) >>> p1.distance(p2) 5 >>> from diofant.abc import x, y >>> p3 = Point(x, y) >>> p3.distance(Point(0, 0)) sqrt(x**2 + y**2) """ p = Point(p) return sqrt(sum((a - b)**2 for a, b in zip(self.args, p.args)))
def direction_cosine(self, point): """ Gives the direction cosine between 2 points Parameters ========== p : Point3D Returns ======= list Examples ======== >>> from diofant import Point3D >>> p1 = Point3D(1, 2, 3) >>> p1.direction_cosine(Point3D(2, 3, 5)) [sqrt(6)/6, sqrt(6)/6, sqrt(6)/3] """ a = self.direction_ratio(point) b = sqrt(sum(i**2 for i in a)) return [(point.x - self.x) / b, (point.y - self.y) / b, (point.z - self.z) / b]
def _eval_rewrite_as_besseli(self, z): ot = Rational(1, 3) tt = Rational(2, 3) a = Pow(z, Rational(3, 2)) if re(z).is_positive: return ot * sqrt(z) * (besseli(-ot, tt * a) - besseli(ot, tt * a)) else: return ot * (Pow(a, ot) * besseli(-ot, tt * a) - z * Pow(a, -ot) * besseli(ot, tt * a))
def test_valued_tensor_pow(): (A, B, AB, BA, C, Lorentz, E, px, py, pz, LorentzD, mu0, mu1, mu2, ndm, n0, n1, n2, NA, NB, NC, minkowski, ba_matrix, ndm_matrix, i0, i1, i2, i3, i4) = _get_valued_base_test_variables() assert C**2 == -E**2 + px**2 + py**2 + pz**2 assert C**1 == sqrt(-E**2 + px**2 + py**2 + pz**2) assert C(mu0)**2 == C**2 assert C(mu0)**1 == C**1
def fdiff(self, argindex=1): z, m = self.args fm = sqrt(1 - m * sin(z)**2) if argindex == 1: return 1 / fm elif argindex == 2: return (elliptic_e(z, m) / (2 * m * (1 - m)) - elliptic_f(z, m) / (2 * m) - sin(2 * z) / (4 * (1 - m) * fm)) raise ArgumentIndexError(self, argindex)
def test_issue_6208(): from diofant import root, Rational I = S.ImaginaryUnit assert sqrt(33**(9 * I / 10)) == -33**(9 * I / 20) assert root((6 * I)**(2 * I), 3).as_base_exp()[1] == Rational(1, 3) # != 2*I/3 assert root((6 * I)**(I / 3), 3).as_base_exp()[1] == I / 9 assert sqrt(exp(3 * I)) == exp(3 * I / 2) assert sqrt(-sqrt(3) * (1 + 2 * I)) == sqrt(sqrt(3)) * sqrt(-1 - 2 * I) assert sqrt(exp(5 * I)) == -exp(5 * I / 2) assert root(exp(5 * I), 3).exp == Rational(1, 3)
def jacobi_normalized(n, a, b, x): r""" Jacobi polynomial :math:`P_n^{\left(\alpha, \beta\right)}(x)` jacobi_normalized(n, alpha, beta, x) gives the nth Jacobi polynomial in x, :math:`P_n^{\left(\alpha, \beta\right)}(x)`. The Jacobi polynomials are orthogonal on :math:`[-1, 1]` with respect to the weight :math:`\left(1-x\right)^\alpha \left(1+x\right)^\beta`. This functions returns the polynomials normilzed: .. math:: \int_{-1}^{1} P_m^{\left(\alpha, \beta\right)}(x) P_n^{\left(\alpha, \beta\right)}(x) (1-x)^{\alpha} (1+x)^{\beta} \mathrm{d}x = \delta_{m,n} Examples ======== >>> from diofant import jacobi_normalized >>> from diofant.abc import n,a,b,x >>> jacobi_normalized(n, a, b, x) jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1)/((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1))) See Also ======== gegenbauer, chebyshevt_root, chebyshevu, chebyshevu_root, legendre, assoc_legendre, hermite, laguerre, assoc_laguerre, diofant.polys.orthopolys.jacobi_poly, diofant.polys.orthopolys.gegenbauer_poly diofant.polys.orthopolys.chebyshevt_poly diofant.polys.orthopolys.chebyshevu_poly diofant.polys.orthopolys.hermite_poly diofant.polys.orthopolys.legendre_poly diofant.polys.orthopolys.laguerre_poly References ========== .. [1] http://en.wikipedia.org/wiki/Jacobi_polynomials .. [2] http://mathworld.wolfram.com/JacobiPolynomial.html .. [3] http://functions.wolfram.com/Polynomials/JacobiP/ """ nfactor = (Integer(2)**(a + b + 1) * (gamma(n + a + 1) * gamma(n + b + 1)) / (2*n + a + b + 1) / (factorial(n) * gamma(n + a + b + 1))) return jacobi(n, a, b, x) / sqrt(nfactor)
def eval(cls, a, x): # For lack of a better place, we use this one to extract branching # information. The following can be # found in the literature (c/f references given above), albeit scattered: # 1) For fixed x != 0, lowergamma(s, x) is an entire function of s # 2) For fixed positive integers s, lowergamma(s, x) is an entire # function of x. # 3) For fixed non-positive integers s, # lowergamma(s, exp(I*2*pi*n)*x) = # 2*pi*I*n*(-1)**(-s)/factorial(-s) + lowergamma(s, x) # (this follows from lowergamma(s, x).diff(x) = x**(s-1)*exp(-x)). # 4) For fixed non-integral s, # lowergamma(s, x) = x**s*gamma(s)*lowergamma_unbranched(s, x), # where lowergamma_unbranched(s, x) is an entire function (in fact # of both s and x), i.e. # lowergamma(s, exp(2*I*pi*n)*x) = exp(2*pi*I*n*a)*lowergamma(a, x) from diofant import unpolarify, I nx, n = x.extract_branch_factor() if a.is_integer and a.is_positive: nx = unpolarify(x) if nx != x: return lowergamma(a, nx) elif a.is_integer and a.is_nonpositive: if n != 0: return 2 * pi * I * n * (-1)**( -a) / factorial(-a) + lowergamma(a, nx) elif n != 0: return exp(2 * pi * I * n * a) * lowergamma(a, nx) # Special values. if a.is_Number: # TODO this should be non-recursive if a is S.One: return S.One - exp(-x) elif a is S.Half: return sqrt(pi) * erf(sqrt(x)) elif a.is_Integer or (2 * a).is_Integer: b = a - 1 if b.is_positive: return b * cls(b, x) - x**b * exp(-x) if not a.is_Integer: return (cls(a + 1, x) + x**a * exp(-x)) / a
def _cholesky(self): """Helper function of cholesky. Without the error checks. To be used privately. """ L = zeros(self.rows, self.rows) for i in range(self.rows): for j in range(i): L[i, j] = (1 / L[j, j]) * (self[i, j] - sum(L[i, k] * L[j, k] for k in range(j))) L[i, i] = sqrt(self[i, i] - sum(L[i, k]**2 for k in range(i))) return self._new(L)
def fdiff(self, argindex=1): if len(self.args) == 2: z, m = self.args if argindex == 1: return sqrt(1 - m * sin(z)**2) elif argindex == 2: return (elliptic_e(z, m) - elliptic_f(z, m)) / (2 * m) else: z = self.args[0] if argindex == 1: return (elliptic_e(z) - elliptic_k(z)) / (2 * z) raise ArgumentIndexError(self, argindex)
def test_real_root(): assert real_root(-8, 3) == -2 assert real_root(-16, 4) == root(-16, 4) r = root(-7, 4) assert real_root(r) == r r1 = root(-1, 3) r2 = r1**2 r3 = root(-1, 4) assert real_root(r1 + r2 + r3) == -1 + r2 + r3 assert real_root(root(-2, 3)) == -root(2, 3) assert real_root(-8., 3) == -2 x = Symbol('x') n = Symbol('n') g = real_root(x, n) assert g.subs(dict(x=-8, n=3)) == -2 assert g.subs(dict(x=8, n=3)) == 2 # give principle root if there is no real root -- if this is not desired # then maybe a Root class is needed to raise an error instead assert g.subs(dict(x=I, n=3)) == cbrt(I) assert g.subs(dict(x=-8, n=2)) == sqrt(-8) assert g.subs(dict(x=I, n=2)) == sqrt(I)
def eval(cls, z): if z is S.Zero: return pi / 2 elif z is S.Half: return 8 * pi**Rational(3, 2) / gamma(-Rational(1, 4))**2 elif z is S.One: return S.ComplexInfinity elif z is S.NegativeOne: return gamma(Rational(1, 4))**2 / (4 * sqrt(2 * pi)) elif z in (S.Infinity, S.NegativeInfinity, I * S.Infinity, I * S.NegativeInfinity, S.ComplexInfinity): return S.Zero
def fdiff(self, argindex=4): if argindex == 3: # Diff wrt theta n, m, theta, phi = self.args return (m * cot(theta) * Ynm(n, m, theta, phi) + sqrt((n - m)*(n + m + 1)) * exp(-I*phi) * Ynm(n, m + 1, theta, phi)) elif argindex == 4: # Diff wrt phi n, m, theta, phi = self.args return I * m * Ynm(n, m, theta, phi) else: # diff wrt n, m, etc raise ArgumentIndexError(self, argindex)
def eval(cls, a, z): from diofant import unpolarify, I, expint if z.is_Number: if z is S.Infinity: return S.Zero elif z is S.Zero: # TODO: Holds only for Re(a) > 0: return gamma(a) # We extract branching information here. C/f lowergamma. nx, n = z.extract_branch_factor() if a.is_integer and (a > 0) is S.true: nx = unpolarify(z) if z != nx: return uppergamma(a, nx) elif a.is_integer and (a <= 0) is S.true: if n != 0: return -2 * pi * I * n * (-1)**( -a) / factorial(-a) + uppergamma(a, nx) elif n != 0: return gamma(a) * (1 - exp(2 * pi * I * n * a)) + exp( 2 * pi * I * n * a) * uppergamma(a, nx) # Special values. if a.is_Number: # TODO this should be non-recursive if a is S.One: return exp(-z) elif a is S.Half: return sqrt(pi) * (1 - erf(sqrt(z))) # TODO could use erfc... elif a.is_Integer or (2 * a).is_Integer: b = a - 1 if b.is_positive: return b * cls(b, z) + z**b * exp(-z) elif b.is_Integer: return expint(-b, z) * unpolarify(z)**(b + 1) if not a.is_Integer: return (cls(a + 1, z) - z**a * exp(-z)) / a
def eval(cls, n, m, z=None): if z is not None: n, z, m = n, m, z k = 2 * z / pi if n == S.Zero: return elliptic_f(z, m) elif n == S.One: return (elliptic_f(z, m) + (sqrt(1 - m * sin(z)**2) * tan(z) - elliptic_e(z, m)) / (1 - m)) elif k.is_integer: return k * elliptic_pi(n, m) elif m == S.Zero: return atanh(sqrt(n - 1) * tan(z)) / sqrt(n - 1) elif n == m: return (elliptic_f(z, n) - elliptic_pi(1, z, n) + tan(z) / sqrt(1 - n * sin(z)**2)) elif n in (S.Infinity, S.NegativeInfinity): return S.Zero elif m in (S.Infinity, S.NegativeInfinity): return S.Zero elif z.could_extract_minus_sign(): return -elliptic_pi(n, -z, m) else: if n == S.Zero: return elliptic_k(m) elif n == S.One: return S.ComplexInfinity elif m == S.Zero: return pi / (2 * sqrt(1 - n)) elif m == S.One: return -S.Infinity / sign(n - 1) elif n == m: return elliptic_e(n) / (1 - n) elif n in (S.Infinity, S.NegativeInfinity): return S.Zero elif m in (S.Infinity, S.NegativeInfinity): return S.Zero
def test_sympyissue_6068(): assert sqrt(sin(x)).series(x, 0, 8) == \ sqrt(x) - x**Rational(5, 2)/12 + x**Rational(9, 2)/1440 - \ x**Rational(13, 2)/24192 + O(x**8) assert sqrt(sin(x)).series(x, 0, 10) == \ sqrt(x) - x**Rational(5, 2)/12 + x**Rational(9, 2)/1440 - \ x**Rational(13, 2)/24192 - 67*x**Rational(17, 2)/29030400 + O(x**10) assert sqrt(sin(x**3)).series(x, 0, 19) == \ x**Rational(3, 2) - x**Rational(15, 2)/12 + x**Rational(27, 2)/1440 + O(x**19) assert sqrt(sin(x**3)).series(x, 0, 20) == \ x**Rational(3, 2) - x**Rational(15, 2)/12 + x**Rational(27, 2)/1440 - \ x**Rational(39, 2)/24192 + O(x**20)
def test_sympyissue_3866(): assert --sqrt(sqrt(5) - 1) == sqrt(sqrt(5) - 1)
def test_sympyissue_6990(): assert (sqrt(a + b*x + x**2)).series(x, 0, 3).removeO() == \ b*x/(2*sqrt(a)) + x**2*(1/(2*sqrt(a)) - b**2/(8*a**Rational(3, 2))) + sqrt(a)
def test_sympyissue_6782(): assert sqrt(sin(x**3)).series(x, 0, 7) == x**Rational(3, 2) + O(x**7) assert sqrt(sin(x**4)).series(x, 0, 3) == x**2 + O(x**3)
def test_sympyissue_7638(): f = pi/log(sqrt(2)) assert ((1 + I)**(I*f/2))**0.3 == (1 + I)**(0.15*I*f) # if 1/3 -> 1.0/3 this should fail since it cannot be shown that the # sign will be +/-1; for the previous "small arg" case, it didn't matter # that this could not be proved assert (1 + I)**(4*I*f) == cbrt((1 + I)**(12*I*f)) assert cbrt((1 + I)**(I*(1 + 7*f))).exp == Rational(1, 3) r = symbols('r', extended_real=True) assert sqrt(r**2) == abs(r) assert cbrt(r**3) != r assert sqrt(Pow(2*I, Rational(5, 2))) != (2*I)**Rational(5, 4) p = symbols('p', positive=True) assert cbrt(p**2) == p**Rational(2, 3) assert NS(((0.2 + 0.7*I)**(0.7 + 1.0*I))**(0.5 - 0.1*I), 1) == '0.4 + 0.2*I' assert sqrt(1/(1 + I)) == sqrt((1 - I)/2) # or 1/sqrt(1 + I) e = 1/(1 - sqrt(2)) assert sqrt(e) == I/sqrt(-1 + sqrt(2)) assert e**Rational(-1, 2) == -I*sqrt(-1 + sqrt(2)) assert sqrt((cos(1)**2 + sin(1)**2 - 1)**(3 + I)).exp == Rational(1, 2) assert sqrt(r**Rational(4, 3)) != r**Rational(2, 3) assert sqrt((p + I)**Rational(4, 3)) == (p + I)**Rational(2, 3) assert sqrt((p - p**2*I)**2) == p - p**2*I assert sqrt((p + r*I)**2) != p + r*I e = (1 + I/5) assert sqrt(e**5) == e**Rational(5, 2) assert sqrt(e**6) == e**3 assert sqrt((1 + I*r)**6) != (1 + I*r)**3
def test_sympyissue_6653(): assert (1 / sqrt(1 + sin(x**2))).series(x, 0, 3) == 1 - x**2/2 + O(x**3)
def test_dispersion(): pytest.raises(ValueError, lambda: dispersionset(poly(x*y, x, y), poly(x, x))) pytest.raises(ValueError, lambda: dispersionset(poly(x, x), poly(y, y))) fp = poly(0, x) assert sorted(dispersionset(fp)) == [0] fp = poly(2, x) assert sorted(dispersionset(fp)) == [0] fp = poly(x + 1, x) assert sorted(dispersionset(fp)) == [0] assert dispersion(fp) == 0 fp = poly((x + 1)*(x + 2), x) assert sorted(dispersionset(fp)) == [0, 1] assert dispersion(fp) == 1 fp = poly(x*(x + 3), x) assert sorted(dispersionset(fp)) == [0, 3] assert dispersion(fp) == 3 fp = poly((x - 3)*(x + 3), x) assert sorted(dispersionset(fp)) == [0, 6] assert dispersion(fp) == 6 fp = poly(x**4 - 3*x**2 + 1, x) gp = fp.shift(-3) assert sorted(dispersionset(fp, gp)) == [2, 3, 4] assert dispersion(fp, gp) == 4 assert sorted(dispersionset(gp, fp)) == [] assert dispersion(gp, fp) == -oo fp = poly(x**2 + 2*x - 1, x) gp = poly(x**2 + 2*x + 3, x) assert dispersionset(fp, gp) == set() fp = poly(x*(3*x**2+a)*(x-2536)*(x**3+a), x) gp = fp.as_expr().subs({x: x - 345}).as_poly(x) assert sorted(dispersionset(fp, gp)) == [345, 2881] assert sorted(dispersionset(gp, fp)) == [2191] gp = poly((x-2)**2*(x-3)**3*(x-5)**3, x) assert sorted(dispersionset(gp)) == [0, 1, 2, 3] assert sorted(dispersionset(gp, (gp+4)**2)) == [1, 2] fp = poly(x*(x+2)*(x-1), x) assert sorted(dispersionset(fp)) == [0, 1, 2, 3] fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>') gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>') assert sorted(dispersionset(fp, gp)) == [2] assert sorted(dispersionset(gp, fp)) == [1, 4] # There are some difficulties if we compute over Z[a] # and alpha happenes to lie in Z[a] instead of simply Z. # Hence we can not decide if alpha is indeed integral # in general. fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x) assert sorted(dispersionset(fp)) == [0, 1] # For any specific value of a, the dispersion is 3*a # but the algorithm can not find this in general. # This is the point where the resultant based Ansatz # is superior to the current one. fp = poly(a**2*x**3 + (a**3 + a**2 + a + 1)*x, x) gp = fp.as_expr().subs({x: x - 3*a}).as_poly(x) assert sorted(dispersionset(fp, gp)) == [] fpa = fp.as_expr().subs({a: 2}).as_poly(x) gpa = gp.as_expr().subs({a: 2}).as_poly(x) assert sorted(dispersionset(fpa, gpa)) == [6] # Work with Expr instead of Poly f = (x + 1)*(x + 2) assert sorted(dispersionset(f)) == [0, 1] assert dispersion(f) == 1 f = x**4 - 3*x**2 + 1 g = x**4 - 12*x**3 + 51*x**2 - 90*x + 55 assert sorted(dispersionset(f, g)) == [2, 3, 4] assert dispersion(f, g) == 4 # Work with Expr and specify a generator f = (x + 1)*(x + 2) assert sorted(dispersionset(f, None, x)) == [0, 1] assert dispersion(f, None, x) == 1 f = x**4 - 3*x**2 + 1 g = x**4 - 12*x**3 + 51*x**2 - 90*x + 55 assert sorted(dispersionset(f, g, x)) == [2, 3, 4] assert dispersion(f, g, x) == 4
def test_sympyissue_4362(): neg = Symbol('neg', negative=True) nonneg = Symbol('nonneg', nonnegative=True) any = Symbol('any') num, den = sqrt(1/neg).as_numer_denom() assert num == sqrt(-1) assert den == sqrt(-neg) num, den = sqrt(1/nonneg).as_numer_denom() assert num == 1 assert den == sqrt(nonneg) num, den = sqrt(1/any).as_numer_denom() assert num == sqrt(1/any) assert den == 1 def eqn(num, den, pow): return (num/den)**pow npos = 1 nneg = -1 dpos = 2 - sqrt(3) dneg = 1 - sqrt(3) assert dpos > 0 and dneg < 0 and npos > 0 and nneg < 0 # pos or neg integer eq = eqn(npos, dpos, 2) assert eq.is_Pow and eq.as_numer_denom() == (1, dpos**2) eq = eqn(npos, dneg, 2) assert eq.is_Pow and eq.as_numer_denom() == (1, dneg**2) eq = eqn(nneg, dpos, 2) assert eq.is_Pow and eq.as_numer_denom() == (1, dpos**2) eq = eqn(nneg, dneg, 2) assert eq.is_Pow and eq.as_numer_denom() == (1, dneg**2) eq = eqn(npos, dpos, -2) assert eq.is_Pow and eq.as_numer_denom() == (dpos**2, 1) eq = eqn(npos, dneg, -2) assert eq.is_Pow and eq.as_numer_denom() == (dneg**2, 1) eq = eqn(nneg, dpos, -2) assert eq.is_Pow and eq.as_numer_denom() == (dpos**2, 1) eq = eqn(nneg, dneg, -2) assert eq.is_Pow and eq.as_numer_denom() == (dneg**2, 1) # pos or neg rational pow = Rational(1, 2) eq = eqn(npos, dpos, pow) assert eq.is_Pow and eq.as_numer_denom() == (npos**pow, dpos**pow) eq = eqn(npos, dneg, pow) assert eq.is_Pow is False and eq.as_numer_denom() == ((-npos)**pow, (-dneg)**pow) eq = eqn(nneg, dpos, pow) assert not eq.is_Pow or eq.as_numer_denom() == (nneg**pow, dpos**pow) eq = eqn(nneg, dneg, pow) assert eq.is_Pow and eq.as_numer_denom() == ((-nneg)**pow, (-dneg)**pow) eq = eqn(npos, dpos, -pow) assert eq.is_Pow and eq.as_numer_denom() == (dpos**pow, npos**pow) eq = eqn(npos, dneg, -pow) assert eq.is_Pow is False and eq.as_numer_denom() == (-(-npos)**pow*(-dneg)**pow, npos) eq = eqn(nneg, dpos, -pow) assert not eq.is_Pow or eq.as_numer_denom() == (dpos**pow, nneg**pow) eq = eqn(nneg, dneg, -pow) assert eq.is_Pow and eq.as_numer_denom() == ((-dneg)**pow, (-nneg)**pow) # unknown exponent pow = 2*any eq = eqn(npos, dpos, pow) assert eq.is_Pow and eq.as_numer_denom() == (npos**pow, dpos**pow) eq = eqn(npos, dneg, pow) assert eq.is_Pow and eq.as_numer_denom() == ((-npos)**pow, (-dneg)**pow) eq = eqn(nneg, dpos, pow) assert eq.is_Pow and eq.as_numer_denom() == (nneg**pow, dpos**pow) eq = eqn(nneg, dneg, pow) assert eq.is_Pow and eq.as_numer_denom() == ((-nneg)**pow, (-dneg)**pow) eq = eqn(npos, dpos, -pow) assert eq.as_numer_denom() == (dpos**pow, npos**pow) eq = eqn(npos, dneg, -pow) assert eq.is_Pow and eq.as_numer_denom() == ((-dneg)**pow, (-npos)**pow) eq = eqn(nneg, dpos, -pow) assert eq.is_Pow and eq.as_numer_denom() == (dpos**pow, nneg**pow) eq = eqn(nneg, dneg, -pow) assert eq.is_Pow and eq.as_numer_denom() == ((-dneg)**pow, (-nneg)**pow) assert ((1/(1 + x/3))**-1).as_numer_denom() == (3 + x, 3) notp = Symbol('notp', positive=False) # not positive does not imply real b = ((1 + x/notp)**-2) assert (b**(-y)).as_numer_denom() == (1, b**y) assert (b**-1).as_numer_denom() == ((notp + x)**2, notp**2) nonp = Symbol('nonp', nonpositive=True) assert (((1 + x/nonp)**-2)**-1).as_numer_denom() == ((-nonp - x)**2, nonp**2) n = Symbol('n', negative=True) assert (x**n).as_numer_denom() == (1, x**-n) assert sqrt(1/n).as_numer_denom() == (I, sqrt(-n)) n = Symbol('0 or neg', nonpositive=True) # if x and n are split up without negating each term and n is negative # then the answer might be wrong; if n is 0 it won't matter since # 1/oo and 1/zoo are both zero as is sqrt(0)/sqrt(-x) unless x is also # zero (in which case the negative sign doesn't matter): # 1/sqrt(1/-1) = -I but sqrt(-1)/sqrt(1) = I assert (1/sqrt(x/n)).as_numer_denom() == (sqrt(-n), sqrt(-x)) c = Symbol('c', complex=True) e = sqrt(1/c) assert e.as_numer_denom() == (e, 1) i = Symbol('i', integer=True) assert (((1 + x/y)**i)).as_numer_denom() == ((x + y)**i, y**i)