1 from sympy
import sin
, cos
, exp
, cot
, sqrt
, S
, I
, E
, pi
, symbols
, Function
, Matrix
, Eq
, RootSum
, Lambda
2 from sympy
.simplify
import cancel
, trim
, apart
, together
3 from sympy
.integrals
import integrate
4 from sympy
.utilities
.pytest
import XFAIL
6 x
,y
,z
,n
= symbols('xyzn')
10 assert cot(x
).has(cot
)
11 assert not cot(x
).has(sin
)
13 assert sin(x
).has(sin
)
14 assert not sin(x
).has(cot
)
16 def test_sin_exp_rewrite():
17 assert sin(x
).rewrite(sin
, exp
) == -I
/2*(exp(I
*x
)-exp(-I
*x
))
18 assert sin(x
).rewrite(sin
, exp
).rewrite(exp
, sin
) == sin(x
)
19 assert cos(x
).rewrite(cos
, exp
).rewrite(exp
, cos
) == cos(x
)
20 assert (sin(5*y
) - sin(2*x
)).rewrite(sin
, exp
).rewrite(exp
, sin
) == sin(5*y
) - sin(2*x
)
21 assert sin(x
+y
).rewrite(sin
, exp
).rewrite(exp
, sin
) == sin(x
+y
)
22 assert cos(x
+y
).rewrite(cos
, exp
).rewrite(exp
, cos
) == cos(x
+y
)
23 # This next test currently passes... not clear whether it should or not?
24 assert cos(x
).rewrite(cos
, exp
).rewrite(exp
, sin
) == cos(x
)
27 assert cancel((x
**2-2)/(x
+sqrt(2))) == x
- sqrt(2)
28 assert cancel((x
**2-y
**2)/(x
-y
), x
, y
) == x
+ y
29 assert cancel((x
**2-y
)/(x
-y
)) == 1/(x
- y
)*(x
**2 - y
)
30 assert cancel((x
**2-y
**2)/(x
-y
), x
) == x
+ y
31 assert cancel((x
**2-y
**2)/(x
-y
), y
) == x
+ y
32 assert cancel((x
**2-y
**2)/(x
-y
)) == x
+ y
33 assert cancel((E
*x
+2)/(x
-pi
)*(x
-1)) == (-2 + x
*(2 - E
) + E
*x
**2)/(x
- pi
)
34 assert cancel(Eq((x
**3-1)/(x
-1), sin(x
))) == Eq(1 + x
+ x
**2, sin(x
))
35 assert cancel((x
**2-1)/(x
-1) == (x
**2+1)/(x
-I
), x
) == (1 + x
== I
+ x
)
36 assert cancel((x
**2-1)/(x
-1) + (x
**2+1)/(x
-I
), x
) == 1 + I
+ 2*x
37 assert cancel((x
**2-1)/(x
-1) + (x
**2+1)/(x
-I
), y
) in [
38 1/(1 - x
)*(1 - x
**2) + 1/(x
- I
)*(1 + x
**2),
39 -1/(1 - x
)*(-1 + x
**2) + 1/(x
- I
)*(1 + x
**2),
40 1/(-1 + x
)*(-1 + x
**2) + 1/(x
- I
)*(1 + x
**2),
46 assert trim((f(x
)**2+f(x
))/f(x
)) == 1 + f(x
)
47 assert trim((sin(x
)**2+sin(x
))/sin(x
)) == 1 + sin(x
)
49 assert trim((f(x
)+y
*f(x
))/f(x
)) == 1 + y
51 expr
= integrate(1/(x
**3+1), x
)
53 assert trim(together(expr
.diff(x
))) == 1/(x
**3+1)
54 assert cancel(together(expr
.diff(x
))) == 1/(x
**3+1)
56 expr
= together(expr
.subs(x
, sin(x
)).diff(x
))
58 assert trim(expr
) == cos(x
)/(1 + sin(x
)**3)
60 assert trim((2 * (1/n
- cos(n
* pi
)/n
))/pi
) == \
61 1/pi
/n
*(2 - 2*cos(pi
*n
))
63 assert trim(sin((f(x
)**2+f(x
))/f(x
))) == sin(1 + f(x
))
65 assert trim(exp(x
)*sin(x
)/2 + cos(x
)*exp(x
)) == \
66 exp(x
)*(sin(x
) + 2*cos(x
))/2
68 @XFAIL # because of #666
69 def test_trim_xfail():
70 assert trim((exp(3*x
)+exp(2*x
*y
) + y
*exp(x
))/exp(x
)) == \
71 y
+ exp(2*x
) + exp(-x
+ 2*x
*y
)
74 assert apart(1/(x
+2)/(x
+1), x
) == 1/(1 + x
) - 1/(2 + x
)
75 assert apart(1/(x
+1)/(x
+5), x
) == -1/(5 + x
)/4 + 1/(1 + x
)/4
77 f
= apart(1/(x
-y
)/(x
-z
), x
)
79 assert f
.subs({y
:1,z
:2}) == apart(1/(x
-1)/(x
-2), x
)
81 assert apart((E
*x
+2)/(x
-pi
)*(x
-1), x
) in [
82 2 - E
+ E
*pi
+ E
*x
- 1/(x
- pi
)*( 2 - 2*pi
+ E
*pi
- E
*pi
**2),
83 2 - E
+ E
*pi
+ E
*x
+ 1/(x
- pi
)*(-2 + 2*pi
- E
*pi
+ E
*pi
**2),
86 M
= Matrix(2, 2, lambda i
, j
: 1/(x
-(i
+1))/(x
-(1-j
)))
88 assert apart(M
, x
) in [
90 [(x
-1)**(-2), -1/x
-1/(1-x
) ],
91 [1/(1-x
)-1/(2-x
), -S
.Half
/x
-S
.Half
/(2-x
)],
94 [(-1+x
)**(-2), -1/x
+1/(-1+x
) ],
95 [-1/(-1+x
)+1/(-2+x
), -S
.Half
/x
+S
.Half
/(-2+x
)],
99 assert apart(Eq((x
**2+1)/(x
+1), sin(x
)), x
) == \
100 Eq(x
- 1 + 2/(x
+1), sin(x
))
102 assert str(apart(1/(1+x
**5), x
, evaluate
=False)) in [
103 "RootSum(Lambda(_a, -1/5/(x - _a)*_a), x**5 + 1, x)",
104 "RootSum(Lambda(_a, -_a/(5*(x - _a))), x**5 + 1, x)"]