def test_nsimplify(): x = Symbol("x") assert nsimplify(0) == 0 assert nsimplify(-1) == -1 assert nsimplify(1) == 1 assert nsimplify(1+x) == 1+x assert nsimplify(2.7) == Rational(27, 10) assert nsimplify(1-GoldenRatio) == (1-sqrt(5))/2 assert nsimplify((1+sqrt(5))/4, [GoldenRatio]) == GoldenRatio/2 assert nsimplify(2/GoldenRatio, [GoldenRatio]) == 2*GoldenRatio - 2 assert nsimplify(exp(5*pi*I/3, evaluate=False)) == sympify('1/2 - sqrt(3)*I/2') assert nsimplify(sin(3*pi/5, evaluate=False)) == sympify('sqrt(sqrt(5)/8 + 5/8)') assert nsimplify(sqrt(atan('1', evaluate=False))*(2+I), [pi]) == sqrt(pi) + sqrt(pi)/2*I assert nsimplify(2 + exp(2*atan('1/4')*I)) == sympify('49/17 + 8*I/17') assert nsimplify(pi, tolerance=0.01) == Rational(22, 7) assert nsimplify(pi, tolerance=0.001) == Rational(355, 113) assert nsimplify(0.33333, tolerance=1e-4) == Rational(1, 3) assert nsimplify(2.0**(1/3.), tolerance=0.001) == Rational(635, 504) assert nsimplify(2.0**(1/3.), tolerance=0.001, full=True) == 2**Rational(1, 3) assert nsimplify(x + .5, rational=True) == Rational(1, 2) + x assert nsimplify(1/.3 + x, rational=True) == Rational(10, 3) + x assert nsimplify(log(3).n(), rational=True) == \ sympify('109861228866811/100000000000000') assert nsimplify(Float(0.272198261287950), [pi,log(2)]) == pi*log(2)/8 assert nsimplify(Float(0.272198261287950).n(3), [pi,log(2)]) == \ -pi/4 - log(2) + S(7)/4 assert nsimplify(x/7.0) == x/7 assert nsimplify(pi/1e2) == pi/100 assert nsimplify(pi/1e2, rational=False) == pi/100.0 assert nsimplify(pi/1e-7) == 10000000*pi
def _expr_big_minus(cls, a, z, n): from sympy import I, pi, exp, sqrt, atan, sin if n.is_even: return (1 + z)**a*exp(2*pi*I*n*a)*sqrt(z)*sin(2*a*atan(sqrt(z))) else: return (1 + z)**a*exp(2*pi*I*n*a)*sqrt(z) \ *sin(2*a*atan(sqrt(z)) - 2*pi*a)
def log_to_atan(f, g): """Convert complex logarithms to real arctangents. Given a real field K and polynomials f and g in K[x], with g != 0, returns a sum h of arctangents of polynomials in K[x], such that: df d f + I g -- = -- I log( ------- ) dx dx f - I g """ if f.degree() < g.degree(): f, g = -g, f f = f.to_field() g = g.to_field() p, q = f.div(g) if q.is_zero: return 2*atan(p.as_expr()) else: s, t, h = g.gcdex(-f) u = (f*s+g*t).quo(h) A = 2*atan(u.as_expr()) return A + log_to_atan(s, t)
def test_evalf_integrals(): assert NS(Integral(x, (x, 2, 5)), 15) == '10.5000000000000' gauss = Integral(exp(-x**2), (x, -oo, oo)) assert NS(gauss, 15) == '1.77245385090552' assert NS(gauss**2 - pi + E*Rational(1,10**20), 15) in ('2.71828182845904e-20', '2.71828182845905e-20') # A monster of an integral from http://mathworld.wolfram.com/DefiniteIntegral.html t = Symbol('t') a = 8*sqrt(3)/(1+3*t**2) b = 16*sqrt(2)*(3*t+1)*(4*t**2+t+1)**Rational(3,2) c = (3*t**2+1)*(11*t**2+2*t+3)**2 d = sqrt(2)*(249*t**2+54*t+65)/(11*t**2+2*t+3)**2 f = a - b/c - d assert NS(Integral(f, (t, 0, 1)), 50) == NS((3*sqrt(2)-49*pi+162*atan(sqrt(2)))/12,50) # http://mathworld.wolfram.com/VardisIntegral.html assert NS(Integral(log(log(1/x))/(1+x+x**2), (x, 0, 1)), 15) == NS('pi/sqrt(3) * log(2*pi**(5/6) / gamma(1/6))', 15) # http://mathworld.wolfram.com/AhmedsIntegral.html assert NS(Integral(atan(sqrt(x**2+2))/(sqrt(x**2+2)*(x**2+1)), (x, 0, 1)), 15) == NS(5*pi**2/96, 15) # http://mathworld.wolfram.com/AbelsIntegral.html assert NS(Integral(x/((exp(pi*x)-exp(-pi*x))*(x**2+1)), (x, 0, oo)), 15) == NS('log(2)/2-1/4',15) # Complex part trimming # http://mathworld.wolfram.com/VardisIntegral.html assert NS(Integral(log(log(sin(x)/cos(x))), (x, pi/4, pi/2)), 15, chop=True) == \ NS('pi/4*log(4*pi**3/gamma(1/4)**4)', 15) # # Endpoints causing trouble (rounding error in integration points -> complex log) assert NS(2+Integral(log(2*cos(x/2)), (x, -pi, pi)), 17, chop=True) == NS(2, 17) assert NS(2+Integral(log(2*cos(x/2)), (x, -pi, pi)), 20, chop=True) == NS(2, 20) assert NS(2+Integral(log(2*cos(x/2)), (x, -pi, pi)), 22, chop=True) == NS(2, 22) # Needs zero handling assert NS(pi - 4*Integral('sqrt(1-x**2)', (x, 0, 1)), 15, maxn=30, chop=True) in ('0.0', '0') # Oscillatory quadrature a = Integral(sin(x)/x**2, (x, 1, oo)).evalf(maxn=15) assert 0.49 < a < 0.51 assert NS(Integral(sin(x)/x**2, (x, 1, oo)), quad='osc') == '0.504067061906928' assert NS(Integral(cos(pi*x+1)/x, (x, -oo, -1)), quad='osc') == '0.276374705640365'
def test_solve_sqrt_3(): R = Symbol("R") eq = sqrt(2) * R * sqrt(1 / (R + 1)) + (R + 1) * (sqrt(2) * sqrt(1 / (R + 1)) - 1) sol = solveset_complex(eq, R) assert sol == FiniteSet( *[ S(5) / 3 + 4 * sqrt(10) * cos(atan(3 * sqrt(111) / 251) / 3) / 3, -sqrt(10) * cos(atan(3 * sqrt(111) / 251) / 3) / 3 + 40 * re(1 / ((-S(1) / 2 - sqrt(3) * I / 2) * (S(251) / 27 + sqrt(111) * I / 9) ** (S(1) / 3))) / 9 + sqrt(30) * sin(atan(3 * sqrt(111) / 251) / 3) / 3 + S(5) / 3 + I * ( -sqrt(30) * cos(atan(3 * sqrt(111) / 251) / 3) / 3 - sqrt(10) * sin(atan(3 * sqrt(111) / 251) / 3) / 3 + 40 * im(1 / ((-S(1) / 2 - sqrt(3) * I / 2) * (S(251) / 27 + sqrt(111) * I / 9) ** (S(1) / 3))) / 9 ), ] ) # the number of real roots will depend on the value of m: for m=1 there are 4 # and for m=-1 there are none. eq = -sqrt((m - q) ** 2 + (-m / (2 * q) + S(1) / 2) ** 2) + sqrt( (-m ** 2 / 2 - sqrt(4 * m ** 4 - 4 * m ** 2 + 8 * m + 1) / 4 - S(1) / 4) ** 2 + (m ** 2 / 2 - m - sqrt(4 * m ** 4 - 4 * m ** 2 + 8 * m + 1) / 4 - S(1) / 4) ** 2 ) raises(NotImplementedError, lambda: solveset_real(eq, q))
def eval(cls, arg): from sympy import atan arg = sympify(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.One: return S.Infinity elif arg is S.NegativeOne: return S.NegativeInfinity elif arg is S.Infinity: return -S.ImaginaryUnit * atan(arg) elif arg is S.NegativeInfinity: return S.ImaginaryUnit * atan(-arg) elif arg.is_negative: return -cls(-arg) else: if arg is S.ComplexInfinity: from sympy.calculus.util import AccumBounds return S.ImaginaryUnit*AccumBounds(-S.Pi/2, S.Pi/2) i_coeff = arg.as_coefficient(S.ImaginaryUnit) if i_coeff is not None: return S.ImaginaryUnit * atan(i_coeff) else: if _coeff_isneg(arg): return -cls(-arg)
def test_issue_2850(): assert manualintegrate(asin(x)*log(x), x) == -x*asin(x) - sqrt(-x**2 + 1) \ + (x*asin(x) + sqrt(-x**2 + 1))*log(x) - Integral(sqrt(-x**2 + 1)/x, x) assert manualintegrate(acos(x)*log(x), x) == -x*acos(x) + sqrt(-x**2 + 1) + \ (x*acos(x) - sqrt(-x**2 + 1))*log(x) + Integral(sqrt(-x**2 + 1)/x, x) assert manualintegrate(atan(x)*log(x), x) == -x*atan(x) + (x*atan(x) - \ log(x**2 + 1)/2)*log(x) + log(x**2 + 1)/2 + Integral(log(x**2 + 1)/x, x)/2
def _expr_big_minus(cls, x, n): from sympy import atan, sqrt, pi if n.is_even: return atan(sqrt(x)) / sqrt(x) else: return (atan(sqrt(x)) - pi) / sqrt(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) assert heurisch(sin(x)/(cos(x)**2+1), x) == -atan(cos(x)) #fixes issue 13723 assert heurisch(1/(cos(x)+2), x) == 2*sqrt(3)*atan(sqrt(3)*tan(x/2)/3)/3 assert heurisch(2*sin(x)*cos(x)/(sin(x)**4 + 1), x) == atan(sqrt(2)*sin(x) - 1) - atan(sqrt(2)*sin(x) + 1) assert heurisch(1/cosh(x), x) == 2*atan(tanh(x/2))
def test_atan2_expansion(): assert cancel(atan2(x + 1, x ** 2).diff(x) - atan((x + 1) / x ** 2).diff(x)) == 0 assert cancel(atan(x / y).series(x, 0, 5) - atan2(x, y).series(x, 0, 5) + atan2(0, y) - atan(0)) == O(x ** 5) assert cancel(atan(x / y).series(y, 1, 4) - atan2(x, y).series(y, 1, 4) + atan2(x, 1) - atan(x)) == O(y ** 4) assert cancel( atan((x + y) / y).series(y, 1, 3) - atan2(x + y, y).series(y, 1, 3) + atan2(1 + x, 1) - atan(1 + x) ) == O(y ** 3) assert Matrix([atan2(x, y)]).jacobian([x, y]) == Matrix([[y / (x ** 2 + y ** 2), -x / (x ** 2 + y ** 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 ** 4) 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 ** 3) assert Matrix([atan2(y, x)]).jacobian([y, x]) == Matrix([[x / (y ** 2 + x ** 2), -y / (y ** 2 + x ** 2)]])
def test_atan2(): assert refine(atan2(y, x), Q.real(y) & Q.positive(x)) == atan(y/x) assert refine(atan2(y, x), Q.negative(y) & Q.positive(x)) == atan(y/x) assert refine(atan2(y, x), Q.negative(y) & Q.negative(x)) == atan(y/x) - pi assert refine(atan2(y, x), Q.positive(y) & Q.negative(x)) == atan(y/x) + pi assert refine(atan2(y, x), Q.zero(y) & Q.negative(x)) == pi assert refine(atan2(y, x), Q.positive(y) & Q.zero(x)) == pi/2 assert refine(atan2(y, x), Q.negative(y) & Q.zero(x)) == -pi/2 assert refine(atan2(y, x), Q.zero(y) & Q.zero(x)) == nan
def test_manualintegrate_parts(): assert manualintegrate(exp(x) * sin(x), x) == \ (exp(x) * sin(x)) / 2 - (exp(x) * cos(x)) / 2 assert manualintegrate(2*x*cos(x), x) == 2*x*sin(x) + 2*cos(x) assert manualintegrate(x * log(x), x) == x**2*log(x)/2 - x**2/4 assert manualintegrate(log(x), x) == x * log(x) - x assert manualintegrate((3*x**2 + 5) * exp(x), x) == \ -6*x*exp(x) + (3*x**2 + 5)*exp(x) + 6*exp(x) assert manualintegrate(atan(x), x) == x*atan(x) - log(x**2 + 1)/2
def test_atan2(): assert atan2.nargs == FiniteSet(2) assert atan2(0, 0) == S.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', 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(2/S(3)) + atan(3/S(2)) i = symbols('i', imaginary=True) r = symbols('r', 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) 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)
def test_invert_real(): x = Symbol('x', real=True) x = Dummy(real=True) n = Symbol('n') d = Dummy() assert solveset(abs(x) - n, x) == solveset(abs(x) - d, x) == EmptySet() n = Symbol('n', real=True) assert invert_real(x + 3, y, x) == (x, FiniteSet(y - 3)) assert invert_real(x*3, y, x) == (x, FiniteSet(y / 3)) assert invert_real(exp(x), y, x) == (x, FiniteSet(log(y))) assert invert_real(exp(3*x), y, x) == (x, FiniteSet(log(y) / 3)) assert invert_real(exp(x + 3), y, x) == (x, FiniteSet(log(y) - 3)) assert invert_real(exp(x) + 3, y, x) == (x, FiniteSet(log(y - 3))) assert invert_real(exp(x)*3, y, x) == (x, FiniteSet(log(y / 3))) assert invert_real(log(x), y, x) == (x, FiniteSet(exp(y))) assert invert_real(log(3*x), y, x) == (x, FiniteSet(exp(y) / 3)) assert invert_real(log(x + 3), y, x) == (x, FiniteSet(exp(y) - 3)) assert invert_real(Abs(x), y, x) == (x, FiniteSet(-y, y)) assert invert_real(2**x, y, x) == (x, FiniteSet(log(y)/log(2))) assert invert_real(2**exp(x), y, x) == (x, FiniteSet(log(log(y)/log(2)))) assert invert_real(x**2, y, x) == (x, FiniteSet(sqrt(y), -sqrt(y))) assert invert_real(x**Rational(1, 2), y, x) == (x, FiniteSet(y**2)) raises(ValueError, lambda: invert_real(x, x, x)) raises(ValueError, lambda: invert_real(x**pi, y, x)) raises(ValueError, lambda: invert_real(S.One, y, x)) assert invert_real(x**31 + x, y, x) == (x**31 + x, FiniteSet(y)) assert invert_real(Abs(x**31 + x + 1), y, x) == (x**31 + x, FiniteSet(-y - 1, y - 1)) assert invert_real(tan(x), y, x) == \ (x, imageset(Lambda(n, n*pi + atan(y)), S.Integers)) assert invert_real(tan(exp(x)), y, x) == \ (x, imageset(Lambda(n, log(n*pi + atan(y))), S.Integers)) assert invert_real(cot(x), y, x) == \ (x, imageset(Lambda(n, n*pi + acot(y)), S.Integers)) assert invert_real(cot(exp(x)), y, x) == \ (x, imageset(Lambda(n, log(n*pi + acot(y))), S.Integers)) assert invert_real(tan(tan(x)), y, x) == \ (tan(x), imageset(Lambda(n, n*pi + atan(y)), S.Integers)) x = Symbol('x', positive=True) assert invert_real(x**pi, y, x) == (x, FiniteSet(y**(1/pi)))
def test_nsimplify(): x = Symbol("x") assert nsimplify(0) == 0 assert nsimplify(-1) == -1 assert nsimplify(1) == 1 assert nsimplify(1 + x) == 1 + x assert nsimplify(2.7) == Rational(27, 10) assert nsimplify(1 - GoldenRatio) == (1 - sqrt(5)) / 2 assert nsimplify((1 + sqrt(5)) / 4, [GoldenRatio]) == GoldenRatio / 2 assert nsimplify(2 / GoldenRatio, [GoldenRatio]) == 2 * GoldenRatio - 2 assert nsimplify(exp(5 * pi * I / 3, evaluate=False)) == sympify("1/2 - sqrt(3)*I/2") assert nsimplify(sin(3 * pi / 5, evaluate=False)) == sympify("sqrt(sqrt(5)/8 + 5/8)") assert nsimplify(sqrt(atan("1", evaluate=False)) * (2 + I), [pi]) == sqrt(pi) + sqrt(pi) / 2 * I assert nsimplify(2 + exp(2 * atan("1/4") * I)) == sympify("49/17 + 8*I/17") assert nsimplify(pi, tolerance=0.01) == Rational(22, 7) assert nsimplify(pi, tolerance=0.001) == Rational(355, 113) assert nsimplify(0.33333, tolerance=1e-4) == Rational(1, 3) assert nsimplify(2.0 ** (1 / 3.0), tolerance=0.001) == Rational(635, 504) assert nsimplify(2.0 ** (1 / 3.0), tolerance=0.001, full=True) == 2 ** Rational(1, 3) assert nsimplify(x + 0.5, rational=True) == Rational(1, 2) + x assert nsimplify(1 / 0.3 + x, rational=True) == Rational(10, 3) + x assert nsimplify(log(3).n(), rational=True) == sympify("109861228866811/100000000000000") assert nsimplify(Float(0.272198261287950), [pi, log(2)]) == pi * log(2) / 8 assert nsimplify(Float(0.272198261287950).n(3), [pi, log(2)]) == -pi / 4 - log(2) + S(7) / 4 assert nsimplify(x / 7.0) == x / 7 assert nsimplify(pi / 1e2) == pi / 100 assert nsimplify(pi / 1e2, rational=False) == pi / 100.0 assert nsimplify(pi / 1e-7) == 10000000 * pi assert not nsimplify(factor(-3.0 * z ** 2 * (z ** 2) ** (-2.5) + 3 * (z ** 2) ** (-1.5))).atoms(Float) e = x ** 0.0 assert e.is_Pow and nsimplify(x ** 0.0) == 1 assert nsimplify(3.333333, tolerance=0.1, rational=True) == Rational(10, 3) assert nsimplify(3.333333, tolerance=0.01, rational=True) == Rational(10, 3) assert nsimplify(3.666666, tolerance=0.1, rational=True) == Rational(11, 3) assert nsimplify(3.666666, tolerance=0.01, rational=True) == Rational(11, 3) assert nsimplify(33, tolerance=10, rational=True) == Rational(33) assert nsimplify(33.33, tolerance=10, rational=True) == Rational(30) assert nsimplify(37.76, tolerance=10, rational=True) == Rational(40) assert nsimplify(-203.1) == -S(2031) / 10 assert nsimplify(0.2, tolerance=0) == S.One / 5 assert nsimplify(-0.2, tolerance=0) == -S.One / 5 assert nsimplify(0.2222, tolerance=0) == S(1111) / 5000 assert nsimplify(-0.2222, tolerance=0) == -S(1111) / 5000 # issue 7211, PR 4112 assert nsimplify(S(2e-8)) == S(1) / 50000000 # issue 7322 direct test assert nsimplify(1e-42, rational=True) != 0 # issue 10336 inf = Float("inf") infs = (-oo, oo, inf, -inf) for i in infs: ans = sign(i) * oo assert nsimplify(i) == ans assert nsimplify(i + x) == x + ans
def test_manualintegrate_inversetrig(): # atan assert manualintegrate(exp(x) / (1 + exp(2*x)), x) == atan(exp(x)) assert manualintegrate(1 / (4 + 9 * x**2), x) == atan(3 * x/2) / 6 assert manualintegrate(1 / (16 + 16 * x**2), x) == atan(x) / 16 assert manualintegrate(1 / (4 + x**2), x) == atan(x / 2) / 2 assert manualintegrate(1 / (1 + 4 * x**2), x) == atan(2*x) / 2 assert manualintegrate(1/(a + b*x**2), x) == \ Piecewise(((sqrt(a/b)*atan(x*sqrt(b/a))/a), And(a > 0, b > 0))) assert manualintegrate(1/(4 + b*x**2), x) == \ Piecewise((sqrt(1/b)*atan(sqrt(b)*x/2)/2, b > 0)) assert manualintegrate(1/(a + 4*x**2), x) == \ Piecewise((atan(2*x*sqrt(1/a))/(2*sqrt(a)), a > 0)) assert manualintegrate(1/(4 + 4*x**2), x) == atan(x) / 4 # asin assert manualintegrate(1/sqrt(1-x**2), x) == asin(x) assert manualintegrate(1/sqrt(4-4*x**2), x) == asin(x)/2 assert manualintegrate(3/sqrt(1-9*x**2), x) == asin(3*x) assert manualintegrate(1/sqrt(4-9*x**2), x) == asin(3*x/2)/3 # asinh assert manualintegrate(1/sqrt(x**2 + 1), x) == \ asinh(x) assert manualintegrate(1/sqrt(x**2 + 4), x) == \ asinh(x/2) assert manualintegrate(1/sqrt(4*x**2 + 4), x) == \ asinh(x)/2 assert manualintegrate(1/sqrt(4*x**2 + 1), x) == \ asinh(2*x)/2 assert manualintegrate(1/sqrt(a*x**2 + 1), x) == \ Piecewise((sqrt(-1/a)*asin(x*sqrt(-a)), a < 0), (sqrt(1/a)*asinh(sqrt(a)*x), a > 0)) assert manualintegrate(1/sqrt(a + x**2), x) == \ Piecewise((asinh(x*sqrt(1/a)), a > 0), (acosh(x*sqrt(-1/a)), a < 0)) # acosh assert manualintegrate(1/sqrt(x**2 - 1), x) == \ acosh(x) assert manualintegrate(1/sqrt(x**2 - 4), x) == \ acosh(x/2) assert manualintegrate(1/sqrt(4*x**2 - 4), x) == \ acosh(x)/2 assert manualintegrate(1/sqrt(9*x**2 - 1), x) == \ acosh(3*x)/3 assert manualintegrate(1/sqrt(a*x**2 - 4), x) == \ Piecewise((sqrt(1/a)*acosh(sqrt(a)*x/2), a > 0)) assert manualintegrate(1/sqrt(-a + 4*x**2), x) == \ Piecewise((asinh(2*x*sqrt(-1/a))/2, -a > 0), (acosh(2*x*sqrt(1/a))/2, -a < 0)) # piecewise assert manualintegrate(1/sqrt(a-b*x**2), x) == \ Piecewise((sqrt(a/b)*asin(x*sqrt(b/a))/sqrt(a), And(-b < 0, a > 0)), (sqrt(-a/b)*asinh(x*sqrt(-b/a))/sqrt(a), And(-b > 0, a > 0)), (sqrt(a/b)*acosh(x*sqrt(b/a))/sqrt(-a), And(-b > 0, a < 0))) assert manualintegrate(1/sqrt(a + b*x**2), x) == \ Piecewise((sqrt(-a/b)*asin(x*sqrt(-b/a))/sqrt(a), And(a > 0, b < 0)), (sqrt(a/b)*asinh(x*sqrt(b/a))/sqrt(a), And(a > 0, b > 0)), (sqrt(-a/b)*acosh(x*sqrt(-b/a))/sqrt(-a), And(a < 0, b > 0)))
def test_main_trig_functions_numeric(self): print "\n\n\n" + " Test if sin, cos and tan and inverses Work Numerically ".center(75, "#") from sympy import symbols, sin, cos, tan, asin, acos, atan x, y = symbols('x,y') test_expr = sin(x) + cos(x) + tan(x) + asin(y) + acos(y) + atan(y) target_expr = sin(x) + cos(x) + tan(x) + asin(y) + acos(y) + atan(y) print "Target expression: '%s'" % target_expr print "Test expression: '%s'" % test_expr equal = api.numeric_equality(test_expr, target_expr) self.assertTrue(equal, "Expected expressions to be found numerically equal!") print " PASS ".center(75, "#")
def test_fps__hyper(): f = sin(x) assert fps(f, x).truncate() == x - x ** 3 / 6 + x ** 5 / 120 + O(x ** 6) f = cos(x) assert fps(f, x).truncate() == 1 - x ** 2 / 2 + x ** 4 / 24 + O(x ** 6) f = exp(x) assert fps(f, x).truncate() == 1 + x + x ** 2 / 2 + x ** 3 / 6 + x ** 4 / 24 + x ** 5 / 120 + O(x ** 6) f = atan(x) assert fps(f, x).truncate() == x - x ** 3 / 3 + x ** 5 / 5 + O(x ** 6) f = exp(acos(x)) assert fps(f, x).truncate() == ( exp(pi / 2) - x * exp(pi / 2) + x ** 2 * exp(pi / 2) / 2 - x ** 3 * exp(pi / 2) / 3 + 5 * x ** 4 * exp(pi / 2) / 24 - x ** 5 * exp(pi / 2) / 6 + O(x ** 6) ) f = exp(acosh(x)) assert fps(f, x).truncate() == I + x - I * x ** 2 / 2 - I * x ** 4 / 8 + O(x ** 6) f = atan(1 / x) assert fps(f, x).truncate() == pi / 2 - x + x ** 3 / 3 - x ** 5 / 5 + O(x ** 6) f = x * atan(x) - log(1 + x ** 2) / 2 assert fps(f, x, rational=False).truncate() == x ** 2 / 2 - x ** 4 / 12 + O(x ** 6) f = log(1 + x) assert fps(f, x, rational=False).truncate() == x - x ** 2 / 2 + x ** 3 / 3 - x ** 4 / 4 + x ** 5 / 5 + O(x ** 6) f = airyai(x ** 2) assert fps(f, x).truncate() == ( 3 ** Rational(5, 6) * gamma(Rational(1, 3)) / (6 * pi) - 3 ** Rational(2, 3) * x ** 2 / (3 * gamma(Rational(1, 3))) + O(x ** 6) ) f = exp(x) * sin(x) assert fps(f, x).truncate() == x + x ** 2 + x ** 3 / 3 - x ** 5 / 30 + O(x ** 6) f = exp(x) * sin(x) / x assert fps(f, x).truncate() == 1 + x + x ** 2 / 3 - x ** 4 / 30 - x ** 5 / 90 + O(x ** 6) f = sin(x) * cos(x) assert fps(f, x).truncate() == x - 2 * x ** 3 / 3 + 2 * x ** 5 / 15 + O(x ** 6)
def test_rational_algorithm(): f = 1 / ((x - 1) ** 2 * (x - 2)) assert rational_algorithm(f, x, k) == (-2 ** (-k - 1) + 1 - (factorial(k + 1) / factorial(k)), 0, 0) f = (1 + x + x ** 2 + x ** 3) / ((x - 1) * (x - 2)) assert rational_algorithm(f, x, k) == (-15 * 2 ** (-k - 1) + 4, x + 4, 0) f = z / (y * m - m * x - y * x + x ** 2) assert rational_algorithm(f, x, k) == (((-y ** (-k - 1) * z) / (y - m)) + ((m ** (-k - 1) * z) / (y - m)), 0, 0) f = x / (1 - x - x ** 2) assert rational_algorithm(f, x, k) is None assert rational_algorithm(f, x, k, full=True) == ( ((-Rational(1, 2) + sqrt(5) / 2) ** (-k - 1) * (-sqrt(5) / 10 + Rational(1, 2))) + ((-sqrt(5) / 2 - Rational(1, 2)) ** (-k - 1) * (sqrt(5) / 10 + Rational(1, 2))), 0, 0, ) f = 1 / (x ** 2 + 2 * x + 2) assert rational_algorithm(f, x, k) is None assert rational_algorithm(f, x, k, full=True) == ( (I * (-1 + I) ** (-k - 1)) / 2 - (I * (-1 - I) ** (-k - 1)) / 2, 0, 0, ) f = log(1 + x) assert rational_algorithm(f, x, k) == (-(-1) ** (-k) / k, 0, 1) f = atan(x) assert rational_algorithm(f, x, k) is None assert rational_algorithm(f, x, k, full=True) == (((I * I ** (-k)) / 2 - (I * (-I) ** (-k)) / 2) / k, 0, 1) f = x * atan(x) - log(1 + x ** 2) / 2 assert rational_algorithm(f, x, k) is None assert rational_algorithm(f, x, k, full=True) == ( ((I * I ** (-k + 1)) / 2 - (I * (-I) ** (-k + 1)) / 2) / (k * (k - 1)), 0, 2, ) f = log((1 + x) / (1 - x)) / 2 - atan(x) assert rational_algorithm(f, x, k) is None assert rational_algorithm(f, x, k, full=True) == ( (-(-1) ** (-k) / 2 - (I * I ** (-k)) / 2 + (I * (-I) ** (-k)) / 2 + Rational(1, 2)) / k, 0, 1, ) assert rational_algorithm(cos(x), x, k) is None
def test_fps__rational(): assert fps(1/x) == (1/x) assert fps((x**2 + x + 1) / x**3, dir=-1) == (x**2 + x + 1) / x**3 f = 1 / ((x - 1)**2 * (x - 2)) assert fps(f, x).truncate() == \ (-Rational(1, 2) - 5*x/4 - 17*x**2/8 - 49*x**3/16 - 129*x**4/32 - 321*x**5/64 + O(x**6)) f = (1 + x + x**2 + x**3) / ((x - 1) * (x - 2)) assert fps(f, x).truncate() == \ (Rational(1, 2) + 5*x/4 + 17*x**2/8 + 49*x**3/16 + 113*x**4/32 + 241*x**5/64 + O(x**6)) f = x / (1 - x - x**2) assert fps(f, x, full=True).truncate() == \ x + x**2 + 2*x**3 + 3*x**4 + 5*x**5 + O(x**6) f = 1 / (x**2 + 2*x + 2) assert fps(f, x, full=True).truncate() == \ Rational(1, 2) - x/2 + x**2/4 - x**4/8 + x**5/8 + O(x**6) f = log(1 + x) assert fps(f, x).truncate() == \ x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6) assert fps(f, x, dir=1).truncate() == fps(f, x, dir=-1).truncate() assert fps(f, x, 2).truncate() == \ (log(3) - Rational(2, 3) - (x - 2)**2/18 + (x - 2)**3/81 - (x - 2)**4/324 + (x - 2)**5/1215 + x/3 + O((x - 2)**6, (x, 2))) assert fps(f, x, 2, dir=-1).truncate() == \ (log(3) - Rational(2, 3) - (-x + 2)**2/18 - (-x + 2)**3/81 - (-x + 2)**4/324 - (-x + 2)**5/1215 + x/3 + O((x - 2)**6, (x, 2))) f = atan(x) assert fps(f, x, full=True).truncate() == x - x**3/3 + x**5/5 + O(x**6) assert fps(f, x, full=True, dir=1).truncate() == \ fps(f, x, full=True, dir=-1).truncate() assert fps(f, x, 2, full=True).truncate() == \ (atan(2) - Rational(2, 5) - 2*(x - 2)**2/25 + 11*(x - 2)**3/375 - 6*(x - 2)**4/625 + 41*(x - 2)**5/15625 + x/5 + O((x - 2)**6, (x, 2))) assert fps(f, x, 2, full=True, dir=-1).truncate() == \ (atan(2) - Rational(2, 5) - 2*(-x + 2)**2/25 - 11*(-x + 2)**3/375 - 6*(-x + 2)**4/625 - 41*(-x + 2)**5/15625 + x/5 + O((x - 2)**6, (x, 2))) f = x*atan(x) - log(1 + x**2) / 2 assert fps(f, x, full=True).truncate() == x**2/2 - x**4/12 + O(x**6) f = log((1 + x) / (1 - x)) / 2 - atan(x) assert fps(f, x, full=True).truncate(n=10) == 2*x**3/3 + 2*x**7/7 + O(x**10)
def test_atan(): assert atan(nan) == nan assert atan(oo) == pi/2 assert atan(-oo) == -pi/2 assert atan(0) == 0 assert atan(1) == pi/4 assert atan(sqrt(3)) == pi/3 assert atan(oo) == pi/2 assert atan(x).diff(x) == 1/(1 + x**2) assert atan(r).is_real is True assert atan(-2*I) == -I*atanh(2)
def test_issue_1572_1364_1368(): assert solve((sqrt(x**2 - 1) - 2)) in ([sqrt(5), -sqrt(5)], [-sqrt(5), sqrt(5)]) assert set(solve((2**exp(y**2/x) + 2)/(x**2 + 15), y)) == set([ -sqrt(x)*sqrt(-log(log(2)) + log(log(2) + I*pi)), sqrt(x)*sqrt(-log(log(2)) + log(log(2) + I*pi))]) C1, C2 = symbols('C1 C2') f = Function('f') assert solve(C1 + C2/x**2 - exp(-f(x)), f(x)) == [log(x**2/(C1*x**2 + C2))] a = Symbol('a') E = S.Exp1 assert solve(1 - log(a + 4*x**2), x) in ( [-sqrt(-a + E)/2, sqrt(-a + E)/2], [sqrt(-a + E)/2, -sqrt(-a + E)/2] ) assert solve(log(a**(-3) - x**2)/a, x) in ( [-sqrt(-1 + a**(-3)), sqrt(-1 + a**(-3))], [sqrt(-1 + a**(-3)), -sqrt(-1 + a**(-3))],) assert solve(1 - log(a + 4*x**2), x) in ( [-sqrt(-a + E)/2, sqrt(-a + E)/2], [sqrt(-a + E)/2, -sqrt(-a + E)/2],) assert set(solve(( a**2 + 1) * (sin(a*x) + cos(a*x)), x)) == set([-pi/(4*a), 3*pi/(4*a)]) assert solve(3 - (sinh(a*x) + cosh(a*x)), x) == [2*atanh(S.Half)/a] assert set(solve(3 - (sinh(a*x) + cosh(a*x)**2), x)) == \ set([ 2*atanh(-1 + sqrt(2))/a, 2*atanh(S(1)/2 + sqrt(5)/2)/a, 2*atanh(-sqrt(2) - 1)/a, 2*atanh(-sqrt(5)/2 + S(1)/2)/a ]) assert solve(atan(x) - 1) == [tan(1)]
def test_acos_rewrite(): assert acos(x).rewrite(log) == pi/2 + I*log(I*x + sqrt(1 - x**2)) assert acos(x).rewrite(atan) == \ atan(sqrt(1 - x**2)/x) + (pi/2)*(1 - x*sqrt(1/x**2)) assert acos(0).rewrite(atan) == S.Pi/2 assert acos(0.5).rewrite(atan) == acos(0.5).rewrite(log) assert acos(x).rewrite(asin) == S.Pi/2 - asin(x)
def test_atan2(): assert atan2(0, 0) == S.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 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))) 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) assert isinstance(atan2(2, 3*I).n(), atan2)
def eval_trigsubstitution(theta, func, rewritten, substep, restriction, integrand, symbol): func = func.subs(sympy.sec(theta), 1/sympy.cos(theta)) trig_function = list(func.find(TrigonometricFunction)) assert len(trig_function) == 1 trig_function = trig_function[0] relation = sympy.solve(symbol - func, trig_function) assert len(relation) == 1 numer, denom = sympy.fraction(relation[0]) if isinstance(trig_function, sympy.sin): opposite = numer hypotenuse = denom adjacent = sympy.sqrt(denom**2 - numer**2) inverse = sympy.asin(relation[0]) elif isinstance(trig_function, sympy.cos): adjacent = numer hypotenuse = denom opposite = sympy.sqrt(denom**2 - numer**2) inverse = sympy.acos(relation[0]) elif isinstance(trig_function, sympy.tan): opposite = numer adjacent = denom hypotenuse = sympy.sqrt(denom**2 + numer**2) inverse = sympy.atan(relation[0]) substitution = [ (sympy.sin(theta), opposite/hypotenuse), (sympy.cos(theta), adjacent/hypotenuse), (sympy.tan(theta), opposite/adjacent), (theta, inverse) ] return sympy.Piecewise( (_manualintegrate(substep).subs(substitution).trigsimp(), restriction) )
def test_issue_6746(): y = Symbol('y') n = Symbol('n') assert manualintegrate(y**x, x) == \ Piecewise((x, Eq(log(y), 0)), (y**x/log(y), True)) assert manualintegrate(y**(n*x), x) == \ Piecewise( (x, Eq(n, 0)), (Piecewise( (n*x, Eq(log(y), 0)), (y**(n*x)/log(y), True))/n, True)) assert manualintegrate(exp(n*x), x) == \ Piecewise((x, Eq(n, 0)), (exp(n*x)/n, True)) y = Symbol('y', positive=True) assert manualintegrate((y + 1)**x, x) == (y + 1)**x/log(y + 1) y = Symbol('y', zero=True) assert manualintegrate((y + 1)**x, x) == x y = Symbol('y') n = Symbol('n', nonzero=True) assert manualintegrate(y**(n*x), x) == \ Piecewise((n*x, Eq(log(y), 0)), (y**(n*x)/log(y), True))/n y = Symbol('y', positive=True) assert manualintegrate((y + 1)**(n*x), x) == \ (y + 1)**(n*x)/(n*log(y + 1)) a = Symbol('a', negative=True) assert manualintegrate(1 / (a + b*x**2), x) == \ Piecewise((atan(x/sqrt(a/b))/(b*sqrt(a/b)), a/b > 0), \ (-acoth(x/sqrt(-a/b))/(b*sqrt(-a/b)), And(a/b < 0, x**2 > -a/b)), \ (-atanh(x/sqrt(-a/b))/(b*sqrt(-a/b)), And(a/b < 0, x**2 < -a/b)))
def test_intrinsic_math1_codegen(): # not included: log10 from sympy import acos, asin, atan, ceiling, cos, cosh, floor, log, ln, \ sin, sinh, sqrt, tan, tanh, N x = symbols('x') 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)) 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_messy(): from sympy import (laplace_transform, Si, Shi, Chi, atan, Piecewise, acoth, E1, besselj, acosh, asin, And, re, fourier_transform, sqrt) assert laplace_transform(Si(x), x, s) == ((-atan(s) + pi/2)/s, 0, True) assert laplace_transform(Shi(x), x, s) == (acoth(s)/s, 1, True) # where should the logs be simplified? assert laplace_transform(Chi(x), x, s) == \ ((log(s**(-2)) - log((s**2 - 1)/s**2))/(2*s), 1, True) # TODO maybe simplify the inequalities? assert laplace_transform(besselj(a, x), x, s)[1:] == \ (0, And(S(0) < re(a/2) + S(1)/2, S(0) < re(a/2) + 1)) # NOTE s < 0 can be done, but argument reduction is not good enough yet assert fourier_transform(besselj(1, x)/x, x, s, noconds=False) == \ (Piecewise((0, 4*abs(pi**2*s**2) > 1), (2*sqrt(-4*pi**2*s**2 + 1), True)), s > 0) # TODO FT(besselj(0,x)) - conditions are messy (but for acceptable reasons) # - folding could be better assert integrate(E1(x)*besselj(0, x), (x, 0, oo), meijerg=True) == \ log(1 + sqrt(2)) assert integrate(E1(x)*besselj(1, x), (x, 0, oo), meijerg=True) == \ log(S(1)/2 + sqrt(2)/2) assert integrate(1/x/sqrt(1 - x**2), x, meijerg=True) == \ Piecewise((-acosh(1/x), 1 < abs(x**(-2))), (I*asin(1/x), True))
def test_heuristic(): x = Symbol("x", real=True) assert heuristics(sin(1 / x) + atan(x), x, 0, '+') == AccumBounds(-1, 1) assert limit(log(2 + sqrt(atan(x)) * sqrt(sin(1 / x))), x, 0) == log(2)
def eval_arctan(a, b, c, integrand, symbol): return a / b * 1 / sympy.sqrt(c / b) * sympy.atan( symbol / sympy.sqrt(c / b))
def test_heuristic_function_sum(): eq = f(x).diff(x) - (3*(1 + x**2/f(x)**2)*atan(f(x)/x) + (1 - 2*f(x))/x + (1 - 3*f(x))*(x/f(x)**2)) i = infinitesimals(eq, hint='function_sum') assert i == [{eta(x, f(x)): f(x)**(-2) + x**(-2), xi(x, f(x)): 0}] assert checkinfsol(eq, i)[0]
def test_Abs(): raises(TypeError, lambda: Abs(Interval(2, 3))) # issue 8717 x, y = symbols('x,y') assert sign(sign(x)) == sign(x) assert sign(x*y).func is sign assert Abs(0) == 0 assert Abs(1) == 1 assert Abs(-1) == 1 assert Abs(I) == 1 assert Abs(-I) == 1 assert Abs(nan) == nan assert Abs(zoo) == oo assert Abs(I * pi) == pi assert Abs(-I * pi) == pi assert Abs(I * x) == Abs(x) assert Abs(-I * x) == Abs(x) assert Abs(-2*x) == 2*Abs(x) assert Abs(-2.0*x) == 2.0*Abs(x) assert Abs(2*pi*x*y) == 2*pi*Abs(x*y) assert Abs(conjugate(x)) == Abs(x) assert conjugate(Abs(x)) == Abs(x) assert Abs(x).expand(complex=True) == sqrt(re(x)**2 + im(x)**2) a = Symbol('a', positive=True) assert Abs(2*pi*x*a) == 2*pi*a*Abs(x) assert Abs(2*pi*I*x*a) == 2*pi*a*Abs(x) x = Symbol('x', real=True) n = Symbol('n', integer=True) assert Abs((-1)**n) == 1 assert x**(2*n) == Abs(x)**(2*n) assert Abs(x).diff(x) == sign(x) assert abs(x) == Abs(x) # Python built-in assert Abs(x)**3 == x**2*Abs(x) assert Abs(x)**4 == x**4 assert ( Abs(x)**(3*n)).args == (Abs(x), 3*n) # leave symbolic odd unchanged assert (1/Abs(x)).args == (Abs(x), -1) assert 1/Abs(x)**3 == 1/(x**2*Abs(x)) assert Abs(x)**-3 == Abs(x)/(x**4) assert Abs(x**3) == x**2*Abs(x) assert Abs(I**I) == exp(-pi/2) assert Abs((4 + 5*I)**(6 + 7*I)) == 68921*exp(-7*atan(S(5)/4)) y = Symbol('y', real=True) assert Abs(I**y) == 1 y = Symbol('y') assert Abs(I**y) == exp(-pi*im(y)/2) x = Symbol('x', imaginary=True) assert Abs(x).diff(x) == -sign(x) eq = -sqrt(10 + 6*sqrt(3)) + sqrt(1 + sqrt(3)) + sqrt(3 + 3*sqrt(3)) # if there is a fast way to know when you can and when you cannot prove an # expression like this is zero then the equality to zero is ok assert abs(eq).func is Abs or abs(eq) == 0 # but sometimes it's hard to do this so it's better not to load # abs down with tests that will be very slow q = 1 + sqrt(2) - 2*sqrt(3) + 1331*sqrt(6) p = expand(q**3)**Rational(1, 3) d = p - q assert abs(d).func is Abs or abs(d) == 0 assert Abs(4*exp(pi*I/4)) == 4 assert Abs(3**(2 + I)) == 9 assert Abs((-3)**(1 - I)) == 3*exp(pi) assert Abs(oo) is oo assert Abs(-oo) is oo assert Abs(oo + I) is oo assert Abs(oo + I*oo) is oo a = Symbol('a', algebraic=True) t = Symbol('t', transcendental=True) x = Symbol('x') assert re(a).is_algebraic assert re(x).is_algebraic is None assert re(t).is_algebraic is False assert Abs(x).fdiff() == sign(x) raises(ArgumentIndexError, lambda: Abs(x).fdiff(2)) # doesn't have recursion error arg = sqrt(acos(1 - I)*acos(1 + I)) assert abs(arg) == arg # special handling to put Abs in denom assert abs(1/x) == 1/Abs(x) e = abs(2/x**2) assert e.is_Mul and e == 2/Abs(x**2) assert unchanged(Abs, y/x) assert unchanged(Abs, x/(x + 1)) assert unchanged(Abs, x*y) p = Symbol('p', positive=True) assert abs(x/p) == abs(x)/p # coverage assert unchanged(Abs, Symbol('x', real=True)**y)
def test_asin_rewrite(): assert asin(x).rewrite(log) == -I * log(I * x + sqrt(1 - x**2)) assert asin(x).rewrite(atan) == 2 * atan(x / (1 + sqrt(1 - x**2))) assert asin(x).rewrite(acos) == S.Pi / 2 - acos(x)
def test_atan_rewrite(): assert atan(x).rewrite(log) == I * log((1 - I * x) / (1 + I * x)) / 2
def test_nsimplify(): x = Symbol("x") assert nsimplify(0) == 0 assert nsimplify(-1) == -1 assert nsimplify(1) == 1 assert nsimplify(1 + x) == 1 + x assert nsimplify(2.7) == Rational(27, 10) assert nsimplify(1 - GoldenRatio) == (1 - sqrt(5)) / 2 assert nsimplify((1 + sqrt(5)) / 4, [GoldenRatio]) == GoldenRatio / 2 assert nsimplify(2 / GoldenRatio, [GoldenRatio]) == 2 * GoldenRatio - 2 assert nsimplify(exp(5*pi*I/3, evaluate=False)) == \ sympify('1/2 - sqrt(3)*I/2') assert nsimplify(sin(3*pi/5, evaluate=False)) == \ sympify('sqrt(sqrt(5)/8 + 5/8)') assert nsimplify(sqrt(atan('1', evaluate=False))*(2 + I), [pi]) == \ sqrt(pi) + sqrt(pi)/2*I assert nsimplify(2 + exp(2 * atan('1/4') * I)) == sympify('49/17 + 8*I/17') assert nsimplify(pi, tolerance=0.01) == Rational(22, 7) assert nsimplify(pi, tolerance=0.001) == Rational(355, 113) assert nsimplify(0.33333, tolerance=1e-4) == Rational(1, 3) assert nsimplify(2.0**(1 / 3.), tolerance=0.001) == Rational(635, 504) assert nsimplify(2.0**(1/3.), tolerance=0.001, full=True) == \ 2**Rational(1, 3) assert nsimplify(x + .5, rational=True) == Rational(1, 2) + x assert nsimplify(1 / .3 + x, rational=True) == Rational(10, 3) + x assert nsimplify(log(3).n(), rational=True) == \ sympify('109861228866811/100000000000000') assert nsimplify(Float(0.272198261287950), [pi, log(2)]) == pi * log(2) / 8 assert nsimplify(Float(0.272198261287950).n(3), [pi, log(2)]) == \ -pi/4 - log(2) + S(7)/4 assert nsimplify(x / 7.0) == x / 7 assert nsimplify(pi / 1e2) == pi / 100 assert nsimplify(pi / 1e2, rational=False) == pi / 100.0 assert nsimplify(pi / 1e-7) == 10000000 * pi assert not nsimplify( factor(-3.0 * z**2 * (z**2)**(-2.5) + 3 * (z**2)**(-1.5))).atoms(Float) e = x**0.0 assert e.is_Pow and nsimplify(x**0.0) == 1 assert nsimplify(3.333333, tolerance=0.1, rational=True) == Rational(10, 3) assert nsimplify(3.333333, tolerance=0.01, rational=True) == Rational(10, 3) assert nsimplify(3.666666, tolerance=0.1, rational=True) == Rational(11, 3) assert nsimplify(3.666666, tolerance=0.01, rational=True) == Rational(11, 3) assert nsimplify(33, tolerance=10, rational=True) == Rational(33) assert nsimplify(33.33, tolerance=10, rational=True) == Rational(30) assert nsimplify(37.76, tolerance=10, rational=True) == Rational(40) assert nsimplify(-203.1) == -S(2031) / 10 assert nsimplify(.2, tolerance=0) == S.One / 5 assert nsimplify(-.2, tolerance=0) == -S.One / 5 assert nsimplify(.2222, tolerance=0) == S(1111) / 5000 assert nsimplify(-.2222, tolerance=0) == -S(1111) / 5000 # issue 7211, PR 4112 assert nsimplify(S(2e-8)) == S(1) / 50000000 # issue 7322 direct test assert nsimplify(1e-42, rational=True) != 0 # issue 10336 inf = Float('inf') infs = (-oo, oo, inf, -inf) for i in infs: ans = sign(i) * oo assert nsimplify(i) == ans assert nsimplify(i + x) == x + ans assert nsimplify(0.33333333, rational=True, rational_conversion='exact') == Rational(0.33333333) # Make sure nsimplify on expressions uses full precision assert nsimplify( pi.evalf(100) * x, rational_conversion='exact').evalf(100) == pi.evalf(100) * x
def _ex_atan(self, e): return sp.atan(self.ex(e[0]))
def test_aseries_trig(): assert cancel( gruntz(1 / log(atan(x)), x, oo) - 1 / (log(pi) + log(S(1) / 2))) == 0 assert gruntz(1 / acot(x), x, -oo) == -oo
def test_issue689(): assert integrate(1 / (1 + x**2), x) == atan(x)
def AXIMOCdesigner(p_e, F_T, gamma, M_e, T_c, R, r_c, num_of_char_lines, numiter, theta_max_rad, L, pressure_type, q_factor): global outbox fig = Figure(figsize=(8, 5), dpi=100) c_v = float(R / (gamma - 1)) c_p = float(c_v + R) p_c = float(p_e/(1+((gamma-1)/2)*M_e**2)**(-gamma/(gamma-1))) rho_c = float(p_c/(R*T_c)) rho_e = float(rho_c*(1+((gamma-1)/2)*M_e**2)**(-1/(gamma-1))) p_t = float(p_c * ((2 / (gamma + 1)) ** (gamma / (gamma - 1)))) T_e = float(T_c/(p_c/p_e)**((gamma-1)/gamma)) u_e = M_e * sp.sqrt(gamma * R * T_e) A_e = float(F_T / (u_e ** 2 * rho_e)) M_t = float(1.0001) A_t = float(A_e/sp.sqrt((1/M_e**2)*((2/(gamma+1))*(1+((gamma-1)/2)*M_e**2))**((gamma+1)/(gamma-1)))) r_e = float(sp.sqrt(A_e/sp.pi)) r_t = float(sp.sqrt(A_t/sp.pi)) alpha = float(sp.sqrt(2 / ((gamma + 1) * 1.5 * r_t * r_t))) x_t = float(0) print(x_t) if r_cset.get() == 1: r_c = float(sp.sqrt(3)*r_t) dec3 = Decimal(10) ** -3 dec4 = Decimal(10) ** -4 dec1 = Decimal(10) ** -2 warnings.filterwarnings("ignore") # _________________________________MOC_________________________________________ book = openpyxl.load_workbook('export.xlsx') sheet = book.active v_PMerad = float(sp.sqrt((gamma + 1) / (gamma - 1))) * sp.atan( sp.sqrt((gamma - 1) * (M_e ** 2 - 1) / (gamma + 1))) - sp.atan(sp.sqrt(M_e ** 2 - 1)) v_PMedeg = float(math.degrees(v_PMerad)) theta_max_deg = float(math.degrees(theta_max_rad)) first_point = x_t # _____________________________________________________________________________ z = 0 x_list = [] y_list = [] p_xx_list = [] M_xx_list = [] v_PM_xx_rad_list = [] T_xx_list = [] V_xx_list = [] mu_xx_rad_list = [] theta_xx_rad_list = [] K_neg_xx_rad_list = [] K_pos_xx_rad_list = [] K_neg_xx_deg_list = [] K_pos_xx_deg_list = [] theta_xx_deg_list = [] v_PM_xx_deg_list = [] mu_xx_deg_list = [] xerr_list = [] yerr_list = [] thetaerr_list = [] Verr_list = [] iteration_list = [] sheet.cell(row=1, column=1).value = "K_neg" sheet.cell(row=1, column=2).value = "K_pos" sheet.cell(row=1, column=3).value = "Theta" sheet.cell(row=1, column=4).value = "v" sheet.cell(row=1, column=5).value = "M" sheet.cell(row=1, column=6).value = "mu" sheet.cell(row=1, column=7).value = "V" sheet.cell(row=1, column=8).value = "x" sheet.cell(row=1, column=9).value = "y" sheet.cell(row=1, column=10).value = "p" sheet.cell(row=1, column=11).value = "T" if num_of_char_lines > 80: outbox = Text(tab1, borderwidth=2, width=90, height=10, fg="yellow") outbox.grid(row=26, column=0, columnspan=100, padx=5, pady=40) outbox.insert(END, "CAUTION: For number of characteristic lines above 80, the plot window and plot resizing functions may take longer to respond. It is recommended that the user is patient." + '\n') outbox.see("end") for x in np.linspace(first_point, L, num=num_of_char_lines, endpoint=True): x_list.append(x) y = float(0) y_list.append(y) if pressure_type == 'parabolic': p_xx = float(mp.e ** ((L ** (-2)) * (x - L) ** 2 * (sp.ln(p_t) - sp.ln(p_e)) + ln(p_e))) if pressure_type == 'cubic': p_xx = float(sp.exp(((q_factor * L + 2 * (sp.ln(p_t) - sp.ln(p_e))) / L ** 3) * x ** 3 - ((2 * q_factor * L + 3 * (sp.ln(p_t) - sp.ln(p_e))) / L ** 2) * x ** 2 + q_factor * x + sp.ln(p_t))) p_xx_list.append(p_xx) M_xx = float(sp.sqrt(((p_c / p_xx) ** ((gamma - 1) / gamma) - 1) * (2 / (gamma - 1)))) M_xx_list.append(M_xx) v_PM_xx_rad = float((math.sqrt((gamma + 1) / (gamma - 1))) * sp.atan( math.sqrt((gamma - 1) * (M_xx ** 2 - 1) / (gamma + 1))) - sp.atan(math.sqrt(M_xx ** 2 - 1))) v_PM_xx_rad_list.append(v_PM_xx_rad) T_xx = T_c * (p_xx / p_c) ** ((gamma - 1) / gamma) T_xx_list.append(T_xx) V_xx = float(M_xx * sp.sqrt(gamma * R * T_xx)) V_xx_list.append(V_xx) mu_xx_rad = float(sp.asin(1 / M_xx)) mu_xx_rad_list.append(mu_xx_rad) theta_xx_rad = float(0) theta_xx_rad_list.append(theta_xx_rad) K_neg_xx_rad = float(theta_xx_rad + v_PM_xx_rad) K_neg_xx_rad_list.append(K_neg_xx_rad) K_pos_xx_rad = float(theta_xx_rad - v_PM_xx_rad) K_pos_xx_rad_list.append(K_pos_xx_rad) plt.plot(x_list, y_list, 'k.', markersize=1) z += 1 w = 1 for g in range(0, z, 1): sheet.cell(row=w + 1, column=1).value = math.degrees(K_neg_xx_rad_list[g]) sheet.cell(row=w + 1, column=2).value = math.degrees(K_pos_xx_rad_list[g]) sheet.cell(row=w + 1, column=3).value = math.degrees(theta_xx_rad_list[g]) sheet.cell(row=w + 1, column=4).value = math.degrees(v_PM_xx_rad_list[g]) sheet.cell(row=w + 1, column=5).value = M_xx_list[g] sheet.cell(row=w + 1, column=6).value = math.degrees(mu_xx_rad_list[g]) sheet.cell(row=w + 1, column=7).value = V_xx_list[g] sheet.cell(row=w + 1, column=8).value = x_list[g] sheet.cell(row=w + 1, column=9).value = y_list[g] sheet.cell(row=w + 1, column=10).value = p_xx_list[g] sheet.cell(row=w + 1, column=11).value = T_xx_list[g] w = w + z + 1 - g for j in range(z - 1, 0, -1): f = 0 progress['value'] = 0 perbox.delete(0, END) tab1.update_idletasks() percentage = int(round(float(abs((j/(z)*100)-100)))) percentagestr = str(percentage) strtotal = str(percentagestr+str('%')) perbox.insert(0, strtotal) progress['value'] = percentage tab1.update_idletasks() root.update() if j == 1: perbox.delete(0, END) progress['value'] = 100 perbox.insert(0, 'Done') strtotal = str('') tab1.update_idletasks() root.update() remaining.delete(0, j) remaining.insert(0, j) for i in range(0, j, 1): if j == z - 1: x = float((y_list[i] - y_list[i + 1] + x_list[i + 1] * sp.tan( theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]) - x_list[i] * sp.tan( theta_xx_rad_list[i] + mu_xx_rad_list[i])) / ( sp.tan(theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]) - sp.tan( theta_xx_rad_list[i] + mu_xx_rad_list[i]))) y = float(y_list[i] + (x - x_list[i]) * sp.tan(theta_xx_rad_list[i] + mu_xx_rad_list[i])) A = float((sp.tan(mu_xx_rad_list[i]) * sp.sin(mu_xx_rad_list[i]) * sp.sin(theta_xx_rad_list[i])) / ( y * sp.cos(theta_xx_rad_list[i] + mu_xx_rad_list[i]))) B = float((sp.tan(mu_xx_rad_list[i + 1]) * sp.sin(mu_xx_rad_list[i + 1]) * sp.sin( theta_xx_rad_list[i + 1])) / (y * sp.cos(theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]))) V = float(((sp.cot(mu_xx_rad_list[i]) * (1 + (A * (x - x_list[i])))) + ( sp.cot(mu_xx_rad_list[i + 1]) * (1 + (B * (x - x_list[i + 1])))) + theta_xx_rad_list[ i + 1] - theta_xx_rad_list[i]) / ((sp.cot(mu_xx_rad_list[i]) / V_xx_list[i]) + ( sp.cot(mu_xx_rad_list[i + 1]) / V_xx_list[i + 1]))) theta = float(theta_xx_rad_list[i] + sp.cot(mu_xx_rad_list[i]) * ( ((V - V_xx_list[i]) / V_xx_list[i]) - (A * (x - x_list[i])))) T = float(T_c - (V ** 2 / (2 * c_p))) a = float(sp.sqrt(gamma * R * T)) M = float(V / a) mu = float(sp.asin(1 / M)) v = float((math.sqrt((gamma + 1) / (gamma - 1))) * sp.atan( math.sqrt((gamma - 1) * (M ** 2 - 1) / (gamma + 1))) - sp.atan(math.sqrt(M ** 2 - 1))) K_neg = float(theta + v) K_pos = float(theta - v) counting = 1 # --------------------iterations for iteration in range(1, numiter + 1, 1): x_2 = float((y_list[i] - y_list[i + 1] + x_list[i + 1] * 0.5 * ( sp.tan(theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]) + sp.tan(theta - mu)) - x_list[i] * 0.5 * (sp.tan(theta_xx_rad_list[i] + mu_xx_rad_list[i]) + sp.tan( theta + mu))) / (0.5 * ( sp.tan(theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]) - sp.tan( theta_xx_rad_list[i] + mu_xx_rad_list[i]) + sp.tan(theta - mu) - sp.tan(theta + mu)))) y_2 = float(y_list[i] + (x - x_list[i]) * 0.5 * ( sp.tan(theta_xx_rad_list[i] + mu_xx_rad_list[i]) + sp.tan(theta + mu))) A_2A = float( (sp.tan(mu_xx_rad_list[i]) * sp.sin(mu_xx_rad_list[i]) * sp.sin(theta_xx_rad_list[i])) / ( y * sp.cos(theta_xx_rad_list[i] + mu_xx_rad_list[i]))) A_2C = float((sp.tan(mu) * sp.sin(mu) * sp.sin(theta)) / (y * sp.cos(theta + mu))) B_2B = float((sp.tan(mu_xx_rad_list[i + 1]) * sp.sin(mu_xx_rad_list[i + 1]) * sp.sin( theta_xx_rad_list[i + 1])) / (y * sp.cos(theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]))) B_2C = float((sp.tan(mu) * sp.sin(mu) * sp.sin(theta)) / (y * sp.cos(theta - mu))) A_2 = float((2 / (sp.tan(mu_xx_rad_list[i]) + sp.tan(mu))) * ( ((2 * V_xx_list[i]) / (V_xx_list[i] + V)) + ((A_2A + A_2C) / 2) * (x - x_list[i]))) B_2 = float((2 / (sp.tan(mu_xx_rad_list[i + 1]) + sp.tan(mu))) * ( ((2 * V_xx_list[i + 1]) / (V_xx_list[i + 1] + V)) + ((B_2B + B_2C) / 2) * ( x - x_list[i + 1]))) V_2 = float((A_2 + B_2 + theta_xx_rad_list[i + 1] - theta_xx_rad_list[i]) / (4 * ( (V_xx_list[i] + V) ** (-1) * (sp.tan(mu_xx_rad_list[i]) + sp.tan(mu)) ** (-1) + ( V_xx_list[i + 1] + V) ** (-1) * ( sp.tan(mu_xx_rad_list[i + 1]) + sp.tan(mu)) ** (-1)))) theta_2 = float(theta_xx_rad_list[i] + (2 / (sp.tan(mu_xx_rad_list[i]) + sp.tan(mu))) * ( ((2 * (V_2 - V_xx_list[i])) / (V + V_xx_list[i])) - ((A_2A + A_2C) / 2) * ( x - x_list[i]))) T_2 = float(T_c - (V_2 ** 2 / (2 * c_p))) a_2 = float(sp.sqrt(gamma * R * T_2)) M_2 = float(V_2 / a_2) mu_2 = float(sp.asin(1 / M_2)) v_2 = float((math.sqrt((gamma + 1) / (gamma - 1))) * sp.atan( math.sqrt((gamma - 1) * (M_2 ** 2 - 1) / (gamma + 1))) - sp.atan(math.sqrt(M_2 ** 2 - 1))) K_neg_2 = float(theta_2 + v_2) K_pos_2 = float(theta_2 - v_2) xdiff = float(x - x_2) if xdiff == 0: xerr = float(0) else: xerr = float(x_2 / xdiff) ydiff = float(y - y_2) if ydiff == 0: yerr = float(0) else: yerr = float(y_2 / ydiff) thetadiff = float(theta - theta_2) if thetadiff == 0: thetaerr = float(0) else: thetaerr = float(theta_2 / thetadiff) Vdiff = float(V - V_2) if Vdiff == 0: Verr = float(0) else: Verr = float(V_2 / Vdiff) x = x_2 y = y_2 V = V_2 theta = theta_2 T = T_2 a = a_2 M = M_2 mu = mu_2 v = v_2 K_neg = K_neg_2 K_pos = K_pos_2 counting += 1 else: x = float((y_list[i] - y_list[i + 1] + x_list[i + 1] * sp.tan( theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]) - x_list[i] * sp.tan( theta_xx_rad_list[i] + mu_xx_rad_list[i])) / ( sp.tan(theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]) - sp.tan( theta_xx_rad_list[i] + mu_xx_rad_list[i]))) y = float(y_list[i] + (x - x_list[i]) * sp.tan(theta_xx_rad_list[i] + mu_xx_rad_list[i])) A = float((sp.tan(mu_xx_rad_list[i]) * sp.sin(mu_xx_rad_list[i]) * sp.sin(theta_xx_rad_list[i])) / ( y_list[i] * sp.cos(theta_xx_rad_list[i] + mu_xx_rad_list[i]))) B = float((sp.tan(mu_xx_rad_list[i + 1]) * sp.sin(mu_xx_rad_list[i + 1]) * sp.sin( theta_xx_rad_list[i + 1])) / ( y_list[i + 1] * sp.cos(theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]))) V = float(((sp.cot(mu_xx_rad_list[i]) * (1 + (A * (x - x_list[i])))) + ( sp.cot(mu_xx_rad_list[i + 1]) * (1 + (B * (x - x_list[i + 1])))) + theta_xx_rad_list[ i + 1] - theta_xx_rad_list[i]) / ((sp.cot(mu_xx_rad_list[i]) / V_xx_list[i]) + ( sp.cot(mu_xx_rad_list[i + 1]) / V_xx_list[i + 1]))) theta = float(theta_xx_rad_list[i] + sp.cot(mu_xx_rad_list[i]) * ( ((V - V_xx_list[i]) / V_xx_list[i]) - (A * (x - x_list[i])))) T = float(T_c - (V ** 2 / (2 * c_p))) a = float(sp.sqrt(gamma * R * T)) M = float(V / a) v = float((math.sqrt((gamma + 1) / (gamma - 1))) * sp.atan( math.sqrt((gamma - 1) * (M ** 2 - 1) / (gamma + 1))) - sp.atan(math.sqrt(M ** 2 - 1))) K_neg = float(theta + v) K_pos = float(theta - v) mu = float(sp.asin(1 / M)) kkk = 0 # --------------------iterations for iteration in range(1, numiter + 1, 1): if iteration == numiter + 1: kkk = kkk + numiter + 1 iteration_num = kkk + iteration x_2 = float((y_list[i] - y_list[i + 1] + x_list[i + 1] * 0.5 * ( sp.tan(theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]) + sp.tan(theta - mu)) - x_list[i] * 0.5 * (sp.tan(theta_xx_rad_list[i] + mu_xx_rad_list[i]) + sp.tan( theta + mu))) / (0.5 * ( sp.tan(theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]) - sp.tan( theta_xx_rad_list[i] + mu_xx_rad_list[i]) + sp.tan(theta - mu) - sp.tan(theta + mu)))) y_2 = float(y_list[i] + (x - x_list[i]) * 0.5 * ( sp.tan(theta_xx_rad_list[i] + mu_xx_rad_list[i]) + sp.tan(theta + mu))) A_2A = float( (sp.tan(mu_xx_rad_list[i]) * sp.sin(mu_xx_rad_list[i]) * sp.sin(theta_xx_rad_list[i])) / ( y_list[i] * sp.cos(theta_xx_rad_list[i] + mu_xx_rad_list[i]))) A_2C = float((sp.tan(mu) * sp.sin(mu) * sp.sin(theta)) / (y * sp.cos(theta + mu))) B_2B = float((sp.tan(mu_xx_rad_list[i + 1]) * sp.sin(mu_xx_rad_list[i + 1]) * sp.sin( theta_xx_rad_list[i + 1])) / (y_list[i + 1] * sp.cos( theta_xx_rad_list[i + 1] - mu_xx_rad_list[i + 1]))) B_2C = float((sp.tan(mu) * sp.sin(mu) * sp.sin(theta)) / (y * sp.cos(theta - mu))) A_2 = float((2 / (sp.tan(mu_xx_rad_list[i]) + sp.tan(mu))) * ( ((2 * V_xx_list[i]) / (V_xx_list[i] + V)) + ((A_2A + A_2C) / 2) * (x - x_list[i]))) B_2 = float((2 / (sp.tan(mu_xx_rad_list[i + 1]) + sp.tan(mu))) * ( ((2 * V_xx_list[i + 1]) / (V_xx_list[i + 1] + V)) + ((B_2B + B_2C) / 2) * ( x - x_list[i + 1]))) V_2 = float((A_2 + B_2 + theta_xx_rad_list[i + 1] - theta_xx_rad_list[i]) / (4 * ( (V_xx_list[i] + V) ** (-1) * (sp.tan(mu_xx_rad_list[i]) + sp.tan(mu)) ** (-1) + ( V_xx_list[i + 1] + V) ** (-1) * ( sp.tan(mu_xx_rad_list[i + 1]) + sp.tan(mu)) ** (-1)))) theta_2 = float(theta_xx_rad_list[i] + (2 / (sp.tan(mu_xx_rad_list[i]) + sp.tan(mu))) * ( ((2 * (V_2 - V_xx_list[i])) / (V + V_xx_list[i])) - ((A_2A + A_2C) / 2) * ( x - x_list[i]))) T_2 = float(T_c - (V_2 ** 2 / (2 * c_p))) a_2 = float(sp.sqrt(gamma * R * T_2)) M_2 = float(V_2 / a_2) mu_2 = float(sp.asin(1 / M_2)) v_2 = float((math.sqrt((gamma + 1) / (gamma - 1))) * sp.atan( math.sqrt((gamma - 1) * (M_2 ** 2 - 1) / (gamma + 1))) - sp.atan(math.sqrt(M_2 ** 2 - 1))) K_neg_2 = float(theta_2 + v_2) K_pos_2 = float(theta_2 - v_2) xdiff = float(x - x_2) if xdiff == 0: xerr = float(0) else: xerr = float(x_2 / xdiff) ydiff = float(y - y_2) if ydiff == 0: yerr = float(0) else: yerr = float(y_2 / ydiff) thetadiff = float(theta - theta_2) if thetadiff == 0: thetaerr = float(0) else: thetaerr = float(theta_2 / thetadiff) Vdiff = float(V - V_2) if Vdiff == 0: Verr = float(0) else: Verr = float(V_2 / Vdiff) x = x_2 y = y_2 V = V_2 theta = theta_2 T = T_2 a = a_2 M = M_2 mu = mu_2 v = v_2 K_neg = K_neg_2 K_pos = K_pos_2 x_list[i] = x y_list[i] = y V_xx_list[i] = V theta_xx_rad_list[i] = theta v_PM_xx_rad_list[i] = v K_neg_xx_rad_list[i] = K_neg K_pos_xx_rad_list[i] = K_pos M_xx_list[i] = M mu_xx_rad_list[i] = mu T_xx_list[i] = T plt.plot(x_list, y_list, 'k.', markersize=1) n = (z - j + 1) + f f = f + z + 1 - i sheet.cell(row=n + 1, column=1).value = math.degrees(K_neg_xx_rad_list[i]) sheet.cell(row=n + 1, column=2).value = math.degrees(K_pos_xx_rad_list[i]) sheet.cell(row=n + 1, column=3).value = math.degrees(theta_xx_rad_list[i]) sheet.cell(row=n + 1, column=4).value = math.degrees(v_PM_xx_rad_list[i]) sheet.cell(row=n + 1, column=5).value = M_xx_list[i] sheet.cell(row=n + 1, column=6).value = math.degrees(mu_xx_rad_list[i]) sheet.cell(row=n + 1, column=7).value = V_xx_list[i] sheet.cell(row=n + 1, column=8).value = x_list[i] sheet.cell(row=n + 1, column=9).value = y_list[i] sheet.cell(row=n + 1, column=11).value = T_xx_list[i] counter = 1 sheet.cell(row=1, column=12).value = "x nozzle wall" sheet.cell(row=1, column=13).value = "y nozzle wall" d = 0 for q in range(0, z, 1): if q == 0: x = float((-x_list[q] * sp.tan(theta_xx_rad_list[q] + mu_xx_rad_list[q]) + y_list[q] - r_t) / ( sp.tan(theta_max_rad) - sp.tan(theta_xx_rad_list[q] + mu_xx_rad_list[q]))) y = float(sp.tan(theta_xx_rad_list[q] + mu_xx_rad_list[q]) * (x - x_list[q]) + y_list[q]) theta = float((theta_max_rad + theta_xx_rad_list[q]) / 2) else: x = float((-x_list[q] * sp.tan(theta_xx_rad_list[q] + mu_xx_rad_list[q]) + y_list[q] + x_list[ q - 1] * sp.tan(theta) - y_list[q - 1]) / ( sp.tan(theta) - sp.tan(theta_xx_rad_list[q] + mu_xx_rad_list[q]))) y = float(sp.tan(theta_xx_rad_list[q] + mu_xx_rad_list[q]) * (x - x_list[q]) + y_list[q]) theta = float((theta + theta_xx_rad_list[q]) / 2) x_list[q] = x y_list[q] = y d = d + z + 1 - q sheet.cell(row=d + 1, column=1).value = math.degrees(K_neg_xx_rad_list[q]) sheet.cell(row=d + 1, column=2).value = math.degrees(K_pos_xx_rad_list[q]) sheet.cell(row=d + 1, column=3).value = math.degrees(theta_xx_rad_list[q]) sheet.cell(row=d + 1, column=4).value = math.degrees(v_PM_xx_rad_list[q]) sheet.cell(row=d + 1, column=5).value = M_xx_list[q] sheet.cell(row=d + 1, column=6).value = math.degrees(mu_xx_rad_list[q]) sheet.cell(row=d + 1, column=7).value = V_xx_list[q] sheet.cell(row=d + 1, column=8).value = x_list[q] sheet.cell(row=d + 1, column=9).value = y_list[q] sheet.cell(row=counter + 1, column=12).value = x sheet.cell(row=counter + 1, column=13).value = y counter += 1 sheet.cell(row=1, column=15).value = "x_conv" sheet.cell(row=1, column=16).value = "y_conv" x_conv_list = [] y_conv_list = [] counter = 1 for beta in np.arange(-90, -136, -1): x_conv = float(1.5 * r_t * sp.cos(math.radians(beta))) y_conv = float(1.5 * r_t * sp.sin(math.radians(beta)) + 1.5 * r_t + r_t) sheet.cell(row=counter + 1, column=15).value = x_conv sheet.cell(row=counter + 1, column=16).value = y_conv x_conv_list.append(x_conv) y_conv_list.append(y_conv) counter += 1 outbox = Text(tab1, borderwidth=2, width=90, height=10, fg="black") outbox.grid(row=26, column=0, columnspan=100, padx=5, pady=40) outbox.insert(END,"All data from calculation has been exported in 'export1.xlsx'. If another calculation is initiated, the data from the previous calculation will be overwritten." + '\n') outbox.see("end") x_line = ((r_c - y_conv) / sp.tan(math.radians(-45))) + x_conv x_conv_list.insert(45, x_line) y_conv_list.insert(45, r_c) plt.plot(x_conv_list, y_conv_list, 'ko', linewidth=2, linestyle='-', markersize=2) x_list.insert(0, 0) y_list.insert(0, r_t) plt.plot(x_list, y_list, 'ko', linewidth=2, linestyle='-', markersize=4) plt.ylabel("Radius [m]") plt.xlabel("Length [m]") plt.axis('equal') rerr = float((r_e - y_list[z]) / y_list[z] * 100) exhaust_radius.insert(0, y_list[z]) nozzle_length.insert(0, x_list[z]) isen_exhaust_radius.insert(0, r_e) error.insert(0, rerr) textstr = "Combustion Pressure: "+str(p_c)+" [Pa]\n" \ "Combusiton Temperature: "+str(T_c)+" [K]\n" \ "Ratio of Spec. Heat: "+str(gamma)+" [-]\n" \ "Specific Gas Constant: "+str(R)+" [J/kgK]\n" \ "Throat Radius: "+str(r_t)+" [m]\n" \ "Combustion Chamber Radius: "+str(r_c)+" [m]\n" \ "Number of Char. Lines: "+str(num_of_char_lines)+" \n" \ "Number of Iterations: "+str(numiter)+" \n" \ "Initial Expansion Angle: "+str(theta_max_deg)+" [deg]\n" \ "Characteristic Net Length: "+str(L)+" [m]\n" \ "Pressure Distribution Type: "+str(pressure_type)+" \n" \ "Cubic q-factor: "+str(q_factor_value)+" [-]\n" exhauststr = "Exhaust Mach Number: " + str(M_e) + " [-]\n" \ "Exhaust velocity: " + str(u_e) + " [m/s]\n" \ "Exhaust Pressure: " + str(p_e) + " [Pa]\n" \ "Exhaust Temperature: " + str(T_e) + " [K]\n" \ "Exhaust Density: " + str(rho_e) + " [kg/m^3]\n" \ "MOC Exahust Radius: " + str(y_list[z]) + " [m]\n" \ "Isentropic Exhaust Radius: " + str(r_e) + " [m]\n" \ T_t = float(T_c*(2/(gamma+1))) rho_t = float(rho_c*(2/(gamma+1))**(1/(gamma-1))) throatstr = "Throat Mach Number: "+str(M_t)+" [-]\n" \ "Throat velocity: " + str(float(sp.sqrt(gamma*R*T_t))) + " [m/s]\n" \ "Throat Pressure: " + str(p_t) + " [Pa]\n" \ "Throat Temperature: " + str(T_t) + " [K]\n" \ "Throat Density: " + str(rho_t) + " [kg/m^3]\n" \ "Initial Expansion Angle: " + str(theta_max_deg) + " [deg]\n" \ "Isentropic Throat Radius: " + str(r_t) + " [m]\n" \ combstr = "Combustion Pressure: " + str(p_c) + " [Pa]\n" \ "Combustion Temperature: " + str(T_c) + " [K]\n" \ "Combustion Density: " + str(rho_c) + " [kg/m^3]\n" \ "Combustion Chamber Radius: " + str(r_c) + " [m]\n" \ lengthstr = "Characteristic Net Length: "+str(L)+" [m] \n" \ "Nozzle Length: "+str(x_list[z])+" [m]" plt.title("AXIMOC Nozzle with "+str(num_of_char_lines)+" char. lines; "+str(numiter)+" iterations. Pressure type: "+str(pressure_type)+"; Expansion angle: "+str(theta_max_deg)+" [deg].") m_dot = float(rho_e*u_e*A_e) print(m_dot) plt.text(x_list[z-round(num_of_char_lines/4)], y_list[z], exhauststr, fontsize=8) plt.text(x_list[z-round(num_of_char_lines)], y_list[z], throatstr, fontsize=8) plt.text(x_line, -0.06, combstr, fontsize=8) plt.text(x_list[round(z/3)], -0.025, lengthstr, fontsize=8) book.save('export1.xlsx') calculatebutton = Button(tab1, text="Calculate", padx=35, pady=10, command=calculate).grid(row=11, column=3, rowspan=2, columnspan=10, sticky=W) return plt.show()
def test_issue_4403_2(): assert integrate(sqrt(-x**2 - 4), x) == \ -2*atan(x/sqrt(-4 - x**2)) + x*sqrt(-4 - x**2)/2
def test_invert_real(): x = Dummy(real=True) n = Symbol('n') minus_n = Intersection(Interval(-oo, 0), FiniteSet(-n)) plus_n = Intersection(Interval(0, oo), FiniteSet(n)) assert solveset(abs(x) - n, x, S.Reals) == Union(minus_n, plus_n) n = Symbol('n', real=True) assert invert_real(x + 3, y, x) == (x, FiniteSet(y - 3)) assert invert_real(x * 3, y, x) == (x, FiniteSet(y / 3)) assert invert_real(exp(x), y, x) == (x, FiniteSet(log(y))) assert invert_real(exp(3 * x), y, x) == (x, FiniteSet(log(y) / 3)) assert invert_real(exp(x + 3), y, x) == (x, FiniteSet(log(y) - 3)) assert invert_real(exp(x) + 3, y, x) == (x, FiniteSet(log(y - 3))) assert invert_real(exp(x) * 3, y, x) == (x, FiniteSet(log(y / 3))) assert invert_real(log(x), y, x) == (x, FiniteSet(exp(y))) assert invert_real(log(3 * x), y, x) == (x, FiniteSet(exp(y) / 3)) assert invert_real(log(x + 3), y, x) == (x, FiniteSet(exp(y) - 3)) minus_y = Intersection(Interval(-oo, 0), FiniteSet(-y)) plus_y = Intersection(Interval(0, oo), FiniteSet(y)) assert invert_real(Abs(x), y, x) == (x, Union(minus_y, plus_y)) assert invert_real(2**x, y, x) == (x, FiniteSet(log(y) / log(2))) assert invert_real(2**exp(x), y, x) == (x, FiniteSet(log(log(y) / log(2)))) assert invert_real(x**2, y, x) == (x, FiniteSet(sqrt(y), -sqrt(y))) assert invert_real(x**Rational(1, 2), y, x) == (x, FiniteSet(y**2)) raises(ValueError, lambda: invert_real(x, x, x)) raises(ValueError, lambda: invert_real(x**pi, y, x)) raises(ValueError, lambda: invert_real(S.One, y, x)) assert invert_real(x**31 + x, y, x) == (x**31 + x, FiniteSet(y)) y_1 = Intersection(Interval(-1, oo), FiniteSet(y - 1)) y_2 = Intersection(Interval(-oo, -1), FiniteSet(-y - 1)) assert invert_real(Abs(x**31 + x + 1), y, x) == (x**31 + x, Union(y_1, y_2)) assert invert_real(sin(x), y, x) == \ (x, imageset(Lambda(n, n*pi + (-1)**n*asin(y)), S.Integers)) assert invert_real(sin(exp(x)), y, x) == \ (x, imageset(Lambda(n, log((-1)**n*asin(y) + n*pi)), S.Integers)) assert invert_real(csc(x), y, x) == \ (x, imageset(Lambda(n, n*pi + (-1)**n*acsc(y)), S.Integers)) assert invert_real(csc(exp(x)), y, x) == \ (x, imageset(Lambda(n, log((-1)**n*acsc(y) + n*pi)), S.Integers)) assert invert_real(cos(x), y, x) == \ (x, Union(imageset(Lambda(n, 2*n*pi + acos(y)), S.Integers), \ imageset(Lambda(n, 2*n*pi - acos(y)), S.Integers))) assert invert_real(cos(exp(x)), y, x) == \ (x, Union(imageset(Lambda(n, log(2*n*pi + acos(y))), S.Integers), \ imageset(Lambda(n, log(2*n*pi - acos(y))), S.Integers))) assert invert_real(sec(x), y, x) == \ (x, Union(imageset(Lambda(n, 2*n*pi + asec(y)), S.Integers), \ imageset(Lambda(n, 2*n*pi - asec(y)), S.Integers))) assert invert_real(sec(exp(x)), y, x) == \ (x, Union(imageset(Lambda(n, log(2*n*pi + asec(y))), S.Integers), \ imageset(Lambda(n, log(2*n*pi - asec(y))), S.Integers))) assert invert_real(tan(x), y, x) == \ (x, imageset(Lambda(n, n*pi + atan(y)), S.Integers)) assert invert_real(tan(exp(x)), y, x) == \ (x, imageset(Lambda(n, log(n*pi + atan(y))), S.Integers)) assert invert_real(cot(x), y, x) == \ (x, imageset(Lambda(n, n*pi + acot(y)), S.Integers)) assert invert_real(cot(exp(x)), y, x) == \ (x, imageset(Lambda(n, log(n*pi + acot(y))), S.Integers)) assert invert_real(tan(tan(x)), y, x) == \ (tan(x), imageset(Lambda(n, n*pi + atan(y)), S.Integers)) x = Symbol('x', positive=True) assert invert_real(x**pi, y, x) == (x, FiniteSet(y**(1 / pi))) # Test for ``set_h`` containing information about the domain n = Dummy('n') x = Symbol('x') h1 = Intersection(Interval(-3, oo), FiniteSet(a + b - 3), imageset(Lambda(n, -n + a - 3), Interval(-oo, 0))) h2 = Intersection(Interval(-oo, -3), FiniteSet(-a + b - 3), imageset(Lambda(n, n - a - 3), Interval(0, oo))) h3 = Intersection(Interval(-3, oo), FiniteSet(a - b - 3), imageset(Lambda(n, -n + a - 3), Interval(0, oo))) h4 = Intersection(Interval(-oo, -3), FiniteSet(-a - b - 3), imageset(Lambda(n, n - a - 3), Interval(-oo, 0))) assert invert_real(Abs(Abs(x + 3) - a) - b, 0, x) == (x, Union(h1, h2, h3, h4))
def test_cos(): x, y = symbols('x y') assert cos.nargs == FiniteSet(1) assert cos(nan) == nan 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) assert cos(pi * n / 2) == 0 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) == S.Half assert cos(-2 * pi / 3) == -S.Half assert cos(pi / 4) == S.Half * sqrt(2) assert cos(-pi / 4) == S.Half * sqrt(2) assert cos(11 * pi / 4) == -S.Half * sqrt(2) assert cos(-3 * pi / 4) == -S.Half * sqrt(2) assert cos(pi / 6) == S.Half * sqrt(3) assert cos(-pi / 6) == S.Half * sqrt(3) assert cos(7 * pi / 6) == -S.Half * sqrt(3) assert cos(-5 * pi / 6) == -S.Half * sqrt(3) 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(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 is True assert cos(k * pi) == (-1)**k assert cos(2 * k * pi) == 1 for d in list(range(1, 22)) + [60, 85]: for n in xrange(0, 2 * d + 1): x = n * pi / d e = abs(float(cos(x)) - cos(float(x))) assert e < 1e-12
def log(self): """ logarithmic map""" n = sympy.sqrt(sophus.squared_norm(self.q.vec)) return 2 * sympy.atan(n / self.q.real) / n * self.q.vec
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 n = symbols('n', integer=True) assert sin(pi * n / 2) == (-1)**(n / 2 - S.Half) assert sin(pi / 3) == S.Half * sqrt(3) assert sin(-2 * pi / 3) == -S.Half * sqrt(3) assert sin(pi / 4) == S.Half * sqrt(2) assert sin(-pi / 4) == -S.Half * sqrt(2) assert sin(17 * pi / 4) == S.Half * sqrt(2) assert sin(-3 * pi / 4) == -S.Half * sqrt(2) assert sin(pi / 6) == S.Half assert sin(-pi / 6) == -S.Half assert sin(7 * pi / 6) == -S.Half assert sin(-5 * pi / 6) == -S.Half 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(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 is True 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 xrange(0, d * 2 + 1): x = n * pi / d e = abs(float(sin(x)) - sin(float(x))) assert e < 1e-12
def eval_arctan(integrand, symbol): return sympy.atan(symbol)
def test_ratint(): assert ratint(S(0), x) == 0 assert ratint(S(7), x) == 7*x assert ratint(x, x) == x**2/2 assert ratint(2*x, x) == x**2 assert ratint(-2*x, x) == -x**2 assert ratint(8*x**7+2*x+1, x) == x**8+x**2+x f = S(1) g = x + 1 assert ratint(f / g, x) == log(x + 1) assert ratint((f,g), x) == log(x + 1) f = x**3 - x g = x - 1 assert ratint(f/g, x) == x**3/3 + x**2/2 f = x g = (x - a)*(x + a) assert ratint(f/g, x) == log(x**2 - a**2)/2 f = S(1) g = x**2 + 1 assert ratint(f/g, x, real=None) == atan(x) assert ratint(f/g, x, real=True) == atan(x) assert ratint(f/g, x, real=False) == I*log(x + I)/2 - I*log(x - I)/2 f = S(36) g = x**5-2*x**4-2*x**3+4*x**2+x-2 assert ratint(f/g, x) == \ -4*log(x + 1) + 4*log(x - 2) + (12*x + 6)/(x**2 - 1) f = x**4-3*x**2+6 g = x**6-5*x**4+5*x**2+4 assert ratint(f/g, x) == \ atan(x) + atan(x**3) + atan(x/2 - 3*x**S(3)/2 + S(1)/2*x**5) f = x**7-24*x**4-4*x**2+8*x-8 g = x**8+6*x**6+12*x**4+8*x**2 assert ratint(f/g, x) == \ (4 + 6*x + 8*x**2 + 3*x**3)/(4*x + 4*x**3 + x**5) + log(x) assert ratint((x**3*f)/(x*g), x) == \ -(12 - 16*x + 6*x**2 - 14*x**3)/(4 + 4*x**2 + x**4) - \ 5*sqrt(2)*atan(x*sqrt(2)/2) + S(1)/2*x**2 - 3*log(2 + x**2) f = x**5-x**4+4*x**3+x**2-x+5 g = x**4-2*x**3+5*x**2-4*x+4 assert ratint(f/g, x) == \ x + S(1)/2*x**2 + S(1)/2*log(2-x+x**2) - (4*x-9)/(14-7*x+7*x**2) + \ 13*sqrt(7)*atan(-S(1)/7*sqrt(7) + 2*x*sqrt(7)/7)/49 assert ratint(1/(x**2+x+1), x) == \ 2*sqrt(3)*atan(sqrt(3)/3 + 2*x*sqrt(3)/3)/3 assert ratint(1/(x**3+1), x) == \ -log(1 - x + x**2)/6 + log(1 + x)/3 + sqrt(3)*atan(-sqrt(3)/3 + 2*x*sqrt(3)/3)/3 assert ratint(1/(x**2+x+1), x, real=False) == \ -I*3**half*log(half + x - half*I*3**half)/3 + \ I*3**half*log(half + x + half*I*3**half)/3 assert ratint(1/(x**3+1), x, real=False) == log(1 + x)/3 + \ (-S(1)/6 + I*3**half/6)*log(-half + x + I*3**half/2) + \ (-S(1)/6 - I*3**half/6)*log(-half + x - I*3**half/2) # Issue 1892 assert ratint(1/(x*(a+b*x)**3), x) == \ (sqrt(a**(-6))*log(x + (a - a**4*sqrt(a**(-6)))/(2*b)) + (3*a + 2*b*x)/(2*a**2*b**2*x**2 + 4*b*x*a**3 + 2*a**4) - sqrt(a**(-6))*log(x + (a + a**4*sqrt(a**(-6)))/(2*b))) assert ratint(x/(1 - x**2), x) == -log(x**2 - 1)/2 assert ratint(-x/(1 - x**2), x) == log(x**2 - 1)/2 ans = atan(x) assert ratint(1/(x**2 + 1), x, symbol=x) == ans assert ratint(1/(x**2 + 1), x, symbol='x') == ans assert ratint(1/(x**2 + 1), x, symbol=a) == ans
def test_issue_8520(): assert manualintegrate(x / (x**4 + 1), x) == atan(x**2) / 2 assert manualintegrate(x**2 / (x**6 + 25), x) == atan(x**3 / 5) / 15 f = x / (9 * x**4 + 4)**2 assert manualintegrate(f, x).diff(x).factor() == f
def test_issue_10847_slow(): assert manualintegrate((4*x**4 + 4*x**3 + 16*x**2 + 12*x + 8) / (x**6 + 2*x**5 + 3*x**4 + 4*x**3 + 3*x**2 + 2*x + 1), x) == \ 2*x/(x**2 + 1) + 3*atan(x) - 1/(x**2 + 1) - 3/(x + 1)
def test_log_to_atan(): f, g = (Poly(x + S(1)/2, x, domain='QQ'), Poly(sqrt(3)/2, x, domain='EX')) fg_ans = 2*atan(2*sqrt(3)*x/3 + sqrt(3)/3) assert log_to_atan(f, g) == fg_ans assert log_to_atan(g, f) == -fg_ans
def test_atan(): x = Symbol("x", real=True) assert limit(atan(x) * sin(1 / x), x, 0) == 0 assert limit(atan(x) + sqrt(x + 1) - sqrt(x), x, oo) == pi / 2
def test_issue_2882(): u = symbols('u') assert integrate(1/(u**2 + 1)) == atan(u)
def test_issue_2718(): a, b, c = symbols('a,b,c', positive=True) assert simplify(ratint(a/(b*c*x**2 + a**2 + b*a), x)) == \ sqrt(a)*atan(sqrt(b)*sqrt(c)*x/(sqrt(a)*sqrt(a + b)))/(sqrt(b)*sqrt(c)*sqrt(a + b))
def test_manualintegrate_inversetrig(): # atan assert manualintegrate(exp(x) / (1 + exp(2 * x)), x) == atan(exp(x)) assert manualintegrate(1 / (4 + 9 * x**2), x) == atan(3 * x / 2) / 6 assert manualintegrate(1 / (16 + 16 * x**2), x) == atan(x) / 16 assert manualintegrate(1 / (4 + x**2), x) == atan(x / 2) / 2 assert manualintegrate(1 / (1 + 4 * x**2), x) == atan(2 * x) / 2 ra = Symbol('a', real=True) rb = Symbol('b', real=True) assert manualintegrate(1/(ra + rb*x**2), x) == \ Piecewise((atan(x/sqrt(ra/rb))/(rb*sqrt(ra/rb)), ra/rb > 0), (-acoth(x/sqrt(-ra/rb))/(rb*sqrt(-ra/rb)), And(ra/rb < 0, x**2 > -ra/rb)), (-atanh(x/sqrt(-ra/rb))/(rb*sqrt(-ra/rb)), And(ra/rb < 0, x**2 < -ra/rb))) assert manualintegrate(1/(4 + rb*x**2), x) == \ Piecewise((atan(x/(2*sqrt(1/rb)))/(2*rb*sqrt(1/rb)), 4/rb > 0), (-acoth(x/(2*sqrt(-1/rb)))/(2*rb*sqrt(-1/rb)), And(4/rb < 0, x**2 > -4/rb)), (-atanh(x/(2*sqrt(-1/rb)))/(2*rb*sqrt(-1/rb)), And(4/rb < 0, x**2 < -4/rb))) assert manualintegrate(1/(ra + 4*x**2), x) == \ Piecewise((atan(2*x/sqrt(ra))/(2*sqrt(ra)), ra/4 > 0), (-acoth(2*x/sqrt(-ra))/(2*sqrt(-ra)), And(ra/4 < 0, x**2 > -ra/4)), (-atanh(2*x/sqrt(-ra))/(2*sqrt(-ra)), And(ra/4 < 0, x**2 < -ra/4))) assert manualintegrate(1 / (4 + 4 * x**2), x) == atan(x) / 4 assert manualintegrate(1 / (a + b * x**2), x) == atan(x / sqrt(a / b)) / (b * sqrt(a / b)) # asin assert manualintegrate(1 / sqrt(1 - x**2), x) == asin(x) assert manualintegrate(1 / sqrt(4 - 4 * x**2), x) == asin(x) / 2 assert manualintegrate(3 / sqrt(1 - 9 * x**2), x) == asin(3 * x) assert manualintegrate(1 / sqrt(4 - 9 * x**2), x) == asin(x * Rational(3, 2)) / 3 # asinh assert manualintegrate(1/sqrt(x**2 + 1), x) == \ asinh(x) assert manualintegrate(1/sqrt(x**2 + 4), x) == \ asinh(x/2) assert manualintegrate(1/sqrt(4*x**2 + 4), x) == \ asinh(x)/2 assert manualintegrate(1/sqrt(4*x**2 + 1), x) == \ asinh(2*x)/2 assert manualintegrate(1/sqrt(a*x**2 + 1), x) == \ Piecewise((sqrt(-1/a)*asin(x*sqrt(-a)), a < 0), (sqrt(1/a)*asinh(sqrt(a)*x), a > 0)) assert manualintegrate(1/sqrt(a + x**2), x) == \ Piecewise((asinh(x*sqrt(1/a)), a > 0), (acosh(x*sqrt(-1/a)), a < 0)) # acosh assert manualintegrate(1/sqrt(x**2 - 1), x) == \ acosh(x) assert manualintegrate(1/sqrt(x**2 - 4), x) == \ acosh(x/2) assert manualintegrate(1/sqrt(4*x**2 - 4), x) == \ acosh(x)/2 assert manualintegrate(1/sqrt(9*x**2 - 1), x) == \ acosh(3*x)/3 assert manualintegrate(1/sqrt(a*x**2 - 4), x) == \ Piecewise((sqrt(1/a)*acosh(sqrt(a)*x/2), a > 0)) assert manualintegrate(1/sqrt(-a + 4*x**2), x) == \ Piecewise((asinh(2*x*sqrt(-1/a))/2, -a > 0), (acosh(2*x*sqrt(1/a))/2, -a < 0)) # piecewise assert manualintegrate(1/sqrt(a-b*x**2), x) == \ Piecewise((sqrt(a/b)*asin(x*sqrt(b/a))/sqrt(a), And(-b < 0, a > 0)), (sqrt(-a/b)*asinh(x*sqrt(-b/a))/sqrt(a), And(-b > 0, a > 0)), (sqrt(a/b)*acosh(x*sqrt(b/a))/sqrt(-a), And(-b > 0, a < 0))) assert manualintegrate(1/sqrt(a + b*x**2), x) == \ Piecewise((sqrt(-a/b)*asin(x*sqrt(-b/a))/sqrt(a), And(a > 0, b < 0)), (sqrt(a/b)*asinh(x*sqrt(b/a))/sqrt(a), And(a > 0, b > 0)), (sqrt(-a/b)*acosh(x*sqrt(-b/a))/sqrt(-a), And(a < 0, b > 0)))
def test_issue_2315(): assert ratint(1/(x**2 + 16), x) == atan(x/4)/4
from sympy import var, sqrt, sin, pprint, simplify, ccode, atan2, atan, exp, S var("x y alpha_w alpha_p omega pi x_w y_w x_p y_p epsilon r0") def diff_f_x(f): return f.diff(x, 1) r = sqrt(x**2 + y**2) r_w = sqrt((x - x_w)**2 + (y - y_w)**2) r_p_squared = (x - x_p)**2 + (y - y_p)**2 theta = atan(y / x) u = r**(pi / omega) * sin(theta * pi / omega) + atan( alpha_w * (r_w - r0)) + exp(-alpha_p * r_p_squared) + exp(-(1 + y) / epsilon) params = { omega: 3 * pi / 2, x_w: 0, y_w: -S(3) / 4, r0: S(3) / 4, alpha_p: 1000, alpha_w: 200, x_p: sqrt(5) / 4, y_p: -S(1) / 4, epsilon: S(1) / 100, } u = u.subs(params) lhs = diff_f_x(u) print "lhs, as a formula:" pprint(lhs)
def test_issue_15539(): assert series(atan(x), x, -oo) == (-1 / (5 * x**5) + 1 / (3 * x**3) - 1 / x - pi / 2 + O(x**(-6), (x, -oo))) assert series(atan(x), x, oo) == (-1 / (5 * x**5) + 1 / (3 * x**3) - 1 / x + pi / 2 + O(x**(-6), (x, oo)))
def test_issue_1707(): assert integrate(atan(x)**2, (x, -1, 1)).evalf().round(1) == 0.5 assert atan(0, evaluate=False).n() == 0
'square': lambda x: x**2, 'cube': lambda x: x**3, 'plus': lambda x, y: x + y, 'sub': lambda x, y: x - y, 'neg': lambda x: -x, 'pow': lambda x, y: sympy.sign(x) * abs(x)**y, 'cos': lambda x: sympy.cos(x), 'sin': lambda x: sympy.sin(x), 'tan': lambda x: sympy.tan(x), 'cosh': lambda x: sympy.cosh(x), 'sinh': lambda x: sympy.sinh(x), 'tanh': lambda x: sympy.tanh(x), 'exp': lambda x: sympy.exp(x), 'acos': lambda x: sympy.acos(x), 'asin': lambda x: sympy.asin(x), 'atan': lambda x: sympy.atan(x), 'acosh': lambda x: sympy.acosh(x), 'asinh': lambda x: sympy.asinh(x), 'atanh': lambda x: sympy.atanh(x), 'abs': lambda x: abs(x), 'mod': lambda x, y: sympy.Mod(x, y), 'erf': lambda x: sympy.erf(x), 'erfc': lambda x: sympy.erfc(x), 'logm': lambda x: sympy.log(abs(x)), 'logm10': lambda x: sympy.log10(abs(x)), 'logm2': lambda x: sympy.log2(abs(x)), 'log1p': lambda x: sympy.log(x + 1), 'floor': lambda x: sympy.floor(x), 'ceil': lambda x: sympy.ceil(x), 'sign': lambda x: sympy.sign(x), 'round': lambda x: sympy.round(x),
def test_linearize_pendulum_lagrange_nonminimal(): q1, q2 = dynamicsymbols('q1:3') q1d, q2d = dynamicsymbols('q1:3', level=1) L, m, t = symbols('L, m, t') g = 9.8 # Compose World Frame N = ReferenceFrame('N') pN = Point('N*') pN.set_vel(N, 0) # A.x is along the pendulum theta1 = atan(q2/q1) A = N.orientnew('A', 'axis', [theta1, N.z]) # Create point P, the pendulum mass P = pN.locatenew('P1', q1*N.x + q2*N.y) P.set_vel(N, P.pos_from(pN).dt(N)) pP = Particle('pP', P, m) # Constraint Equations f_c = Matrix([q1**2 + q2**2 - L**2]) # Calculate the lagrangian, and form the equations of motion Lag = Lagrangian(N, pP) LM = LagrangesMethod(Lag, [q1, q2], hol_coneqs=f_c, forcelist=[(P, m*g*N.x)], frame=N) LM.form_lagranges_equations() # Compose operating point op_point = {q1: L, q2: 0, q1d: 0, q2d: 0, q1d.diff(t): 0, q2d.diff(t): 0} # Solve for multiplier operating point lam_op = LM.solve_multipliers(op_point=op_point) op_point.update(lam_op) # Perform the Linearization A, B, inp_vec = LM.linearize([q2], [q2d], [q1], [q1d], op_point=op_point, A_and_B=True) assert A == Matrix([[0, 1], [-9.8/L, 0]]) assert B == Matrix([])