Пример #1
0
def test_issue_7535():
    assert limit(tan(x)/sin(tan(x)), x, pi/2) == Limit(tan(x)/sin(tan(x)), x, pi/2, dir='+')
    assert limit(tan(x)/sin(tan(x)), x, pi/2, dir='-') == Limit(tan(x)/sin(tan(x)), x, pi/2, dir='-')
    assert limit(tan(x)/sin(tan(x)), x, pi/2, dir='+-') == Limit(tan(x)/sin(tan(x)), x, pi/2, dir='+-')
    assert limit(sin(tan(x)),x,pi/2) == AccumBounds(-1, 1)
    assert -oo*(1/sin(-oo)) == AccumBounds(-oo, oo)
    assert oo*(1/sin(oo)) == AccumBounds(-oo, oo)
    assert oo*(1/sin(-oo)) == AccumBounds(-oo, oo)
    assert -oo*(1/sin(oo)) == AccumBounds(-oo, oo)
Пример #2
0
def test_issue_19770():
    m = Symbol('m')
    # the result is not 0 for non-real m
    assert limit(cos(m*x)/x, x, oo) == Limit(cos(m*x)/x, x, oo, dir='-')
    m = Symbol('m', real=True)
    # can be improved to give the correct result 0
    assert limit(cos(m*x)/x, x, oo) == Limit(cos(m*x)/x, x, oo, dir='-')
    m = Symbol('m', nonzero=True)
    assert limit(cos(m*x), x, oo) == AccumBounds(-1, 1)
    assert limit(cos(m*x)/x, x, oo) == 0
Пример #3
0
def test_issue_18473():
    assert limit(sin(x)**(1/x), x, oo) == Limit(sin(x)**(1/x), x, oo, dir='-')
    assert limit(cos(x)**(1/x), x, oo) == Limit(cos(x)**(1/x), x, oo, dir='-')
    assert limit(tan(x)**(1/x), x, oo) == Limit(tan(x)**(1/x), x, oo, dir='-')
    assert limit((cos(x) + 2)**(1/x), x, oo) == 1
    assert limit((sin(x) + 10)**(1/x), x, oo) == 1
    assert limit((cos(x) - 2)**(1/x), x, oo) == Limit((cos(x) - 2)**(1/x), x, oo, dir='-')
    assert limit((cos(x) + 1)**(1/x), x, oo) == AccumBounds(0, 1)
    assert limit((tan(x)**2)**(2/x) , x, oo) == AccumBounds(0, oo)
    assert limit((sin(x)**2)**(1/x), x, oo) == AccumBounds(0, 1)
Пример #4
0
def test_basic5():
    class my(Function):
        @classmethod
        def eval(cls, arg):
            if arg is S.Infinity:
                return S.NaN
    assert limit(my(x), x, oo) == Limit(my(x), x, oo)
Пример #5
0
def test_Rational():
    n1 = Rational(1, 4)
    n2 = Rational(1, 3)
    n3 = Rational(2, 4)
    n4 = Rational(2, -4)
    n5 = Rational(0)
    n7 = Rational(3)
    n8 = Rational(-3)
    assert str(n1*n2) == "1/12"
    assert str(n1*n2) == "1/12"
    assert str(n3) == "1/2"
    assert str(n1*n3) == "1/8"
    assert str(n1 + n3) == "3/4"
    assert str(n1 + n2) == "7/12"
    assert str(n1 + n4) == "-1/4"
    assert str(n4*n4) == "1/4"
    assert str(n4 + n2) == "-1/6"
    assert str(n4 + n5) == "-1/2"
    assert str(n4*n5) == "0"
    assert str(n3 + n4) == "0"
    assert str(n1**n7) == "1/64"
    assert str(n2**n7) == "1/27"
    assert str(n2**n8) == "27"
    assert str(n7**n8) == "1/27"
    assert str(Rational("-25")) == "-25"
    assert str(Rational("1.25")) == "5/4"
    assert str(Rational("-2.6e-2")) == "-13/500"
    assert str(S("25/7")) == "25/7"
    assert str(S("-123/569")) == "-123/569"
    assert str(S("0.1[23]", rational=1)) == "61/495"
    assert str(S("5.1[666]", rational=1)) == "31/6"
    assert str(S("-5.1[666]", rational=1)) == "-31/6"
    assert str(S("0.[9]", rational=1)) == "1"
    assert str(S("-0.[9]", rational=1)) == "-1"

    assert str(sqrt(Rational(1, 4))) == "1/2"
    assert str(sqrt(Rational(1, 36))) == "1/6"

    assert str((123**25) ** Rational(1, 25)) == "123"
    assert str((123**25 + 1)**Rational(1, 25)) != "123"
    assert str((123**25 - 1)**Rational(1, 25)) != "123"
    assert str((123**25 - 1)**Rational(1, 25)) != "122"

    assert str(sqrt(Rational(81, 36))**3) == "27/8"
    assert str(1/sqrt(Rational(81, 36))**3) == "8/27"

    assert str(sqrt(-4)) == str(2*I)
    assert str(2**Rational(1, 10**10)) == "2**(1/10000000000)"

    assert sstr(Rational(2, 3), sympy_integers=True) == "S(2)/3"
    x = Symbol("x")
    assert sstr(x**Rational(2, 3), sympy_integers=True) == "x**(S(2)/3)"
    assert sstr(Eq(x, Rational(2, 3)), sympy_integers=True) == "Eq(x, S(2)/3)"
    assert sstr(Limit(x, x, Rational(7, 2)), sympy_integers=True) == \
        "Limit(x, x, S(7)/2)"
Пример #6
0
def prove(Eq):
    n = Symbol.n(integer=True, positive=True)
    Eq << apply(n)

    x = Symbol.x(real=True)
    x0 = Symbol.x0(real=True, positive=True)
    Eq.continuity = Equality(Limit(1 / x, x, x0, "+-"), 1 / x0, plausible=True)

    Eq << Eq.continuity.this.lhs.doit()

    k, *ab = Eq[-1].lhs.args[0].args[-1].limits[0]
    k = k.copy(domain=Interval(*ab, integer=True))

    Eq << Eq.continuity.forall((x0, k, k + 1))

    Eq.mean_value_theorem = axiom.calculus.integral.mean_value_theorem.apply(
        Eq[-1])

    Eq << Eq[-1].limits_assertion()

    Eq << Eq[-1].inverse().split()

    Eq << (Eq.mean_value_theorem.subs(
        Eq[-1]), Eq.mean_value_theorem.subs(Eq[-2]))

    Eq << (Eq[-1].sum((k, 1, n - 1)), Eq[-2].sum((k, 1, n)))

    Eq << (Eq[-1].this.lhs.doit(), Eq[-2].this.lhs.doit().reversed)

    k = Eq[-1].lhs.variable
    Eq << Eq[-1].this.lhs.limits_subs(k, k - 1) + 1

    assert Eq[-3].lhs > 0
    Eq << (Eq[-3] / Eq[-3].lhs, Eq[-1] / Eq[-3].lhs)

    Eq << (Eq[-2].limit(n, oo), Eq[-1].limit(n, oo))

    Eq << Eq[-1].subs(Eq[-2])
Пример #7
0
def test_issue_17325():
    assert Limit(sin(x)/x, x, 0, dir="+-").doit() == 1
    assert Limit(x**2, x, 0, dir="+-").doit() == 0
    assert Limit(1/x**2, x, 0, dir="+-").doit() is oo
    assert Limit(1/x, x, 0, dir="+-").doit() is zoo
Пример #8
0
def test_issue_14456():
    raises(NotImplementedError, lambda: Limit(exp(x), x, zoo).doit())
    raises(NotImplementedError, lambda: Limit(x**2/(x+1), x, zoo).doit())
Пример #9
0
def test_issue_9205():
    x, y, a = symbols('x, y, a')
    assert Limit(x, x, a).free_symbols == {a}
    assert Limit(x, x, a, '-').free_symbols == {a}
    assert Limit(x + y, x + y, a).free_symbols == {a}
    assert Limit(-x**2 + y, x**2, a).free_symbols == {y, a}
Пример #10
0
def test_Limit_dir():
    raises(TypeError, lambda: Limit(x, x, 0, dir=0))
    raises(ValueError, lambda: Limit(x, x, 0, dir='0'))
Пример #11
0
def test_extended_real_line():
    assert limit(x - oo, x, oo) == Limit(x - oo, x, oo)
    assert limit(1/(x + sin(x)) - oo, x, 0) == Limit(1/(x + sin(x)) - oo, x, 0)
    assert limit(oo/x, x, oo) == Limit(oo/x, x, oo)
    assert limit(x - oo + 1/x, x, oo) == Limit(x - oo + 1/x, x, oo)
Пример #12
0
def test_doit2():
    f = Integral(2 * x, x)
    l = Limit(f, x, oo)
    # limit() breaks on the contained Integral.
    assert l.doit(deep=False) == l
Пример #13
0
def apply(n):
    k = Symbol.k(integer=True)
    return Equality(Limit(Sum[k:1:n](1 / k) / log(n + 1), n, oo), 1)
Пример #14
0
def _limit_inf(expr, n):
    try:
        return Limit(expr, n, S.Infinity).doit(deep=False)
    except (NotImplementedError, PoleError):
        return None
Пример #15
0
def test_sympy__series__limits__Limit():
    from sympy.series.limits import Limit
    assert _test_args(Limit(x, x, 0, dir='-'))
Пример #16
0
def test_issue_18442():
    assert limit(tan(x)**(2**(sqrt(pi))), x, oo, dir='-') == Limit(tan(x)**(2**(sqrt(pi))), x, oo, dir='-')
Пример #17
0
 def contains(self, expr):
     r"""
     Return True if expr belongs to Order(self.expr, \*self.variables).
     Return False if self belongs to expr.
     Return None if the inclusion relation cannot be determined
     (e.g. when self and expr have different symbols).
     """
     from sympy import powsimp
     if expr is S.Zero:
         return True
     if expr is S.NaN:
         return False
     if expr.is_Order:
         if (not all(p == expr.point[0] for p in expr.point)
                 and not all(p == self.point[0]
                             for p in self.point)):  # pragma: no cover
             raise NotImplementedError(
                 'Order at points other than 0 '
                 'or oo not supported, got %s as a point.' % point)
         else:
             # self and/or expr is O(1):
             if any(not p for p in [expr.point, self.point]):
                 point = self.point + expr.point
                 if point:
                     point = point[0]
                 else:
                     point = S.Zero
             else:
                 point = self.point[0]
         if expr.expr == self.expr:
             # O(1) + O(1), O(1) + O(1, x), etc.
             return all([x in self.args[1:] for x in expr.args[1:]])
         if expr.expr.is_Add:
             return all([self.contains(x) for x in expr.expr.args])
         if self.expr.is_Add and point == S.Zero:
             return any([
                 self.func(x, *self.args[1:]).contains(expr)
                 for x in self.expr.args
             ])
         if self.variables and expr.variables:
             common_symbols = tuple(
                 [s for s in self.variables if s in expr.variables])
         elif self.variables:
             common_symbols = self.variables
         else:
             common_symbols = expr.variables
         if not common_symbols:
             return None
         if (self.expr.is_Pow and self.expr.base.is_symbol
                 and self.expr.exp.is_positive):
             if expr.expr.is_Pow and self.expr.base == expr.expr.base:
                 return not (self.expr.exp - expr.expr.exp).is_positive
             if expr.expr.is_Mul:
                 for arg in expr.expr.args:
                     if (arg.is_Pow and self.expr.base == arg.base
                             and (expr.expr / arg).is_number):
                         r = (self.expr.exp - arg.exp).is_positive
                         if not (r is None):
                             return not r
         r = None
         ratio = self.expr / expr.expr
         ratio = powsimp(ratio, deep=True, combine='exp')
         for s in common_symbols:
             from sympy.series.limits import Limit
             l = Limit(ratio, s, point).doit(heuristics=False)
             if not isinstance(l, Limit):
                 l = l != 0
             else:
                 l = None
             if r is None:
                 r = l
             else:
                 if r != l:
                     return
         return r
     if (self.expr.is_Pow and self.expr.base.is_symbol
             and self.expr.exp.is_positive):
         if expr.is_Pow and self.expr.base == expr.base:
             return not (self.expr.exp - expr.exp).is_positive
         if expr.is_Mul:
             for arg in expr.args:
                 if (arg.is_Pow and self.expr.base == arg.base
                         and (expr / arg).is_number):
                     r = (self.expr.exp - arg.exp).is_positive
                     if not (r is None):
                         return not r
     obj = self.func(expr, *self.args[1:])
     return self.contains(obj)
Пример #18
0
def test_Limit():
    assert str(Limit(sin(x)/x, x, y)) == "Limit(sin(x)/x, x, y)"
    assert str(Limit(1/x, x, 0)) == "Limit(1/x, x, 0)"
    assert str(
        Limit(sin(x)/x, x, y, dir="-")) == "Limit(sin(x)/x, x, y, dir='-')"
Пример #19
0
 (r"\lceil x \rceil", ceiling(x)),
 (r"\langle x |", Bra('x')),
 (r"| x \rangle", Ket('x')),
 (r"\sin \theta", sin(theta)),
 (r"\sin(\theta)", sin(theta)),
 (r"\sin^{-1} a", asin(a)),
 (r"\sin a \cos b", _Mul(sin(a), cos(b))),
 (r"\sin \cos \theta", sin(cos(theta))),
 (r"\sin(\cos \theta)", sin(cos(theta))),
 (r"\frac{a}{b}", a / b),
 (r"\dfrac{a}{b}", a / b),
 (r"\tfrac{a}{b}", a / b),
 (r"\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))),
 (r"\frac{7}{3}", _Mul(7, _Pow(3, -1))),
 (r"(\csc x)(\sec y)", csc(x) * sec(y)),
 (r"\lim_{x \to 3} a", Limit(a, x, 3)),
 (r"\lim_{x \rightarrow 3} a", Limit(a, x, 3)),
 (r"\lim_{x \Rightarrow 3} a", Limit(a, x, 3)),
 (r"\lim_{x \longrightarrow 3} a", Limit(a, x, 3)),
 (r"\lim_{x \Longrightarrow 3} a", Limit(a, x, 3)),
 (r"\lim_{x \to 3^{+}} a", Limit(a, x, 3, dir='+')),
 (r"\lim_{x \to 3^{-}} a", Limit(a, x, 3, dir='-')),
 (r"\infty", oo),
 (r"\lim_{x \to \infty} \frac{1}{x}", Limit(_Pow(x, -1), x, oo)),
 (r"\frac{d}{dx} x", Derivative(x, x)),
 (r"\frac{d}{dt} x", Derivative(x, t)),
 (r"f(x)", f(x)),
 (r"f(x, y)", f(x, y)),
 (r"f(x, y, z)", f(x, y, z)),
 (r"\frac{d f(x)}{dx}", Derivative(f(x), x)),
 (r"\frac{d\theta(x)}{dx}", Derivative(Function('theta')(x), x)),
Пример #20
0
def test_Limit():
    assert Limit(sin(x)/x, x, 0) != 1
    assert Limit(sin(x)/x, x, 0).doit() == 1
    assert Limit(x, x, 0, dir='+-').args == (x, x, 0, Symbol('+-'))
Пример #21
0
def test_doit():
    f = Integral(2 * x, x)
    l = Limit(f, x, oo)
    assert l.doit() is oo
Пример #22
0
def test_series():
    e = Symbol("e")
    x = Symbol("x")
    for c in (Limit, Limit(e, x, 1), Order, Order(e)):
        check(c)
Пример #23
0
def test_basic1():
    assert limit(x, x, oo) is oo
    assert limit(x, x, -oo) is -oo
    assert limit(-x, x, oo) is -oo
    assert limit(x**2, x, -oo) is oo
    assert limit(-x**2, x, oo) is -oo
    assert limit(x*log(x), x, 0, dir="+") == 0
    assert limit(1/x, x, oo) == 0
    assert limit(exp(x), x, oo) is oo
    assert limit(-exp(x), x, oo) is -oo
    assert limit(exp(x)/x, x, oo) is oo
    assert limit(1/x - exp(-x), x, oo) == 0
    assert limit(x + 1/x, x, oo) is oo
    assert limit(x - x**2, x, oo) is -oo
    assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1
    assert limit((1 + x)**oo, x, 0) == Limit((x + 1)**oo, x, 0)
    assert limit((1 + x)**oo, x, 0, dir='-') == Limit((x + 1)**oo, x, 0, dir='-')
    assert limit((1 + x + y)**oo, x, 0, dir='-') == Limit((1 + x + y)**oo, x, 0, dir='-')
    assert limit(y/x/log(x), x, 0) == -oo*sign(y)
    assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo
    assert limit(gamma(1/x + 3), x, oo) == 2
    assert limit(S.NaN, x, -oo) is S.NaN
    assert limit(Order(2)*x, x, S.NaN) is S.NaN
    assert limit(1/(x - 1), x, 1, dir="+") is oo
    assert limit(1/(x - 1), x, 1, dir="-") is -oo
    assert limit(1/(5 - x)**3, x, 5, dir="+") is -oo
    assert limit(1/(5 - x)**3, x, 5, dir="-") is oo
    assert limit(1/sin(x), x, pi, dir="+") is -oo
    assert limit(1/sin(x), x, pi, dir="-") is oo
    assert limit(1/cos(x), x, pi/2, dir="+") is -oo
    assert limit(1/cos(x), x, pi/2, dir="-") is oo
    assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="+") is oo
    assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="-") is -oo
    assert limit(1/cot(x)**3, x, (pi*Rational(3, 2)), dir="+") is -oo
    assert limit(1/cot(x)**3, x, (pi*Rational(3, 2)), dir="-") is oo
    assert limit(tan(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
    assert limit(cot(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
    assert limit(sec(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
    assert limit(csc(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)

    # test bi-directional limits
    assert limit(sin(x)/x, x, 0, dir="+-") == 1
    assert limit(x**2, x, 0, dir="+-") == 0
    assert limit(1/x**2, x, 0, dir="+-") is oo

    # test failing bi-directional limits
    assert limit(1/x, x, 0, dir="+-") is zoo
    # approaching 0
    # from dir="+"
    assert limit(1 + 1/x, x, 0) is oo
    # from dir='-'
    # Add
    assert limit(1 + 1/x, x, 0, dir='-') is -oo
    # Pow
    assert limit(x**(-2), x, 0, dir='-') is oo
    assert limit(x**(-3), x, 0, dir='-') is -oo
    assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I
    assert limit(x**2, x, 0, dir='-') == 0
    assert limit(sqrt(x), x, 0, dir='-') == 0
    assert limit(x**-pi, x, 0, dir='-') == oo/(-1)**pi
    assert limit((1 + cos(x))**oo, x, 0) == Limit((cos(x) + 1)**oo, x, 0)

    # test pull request 22491
    assert limit(1/asin(x), x, 0, dir = '+') == oo
    assert limit(1/asin(x), x, 0, dir = '-') == -oo
    assert limit(1/sinh(x), x, 0, dir = '+') == oo
    assert limit(1/sinh(x), x, 0, dir = '-') == -oo
    assert limit(log(1/x) + 1/sin(x), x, 0, dir = '+') == oo
    assert limit(log(1/x) + 1/x, x, 0, dir = '+') == oo
Пример #24
0
    def contains(self, expr):
        r"""
        Return True if expr belongs to Order(self.expr, \*self.variables).
        Return False if self belongs to expr.
        Return None if the inclusion relation cannot be determined
        (e.g. when self and expr have different symbols).
        """
        from sympy import powsimp
        if expr is S.Zero:
            return True
        if expr is S.NaN:
            return False
        point = self.point[0] if self.point else S.Zero
        if expr.is_Order:
            if (any(p != point for p in expr.point)
                    or any(p != point for p in self.point)):
                return None
            if expr.expr == self.expr:
                # O(1) + O(1), O(1) + O(1, x), etc.
                return all([x in self.args[1:] for x in expr.args[1:]])
            if expr.expr.is_Add:
                return all([self.contains(x) for x in expr.expr.args])
            if self.expr.is_Add and point == S.Zero:
                return any([
                    self.func(x, *self.args[1:]).contains(expr)
                    for x in self.expr.args
                ])
            if self.variables and expr.variables:
                common_symbols = tuple(
                    [s for s in self.variables if s in expr.variables])
            elif self.variables:
                common_symbols = self.variables
            else:
                common_symbols = expr.variables
            if not common_symbols:
                return None
            if (self.expr.is_Power and len(self.variables) == 1
                    and self.variables == expr.variables):
                symbol = self.variables[0]
                other = expr.expr.as_independent(symbol, as_Add=False)[1]
                if (other.is_Power and other.base == symbol
                        and self.expr.base == symbol):
                    if point == S.Zero:
                        rv = (self.expr.exp - other.exp).is_nonpositive
                    if point.is_infinite:
                        rv = (self.expr.exp - other.exp).is_nonnegative
                    if rv is not None:
                        return rv

            r = None
            ratio = self.expr / expr.expr
            ratio = powsimp(ratio, deep=True, combine='exp')
            for s in common_symbols:
                from sympy.series.limits import Limit
                l = Limit(ratio, s, point).doit(heuristics=False)
                if not isinstance(l, Limit):
                    l = l != 0
                else:
                    l = None
                if r is None:
                    r = l
                else:
                    if r != l:
                        return
            return r

        if self.expr.is_Power and len(self.variables) == 1:
            symbol = self.variables[0]
            other = expr.as_independent(symbol, as_Add=False)[1]
            if (other.is_Power and other.base == symbol
                    and self.expr.base == symbol):
                if point == S.Zero:
                    rv = (self.expr.exp - other.exp).is_nonpositive
                if point.is_infinite:
                    rv = (self.expr.exp - other.exp).is_nonnegative
                if rv is not None:
                    return rv

        obj = self.func(expr, *self.args[1:])
        return self.contains(obj)
Пример #25
0
def lens_formula(focal_length=None, u=None, v=None):
    """
    This function provides one of the three parameters
    when two of them are supplied.
    This is valid only for paraxial rays.

    Parameters
    ==========

    focal_length : sympifiable
        Focal length of the mirror.
    u : sympifiable
        Distance of object from the optical center on
        the principal axis.
    v : sympifiable
        Distance of the image from the optical center
        on the principal axis.

    Examples
    ========

    >>> from sympy.physics.optics import lens_formula
    >>> from sympy.abc import f, u, v
    >>> lens_formula(focal_length=f, u=u)
    f*u/(f + u)
    >>> lens_formula(focal_length=f, v=v)
    f*v/(f - v)
    >>> lens_formula(u=u, v=v)
    u*v/(u - v)

    """
    if focal_length and u and v:
        raise ValueError("Please provide only two parameters")

    focal_length = sympify(focal_length)
    u = sympify(u)
    v = sympify(v)
    if u is oo:
        _u = Symbol('u')
    if v is oo:
        _v = Symbol('v')
    if focal_length is oo:
        _f = Symbol('f')
    if focal_length is None:
        if u is oo and v is oo:
            return Limit(Limit(_v * _u / (_u - _v), _u, oo), _v, oo).doit()
        if u is oo:
            return Limit(v * _u / (_u - v), _u, oo).doit()
        if v is oo:
            return Limit(_v * u / (u - _v), _v, oo).doit()
        return v * u / (u - v)
    if u is None:
        if v is oo and focal_length is oo:
            return Limit(Limit(_v * _f / (_f - _v), _v, oo), _f, oo).doit()
        if v is oo:
            return Limit(_v * focal_length / (focal_length - _v), _v,
                         oo).doit()
        if focal_length is oo:
            return Limit(v * _f / (_f - v), _f, oo).doit()
        return v * focal_length / (focal_length - v)
    if v is None:
        if u is oo and focal_length is oo:
            return Limit(Limit(_u * _f / (_u + _f), _u, oo), _f, oo).doit()
        if u is oo:
            return Limit(_u * focal_length / (_u + focal_length), _u,
                         oo).doit()
        if focal_length is oo:
            return Limit(u * _f / (u + _f), _f, oo).doit()
        return u * focal_length / (u + focal_length)