def test_issue_75(): assert limit(abs(log(x)), x, oo) == oo assert limit(tan(abs(pi/2 + 1/x))/acosh(pi/2 + 1/x), x, oo) == -oo assert limit(tan(abs(pi/2 - 1/x))/acosh(pi/2 - 1/x), x, oo) == +oo assert limit(abs(log(2 + 1/x)) - log(2 + 1/x), x, oo) == 0 assert limit(abs(log(2 - 1/x)) - log(2 - 1/x), x, oo) == 0
def test_heurisch_trigonometric(): assert heurisch(sin(x), x) == -cos(x) assert heurisch(pi * sin(x) + 1, x) == x - pi * cos(x) assert heurisch(cos(x), x) == sin(x) assert heurisch(tan(x), x) in [ log(1 + tan(x)**2) / 2, log(tan(x) + I) + I * x, log(tan(x) - I) - I * x, ] assert heurisch(sin(x) * sin(y), x) == -cos(x) * sin(y) assert heurisch(sin(x) * sin(y), y) == -cos(y) * sin(x) # gives sin(x) in answer when run via setup.py and cos(x) when run via py.test assert heurisch(sin(x) * cos(x), x) in [sin(x)**2 / 2, -cos(x)**2 / 2] assert heurisch(cos(x) / sin(x), x) == log(sin(x)) assert heurisch(x * sin(7 * x), x) == sin(7 * x) / 49 - x * cos(7 * x) / 7 assert heurisch( 1 / pi / 4 * x**2 * cos(x), x) == 1 / pi / 4 * (x**2 * sin(x) - 2 * sin(x) + 2 * x * cos(x)) assert heurisch(acos(x/4) * asin(x/4), x) == 2*x - (sqrt(16 - x**2))*asin(x/4) \ + (sqrt(16 - x**2))*acos(x/4) + x*asin(x/4)*acos(x/4)
def test_diofantissue_75(): assert gruntz(abs(log(x)), x) == oo assert gruntz(tan(abs(pi / 2 + 1 / x)) / acosh(pi / 2 + 1 / x), x) == -oo assert gruntz(tan(abs(pi / 2 - 1 / x)) / acosh(pi / 2 - 1 / x), x) == +oo assert gruntz(abs(log(2 + 1 / x)) - log(2 + 1 / x), x) == 0 assert gruntz(abs(log(2 - 1 / x)) - log(2 - 1 / x), x) == 0
def test_heurisch_trigonometric(): assert heurisch(sin(x), x) == -cos(x) assert heurisch(pi * sin(x) + 1, x) == x - pi * cos(x) assert heurisch(cos(x), x) == sin(x) assert heurisch(tan(x), x) in [ log(1 + tan(x)**2) / 2, log(tan(x) + I) + I * x, log(tan(x) - I) - I * x, ] assert heurisch(sin(x) * sin(y), x) == -cos(x) * sin(y) assert heurisch(sin(x) * sin(y), y) == -cos(y) * sin(x) assert heurisch(sin(x) * cos(x), x) in [sin(x)**2 / 2, -cos(x)**2 / 2] assert heurisch(cos(x) / sin(x), x) == log(sin(x)) assert heurisch(x * sin(7 * x), x) == sin(7 * x) / 49 - x * cos(7 * x) / 7 assert heurisch( 1 / pi / 4 * x**2 * cos(x), x) == 1 / pi / 4 * (x**2 * sin(x) - 2 * sin(x) + 2 * x * cos(x)) assert heurisch(acos(x/4) * asin(x/4), x) == 2*x - (sqrt(16 - x**2))*asin(x/4) \ + (sqrt(16 - x**2))*acos(x/4) + x*asin(x/4)*acos(x/4) assert heurisch(1 / sin(1 / x) / x**2, x) == -log(tan(1 / x / 2))
def test_diofantissue_75(): assert gruntz(abs(log(x)), x) == oo assert gruntz(tan(abs(pi/2 + 1/x))/acosh(pi/2 + 1/x), x) == -oo assert gruntz(tan(abs(pi/2 - 1/x))/acosh(pi/2 - 1/x), x) == +oo assert gruntz(abs(log(2 + 1/x)) - log(2 + 1/x), x) == 0 assert gruntz(abs(log(2 - 1/x)) - log(2 - 1/x), x) == 0
def test_hyper_as_trig(): from diofant.simplify.fu import _osborne as o, _osbornei as i, TR12 eq = sinh(x)**2 + cosh(x)**2 t, f = hyper_as_trig(eq) assert f(fu(t)) == cosh(2*x) e, f = hyper_as_trig(tanh(x + y)) assert f(TR12(e)) == (tanh(x) + tanh(y))/(tanh(x)*tanh(y) + 1) d = Dummy() assert o(sinh(x), d) == I*sin(x*d) assert o(tanh(x), d) == I*tan(x*d) assert o(coth(x), d) == cot(x*d)/I assert o(cosh(x), d) == cos(x*d) for func in (sinh, cosh, tanh, coth): h = func(pi) assert i(o(h, d), d) == h # /!\ the _osborne functions are not meant to work # in the o(i(trig, d), d) direction so we just check # that they work as they are supposed to work assert i(cos(x*y), y) == cosh(x) assert i(sin(x*y), y) == sinh(x)/I assert i(tan(x*y), y) == tanh(x)/I assert i(cot(x*y), y) == coth(x)*I assert i(sec(x*y), y) == 1/cosh(x) assert i(csc(x*y), y) == I/sinh(x)
def test_basic1(): assert limit(x, x, oo) == oo assert limit(x, x, -oo) == -oo assert limit(-x, x, oo) == -oo assert limit(x**2, x, -oo) == oo assert limit(-x**2, x, oo) == -oo assert limit(x*log(x), x, 0, dir="+") == 0 assert limit(1/x, x, oo) == 0 assert limit(exp(x), x, oo) == oo assert limit(-exp(x), x, oo) == -oo assert limit(exp(x)/x, x, oo) == oo assert limit(1/x - exp(-x), x, oo) == 0 assert limit(x + 1/x, x, oo) == oo assert limit(x - x**2, x, oo) == -oo assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1 assert limit((1 + x)**oo, x, 0) == oo assert limit((1 + x)**oo, x, 0, dir='-') == 0 assert limit((1 + x + y)**oo, x, 0, dir='-') == (1 + y)**(oo) assert limit(y/x/log(x), x, 0) == -oo*sign(y) assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo limit(Sum(1/x, (x, 1, y)) - log(y), y, oo) limit(Sum(1/x, (x, 1, y)) - 1/y, y, oo) assert limit(gamma(1/x + 3), x, oo) == 2 assert limit(S.NaN, x, -oo) == S.NaN assert limit(O(2)*x, x, S.NaN) == S.NaN assert limit(sin(O(x)), x, 0) == 0 assert limit(1/(x - 1), x, 1, dir="+") == oo assert limit(1/(x - 1), x, 1, dir="-") == -oo assert limit(1/(5 - x)**3, x, 5, dir="+") == -oo assert limit(1/(5 - x)**3, x, 5, dir="-") == oo assert limit(1/sin(x), x, pi, dir="+") == -oo assert limit(1/sin(x), x, pi, dir="-") == oo assert limit(1/cos(x), x, pi/2, dir="+") == -oo assert limit(1/cos(x), x, pi/2, dir="-") == oo assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="+") == oo assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="-") == -oo assert limit(1/cot(x)**3, x, (3*pi/2), dir="+") == -oo assert limit(1/cot(x)**3, x, (3*pi/2), dir="-") == oo # approaching 0 # from dir="+" assert limit(1 + 1/x, x, 0) == oo # from dir='-' # Add assert limit(1 + 1/x, x, 0, dir='-') == -oo # Pow assert limit(x**(-2), x, 0, dir='-') == oo assert limit(x**(-3), x, 0, dir='-') == -oo assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I assert limit(x**2, x, 0, dir='-') == 0 assert limit(sqrt(x), x, 0, dir='-') == 0 assert limit(x**-pi, x, 0, dir='-') == oo*sign((-1)**(-pi)) assert limit((1 + cos(x))**oo, x, 0) == oo assert limit(x**2, x, 0, dir='real') == 0 assert limit(exp(x), x, 0, dir='real') == 1 pytest.raises(PoleError, lambda: limit(1/x, x, 0, dir='real'))
def test_issue_74(): assert limit(sign(log(1 + 1/x)), x, oo) == +1 assert limit(sign(log(1 - 1/x)), x, oo) == -1 assert limit(sign(sin(+1/x)), x, oo) == +1 assert limit(sign(sin(-1/x)), x, oo) == -1 assert limit(sign(tan(+1/x)), x, oo) == +1 assert limit(sign(tan(-1/x)), x, oo) == -1 assert limit(sign(cos(pi/2 + 1/x)), x, oo) == -1 assert limit(sign(cos(pi/2 - 1/x)), x, oo) == +1
def test_diofantissue_74(): assert gruntz(sign(log(1 + 1 / x)), x) == +1 assert gruntz(sign(log(1 - 1 / x)), x) == -1 assert gruntz(sign(sin(+1 / x)), x) == +1 assert gruntz(sign(sin(-1 / x)), x) == -1 assert gruntz(sign(tan(+1 / x)), x) == +1 assert gruntz(sign(tan(-1 / x)), x) == -1 assert gruntz(sign(cos(pi / 2 + 1 / x)), x) == -1 assert gruntz(sign(cos(pi / 2 - 1 / x)), x) == +1
def test_basic1(): assert limit(x, x, oo) == oo assert limit(x, x, -oo) == -oo assert limit(-x, x, oo) == -oo assert limit(x**2, x, -oo) == oo assert limit(-x**2, x, oo) == -oo assert limit(x*log(x), x, 0, dir="+") == 0 assert limit(1/x, x, oo) == 0 assert limit(exp(x), x, oo) == oo assert limit(-exp(x), x, oo) == -oo assert limit(exp(x)/x, x, oo) == oo assert limit(1/x - exp(-x), x, oo) == 0 assert limit(x + 1/x, x, oo) == oo assert limit(x - x**2, x, oo) == -oo assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1 assert limit((1 + x)**oo, x, 0) == oo assert limit((1 + x)**oo, x, 0, dir='-') == 0 assert limit((1 + x + y)**oo, x, 0, dir='-') == (1 + y)**oo assert limit(y/x/log(x), x, 0) == -oo*sign(y) assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo limit(Sum(1/x, (x, 1, y)) - log(y), y, oo) limit(Sum(1/x, (x, 1, y)) - 1/y, y, oo) assert limit(gamma(1/x + 3), x, oo) == 2 assert limit(nan, x, -oo) == nan assert limit(O(2)*x, x, nan) == nan assert limit(sin(O(x)), x, 0) == 0 assert limit(1/(x - 1), x, 1, dir="+") == oo assert limit(1/(x - 1), x, 1, dir="-") == -oo assert limit(1/(5 - x)**3, x, 5, dir="+") == -oo assert limit(1/(5 - x)**3, x, 5, dir="-") == oo assert limit(1/sin(x), x, pi, dir="+") == -oo assert limit(1/sin(x), x, pi, dir="-") == oo assert limit(1/cos(x), x, pi/2, dir="+") == -oo assert limit(1/cos(x), x, pi/2, dir="-") == oo assert limit(1/tan(x**3), x, cbrt(2*pi), dir="+") == oo assert limit(1/tan(x**3), x, cbrt(2*pi), dir="-") == -oo assert limit(1/cot(x)**3, x, 3*pi/2, dir="+") == -oo assert limit(1/cot(x)**3, x, 3*pi/2, dir="-") == oo # approaching 0 # from dir="+" assert limit(1 + 1/x, x, 0) == oo # from dir='-' # Add assert limit(1 + 1/x, x, 0, dir='-') == -oo # Pow assert limit(x**(-2), x, 0, dir='-') == oo assert limit(x**(-3), x, 0, dir='-') == -oo assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I assert limit(x**2, x, 0, dir='-') == 0 assert limit(sqrt(x), x, 0, dir='-') == 0 assert limit(x**-pi, x, 0, dir='-') == oo*sign((-1)**(-pi)) assert limit((1 + cos(x))**oo, x, 0) == oo assert limit(x**2, x, 0, dir='real') == 0 assert limit(exp(x), x, 0, dir='real') == 1 pytest.raises(PoleError, lambda: limit(1/x, x, 0, dir='real'))
def test_diofantissue_74(): assert gruntz(sign(log(1 + 1/x)), x) == +1 assert gruntz(sign(log(1 - 1/x)), x) == -1 assert gruntz(sign(sin(+1/x)), x) == +1 assert gruntz(sign(sin(-1/x)), x) == -1 assert gruntz(sign(tan(+1/x)), x) == +1 assert gruntz(sign(tan(-1/x)), x) == -1 assert gruntz(sign(cos(pi/2 + 1/x)), x) == -1 assert gruntz(sign(cos(pi/2 - 1/x)), x) == +1
def test_diofantissue_74(): from diofant.functions import sign assert gruntz(sign(log(1 + 1 / x)), x) == +1 assert gruntz(sign(log(1 - 1 / x)), x) == -1 assert gruntz(sign(sin(1 / x)), x) == +1 assert gruntz(sign(sin(-1 / x)), x) == -1 assert gruntz(sign(tan(1 / x)), x) == +1 assert gruntz(sign(tan(-1 / x)), x) == -1 assert gruntz(sign(cos(pi / 2 + 1 / x)), x) == -1 assert gruntz(sign(cos(pi / 2 - 1 / x)), x) == +1
def test_RootSum_independent(): f = (x**3 - a)**2 * (x**4 - b)**3 g = Lambda(x, 5 * tan(x) + 7) h = Lambda(x, tan(x)) r0 = RootSum(x**3 - a, h, x) r1 = RootSum(x**4 - b, h, x) assert RootSum(f, g, x).as_ordered_terms() == [10 * r0, 15 * r1, 126]
def test_RootSum_independent(): f = (x**3 - a)**2*(x**4 - b)**3 g = Lambda(x, 5*tan(x) + 7) h = Lambda(x, tan(x)) r0 = RootSum(x**3 - a, h, x) r1 = RootSum(x**4 - b, h, x) assert RootSum(f, g, x).as_ordered_terms() == [10*r0, 15*r1, 126]
def test_sympyissue_22220(): e1 = sqrt(30)*atan(sqrt(30)*tan(x/2)/6)/30 e2 = sqrt(30)*I*(-log(sqrt(2)*tan(x/2) - 2*sqrt(15)*I/5) + +log(sqrt(2)*tan(x/2) + 2*sqrt(15)*I/5))/60 assert limit(e1, x, -pi) == -sqrt(30)*pi/60 assert limit(e2, x, -pi) == -sqrt(30)*pi/30 assert limit(e1, x, -pi, 1) == sqrt(30)*pi/60 assert limit(e2, x, -pi, 1) == 0
def test_exptrigsimp(): def valid(a, b): from diofant.utilities.randtest import verify_numerically as tn if not (tn(a, b) and a == b): return False return True assert exptrigsimp(exp(x) + exp(-x)) == 2 * cosh(x) assert exptrigsimp(exp(x) - exp(-x)) == 2 * sinh(x) e = [ cos(x) + I * sin(x), cos(x) - I * sin(x), cosh(x) - sinh(x), cosh(x) + sinh(x) ] ok = [exp(I * x), exp(-I * x), exp(-x), exp(x)] assert all(valid(i, j) for i, j in zip([exptrigsimp(ei) for ei in e], ok)) ue = [ cos(x) + sin(x), cos(x) - sin(x), cosh(x) + I * sinh(x), cosh(x) - I * sinh(x) ] assert [exptrigsimp(ei) == ei for ei in ue] res = [] ok = [ y * tanh(1), 1 / (y * tanh(1)), I * y * tan(1), -I / (y * tan(1)), y * tanh(x), 1 / (y * tanh(x)), I * y * tan(x), -I / (y * tan(x)), y * tanh(1 + I), 1 / (y * tanh(1 + I)) ] for a in (1, I, x, I * x, 1 + I): w = exp(a) eq = y * (w - 1 / w) / (w + 1 / w) s = simplify(eq) assert s == exptrigsimp(eq) res.append(s) sinv = simplify(1 / eq) assert sinv == exptrigsimp(1 / eq) res.append(sinv) assert all(valid(i, j) for i, j in zip(res, ok)) for a in range(1, 3): w = exp(a) e = w + 1 / w s = simplify(e) assert s == exptrigsimp(e) assert valid(s, 2 * cosh(a)) e = w - 1 / w s = simplify(e) assert s == exptrigsimp(e) assert valid(s, 2 * sinh(a))
def test_trigsimp1a(): assert trigsimp(sin(2)**2*cos(3)*exp(2)/cos(2)**2) == tan(2)**2*cos(3)*exp(2) assert trigsimp(tan(2)**2*cos(3)*exp(2)*cos(2)**2) == sin(2)**2*cos(3)*exp(2) assert trigsimp(cot(2)*cos(3)*exp(2)*sin(2)) == cos(3)*exp(2)*cos(2) assert trigsimp(tan(2)*cos(3)*exp(2)/sin(2)) == cos(3)*exp(2)/cos(2) assert trigsimp(cot(2)*cos(3)*exp(2)/cos(2)) == cos(3)*exp(2)/sin(2) assert trigsimp(cot(2)*cos(3)*exp(2)*tan(2)) == cos(3)*exp(2) assert trigsimp(sinh(2)*cos(3)*exp(2)/cosh(2)) == tanh(2)*cos(3)*exp(2) assert trigsimp(tanh(2)*cos(3)*exp(2)*cosh(2)) == sinh(2)*cos(3)*exp(2) assert trigsimp(coth(2)*cos(3)*exp(2)*sinh(2)) == cosh(2)*cos(3)*exp(2) assert trigsimp(tanh(2)*cos(3)*exp(2)/sinh(2)) == cos(3)*exp(2)/cosh(2) assert trigsimp(coth(2)*cos(3)*exp(2)/cosh(2)) == cos(3)*exp(2)/sinh(2) assert trigsimp(coth(2)*cos(3)*exp(2)*tanh(2)) == cos(3)*exp(2)
def test_hyperbolic_simp(): assert trigsimp(sinh(x)**2 + 1) == cosh(x)**2 assert trigsimp(cosh(x)**2 - 1) == sinh(x)**2 assert trigsimp(cosh(x)**2 - sinh(x)**2) == 1 assert trigsimp(1 - tanh(x)**2) == 1 / cosh(x)**2 assert trigsimp(1 - 1 / cosh(x)**2) == tanh(x)**2 assert trigsimp(tanh(x)**2 + 1 / cosh(x)**2) == 1 assert trigsimp(coth(x)**2 - 1) == 1 / sinh(x)**2 assert trigsimp(1 / sinh(x)**2 + 1) == 1 / tanh(x)**2 assert trigsimp(coth(x)**2 - 1 / sinh(x)**2) == 1 assert trigsimp(5 * cosh(x)**2 - 5 * sinh(x)**2) == 5 assert trigsimp(5 * cosh(x / 2)**2 - 2 * sinh(x / 2)**2) == 3 * cosh(x) / 2 + Rational(7, 2) assert trigsimp(sinh(x) / cosh(x)) == tanh(x) assert trigsimp(tanh(x)) == trigsimp(sinh(x) / cosh(x)) assert trigsimp(cosh(x) / sinh(x)) == 1 / tanh(x) assert trigsimp(2 * tanh(x) * cosh(x)) == 2 * sinh(x) assert trigsimp(coth(x)**3 * sinh(x)**3) == cosh(x)**3 assert trigsimp(y * tanh(x)**2 / sinh(x)**2) == y / cosh(x)**2 assert trigsimp(coth(x) / cosh(x)) == 1 / sinh(x) e = 2 * cosh(x)**2 - 2 * sinh(x)**2 assert trigsimp(log(e)) == log(2) assert trigsimp(cosh(x)**2 * cosh(y)**2 - cosh(x)**2 * sinh(y)**2 - sinh(x)**2, recursive=True) == 1 assert trigsimp(sinh(x)**2 * sinh(y)**2 - sinh(x)**2 * cosh(y)**2 + cosh(x)**2, recursive=True) == 1 assert abs(trigsimp(2.0 * cosh(x)**2 - 2.0 * sinh(x)**2) - 2.0) < 1e-10 assert trigsimp(sinh(x)**2 / cosh(x)**2) == tanh(x)**2 assert trigsimp(sinh(x)**3 / cosh(x)**3) == tanh(x)**3 assert trigsimp(sinh(x)**10 / cosh(x)**10) == tanh(x)**10 assert trigsimp(cosh(x)**3 / sinh(x)**3) == 1 / tanh(x)**3 assert trigsimp(cosh(x) / sinh(x)) == 1 / tanh(x) assert trigsimp(cosh(x)**2 / sinh(x)**2) == 1 / tanh(x)**2 assert trigsimp(cosh(x)**10 / sinh(x)**10) == 1 / tanh(x)**10 assert trigsimp(x * cosh(x) * tanh(x)) == x * sinh(x) assert trigsimp(-sinh(x) + cosh(x) * tanh(x)) == 0 assert tan(x) != 1 / cot(x) # cot doesn't auto-simplify assert trigsimp(tan(x) - 1 / cot(x)) == 0 assert trigsimp(3 * tanh(x)**7 - 2 / coth(x)**7) == tanh(x)**7
def test_RootSum___new__(): f = x**3 + x + 3 g = Lambda(r, log(r * x)) s = RootSum(f, g) assert isinstance(s, RootSum) is True assert RootSum(f**2, g) == 2 * RootSum(f, g) assert RootSum((x - 7) * f**3, g) == log(7 * x) + 3 * RootSum(f, g) # issue sympy/sympy#5571 assert hash(RootSum((x - 7) * f**3, g)) == hash(log(7 * x) + 3 * RootSum(f, g)) pytest.raises(MultivariatePolynomialError, lambda: RootSum(x**3 + x + y)) pytest.raises(ValueError, lambda: RootSum(x**2 + 3, lambda x: x)) assert RootSum(f, log) == RootSum(f, Lambda(x, log(x))) assert isinstance(RootSum(f, auto=False), RootSum) is True assert RootSum(f) == 0 assert RootSum(f, Lambda(x, x)) == 0 assert RootSum(f, Lambda(x, x**2)) == -2 assert RootSum(f, Lambda(x, 1)) == 3 assert RootSum(f, Lambda(x, 2)) == 6 assert RootSum(f, auto=False).is_commutative is True assert RootSum(f, Lambda(x, 1 / (x + x**2))) == Rational(11, 3) assert RootSum(f, Lambda(x, y / (x + x**2))) == Rational(11, 3) * y assert RootSum(x**2 - 1, Lambda(x, 3 * x**2), x) == 6 assert RootSum(x**2 - y, Lambda(x, 3 * x**2), x) == 6 * y assert RootSum(x**2 - 1, Lambda(x, z * x**2), x) == 2 * z assert RootSum(x**2 - y, Lambda(x, z * x**2), x) == 2 * z * y assert RootSum(x**2 - 1, Lambda(x, exp(x)), quadratic=True) == exp(-1) + exp(1) assert RootSum(x**3 + a*x + a**3, tan, x) == \ RootSum(x**3 + x + 1, Lambda(x, tan(a*x))) assert RootSum(a**3*x**3 + a*x + 1, tan, x) == \ RootSum(x**3 + x + 1, Lambda(x, tan(x/a))) assert isinstance( RootSum(x**7 + 2 * x + 1, Lambda(x, log(x))).doit(), RootSum)
def test_fu(): assert fu(sin(50)**2 + cos(50)**2 + sin(pi/6)) == Rational(3, 2) assert fu(sqrt(6)*cos(x) + sqrt(2)*sin(x)) == 2*sqrt(2)*sin(x + pi/3) eq = sin(x)**4 - cos(y)**2 + sin(y)**2 + 2*cos(x)**2 assert fu(eq) == cos(x)**4 - 2*cos(y)**2 + 2 assert fu(Rational(1, 2) - cos(2*x)/2) == sin(x)**2 assert fu(sin(a)*(cos(b) - sin(b)) + cos(a)*(sin(b) + cos(b))) == \ sqrt(2)*sin(a + b + pi/4) assert fu(sqrt(3)*cos(x)/2 + sin(x)/2) == sin(x + pi/3) assert fu(1 - sin(2*x)**2/4 - sin(y)**2 - cos(x)**4) == \ -cos(x)**2 + cos(y)**2 assert fu(cos(4*pi/9)) == sin(pi/18) assert fu(cos(pi/9)*cos(2*pi/9)*cos(3*pi/9)*cos(4*pi/9)) == Rational(1, 16) assert fu( tan(7*pi/18) + tan(5*pi/18) - sqrt(3)*tan(5*pi/18)*tan(7*pi/18)) == \ -sqrt(3) assert fu(tan(1)*tan(2)) == tan(1)*tan(2) expr = Mul(*[cos(2**i) for i in range(10)]) assert fu(expr) == sin(1024)/(1024*sin(1))
def test_RootSum___new__(): f = x**3 + x + 3 g = Lambda(r, log(r*x)) s = RootSum(f, g) assert isinstance(s, RootSum) is True assert RootSum(f**2, g) == 2*RootSum(f, g) assert RootSum((x - 7)*f**3, g) == log(7*x) + 3*RootSum(f, g) # issue sympy/sympy#5571 assert hash(RootSum((x - 7)*f**3, g)) == hash(log(7*x) + 3*RootSum(f, g)) pytest.raises(MultivariatePolynomialError, lambda: RootSum(x**3 + x + y)) pytest.raises(ValueError, lambda: RootSum(x**2 + 3, lambda x: x)) assert RootSum(f, log) == RootSum(f, Lambda(x, log(x))) assert isinstance(RootSum(f, auto=False), RootSum) is True assert RootSum(f) == 0 assert RootSum(f, Lambda(x, x)) == 0 assert RootSum(f, Lambda(x, x**2)) == -2 assert RootSum(f, Lambda(x, 1)) == 3 assert RootSum(f, Lambda(x, 2)) == 6 assert RootSum(f, auto=False).is_commutative is True assert RootSum(f, Lambda(x, 1/(x + x**2))) == Rational(11, 3) assert RootSum(f, Lambda(x, y/(x + x**2))) == Rational(11, 3)*y assert RootSum(x**2 - 1, Lambda(x, 3*x**2), x) == 6 assert RootSum(x**2 - y, Lambda(x, 3*x**2), x) == 6*y assert RootSum(x**2 - 1, Lambda(x, z*x**2), x) == 2*z assert RootSum(x**2 - y, Lambda(x, z*x**2), x) == 2*z*y assert RootSum( x**2 - 1, Lambda(x, exp(x)), quadratic=True) == exp(-1) + exp(1) assert RootSum(x**3 + a*x + a**3, tan, x) == \ RootSum(x**3 + x + 1, Lambda(x, tan(a*x))) assert RootSum(a**3*x**3 + a*x + 1, tan, x) == \ RootSum(x**3 + x + 1, Lambda(x, tan(x/a))) assert isinstance(RootSum(x**7 + 2*x + 1, Lambda(x, log(x))).doit(), RootSum)
def test_fu(): assert fu(sin(50)**2 + cos(50)**2 + sin(pi / 6)) == Rational(3, 2) assert fu(sqrt(6) * cos(x) + sqrt(2) * sin(x)) == 2 * sqrt(2) * sin(x + pi / 3) eq = sin(x)**4 - cos(y)**2 + sin(y)**2 + 2 * cos(x)**2 assert fu(eq) == cos(x)**4 - 2 * cos(y)**2 + 2 assert fu(Rational(1, 2) - cos(2 * x) / 2) == sin(x)**2 assert fu(sin(a)*(cos(b) - sin(b)) + cos(a)*(sin(b) + cos(b))) == \ sqrt(2)*sin(a + b + pi/4) assert fu(sqrt(3) * cos(x) / 2 + sin(x) / 2) == sin(x + pi / 3) assert fu(1 - sin(2*x)**2/4 - sin(y)**2 - cos(x)**4) == \ -cos(x)**2 + cos(y)**2 assert fu(cos(4 * pi / 9)) == sin(pi / 18) assert fu( cos(pi / 9) * cos(2 * pi / 9) * cos(3 * pi / 9) * cos(4 * pi / 9)) == Rational(1, 16) assert fu( tan(7*pi/18) + tan(5*pi/18) - sqrt(3)*tan(5*pi/18)*tan(7*pi/18)) == \ -sqrt(3) assert fu(tan(1) * tan(2)) == tan(1) * tan(2) expr = Mul(*[cos(2**i) for i in range(10)]) assert fu(expr) == sin(1024) / (1024 * sin(1))
def test_tancot_rewrite_sqrt(): for p in [1, 3, 5, 17]: for t in [1, 8]: n = t*p for i in range(1, (n + 1)//2 + 1): if 1 == gcd(i, n): x = i*pi/n if 2*i != n and 3*i != 2*n: t1 = tan(x).rewrite(sqrt) assert not t1.has(cot, tan), f'fails for {i:d}*pi/{n:d}' assert 1e-3 > abs( tan(x.evalf(7)) - t1.evalf(4) ), f'fails for {i:d}*pi/{n:d}' if i != 0 and i != n: c1 = cot(x).rewrite(sqrt) assert not c1.has(cot, tan), f'fails for {i:d}*pi/{n:d}' assert 1e-3 > abs( cot(x.evalf(7)) - c1.evalf(4) ), f'fails for {i:d}*pi/{n:d}'
def test_hyperbolic_simp(): assert trigsimp(sinh(x)**2 + 1) == cosh(x)**2 assert trigsimp(cosh(x)**2 - 1) == sinh(x)**2 assert trigsimp(cosh(x)**2 - sinh(x)**2) == 1 assert trigsimp(1 - tanh(x)**2) == 1/cosh(x)**2 assert trigsimp(1 - 1/cosh(x)**2) == tanh(x)**2 assert trigsimp(tanh(x)**2 + 1/cosh(x)**2) == 1 assert trigsimp(coth(x)**2 - 1) == 1/sinh(x)**2 assert trigsimp(1/sinh(x)**2 + 1) == 1/tanh(x)**2 assert trigsimp(coth(x)**2 - 1/sinh(x)**2) == 1 assert trigsimp(5*cosh(x)**2 - 5*sinh(x)**2) == 5 assert trigsimp(5*cosh(x/2)**2 - 2*sinh(x/2)**2) == 3*cosh(x)/2 + Rational(7, 2) assert trigsimp(sinh(x)/cosh(x)) == tanh(x) assert trigsimp(tanh(x)) == trigsimp(sinh(x)/cosh(x)) assert trigsimp(cosh(x)/sinh(x)) == 1/tanh(x) assert trigsimp(2*tanh(x)*cosh(x)) == 2*sinh(x) assert trigsimp(coth(x)**3*sinh(x)**3) == cosh(x)**3 assert trigsimp(y*tanh(x)**2/sinh(x)**2) == y/cosh(x)**2 assert trigsimp(coth(x)/cosh(x)) == 1/sinh(x) e = 2*cosh(x)**2 - 2*sinh(x)**2 assert trigsimp(log(e)) == log(2) assert trigsimp(cosh(x)**2*cosh(y)**2 - cosh(x)**2*sinh(y)**2 - sinh(x)**2, recursive=True) == 1 assert trigsimp(sinh(x)**2*sinh(y)**2 - sinh(x)**2*cosh(y)**2 + cosh(x)**2, recursive=True) == 1 assert abs(trigsimp(2.0*cosh(x)**2 - 2.0*sinh(x)**2) - 2.0) < 1e-10 assert trigsimp(sinh(x)**2/cosh(x)**2) == tanh(x)**2 assert trigsimp(sinh(x)**3/cosh(x)**3) == tanh(x)**3 assert trigsimp(sinh(x)**10/cosh(x)**10) == tanh(x)**10 assert trigsimp(cosh(x)**3/sinh(x)**3) == 1/tanh(x)**3 assert trigsimp(cosh(x)/sinh(x)) == 1/tanh(x) assert trigsimp(cosh(x)**2/sinh(x)**2) == 1/tanh(x)**2 assert trigsimp(cosh(x)**10/sinh(x)**10) == 1/tanh(x)**10 assert trigsimp(x*cosh(x)*tanh(x)) == x*sinh(x) assert trigsimp(-sinh(x) + cosh(x)*tanh(x)) == 0 assert tan(x) != 1/cot(x) # cot doesn't auto-simplify assert trigsimp(tan(x) - 1/cot(x)) == 0 assert trigsimp(3*tanh(x)**7 - 2/coth(x)**7) == tanh(x)**7
def test_P(): assert P(0, z, m) == F(z, m) assert P(1, z, m) == F(z, m) + \ (sqrt(1 - m*sin(z)**2)*tan(z) - E(z, m))/(1 - m) assert P(n, i*pi/2, m) == i*P(n, m) assert P(n, z, 0) == atanh(sqrt(n - 1)*tan(z))/sqrt(n - 1) assert P(n, z, n) == F(z, n) - P(1, z, n) + tan(z)/sqrt(1 - n*sin(z)**2) assert P(oo, z, m) == 0 assert P(-oo, z, m) == 0 assert P(n, z, oo) == 0 assert P(n, z, -oo) == 0 assert P(0, m) == K(m) assert P(1, m) == zoo assert P(n, 0) == pi/(2*sqrt(1 - n)) assert P(2, 1) == -oo assert P(-1, 1) == oo assert P(n, n) == E(n)/(1 - n) assert P(oo, m) == 0 assert P(n, oo) == 0 assert P(n, -z, m) == -P(n, z, m) ni, mi = Symbol('n', extended_real=False), Symbol('m', extended_real=False) assert P(ni, z, mi).conjugate() == \ P(ni.conjugate(), z.conjugate(), mi.conjugate()) nr, mr = Symbol('n', extended_real=True, negative=True), \ Symbol('m', extended_real=True, negative=True) assert P(nr, z, mr).conjugate() == P(nr, z.conjugate(), mr) assert P(n, m).conjugate() == P(n.conjugate(), m.conjugate()) assert P(n, z, m).diff(n) == (E(z, m) + (m - n)*F(z, m)/n + (n**2 - m)*P(n, z, m)/n - n*sqrt(1 - m*sin(z)**2)*sin(2*z)/(2*(1 - n*sin(z)**2)))/(2*(m - n)*(n - 1)) assert P(n, z, m).diff(z) == 1/(sqrt(1 - m*sin(z)**2)*(1 - n*sin(z)**2)) assert P(n, z, m).diff(m) == (E(z, m)/(m - 1) + P(n, z, m) - m*sin(2*z)/(2*(m - 1)*sqrt(1 - m*sin(z)**2)))/(2*(n - m)) assert P(n, m).diff(n) == (E(m) + (m - n)*K(m)/n + (n**2 - m)*P(n, m)/n)/(2*(m - n)*(n - 1)) assert P(n, m).diff(m) == (E(m)/(m - 1) + P(n, m))/(2*(n - m)) rx, ry = randcplx(), randcplx() assert td(P(n, rx, ry), n) assert td(P(rx, z, ry), z) assert td(P(rx, ry, m), m) pytest.raises(ArgumentIndexError, lambda: P(n, z, m).fdiff(4)) pytest.raises(ArgumentIndexError, lambda: P(n, m).fdiff(3)) assert P(n, z, m).series(z) == z + z**3*(m/6 + n/3) + \ z**5*(3*m**2/40 + m*n/10 - m/30 + n**2/5 - n/15) + O(z**6)
def test_TR14(): eq = (cos(x) - 1)*(cos(x) + 1) ans = -sin(x)**2 assert TR14(eq) == ans assert TR14(1/eq) == 1/ans assert TR14((cos(x) - 1)**2*(cos(x) + 1)**2) == ans**2 assert TR14((cos(x) - 1)**2*(cos(x) + 1)**3) == ans**2*(cos(x) + 1) assert TR14((cos(x) - 1)**3*(cos(x) + 1)**2) == ans**2*(cos(x) - 1) eq = (cos(x) - 1)**y*(cos(x) + 1)**y assert TR14(eq) == eq eq = (cos(x) - 2)**y*(cos(x) + 1) assert TR14(eq) == eq eq = (tan(x) - 2)**2*(cos(x) + 1) assert TR14(eq) == eq i = symbols('i', integer=True) assert TR14((cos(x) - 1)**i*(cos(x) + 1)**i) == ans**i assert TR14((sin(x) - 1)**i*(sin(x) + 1)**i) == (-cos(x)**2)**i # could use extraction in this case eq = (cos(x) - 1)**(i + 1)*(cos(x) + 1)**i assert TR14(eq) in [(cos(x) - 1)*ans**i, eq] assert TR14((sin(x) - 1)*(sin(x) + 1)) == -cos(x)**2 p1 = (cos(x) + 1)*(cos(x) - 1) p2 = (cos(y) - 1)*2*(cos(y) + 1) p3 = (3*(cos(y) - 1))*(3*(cos(y) + 1)) assert TR14(p1*p2*p3*(x - 1)) == -18*((x - 1)*sin(x)**2*sin(y)**4)
def test_intrinsic_math1_codegen(): # not included: log10 name_expr = [ ("test_fabs", abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] numerical_tests = [] for name, expr in name_expr: for xval in 0.2, 0.5, 0.8: expected = N(expr.subs({x: xval}), strict=False) numerical_tests.append((name, (xval,), expected, 1e-14)) for lang, commands in valid_lang_commands: if lang == "C": name_expr_C = [("test_floor", floor(x)), ("test_ceil", ceiling(x))] else: name_expr_C = [] run_test("intrinsic_math1", name_expr + name_expr_C, numerical_tests, lang, commands)
def test_trigsimp_groebner(): c = cos(x) s = sin(x) ex = (4*s*c + 12*s + 5*c**3 + 21*c**2 + 23*c + 15)/( -s*c**2 + 2*s*c + 15*s + 7*c**3 + 31*c**2 + 37*c + 21) resnum = (5*s - 5*c + 1) resdenom = (8*s - 6*c) results = [resnum/resdenom, (-resnum)/(-resdenom)] assert trigsimp_groebner(ex) in results assert trigsimp_groebner(s/c, hints=[tan]) == tan(x) assert trigsimp((-s + 1)/c + c/(-s + 1), method='groebner') == 2/c assert trigsimp((-s + 1)/c + c/(-s + 1), method='groebner', polynomial=True) == 2/c # Test quick=False works assert trigsimp_groebner(ex, hints=[2]) in results # test "I" assert trigsimp_groebner(sin(I*x)/cos(I*x), hints=[tanh]) == I*tanh(x) # test hyperbolic / sums assert trigsimp_groebner((tanh(x)+tanh(y))/(1+tanh(x)*tanh(y)), hints=[(tanh, x, y)]) == tanh(x + y) # issue sympy/sympy#11062 trigsimp_groebner(csc(x) * sin(x)) # not raises
def test_TR14(): eq = (cos(x) - 1) * (cos(x) + 1) ans = -sin(x)**2 assert TR14(eq) == ans assert TR14(1 / eq) == 1 / ans assert TR14((cos(x) - 1)**2 * (cos(x) + 1)**2) == ans**2 assert TR14((cos(x) - 1)**2 * (cos(x) + 1)**3) == ans**2 * (cos(x) + 1) assert TR14((cos(x) - 1)**3 * (cos(x) + 1)**2) == ans**2 * (cos(x) - 1) eq = (cos(x) - 1)**y * (cos(x) + 1)**y assert TR14(eq) == eq eq = (cos(x) - 2)**y * (cos(x) + 1) assert TR14(eq) == eq eq = (tan(x) - 2)**2 * (cos(x) + 1) assert TR14(eq) == eq i = symbols('i', integer=True) assert TR14((cos(x) - 1)**i * (cos(x) + 1)**i) == ans**i assert TR14((sin(x) - 1)**i * (sin(x) + 1)**i) == (-cos(x)**2)**i # could use extraction in this case eq = (cos(x) - 1)**(i + 1) * (cos(x) + 1)**i assert TR14(eq) in [(cos(x) - 1) * ans**i, eq] assert TR14((sin(x) - 1) * (sin(x) + 1)) == -cos(x)**2 p1 = (cos(x) + 1) * (cos(x) - 1) p2 = (cos(y) - 1) * 2 * (cos(y) + 1) p3 = (3 * (cos(y) - 1)) * (3 * (cos(y) + 1)) assert TR14(p1 * p2 * p3 * (x - 1)) == -18 * ((x - 1) * sin(x)**2 * sin(y)**4)
def test_intrinsic_math1_codegen(): # not included: log10 name_expr = [ ("test_fabs", abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] numerical_tests = [] for name, expr in name_expr: for xval in 0.2, 0.5, 0.8: expected = N(expr.subs(x, xval), strict=False) numerical_tests.append((name, (xval, ), expected, 1e-14)) for lang, commands in valid_lang_commands: if lang == "C": name_expr_C = [("test_floor", floor(x)), ("test_ceil", ceiling(x))] else: name_expr_C = [] run_test("intrinsic_math1", name_expr + name_expr_C, numerical_tests, lang, commands)
def test_intrinsic_math1_codegen(): # not included: log10 name_expr = [ ('test_fabs', abs(x)), ('test_acos', acos(x)), ('test_asin', asin(x)), ('test_atan', atan(x)), ('test_cos', cos(x)), ('test_cosh', cosh(x)), ('test_log', log(x)), ('test_ln', ln(x)), ('test_sin', sin(x)), ('test_sinh', sinh(x)), ('test_sqrt', sqrt(x)), ('test_tan', tan(x)), ('test_tanh', tanh(x)), ] numerical_tests = [] for name, expr in name_expr: for xval in 0.2, 0.5, 0.8: expected = N(expr.subs({x: xval}), strict=False) numerical_tests.append((name, (xval, ), expected, 1e-14)) for lang, commands in valid_lang_commands: if lang == 'C': name_expr_C = [('test_floor', floor(x)), ('test_ceil', ceiling(x))] else: name_expr_C = [] run_test('intrinsic_math1', name_expr + name_expr_C, numerical_tests, lang, commands)
def test_real_imag(): a, b = symbols('a b', extended_real=True) z = a + b*I for deep in [True, False]: assert sin( z).as_real_imag(deep=deep) == (sin(a)*cosh(b), cos(a)*sinh(b)) assert cos( z).as_real_imag(deep=deep) == (cos(a)*cosh(b), -sin(a)*sinh(b)) 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))) 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) assert cot(a).as_real_imag(deep=deep) == (cot(a), 0)
def plot_and_save(name): # implicit plot tests plot_implicit(Eq(y, cos(x)), (x, -5, 5), (y, -2, 2), show=False).save(tmp_file(name)) plot_implicit(Eq(y**2, x**3 - x), (x, -5, 5), (y, -4, 4), show=False).save(tmp_file(name)) plot_implicit(y > 1 / x, (x, -5, 5), (y, -2, 2), show=False).save(tmp_file(name)) plot_implicit(y < 1 / tan(x), (x, -5, 5), (y, -2, 2), show=False).save(tmp_file(name)) plot_implicit(y >= 2 * sin(x) * cos(x), (x, -5, 5), (y, -2, 2), show=False).save(tmp_file(name)) plot_implicit(y <= x**2, (x, -3, 3), (y, -1, 5), show=False).save(tmp_file(name)) plot_implicit(Or(And(x < y, x > y**2), sin(y) > x), show=False).save(tmp_file(name)) plot_implicit(Or(And(x < y, x > y**2), sin(y)), show=False).save(tmp_file(name)) plot_implicit(Or(x < y, sin(x) >= y), show=False).save(tmp_file(name)) # Test all input args for plot_implicit plot_implicit(Eq(y**2, x**3 - x), show=False).save(tmp_file(name)) plot_implicit(Eq(y**2, x**3 - x), adaptive=False, show=False).save(tmp_file(name)) plot_implicit(Eq(y**2, x**3 - x), adaptive=False, points=500, show=False).save(tmp_file(name)) plot_implicit(y > x, (x, -5, 5), show=False).save(tmp_file(name)) plot_implicit(And(y > exp(x), y > x + 2), show=False).save(tmp_file(name)) plot_implicit(Or(y > x, y > -x), show=False).save(tmp_file(name)) plot_implicit(x**2 - 1, (x, -5, 5), show=False).save(tmp_file(name)) plot_implicit(x**2 - 1, show=False).save(tmp_file(name)) plot_implicit(y > x, depth=-5, show=False).save(tmp_file(name)) plot_implicit(y > x, depth=5, show=False).save(tmp_file(name)) plot_implicit(y > cos(x), adaptive=False, show=False).save(tmp_file(name)) plot_implicit(y < cos(x), adaptive=False, show=False).save(tmp_file(name)) plot_implicit(y - cos(pi / x), show=False).save(tmp_file(name)) pytest.raises(ValueError, lambda: plot_implicit(y > x, (x, -1, 1, 2)))
def test_trigsimp_groebner(): c = cos(x) s = sin(x) ex = (4 * s * c + 12 * s + 5 * c**3 + 21 * c**2 + 23 * c + 15) / ( -s * c**2 + 2 * s * c + 15 * s + 7 * c**3 + 31 * c**2 + 37 * c + 21) resnum = (5 * s - 5 * c + 1) resdenom = (8 * s - 6 * c) results = [resnum / resdenom, (-resnum) / (-resdenom)] assert trigsimp_groebner(ex) in results assert trigsimp_groebner(s / c, hints=[tan]) == tan(x) assert trigsimp((-s + 1) / c + c / (-s + 1), method='groebner') == 2 / c assert trigsimp((-s + 1) / c + c / (-s + 1), method='groebner', polynomial=True) == 2 / c # Test quick=False works assert trigsimp_groebner(ex, hints=[2]) in results # test "I" assert trigsimp_groebner(sin(I * x) / cos(I * x), hints=[tanh]) == I * tanh(x) # test hyperbolic / sums assert trigsimp_groebner((tanh(x) + tanh(y)) / (1 + tanh(x) * tanh(y)), hints=[(tanh, x, y)]) == tanh(x + y) # issue sympy/sympy#11062 trigsimp_groebner(csc(x) * sin(x)) # not raises
def test_diff3(): p = Integer(5) e = a*b + sin(b**p) assert e == a*b + sin(b**5) assert e.diff(a) == b assert e.diff(b) == a + 5*b**4*cos(b**5) e = tan(c) assert e == tan(c) assert e.diff(c) in [cos(c)**(-2), 1 + sin(c)**2/cos(c)**2, 1 + tan(c)**2] e = c*log(c) - c assert e == -c + c*log(c) assert e.diff(c) == log(c) e = log(sin(c)) assert e == log(sin(c)) assert e.diff(c) in [sin(c)**(-1)*cos(c), cot(c)] e = 2**a/log(2) assert e.diff(a) == 2**a
def test_numpy_numexpr(): a, b, c = numpy.random.randn(3, 128, 128) # ensure that numpy and numexpr return same value for complicated expression expr = (sin(x) + cos(y) + tan(z)**2 + Abs(z - y)*acos(sin(y*z)) + Abs(y - z)*acosh(2 + exp(y - x)) - sqrt(x**2 + I*y**2)) npfunc = lambdify((x, y, z), expr, modules='numpy') nefunc = lambdify((x, y, z), expr, modules='numexpr') assert numpy.allclose(npfunc(a, b, c), nefunc(a, b, c))
def test_complicated_codegen_f95(): name_expr = [ ("test1", ((sin(x) + cos(y) + tan(z))**7).expand()), ("test2", cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))))), ] result = codegen(name_expr, "F95", "file", header=False, empty=False) assert result[0][0] == "file.f90" expected = ( 'REAL*8 function test1(x, y, z)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'REAL*8, intent(in) :: y\n' 'REAL*8, intent(in) :: z\n' 'test1 = sin(x)**7 + 7*sin(x)**6*cos(y) + 7*sin(x)**6*tan(z) + 21*sin(x) &\n' ' **5*cos(y)**2 + 42*sin(x)**5*cos(y)*tan(z) + 21*sin(x)**5*tan(z) &\n' ' **2 + 35*sin(x)**4*cos(y)**3 + 105*sin(x)**4*cos(y)**2*tan(z) + &\n' ' 105*sin(x)**4*cos(y)*tan(z)**2 + 35*sin(x)**4*tan(z)**3 + 35*sin( &\n' ' x)**3*cos(y)**4 + 140*sin(x)**3*cos(y)**3*tan(z) + 210*sin(x)**3* &\n' ' cos(y)**2*tan(z)**2 + 140*sin(x)**3*cos(y)*tan(z)**3 + 35*sin(x) &\n' ' **3*tan(z)**4 + 21*sin(x)**2*cos(y)**5 + 105*sin(x)**2*cos(y)**4* &\n' ' tan(z) + 210*sin(x)**2*cos(y)**3*tan(z)**2 + 210*sin(x)**2*cos(y) &\n' ' **2*tan(z)**3 + 105*sin(x)**2*cos(y)*tan(z)**4 + 21*sin(x)**2*tan &\n' ' (z)**5 + 7*sin(x)*cos(y)**6 + 42*sin(x)*cos(y)**5*tan(z) + 105* &\n' ' sin(x)*cos(y)**4*tan(z)**2 + 140*sin(x)*cos(y)**3*tan(z)**3 + 105 &\n' ' *sin(x)*cos(y)**2*tan(z)**4 + 42*sin(x)*cos(y)*tan(z)**5 + 7*sin( &\n' ' x)*tan(z)**6 + cos(y)**7 + 7*cos(y)**6*tan(z) + 21*cos(y)**5*tan( &\n' ' z)**2 + 35*cos(y)**4*tan(z)**3 + 35*cos(y)**3*tan(z)**4 + 21*cos( &\n' ' y)**2*tan(z)**5 + 7*cos(y)*tan(z)**6 + tan(z)**7\n' 'end function\n' 'REAL*8 function test2(x, y, z)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'REAL*8, intent(in) :: y\n' 'REAL*8, intent(in) :: z\n' 'test2 = cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))))\n' 'end function\n' ) assert result[0][1] == expected assert result[1][0] == "file.h" expected = ( 'interface\n' 'REAL*8 function test1(x, y, z)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'REAL*8, intent(in) :: y\n' 'REAL*8, intent(in) :: z\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test2(x, y, z)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'REAL*8, intent(in) :: y\n' 'REAL*8, intent(in) :: z\n' 'end function\n' 'end interface\n' ) assert result[1][1] == expected
def test_trigsimp_noncommutative(): A, B = symbols('A,B', commutative=False) assert trigsimp(A - A*sin(x)**2) == A*cos(x)**2 assert trigsimp(A - A*cos(x)**2) == A*sin(x)**2 assert trigsimp(A*sin(x)**2 + A*cos(x)**2) == A assert trigsimp(A + A*tan(x)**2) == A/cos(x)**2 assert trigsimp(A/cos(x)**2 - A) == A*tan(x)**2 assert trigsimp(A/cos(x)**2 - A*tan(x)**2) == A assert trigsimp(A + A*cot(x)**2) == A/sin(x)**2 assert trigsimp(A/sin(x)**2 - A) == A/tan(x)**2 assert trigsimp(A/sin(x)**2 - A*cot(x)**2) == A assert trigsimp(y*A*cos(x)**2 + y*A*sin(x)**2) == y*A assert trigsimp(A*sin(x)/cos(x)) == A*tan(x) assert trigsimp(A*tan(x)*cos(x)) == A*sin(x) assert trigsimp(A*cot(x)**3*sin(x)**3) == A*cos(x)**3 assert trigsimp(y*A*tan(x)**2/sin(x)**2) == y*A/cos(x)**2 assert trigsimp(A*cot(x)/cos(x)) == A/sin(x) assert trigsimp(A*sin(x + y) + A*sin(x - y)) == 2*A*sin(x)*cos(y) assert trigsimp(A*sin(x + y) - A*sin(x - y)) == 2*A*sin(y)*cos(x) assert trigsimp(A*cos(x + y) + A*cos(x - y)) == 2*A*cos(x)*cos(y) assert trigsimp(A*cos(x + y) - A*cos(x - y)) == -2*A*sin(x)*sin(y) assert trigsimp(A*sinh(x + y) + A*sinh(x - y)) == 2*A*sinh(x)*cosh(y) assert trigsimp(A*sinh(x + y) - A*sinh(x - y)) == 2*A*sinh(y)*cosh(x) assert trigsimp(A*cosh(x + y) + A*cosh(x - y)) == 2*A*cosh(x)*cosh(y) assert trigsimp(A*cosh(x + y) - A*cosh(x - y)) == 2*A*sinh(x)*sinh(y) assert trigsimp(A*cos(0.12345)**2 + A*sin(0.12345)**2) == 1.0*A
def test_diff3(): a, b, c = map(Symbol, 'abc') p = Integer(5) e = a*b + sin(b**p) assert e == a*b + sin(b**5) assert e.diff(a) == b assert e.diff(b) == a + 5*b**4*cos(b**5) e = tan(c) assert e == tan(c) assert e.diff(c) in [cos(c)**(-2), 1 + sin(c)**2/cos(c)**2, 1 + tan(c)**2] e = c*log(c) - c assert e == -c + c*log(c) assert e.diff(c) == log(c) e = log(sin(c)) assert e == log(sin(c)) assert e.diff(c) in [sin(c)**(-1)*cos(c), cot(c)] e = 2**a/log(2) assert e.diff(a) == 2**a
def test_sympyissue_21176(): e = x**2 * cot(pi * x) / (x**4 + 1) pt = -sqrt(2) / 2 - sqrt(2) * I / 2 assert residue( e, x, pt) == sqrt(2) * I / Mul(2, -2 + 2 * I, tan(sqrt(2) * pi / 2 + sqrt(2) * I * pi / 2), evaluate=False)
def test_trigsimp3(): assert trigsimp(sin(x) / cos(x)) == tan(x) assert trigsimp(sin(x)**2 / cos(x)**2) == tan(x)**2 assert trigsimp(sin(x)**3 / cos(x)**3) == tan(x)**3 assert trigsimp(sin(x)**10 / cos(x)**10) == tan(x)**10 assert trigsimp(cos(x) / sin(x)) == 1 / tan(x) assert trigsimp(cos(x)**2 / sin(x)**2) == 1 / tan(x)**2 assert trigsimp(cos(x)**10 / sin(x)**10) == 1 / tan(x)**10 assert trigsimp(tan(x)) == trigsimp(sin(x) / cos(x))
def test_trigsimp3(): assert trigsimp(sin(x)/cos(x)) == tan(x) assert trigsimp(sin(x)**2/cos(x)**2) == tan(x)**2 assert trigsimp(sin(x)**3/cos(x)**3) == tan(x)**3 assert trigsimp(sin(x)**10/cos(x)**10) == tan(x)**10 assert trigsimp(cos(x)/sin(x)) == 1/tan(x) assert trigsimp(cos(x)**2/sin(x)**2) == 1/tan(x)**2 assert trigsimp(cos(x)**10/sin(x)**10) == 1/tan(x)**10 assert trigsimp(tan(x)) == trigsimp(sin(x)/cos(x))
def test_trigintegrate_mixed(): assert trigintegrate(sin(x) * sec(x), x) == -log(sin(x)**2 - 1) / 2 assert trigintegrate(sin(x) * csc(x), x) == x assert trigintegrate(sin(x) * cot(x), x) == sin(x) assert trigintegrate(cos(x) * sec(x), x) == x assert trigintegrate(cos(x) * csc(x), x) == log(cos(x)**2 - 1) / 2 assert trigintegrate(cos(x) * tan(x), x) == -cos(x) assert trigintegrate(cos(x)*cot(x), x) == log(cos(x) - 1)/2 \ - log(cos(x) + 1)/2 + cos(x)
def test_exptrigsimp(): def valid(a, b): if not (tn(a, b) and a == b): return False return True assert exptrigsimp(exp(x) + exp(-x)) == 2*cosh(x) assert exptrigsimp(exp(x) - exp(-x)) == 2*sinh(x) e = [cos(x) + I*sin(x), cos(x) - I*sin(x), cosh(x) - sinh(x), cosh(x) + sinh(x)] ok = [exp(I*x), exp(-I*x), exp(-x), exp(x)] assert all(valid(i, j) for i, j in zip( [exptrigsimp(ei) for ei in e], ok)) ue = [cos(x) + sin(x), cos(x) - sin(x), cosh(x) + I*sinh(x), cosh(x) - I*sinh(x)] assert [exptrigsimp(ei) == ei for ei in ue] res = [] ok = [y*tanh(1), 1/(y*tanh(1)), I*y*tan(1), -I/(y*tan(1)), y*tanh(x), 1/(y*tanh(x)), I*y*tan(x), -I/(y*tan(x)), y*tanh(1 + I), 1/(y*tanh(1 + I))] for a in (1, I, x, I*x, 1 + I): w = exp(a) eq = y*(w - 1/w)/(w + 1/w) s = simplify(eq) assert s == exptrigsimp(eq) res.append(s) sinv = simplify(1/eq) assert sinv == exptrigsimp(1/eq) res.append(sinv) assert all(valid(i, j) for i, j in zip(res, ok)) for a in range(1, 3): w = exp(a) e = w + 1/w s = simplify(e) assert s == exptrigsimp(e) assert valid(s, 2*cosh(a)) e = w - 1/w s = simplify(e) assert s == exptrigsimp(e) assert valid(s, 2*sinh(a))
def test_inverses(): pytest.raises(AttributeError, lambda: sin(x).inverse()) pytest.raises(AttributeError, lambda: cos(x).inverse()) assert tan(x).inverse() == atan assert cot(x).inverse() == acot pytest.raises(AttributeError, lambda: csc(x).inverse()) pytest.raises(AttributeError, lambda: sec(x).inverse()) assert asin(x).inverse() == sin assert acos(x).inverse() == cos assert atan(x).inverse() == tan assert acot(x).inverse() == cot
def test_evalc(): x = Symbol("x", extended_real=True) y = Symbol("y", extended_real=True) z = Symbol("z") assert ((x + I*y)**2).expand(complex=True) == x**2 + 2*I*x*y - y**2 assert expand_complex(z**(2*I)) == (re((re(z) + I*im(z))**(2*I)) + I*im((re(z) + I*im(z))**(2*I))) assert expand_complex( z**(2*I), deep=False) == I*im(z**(2*I)) + re(z**(2*I)) assert exp(I*x) != cos(x) + I*sin(x) assert exp(I*x).expand(complex=True) == cos(x) + I*sin(x) assert exp(I*x + y).expand(complex=True) == exp(y)*cos(x) + I*sin(x)*exp(y) assert sin(I*x).expand(complex=True) == I * sinh(x) assert sin(x + I*y).expand(complex=True) == sin(x)*cosh(y) + \ I * sinh(y) * cos(x) assert cos(I*x).expand(complex=True) == cosh(x) assert cos(x + I*y).expand(complex=True) == cos(x)*cosh(y) - \ I * sinh(y) * sin(x) assert tan(I*x).expand(complex=True) == tanh(x) * I assert tan(x + I*y).expand(complex=True) == ( sin(2*x)/(cos(2*x) + cosh(2*y)) + I*sinh(2*y)/(cos(2*x) + cosh(2*y))) assert sinh(I*x).expand(complex=True) == I * sin(x) assert sinh(x + I*y).expand(complex=True) == sinh(x)*cos(y) + \ I * sin(y) * cosh(x) assert cosh(I*x).expand(complex=True) == cos(x) assert cosh(x + I*y).expand(complex=True) == cosh(x)*cos(y) + \ I * sin(y) * sinh(x) assert tanh(I*x).expand(complex=True) == tan(x) * I assert tanh(x + I*y).expand(complex=True) == ( (sinh(x)*cosh(x) + I*cos(y)*sin(y)) / (sinh(x)**2 + cos(y)**2)).expand()
def test_pow_eval(): # XXX Pow does not fully support conversion of negative numbers # to their complex equivalent assert sqrt(-1) == I assert sqrt(-4) == 2*I assert sqrt(+4) == 2 assert cbrt(+8) == 2 assert cbrt(-8) == 2*cbrt(-1) assert sqrt(-2) == I*sqrt(2) assert cbrt(-1) != I assert cbrt(-10) != I*cbrt(10) assert root(-2, 4) != root(2, 4) assert cbrt(64) == 4 assert 64**Rational(2, 3) == 16 assert 24/sqrt(64) == 3 assert cbrt(-27) == 3*cbrt(-1) assert (cos(2) / tan(2))**2 == (cos(2) / tan(2))**2
def test_conjugate(): a = Symbol("a", extended_real=True) b = Symbol("b", extended_real=True) c = Symbol("c", imaginary=True) d = Symbol("d", imaginary=True) x = Symbol('x') z = a + I*b + c + I*d zc = a - I*b - c + I*d assert conjugate(z) == zc assert conjugate(exp(z)) == exp(zc) assert conjugate(exp(I*x)) == exp(-I*conjugate(x)) assert conjugate(z**5) == zc**5 assert conjugate(abs(x)) == abs(x) assert conjugate(sign(z)) == sign(zc) assert conjugate(sin(z)) == sin(zc) assert conjugate(cos(z)) == cos(zc) assert conjugate(tan(z)) == tan(zc) assert conjugate(cot(z)) == cot(zc) assert conjugate(sinh(z)) == sinh(zc) assert conjugate(cosh(z)) == cosh(zc) assert conjugate(tanh(z)) == tanh(zc) assert conjugate(coth(z)) == coth(zc)
def test_ansi_math1_codegen(): # not included: log10 name_expr = [ ("test_fabs", Abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), ("test_ceil", ceiling(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), ("test_floor", floor(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] result = codegen(name_expr, "C", "file", header=False, empty=False) assert result[0][0] == "file.c" assert result[0][1] == ( '#include "file.h"\n#include <math.h>\n' 'double test_fabs(double x) {\n double test_fabs_result;\n test_fabs_result = fabs(x);\n return test_fabs_result;\n}\n' 'double test_acos(double x) {\n double test_acos_result;\n test_acos_result = acos(x);\n return test_acos_result;\n}\n' 'double test_asin(double x) {\n double test_asin_result;\n test_asin_result = asin(x);\n return test_asin_result;\n}\n' 'double test_atan(double x) {\n double test_atan_result;\n test_atan_result = atan(x);\n return test_atan_result;\n}\n' 'double test_ceil(double x) {\n double test_ceil_result;\n test_ceil_result = ceil(x);\n return test_ceil_result;\n}\n' 'double test_cos(double x) {\n double test_cos_result;\n test_cos_result = cos(x);\n return test_cos_result;\n}\n' 'double test_cosh(double x) {\n double test_cosh_result;\n test_cosh_result = cosh(x);\n return test_cosh_result;\n}\n' 'double test_floor(double x) {\n double test_floor_result;\n test_floor_result = floor(x);\n return test_floor_result;\n}\n' 'double test_log(double x) {\n double test_log_result;\n test_log_result = log(x);\n return test_log_result;\n}\n' 'double test_ln(double x) {\n double test_ln_result;\n test_ln_result = log(x);\n return test_ln_result;\n}\n' 'double test_sin(double x) {\n double test_sin_result;\n test_sin_result = sin(x);\n return test_sin_result;\n}\n' 'double test_sinh(double x) {\n double test_sinh_result;\n test_sinh_result = sinh(x);\n return test_sinh_result;\n}\n' 'double test_sqrt(double x) {\n double test_sqrt_result;\n test_sqrt_result = sqrt(x);\n return test_sqrt_result;\n}\n' 'double test_tan(double x) {\n double test_tan_result;\n test_tan_result = tan(x);\n return test_tan_result;\n}\n' 'double test_tanh(double x) {\n double test_tanh_result;\n test_tanh_result = tanh(x);\n return test_tanh_result;\n}\n' ) assert result[1][0] == "file.h" assert result[1][1] == ( '#ifndef PROJECT__FILE__H\n#define PROJECT__FILE__H\n' 'double test_fabs(double x);\ndouble test_acos(double x);\n' 'double test_asin(double x);\ndouble test_atan(double x);\n' 'double test_ceil(double x);\ndouble test_cos(double x);\n' 'double test_cosh(double x);\ndouble test_floor(double x);\n' 'double test_log(double x);\ndouble test_ln(double x);\n' 'double test_sin(double x);\ndouble test_sinh(double x);\n' 'double test_sqrt(double x);\ndouble test_tan(double x);\n' 'double test_tanh(double x);\n#endif\n' )
def test_complicated_codegen(): name_expr = [ ("test1", ((sin(x) + cos(y) + tan(z))**7).expand()), ("test2", cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))))), ] numerical_tests = [] for name, expr in name_expr: for xval, yval, zval in (0.2, 1.3, -0.3), (0.5, -0.2, 0.0), (0.8, 2.1, 0.8): expected = N(expr.subs({x: xval, y: yval, z: zval}), strict=False) numerical_tests.append((name, (xval, yval, zval), expected, 1e-12)) for lang, commands in valid_lang_commands: run_test( "complicated_codegen", name_expr, numerical_tests, lang, commands)
def test_hyper_as_trig(): eq = sinh(x)**2 + cosh(x)**2 t, f = hyper_as_trig(eq) assert f(fu(t)) == cosh(2*x) e, f = hyper_as_trig(tanh(x + y)) assert f(TR12(e)) == (tanh(x) + tanh(y))/(tanh(x)*tanh(y) + 1) d = Dummy() assert o(sinh(x), d) == I*sin(x*d) assert o(tanh(x), d) == I*tan(x*d) assert o(coth(x), d) == cot(x*d)/I assert o(cosh(x), d) == cos(x*d) for func in (sinh, cosh, tanh, coth): h = func(pi) assert i(o(h, d), d) == h # /!\ the _osborne functions are not meant to work # in the o(i(trig, d), d) direction so we just check # that they work as they are supposed to work assert i(cos(x*y), y) == cosh(x) assert i(sin(x*y), y) == sinh(x)/I assert i(tan(x*y), y) == tanh(x)/I assert i(cot(x*y), y) == coth(x)*I assert i(sec(x*y), y) == 1/cosh(x) assert i(csc(x*y), y) == I/sinh(x)
def test_heurisch_trigonometric(): assert heurisch(sin(x), x) == -cos(x) assert heurisch(pi*sin(x) + 1, x) == x - pi*cos(x) assert heurisch(cos(x), x) == sin(x) assert heurisch(tan(x), x) in [ log(1 + tan(x)**2)/2, log(tan(x) + I) + I*x, log(tan(x) - I) - I*x, ] assert heurisch(sin(x)*sin(y), x) == -cos(x)*sin(y) assert heurisch(sin(x)*sin(y), y) == -cos(y)*sin(x) # gives sin(x) in answer when run via setup.py and cos(x) when run via py.test assert heurisch(sin(x)*cos(x), x) in [sin(x)**2 / 2, -cos(x)**2 / 2] assert heurisch(cos(x)/sin(x), x) == log(sin(x)) assert heurisch(x*sin(7*x), x) == sin(7*x) / 49 - x*cos(7*x) / 7 assert heurisch(1/pi/4 * x**2*cos(x), x) == 1/pi/4*(x**2*sin(x) - 2*sin(x) + 2*x*cos(x)) assert heurisch(acos(x/4) * asin(x/4), x) == 2*x - (sqrt(16 - x**2))*asin(x/4) \ + (sqrt(16 - x**2))*acos(x/4) + x*asin(x/4)*acos(x/4)
def test_complicated_m_codegen(): name_expr = ("testlong", [((sin(x) + cos(y) + tan(z))**3).expand(), cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))))]) result = codegen(name_expr, "Octave", header=False, empty=False) assert result[0][0] == "testlong.m" source = result[0][1] expected = ( "function [out1, out2] = testlong(x, y, z)\n" " out1 = sin(x).^3 + 3*sin(x).^2.*cos(y) + 3*sin(x).^2.*tan(z)" " + 3*sin(x).*cos(y).^2 + 6*sin(x).*cos(y).*tan(z) + 3*sin(x).*tan(z).^2" " + cos(y).^3 + 3*cos(y).^2.*tan(z) + 3*cos(y).*tan(z).^2 + tan(z).^3;\n" " out2 = cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))));\n" "end\n" ) assert source == expected
def test_trigsimp_issues(): # issue sympy/sympy#4625 - factor_terms works, too assert trigsimp(sin(x)**3 + cos(x)**2*sin(x)) == sin(x) # issue sympy/sympy#5948 assert trigsimp(diff(integrate(cos(x)/sin(x)**3, x), x)) == \ cos(x)/sin(x)**3 assert trigsimp(diff(integrate(sin(x)/cos(x)**3, x), x)) == \ sin(x)/cos(x)**3 # check integer exponents e = sin(x)**y/cos(x)**y assert trigsimp(e) == e assert trigsimp(e.subs({y: 2})) == tan(x)**2 assert trigsimp(e.subs({x: 1})) == tan(1)**y # check for multiple patterns assert (cos(x)**2/sin(x)**2*cos(y)**2/sin(y)**2).trigsimp() == \ 1/tan(x)**2/tan(y)**2 assert trigsimp(cos(x)/sin(x)*cos(x+y)/sin(x+y)) == \ 1/(tan(x)*tan(x + y)) eq = cos(2)*(cos(3) + 1)**2/(cos(3) - 1)**2 assert trigsimp(eq) == eq.factor() # factor makes denom (-1 + cos(3))**2 assert trigsimp(cos(2)*(cos(3) + 1)**2*(cos(3) - 1)**2) == \ cos(2)*sin(3)**4 # issue sympy/sympy#6789; this generates an expression that formerly caused # trigsimp to hang assert cot(x).equals(tan(x)) is False # nan or the unchanged expression is ok, but not sin(1) z = cos(x)**2 + sin(x)**2 - 1 z1 = tan(x)**2 - 1/cot(x)**2 n = (1 + z1/z) assert trigsimp(sin(n)) != sin(1) eq = x*(n - 1) - x*n assert trigsimp(eq) is nan assert trigsimp(eq, recursive=True) is nan assert trigsimp(1).is_Integer assert trigsimp(-sin(x)**4 - 2*sin(x)**2*cos(x)**2 - cos(x)**4) == -1
def test_mathml_trig(): mml = mp._print(sin(x)) assert mml.childNodes[0].nodeName == 'sin' mml = mp._print(cos(x)) assert mml.childNodes[0].nodeName == 'cos' mml = mp._print(tan(x)) assert mml.childNodes[0].nodeName == 'tan' mml = mp._print(asin(x)) assert mml.childNodes[0].nodeName == 'arcsin' mml = mp._print(acos(x)) assert mml.childNodes[0].nodeName == 'arccos' mml = mp._print(atan(x)) assert mml.childNodes[0].nodeName == 'arctan' mml = mp._print(sinh(x)) assert mml.childNodes[0].nodeName == 'sinh' mml = mp._print(cosh(x)) assert mml.childNodes[0].nodeName == 'cosh' mml = mp._print(tanh(x)) assert mml.childNodes[0].nodeName == 'tanh' mml = mp._print(asinh(x)) assert mml.childNodes[0].nodeName == 'arcsinh' mml = mp._print(atanh(x)) assert mml.childNodes[0].nodeName == 'arctanh' mml = mp._print(acosh(x)) assert mml.childNodes[0].nodeName == 'arccosh'
def test_trigonometric(): n3 = Integer(3) e = (sin(x)**2).diff(x) assert e == 2*sin(x)*cos(x) e = e.subs({x: n3}) assert e == 2*cos(n3)*sin(n3) e = (sin(x)**2).diff(x) assert e == 2*sin(x)*cos(x) e = e.subs({sin(x): cos(x)}) assert e == 2*cos(x)**2 i = Symbol('i', integer=True) assert tan(x).subs({x: pi/2}) is zoo assert cot(x).subs({x: pi}) is zoo assert cot(i*x).subs({x: pi}) is zoo assert tan(i*x).subs({x: pi/2}) == tan(i*pi/2) assert tan(i*x).subs({x: pi/2}).subs({i: 1}) is zoo o = Symbol('o', odd=True) assert tan(o*x).subs({x: pi/2}) == tan(o*pi/2)
def test_pmint_trig(): f = (x - tan(x)) / tan(x)**2 + tan(x) g = -x**2/2 - x/tan(x) + log(tan(x)**2 + 1)/2 assert heurisch(f, x) == g