def test_functional_diffgeom_ch2(): x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', extended_real=True) f = Function('f') assert (R2_p.point_to_coords(R2_r.point([x0, y0])) == Matrix( [sqrt(x0**2 + y0**2), atan2(y0, x0)])) assert (R2_r.point_to_coords(R2_p.point([r0, theta0])) == Matrix( [r0 * cos(theta0), r0 * sin(theta0)])) assert R2_p.jacobian(R2_r, [r0, theta0]) == Matrix( [[cos(theta0), -r0 * sin(theta0)], [sin(theta0), r0 * cos(theta0)]]) field = f(R2.x, R2.y) p1_in_rect = R2_r.point([x0, y0]) p1_in_polar = R2_p.point([sqrt(x0**2 + y0**2), atan2(y0, x0)]) assert field.rcall(p1_in_rect) == f(x0, y0) assert field.rcall(p1_in_polar) == f(x0, y0) p_r = R2_r.point([x0, y0]) p_p = R2_p.point([r0, theta0]) assert R2.x(p_r) == x0 assert R2.x(p_p) == r0 * cos(theta0) assert R2.r(p_p) == r0 assert R2.r(p_r) == sqrt(x0**2 + y0**2) assert R2.theta(p_r) == atan2(y0, x0) h = R2.x * R2.r**2 + R2.y**3 assert h.rcall(p_r) == x0 * (x0**2 + y0**2) + y0**3 assert h.rcall(p_p) == r0**3 * sin(theta0)**3 + r0**3 * cos(theta0)
def test_sympyissue_8514(): a, b, c, = symbols('a b c', positive=True, finite=True) t = symbols('t', positive=True) ft = simplify(inverse_laplace_transform(1/(a*s**2 + b*s + c), s, t)) assert ft.rewrite(atan2) == ((exp(t*(exp(I*atan2(0, -4*a*c + b**2)/2) - exp(-I*atan2(0, -4*a*c + b**2)/2)) * sqrt(Abs(4*a*c - b**2))/(4*a))*exp(t*cos(atan2(0, -4*a*c + b**2)/2) * sqrt(Abs(4*a*c - b**2))/a) + I*sin(t*sin(atan2(0, -4*a*c + b**2)/2) * sqrt(Abs(4*a*c - b**2))/(2*a)) - cos(t*sin(atan2(0, -4*a*c + b**2)/2) * sqrt(Abs(4*a*c - b**2))/(2*a)))*exp(-t*(b + cos(atan2(0, -4*a*c + b**2)/2) * sqrt(Abs(4*a*c - b**2)))/(2*a))/sqrt(-4*a*c + b**2))
def test_atan2_expansion(): assert cancel(atan2(x**2, x + 1).diff(x) - atan(x**2/(x + 1)).diff(x)) == 0 assert cancel(atan(y/x).series(y, 0, 5) - atan2(y, x).series(y, 0, 5) + atan2(0, x) - atan(0)) == O(y**5) assert cancel(atan(y/x).series(x, 1, 4) - atan2(y, x).series(x, 1, 4) + atan2(y, 1) - atan(y)) == O((x - 1)**4, (x, 1)) assert cancel(atan((y + x)/x).series(x, 1, 3) - atan2(y + x, x).series(x, 1, 3) + atan2(1 + y, 1) - atan(1 + y)) == O((x - 1)**3, (x, 1)) assert Matrix([atan2(y, x)]).jacobian([y, x]) == \ Matrix([[x/(y**2 + x**2), -y/(y**2 + x**2)]])
def test_functions_subs(): f, g = symbols('f g', cls=Function) l = Lambda((x, y), sin(x) + y) assert (g(y, x) + cos(x)).subs({g: l}) == sin(y) + x + cos(x) assert (f(x)**2).subs({f: sin}) == sin(x)**2 assert (f(x, y)).subs({f: log}) == log(x, y) assert (f(x, y)).subs({f: sin}) == f(x, y) assert (sin(x) + atan2(x, y)).subs({atan2: f, sin: g}) == \ f(x, y) + g(x) assert (g(f(x + y, x))).subs({f: l, g: Lambda(x, exp(x))}) == exp(x + sin(x + y))
def test_functions_subs(): f, g = symbols('f g', cls=Function) l = Lambda((x, y), sin(x) + y) assert (g(y, x) + cos(x)).subs(g, l) == sin(y) + x + cos(x) assert (f(x)**2).subs(f, sin) == sin(x)**2 assert (f(x, y)).subs(f, log) == log(x, y) assert (f(x, y)).subs(f, sin) == f(x, y) assert (sin(x) + atan2(x, y)).subs([[atan2, f], [sin, g]]) == \ f(x, y) + g(x) assert (g(f(x + y, x))).subs([[f, l], [g, Lambda(x, exp(x))]]) == exp(x + sin(x + y))
def test_instrinsic_math2_codegen(): # not included: frexp, ldexp, modf, fmod name_expr = [ ("test_atan2", atan2(x, y)), ("test_pow", x**y), ] numerical_tests = [] for name, expr in name_expr: for xval, yval in (0.2, 1.3), (0.5, -0.2), (0.8, 0.8): expected = N(expr.subs({x: xval, y: yval}), strict=False) numerical_tests.append((name, (xval, yval), expected, 1e-14)) for lang, commands in valid_lang_commands: run_test("intrinsic_math2", name_expr, numerical_tests, lang, commands)
def test_functions_subs(): f, g = symbols('f g', cls=Function) l = Lambda((x, y), sin(x) + y) assert (g(y, x) + cos(x)).subs({g: l}) == sin(y) + x + cos(x) assert (f(x)**2).subs({f: sin}) == sin(x)**2 assert (f(x, y)).subs({f: log}) == log(x, y) assert (f(x, y)).subs({f: sin}) == f(x, y) assert (sin(x) + atan2(x, y)).subs({atan2: f, sin: g}) == \ f(x, y) + g(x) assert (g(f(x + y, x))).subs({ f: l, g: Lambda(x, exp(x)) }) == exp(x + sin(x + y))
def test_instrinsic_math2_codegen(): # not included: frexp, ldexp, modf, fmod name_expr = [ ("test_atan2", atan2(x, y)), ("test_pow", x**y), ] numerical_tests = [] for name, expr in name_expr: for xval, yval in (0.2, 1.3), (0.5, -0.2), (0.8, 0.8): expected = N(expr.subs(x, xval).subs(y, yval), strict=False) numerical_tests.append((name, (xval, yval), expected, 1e-14)) for lang, commands in valid_lang_commands: run_test("intrinsic_math2", name_expr, numerical_tests, lang, commands)
def test_intrinsic_math2_codegen(): # not included: frexp, ldexp, modf, fmod name_expr = [ ("test_atan2", atan2(x, y)), ("test_pow", x**y), ] result = codegen(name_expr, "F95", "file", header=False, empty=False) assert result[0][0] == "file.f90" expected = ( 'REAL*8 function test_atan2(x, y)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'REAL*8, intent(in) :: y\n' 'test_atan2 = atan2(x, y)\n' 'end function\n' 'REAL*8 function test_pow(x, y)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'REAL*8, intent(in) :: y\n' 'test_pow = x**y\n' 'end function\n' ) assert result[0][1] == expected assert result[1][0] == "file.h" expected = ( 'interface\n' 'REAL*8 function test_atan2(x, y)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'REAL*8, intent(in) :: y\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_pow(x, y)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'REAL*8, intent(in) :: y\n' 'end function\n' 'end interface\n' ) assert result[1][1] == expected
def test_ansi_math2_codegen(): # not included: frexp, ldexp, modf, fmod name_expr = [ ("test_atan2", atan2(x, y)), ("test_pow", x**y), ] 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_atan2(double x, double y) {\n double test_atan2_result;\n test_atan2_result = atan2(x, y);\n return test_atan2_result;\n}\n' 'double test_pow(double x, double y) {\n double test_pow_result;\n test_pow_result = pow(x, y);\n return test_pow_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_atan2(double x, double y);\n' 'double test_pow(double x, double y);\n' '#endif\n' )
def test_issue_8514(): a, b, c, = symbols('a b c', positive=True, finite=True) t = symbols('t', positive=True) ft = simplify(inverse_laplace_transform(1 / (a * s**2 + b * s + c), s, t)) assert ft.rewrite(atan2) == ( (exp(t * (exp(I * atan2(0, -4 * a * c + b**2) / 2) - exp(-I * atan2( 0, -4 * a * c + b**2) / 2)) * sqrt(Abs(4 * a * c - b**2)) / (4 * a)) * exp(t * cos(atan2(0, -4 * a * c + b**2) / 2) * sqrt(Abs(4 * a * c - b**2)) / a) + I * sin(t * sin(atan2(0, -4 * a * c + b**2) / 2) * sqrt(Abs(4 * a * c - b**2)) / (2 * a)) - cos(t * sin(atan2(0, -4 * a * c + b**2) / 2) * sqrt(Abs(4 * a * c - b**2)) / (2 * a))) * exp(-t * (b + cos(atan2(0, -4 * a * c + b**2) / 2) * sqrt(Abs(4 * a * c - b**2))) / (2 * a)) / sqrt(-4 * a * c + b**2))
def test_arg_rewrite(): assert arg(1 + I) == atan2(1, 1) x = Symbol('x', extended_real=True) y = Symbol('y', extended_real=True) assert arg(x + I*y).rewrite(atan2) == atan2(y, x)
def test_point(): p = R2_r.point([x, y]) # TODO assert p.free_symbols() == {x, y} assert p.coords(R2_r) == p.coords() == Matrix([x, y]) assert p.coords(R2_p) == Matrix([sqrt(x**2 + y**2), atan2(y, x)])
def test_cot(): assert cot(nan) == nan assert cot.nargs == FiniteSet(1) assert cot(oo*I) == -I assert cot(-oo*I) == I assert cot(0) == zoo assert cot(2*pi) == zoo assert cot(acot(x)) == x assert cot(atan(x)) == 1 / x assert cot(asin(x)) == sqrt(1 - x**2) / x assert cot(acos(x)) == x / sqrt(1 - x**2) assert cot(atan2(y, x)) == x/y assert cot(pi*I) == -coth(pi)*I assert cot(-pi*I) == coth(pi)*I assert cot(-2*I) == coth(2)*I assert cot(pi) == cot(2*pi) == cot(3*pi) assert cot(-pi) == cot(-2*pi) == cot(-3*pi) assert cot(pi/2) == 0 assert cot(-pi/2) == 0 assert cot(5*pi/2) == 0 assert cot(7*pi/2) == 0 assert cot(pi/3) == 1/sqrt(3) assert cot(-2*pi/3) == 1/sqrt(3) assert cot(+pi/4) == +1 assert cot(-pi/4) == -1 assert cot(17*pi/4) == 1 assert cot(-3*pi/4) == 1 assert cot(pi/6) == sqrt(3) assert cot(-pi/6) == -sqrt(3) assert cot(7*pi/6) == sqrt(3) assert cot(-5*pi/6) == sqrt(3) assert cot(pi/8).simplify() == 1 + sqrt(2) assert cot(3*pi/8).simplify() == -1 + sqrt(2) assert cot(5*pi/8).simplify() == 1 - sqrt(2) assert cot(7*pi/8).simplify() == -1 - sqrt(2) assert cot(pi/12).simplify() == sqrt(3) + 2 assert cot(5*pi/12).simplify() == -sqrt(3) + 2 assert cot(7*pi/12).simplify() == sqrt(3) - 2 assert cot(11*pi/12).simplify() == -sqrt(3) - 2 assert cot(pi/24).radsimp() == sqrt(2) + sqrt(3) + 2 + sqrt(6) assert cot(5*pi/24).radsimp() == -sqrt(2) - sqrt(3) + 2 + sqrt(6) assert cot(7*pi/24).radsimp() == -sqrt(2) + sqrt(3) - 2 + sqrt(6) assert cot(11*pi/24).radsimp() == sqrt(2) - sqrt(3) - 2 + sqrt(6) assert cot(13*pi/24).radsimp() == -sqrt(2) + sqrt(3) + 2 - sqrt(6) assert cot(17*pi/24).radsimp() == sqrt(2) - sqrt(3) + 2 - sqrt(6) assert cot(19*pi/24).radsimp() == sqrt(2) + sqrt(3) - 2 - sqrt(6) assert cot(23*pi/24).radsimp() == -sqrt(2) - sqrt(3) - 2 - sqrt(6) assert 1 == (cot(4*pi/15)*sin(4*pi/15)/cos(4*pi/15)).ratsimp() assert cot(x*I) == -coth(x)*I assert cot(k*pi*I) == -coth(k*pi)*I ni = Symbol('ni', noninteger=True) assert cot(pi*ni/2).is_extended_real is True assert cot(a).is_algebraic is None assert cot(na).is_algebraic is False assert cot(10*pi/7) == cot(3*pi/7) assert cot(11*pi/7) == -cot(3*pi/7) assert cot(-11*pi/7) == cot(3*pi/7) assert cot(39*pi/34) == cot(5*pi/34) assert cot(-41*pi/34) == -cot(7*pi/34) assert cot(x).is_finite is None assert cot(r).is_finite is None i = Symbol('i', imaginary=True, nonzero=True) assert cot(i).is_finite is True assert cot(x).subs({x: 3*pi}) == zoo pytest.raises(ArgumentIndexError, lambda: cot(x).fdiff(2)) # issue sympy/sympy#4547 assert cot(x).fdiff() == -1 - cot(x)**2
def test_sin(): x, y = symbols('x y') assert sin.nargs == FiniteSet(1) assert sin(nan) == nan assert sin(oo*I) == oo*I assert sin(-oo*I) == -oo*I assert sin(oo).args[0] == oo assert sin(0) == 0 assert sin(asin(x)) == x assert sin(atan(x)) == x / sqrt(1 + x**2) assert sin(acos(x)) == sqrt(1 - x**2) assert sin(acot(x)) == 1 / (sqrt(1 + 1 / x**2) * x) assert sin(atan2(y, x)) == y / sqrt(x**2 + y**2) assert sin(pi*I) == sinh(pi)*I assert sin(-pi*I) == -sinh(pi)*I assert sin(-2*I) == -sinh(2)*I assert sin(pi) == 0 assert sin(-pi) == 0 assert sin(2*pi) == 0 assert sin(-2*pi) == 0 assert sin(-3*10**73*pi) == 0 assert sin(7*10**103*pi) == 0 assert sin(pi/2) == 1 assert sin(-pi/2) == -1 assert sin(5*pi/2) == 1 assert sin(7*pi/2) == -1 ne = symbols('ne', integer=True, even=False) e = symbols('e', even=True) assert sin(pi*ne/2) == (-1)**(ne/2 - Rational(1, 2)) assert sin(pi*k/2).func == sin assert sin(pi*e/2) == 0 assert sin(pi*k) == 0 assert sin(pi*k).subs({k: 3}) == sin(pi*k/2).subs({k: 6}) # issue sympy/sympy#8298 assert sin(pi/2*cos(k*pi)) == (-1)**k assert sin(pi/3) == sqrt(3)/2 assert sin(-2*pi/3) == -sqrt(3)/2 assert sin(pi/4) == sqrt(2)/2 assert sin(-pi/4) == -sqrt(2)/2 assert sin(17*pi/4) == sqrt(2)/2 assert sin(-3*pi/4) == -sqrt(2)/2 assert sin(pi/6) == Rational(1, 2) assert sin(-pi/6) == Rational(-1, 2) assert sin(7*pi/6) == Rational(-1, 2) assert sin(-5*pi/6) == Rational(-1, 2) assert sin(1*pi/5) == sqrt((5 - sqrt(5)) / 8) assert sin(2*pi/5) == sqrt((5 + sqrt(5)) / 8) assert sin(3*pi/5) == sin(2*pi/5) assert sin(4*pi/5) == sin(1*pi/5) assert sin(6*pi/5) == -sin(1*pi/5) assert sin(8*pi/5) == -sin(2*pi/5) assert sin(-1273*pi/5) == -sin(2*pi/5) assert sin(pi/8) == sqrt((2 - sqrt(2))/4) assert sin(pi/10) == -Rational(1, 4) + sqrt(5)/4 assert sin(pi/12) == -sqrt(2)/4 + sqrt(6)/4 assert sin(5*pi/12) == sqrt(2)/4 + sqrt(6)/4 assert sin(-7*pi/12) == -sqrt(2)/4 - sqrt(6)/4 assert sin(-11*pi/12) == sqrt(2)/4 - sqrt(6)/4 assert sin(104*pi/105) == sin(pi/105) assert sin(106*pi/105) == -sin(pi/105) assert sin(-104*pi/105) == -sin(pi/105) assert sin(-106*pi/105) == sin(pi/105) assert sin(x*I) == sinh(x)*I assert sin(k*pi) == 0 assert sin(17*k*pi) == 0 assert sin(k*pi*I) == sinh(k*pi)*I assert sin(r).is_real assert sin(c).is_complex assert sin(0, evaluate=False).is_algebraic assert sin(a).is_algebraic is None assert sin(na).is_algebraic is False q = Symbol('q', rational=True) assert sin(pi*q).is_algebraic qz = Symbol('qz', zero=True) qn = Symbol('qn', rational=True, nonzero=True) assert sin(qz).is_rational assert sin(0, evaluate=False).is_rational assert sin(qn).is_rational is False assert sin(q).is_rational is None # issue sympy/sympy#8653 assert isinstance(sin( re(x) - im(y)), sin) is True assert isinstance(sin(-re(x) + im(y)), sin) is False for d in list(range(1, 22)) + [60, 85]: for n in range(d*2 + 1): x = n*pi/d e = abs( float(sin(x)) - sin(float(x)) ) assert e < 1e-12 assert sin(z).taylor_term(3, z, *(z, 0)) == -z**3/6
from .diffgeom import Manifold, Patch, CoordSystem from diofant import sqrt, atan2, acos, sin, cos, Dummy ############################################################################### # R2 ############################################################################### R2 = Manifold('R', 2) # Patch and coordinate systems. R2_origin = Patch('origin', R2) R2_r = CoordSystem('rectangular', R2_origin, ['x', 'y']) R2_p = CoordSystem('polar', R2_origin, ['r', 'theta']) # Connecting the coordinate charts. x, y, r, theta = [Dummy(s) for s in ['x', 'y', 'r', 'theta']] R2_r.connect_to(R2_p, [x, y], [sqrt(x**2 + y**2), atan2(y, x)], inverse=False, fill_in_gaps=False) R2_p.connect_to(R2_r, [r, theta], [r * cos(theta), r * sin(theta)], inverse=False, fill_in_gaps=False) del x, y, r, theta # Defining the basis coordinate functions and adding shortcuts for them to the # manifold and the patch. R2.x, R2.y = R2_origin.x, R2_origin.y = R2_r.x, R2_r.y = R2_r.coord_functions() R2.r, R2.theta = R2_origin.r, R2_origin.theta = R2_p.r, R2_p.theta = R2_p.coord_functions( ) # Defining the basis vector fields and adding shortcuts for them to the # manifold and the patch.
def test_implicit(): x, y = symbols('x,y') assert fcode(sin(x)) == " sin(x)" assert fcode(atan2(x, y)) == " atan2(x, y)" assert fcode(conjugate(x)) == " conjg(x)"
def test_atan2(): assert atan2.nargs == FiniteSet(2) assert atan2(0, 0) == nan assert atan2(0, 1) == 0 assert atan2(1, 1) == pi/4 assert atan2(1, 0) == pi/2 assert atan2(1, -1) == 3*pi/4 assert atan2(0, -1) == pi assert atan2(-1, -1) == -3*pi/4 assert atan2(-1, 0) == -pi/2 assert atan2(-1, 1) == -pi/4 i = symbols('i', imaginary=True) r = symbols('r', extended_real=True) eq = atan2(r, i) ans = -I*log((i + I*r)/sqrt(i**2 + r**2)) reps = ((r, 2), (i, I)) assert eq.subs(reps) == ans.subs(reps) x = Symbol('x', negative=True) y = Symbol('y', negative=True) assert atan2(y, x) == atan(y/x) - pi y = Symbol('y', nonnegative=True) assert atan2(y, x) == atan(y/x) + pi y = Symbol('y') assert atan2(y, x) == atan2(y, x, evaluate=False) u = Symbol('u', positive=True) assert atan2(0, u) == 0 u = Symbol('u', negative=True) assert atan2(0, u) == pi assert atan2(y, oo) == 0 assert atan2(y, -oo) == 2*pi*Heaviside(re(y)) - pi assert atan2(y, x).rewrite(log) == -I*log((x + I*y)/sqrt(x**2 + y**2)) assert atan2(y, x).rewrite(atan) == 2*atan(y/(x + sqrt(x**2 + y**2))) ex = atan2(y, x) - arg(x + I*y) assert ex.subs({x: 2, y: 3}).rewrite(arg) == 0 assert ex.subs({x: 2, y: 3*I}).rewrite(arg) == -pi - I*log(sqrt(5)*I/5) assert ex.subs({x: 2*I, y: 3}).rewrite(arg) == -pi/2 - I*log(sqrt(5)*I) assert ex.subs({x: 2*I, y: 3*I}).rewrite(arg) == -pi + atan(Rational(2, 3)) + atan(Rational(3, 2)) i = symbols('i', imaginary=True) r = symbols('r', extended_real=True) e = atan2(i, r) rewrite = e.rewrite(arg) reps = {i: I, r: -2} assert rewrite == -I*log(abs(I*i + r)/sqrt(abs(i**2 + r**2))) + arg((I*i + r)/sqrt(i**2 + r**2)) assert (e - rewrite).subs(reps).equals(0) r1 = Symbol('r1', real=True, nonzero=True) r2 = Symbol('r2', real=True, nonzero=True) assert atan2(r1, r2).is_real assert atan2(0, r1) == pi*(-Heaviside(r1) + 1) r1 = Symbol('r1', real=True) r2 = Symbol('r2', real=True) assert atan2(r1, r2).is_real is None assert atan2(r1, r2).rewrite(arg) == arg(I*r1 + r2) assert conjugate(atan2(x, y)) == atan2(conjugate(x), conjugate(y)) assert diff(atan2(y, x), x) == -y/(x**2 + y**2) assert diff(atan2(y, x), y) == x/(x**2 + y**2) assert simplify(diff(atan2(y, x).rewrite(log), x)) == -y/(x**2 + y**2) assert simplify(diff(atan2(y, x).rewrite(log), y)) == x/(x**2 + y**2) pytest.raises(ArgumentIndexError, lambda: atan2(x, y).fdiff(3))
def test_cos(): x, y = symbols('x y') assert cos.nargs == FiniteSet(1) assert cos(nan) == nan assert cos(oo) == cos(oo, evaluate=False) assert cos(oo*I) == oo assert cos(-oo*I) == oo assert cos(0) == 1 assert cos(acos(x)) == x assert cos(atan(x)) == 1 / sqrt(1 + x**2) assert cos(asin(x)) == sqrt(1 - x**2) assert cos(acot(x)) == 1 / sqrt(1 + 1 / x**2) assert cos(atan2(y, x)) == x / sqrt(x**2 + y**2) assert cos(pi*I) == cosh(pi) assert cos(-pi*I) == cosh(pi) assert cos(-2*I) == cosh(2) assert cos(pi/2) == 0 assert cos(-pi/2) == 0 assert cos(pi/2) == 0 assert cos(-pi/2) == 0 assert cos((-3*10**73 + 1)*pi/2) == 0 assert cos((7*10**103 + 1)*pi/2) == 0 n = symbols('n', integer=True, even=False) e = symbols('e', even=True) assert cos(pi*n/2) == 0 assert cos(pi*e/2) == (-1)**(e/2) assert cos(pi) == -1 assert cos(-pi) == -1 assert cos(2*pi) == 1 assert cos(5*pi) == -1 assert cos(8*pi) == 1 assert cos(pi/3) == Rational(1, 2) assert cos(-2*pi/3) == Rational(-1, 2) assert cos(pi/4) == sqrt(2)/2 assert cos(-pi/4) == sqrt(2)/2 assert cos(11*pi/4) == -sqrt(2)/2 assert cos(-3*pi/4) == -sqrt(2)/2 assert cos(pi/6) == sqrt(3)/2 assert cos(-pi/6) == sqrt(3)/2 assert cos(7*pi/6) == -sqrt(3)/2 assert cos(-5*pi/6) == -sqrt(3)/2 assert cos(1*pi/5) == (sqrt(5) + 1)/4 assert cos(2*pi/5) == (sqrt(5) - 1)/4 assert cos(3*pi/5) == -cos(2*pi/5) assert cos(4*pi/5) == -cos(1*pi/5) assert cos(6*pi/5) == -cos(1*pi/5) assert cos(8*pi/5) == cos(2*pi/5) assert cos(-1273*pi/5) == -cos(2*pi/5) assert cos(pi/8) == sqrt((2 + sqrt(2))/4) assert cos(pi/12) == sqrt(2)/4 + sqrt(6)/4 assert cos(5*pi/12) == -sqrt(2)/4 + sqrt(6)/4 assert cos(7*pi/12) == sqrt(2)/4 - sqrt(6)/4 assert cos(11*pi/12) == -sqrt(2)/4 - sqrt(6)/4 assert cos(104*pi/105) == -cos(pi/105) assert cos(106*pi/105) == -cos(pi/105) assert cos(-104*pi/105) == -cos(pi/105) assert cos(-106*pi/105) == -cos(pi/105) assert cos(x*I) == cosh(x) assert cos(k*pi*I) == cosh(k*pi) assert cos(r).is_real assert cos(c).is_complex assert cos(0, evaluate=False).is_algebraic assert cos(a).is_algebraic is None assert cos(na).is_algebraic is False q = Symbol('q', rational=True) assert cos(pi*q).is_algebraic assert cos(2*pi/7).is_algebraic qz = Symbol('qz', zero=True) qn = Symbol('qn', rational=True, nonzero=True) assert cos(qz).is_rational assert cos(0, evaluate=False).is_rational assert cos(qn).is_rational is False assert cos(q).is_rational is None assert cos(k*pi) == (-1)**k assert cos(2*k*pi) == 1 for d in list(range(1, 22)) + [60, 85]: for n in range(2*d + 1): x = n*pi/d e = abs( float(cos(x)) - cos(float(x)) ) assert e < 1e-12 assert cos(z).taylor_term(2, z, *(1, 0)) == -z**2/2 pytest.raises(ArgumentIndexError, lambda: cos(z).fdiff(2))
def test_tan(): assert tan(nan) == nan assert tan.nargs == FiniteSet(1) assert tan(oo*I) == I assert tan(-oo*I) == -I assert tan(0) == 0 assert tan(atan(x)) == x assert tan(asin(x)) == x / sqrt(1 - x**2) assert tan(acos(x)) == sqrt(1 - x**2) / x assert tan(acot(x)) == 1 / x assert tan(atan2(y, x)) == y/x assert tan(pi*I) == tanh(pi)*I assert tan(-pi*I) == -tanh(pi)*I assert tan(-2*I) == -tanh(2)*I assert tan(pi) == 0 assert tan(-pi) == 0 assert tan(2*pi) == 0 assert tan(-2*pi) == 0 assert tan(-3*10**73*pi) == 0 assert tan(pi/2) == zoo assert tan(3*pi/2) == zoo assert tan(pi/3) == sqrt(3) assert tan(-2*pi/3) == sqrt(3) assert tan(+pi/4) == +1 assert tan(-pi/4) == -1 assert tan(17*pi/4) == 1 assert tan(-3*pi/4) == 1 assert tan(pi/6) == 1/sqrt(3) assert tan(-pi/6) == -1/sqrt(3) assert tan(7*pi/6) == 1/sqrt(3) assert tan(-5*pi/6) == 1/sqrt(3) assert tan(pi/8).expand() == -1 + sqrt(2) assert tan(3*pi/8).expand() == 1 + sqrt(2) assert tan(5*pi/8).expand() == -1 - sqrt(2) assert tan(7*pi/8).expand() == 1 - sqrt(2) assert tan(pi/12) == -sqrt(3) + 2 assert tan(5*pi/12) == sqrt(3) + 2 assert tan(7*pi/12) == -sqrt(3) - 2 assert tan(11*pi/12) == sqrt(3) - 2 assert tan(pi/24).radsimp() == -2 - sqrt(3) + sqrt(2) + sqrt(6) assert tan(5*pi/24).radsimp() == -2 + sqrt(3) - sqrt(2) + sqrt(6) assert tan(7*pi/24).radsimp() == 2 - sqrt(3) - sqrt(2) + sqrt(6) assert tan(11*pi/24).radsimp() == 2 + sqrt(3) + sqrt(2) + sqrt(6) assert tan(13*pi/24).radsimp() == -2 - sqrt(3) - sqrt(2) - sqrt(6) assert tan(17*pi/24).radsimp() == -2 + sqrt(3) + sqrt(2) - sqrt(6) assert tan(19*pi/24).radsimp() == 2 - sqrt(3) + sqrt(2) - sqrt(6) assert tan(23*pi/24).radsimp() == 2 + sqrt(3) - sqrt(2) - sqrt(6) assert 1 == (tan(8*pi/15)*cos(8*pi/15)/sin(8*pi/15)).ratsimp() assert tan(x*I) == tanh(x)*I assert tan(k*pi) == 0 assert tan(17*k*pi) == 0 assert tan(k*pi*I) == tanh(k*pi)*I ni = Symbol('ni', noninteger=True) assert tan(ni*pi/2).is_real is True assert tan(0, evaluate=False).is_algebraic assert tan(a).is_algebraic is None assert tan(na).is_algebraic is False qz = Symbol('qz', zero=True) qn = Symbol('qn', rational=True, nonzero=True) assert tan(qz).is_rational assert tan(0, evaluate=False).is_rational assert tan(qn).is_rational is False assert tan(x).is_rational is None assert tan(qz).is_algebraic assert tan(10*pi/7, evaluate=False).is_algebraic assert tan(pi*k/2).is_algebraic is None assert tan(10*pi/7) == tan(3*pi/7) assert tan(11*pi/7) == -tan(3*pi/7) assert tan(-11*pi/7) == tan(3*pi/7) assert tan(15*pi/14) == tan(pi/14) assert tan(-15*pi/14) == -tan(pi/14) pytest.raises(ArgumentIndexError, lambda: tan(x).fdiff(2))
def test_implicit(): assert fcode(sin(x)) == ' sin(x)' assert fcode(atan2(x, y)) == ' atan2(x, y)' assert fcode(conjugate(x)) == ' conjg(x)'
def test_implicit(): assert fcode(sin(x)) == " sin(x)" assert fcode(atan2(x, y)) == " atan2(x, y)" assert fcode(conjugate(x)) == " conjg(x)"