示例#1
0
    def intersection(self, o):
        if isinstance(o, Circle):
            dx,dy = o.center - self.center
            d = sqrt( simplify(dy**2 + dx**2) )
            a = simplify((self.radius**2 - o.radius**2 + d**2) / (2*d))

            x2 = self.center[0] + (dx * a/d)
            y2 = self.center[1] + (dy * a/d)

            h = sqrt( simplify(self.radius**2 - a**2) )
            rx = -dy * (h/d)
            ry =  dx * (h/d)

            xi_1 = simplify(x2 + rx)
            xi_2 = simplify(x2 - rx)
            yi_1 = simplify(y2 + ry)
            yi_2 = simplify(y2 - ry)

            ret = [Point(xi_1, yi_1)]
            if xi_1 != xi_2 or yi_1 != yi_2:
                ret.append(Point(xi_2, yi_2))
            return ret
        elif isinstance(o, Ellipse):
            a, b, r = o.hradius, o.vradius, self.radius
            x = a*sqrt(simplify((r**2 - b**2)/(a**2 - b**2)))
            y = b*sqrt(simplify((a**2 - r**2)/(a**2 - b**2)))
            return list(set([Point(x,y), Point(x,-y), Point(-x,y), Point(-x,-y)]))

        return Ellipse.intersection(self, o)
示例#2
0
    def __new__(
        cls, center=None, hradius=None, vradius=None, eccentricity=None,
            **kwargs):
        hradius = sympify(hradius)
        vradius = sympify(vradius)

        eccentricity = sympify(eccentricity)

        if center is None:
            center = Point(0, 0)
        else:
            center = Point(center, dim=2)

        if len(center) != 2:
            raise ValueError('The center of "{0}" must be a two dimensional point'.format(cls))

        if len(list(filter(None, (hradius, vradius, eccentricity)))) != 2:
            raise ValueError('Exactly two arguments of "hradius", '
                '"vradius", and "eccentricity" must not be None."')

        if eccentricity is not None:
            if hradius is None:
                hradius = vradius / sqrt(1 - eccentricity**2)
            elif vradius is None:
                vradius = hradius * sqrt(1 - eccentricity**2)

        if hradius == vradius:
            return Circle(center, hradius, **kwargs)

        return GeometryEntity.__new__(cls, center, hradius, vradius, **kwargs)
示例#3
0
def swinnerton_dyer_poly(n, x=None, **args):
    """Generates n-th Swinnerton-Dyer polynomial in `x`.  """
    if n <= 0:
        raise ValueError(
            "can't generate Swinnerton-Dyer polynomial of order %s" % n)

    if x is not None:
        x, cls = sympify(x), Poly
    else:
        x, cls = Dummy('x'), PurePoly

    p, elts = 2, [[x, -sqrt(2)],
                  [x, sqrt(2)]]

    for i in xrange(2, n + 1):
        p, _elts = nextprime(p), []

        neg_sqrt = -sqrt(p)
        pos_sqrt = +sqrt(p)

        for elt in elts:
            _elts.append(elt + [neg_sqrt])
            _elts.append(elt + [pos_sqrt])

        elts = _elts

    poly = []

    for elt in elts:
        poly.append(Add(*elt))

    if not args.get('polys', False):
        return Mul(*poly).expand()
    else:
        return PurePoly(Mul(*poly), x)
示例#4
0
文件: dense.py 项目: bjodah/sympy
 def _cholesky(self, hermitian=True):
     """Helper function of cholesky.
     Without the error checks.
     To be used privately.
     Implements the Cholesky-Banachiewicz algorithm.
     Returns L such that L*L.H == self if hermitian flag is True,
     or L*L.T == self if hermitian is False.
     """
     L = zeros(self.rows, self.rows)
     if hermitian:
         for i in range(self.rows):
             for j in range(i):
                 L[i, j] = (1 / L[j, j])*expand_mul(self[i, j] -
                     sum(L[i, k]*L[j, k].conjugate() for k in range(j)))
             Lii2 = expand_mul(self[i, i] -
                 sum(L[i, k]*L[i, k].conjugate() for k in range(i)))
             if Lii2.is_positive is False:
                 raise ValueError("Matrix must be positive-definite")
             L[i, i] = sqrt(Lii2)
     else:
         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 test_root():
    from sympy.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)
示例#6
0
def test_issue_3891():
    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 ** (S(3) / 2))
    ) + sqrt(a)
示例#7
0
def test_power_rewrite_exp():
    assert (I**I).rewrite(exp) == exp(-pi/2)

    expr = (2 + 3*I)**(4 + 5*I)
    assert expr.rewrite(exp) == exp((4 + 5*I)*(log(sqrt(13)) + I*atan(S(3)/2)))
    assert expr.rewrite(exp).expand() == \
        169*exp(5*I*log(13)/2)*exp(4*I*atan(S(3)/2))*exp(-5*atan(S(3)/2))

    assert ((6 + 7*I)**5).rewrite(exp) == 7225*sqrt(85)*exp(5*I*atan(S(7)/6))

    expr = 5**(6 + 7*I)
    assert expr.rewrite(exp) == exp((6 + 7*I)*log(5))
    assert expr.rewrite(exp).expand() == 15625*exp(7*I*log(5))

    assert Pow(123, 789, evaluate=False).rewrite(exp) == 123**789
    assert (1**I).rewrite(exp) == 1**I
    assert (0**I).rewrite(exp) == 0**I

    expr = (-2)**(2 + 5*I)
    assert expr.rewrite(exp) == exp((2 + 5*I)*(log(2) + I*pi))
    assert expr.rewrite(exp).expand() == 4*exp(-5*pi)*exp(5*I*log(2))

    assert ((-2)**S(-5)).rewrite(exp) == (-2)**S(-5)

    x, y = symbols('x y')
    assert (x**y).rewrite(exp) == exp(y*log(x))
    assert (7**x).rewrite(exp) == exp(x*log(7), evaluate=False)
    assert ((2 + 3*I)**x).rewrite(exp) == exp(x*(log(sqrt(13)) + I*atan(S(3)/2)))
    assert (y**(5 + 6*I)).rewrite(exp) == exp(log(y)*(5 + 6*I))

    assert all((1/func(x)).rewrite(exp) == 1/(func(x).rewrite(exp)) for func in
                    (sin, cos, tan, sec, csc, sinh, cosh, tanh))
示例#8
0
    def eval(cls, arg):
        arg = sympify(arg)

        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Infinity
            elif arg is S.NegativeInfinity:
                return S.Infinity
            elif arg is S.Zero:
                return S.One
            elif arg.is_negative:
                return cls(-arg)
        else:
            i_coeff = arg.as_coefficient(S.ImaginaryUnit)

            if i_coeff is not None:
                return C.cos(i_coeff)
            else:
                coeff, terms = arg.as_coeff_terms()

                if coeff.is_negative:
                    return cls(-arg)

            if isinstance(arg, asinh):
                return sqrt(1+arg.args[0]**2)

            if isinstance(arg, acosh):
                return arg.args[0]

            if isinstance(arg, atanh):
                return 1/sqrt(1-arg.args[0]**2)
 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
示例#10
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Infinity
            elif arg.is_Integer:
                if arg.is_positive:
                    return C.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
示例#11
0
    def eval(cls, arg):
        arg = sympify(arg)

        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.One
            elif arg is S.NegativeInfinity:
                return S.NegativeOne
            elif arg is S.Zero:
                return S.Zero
            elif arg.is_negative:
                return -cls(-arg)
        else:
            i_coeff = arg.as_coefficient(S.ImaginaryUnit)

            if i_coeff is not None:
                return S.ImaginaryUnit * C.tan(i_coeff)
            else:
                coeff, terms = arg.as_coeff_terms()

                if coeff.is_negative:
                    return -cls(-arg)

            if isinstance(arg, asinh):
                x = arg.args[0]
                return x/sqrt(1+x**2)

            if isinstance(arg, acosh):
                x = arg.args[0]
                return sqrt(x-1) * sqrt(x+1) / x

            if isinstance(arg, atanh):
                return arg.args[0]
示例#12
0
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 xrange(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)
示例#13
0
def test_issue_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**(S(3)/2))) + sqrt(a)
示例#14
0
文件: ellipse.py 项目: pyc111/sympy
    def intersection(self, o):
        if isinstance(o, Circle):
            if o.center == self.center:
                if o.radius == self.radius:
                    return o
                return []
            dx,dy = o.center - self.center
            d = sqrt( simplify(dy**2 + dx**2) )
            R = o.radius + self.radius
            if d > R or d < abs(self.radius - o.radius):
                return []

            a = simplify((self.radius**2 - o.radius**2 + d**2) / (2*d))

            x2 = self.center[0] + (dx * a/d)
            y2 = self.center[1] + (dy * a/d)

            h = sqrt( simplify(self.radius**2 - a**2) )
            rx = -dy * (h/d)
            ry =  dx * (h/d)

            xi_1 = simplify(x2 + rx)
            xi_2 = simplify(x2 - rx)
            yi_1 = simplify(y2 + ry)
            yi_2 = simplify(y2 - ry)

            ret = [Point(xi_1, yi_1)]
            if xi_1 != xi_2 or yi_1 != yi_2:
                ret.append(Point(xi_2, yi_2))
            return ret

        return Ellipse.intersection(self, o)
示例#15
0
def test_issue350():
    #test if powers are simplified correctly
    #see also issue 896
    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', real=True)
    b = Symbol('b', 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)
示例#16
0
    def eval(cls, arg):
        arg = sympify(arg)

        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Infinity
            elif arg is S.NegativeInfinity:
                return S.NegativeInfinity
            elif arg is S.Zero:
                return S.Zero
            elif arg.is_negative:
                return -cls(-arg)
        else:
            i_coeff = arg.as_coefficient(S.ImaginaryUnit)

            if i_coeff is not None:
                return S.ImaginaryUnit * C.sin(i_coeff)
            else:
                coeff, terms = arg.as_coeff_terms()

                if coeff.is_negative:
                    return -cls(-arg)

            if arg.func == asinh:
                return arg.args[0]

            if arg.func == acosh:
                x = arg.args[0]
                return sqrt(x - 1) * sqrt(x + 1)

            if arg.func == atanh:
                x = arg.args[0]
                return x / sqrt(1 - x ** 2)
示例#17
0
    def eval(cls, arg):
        from sympy import asin
        arg = sympify(arg)

        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Infinity
            elif arg is S.NegativeInfinity:
                return S.NegativeInfinity
            elif arg is S.Zero:
                return S.Zero
            elif arg is S.One:
                return log(sqrt(2) + 1)
            elif arg is S.NegativeOne:
                return log(sqrt(2) - 1)
            elif arg.is_negative:
                return -cls(-arg)
        else:
            if arg is S.ComplexInfinity:
                return S.ComplexInfinity

            i_coeff = arg.as_coefficient(S.ImaginaryUnit)

            if i_coeff is not None:
                return S.ImaginaryUnit * asin(i_coeff)
            else:
                if _coeff_isneg(arg):
                    return -cls(-arg)
示例#18
0
文件: hyperbolic.py 项目: Aang/sympy
    def eval(cls, arg):
        arg = sympify(arg)

        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Infinity
            elif arg is S.NegativeInfinity:
                return S.Infinity
            elif arg is S.Zero:
                return S.One
            elif arg.is_negative:
                return cls(-arg)
        else:
            i_coeff = arg.as_coefficient(S.ImaginaryUnit)

            if i_coeff is not None:
                return C.cos(i_coeff)
            else:
                if arg.as_coeff_mul()[0].is_negative:
                    return cls(-arg)

            if arg.func == asinh:
                return sqrt(1+arg.args[0]**2)

            if arg.func == acosh:
                return arg.args[0]

            if arg.func == atanh:
                return 1/sqrt(1-arg.args[0]**2)

            if arg.func == acoth:
                x = arg.args[0]
                return x/(sqrt(x-1) * sqrt(x+1))
示例#19
0
    def __new__(cls, center=None, hradius=None, vradius=None, eccentricity=None,
                **kwargs):
        hradius = sympify(hradius)
        vradius = sympify(vradius)
        eccentricity = sympify(eccentricity)

        if len(filter(None, (hradius, vradius, eccentricity))) != 2:
            raise ValueError, 'Exactly two arguments between "hradius", '\
                '"vradius", and "eccentricity" must be not None."'

        if eccentricity is not None:
            if hradius is None:
                hradius = vradius / sqrt(1 - eccentricity**2)
            elif vradius is None:
                vradius = hradius * sqrt(1 - eccentricity**2)
        else:
            if hradius is None and vradius is None:
                raise ValueError("At least two arguments between hradius, "
                    "vradius and eccentricity must not be none.")

        if center is None:
            center = Point(0, 0)

        if not isinstance(center, Point):
            raise TypeError("center must be a Point")

        if hradius == vradius:
            return Circle(center, hradius, **kwargs)
        return GeometryEntity.__new__(cls, center, hradius, vradius, **kwargs)
示例#20
0
def test_round():
    from sympy.abc import x

    assert round(Float('0.1249999'), 2) == 0.12
    d20 = 12345678901234567890
    ans = round(S(d20), 2)
    assert ans.is_Float and ans == d20
    ans = round(S(d20), -2)
    assert ans.is_Float and ans == 12345678901234567900
    assert round(S('1/7'), 4) == 0.1429
    assert round(S('.[12345]'), 4) == 0.1235
    assert round(S('.1349'), 2) == 0.13
    n = S(12345)
    ans = round(n)
    assert ans.is_Float
    assert ans == n
    ans = round(n, 1)
    assert ans.is_Float
    assert ans == n
    ans = round(n, 4)
    assert ans.is_Float
    assert ans == n
    assert round(n, -1) == 12350

    r = round(n, -4)
    assert r == 10000
    # in fact, it should equal many values since __eq__
    # compares at equal precision
    assert all(r == i for i in range(9984, 10049))

    assert round(n, -5) == 0

    assert round(pi + sqrt(2), 2) == 4.56
    assert round(10*(pi + sqrt(2)), -1) == 50
    raises(TypeError, 'round(x + 2, 2)')
    assert round(S(2.3), 1) == 2.3
    e = round(12.345, 2)
    assert e == _pyround(12.345, 2)
    assert type(e) is float

    assert round(Float(.3, 3) + 2*pi) == 7
    assert round(Float(.3, 3) + 2*pi*100) == 629
    assert round(Float(.03, 3) + 2*pi/100, 5) == 0.09283
    assert round(Float(.03, 3) + 2*pi/100, 4) == 0.0928

    assert round(S.Zero) == 0

    a = (Add(1, Float('1.'+'9'*27, ''), evaluate=0))
    assert round(a, 10) == Float('3.0000000000','')
    assert round(a, 25) == Float('3.0000000000000000000000000','')
    assert round(a, 26) == Float('3.00000000000000000000000000','')
    assert round(a, 27) == Float('2.999999999999999999999999999','')
    assert round(a, 30) == Float('2.999999999999999999999999999','')

    raises(TypeError, 'round(x)')
    raises(TypeError, 'round(1 + 3*I)')

    # exact magnitude of 10
    assert str(round(S(1))) == '1.'
    assert str(round(S(100))) == '100.'
示例#21
0
def get_math_macros():
    """ Returns a dictionary with math-related macros from math.h/cmath

    Note that these macros are not strictly required by the C/C++-standard.
    For MSVC they are enabled by defining "_USE_MATH_DEFINES" (preferably
    via a compilation flag).

    Returns
    =======

    Dictionary mapping sympy expressions to strings (macro names)

    """
    from sympy.codegen.cfunctions import log2, Sqrt
    from sympy.functions.elementary.exponential import log
    from sympy.functions.elementary.miscellaneous import sqrt

    return {
        S.Exp1: 'M_E',
        log2(S.Exp1): 'M_LOG2E',
        1/log(2): 'M_LOG2E',
        log(2): 'M_LN2',
        log(10): 'M_LN10',
        S.Pi: 'M_PI',
        S.Pi/2: 'M_PI_2',
        S.Pi/4: 'M_PI_4',
        1/S.Pi: 'M_1_PI',
        2/S.Pi: 'M_2_PI',
        2/sqrt(S.Pi): 'M_2_SQRTPI',
        2/Sqrt(S.Pi): 'M_2_SQRTPI',
        sqrt(2): 'M_SQRT2',
        Sqrt(2): 'M_SQRT2',
        1/sqrt(2): 'M_SQRT1_2',
        1/Sqrt(2): 'M_SQRT1_2'
    }
示例#22
0
    def eval(cls, n, z):
        n, z = list(map(sympify, (n, z)))
        from sympy import unpolarify

        if n.is_integer:
            if n.is_nonnegative:
                nz = unpolarify(z)
                if z != nz:
                    return polygamma(n, nz)

            if n == -1:
                return loggamma(z)
            else:
                if z.is_Number:
                    if z is S.NaN:
                        return S.NaN
                    elif z is S.Infinity:
                        if n.is_Number:
                            if n is S.Zero:
                                return S.Infinity
                            else:
                                return S.Zero
                    elif z.is_Integer:
                        if z.is_nonpositive:
                            return S.ComplexInfinity
                        else:
                            if n is S.Zero:
                                return -S.EulerGamma + C.harmonic(z - 1, 1)
                            elif n.is_odd:
                                return (-1) ** (n + 1) * C.factorial(n) * zeta(n + 1, z)

        if n == 0:
            if z is S.NaN:
                return S.NaN
            elif z.is_Rational:
                # TODO actually *any* n/m can be done, but that is messy
                lookup = {
                    S(1) / 2: -2 * log(2) - S.EulerGamma,
                    S(1) / 3: -S.Pi / 2 / sqrt(3) - 3 * log(3) / 2 - S.EulerGamma,
                    S(1) / 4: -S.Pi / 2 - 3 * log(2) - S.EulerGamma,
                    S(3) / 4: -3 * log(2) - S.EulerGamma + S.Pi / 2,
                    S(2) / 3: -3 * log(3) / 2 + S.Pi / 2 / sqrt(3) - S.EulerGamma,
                }
                if z > 0:
                    n = floor(z)
                    z0 = z - n
                    if z0 in lookup:
                        return lookup[z0] + Add(*[1 / (z0 + k) for k in range(n)])
                elif z < 0:
                    n = floor(1 - z)
                    z0 = z + n
                    if z0 in lookup:
                        return lookup[z0] - Add(*[1 / (z0 - 1 - k) for k in range(n)])
            elif z in (S.Infinity, S.NegativeInfinity):
                return S.Infinity
            else:
                t = z.extract_multiplicatively(S.ImaginaryUnit)
                if t in (S.Infinity, S.NegativeInfinity):
                    return S.Infinity
示例#23
0
 def eval(cls, arg):
     from sympy.simplify.simplify import signsimp
     if hasattr(arg, '_eval_Abs'):
         obj = arg._eval_Abs()
         if obj is not None:
             return obj
     # handle what we can
     arg = signsimp(arg, evaluate=False)
     if arg.is_Mul:
         known = []
         unk = []
         for t in arg.args:
             tnew = cls(t)
             if tnew.func is cls:
                 unk.append(tnew.args[0])
             else:
                 known.append(tnew)
         known = Mul(*known)
         unk = cls(Mul(*unk), evaluate=False) if unk else S.One
         return known*unk
     if arg is S.NaN:
         return S.NaN
     if arg.is_Pow:
         base, exponent = arg.as_base_exp()
         if base.is_real:
             if exponent.is_integer:
                 if exponent.is_even:
                     return arg
                 if base is S.NegativeOne:
                     return S.One
                 if base.func is cls and exponent is S.NegativeOne:
                     return arg
                 return Abs(base)**exponent
             if base.is_positive == True:
                 return base**re(exponent)
             return (-base)**re(exponent)*C.exp(-S.Pi*im(exponent))
     if isinstance(arg, C.exp):
         return C.exp(re(arg.args[0]))
     if arg.is_zero:  # it may be an Expr that is zero
         return S.Zero
     if arg.is_nonnegative:
         return arg
     if arg.is_nonpositive:
         return -arg
     if arg.is_imaginary:
         arg2 = -S.ImaginaryUnit * arg
         if arg2.is_nonnegative:
             return arg2
     if arg.is_Add:
         if arg.has(S.Infinity, S.NegativeInfinity):
             if any(a.is_infinite for a in arg.as_real_imag()):
                 return S.Infinity
         if arg.is_real is None and arg.is_imaginary is None:
             if all(a.is_real or a.is_imaginary or (S.ImaginaryUnit*a).is_real for a in arg.args):
                 from sympy import expand_mul
                 return sqrt(expand_mul(arg*arg.conjugate()))
     if arg.is_real is False and arg.is_imaginary is False:
         from sympy import expand_mul
         return sqrt(expand_mul(arg*arg.conjugate()))
 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) * C.factorial(n - m)/C.factorial(n + m)) *
           C.cos(m*phi) * assoc_legendre(n, m, C.cos(theta)))
     im = (sqrt((2*n + 1)/(4*pi) * C.factorial(n - m)/C.factorial(n + m)) *
           C.sin(m*phi) * assoc_legendre(n, m, C.cos(theta)))
     return (re, im)
示例#25
0
def test_issue_3554():
    x = Symbol("x")
    assert (1 / sqrt(1 + cos(x) * sin(x ** 2))).series(x, 0, 7) == 1 - x ** 2 / 2 + 5 * x ** 4 / 8 - 5 * x ** 6 / 8 + O(
        x ** 7
    )
    assert (1 / sqrt(1 + cos(x) * sin(x ** 2))).series(x, 0, 8) == 1 - x ** 2 / 2 + 5 * x ** 4 / 8 - 5 * x ** 6 / 8 + O(
        x ** 8
    )
示例#26
0
文件: ellipse.py 项目: Narsil/sympy
    def intersection(self, o):
        """The intersection of this circle with another geometrical entity.

        Parameters
        ----------
        o : GeometryEntity

        Returns
        -------
        intersection : list of GeometryEntities

        Examples
        --------
        >>> from sympy import Point, Circle, Line, Ray
        >>> p1, p2, p3 = Point(0, 0), Point(5, 5), Point(6, 0)
        >>> p4 = Point(5, 0)
        >>> c1 = Circle(p1, 5)
        >>> c1.intersection(p2)
        []
        >>> c1.intersection(p4)
        [Point(5, 0)]
        >>> c1.intersection(Ray(p1, p2))
        [Point(5*sqrt(2)/2, 5*sqrt(2)/2)]
        >>> c1.intersection(Line(p2, p3))
        []

        """
        if isinstance(o, Circle):
            if o.center == self.center:
                if o.radius == self.radius:
                    return o
                return []
            dx, dy = o.center - self.center
            d = sqrt(simplify(dy ** 2 + dx ** 2))
            R = o.radius + self.radius
            if d > R or d < abs(self.radius - o.radius):
                return []

            a = simplify((self.radius ** 2 - o.radius ** 2 + d ** 2) / (2 * d))

            x2 = self.center[0] + (dx * a / d)
            y2 = self.center[1] + (dy * a / d)

            h = sqrt(simplify(self.radius ** 2 - a ** 2))
            rx = -dy * (h / d)
            ry = dx * (h / d)

            xi_1 = simplify(x2 + rx)
            xi_2 = simplify(x2 - rx)
            yi_1 = simplify(y2 + ry)
            yi_2 = simplify(y2 - ry)

            ret = [Point(xi_1, yi_1)]
            if xi_1 != xi_2 or yi_1 != yi_2:
                ret.append(Point(xi_2, yi_2))
            return ret

        return Ellipse.intersection(self, o)
示例#27
0
 def _eval_expand_func(self, **hints):
     n, m, theta, phi = self.args
     rv = (
         sqrt((2 * n + 1) / (4 * pi) * C.factorial(n - m) / C.factorial(n + m))
         * C.exp(I * m * phi)
         * assoc_legendre(n, m, C.cos(theta))
     )
     # We can do this because of the range of theta
     return rv.subs(sqrt(-cos(theta) ** 2 + 1), sin(theta))
示例#28
0
 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))
示例#29
0
    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
示例#30
0
 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))
示例#31
0
    def _eval_rewrite_as_Integral(self, *args):
        from sympy import Integral, Dummy

        t = Dummy("t")
        m = self.args[0]
        return Integral(1 / sqrt(1 - m * sin(t)**2), (t, 0, pi / 2))
示例#32
0
    def eval(cls, arg):
        from sympy import acos
        arg = sympify(arg)

        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Infinity
            elif arg is S.NegativeInfinity:
                return S.Infinity
            elif arg is S.Zero:
                return S.Pi * S.ImaginaryUnit / 2
            elif arg is S.One:
                return S.Zero
            elif arg is S.NegativeOne:
                return S.Pi * S.ImaginaryUnit

        if arg.is_number:
            cst_table = {
                S.ImaginaryUnit: log(S.ImaginaryUnit * (1 + sqrt(2))),
                -S.ImaginaryUnit: log(-S.ImaginaryUnit * (1 + sqrt(2))),
                S.Half: S.Pi / 3,
                -S.Half: 2 * S.Pi / 3,
                sqrt(2) / 2: S.Pi / 4,
                -sqrt(2) / 2: 3 * S.Pi / 4,
                1 / sqrt(2): S.Pi / 4,
                -1 / sqrt(2): 3 * S.Pi / 4,
                sqrt(3) / 2: S.Pi / 6,
                -sqrt(3) / 2: 5 * S.Pi / 6,
                (sqrt(3) - 1) / sqrt(2**3): 5 * S.Pi / 12,
                -(sqrt(3) - 1) / sqrt(2**3): 7 * S.Pi / 12,
                sqrt(2 + sqrt(2)) / 2: S.Pi / 8,
                -sqrt(2 + sqrt(2)) / 2: 7 * S.Pi / 8,
                sqrt(2 - sqrt(2)) / 2: 3 * S.Pi / 8,
                -sqrt(2 - sqrt(2)) / 2: 5 * S.Pi / 8,
                (1 + sqrt(3)) / (2 * sqrt(2)): S.Pi / 12,
                -(1 + sqrt(3)) / (2 * sqrt(2)): 11 * S.Pi / 12,
                (sqrt(5) + 1) / 4: S.Pi / 5,
                -(sqrt(5) + 1) / 4: 4 * S.Pi / 5
            }

            if arg in cst_table:
                if arg.is_real:
                    return cst_table[arg] * S.ImaginaryUnit
                return cst_table[arg]

        if arg.is_infinite:
            return S.Infinity
示例#33
0
    def _eval_rewrite_as_Integral(self, *args):
        from sympy import Integral, Dummy

        t = Dummy("t")
        z, m = self.args[0], self.args[1]
        return Integral(1 / (sqrt(1 - m * sin(t)**2)), (t, 0, z))
示例#34
0
def gauss_hermite(n, n_digits):
    r"""
    Computes the Gauss-Hermite quadrature [1]_ points and weights.

    The Gauss-Hermite quadrature approximates the integral:

    .. math::
        \int_{-\infty}^{\infty} e^{-x^2} f(x)\,dx \approx \sum_{i=1}^n w_i f(x_i)

    The nodes `x_i` of an order `n` quadrature rule are the roots of `H_n`
    and the weights `w_i` are given by:

    .. math::
        w_i = \frac{2^{n-1} n! \sqrt{\pi}}{n^2 \left(H_{n-1}(x_i)\right)^2}

    Parameters
    ==========

    n : the order of quadrature

    n_digits : number of significant digits of the points and weights to return

    Returns
    =======

    (x, w) : the ``x`` and ``w`` are lists of points and weights as Floats.
             The points `x_i` and weights `w_i` are returned as ``(x, w)``
             tuple of lists.

    Examples
    ========

    >>> from sympy.integrals.quadrature import gauss_hermite
    >>> x, w = gauss_hermite(3, 5)
    >>> x
    [-1.2247, 0, 1.2247]
    >>> w
    [0.29541, 1.1816, 0.29541]

    >>> x, w = gauss_hermite(6, 5)
    >>> x
    [-2.3506, -1.3358, -0.43608, 0.43608, 1.3358, 2.3506]
    >>> w
    [0.00453, 0.15707, 0.72463, 0.72463, 0.15707, 0.00453]

    See Also
    ========

    gauss_legendre, gauss_laguerre, gauss_gen_laguerre, gauss_chebyshev_t, gauss_chebyshev_u, gauss_jacobi

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Gauss-Hermite_Quadrature
    .. [2] http://people.sc.fsu.edu/~jburkardt/cpp_src/hermite_rule/hermite_rule.html
    .. [3] http://people.sc.fsu.edu/~jburkardt/cpp_src/gen_hermite_rule/gen_hermite_rule.html
    """
    x = Dummy("x")
    p = hermite_poly(n, x, polys=True)
    p1 = hermite_poly(n - 1, x, polys=True)
    xi = []
    w = []
    for r in p.real_roots():
        if isinstance(r, RootOf):
            r = r.eval_rational(S(1) / 10**(n_digits + 2))
        xi.append(r.n(n_digits))
        w.append(((2**(n - 1) * factorial(n) * sqrt(pi)) /
                  (n**2 * p1.subs(x, r)**2)).n(n_digits))
    return xi, w
示例#35
0
def test_issue_6990():
    x = Symbol('x')
    a = Symbol('a')
    b = Symbol('b')
    assert (sqrt(a + b*x + x**2)).series(x, 0, 3).removeO() == \
        sqrt(a)*x**2*(1/(2*a) - b**2/(8*a**2)) + sqrt(a) + b*x/(2*sqrt(a))
示例#36
0
 def _eval_rewrite_as_log(self, x):
     """
     Rewrites asinh as log function.
     """
     return log(x + sqrt(x**2 + 1))
示例#37
0
def test_periodicity():
    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z', real=True)

    assert periodicity(sin(2 * x), x) == pi
    assert periodicity((-2) * tan(4 * x), x) == pi / 4
    assert periodicity(sin(x)**2, x) == 2 * pi
    assert periodicity(3**tan(3 * x), x) == pi / 3
    assert periodicity(tan(x) * cos(x), x) == 2 * pi
    assert periodicity(sin(x)**(tan(x)), x) == 2 * pi
    assert periodicity(tan(x) * sec(x), x) == 2 * pi
    assert periodicity(sin(2 * x) * cos(2 * x) - y, x) == pi / 2
    assert periodicity(tan(x) + cot(x), x) == pi
    assert periodicity(sin(x) - cos(2 * x), x) == 2 * pi
    assert periodicity(sin(x) - 1, x) == 2 * pi
    assert periodicity(sin(4 * x) + sin(x) * cos(x), x) == pi
    assert periodicity(exp(sin(x)), x) == 2 * pi
    assert periodicity(log(cot(2 * x)) - sin(cos(2 * x)), x) == pi
    assert periodicity(sin(2 * x) * exp(tan(x) - csc(2 * x)), x) == pi
    assert periodicity(cos(sec(x) - csc(2 * x)), x) == 2 * pi
    assert periodicity(tan(sin(2 * x)), x) == pi
    assert periodicity(2 * tan(x)**2, x) == pi
    assert periodicity(sin(x % 4), x) == 4
    assert periodicity(sin(x) % 4, x) == 2 * pi
    assert periodicity(tan((3 * x - 2) % 4), x) == Rational(4, 3)
    assert periodicity((sqrt(2) * (x + 1) + x) % 3, x) == 3 / (sqrt(2) + 1)
    assert periodicity((x**2 + 1) % x, x) is None
    assert periodicity(sin(re(x)), x) == 2 * pi
    assert periodicity(sin(x)**2 + cos(x)**2, x) is S.Zero
    assert periodicity(tan(x), y) is S.Zero
    assert periodicity(sin(x) + I * cos(x), x) == 2 * pi
    assert periodicity(x - sin(2 * y), y) == pi

    assert periodicity(exp(x), x) is None
    assert periodicity(exp(I * x), x) == 2 * pi
    assert periodicity(exp(I * z), z) == 2 * pi
    assert periodicity(exp(z), z) is None
    assert periodicity(exp(log(sin(z) + I * cos(2 * z)), evaluate=False),
                       z) == 2 * pi
    assert periodicity(exp(log(sin(2 * z) + I * cos(z)), evaluate=False),
                       z) == 2 * pi
    assert periodicity(exp(sin(z)), z) == 2 * pi
    assert periodicity(exp(2 * I * z), z) == pi
    assert periodicity(exp(z + I * sin(z)), z) is None
    assert periodicity(exp(cos(z / 2) + sin(z)), z) == 4 * pi
    assert periodicity(log(x), x) is None
    assert periodicity(exp(x)**sin(x), x) is None
    assert periodicity(sin(x)**y, y) is None

    assert periodicity(Abs(sin(Abs(sin(x)))), x) == pi
    assert all(
        periodicity(Abs(f(x)), x) == pi
        for f in (cos, sin, sec, csc, tan, cot))
    assert periodicity(Abs(sin(tan(x))), x) == pi
    assert periodicity(Abs(sin(sin(x) + tan(x))), x) == 2 * pi
    assert periodicity(sin(x) > S.Half, x) == 2 * pi

    assert periodicity(x > 2, x) is None
    assert periodicity(x**3 - x**2 + 1, x) is None
    assert periodicity(Abs(x), x) is None
    assert periodicity(Abs(x**2 - 1), x) is None

    assert periodicity((x**2 + 4) % 2, x) is None
    assert periodicity((E**x) % 3, x) is None

    assert periodicity(sin(expint(1, x)) / expint(1, x), x) is None
    # returning `None` for any Piecewise
    p = Piecewise((0, x < -1), (x**2, x <= 1), (log(x), True))
    assert periodicity(p, x) is None

    m = MatrixSymbol('m', 3, 3)
    raises(NotImplementedError, lambda: periodicity(sin(m), m))
    raises(NotImplementedError, lambda: periodicity(sin(m[0, 0]), m))
    raises(NotImplementedError, lambda: periodicity(sin(m), m[0, 0]))
    raises(NotImplementedError, lambda: periodicity(sin(m[0, 0]), m[0, 0]))
示例#38
0
    def eval(cls, arg):
        from sympy.simplify.simplify import signsimp
        from sympy.core.function import expand_mul
        from sympy.core.power import Pow

        if hasattr(arg, '_eval_Abs'):
            obj = arg._eval_Abs()
            if obj is not None:
                return obj
        if not isinstance(arg, Expr):
            raise TypeError("Bad argument type for Abs(): %s" % type(arg))

        # handle what we can
        arg = signsimp(arg, evaluate=False)
        n, d = arg.as_numer_denom()
        if d.free_symbols and not n.free_symbols:
            return cls(n) / cls(d)

        if arg.is_Mul:
            known = []
            unk = []
            for t in arg.args:
                if t.is_Pow and t.exp.is_integer and t.exp.is_negative:
                    bnew = cls(t.base)
                    if isinstance(bnew, cls):
                        unk.append(t)
                    else:
                        known.append(Pow(bnew, t.exp))
                else:
                    tnew = cls(t)
                    if isinstance(tnew, cls):
                        unk.append(t)
                    else:
                        known.append(tnew)
            known = Mul(*known)
            unk = cls(Mul(*unk), evaluate=False) if unk else S.One
            return known * unk
        if arg is S.NaN:
            return S.NaN
        if arg is S.ComplexInfinity:
            return S.Infinity
        if arg.is_Pow:
            base, exponent = arg.as_base_exp()
            if base.is_extended_real:
                if exponent.is_integer:
                    if exponent.is_even:
                        return arg
                    if base is S.NegativeOne:
                        return S.One
                    return Abs(base)**exponent
                if base.is_extended_nonnegative:
                    return base**re(exponent)
                if base.is_extended_negative:
                    return (-base)**re(exponent) * exp(-S.Pi * im(exponent))
                return
            elif not base.has(Symbol):  # complex base
                # express base**exponent as exp(exponent*log(base))
                a, b = log(base).as_real_imag()
                z = a + I * b
                return exp(re(exponent * z))
        if isinstance(arg, exp):
            return exp(re(arg.args[0]))
        if isinstance(arg, AppliedUndef):
            if arg.is_positive:
                return arg
            elif arg.is_negative:
                return -arg
            return
        if arg.is_Add and arg.has(S.Infinity, S.NegativeInfinity):
            if any(a.is_infinite for a in arg.as_real_imag()):
                return S.Infinity
        if arg.is_zero:
            return S.Zero
        if arg.is_extended_nonnegative:
            return arg
        if arg.is_extended_nonpositive:
            return -arg
        if arg.is_imaginary:
            arg2 = -S.ImaginaryUnit * arg
            if arg2.is_extended_nonnegative:
                return arg2
        if arg.is_extended_real:
            return
        # reject result if all new conjugates are just wrappers around
        # an expression that was already in the arg
        conj = signsimp(arg.conjugate(), evaluate=False)
        new_conj = conj.atoms(conjugate) - arg.atoms(conjugate)
        if new_conj and all(arg.has(i.args[0]) for i in new_conj):
            return
        if arg != conj and arg != -conj:
            ignore = arg.atoms(Abs)
            abs_free_arg = arg.xreplace({i: Dummy(real=True) for i in ignore})
            unk = [
                a for a in abs_free_arg.free_symbols
                if a.is_extended_real is None
            ]
            if not unk or not all(conj.has(conjugate(u)) for u in unk):
                return sqrt(expand_mul(arg * conj))
示例#39
0
def test_issue_17524():
    a = symbols("a", real=True)
    e = (-1 - a**2)*sqrt(1 + a**2)
    assert signsimp(powsimp(e)) == signsimp(e) == -(a**2 + 1)**(S(3)/2)
示例#40
0
def test_issue_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 = S.Half
    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)

    x = Symbol('x')
    y = Symbol('y')
    assert ((1 / (1 + x / 3))**(-S.One)).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**(-S.One)).as_numer_denom() == ((notp + x)**2, notp**2)
    nonp = Symbol('nonp', nonpositive=True)
    assert (((1 + x / nonp)**-2)**(-S.One)).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() == (S.ImaginaryUnit, 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)
示例#41
0
def test_issue_3866():
    assert --sqrt(sqrt(5) - 1) == sqrt(sqrt(5) - 1)
示例#42
0
 def fdiff(self, argindex=1):
     if argindex == 1:
         z = self.args[0]
         return -1 / (z * sqrt(1 - z**2))
     else:
         raise ArgumentIndexError(self, argindex)
示例#43
0
    def eval(cls, arg):
        from sympy import asec
        arg = sympify(arg)

        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Pi * S.ImaginaryUnit / 2
            elif arg is S.NegativeInfinity:
                return S.Pi * S.ImaginaryUnit / 2
            elif arg is S.Zero:
                return S.Infinity
            elif arg is S.One:
                return S.Zero
            elif arg is S.NegativeOne:
                return S.Pi * S.ImaginaryUnit

        if arg.is_number:
            cst_table = {
                S.ImaginaryUnit:
                -(S.Pi * S.ImaginaryUnit / 2) + log(1 + sqrt(2)),
                -S.ImaginaryUnit:
                (S.Pi * S.ImaginaryUnit / 2) + log(1 + sqrt(2)),
                (sqrt(6) - sqrt(2)): S.Pi / 12,
                (sqrt(2) - sqrt(6)): 11 * S.Pi / 12,
                sqrt(2 - 2 / sqrt(5)): S.Pi / 10,
                -sqrt(2 - 2 / sqrt(5)): 9 * S.Pi / 10,
                2 / sqrt(2 + sqrt(2)): S.Pi / 8,
                -2 / sqrt(2 + sqrt(2)): 7 * S.Pi / 8,
                2 / sqrt(3): S.Pi / 6,
                -2 / sqrt(3): 5 * S.Pi / 6,
                (sqrt(5) - 1): S.Pi / 5,
                (1 - sqrt(5)): 4 * S.Pi / 5,
                sqrt(2): S.Pi / 4,
                -sqrt(2): 3 * S.Pi / 4,
                sqrt(2 + 2 / sqrt(5)): 3 * S.Pi / 10,
                -sqrt(2 + 2 / sqrt(5)): 7 * S.Pi / 10,
                S(2): S.Pi / 3,
                -S(2): 2 * S.Pi / 3,
                sqrt(2 * (2 + sqrt(2))): 3 * S.Pi / 8,
                -sqrt(2 * (2 + sqrt(2))): 5 * S.Pi / 8,
                (1 + sqrt(5)): 2 * S.Pi / 5,
                (-1 - sqrt(5)): 3 * S.Pi / 5,
                (sqrt(6) + sqrt(2)): 5 * S.Pi / 12,
                (-sqrt(6) - sqrt(2)): 7 * S.Pi / 12,
            }

            if arg in cst_table:
                if arg.is_real:
                    return cst_table[arg] * S.ImaginaryUnit
                return cst_table[arg]

        if arg is S.ComplexInfinity:
            return S.NaN
示例#44
0
 def _eval_rewrite_as_log(self, arg):
     return log(1 / arg + sqrt(1 / arg**2 - 1))
示例#45
0
 def _eval_rewrite_as_conjugate(self, arg, **kwargs):
     return sqrt(arg * conjugate(arg))
示例#46
0
def test_powsimp():
    x, y, z, n = symbols('x,y,z,n')
    f = Function('f')
    assert powsimp( 4**x * 2**(-x) * 2**(-x) ) == 1
    assert powsimp( (-4)**x * (-2)**(-x) * 2**(-x) ) == 1

    assert powsimp(
        f(4**x * 2**(-x) * 2**(-x)) ) == f(4**x * 2**(-x) * 2**(-x))
    assert powsimp( f(4**x * 2**(-x) * 2**(-x)), deep=True ) == f(1)
    assert exp(x)*exp(y) == exp(x)*exp(y)
    assert powsimp(exp(x)*exp(y)) == exp(x + y)
    assert powsimp(exp(x)*exp(y)*2**x*2**y) == (2*E)**(x + y)
    assert powsimp(exp(x)*exp(y)*2**x*2**y, combine='exp') == \
        exp(x + y)*2**(x + y)
    assert powsimp(exp(x)*exp(y)*exp(2)*sin(x) + sin(y) + 2**x*2**y) == \
        exp(2 + x + y)*sin(x) + sin(y) + 2**(x + y)
    assert powsimp(sin(exp(x)*exp(y))) == sin(exp(x)*exp(y))
    assert powsimp(sin(exp(x)*exp(y)), deep=True) == sin(exp(x + y))
    assert powsimp(x**2*x**y) == x**(2 + y)
    # This should remain factored, because 'exp' with deep=True is supposed
    # to act like old automatic exponent combining.
    assert powsimp((1 + E*exp(E))*exp(-E), combine='exp', deep=True) == \
        (1 + exp(1 + E))*exp(-E)
    assert powsimp((1 + E*exp(E))*exp(-E), deep=True) == \
        (1 + exp(1 + E))*exp(-E)
    assert powsimp((1 + E*exp(E))*exp(-E)) == (1 + exp(1 + E))*exp(-E)
    assert powsimp((1 + E*exp(E))*exp(-E), combine='exp') == \
        (1 + exp(1 + E))*exp(-E)
    assert powsimp((1 + E*exp(E))*exp(-E), combine='base') == \
        (1 + E*exp(E))*exp(-E)
    x, y = symbols('x,y', nonnegative=True)
    n = Symbol('n', real=True)
    assert powsimp(y**n * (y/x)**(-n)) == x**n
    assert powsimp(x**(x**(x*y)*y**(x*y))*y**(x**(x*y)*y**(x*y)), deep=True) \
        == (x*y)**(x*y)**(x*y)
    assert powsimp(2**(2**(2*x)*x), deep=False) == 2**(2**(2*x)*x)
    assert powsimp(2**(2**(2*x)*x), deep=True) == 2**(x*4**x)
    assert powsimp(
        exp(-x + exp(-x)*exp(-x*log(x))), deep=False, combine='exp') == \
        exp(-x + exp(-x)*exp(-x*log(x)))
    assert powsimp(
        exp(-x + exp(-x)*exp(-x*log(x))), deep=False, combine='exp') == \
        exp(-x + exp(-x)*exp(-x*log(x)))
    assert powsimp((x + y)/(3*z), deep=False, combine='exp') == (x + y)/(3*z)
    assert powsimp((x/3 + y/3)/z, deep=True, combine='exp') == (x/3 + y/3)/z
    assert powsimp(exp(x)/(1 + exp(x)*exp(y)), deep=True) == \
        exp(x)/(1 + exp(x + y))
    assert powsimp(x*y**(z**x*z**y), deep=True) == x*y**(z**(x + y))
    assert powsimp((z**x*z**y)**x, deep=True) == (z**(x + y))**x
    assert powsimp(x*(z**x*z**y)**x, deep=True) == x*(z**(x + y))**x
    p = symbols('p', positive=True)
    assert powsimp((1/x)**log(2)/x) == (1/x)**(1 + log(2))
    assert powsimp((1/p)**log(2)/p) == p**(-1 - log(2))

    # coefficient of exponent can only be simplified for positive bases
    assert powsimp(2**(2*x)) == 4**x
    assert powsimp((-1)**(2*x)) == (-1)**(2*x)
    i = symbols('i', integer=True)
    assert powsimp((-1)**(2*i)) == 1
    assert powsimp((-1)**(-x)) != (-1)**x  # could be 1/((-1)**x), but is not
    # force=True overrides assumptions
    assert powsimp((-1)**(2*x), force=True) == 1

    # rational exponents allow combining of negative terms
    w, n, m = symbols('w n m', negative=True)
    e = i/a  # not a rational exponent if `a` is unknown
    ex = w**e*n**e*m**e
    assert powsimp(ex) == m**(i/a)*n**(i/a)*w**(i/a)
    e = i/3
    ex = w**e*n**e*m**e
    assert powsimp(ex) == (-1)**i*(-m*n*w)**(i/3)
    e = (3 + i)/i
    ex = w**e*n**e*m**e
    assert powsimp(ex) == (-1)**(3*e)*(-m*n*w)**e

    eq = x**(a*Rational(2, 3))
    # eq != (x**a)**(2/3) (try x = -1 and a = 3 to see)
    assert powsimp(eq).exp == eq.exp == a*Rational(2, 3)
    # powdenest goes the other direction
    assert powsimp(2**(2*x)) == 4**x

    assert powsimp(exp(p/2)) == exp(p/2)

    # issue 6368
    eq = Mul(*[sqrt(Dummy(imaginary=True)) for i in range(3)])
    assert powsimp(eq) == eq and eq.is_Mul

    assert all(powsimp(e) == e for e in (sqrt(x**a), sqrt(x**2)))

    # issue 8836
    assert str( powsimp(exp(I*pi/3)*root(-1,3)) ) == '(-1)**(2/3)'

    # issue 9183
    assert powsimp(-0.1**x) == -0.1**x

    # issue 10095
    assert powsimp((1/(2*E))**oo) == (exp(-1)/2)**oo

    # PR 13131
    eq = sin(2*x)**2*sin(2.0*x)**2
    assert powsimp(eq) == eq

    # issue 14615
    assert powsimp(x**2*y**3*(x*y**2)**Rational(3, 2)
        ) == x*y*(x*y**2)**Rational(5, 2)
示例#47
0
def test_DiracDelta():
    assert DiracDelta(1) == 0
    assert DiracDelta(5.1) == 0
    assert DiracDelta(-pi) == 0
    assert DiracDelta(5, 7) == 0
    assert DiracDelta(i) == 0
    assert DiracDelta(j) == 0
    assert DiracDelta(k) == 0
    assert DiracDelta(nan) is nan
    assert DiracDelta(0).func is DiracDelta
    assert DiracDelta(x).func is DiracDelta
    # FIXME: this is generally undefined @ x=0
    #         But then limit(Delta(c)*Heaviside(x),x,-oo)
    #         need's to be implemented.
    # assert 0*DiracDelta(x) == 0

    assert adjoint(DiracDelta(x)) == DiracDelta(x)
    assert adjoint(DiracDelta(x - y)) == DiracDelta(x - y)
    assert conjugate(DiracDelta(x)) == DiracDelta(x)
    assert conjugate(DiracDelta(x - y)) == DiracDelta(x - y)
    assert transpose(DiracDelta(x)) == DiracDelta(x)
    assert transpose(DiracDelta(x - y)) == DiracDelta(x - y)

    assert DiracDelta(x).diff(x) == DiracDelta(x, 1)
    assert DiracDelta(x, 1).diff(x) == DiracDelta(x, 2)

    assert DiracDelta(x).is_simple(x) is True
    assert DiracDelta(3 * x).is_simple(x) is True
    assert DiracDelta(x**2).is_simple(x) is False
    assert DiracDelta(sqrt(x)).is_simple(x) is False
    assert DiracDelta(x).is_simple(y) is False

    assert DiracDelta(x * y).expand(diracdelta=True,
                                    wrt=x) == DiracDelta(x) / abs(y)
    assert DiracDelta(x * y).expand(diracdelta=True,
                                    wrt=y) == DiracDelta(y) / abs(x)
    assert DiracDelta(x**2 * y).expand(diracdelta=True,
                                       wrt=x) == DiracDelta(x**2 * y)
    assert DiracDelta(y).expand(diracdelta=True, wrt=x) == DiracDelta(y)
    assert DiracDelta((x - 1) * (x - 2) * (x - 3)).expand(
        diracdelta=True, wrt=x) == (DiracDelta(x - 3) / 2 + DiracDelta(x - 2) +
                                    DiracDelta(x - 1) / 2)

    assert DiracDelta(2 * x) != DiracDelta(x)  # scaling property
    assert DiracDelta(x) == DiracDelta(-x)  # even function
    assert DiracDelta(-x, 2) == DiracDelta(x, 2)
    assert DiracDelta(-x, 1) == -DiracDelta(x, 1)  # odd deriv is odd
    assert DiracDelta(-oo * x) == DiracDelta(oo * x)
    assert DiracDelta(x - y) != DiracDelta(y - x)
    assert signsimp(DiracDelta(x - y) - DiracDelta(y - x)) == 0

    assert DiracDelta(x * y).expand(diracdelta=True,
                                    wrt=x) == DiracDelta(x) / abs(y)
    assert DiracDelta(x * y).expand(diracdelta=True,
                                    wrt=y) == DiracDelta(y) / abs(x)
    assert DiracDelta(x**2 * y).expand(diracdelta=True,
                                       wrt=x) == DiracDelta(x**2 * y)
    assert DiracDelta(y).expand(diracdelta=True, wrt=x) == DiracDelta(y)
    assert DiracDelta((x - 1) * (x - 2) * (x - 3)).expand(
        diracdelta=True) == (DiracDelta(x - 3) / 2 + DiracDelta(x - 2) +
                             DiracDelta(x - 1) / 2)

    raises(ArgumentIndexError, lambda: DiracDelta(x).fdiff(2))
    raises(ValueError, lambda: DiracDelta(x, -1))
    raises(ValueError, lambda: DiracDelta(I))
    raises(ValueError, lambda: DiracDelta(2 + 3 * I))
示例#48
0
 def _eval_rewrite_as_bessely(self, nu, z):
     return sqrt(pi / (2 * z)) * bessely(nu + S('1/2'), z)
示例#49
0
def test_issue_18190():
    assert sqrt(1 / tan(1 + I)) == 1 / sqrt(tan(1 + I))
示例#50
0
def test_better_sqrt():
    n = Symbol('n', integer=True, nonnegative=True)
    assert sqrt(3 + 4 * I) == 2 + I
    assert sqrt(3 - 4 * I) == 2 - I
    assert sqrt(-3 - 4 * I) == 1 - 2 * I
    assert sqrt(-3 + 4 * I) == 1 + 2 * I
    assert sqrt(32 + 24 * I) == 6 + 2 * I
    assert sqrt(32 - 24 * I) == 6 - 2 * I
    assert sqrt(-32 - 24 * I) == 2 - 6 * I
    assert sqrt(-32 + 24 * I) == 2 + 6 * I

    # triple (3, 4, 5):
    # parity of 3 matches parity of 5 and
    # den, 4, is a square
    assert sqrt((3 + 4 * I) / 4) == 1 + I / 2
    # triple (8, 15, 17)
    # parity of 8 doesn't match parity of 17 but
    # den/2, 8/2, is a square
    assert sqrt((8 + 15 * I) / 8) == (5 + 3 * I) / 4
    # handle the denominator
    assert sqrt((3 - 4 * I) / 25) == (2 - I) / 5
    assert sqrt((3 - 4 * I) / 26) == (2 - I) / sqrt(26)
    # mul
    #  issue #12739
    assert sqrt((3 + 4 * I) / (3 - 4 * I)) == (3 + 4 * I) / 5
    assert sqrt(2 / (3 + 4 * I)) == sqrt(2) / 5 * (2 - I)
    assert sqrt(n / (3 + 4 * I)).subs(n, 2) == sqrt(2) / 5 * (2 - I)
    assert sqrt(-2 / (3 + 4 * I)) == sqrt(2) / 5 * (1 + 2 * I)
    assert sqrt(-n / (3 + 4 * I)).subs(n, 2) == sqrt(2) / 5 * (1 + 2 * I)
    # power
    assert sqrt(1 / (3 + I * 4)) == (2 - I) / 5
    assert sqrt(1 / (3 - I)) == sqrt(10) * sqrt(3 + I) / 10
    # symbolic
    i = symbols('i', imaginary=True)
    assert sqrt(3 / i) == Mul(sqrt(3), 1 / sqrt(i), evaluate=False)
    # multiples of 1/2; don't make this too automatic
    assert sqrt((3 + 4 * I))**3 == (2 + I)**3
    assert Pow(3 + 4 * I, Rational(3, 2)) == 2 + 11 * I
    assert Pow(6 + 8 * I, Rational(3, 2)) == 2 * sqrt(2) * (2 + 11 * I)
    n, d = (3 + 4 * I), (3 - 4 * I)**3
    a = n / d
    assert a.args == (1 / d, n)
    eq = sqrt(a)
    assert eq.args == (a, S.Half)
    assert expand_multinomial(eq) == sqrt((-117 + 44 * I) * (3 + 4 * I)) / 125
    assert eq.expand() == (7 - 24 * I) / 125

    # issue 12775
    # pos im part
    assert sqrt(2 * I) == (1 + I)
    assert sqrt(2 * 9 * I) == Mul(3, 1 + I, evaluate=False)
    assert Pow(2 * I, 3 * S.Half) == (1 + I)**3
    # neg im part
    assert sqrt(-I / 2) == Mul(S.Half, 1 - I, evaluate=False)
    # fractional im part
    assert Pow(Rational(-9, 2) * I, Rational(3, 2)) == 27 * (1 - I)**3 / 8
示例#51
0
def test_issue_5728():
    b = x*sqrt(y)
    a = sqrt(b)
    c = sqrt(sqrt(x)*y)
    assert powsimp(a*b) == sqrt(b)**3
    assert powsimp(a*b**2*sqrt(y)) == sqrt(y)*a**5
    assert powsimp(a*x**2*c**3*y) == c**3*a**5
    assert powsimp(a*x*c**3*y**2) == c**7*a
    assert powsimp(x*c**3*y**2) == c**7
    assert powsimp(x*c**3*y) == x*y*c**3
    assert powsimp(sqrt(x)*c**3*y) == c**5
    assert powsimp(sqrt(x)*a**3*sqrt(y)) == sqrt(x)*sqrt(y)*a**3
    assert powsimp(Mul(sqrt(x)*c**3*sqrt(y), y, evaluate=False)) == \
        sqrt(x)*sqrt(y)**3*c**3
    assert powsimp(a**2*a*x**2*y) == a**7

    # symbolic powers work, too
    b = x**y*y
    a = b*sqrt(b)
    assert a.is_Mul is True
    assert powsimp(a) == sqrt(b)**3

    # as does exp
    a = x*exp(y*Rational(2, 3))
    assert powsimp(a*sqrt(a)) == sqrt(a)**3
    assert powsimp(a**2*sqrt(a)) == sqrt(a)**5
    assert powsimp(a**2*sqrt(sqrt(a))) == sqrt(sqrt(a))**9
示例#52
0
def test_issue_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) == ((1 + I)**(12 * I * f))**Rational(1, 3)

    assert (((1 + I)**(I * (1 + 7 * f)))**Rational(1, 3)).exp == Rational(1, 3)
    r = symbols('r', real=True)
    assert sqrt(r**2) == abs(r)
    assert cbrt(r**3) != r
    assert sqrt(Pow(2 * I, 5 * S.Half)) != (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) / sqrt(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 in [S.Half, Rational(3, 2) + I / 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**(5 * S.Half)
    assert sqrt(e**6) == e**3
    assert sqrt((1 + I * r)**6) != (1 + I * r)**3
示例#53
0
def test_issue_19869():
    t = symbols('t')
    assert (maximum(sqrt(3) * (t - 1) / (3 * sqrt(t**2 + 1)),
                    t)) == sqrt(3) / 3
示例#54
0
def test_issue_6653():
    x = Symbol('x')
    assert (1 / sqrt(1 + sin(x**2))).series(x, 0, 3) == 1 - x**2 / 2 + O(x**3)
示例#55
0
    def eval(cls, arg, base=None):
        from sympy import unpolarify
        from sympy.calculus import AccumBounds
        from sympy.sets.setexpr import SetExpr
        from sympy.functions.elementary.complexes import Abs

        arg = sympify(arg)

        if base is not None:
            base = sympify(base)
            if base == 1:
                if arg == 1:
                    return S.NaN
                else:
                    return S.ComplexInfinity
            try:
                # handle extraction of powers of the base now
                # or else expand_log in Mul would have to handle this
                n = multiplicity(base, arg)
                if n:
                    return n + log(arg / base**n) / log(base)
                else:
                    return log(arg) / log(base)
            except ValueError:
                pass
            if base is not S.Exp1:
                return cls(arg) / cls(base)
            else:
                return cls(arg)

        if arg.is_Number:
            if arg.is_zero:
                return S.ComplexInfinity
            elif arg is S.One:
                return S.Zero
            elif arg is S.Infinity:
                return S.Infinity
            elif arg is S.NegativeInfinity:
                return S.Infinity
            elif arg is S.NaN:
                return S.NaN
            elif arg.is_Rational and arg.p == 1:
                return -cls(arg.q)

        I = S.ImaginaryUnit
        if isinstance(arg, exp) and arg.args[0].is_extended_real:
            return arg.args[0]
        elif isinstance(arg, exp) and arg.args[0].is_number:
            r_, i_ = match_real_imag(arg.args[0])
            if i_ and i_.is_comparable:
                i_ %= 2 * S.Pi
                if i_ > S.Pi:
                    i_ -= 2 * S.Pi
                return r_ + expand_mul(i_ * I, deep=False)
        elif isinstance(arg, exp_polar):
            return unpolarify(arg.exp)
        elif isinstance(arg, AccumBounds):
            if arg.min.is_positive:
                return AccumBounds(log(arg.min), log(arg.max))
            else:
                return
        elif isinstance(arg, SetExpr):
            return arg._eval_func(cls)

        if arg.is_number:
            if arg.is_negative:
                return S.Pi * I + cls(-arg)
            elif arg is S.ComplexInfinity:
                return S.ComplexInfinity
            elif arg is S.Exp1:
                return S.One

        if arg.is_zero:
            return S.ComplexInfinity

        # don't autoexpand Pow or Mul (see the issue 3351):
        if not arg.is_Add:
            coeff = arg.as_coefficient(I)

            if coeff is not None:
                if coeff is S.Infinity:
                    return S.Infinity
                elif coeff is S.NegativeInfinity:
                    return S.Infinity
                elif coeff.is_Rational:
                    if coeff.is_nonnegative:
                        return S.Pi * I * S.Half + cls(coeff)
                    else:
                        return -S.Pi * I * S.Half + cls(-coeff)

        if arg.is_number and arg.is_algebraic:
            # Match arg = coeff*(r_ + i_*I) with coeff>0, r_ and i_ real.
            coeff, arg_ = arg.as_independent(I, as_Add=False)
            if coeff.is_negative:
                coeff *= -1
                arg_ *= -1
            arg_ = expand_mul(arg_, deep=False)
            r_, i_ = arg_.as_independent(I, as_Add=True)
            i_ = i_.as_coefficient(I)
            if coeff.is_real and i_ and i_.is_real and r_.is_real:
                if r_.is_zero:
                    if i_.is_positive:
                        return S.Pi * I * S.Half + cls(coeff * i_)
                    elif i_.is_negative:
                        return -S.Pi * I * S.Half + cls(coeff * -i_)
                else:
                    from sympy.simplify import ratsimp
                    # Check for arguments involving rational multiples of pi
                    t = (i_ / r_).cancel()
                    t1 = (-t).cancel()
                    atan_table = {
                        # first quadrant only
                        sqrt(3):
                        S.Pi / 3,
                        1:
                        S.Pi / 4,
                        sqrt(5 - 2 * sqrt(5)):
                        S.Pi / 5,
                        sqrt(2) * sqrt(5 - sqrt(5)) / (1 + sqrt(5)):
                        S.Pi / 5,
                        sqrt(5 + 2 * sqrt(5)):
                        S.Pi * Rational(2, 5),
                        sqrt(2) * sqrt(sqrt(5) + 5) / (-1 + sqrt(5)):
                        S.Pi * Rational(2, 5),
                        sqrt(3) / 3:
                        S.Pi / 6,
                        sqrt(2) - 1:
                        S.Pi / 8,
                        sqrt(2 - sqrt(2)) / sqrt(sqrt(2) + 2):
                        S.Pi / 8,
                        sqrt(2) + 1:
                        S.Pi * Rational(3, 8),
                        sqrt(sqrt(2) + 2) / sqrt(2 - sqrt(2)):
                        S.Pi * Rational(3, 8),
                        sqrt(1 - 2 * sqrt(5) / 5):
                        S.Pi / 10,
                        (-sqrt(2) + sqrt(10)) / (2 * sqrt(sqrt(5) + 5)):
                        S.Pi / 10,
                        sqrt(1 + 2 * sqrt(5) / 5):
                        S.Pi * Rational(3, 10),
                        (sqrt(2) + sqrt(10)) / (2 * sqrt(5 - sqrt(5))):
                        S.Pi * Rational(3, 10),
                        2 - sqrt(3):
                        S.Pi / 12,
                        (-1 + sqrt(3)) / (1 + sqrt(3)):
                        S.Pi / 12,
                        2 + sqrt(3):
                        S.Pi * Rational(5, 12),
                        (1 + sqrt(3)) / (-1 + sqrt(3)):
                        S.Pi * Rational(5, 12)
                    }
                    if t in atan_table:
                        modulus = ratsimp(coeff * Abs(arg_))
                        if r_.is_positive:
                            return cls(modulus) + I * atan_table[t]
                        else:
                            return cls(modulus) + I * (atan_table[t] - S.Pi)
                    elif t1 in atan_table:
                        modulus = ratsimp(coeff * Abs(arg_))
                        if r_.is_positive:
                            return cls(modulus) + I * (-atan_table[t1])
                        else:
                            return cls(modulus) + I * (S.Pi - atan_table[t1])
示例#56
0
def test_issue_6782():
    x = Symbol('x')
    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)
示例#57
0
def test_issue_18762():
    e, p = symbols('e p')
    g0 = sqrt(1 + e**2 - 2 * e * cos(p))
    assert len(g0.series(e, 1, 3).args) == 4
示例#58
0
    def _eval_rewrite_as_Integral(self, *args):
        from sympy import Integral, Dummy

        z, m = (pi / 2, self.args[0]) if len(self.args) == 1 else self.args
        t = Dummy("t")
        return Integral(sqrt(1 - m * sin(t)**2), (t, 0, z))
示例#59
0
 def _eval_rewrite_as_yn(self, nu, z):
     return sqrt(2 * z / pi) * yn(nu - S.Half, self.argument)
示例#60
0
 def fdiff(self, argindex=1):
     if argindex == 1:
         return 1 / sqrt(self.args[0]**2 - 1)
     else:
         raise ArgumentIndexError(self, argindex)