Esempio n. 1
0
 def _eval_expand_complex(self, *args):
     if self.args[0].is_real:
         return self
     re, im = self.args[0].as_real_imag()
     denom = sin(re)**2 + C.sinh(im)**2
     return (sin(re)*cos(re) - \
         S.ImaginaryUnit*C.sinh(im)*C.cosh(im))/denom
Esempio n. 2
0
 def _eval_expand_complex(self, *args):
     if self.args[0].is_real:
         return self
     re, im = self.args[0].as_real_imag()
     denom = sin(re)**2 + C.sinh(im)**2
     return (sin(re)*cos(re) - \
         S.ImaginaryUnit*C.sinh(im)*C.cosh(im))/denom
Esempio n. 3
0
 def as_real_imag(self, deep=True, **hints):
     if self.args[0].is_real:
         if deep:
             hints['complex'] = False
             return (self.expand(deep, **hints), S.Zero)
         else:
             return (self, S.Zero)
     if deep:
         re, im = self.args[0].expand(deep, **hints).as_real_imag()
     else:
         re, im = self.args[0].as_real_imag()
     denom = sin(re)**2 + C.sinh(im)**2
     return (sin(re) * cos(re) / denom, -C.sinh(im) * C.cosh(im) / denom)
Esempio n. 4
0
 def as_real_imag(self, deep=True, **hints):
     if self.args[0].is_real:
         if deep:
             hints['complex'] = False
             return (self.expand(deep, **hints), S.Zero)
         else:
             return (self, S.Zero)
     if deep:
         re, im = self.args[0].expand(deep, **hints).as_real_imag()
     else:
         re, im = self.args[0].as_real_imag()
     denom = sin(re)**2 + C.sinh(im)**2
     return (sin(re)*cos(re)/denom, -C.sinh(im)*C.cosh(im)/denom)
Esempio n. 5
0
 def _eval_expand_complex(self, deep=True, **hints):
     if self.args[0].is_real:
         if deep:
             hints['complex'] = False
             return self.expand(deep, **hints)
         else:
             return self
     if deep:
         re, im = self.args[0].expand(deep, **hints).as_real_imag()
     else:
         re, im = self.args[0].as_real_imag()
     denom = sin(re)**2 + C.sinh(im)**2
     return (sin(re)*cos(re) - \
         S.ImaginaryUnit*C.sinh(im)*C.cosh(im))/denom
Esempio n. 6
0
 def _eval_expand_complex(self, deep=True, **hints):
     if self.args[0].is_real:
         if deep:
             hints['complex'] = False
             return self.expand(deep, **hints)
         else:
             return self
     if deep:
         re, im = self.args[0].expand(deep, **hints).as_real_imag()
     else:
         re, im = self.args[0].as_real_imag()
     denom = sin(re)**2 + C.sinh(im)**2
     return (sin(re)*cos(re) - \
         S.ImaginaryUnit*C.sinh(im)*C.cosh(im))/denom
Esempio n. 7
0
 def as_real_imag(self, deep=True, **hints):
     if self.args[0].is_real:
         if deep:
             hints["complex"] = False
             return (self.expand(deep, **hints), S.Zero)
         else:
             return (self, S.Zero)
     if deep:
         re, im = self.args[0].expand(deep, **hints).as_real_imag()
     else:
         re, im = self.args[0].as_real_imag()
     return (cos(re) * C.cosh(im), -sin(re) * C.sinh(im))
Esempio n. 8
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)
Esempio n. 9
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 or arg is S.NegativeInfinity:
                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

            # http://code.google.com/p/sympy/issues/detail?id=2949
            # transform a sine to a cosine, to avoid redundant code
            if pi_coeff.is_Rational:
                x = pi_coeff % 2
                if x > 1:
                    return -cls((x % 1)*S.Pi)
                if 2*x > 1:
                    return cls((1 - x)*S.Pi)
                narg = ((pi_coeff + C.Rational(3, 2)) % 2)*S.Pi
                result = cos(narg)
                if not isinstance(result, cos):
                    return result
                if pi_coeff*S.Pi != arg:
                    return cls(pi_coeff*S.Pi)
                return None

        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 atan2:
            y, x = arg.args
            return y / sqrt(x**2 + y**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)
Esempio n. 10
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)
Esempio n. 11
0
 def _eval_expand_complex(self, *args):
     if self.args[0].is_real:
         return self
     re, im = self.args[0].as_real_imag()
     return cos(re)*C.cosh(im) - \
         S.ImaginaryUnit*sin(re)*C.sinh(im)
Esempio n. 12
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)
Esempio n. 13
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 or arg is S.NegativeInfinity:
                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

            # http://code.google.com/p/sympy/issues/detail?id=2949
            # transform a sine to a cosine, to avoid redundant code
            if pi_coeff.is_Rational:
                x = pi_coeff % 2
                if x > 1:
                    return -cls((x % 1) * S.Pi)
                if 2 * x > 1:
                    return cls((1 - x) * S.Pi)
                narg = ((pi_coeff + C.Rational(3, 2)) % 2) * S.Pi
                result = cos(narg)
                if not isinstance(result, cos):
                    return result
                if pi_coeff * S.Pi != arg:
                    return cls(pi_coeff * S.Pi)
                return None

        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 atan2:
            y, x = arg.args
            return y / sqrt(x**2 + y**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)
Esempio n. 14
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)
Esempio n. 15
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)
Esempio n. 16
0
 def _eval_expand_complex(self, *args):
     if self.args[0].is_real:
         return self
     re, im = self.args[0].as_real_imag()
     return cos(re)*C.cosh(im) - \
         S.ImaginaryUnit*sin(re)*C.sinh(im)
Esempio n. 17
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)