Esempio n. 1
0
def test_match_real_imag():
    x, y = symbols('x,y', real=True)
    i = Symbol('i', imaginary=True)
    assert match_real_imag(S.One) == (1, 0)
    assert match_real_imag(I) == (0, 1)
    assert match_real_imag(3 - 5 * I) == (3, -5)
    assert match_real_imag(-sqrt(3) + S.Half * I) == (-sqrt(3), S.Half)
    assert match_real_imag(x + y * I) == (x, y)
    assert match_real_imag(x * I + y * I) == (0, x + y)
    assert match_real_imag((x + y) * I) == (0, x + y)
    assert match_real_imag(Rational(-2, 3) * i * I) == (None, None)
    assert match_real_imag(1 - 2 * i) == (None, None)
    assert match_real_imag(sqrt(2) * (3 - 5 * I)) == (None, None)
Esempio n. 2
0
def _set_function(f, self):  # noqa:F811
    expr = f.expr
    if not isinstance(expr, Expr):
        return

    n = f.variables[0]
    if expr == abs(n):
        return S.Naturals0

    # f(x) + c and f(-x) + c cover the same integers
    # so choose the form that has the fewest negatives
    c = f(0)
    fx = f(n) - c
    f_x = f(-n) - c
    neg_count = lambda e: sum(_.could_extract_minus_sign()
                              for _ in Add.make_args(e))
    if neg_count(f_x) < neg_count(fx):
        expr = f_x + c

    a = Wild('a', exclude=[n])
    b = Wild('b', exclude=[n])
    match = expr.match(a * n + b)
    if match and match[a] and (not match[a].atoms(Float)
                               and not match[b].atoms(Float)):
        # canonical shift
        a, b = match[a], match[b]
        if a in [1, -1]:
            # drop integer addends in b
            nonint = []
            for bi in Add.make_args(b):
                if not bi.is_integer:
                    nonint.append(bi)
            b = Add(*nonint)
        if b.is_number and a.is_real:
            # avoid Mod for complex numbers, #11391
            br, bi = match_real_imag(b)
            if br and br.is_comparable and a.is_comparable:
                br %= a
                b = br + S.ImaginaryUnit * bi
        elif b.is_number and a.is_imaginary:
            br, bi = match_real_imag(b)
            ai = a / S.ImaginaryUnit
            if bi and bi.is_comparable and ai.is_comparable:
                bi %= ai
                b = br + S.ImaginaryUnit * bi
        expr = a * n + b

    if expr != f.expr:
        return ImageSet(Lambda(n, expr), S.Integers)
Esempio n. 3
0
    def eval(cls, arg):
        from sympy import atan

        arg = sympify(arg)

        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg.is_zero:
                return S.Zero
            elif arg is S.One:
                return S.Infinity
            elif arg is S.NegativeOne:
                return S.NegativeInfinity
            elif arg is S.Infinity:
                return -S.ImaginaryUnit * atan(arg)
            elif arg is S.NegativeInfinity:
                return S.ImaginaryUnit * atan(-arg)
            elif arg.is_negative:
                return -cls(-arg)
        else:
            if arg is S.ComplexInfinity:
                from sympy.calculus.util import AccumBounds

                return S.ImaginaryUnit * AccumBounds(-S.Pi / 2, S.Pi / 2)

            i_coeff = arg.as_coefficient(S.ImaginaryUnit)

            if i_coeff is not None:
                return S.ImaginaryUnit * atan(i_coeff)
            else:
                if _coeff_isneg(arg):
                    return -cls(-arg)

        if arg.is_zero:
            return S.Zero

        if isinstance(arg, tanh) and arg.args[0].is_number:
            z = arg.args[0]
            if z.is_real:
                return z
            r, i = match_real_imag(z)
            if r is not None and i is not None:
                f = floor(2 * i / pi)
                even = f.is_even
                m = z - I * f * pi / 2
                if even is True:
                    return m
                elif even is False:
                    return m - I * pi / 2
Esempio n. 4
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_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

            if arg.is_zero:
                return S.Zero

            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)

        if isinstance(arg, sinh) and arg.args[0].is_number:
            z = arg.args[0]
            if z.is_real:
                return z
            r, i = match_real_imag(z)
            if r is not None and i is not None:
                f = floor((i + pi / 2) / pi)
                m = z - I * pi * f
                even = f.is_even
                if even is True:
                    return m
                elif even is False:
                    return -m
Esempio n. 5
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.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_extended_real:
                    return cst_table[arg] * S.ImaginaryUnit
                return cst_table[arg]

        if arg is S.ComplexInfinity:
            return S.ComplexInfinity
        if arg == S.ImaginaryUnit * S.Infinity:
            return S.Infinity + S.ImaginaryUnit * S.Pi / 2
        if arg == -S.ImaginaryUnit * S.Infinity:
            return S.Infinity - S.ImaginaryUnit * S.Pi / 2

        if isinstance(arg, cosh) and arg.args[0].is_number:
            z = arg.args[0]
            if z.is_real:
                from sympy.functions.elementary.complexes import Abs
                return Abs(z)
            r, i = match_real_imag(z)
            if r is not None and i is not None:
                f = floor(i / pi)
                m = z - I * pi * f
                even = f.is_even
                if even is True:
                    if r.is_nonnegative:
                        return m
                    elif r.is_negative:
                        return -m
                elif even is False:
                    m -= I * pi
                    if r.is_nonpositive:
                        return -m
                    elif r.is_positive:
                        return m