def test_issue_10978(): assert LambertW(x).limit(x, 0) == 0
def test_issue_12571(): assert limit(-LambertW(-log(x)) / log(x), x, 1) == 1
def test_lambertw(): x = Symbol('x') assert LambertW(x) == LambertW(x) assert LambertW(0) == 0 assert LambertW(E) == 1 assert LambertW(-1/E) == -1 assert LambertW(-log(2)/2) == -log(2) assert LambertW(oo) == oo assert LambertW(x**2).diff(x) == 2*LambertW(x**2)/x/(1 + LambertW(x**2)) assert LambertW(sqrt(2)).evalf(30).epsilon_eq( Float("0.701338383413663009202120278965", 30), 1e-29)
def test_lambertw(): k = Symbol('k') assert LambertW(x, 0) == LambertW(x) assert LambertW(x, 0, evaluate=False) != LambertW(x) assert LambertW(0) == 0 assert LambertW(E) == 1 assert LambertW(-1 / E) == -1 assert LambertW(-log(2) / 2) == -log(2) assert LambertW(oo) is oo assert LambertW(0, 1) is -oo assert LambertW(0, 42) is -oo assert LambertW(-pi / 2, -1) == -I * pi / 2 assert LambertW(-1 / E, -1) == -1 assert LambertW(-2 * exp(-2), -1) == -2 assert LambertW(2 * log(2)) == log(2) assert LambertW(-pi / 2) == I * pi / 2 assert LambertW(exp(1 + E)) == E assert LambertW( x**2).diff(x) == 2 * LambertW(x**2) / x / (1 + LambertW(x**2)) assert LambertW(x, k).diff(x) == LambertW(x, k) / x / (1 + LambertW(x, k)) assert LambertW(sqrt(2)).evalf(30).epsilon_eq( Float("0.701338383413663009202120278965", 30), 1e-29) assert re(LambertW(2, -1)).evalf().epsilon_eq(Float("-0.834310366631110")) assert LambertW(-1).is_real is False # issue 5215 assert LambertW(2, evaluate=False).is_real p = Symbol('p', positive=True) assert LambertW(p, evaluate=False).is_real assert LambertW(p - 1, evaluate=False).is_real is None assert LambertW(-p - 2 / S.Exp1, evaluate=False).is_real is False assert LambertW(S.Half, -1, evaluate=False).is_real is False assert LambertW(Rational(-1, 10), -1, evaluate=False).is_real assert LambertW(-10, -1, evaluate=False).is_real is False assert LambertW(-2, 2, evaluate=False).is_real is False assert LambertW(0, evaluate=False).is_algebraic na = Symbol('na', nonzero=True, algebraic=True) assert LambertW(na).is_algebraic is False
def omega(x): return LambertW(exp(x))
def test_transcendental_functions(): assert integrate(LambertW(2*x), x) == \ -x + x*LambertW(2*x) + x/LambertW(2*x)
def test_issue_18969(): a, b = symbols('a b', positive=True) assert limit(LambertW(a), a, b) == LambertW(b) assert limit(exp(LambertW(a)), a, b) == exp(LambertW(b))
def test_M10(): assert solve(exp(x) - x, x) == [-LambertW(-1)]
def plot_and_save(name): tmp_file = TmpFileManager.tmp_file x = Symbol('x') y = Symbol('y') z = Symbol('z') ### # Examples from the 'introduction' notebook ### p = plot(x) p = plot(x * sin(x), x * cos(x)) p.extend(p) p[0].line_color = lambda a: a p[1].line_color = 'b' p.title = 'Big title' p.xlabel = 'the x axis' p[1].label = 'straight line' p.legend = True p.aspect_ratio = (1, 1) p.xlim = (-15, 20) p.save(tmp_file('%s_basic_options_and_colors' % name)) p.extend(plot(x + 1)) p.append(plot(x + 3, x**2)[1]) p.save(tmp_file('%s_plot_extend_append' % name)) p[2] = plot(x**2, (x, -2, 3)) p.save(tmp_file('%s_plot_setitem' % name)) p = plot(sin(x), (x, -2 * pi, 4 * pi)) p.save(tmp_file('%s_line_explicit' % name)) p = plot(sin(x)) p.save(tmp_file('%s_line_default_range' % name)) p = plot((x**2, (x, -5, 5)), (x**3, (x, -3, 3))) p.save(tmp_file('%s_line_multiple_range' % name)) raises(ValueError, lambda: plot(x, y)) #parametric 2d plots. #Single plot with default range. plot_parametric(sin(x), cos(x)).save(tmp_file()) #Single plot with range. p = plot_parametric(sin(x), cos(x), (x, -5, 5)) p.save(tmp_file('%s_parametric_range' % name)) #Multiple plots with same range. p = plot_parametric((sin(x), cos(x)), (x, sin(x))) p.save(tmp_file('%s_parametric_multiple' % name)) #Multiple plots with different ranges. p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5))) p.save(tmp_file('%s_parametric_multiple_ranges' % name)) #depth of recursion specified. p = plot_parametric(x, sin(x), depth=13) p.save(tmp_file('%s_recursion_depth' % name)) #No adaptive sampling. p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500) p.save(tmp_file('%s_adaptive' % name)) #3d parametric plots p = plot3d_parametric_line(sin(x), cos(x), x) p.save(tmp_file('%s_3d_line' % name)) p = plot3d_parametric_line((sin(x), cos(x), x, (x, -5, 5)), (cos(x), sin(x), x, (x, -3, 3))) p.save(tmp_file('%s_3d_line_multiple' % name)) p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30) p.save(tmp_file('%s_3d_line_points' % name)) # 3d surface single plot. p = plot3d(x * y) p.save(tmp_file('%s_surface' % name)) # Multiple 3D plots with same range. p = plot3d(-x * y, x * y, (x, -5, 5)) p.save(tmp_file('%s_surface_multiple' % name)) # Multiple 3D plots with different ranges. p = plot3d((x * y, (x, -3, 3), (y, -3, 3)), (-x * y, (x, -3, 3), (y, -3, 3))) p.save(tmp_file('%s_surface_multiple_ranges' % name)) # Single Parametric 3D plot p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y) p.save(tmp_file('%s_parametric_surface' % name)) # Multiple Parametric 3D plots. p = plot3d_parametric_surface( (x * sin(z), x * cos(z), z, (x, -5, 5), (z, -5, 5)), (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5))) p.save(tmp_file('%s_parametric_surface' % name)) ### # Examples from the 'colors' notebook ### p = plot(sin(x)) p[0].line_color = lambda a: a p.save(tmp_file('%s_colors_line_arity1' % name)) p[0].line_color = lambda a, b: b p.save(tmp_file('%s_colors_line_arity2' % name)) p = plot(x * sin(x), x * cos(x), (x, 0, 10)) p[0].line_color = lambda a: a p.save(tmp_file('%s_colors_param_line_arity1' % name)) p[0].line_color = lambda a, b: a p.save(tmp_file('%s_colors_param_line_arity2a' % name)) p[0].line_color = lambda a, b: b p.save(tmp_file('%s_colors_param_line_arity2b' % name)) p = plot3d_parametric_line( sin(x) + 0.1 * sin(x) * cos(7 * x), cos(x) + 0.1 * cos(x) * cos(7 * x), 0.1 * sin(7 * x), (x, 0, 2 * pi)) p[0].line_color = lambda a: sin(4 * a) p.save(tmp_file('%s_colors_3d_line_arity1' % name)) p[0].line_color = lambda a, b: b p.save(tmp_file('%s_colors_3d_line_arity2' % name)) p[0].line_color = lambda a, b, c: c p.save(tmp_file('%s_colors_3d_line_arity3' % name)) p = plot3d(sin(x) * y, (x, 0, 6 * pi), (y, -5, 5)) p[0].surface_color = lambda a: a p.save(tmp_file('%s_colors_surface_arity1' % name)) p[0].surface_color = lambda a, b: b p.save(tmp_file('%s_colors_surface_arity2' % name)) p[0].surface_color = lambda a, b, c: c p.save(tmp_file('%s_colors_surface_arity3a' % name)) p[0].surface_color = lambda a, b, c: sqrt((a - 3 * pi)**2 + b**2) p.save(tmp_file('%s_colors_surface_arity3b' % name)) p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y, (x, -1, 1), (y, -1, 1)) p[0].surface_color = lambda a: a p.save(tmp_file('%s_colors_param_surf_arity1' % name)) p[0].surface_color = lambda a, b: a * b p.save(tmp_file('%s_colors_param_surf_arity2' % name)) p[0].surface_color = lambda a, b, c: sqrt(a**2 + b**2 + c**2) p.save(tmp_file('%s_colors_param_surf_arity3' % name)) ### # Examples from the 'advanced' notebook ### i = Integral(log((sin(x)**2 + 1) * sqrt(x**2 + 1)), (x, 0, y)) p = plot(i, (y, 1, 5)) p.save(tmp_file('%s_advanced_integral' % name)) s = summation(1 / x**y, (x, 1, oo)) p = plot(s, (y, 2, 10)) p.save(tmp_file('%s_advanced_inf_sum' % name)) p = plot(summation(1 / x, (x, 1, y)), (y, 2, 10), show=False) p[0].only_integers = True p[0].steps = True p.save(tmp_file('%s_advanced_fin_sum' % name)) ### # Test expressions that can not be translated to np and generate complex # results. ### plot(sin(x) + I * cos(x)).save(tmp_file()) plot(sqrt(sqrt(-x))).save(tmp_file()) plot(LambertW(x)).save(tmp_file()) plot(sqrt(LambertW(x))).save(tmp_file()) #Characteristic function of a StudentT distribution with nu=10 plot((meijerg( ((1 / 2, ), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(-I * pi) / 2) + meijerg( ((1 / 2, ), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(I * pi) / 2)) / (48 * pi), (x, 1e-6, 1e-2)).save(tmp_file())
def test_tsolve_1(): a, b = symbols('ab') x, y, z = symbols('xyz') assert solve(exp(x) - 3, x) == [log(3)] assert solve((a * x + b) * (exp(x) - 3), x) == [-b / a, log(3)] assert solve(cos(x) - y, x) == [acos(y)] assert solve(2 * cos(x) - y, x) == [acos(y / 2)] raises(NotImplementedError, "solve(Eq(cos(x), sin(x)), x)") # XXX in the following test, log(2*y + 2*...) should -> log(2) + log(y +...) assert solve(exp(x) + exp(-x) - y, x) == [ -log(4) + log(2 * y + 2 * (-4 + y**2)**Rational(1, 2)), -log(4) + log(2 * y - 2 * (-4 + y**2)**Rational(1, 2)) ] assert solve(exp(x) - 3, x) == [log(3)] assert solve(Eq(exp(x), 3), x) == [log(3)] assert solve(log(x) - 3, x) == [exp(3)] assert solve(sqrt(3 * x) - 4, x) == [Rational(16, 3)] assert solve(3**(x + 2), x) == [-oo] assert solve(3**(2 - x), x) == [oo] assert solve(4 * 3**(5 * x + 2) - 7, x) == [(-log(4) - 2 * log(3) + log(7)) / (5 * log(3))] assert solve(x + 2**x, x) == [-LambertW(log(2)) / log(2)] assert solve(3*x+5+2**(-5*x+3), x) in \ [[-Rational(5,3) + LambertW(-10240*2**Rational(1,3)*log(2)/3)/(5*log(2))],\ [(-25*log(2) + 3*LambertW(-10240*2**(Rational(1, 3))*log(2)/3))/(15*log(2))]] assert solve(5*x-1+3*exp(2-7*x), x) == \ [Rational(1,5) + LambertW(-21*exp(Rational(3,5))/5)/7] assert solve(2*x+5+log(3*x-2), x) == \ [Rational(2,3) + LambertW(2*exp(-Rational(19,3))/3)/2] assert solve(3 * x + log(4 * x), x) == [LambertW(Rational(3, 4)) / 3] assert solve((2 * x + 8) * (8 + exp(x)), x) == [-4, log(8) + pi * I] assert solve(2*exp(3*x+4)-3, x) in [ [-Rational(4,3)+log(Rational(3,2))/3],\ [Rational(-4, 3) - log(2)/3 + log(3)/3]] assert solve(2 * log(3 * x + 4) - 3, x) == [(exp(Rational(3, 2)) - 4) / 3] assert solve(exp(x) + 1, x) == [pi * I] assert solve(x**2 - 2**x, x) == [2] assert solve(x**3 - 3**x, x) == [-3 / log(3) * LambertW(-log(3) / 3)] assert solve(2*(3*x+4)**5 - 6*7**(3*x+9), x) in \ [[Rational(-4,3) - 5/log(7)/3*LambertW(-7*2**Rational(4,5)*6**Rational(1,5)*log(7)/10)],\ [(-5*LambertW(-7*2**(Rational(4, 5))*6**(Rational(1, 5))*log(7)/10) - 4*log(7))/(3*log(7))], \ [-((4*log(7) + 5*LambertW(-7*2**Rational(4,5)*6**Rational(1,5)*log(7)/10))/(3*log(7)))]] assert solve(z * cos(x) - y, x) == [acos(y / z)] assert solve(z * cos(2 * x) - y, x) == [acos(y / z) / 2] assert solve(z * cos(sin(x)) - y, x) == [asin(acos(y / z))] assert solve(z * cos(x), x) == [acos(0)] assert solve(exp(x) + exp(-x) - y, x) == [ -log(4) + log(2 * y + 2 * (-4 + y**2)**(Rational(1, 2))), -log(4) + log(2 * y - 2 * (-4 + y**2)**(Rational(1, 2))) ] # issue #1409 assert solve(y - b * x / (a + x), x) == [a * y / (b - y)] assert solve(y - b * exp(a / x), x) == [a / (-log(b) + log(y))] # issue #1408 assert solve(y - b / (1 + a * x), x) == [(b - y) / (a * y)] # issue #1407 assert solve(y - a * x**b, x) == [y**(1 / b) * (1 / a)**(1 / b)] # issue #1406 assert solve(z**x - y, x) == [log(y) / log(z)] # issue #1405 assert solve(2**x - 10, x) == [log(10) / log(2)]
def test_tsolve(): a, b = symbols('a,b') x, y, z = symbols('x,y,z') assert solve(exp(x)-3, x) == [log(3)] assert solve((a*x+b)*(exp(x)-3), x) == [-b/a, log(3)] assert solve(cos(x)-y, x) == [acos(y)] assert solve(2*cos(x)-y,x)== [acos(y/2)] raises(NotImplementedError, "solve(Eq(cos(x), sin(x)), x)") assert solve(exp(x) + exp(-x) - y, x, simplified=False) == [ log((y/2 + sqrt(y**2 - 4)/2)**2)/2, log((y/2 - sqrt(y**2 - 4)/2)**2)/2] assert solve(exp(x)-3, x) == [log(3)] assert solve(Eq(exp(x), 3), x) == [log(3)] assert solve(log(x)-3, x) == [exp(3)] assert solve(sqrt(3*x)-4, x) == [Rational(16,3)] assert solve(3**(x+2), x) == [zoo] assert solve(3**(2-x), x) == [zoo] assert solve(4*3**(5*x+2)-7, x) == [(-2*log(3) - 2*log(2) + log(7))/(5*log(3))] assert solve(x+2**x, x) == [-LambertW(log(2))/log(2)] assert solve(3*x+5+2**(-5*x+3), x) in [ [-((25*log(2) - 3*LambertW(-10240*2**(Rational(1, 3))*log(2)/3))/(15*log(2)))], [Rational(-5, 3) + LambertW(log(2**(-10240*2**(Rational(1, 3))/3)))/(5*log(2))], [-Rational(5,3) + LambertW(-10240*2**Rational(1,3)*log(2)/3)/(5*log(2))], [(-25*log(2) + 3*LambertW(-10240*2**(Rational(1, 3))*log(2)/3))/(15*log(2))], [-((25*log(2) - 3*LambertW(-10240*2**(Rational(1, 3))*log(2)/3)))/(15*log(2))], [-(25*log(2) - 3*LambertW(log(2**(-10240*2**Rational(1, 3)/3))))/(15*log(2))], [(25*log(2) - 3*LambertW(log(2**(-10240*2**Rational(1, 3)/3))))/(-15*log(2))] ] assert solve(5*x-1+3*exp(2-7*x), x) == \ [Rational(1,5) + LambertW(-21*exp(Rational(3, 5))/5)/7] assert solve(2*x+5+log(3*x-2), x) == \ [Rational(2,3) + LambertW(2*exp(-Rational(19, 3))/3)/2] assert solve(3*x+log(4*x), x) == [LambertW(Rational(3,4))/3] assert solve((2*x+8)*(8+exp(x)), x) == [-4, log(8) + pi*I] assert solve(2*exp(3*x+4)-3, x) in [ [Rational(-4, 3) + log(2**Rational(2, 3)*3**Rational(1, 3)/2)], [-Rational(4, 3)+log(Rational(3, 2))/3], [Rational(-4, 3) - log(2)/3 + log(3)/3], ] assert solve(2*log(3*x+4)-3, x) == [(exp(Rational(3,2))-4)/3] assert solve(exp(x)+1, x) == [pi*I] assert solve(x**2 - 2**x, x) == [2] assert solve(x**3 - 3**x, x) == [-3*LambertW(-log(3)/3)/log(3)] A = -7*2**Rational(4, 5)*6**Rational(1, 5)*log(7)/10 B = -7*3**Rational(1, 5)*log(7)/5 result = solve(2*(3*x+4)**5 - 6*7**(3*x+9), x) assert len(result) == 1 and expand(result[0]) in [ Rational(-4, 3) - 5/log(7)/3*LambertW(A), Rational(-4, 3) - 5/log(7)/3*LambertW(B), ] assert solve(z*cos(x)-y, x) == [acos(y/z)] assert solve(z*cos(2*x)-y, x) == [acos(y/z)/2] assert solve(z*cos(sin(x))-y, x) == [asin(acos(y/z))] assert solve(z*cos(x), x) == [acos(0)] # issue #1409 assert solve(y - b*x/(a+x), x) in [[-a*y/(y - b)], [a*y/(b - y)]] assert solve(y - b*exp(a/x), x) == [a/log(y/b)] # issue #1408 assert solve(y-b/(1+a*x), x) in [[(b - y)/(a*y)], [-((y - b)/(a*y))]] # issue #1407 assert solve(y-a*x**b , x) == [(y/a)**(1/b)] # issue #1406 assert solve(z**x - y, x) == [log(y)/log(z)] # issue #1405 assert solve(2**x - 10, x) == [log(10)/log(2)]
def test_solve_lambert(): assert solveset_real(x * exp(x) - 1, x) == FiniteSet(LambertW(1)) assert solveset_real(x + 2**x, x) == \ FiniteSet(-LambertW(log(2))/log(2)) # issue 4739 assert solveset_real(exp(log(5) * x) - 2**x, x) == FiniteSet(0) ans = solveset_real(3 * x + 5 + 2**(-5 * x + 3), x) assert ans == FiniteSet(-Rational(5, 3) + LambertW(-10240 * 2**(S(1) / 3) * log(2) / 3) / (5 * log(2))) eq = 2 * (3 * x + 4)**5 - 6 * 7**(3 * x + 9) result = solveset_real(eq, x) ans = FiniteSet( (log(2401) + 5 * LambertW(-log(7**(7 * 3**Rational(1, 5) / 5)))) / (3 * log(7)) / -1) assert result == ans assert solveset_real(eq.expand(), x) == result assert solveset_real(5*x - 1 + 3*exp(2 - 7*x), x) == \ FiniteSet(Rational(1, 5) + LambertW(-21*exp(Rational(3, 5))/5)/7) assert solveset_real(2*x + 5 + log(3*x - 2), x) == \ FiniteSet(Rational(2, 3) + LambertW(2*exp(-Rational(19, 3))/3)/2) assert solveset_real(3*x + log(4*x), x) == \ FiniteSet(LambertW(Rational(3, 4))/3) assert solveset_complex(x**z*y**z - 2, z) == \ FiniteSet(log(2)/(log(x) + log(y))) assert solveset_real(x**x - 2) == FiniteSet(exp(LambertW(log(2)))) a = Symbol('a') assert solveset_real(-a * x + 2 * x * log(x), x) == FiniteSet(exp(a / 2)) a = Symbol('a', real=True) assert solveset_real(a/x + exp(x/2), x) == \ FiniteSet(2*LambertW(-a/2)) assert solveset_real((a/x + exp(x/2)).diff(x), x) == \ FiniteSet(4*LambertW(sqrt(2)*sqrt(a)/4)) assert solveset_real(1 / (1 / x - y + exp(y)), x) == EmptySet() # coverage test p = Symbol('p', positive=True) w = Symbol('w') assert solveset_real((1 / p + 1)**(p + 1), p) == EmptySet() assert solveset_real(tanh(x + 3) * tanh(x - 3) - 1, x) == EmptySet() assert solveset_real(2*x**w - 4*y**w, w) == \ solveset_real((x/y)**w - 2, w) assert solveset_real((x**2 - 2*x + 1).subs(x, log(x) + 3*x), x) == \ FiniteSet(LambertW(3*S.Exp1)/3) assert solveset_real((x**2 - 2*x + 1).subs(x, (log(x) + 3*x)**2 - 1), x) == \ FiniteSet(LambertW(3*exp(-sqrt(2)))/3, LambertW(3*exp(sqrt(2)))/3) assert solveset_real((x**2 - 2*x - 2).subs(x, log(x) + 3*x), x) == \ FiniteSet(LambertW(3*exp(1 + sqrt(3)))/3, LambertW(3*exp(-sqrt(3) + 1))/3) assert solveset_real(x*log(x) + 3*x + 1, x) == \ FiniteSet(exp(-3 + LambertW(-exp(3)))) eq = (x * exp(x) - 3).subs(x, x * exp(x)) assert solveset_real(eq, x) == \ FiniteSet(LambertW(3*exp(-LambertW(3)))) assert solveset_real(3*log(a**(3*x + 5)) + a**(3*x + 5), x) == \ FiniteSet(-((log(a**5) + LambertW(S(1)/3))/(3*log(a)))) p = symbols('p', positive=True) assert solveset_real(3*log(p**(3*x + 5)) + p**(3*x + 5), x) == \ FiniteSet( log((-3**(S(1)/3) - 3**(S(5)/6)*I)*LambertW(S(1)/3)**(S(1)/3)/(2*p**(S(5)/3)))/log(p), log((-3**(S(1)/3) + 3**(S(5)/6)*I)*LambertW(S(1)/3)**(S(1)/3)/(2*p**(S(5)/3)))/log(p), log((3*LambertW(S(1)/3)/p**5)**(1/(3*log(p)))),) # checked numerically # check collection b = Symbol('b') eq = 3 * log(a**(3 * x + 5)) + b * log(a**(3 * x + 5)) + a**(3 * x + 5) assert solveset_real( eq, x) == FiniteSet(-((log(a**5) + LambertW(1 / (b + 3))) / (3 * log(a)))) # issue 4271 assert solveset_real((a / x + exp(x / 2)).diff(x, 2), x) == FiniteSet(6 * LambertW( (-1)**(S(1) / 3) * a**(S(1) / 3) / 3)) assert solveset_real(x**3 - 3**x, x) == \ FiniteSet(-3/log(3)*LambertW(-log(3)/3)) assert solveset_real(x**2 - 2**x, x) == FiniteSet(2) assert solveset_real(-x**2 + 2**x, x) == FiniteSet(2) assert solveset_real(3**cos(x) - cos(x)**3) == FiniteSet( acos(-3 * LambertW(-log(3) / 3) / log(3))) assert solveset_real(4**(x / 2) - 2**(x / 3), x) == FiniteSet(0) assert solveset_real(5**(x / 2) - 2**(x / 3), x) == FiniteSet(0) b = sqrt(6) * sqrt(log(2)) / sqrt(log(5)) assert solveset_real(5**(x / 2) - 2**(3 / x), x) == FiniteSet(-b, b)
def plot_and_save(name): tmp_file = TmpFileManager.tmp_file x = Symbol('x') y = Symbol('y') z = Symbol('z') ### # Examples from the 'introduction' notebook ### p = plot(x) p = plot(x * sin(x), x * cos(x)) p.extend(p) p[0].line_color = lambda a: a p[1].line_color = 'b' p.title = 'Big title' p.xlabel = 'the x axis' p[1].label = 'straight line' p.legend = True p.aspect_ratio = (1, 1) p.xlim = (-15, 20) p.save(tmp_file('%s_basic_options_and_colors' % name)) p._backend.close() p.extend(plot(x + 1)) p.append(plot(x + 3, x**2)[1]) p.save(tmp_file('%s_plot_extend_append' % name)) p[2] = plot(x**2, (x, -2, 3)) p.save(tmp_file('%s_plot_setitem' % name)) p._backend.close() p = plot(sin(x), (x, -2 * pi, 4 * pi)) p.save(tmp_file('%s_line_explicit' % name)) p._backend.close() p = plot(sin(x)) p.save(tmp_file('%s_line_default_range' % name)) p._backend.close() p = plot((x**2, (x, -5, 5)), (x**3, (x, -3, 3))) p.save(tmp_file('%s_line_multiple_range' % name)) p._backend.close() raises(ValueError, lambda: plot(x, y)) p = plot(Piecewise((1, x > 0), (0, True)), (x, -1, 1)) p.save(tmp_file('%s_plot_piecewise' % name)) p._backend.close() #parametric 2d plots. #Single plot with default range. plot_parametric(sin(x), cos(x)).save(tmp_file()) #Single plot with range. p = plot_parametric(sin(x), cos(x), (x, -5, 5)) p.save(tmp_file('%s_parametric_range' % name)) p._backend.close() #Multiple plots with same range. p = plot_parametric((sin(x), cos(x)), (x, sin(x))) p.save(tmp_file('%s_parametric_multiple' % name)) p._backend.close() #Multiple plots with different ranges. p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5))) p.save(tmp_file('%s_parametric_multiple_ranges' % name)) p._backend.close() #depth of recursion specified. p = plot_parametric(x, sin(x), depth=13) p.save(tmp_file('%s_recursion_depth' % name)) p._backend.close() #No adaptive sampling. p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500) p.save(tmp_file('%s_adaptive' % name)) p._backend.close() #3d parametric plots p = plot3d_parametric_line(sin(x), cos(x), x) p.save(tmp_file('%s_3d_line' % name)) p._backend.close() p = plot3d_parametric_line((sin(x), cos(x), x, (x, -5, 5)), (cos(x), sin(x), x, (x, -3, 3))) p.save(tmp_file('%s_3d_line_multiple' % name)) p._backend.close() p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30) p.save(tmp_file('%s_3d_line_points' % name)) p._backend.close() # 3d surface single plot. p = plot3d(x * y) p.save(tmp_file('%s_surface' % name)) p._backend.close() # Multiple 3D plots with same range. p = plot3d(-x * y, x * y, (x, -5, 5)) p.save(tmp_file('%s_surface_multiple' % name)) p._backend.close() # Multiple 3D plots with different ranges. p = plot3d((x * y, (x, -3, 3), (y, -3, 3)), (-x * y, (x, -3, 3), (y, -3, 3))) p.save(tmp_file('%s_surface_multiple_ranges' % name)) p._backend.close() # Single Parametric 3D plot p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y) p.save(tmp_file('%s_parametric_surface' % name)) p._backend.close() # Multiple Parametric 3D plots. p = plot3d_parametric_surface( (x * sin(z), x * cos(z), z, (x, -5, 5), (z, -5, 5)), (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5))) p.save(tmp_file('%s_parametric_surface' % name)) p._backend.close() ### # Examples from the 'colors' notebook ### p = plot(sin(x)) p[0].line_color = lambda a: a p.save(tmp_file('%s_colors_line_arity1' % name)) p[0].line_color = lambda a, b: b p.save(tmp_file('%s_colors_line_arity2' % name)) p._backend.close() p = plot(x * sin(x), x * cos(x), (x, 0, 10)) p[0].line_color = lambda a: a p.save(tmp_file('%s_colors_param_line_arity1' % name)) p[0].line_color = lambda a, b: a p.save(tmp_file('%s_colors_param_line_arity2a' % name)) p[0].line_color = lambda a, b: b p.save(tmp_file('%s_colors_param_line_arity2b' % name)) p._backend.close() p = plot3d_parametric_line( sin(x) + 0.1 * sin(x) * cos(7 * x), cos(x) + 0.1 * cos(x) * cos(7 * x), 0.1 * sin(7 * x), (x, 0, 2 * pi)) p[0].line_color = lambdify_(x, sin(4 * x)) p.save(tmp_file('%s_colors_3d_line_arity1' % name)) p[0].line_color = lambda a, b: b p.save(tmp_file('%s_colors_3d_line_arity2' % name)) p[0].line_color = lambda a, b, c: c p.save(tmp_file('%s_colors_3d_line_arity3' % name)) p._backend.close() p = plot3d(sin(x) * y, (x, 0, 6 * pi), (y, -5, 5)) p[0].surface_color = lambda a: a p.save(tmp_file('%s_colors_surface_arity1' % name)) p[0].surface_color = lambda a, b: b p.save(tmp_file('%s_colors_surface_arity2' % name)) p[0].surface_color = lambda a, b, c: c p.save(tmp_file('%s_colors_surface_arity3a' % name)) p[0].surface_color = lambdify_((x, y, z), sqrt((x - 3 * pi)**2 + y**2)) p.save(tmp_file('%s_colors_surface_arity3b' % name)) p._backend.close() p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y, (x, -1, 1), (y, -1, 1)) p[0].surface_color = lambda a: a p.save(tmp_file('%s_colors_param_surf_arity1' % name)) p[0].surface_color = lambda a, b: a * b p.save(tmp_file('%s_colors_param_surf_arity2' % name)) p[0].surface_color = lambdify_((x, y, z), sqrt(x**2 + y**2 + z**2)) p.save(tmp_file('%s_colors_param_surf_arity3' % name)) p._backend.close() ### # Examples from the 'advanced' notebook ### # XXX: This raises the warning "The evaluation of the expression is # problematic. We are trying a failback method that may still work. Please # report this as a bug." It has to use the fallback because using evalf() # is the only way to evaluate the integral. We should perhaps just remove # that warning. with warnings.catch_warnings(record=True) as w: i = Integral(log((sin(x)**2 + 1) * sqrt(x**2 + 1)), (x, 0, y)) p = plot(i, (y, 1, 5)) p.save(tmp_file('%s_advanced_integral' % name)) p._backend.close() # Make sure no other warnings were raised assert len(w) == 1 assert issubclass(w[-1].category, UserWarning) assert "The evaluation of the expression is problematic" in str( w[0].message) s = Sum(1 / x**y, (x, 1, oo)) p = plot(s, (y, 2, 10)) p.save(tmp_file('%s_advanced_inf_sum' % name)) p._backend.close() p = plot(Sum(1 / x, (x, 1, y)), (y, 2, 10), show=False) p[0].only_integers = True p[0].steps = True p.save(tmp_file('%s_advanced_fin_sum' % name)) p._backend.close() ### # Test expressions that can not be translated to np and generate complex # results. ### plot(sin(x) + I * cos(x)).save(tmp_file()) plot(sqrt(sqrt(-x))).save(tmp_file()) plot(LambertW(x)).save(tmp_file()) plot(sqrt(LambertW(x))).save(tmp_file()) #Characteristic function of a StudentT distribution with nu=10 plot((meijerg( ((1 / 2, ), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(-I * pi) / 2) + meijerg( ((1 / 2, ), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(I * pi) / 2)) / (48 * pi), (x, 1e-6, 1e-2)).save(tmp_file())
def test_issue_2574(): eq = -x + exp(exp(LambertW(log(x))) * LambertW(log(x))) assert checksol(eq, x, 2) is True assert checksol(eq, x, 2, numerical=False) is None
def test_pmint_LambertW(): f = LambertW(x) g = x * LambertW(x) - x + x / LambertW(x) assert heurisch(f, x) == g
def test_tsolve(): assert solve(exp(x) - 3, x) == [log(3)] assert set(solve((a * x + b) * (exp(x) - 3), x)) == set([-b / a, log(3)]) assert solve(cos(x) - y, x) == [acos(y)] assert solve(2 * cos(x) - y, x) == [acos(y / 2)] assert set(solve(Eq(cos(x), sin(x)), x)) == set([-3 * pi / 4, pi / 4]) assert set(solve(exp(x) + exp(-x) - y, x)) in [ set([ log(y / 2 - sqrt(y**2 - 4) / 2), log(y / 2 + sqrt(y**2 - 4) / 2), ]), set([ log(y - sqrt(y**2 - 4)) - log(2), log(y + sqrt(y**2 - 4)) - log(2) ]) ] assert solve(exp(x) - 3, x) == [log(3)] assert solve(Eq(exp(x), 3), x) == [log(3)] assert solve(log(x) - 3, x) == [exp(3)] assert solve(sqrt(3 * x) - 4, x) == [Rational(16, 3)] assert solve(3**(x + 2), x) == [] assert solve(3**(2 - x), x) == [] assert solve(x + 2**x, x) == [-LambertW(log(2)) / log(2)] assert solve(3 * x + 5 + 2**(-5 * x + 3), x) in [ [ -((25 * log(2) - 3 * LambertW(-10240 * 2** (Rational(1, 3)) * log(2) / 3)) / (15 * log(2))) ], [ -Rational(5, 3) + LambertW(log(2**(-10240 * 2**(Rational(1, 3)) / 3))) / (5 * log(2)) ], [ -Rational(5, 3) + LambertW(-10240 * 2**Rational(1, 3) * log(2) / 3) / (5 * log(2)) ], [(-25 * log(2) + 3 * LambertW(-10240 * 2**(Rational(1, 3)) * log(2) / 3)) / (15 * log(2))], [ -((25 * log(2) - 3 * LambertW(-10240 * 2** (Rational(1, 3)) * log(2) / 3))) / (15 * log(2)) ], [ -(25 * log(2) - 3 * LambertW(log(2**(-10240 * 2**Rational(1, 3) / 3)))) / (15 * log(2)) ], [(25 * log(2) - 3 * LambertW(log(2**(-10240 * 2**Rational(1, 3) / 3)))) / (-15 * log(2))] ] assert solve(5*x - 1 + 3*exp(2 - 7*x), x) == \ [Rational(1, 5) + LambertW(-21*exp(Rational(3, 5))/5)/7] assert solve(2*x + 5 + log(3*x - 2), x) == \ [Rational(2, 3) + LambertW(2*exp(-Rational(19, 3))/3)/2] assert solve(3 * x + log(4 * x), x) == [LambertW(Rational(3, 4)) / 3] assert set(solve((2 * x + 8) * (8 + exp(x)), x)) == set([S(-4), log(8) + pi * I]) eq = 2 * exp(3 * x + 4) - 3 ans = solve(eq, x) # this generated a failure in flatten assert len(ans) == 3 and all(eq.subs(x, a).n(chop=True) == 0 for a in ans) assert solve(2 * log(3 * x + 4) - 3, x) == [(exp(Rational(3, 2)) - 4) / 3] assert solve(exp(x) + 1, x) == [pi * I] assert solve(x**2 - 2**x, x) == [2] assert solve(x**3 - 3**x, x) == [-3 * LambertW(-log(3) / 3) / log(3)] A = -7 * 2**Rational(4, 5) * 6**Rational(1, 5) * log(7) / 10 B = -7 * 3**Rational(1, 5) * log(7) / 5 result = solve(2 * (3 * x + 4)**5 - 6 * 7**(3 * x + 9), x) assert len(result) == 1 and expand(result[0]) in [ Rational(-4, 3) - 5 / log(7) / 3 * LambertW(A), Rational(-4, 3) - 5 / log(7) / 3 * LambertW(B), ] assert solve(z * cos(x) - y, x) == [acos(y / z)] assert solve(z * cos(2 * x) - y, x) == [acos(y / z) / 2] assert solve(z * cos(sin(x)) - y, x) == [asin(acos(y / z))] assert solve(z * cos(x), x) == [acos(0)] # issue #1409 assert solve(y - b * x / (a + x), x) in [[-a * y / (y - b)], [a * y / (b - y)]] assert solve(y - b * exp(a / x), x) == [a / log(y / b)] # issue #1408 assert solve(y - b / (1 + a * x), x) in [[(b - y) / (a * y)], [-((y - b) / (a * y))]] # issue #1407 assert solve(y - a * x**b, x) == [(y / a)**(1 / b)] # issue #1406 assert solve(z**x - y, x) == [log(y) / log(z)] # issue #1405 assert solve(2**x - 10, x) == [log(10) / log(2)]
def test_lambertw(): x = Symbol('x') assert LambertW(x) == LambertW(x) assert LambertW(0) == 0 assert LambertW(E) == 1 assert LambertW(-1 / E) == -1 assert LambertW(-log(2) / 2) == -log(2) assert LambertW(oo) == oo assert LambertW( x**2).diff(x) == 2 * LambertW(x**2) / x / (1 + LambertW(x**2)) assert LambertW(sqrt(2)).evalf(30).epsilon_eq( Float("0.701338383413663009202120278965", 30), 1e-29) assert LambertW(0, evaluate=False).is_algebraic na = Symbol('na', nonzero=True, algebraic=True) assert LambertW(na).is_algebraic is False