Example #1
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Zero:
                return S.Zero

        if arg.could_extract_minus_sign():
            return -cls(-arg)

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return S.ImaginaryUnit * C.tanh(i_coeff)

        pi_coeff = arg.as_coefficient(S.Pi)
        if pi_coeff is not None:
            if pi_coeff.is_integer:
                return S.Zero
            elif pi_coeff.is_Rational:
                cst_table = {
                   #2 : S.ComplexInfinity,
                    3 : sqrt(3),
                    4 : S.One,
                    6 : 1 / sqrt(3),
                }

                try:
                    result = cst_table[pi_coeff.q]

                    if (2*pi_coeff.p // pi_coeff.q) % 4 in (1, 3):
                        return -result
                    else:
                        return result
                except KeyError:
                    pass

        if arg.is_Add:
            x, m = _peeloff_pi(arg)
            if m:
                if (m*2/S.Pi) % 2 == 0:
                    return tan(x)
                else:
                    return -cot(x)

        if arg.func is atan:
            return arg.args[0]

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

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

        if arg.func is acot:
            x = arg.args[0]
            return 1 / x
Example #2
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Zero:
                return S.Zero

        if arg.could_extract_minus_sign():
            return -cls(-arg)

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return S.ImaginaryUnit * C.tanh(i_coeff)

        pi_coeff = arg.as_coefficient(S.Pi)
        if pi_coeff is not None:
            if pi_coeff.is_integer:
                return S.Zero
            elif pi_coeff.is_Rational:
                cst_table = {
                    2: S.ComplexInfinity,
                    3: sqrt(3),
                    4: S.One,
                    6: 1 / sqrt(3),
                }

                try:
                    result = cst_table[pi_coeff.q]

                    if (2 * pi_coeff.p // pi_coeff.q) % 4 in (1, 3):
                        return -result
                    else:
                        return result
                except KeyError:
                    pass

        if arg.is_Add:
            x, m = _peeloff_pi(arg)
            if m:
                if (m * 2 / S.Pi) % 2 == 0:
                    return tan(x)
                else:
                    return -cot(x)

        if arg.func is atan:
            return arg.args[0]

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

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

        if arg.func is acot:
            x = arg.args[0]
            return 1 / x
Example #3
0
    def canonize(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            #elif arg is S.Zero:
            #    return S.ComplexInfinity
            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.coth(i_coeff)
            else:
                pi_coeff = arg.as_coefficient(S.Pi)

                if pi_coeff is not None:
                    #if pi_coeff.is_integer:
                    #    return S.ComplexInfinity
                    if pi_coeff.is_Rational:
                        cst_table = {
                            2 : S.Zero,
                            3 : 1 / sqrt(3),
                            4 : S.One,
                            6 : sqrt(3)
                        }

                        try:
                            result = cst_table[pi_coeff.q]

                            if (2*pi_coeff.p // pi_coeff.q) % 4 in (1, 3):
                                return -result
                            else:
                                return result
                        except KeyError:
                            pass

                coeff, terms = arg.as_coeff_terms()

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

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

        if isinstance(arg, atan):
            x = arg.args[0]
            return 1 / x

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

        if isinstance(arg, acos):
            x = arg.args[0]
            return x / sqrt(1 - x**2)
Example #4
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            #elif arg is S.Zero:
            #    return S.ComplexInfinity
            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.coth(i_coeff)
            else:
                pi_coeff = arg.as_coefficient(S.Pi)

                if pi_coeff is not None:
                    #if pi_coeff.is_integer:
                    #    return S.ComplexInfinity
                    if pi_coeff.is_Rational:
                        cst_table = {
                            2: S.Zero,
                            3: 1 / sqrt(3),
                            4: S.One,
                            6: sqrt(3)
                        }

                        try:
                            result = cst_table[pi_coeff.q]

                            if (2 * pi_coeff.p // pi_coeff.q) % 4 in (1, 3):
                                return -result
                            else:
                                return result
                        except KeyError:
                            pass

                coeff, terms = arg.as_coeff_terms()

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

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

        if isinstance(arg, atan):
            x = arg.args[0]
            return 1 / x

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

        if isinstance(arg, acos):
            x = arg.args[0]
            return x / sqrt(1 - x**2)
Example #5
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
        if arg.could_extract_minus_sign():
            return -cls(-arg)

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return -S.ImaginaryUnit * C.coth(i_coeff)

        pi_coeff = arg.as_coefficient(S.Pi)
        if pi_coeff is not None:
            if pi_coeff.is_Rational:
                cst_table = {
                    2 : S.Zero,
                    3 : 1 / sqrt(3),
                    4 : S.One,
                    6 : sqrt(3)
                }

                try:
                    result = cst_table[pi_coeff.q]

                    if (2*pi_coeff.p // pi_coeff.q) % 4 in (1, 3):
                        return -result
                    else:
                        return result
                except KeyError:
                    pass

        if arg.func is acot:
            return arg.args[0]

        if arg.func is atan:
            x = arg.args[0]
            return 1 / x

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

        if arg.func is acos:
            x = arg.args[0]
            return x / sqrt(1 - x**2)
Example #6
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Zero
            elif arg is S.NegativeInfinity:
                return S.Zero
            elif arg is S.Zero:
                return S.Pi / 2
            elif arg is S.One:
                return S.Pi / 4
            elif arg is S.NegativeOne:
                return -S.Pi / 4

        if arg.could_extract_minus_sign():
            return -cls(-arg)

        if arg.is_number:
            cst_table = {
                sqrt(3) / 3: 3,
                -sqrt(3) / 3: -3,
                1 / sqrt(3): 3,
                -1 / sqrt(3): -3,
                sqrt(3): 6,
                -sqrt(3): -6,
            }

            if arg in cst_table:
                return S.Pi / cst_table[arg]

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return -S.ImaginaryUnit * C.acoth(i_coeff)
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Infinity * S.ImaginaryUnit
            elif arg is S.NegativeInfinity:
                return S.NegativeInfinity * S.ImaginaryUnit
            elif arg is S.Zero:
                return S.Pi / 2
            elif arg is S.One:
                return S.Zero
            elif arg is S.NegativeOne:
                return S.Pi

        if arg.is_number:
            cst_table = {
                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,
            }

            if arg in cst_table:
                return cst_table[arg]
Example #8
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 * S.ImaginaryUnit
            elif arg is S.NegativeInfinity:
                return S.NegativeInfinity * S.ImaginaryUnit
            elif arg is S.Zero:
                return S.Pi / 2
            elif arg is S.One:
                return S.Zero
            elif arg is S.NegativeOne:
                return S.Pi

        if arg.is_number:
            cst_table = {
                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,
                }

            if arg in cst_table:
                return cst_table[arg]
Example #9
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Pi / 2
            elif arg is S.NegativeInfinity:
                return -S.Pi / 2
            elif arg is S.Zero:
                return S.Zero
            elif arg is S.One:
                return S.Pi / 4
            elif arg is S.NegativeOne:
                return -S.Pi / 4
        if arg.could_extract_minus_sign():
            return -cls(-arg)

        if arg.is_number:
            cst_table = {
                sqrt(3)/3  : 6,
                -sqrt(3)/3 : -6,
                1/sqrt(3)  : 6,
                -1/sqrt(3) : -6,
                sqrt(3)    : 3,
                -sqrt(3)   : -3,
                }

            if arg in cst_table:
                return S.Pi / cst_table[arg]

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return S.ImaginaryUnit * C.atanh(i_coeff)
Example #10
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
        if arg.could_extract_minus_sign():
            return -cls(-arg)

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return -S.ImaginaryUnit * C.coth(i_coeff)

        pi_coeff = arg.as_coefficient(S.Pi)
        if pi_coeff is not None:
            if pi_coeff.is_Rational:
                cst_table = {2: S.Zero, 3: 1 / sqrt(3), 4: S.One, 6: sqrt(3)}

                try:
                    result = cst_table[pi_coeff.q]

                    if (2 * pi_coeff.p // pi_coeff.q) % 4 in (1, 3):
                        return -result
                    else:
                        return result
                except KeyError:
                    pass

        if arg.func is acot:
            return arg.args[0]

        if arg.func is atan:
            x = arg.args[0]
            return 1 / x

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

        if arg.func is acos:
            x = arg.args[0]
            return x / sqrt(1 - x**2)
Example #11
0
    def canonize(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.NegativeInfinity * S.ImaginaryUnit
            elif arg is S.NegativeInfinity:
                return S.Infinity * S.ImaginaryUnit
            elif arg is S.Zero:
                return S.Zero
            elif arg is S.One:
                return S.Pi / 2
            elif arg is S.NegativeOne:
                return -S.Pi / 2

        if arg.is_number:
            cst_table = {
                S.Half     : 6,
                -S.Half    : -6,
                sqrt(2)/2  : 4,
                -sqrt(2)/2 : -4,
                1/sqrt(2)  : 4,
                -1/sqrt(2) : -4,
                sqrt(3)/2  : 3,
                -sqrt(3)/2 : -3,
                }

            if arg in cst_table:
                return S.Pi / cst_table[arg]
            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.asinh(i_coeff)
            else:
                coeff, terms = arg.as_coeff_terms()

                if coeff.is_negative:
                    return -cls(-arg)
Example #12
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.NegativeInfinity * S.ImaginaryUnit
            elif arg is S.NegativeInfinity:
                return S.Infinity * S.ImaginaryUnit
            elif arg is S.Zero:
                return S.Zero
            elif arg is S.One:
                return S.Pi / 2
            elif arg is S.NegativeOne:
                return -S.Pi / 2

        if arg.is_number:
            cst_table = {
                S.Half: 6,
                -S.Half: -6,
                sqrt(2) / 2: 4,
                -sqrt(2) / 2: -4,
                1 / sqrt(2): 4,
                -1 / sqrt(2): -4,
                sqrt(3) / 2: 3,
                -sqrt(3) / 2: -3,
            }

            if arg in cst_table:
                return S.Pi / cst_table[arg]
            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.asinh(i_coeff)
            else:
                coeff, terms = arg.as_coeff_terms()

                if coeff.is_negative:
                    return -cls(-arg)
Example #13
0
    def canonize(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Zero
            elif arg is S.NegativeInfinity:
                return S.Zero
            elif arg is S.Zero:
                return S.Pi/ 2
            elif arg is S.One:
                return S.Pi / 4
            elif arg is S.NegativeOne:
                return -S.Pi / 4

        if arg.is_number:
            cst_table = {
                sqrt(3)/3  : 3,
                -sqrt(3)/3 : -3,
                1/sqrt(3)  : 3,
                -1/sqrt(3) : -3,
                sqrt(3)    : 6,
                -sqrt(3)   : -6,
                }

            if arg in cst_table:
                return S.Pi / cst_table[arg]
            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.acoth(i_coeff)
            else:
                coeff, terms = arg.as_coeff_terms()

                if coeff.is_negative:
                    return -cls(-arg)
Example #14
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Zero
            elif arg is S.NegativeInfinity:
                return S.Zero
            elif arg is S.Zero:
                return S.Pi / 2
            elif arg is S.One:
                return S.Pi / 4
            elif arg is S.NegativeOne:
                return -S.Pi / 4

        if arg.is_number:
            cst_table = {
                sqrt(3) / 3: 3,
                -sqrt(3) / 3: -3,
                1 / sqrt(3): 3,
                -1 / sqrt(3): -3,
                sqrt(3): 6,
                -sqrt(3): -6,
            }

            if arg in cst_table:
                return S.Pi / cst_table[arg]
            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.acoth(i_coeff)
            else:
                coeff, terms = arg.as_coeff_terms()

                if coeff.is_negative:
                    return -cls(-arg)
Example #15
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.NegativeInfinity * S.ImaginaryUnit
            elif arg is S.NegativeInfinity:
                return S.Infinity * S.ImaginaryUnit
            elif arg is S.Zero:
                return S.Zero
            elif arg is S.One:
                return S.Pi / 2
            elif arg is S.NegativeOne:
                return -S.Pi / 2

        if arg.could_extract_minus_sign():
            return -cls(-arg)

        if arg.is_number:
            cst_table = {
                S.Half: 6,
                -S.Half: -6,
                sqrt(2) / 2: 4,
                -sqrt(2) / 2: -4,
                1 / sqrt(2): 4,
                -1 / sqrt(2): -4,
                sqrt(3) / 2: 3,
                -sqrt(3) / 2: -3,
            }

            if arg in cst_table:
                return S.Pi / cst_table[arg]

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return S.ImaginaryUnit * C.asinh(i_coeff)
Example #16
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            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.sinh(i_coeff)
            else:
                pi_coeff = arg.as_coefficient(S.Pi)

                if pi_coeff is not None:
                    if pi_coeff.is_integer:
                        return S.Zero
                    elif pi_coeff.is_Rational:
                        cst_table_some = {
                            2: S.One,
                            3: S.Half * sqrt(3),
                            4: S.Half * sqrt(2),
                            6: S.Half,
                        }

                        cst_table_more = {
                            (1, 5): sqrt((5 - sqrt(5)) / 8),
                            (2, 5): sqrt((5 + sqrt(5)) / 8)
                        }

                        p = pi_coeff.p
                        q = pi_coeff.q

                        Q, P = p // q, p % q

                        try:
                            result = cst_table_some[q]
                        except KeyError:
                            if abs(P) > q // 2:
                                P = q - P

                            try:
                                result = cst_table_more[(P, q)]
                            except KeyError:
                                if P != p:
                                    result = cls(C.Rational(P, q) * S.Pi)
                                else:
                                    return None

                        if Q % 2 == 1:
                            return -result
                        else:
                            return result

                if arg.is_Mul and arg.args[0].is_negative:
                    return -cls(-arg)
                if arg.is_Add:
                    x, m = arg.as_independent(S.Pi)
                    if m in [S.Pi / 2, S.Pi]:
                        return sin(m) * cos(x) + cos(m) * sin(x)
                    # normalize sin(-x-y) to -sin(x+y)
                    if arg.args[0].is_Mul:
                        if arg.args[0].args[0].is_negative:
                            # e.g. arg = -x - y
                            if (-arg).args[0].is_Mul:
                                if (-arg).args[0].args[0].is_negative:
                                    # This is to prevent infinite recursion in
                                    # the case sin(-x+y), for which
                                    # -arg = -y + x. See also #838 for the
                                    # root of the problem here.
                                    return
                            # convert sin(-x-y) to -sin(x+y)
                            return -cls(-arg)
                    if arg.args[0].is_negative:
                        if (-arg).args[0].is_negative:
                            # This is to avoid infinite recursion in the case
                            # sin(-x-1)
                            return
                        return -cls(-arg)

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

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

            if isinstance(arg, acos):
                x = arg.args[0]
                return sqrt(1 - x**2)

            if isinstance(arg, acot):
                x = arg.args[0]
                return 1 / (sqrt(1 + 1 / x**2) * x)
Example #17
0
 def fdiff(self, argindex=1):
     if argindex == 1:
         return 1/sqrt(1 - self.args[0]**2)
     else:
         raise ArgumentIndexError(self, argindex)
Example #18
0
 def _eval_rewrite_as_atan(self, x):
     return 2*atan(x/(1 + sqrt(1 - x**2)))
Example #19
0
    def canonize(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            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.sinh(i_coeff)
            else:
                pi_coeff = arg.as_coefficient(S.Pi)

                if pi_coeff is not None:
                    if pi_coeff.is_integer:
                        return S.Zero
                    elif pi_coeff.is_Rational:
                        cst_table = {
                            2: S.One,
                            3: S.Half * sqrt(3),
                            4: S.Half * sqrt(2),
                            6: S.Half,
                        }

                        try:
                            result = cst_table[pi_coeff.q]

                            if (pi_coeff.p // pi_coeff.q) % 2 == 1:
                                return -result
                            else:
                                return result
                        except KeyError:
                            pass

                if arg.is_Mul and arg.args[0].is_negative:
                    return -cls(-arg)
                if arg.is_Add:
                    x, m = arg.as_independent(S.Pi)
                    if m in [S.Pi / 2, S.Pi]:
                        return sin(m) * cos(x) + cos(m) * sin(x)
                    # normalize sin(-x-y) to -sin(x+y)
                    if arg.args[0].is_Mul:
                        if arg.args[0].args[0].is_negative:
                            # e.g. arg = -x - y
                            if (-arg).args[0].is_Mul:
                                if (-arg).args[0].args[0].is_negative:
                                    # This is to prevent infinite recursion in
                                    # the case sin(-x+y), for which
                                    # -arg = -y + x. See also #838 for the
                                    # root of the problem here.
                                    return
                            # convert sin(-x-y) to -sin(x+y)
                            return -cls(-arg)
                    if arg.args[0].is_negative:
                        if (-arg).args[0].is_negative:
                            # This is to avoid infinite recursion in the case
                            # sin(-x-1)
                            return
                        return -cls(-arg)

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

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

            if isinstance(arg, acos):
                x = arg.args[0]
                return sqrt(1 - x**2)

            if isinstance(arg, acot):
                x = arg.args[0]
                return 1 / (sqrt(1 + 1 / x**2) * x)
Example #20
0
    def canonize(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            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.sinh(i_coeff)
            else:
                pi_coeff = arg.as_coefficient(S.Pi)

                if pi_coeff is not None:
                    if pi_coeff.is_integer:
                        return S.Zero
                    elif pi_coeff.is_Rational:
                        cst_table = {
                            2 : S.One,
                            3 : S.Half*sqrt(3),
                            4 : S.Half*sqrt(2),
                            6 : S.Half,
                        }

                        try:
                            result = cst_table[pi_coeff.q]

                            if (pi_coeff.p // pi_coeff.q) % 2 == 1:
                                return -result
                            else:
                                return result
                        except KeyError:
                            pass

                if arg.is_Mul and arg.args[0].is_negative:
                    return -cls(-arg)
                if arg.is_Add:
                    x, m = arg.as_independent(S.Pi)
                    if m in [S.Pi/2, S.Pi]:
                        return sin(m)*cos(x)+cos(m)*sin(x)
                    # normalize sin(-x-y) to -sin(x+y)
                    if arg.args[0].is_Mul:
                        if arg.args[0].args[0].is_negative:
                            # e.g. arg = -x - y
                            if (-arg).args[0].is_Mul:
                                if (-arg).args[0].args[0].is_negative:
                                    # This is to prevent infinite recursion in
                                    # the case sin(-x+y), for which
                                    # -arg = -y + x. See also #838 for the
                                    # root of the problem here.
                                    return
                            # convert sin(-x-y) to -sin(x+y)
                            return -cls(-arg)
                    if arg.args[0].is_negative:
                        if (-arg).args[0].is_negative:
                            # This is to avoid infinite recursion in the case
                            # sin(-x-1)
                            return
                        return -cls(-arg)

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

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

            if isinstance(arg, acos):
                x = arg.args[0]
                return sqrt(1 - x**2)

            if isinstance(arg, acot):
                x = arg.args[0];
                return 1 / (sqrt(1 + 1 / x**2) * x)
Example #21
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            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.sinh(i_coeff)
            else:
                pi_coeff = arg.as_coefficient(S.Pi)

                if pi_coeff is not None:
                    if pi_coeff.is_integer:
                        return S.Zero
                    elif pi_coeff.is_Rational:
                        cst_table_some = {
                            2 : S.One,
                            3 : S.Half*sqrt(3),
                            4 : S.Half*sqrt(2),
                            6 : S.Half,
                        }

                        cst_table_more = {
                            (1, 5) : sqrt((5 - sqrt(5)) / 8),
                            (2, 5) : sqrt((5 + sqrt(5)) / 8)
                        }

                        p = pi_coeff.p
                        q = pi_coeff.q

                        Q, P = p // q, p % q

                        try:
                            result = cst_table_some[q]
                        except KeyError:
                            if abs(P) > q // 2:
                                P = q - P

                            try:
                                result = cst_table_more[(P, q)]
                            except KeyError:
                                if P != p:
                                    result = cls(C.Rational(P, q)*S.Pi)
                                else:
                                    return None

                        if Q % 2 == 1:
                            return -result
                        else:
                            return result

                if arg.is_Mul and arg.args[0].is_negative:
                    return -cls(-arg)
                if arg.is_Add:
                    x, m = arg.as_independent(S.Pi)
                    if m in [S.Pi/2, S.Pi]:
                        return sin(m)*cos(x)+cos(m)*sin(x)
                    # normalize sin(-x-y) to -sin(x+y)
                    if arg.args[0].is_Mul:
                        if arg.args[0].args[0].is_negative:
                            # e.g. arg = -x - y
                            if (-arg).args[0].is_Mul:
                                if (-arg).args[0].args[0].is_negative:
                                    # This is to prevent infinite recursion in
                                    # the case sin(-x+y), for which
                                    # -arg = -y + x. See also #838 for the
                                    # root of the problem here.
                                    return
                            # convert sin(-x-y) to -sin(x+y)
                            return -cls(-arg)
                    if arg.args[0].is_negative:
                        if (-arg).args[0].is_negative:
                            # This is to avoid infinite recursion in the case
                            # sin(-x-1)
                            return
                        return -cls(-arg)

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

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

            if isinstance(arg, acos):
                x = arg.args[0]
                return sqrt(1 - x**2)

            if isinstance(arg, acot):
                x = arg.args[0];
                return 1 / (sqrt(1 + 1 / x**2) * x)
 def _eval_rewrite_as_atan(self, x):
     return 2 * atan(x / (1 + sqrt(1 - x**2)))
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            if arg is S.Zero:
                return S.ComplexInfinity

        if arg.could_extract_minus_sign():
            return -cls(-arg)

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return -S.ImaginaryUnit * C.coth(i_coeff)

        pi_coeff = _pi_coeff(arg, 2)
        if pi_coeff is not None:
            if pi_coeff.is_integer:
                return S.ComplexInfinity

            if not pi_coeff.is_Rational:
                narg = pi_coeff * S.Pi
                if narg != arg:
                    return cls(narg)
                return None

            cst_table = {2: S.Zero, 3: 1 / sqrt(3), 4: S.One, 6: sqrt(3)}

            try:
                result = cst_table[pi_coeff.q]

                if (2 * pi_coeff.p // pi_coeff.q) % 4 in (1, 3):
                    return -result
                else:
                    return result
            except KeyError:
                if pi_coeff.p > pi_coeff.q:
                    p, q = pi_coeff.p % pi_coeff.q, pi_coeff.q
                    if 2 * p > q:
                        return -cls(Rational(q - p, q) * S.Pi)
                    return cls(Rational(p, q) * S.Pi)

        if arg.is_Add:
            x, m = _peeloff_pi(arg)
            if m:
                if (m * 2 / S.Pi) % 2 == 0:
                    return cot(x)
                else:
                    return -tan(x)

        if arg.func is acot:
            return arg.args[0]

        if arg.func is atan:
            x = arg.args[0]
            return 1 / x

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

        if arg.func is acos:
            x = arg.args[0]
            return x / sqrt(1 - x**2)
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.NegativeInfinity * S.ImaginaryUnit
            elif arg is S.NegativeInfinity:
                return S.Infinity * S.ImaginaryUnit
            elif arg is S.Zero:
                return S.Zero
            elif arg is S.One:
                return S.Pi / 2
            elif arg is S.NegativeOne:
                return -S.Pi / 2

        if arg.could_extract_minus_sign():
            return -cls(-arg)

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

            if arg in cst_table:
                return S.Pi / cst_table[arg]

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return S.ImaginaryUnit * C.asinh(i_coeff)
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Zero:
                return S.One
            elif arg is S.Infinity:
                return

        if arg.could_extract_minus_sign():
            return cls(-arg)

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return C.cosh(i_coeff)

        pi_coeff = _pi_coeff(arg)
        if pi_coeff is not None:
            if not pi_coeff.is_Rational:
                if pi_coeff.is_integer:
                    even = pi_coeff.is_even
                    if even:
                        return S.One
                    elif even is False:
                        return S.NegativeOne
                narg = pi_coeff * S.Pi
                if narg != arg:
                    return cls(narg)
                return None

            cst_table_some = {
                1: S.One,
                2: S.Zero,
                3: S.Half,
                4: S.Half * sqrt(2),
                6: S.Half * sqrt(3),
            }

            cst_table_more = {
                (1, 5): (sqrt(5) + 1) / 4,
                (2, 5): (sqrt(5) - 1) / 4
            }

            p = pi_coeff.p
            q = pi_coeff.q

            Q, P = 2 * p // q, p % q

            try:
                result = cst_table_some[q]
            except KeyError:
                if abs(P) > q // 2:
                    P = q - P

                try:
                    result = cst_table_more[(P, q)]
                except KeyError:
                    if P != p:
                        result = cls(C.Rational(P, q) * S.Pi)
                    else:
                        return None

            if Q % 4 in (1, 2):
                return -result
            else:
                return result

        if arg.is_Add:
            x, m = _peeloff_pi(arg)
            if m:
                return cos(m) * cos(x) - sin(m) * sin(x)

        if arg.func is acos:
            return arg.args[0]

        if arg.func is atan:
            x = arg.args[0]
            return 1 / sqrt(1 + x**2)

        if arg.func is asin:
            x = arg.args[0]
            return sqrt(1 - x**2)

        if arg.func is acot:
            x = arg.args[0]
            return 1 / sqrt(1 + 1 / x**2)
Example #26
0
 def _eval_rewrite_as_log(self, x):
     return S.Pi/2 + S.ImaginaryUnit * C.log(S.ImaginaryUnit * x + sqrt(1 - x**2))
Example #27
0
 def fdiff(self, argindex=1):
     if argindex == 1:
         return 1/sqrt(1 - self.args[0]**2)
     else:
         raise ArgumentIndexError(self, argindex)
Example #28
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Zero:
                return S.One
            elif arg is S.Infinity:
                return

        if arg.could_extract_minus_sign():
            return cls(-arg)

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return C.cosh(i_coeff)

        pi_coeff = _pi_coeff(arg)
        if pi_coeff is not None:
            if not pi_coeff.is_Rational:
                if pi_coeff.is_integer:
                    return (S.NegativeOne)**pi_coeff
                narg = pi_coeff*S.Pi
                if narg != arg:
                    return cls(narg)
                return None

            cst_table_some = {
                1 : S.One,
                2 : S.Zero,
                3 : S.Half,
                4 : S.Half*sqrt(2),
                6 : S.Half*sqrt(3),
            }

            cst_table_more = {
                (1, 5) : (sqrt(5) + 1)/4,
                (2, 5) : (sqrt(5) - 1)/4
            }

            p = pi_coeff.p
            q = pi_coeff.q

            Q, P = 2*p // q, p % q

            try:
                result = cst_table_some[q]
            except KeyError:
                if abs(P) > q // 2:
                    P = q - P

                try:
                    result = cst_table_more[(P, q)]
                except KeyError:
                    if P != p:
                        result = cls(C.Rational(P, q)*S.Pi)
                    else:
                        return None

            if Q % 4 in (1, 2):
                return -result
            else:
                return result

        if arg.is_Add:
            x, m = _peeloff_pi(arg)
            if m:
                return cos(m)*cos(x)-sin(m)*sin(x)

        if arg.func is acos:
            return arg.args[0]

        if arg.func is atan:
            x = arg.args[0]
            return 1 / sqrt(1 + x**2)

        if arg.func is asin:
            x = arg.args[0]
            return sqrt(1 - x ** 2)

        if arg.func is acot:
            x = arg.args[0]
            return 1 / sqrt(1 + 1 / x**2)
Example #29
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Zero
            elif arg is S.NegativeInfinity:
                return S.Zero
            elif arg is S.Zero:
                return S.Pi/ 2
            elif arg is S.One:
                return S.Pi / 4
            elif arg is S.NegativeOne:
                return -S.Pi / 4

        if arg.could_extract_minus_sign():
            return -cls(-arg)

        if arg.is_number:
            cst_table = {
                sqrt(3)/3  : 3,
                -sqrt(3)/3 : -3,
                1/sqrt(3)  : 3,
                -1/sqrt(3) : -3,
                sqrt(3)    : 6,
                -sqrt(3)   : -6,
                (1+sqrt(2)) : 8,
                -(1+sqrt(2)) : -8,
                (1-sqrt(2)) : -S(8)/3,
                (sqrt(2)-1) : S(8)/3,
                sqrt(5+2*sqrt(5)) : 10,
                -sqrt(5+2*sqrt(5)) : -10,
                (2+sqrt(3)) : 12,
                -(2+sqrt(3)) : -12,
                (2-sqrt(3)) : S(12)/5,
                -(2-sqrt(3)) : -S(12)/5,
                }

            if arg in cst_table:
                return S.Pi / cst_table[arg]

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return -S.ImaginaryUnit * C.acoth(i_coeff)
Example #30
0
 def _eval_rewrite_as_atan(self, x):
     if x > -1 and x <= 1:
         return 2 * atan(sqrt(1 - x**2)/(1 + x))
     else:
         raise ValueError("The argument must be bounded in the interval (-1,1]")
 def _eval_rewrite_as_log(self, x):
     return S.Pi / 2 + S.ImaginaryUnit * C.log(S.ImaginaryUnit * x +
                                               sqrt(1 - x**2))
Example #32
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Zero:
                return S.Zero
            elif arg is S.Infinity:
                return

        if arg.could_extract_minus_sign():
            return -cls(-arg)

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return S.ImaginaryUnit * C.sinh(i_coeff)

        pi_coeff = _pi_coeff(arg)
        if pi_coeff is not None:
            if pi_coeff.is_integer:
                return S.Zero

            if not pi_coeff.is_Rational:
                narg = pi_coeff*S.Pi
                if narg != arg:
                    return cls(narg)
                return None

            cst_table_some = {
                2 : S.One,
                3 : S.Half*sqrt(3),
                4 : S.Half*sqrt(2),
                6 : S.Half,
            }

            cst_table_more = {
                (1, 5) : sqrt((5 - sqrt(5)) / 8),
                (2, 5) : sqrt((5 + sqrt(5)) / 8)
            }

            p = pi_coeff.p
            q = pi_coeff.q

            Q, P = p // q, p % q

            try:
                result = cst_table_some[q]
            except KeyError:
                if abs(P) > q // 2:
                    P = q - P

                try:
                    result = cst_table_more[(P, q)]
                except KeyError:
                    if P != p:
                        result = cls(C.Rational(P, q)*S.Pi)
                    else:
                        return None

            if Q % 2 == 1:
                return -result
            else:
                return result

        if arg.is_Add:
            x, m = _peeloff_pi(arg)
            if m:
                return sin(m)*cos(x)+cos(m)*sin(x)

        if arg.func is asin:
            return arg.args[0]

        if arg.func is atan:
            x = arg.args[0]
            return x / sqrt(1 + x**2)

        if arg.func is acos:
            x = arg.args[0]
            return sqrt(1 - x**2)

        if arg.func is acot:
            x = arg.args[0];
            return 1 / (sqrt(1 + 1 / x**2) * x)
 def _eval_rewrite_as_atan(self, x):
     if x > -1 and x <= 1:
         return 2 * atan(sqrt(1 - x**2) / (1 + x))
     else:
         raise ValueError(
             "The argument must be bounded in the interval (-1,1]")
Example #34
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            if arg is S.Zero:
                return S.ComplexInfinity

        if arg.could_extract_minus_sign():
            return -cls(-arg)

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return -S.ImaginaryUnit * C.coth(i_coeff)

        pi_coeff = _pi_coeff(arg, 2)
        if pi_coeff is not None:
            if pi_coeff.is_integer:
                return S.ComplexInfinity

            if not pi_coeff.is_Rational:
                narg = pi_coeff*S.Pi
                if narg != arg:
                    return cls(narg)
                return None

            cst_table = {
                2 : S.Zero,
                3 : 1 / sqrt(3),
                4 : S.One,
                6 : sqrt(3)
            }

            try:
                result = cst_table[pi_coeff.q]

                if (2*pi_coeff.p // pi_coeff.q) % 4 in (1, 3):
                    return -result
                else:
                    return result
            except KeyError:
                if pi_coeff.p > pi_coeff.q:
                    p, q = pi_coeff.p % pi_coeff.q, pi_coeff.q
                    if 2 * p > q:
                        return -cls(Rational(q - p, q)*S.Pi)
                    return cls(Rational(p, q)*S.Pi)

        if arg.is_Add:
            x, m = _peeloff_pi(arg)
            if m:
                if (m*2/S.Pi) % 2 == 0:
                    return cot(x)
                else:
                    return -tan(x)

        if arg.func is acot:
            return arg.args[0]

        if arg.func is atan:
            x = arg.args[0]
            return 1 / x

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

        if arg.func is acos:
            x = arg.args[0]
            return x / sqrt(1 - x**2)
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.Zero
            elif arg is S.NegativeInfinity:
                return S.Zero
            elif arg is S.Zero:
                return S.Pi / 2
            elif arg is S.One:
                return S.Pi / 4
            elif arg is S.NegativeOne:
                return -S.Pi / 4

        if arg.could_extract_minus_sign():
            return -cls(-arg)

        if arg.is_number:
            cst_table = {
                sqrt(3) / 3: 3,
                -sqrt(3) / 3: -3,
                1 / sqrt(3): 3,
                -1 / sqrt(3): -3,
                sqrt(3): 6,
                -sqrt(3): -6,
                (1 + sqrt(2)): 8,
                -(1 + sqrt(2)): -8,
                (1 - sqrt(2)): -S(8) / 3,
                (sqrt(2) - 1): S(8) / 3,
                sqrt(5 + 2 * sqrt(5)): 10,
                -sqrt(5 + 2 * sqrt(5)): -10,
                (2 + sqrt(3)): 12,
                -(2 + sqrt(3)): -12,
                (2 - sqrt(3)): S(12) / 5,
                -(2 - sqrt(3)): -S(12) / 5,
            }

            if arg in cst_table:
                return S.Pi / cst_table[arg]

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return -S.ImaginaryUnit * C.acoth(i_coeff)
Example #36
0
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Infinity:
                return S.NegativeInfinity * S.ImaginaryUnit
            elif arg is S.NegativeInfinity:
                return S.Infinity * S.ImaginaryUnit
            elif arg is S.Zero:
                return S.Zero
            elif arg is S.One:
                return S.Pi / 2
            elif arg is S.NegativeOne:
                return -S.Pi / 2

        if arg.could_extract_minus_sign():
            return -cls(-arg)

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

            if arg in cst_table:
                return S.Pi / cst_table[arg]

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return S.ImaginaryUnit * C.asinh(i_coeff)
    def eval(cls, arg):
        if arg.is_Number:
            if arg is S.NaN:
                return S.NaN
            elif arg is S.Zero:
                return S.Zero
            elif arg is S.Infinity:
                return

        if arg.could_extract_minus_sign():
            return -cls(-arg)

        i_coeff = arg.as_coefficient(S.ImaginaryUnit)
        if i_coeff is not None:
            return S.ImaginaryUnit * C.sinh(i_coeff)

        pi_coeff = _pi_coeff(arg)
        if pi_coeff is not None:
            if pi_coeff.is_integer:
                return S.Zero

            if not pi_coeff.is_Rational:
                narg = pi_coeff * S.Pi
                if narg != arg:
                    return cls(narg)
                return None

            cst_table_some = {
                2: S.One,
                3: S.Half * sqrt(3),
                4: S.Half * sqrt(2),
                6: S.Half,
            }

            cst_table_more = {
                (1, 5): sqrt((5 - sqrt(5)) / 8),
                (2, 5): sqrt((5 + sqrt(5)) / 8)
            }

            p = pi_coeff.p
            q = pi_coeff.q

            Q, P = p // q, p % q

            try:
                result = cst_table_some[q]
            except KeyError:
                if abs(P) > q // 2:
                    P = q - P

                try:
                    result = cst_table_more[(P, q)]
                except KeyError:
                    if P != p:
                        result = cls(C.Rational(P, q) * S.Pi)
                    else:
                        return None

            if Q % 2 == 1:
                return -result
            else:
                return result

        if arg.is_Add:
            x, m = _peeloff_pi(arg)
            if m:
                return sin(m) * cos(x) + cos(m) * sin(x)

        if arg.func is asin:
            return arg.args[0]

        if arg.func is atan:
            x = arg.args[0]
            return x / sqrt(1 + x**2)

        if arg.func is acos:
            x = arg.args[0]
            return sqrt(1 - x**2)

        if arg.func is acot:
            x = arg.args[0]
            return 1 / (sqrt(1 + 1 / x**2) * x)