Repository: sympy/sympy

Issue:
sign error in as_real_imag method of cotangent
It was reported in sage trac ticket #29400 that the imaginary part of the cotangent of a symbolic expression is sometimes incorrect. I tracked this down to a bug in the `as_real_imag` method of sympy's cotangent class (`functions.elementary.trigonometric.cot`). The imaginary part is the negative of what it should be.

I could not get git push to work, so I am attaching a patch instead. It just deletes a minus sign from the `as_real_imag` method (and also from the corresponding test). 

[cot_as_real_imag.patch.txt](https://github.com/sympy/sympy/files/4648565/cot_as_real_imag.patch.txt)

I do not understand sympy or the sage-sympy interface, but I was able to put together the following sage test that demonstrates the problem.
```
import sympy
function_list = [sympy.sin, sympy.cos, sympy.tan, sympy.sec, sympy.csc, sympy.cot]
a, b = sympy.symbols('a b', real=True)
z = a + b*I
test_pt = {a:1, b:3}

for f in function_list:
    re, im = f(z).as_real_imag(z)
    f_re_im = (re.subs(test_pt) + im.subs(test_pt)*I).n()
    if abs(f(z).subs(test_pt).n() - f_re_im) > 10^(-10):
        print("{}: error = {} ({} vs. {})".format(
            str(f), 
            f(z).subs(test_pt).n() - f_re_im, 
            f(z).subs(test_pt).n(6), 
            f_re_im.n(6)))
```
The output (without the patch) is:
```
cot: error = -1.99585789446276*I (0.00449854 - 0.997929*I vs. 0.00449854 + 0.997929*I)
```
Note that the imaginary part of ```f_re_im``` (which uses the values from ```as_real_imag```) is the negative of what it should be, so deleting the minus sign solves the problem.

Discussion:

Patch:
diff --git a/sympy/functions/elementary/trigonometric.py b/sympy/functions/elementary/trigonometric.py
index f178c6e459..dde81babf5 100644
--- a/sympy/functions/elementary/trigonometric.py
+++ b/sympy/functions/elementary/trigonometric.py
@@ -1460,7 +1460,7 @@ def as_real_imag(self, deep=True, **hints):
         re, im = self._as_real_imag(deep=deep, **hints)
         if im:
             denom = cos(2*re) - cosh(2*im)
-            return (-sin(2*re)/denom, -sinh(2*im)/denom)
+            return (-sin(2*re)/denom, sinh(2*im)/denom)
         else:
             return (self.func(re), S.Zero)
 


Test patch:
diff --git a/sympy/functions/elementary/tests/test_trigonometric.py b/sympy/functions/elementary/tests/test_trigonometric.py
index 055b0dbbaf..0a67c63814 100644
--- a/sympy/functions/elementary/tests/test_trigonometric.py
+++ b/sympy/functions/elementary/tests/test_trigonometric.py
@@ -1373,7 +1373,7 @@ def test_real_imag():
         assert tan(z).as_real_imag(deep=deep) == (sin(2*a)/(cos(2*a) +
             cosh(2*b)), sinh(2*b)/(cos(2*a) + cosh(2*b)))
         assert cot(z).as_real_imag(deep=deep) == (-sin(2*a)/(cos(2*a) -
-            cosh(2*b)), -sinh(2*b)/(cos(2*a) - cosh(2*b)))
+            cosh(2*b)), sinh(2*b)/(cos(2*a) - cosh(2*b)))
         assert sin(a).as_real_imag(deep=deep) == (sin(a), 0)
         assert cos(a).as_real_imag(deep=deep) == (cos(a), 0)
         assert tan(a).as_real_imag(deep=deep) == (tan(a), 0)