def test_solve_trig(): from sympy.abc import n assert solveset_real(sin(x), x) == \ Union(imageset(Lambda(n, 2*pi*n), S.Integers), imageset(Lambda(n, 2*pi*n + pi), S.Integers)) assert solveset_real(sin(x) - 1, x) == \ imageset(Lambda(n, 2*pi*n + pi/2), S.Integers) assert solveset_real(cos(x), x) == \ Union(imageset(Lambda(n, 2*pi*n - pi/2), S.Integers), imageset(Lambda(n, 2*pi*n + pi/2), S.Integers)) assert solveset_real(sin(x) + cos(x), x) == \ Union(imageset(Lambda(n, 2*n*pi - pi/4), S.Integers), imageset(Lambda(n, 2*n*pi + 3*pi/4), S.Integers)) assert solveset_real(sin(x)**2 + cos(x)**2, x) == S.EmptySet assert solveset_complex(cos(x) - S.Half, x) == \ Union(imageset(Lambda(n, 2*n*pi + pi/3), S.Integers), imageset(Lambda(n, 2*n*pi - pi/3), S.Integers)) y, a = symbols('y,a') assert solveset(sin(y + a) - sin(y), a, domain=S.Reals) == \ imageset(Lambda(n, 2*n*pi), S.Integers)
def test_solveset_complex_exp(): from sympy.abc import x, n assert solveset_complex(exp(x) - 1, x) == imageset(Lambda(n, I * 2 * n * pi), S.Integers) assert solveset_complex(exp(x) - I, x) == imageset(Lambda(n, I * (2 * n * pi + pi / 2)), S.Integers) assert solveset_complex(1 / exp(x), x) == S.EmptySet assert solveset_complex(sinh(x).rewrite(exp), x) == imageset(Lambda(n, n * pi * I), S.Integers)
def test_solve_trig_simplified(): from sympy.abc import n assert solveset_real(sin(x), x) == imageset(Lambda(n, n * pi), S.Integers) assert solveset_real(cos(x), x) == imageset(Lambda(n, n * pi + pi / 2), S.Integers) assert solveset_real(cos(x) + sin(x), x) == imageset(Lambda(n, n * pi - pi / 4), S.Integers)
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_solveset(): x = Symbol("x") raises(ValueError, lambda: solveset(x + y)) assert solveset(exp(x) - 1, domain=S.Reals) == FiniteSet(0) assert solveset(exp(x) - 1, x, S.Reals) == FiniteSet(0) assert solveset(Eq(exp(x), 1), x, S.Reals) == FiniteSet(0) assert solveset(x - 1 >= 0, x, S.Reals) == Interval(1, oo) assert solveset(exp(x) - 1 >= 0, x, S.Reals) == Interval(0, oo) assert solveset(exp(x) - 1, x) == imageset(Lambda(n, 2 * I * pi * n), S.Integers) assert solveset(Eq(exp(x), 1), x) == imageset(Lambda(n, 2 * I * pi * n), S.Integers)
def test_solveset(): x = Symbol('x', real=True) raises(ValueError, lambda: solveset(x + y)) assert solveset(exp(x) - 1) == FiniteSet(0) assert solveset(exp(x) - 1, x) == FiniteSet(0) assert solveset(Eq(exp(x), 1), x) == FiniteSet(0) assert solveset(x - 1 >= 0, x) == Interval(1, oo) assert solveset(exp(x) - 1 >= 0, x) == Interval(0, oo) x = Symbol('x') assert solveset(exp(x) - 1, x) == imageset(Lambda(n, 2*I*pi*n), S.Integers) assert solveset(Eq(exp(x), 1), x) == imageset(Lambda(n, 2*I*pi*n), S.Integers)
def test_imageset(): ints = S.Integers raises(TypeError, lambda: imageset(x, ints)) raises(ValueError, lambda: imageset(x, y, z, ints)) raises(ValueError, lambda: imageset(Lambda(x, cos(x)), y)) assert imageset(cos, ints) == ImageSet(Lambda(x, cos(x)), ints) def f(x): return cos(x) raises(TypeError, lambda: imageset(f, ints)) f = lambda x: cos(x) assert imageset(f, ints) == ImageSet(Lambda(x, cos(x)), ints) assert imageset(x, 1, ints) == FiniteSet(1) assert imageset(x, y, ints) == FiniteSet(y) assert (str(imageset(lambda y: x + y, Interval(-2, 1)).lamda.expr) in ('_x + x', 'x + _x')) x1, x2 = symbols("x1, x2") assert imageset(lambda x,y: Add(x,y), Interval(1,2), Interval(2, 3)) == \ ImageSet(Lambda((x1, x2), x1+x2), Interval(1,2), Interval(2,3))
def test_imageset(): ints = S.Integers raises(TypeError, lambda: imageset(x, ints)) raises(ValueError, lambda: imageset(x, y, z, ints)) raises(ValueError, lambda: imageset(Lambda(x, cos(x)), y)) assert imageset(cos, ints) == ImageSet(Lambda(x, cos(x)), ints) def f(x): return cos(x) raises(TypeError, lambda: imageset(f, ints)) f = lambda x: cos(x) assert imageset(f, ints) == ImageSet(Lambda(x, cos(x)), ints) assert imageset(x, 1, ints) == FiniteSet(1) assert imageset(x, y, ints) == FiniteSet(y) assert (str(imageset(lambda y: x + y, Interval(-2, 1)).lamda.expr) in ('_x + x', 'x + _x'))
def test_invert_complex(): assert invert_complex(x + 3, y, x) == (x, FiniteSet(y - 3)) assert invert_complex(x * 3, y, x) == (x, FiniteSet(y / 3)) assert invert_complex(exp(x), y, x) == (x, imageset(Lambda(n, I * (2 * pi * n + arg(y)) + log(Abs(y))), S.Integers)) assert invert_complex(log(x), y, x) == (x, FiniteSet(exp(y))) raises(ValueError, lambda: invert_real(S.One, y, x)) raises(ValueError, lambda: invert_complex(x, x, x))
def test_imageset(): ints = S.Integers raises(TypeError, lambda: imageset(x, ints)) raises(ValueError, lambda: imageset(x, y, z, ints)) raises(ValueError, lambda: imageset(Lambda(x, cos(x)), y)) assert imageset(cos, ints) == ImageSet(Lambda(x, cos(x)), ints) def f(x): return cos(x) raises(TypeError, lambda: imageset(f, ints)) f = lambda x: cos(x) assert imageset(f, ints) == ImageSet(Lambda(x, cos(x)), ints) assert imageset(x, 1, ints) == FiniteSet(1) assert imageset(x, y, ints) == FiniteSet(y) assert str(imageset(lambda y: x + y, Interval(-2, 1)).lamda.expr) in ("_x + x", "x + _x")
def test_solveset(): x = Symbol('x') raises(ValueError, lambda: solveset(x + y)) raises(ValueError, lambda: solveset(x, 1)) assert solveset(0, domain=S.Reals) == S.Reals assert solveset(1) == S.EmptySet assert solveset(True, domain=S.Reals) == S.Reals # issue 10197 assert solveset(False, domain=S.Reals) == S.EmptySet assert solveset(exp(x) - 1, domain=S.Reals) == FiniteSet(0) assert solveset(exp(x) - 1, x, S.Reals) == FiniteSet(0) assert solveset(Eq(exp(x), 1), x, S.Reals) == FiniteSet(0) assert solveset(x - 1 >= 0, x, S.Reals) == Interval(1, oo) assert solveset(exp(x) - 1 >= 0, x, S.Reals) == Interval(0, oo) assert solveset(exp(x) - 1, x) == imageset(Lambda(n, 2*I*pi*n), S.Integers) assert solveset(Eq(exp(x), 1), x) == imageset(Lambda(n, 2*I*pi*n), S.Integers)
def test_invert_complex(): assert invert_complex(x + 3, y, x) == (x, FiniteSet(y - 3)) assert invert_complex(x * 3, y, x) == (x, FiniteSet(y / 3)) assert invert_complex(exp(x), y, x) == \ (x, imageset(Lambda(n, I*(2*pi*n + arg(y)) + log(Abs(y))), S.Integers)) assert invert_complex(log(x), y, x) == (x, FiniteSet(exp(y))) raises(ValueError, lambda: invert_real(S.One, y, x)) raises(ValueError, lambda: invert_complex(x, x, x))
def test_image_interval(): x = Symbol("x", real=True) assert imageset(x, 2 * x, Interval(-2, 1)) == Interval(-4, 2) assert imageset(x, 2 * x, Interval(-2, 1, True, False)) == Interval(-4, 2, True, False) assert imageset(x, x ** 2, Interval(-2, 1, True, False)) == Interval(0, 4, False, True) assert imageset(x, x ** 2, Interval(-2, 1)) == Interval(0, 4) assert imageset(x, x ** 2, Interval(-2, 1, True, False)) == Interval(0, 4, False, True) assert imageset(x, x ** 2, Interval(-2, 1, True, True)) == Interval(0, 4, False, True)
def test_conditionset(): assert solveset(Eq(sin(x)**2 + cos(x)**2, 1), x, domain=S.Reals) == \ ConditionSet(x, True, S.Reals) assert solveset(Eq(x**2 + x*sin(x), 1), x, domain=S.Reals) == \ ConditionSet(x, Eq(x*(x + sin(x)) - 1, 0), S.Reals) assert solveset(Eq(sin(Abs(x)), x), x, domain=S.Reals) == \ ConditionSet(x, Eq(-x + sin(Abs(x)), 0), Interval(-oo, oo)) assert solveset(Eq(-I*(exp(I*x) - exp(-I*x))/2, 1), x) == \ imageset(Lambda(n, 2*n*pi + pi/2), S.Integers) assert solveset(x + sin(x) > 1, x, domain=S.Reals) == \ ConditionSet(x, x + sin(x) > 1, S.Reals)
def test_conditonset(): assert solveset(Eq(sin(x)**2 + cos(x)**2, 1), x, domain=S.Reals) == \ ConditionSet(x, True, S.Reals) assert solveset(Eq(x**2 + x*sin(x), 1), x, domain=S.Reals) == \ ConditionSet(x, Eq(x*(x + sin(x)) - 1, 0), S.Reals) assert solveset(Eq(sin(Abs(x)), x), x, domain=S.Reals) == \ ConditionSet(x, Eq(-x + sin(Abs(x)), 0), Interval(-oo, oo)) assert solveset(Eq(-I*(exp(I*x) - exp(-I*x))/2, 1), x) == \ imageset(Lambda(n, 2*n*pi + pi/2), S.Integers) assert solveset(x + sin(x) > 1, x, domain=S.Reals) == \ ConditionSet(x, x + sin(x) > 1, S.Reals)
def test_image_interval(): x = Symbol('x', real=True) assert imageset(x, 2 * x, Interval(-2, 1)) == Interval(-4, 2) assert imageset(x, 2*x, Interval(-2, 1, True, False)) == \ Interval(-4, 2, True, False) assert imageset(x, x**2, Interval(-2, 1, True, False)) == \ Interval(0, 4, False, True) assert imageset(x, x**2, Interval(-2, 1)) == Interval(0, 4) assert imageset(x, x**2, Interval(-2, 1, True, False)) == \ Interval(0, 4, False, True) assert imageset(x, x**2, Interval(-2, 1, True, True)) == \ Interval(0, 4, False, True)
def test_solve_trig(): from sympy.abc import n assert solveset_real(sin(x), x) == \ Union(imageset(Lambda(n, 2*pi*n), S.Integers), imageset(Lambda(n, 2*pi*n + pi), S.Integers)) assert solveset_real(sin(x) - 1, x) == \ imageset(Lambda(n, 2*pi*n + pi/2), S.Integers) assert solveset_real(cos(x), x) == \ Union(imageset(Lambda(n, 2*pi*n - pi/2), S.Integers), imageset(Lambda(n, 2*pi*n + pi/2), S.Integers)) assert solveset_real(sin(x) + cos(x), x) == \ Union(imageset(Lambda(n, 2*n*pi - pi/4), S.Integers), imageset(Lambda(n, 2*n*pi + 3*pi/4), S.Integers)) assert solveset_real(sin(x)**2 + cos(x)**2, x) == S.EmptySet
def max_univariate_value(expr, symbol, domain=sm.S.Reals): printing.init_printing(use_latex='png') diff = sm.diff(expr, symbol).simplify() if not symbol in diff.free_symbols: #for constant exprs return sm.Interval(0,0); solution = sm.solveset(sm.Eq(diff, 0), symbol, domain).intersection(domain) display(solution) #new_solutions = [] #for new_sol in solution: # for old_sol in solutions: # in_sol = sm.solveset(sm.Eq(symbol, new_sol), symbol, domain) # new_solutions.append(in_sol.intersection(old_sol)) #solutions = new_solutions maximum = sm.imageset(symbol, expr, solution).sup result = sm.solveset(sm.Eq(expr, maximum), symbol, domain) return result
def test_issue_16878b(): # in intersection_sets for (ImageSet, Set) there is no code # that handles the base_set of S.Reals like there is # for Integers assert imageset(x, (x, x), S.Reals).is_subset(S.Reals**2) is True
def test_solveset_complex_tan(): s = solveset_complex(tan(x).rewrite(exp), x) assert s == imageset(Lambda(n, pi*n), S.Integers) - \ imageset(Lambda(n, pi*n + pi/2), S.Integers)
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_imageset(): ints = S.Integers assert imageset(x, x - 1, S.Naturals) is S.Naturals0 assert imageset(x, x + 1, S.Naturals0) is S.Naturals assert imageset(x, abs(x), S.Naturals0) is S.Naturals0 assert imageset(x, abs(x), S.Naturals) is S.Naturals assert imageset(x, abs(x), S.Integers) is S.Naturals0 # issue 16878a r = symbols('r', real=True) assert (1, r) not in imageset(x, (x, x), S.Reals) assert (r, r) in imageset(x, (x, x), S.Reals) assert 1 + I in imageset(x, x + I, S.Reals) assert {1} not in imageset(x, (x,), S.Reals) assert (1, 1) not in imageset(x, (x,) , S.Reals) raises(TypeError, lambda: imageset(x, ints)) raises(ValueError, lambda: imageset(x, y, z, ints)) raises(ValueError, lambda: imageset(Lambda(x, cos(x)), y)) raises(ValueError, lambda: imageset(Lambda(x, x), ints, ints)) assert imageset(cos, ints) == ImageSet(Lambda(x, cos(x)), ints) def f(x): return cos(x) assert imageset(f, ints) == imageset(x, cos(x), ints) f = lambda x: cos(x) assert imageset(f, ints) == ImageSet(Lambda(x, cos(x)), ints) assert imageset(x, 1, ints) == FiniteSet(1) assert imageset(x, y, ints) == {y} assert imageset((x, y), (1, z), ints*S.Reals) == {(1, z)} clash = Symbol('x', integer=true) assert (str(imageset(lambda x: x + clash, Interval(-2, 1)).lamda.expr) in ('_x + x', 'x + _x')) x1, x2 = symbols("x1, x2") assert imageset(lambda x,y: Add(x,y), Interval(1,2), Interval(2, 3)) == \ ImageSet(Lambda((x1, x2), x1+x2), Interval(1,2), Interval(2,3))
def test_solve_trig(): from sympy.abc import n assert solveset_real(sin(x), x) == \ Union(imageset(Lambda(n, 2*pi*n), S.Integers), imageset(Lambda(n, 2*pi*n + pi), S.Integers)) assert solveset_real(sin(x) - 1, x) == \ imageset(Lambda(n, 2*pi*n + pi/2), S.Integers) assert solveset_real(cos(x), x) == \ Union(imageset(Lambda(n, 2*pi*n - pi/2), S.Integers), imageset(Lambda(n, 2*pi*n + pi/2), S.Integers)) assert solveset_real(sin(x) + cos(x), x) == \ Union(imageset(Lambda(n, 2*n*pi - pi/4), S.Integers), imageset(Lambda(n, 2*n*pi + 3*pi/4), S.Integers)) assert solveset_real(sin(x)**2 + cos(x)**2, x) == S.EmptySet assert solveset_complex(cos(x) - S.Half, x) == \ Union(imageset(Lambda(n, 2*n*pi + pi/3), S.Integers), imageset(Lambda(n, 2*n*pi - pi/3), S.Integers)) y, a = symbols('y,a') assert solveset(sin(y + a) - sin(y), a, domain=S.Reals) == \ Union(imageset(Lambda(n, 2*n*pi), S.Integers), imageset(Lambda(n, -I*(I*(2*n*pi +arg(-exp(-2*I*y))) + 2*im(y))), S.Integers))
def test_solveset_complex_exp(): from sympy.abc import x, n assert solveset_complex(exp(x) - 1, x) == \ imageset(Lambda(n, I*2*n*pi), S.Integers) assert solveset_complex(exp(x) - I, x) == \ imageset(Lambda(n, I*(2*n*pi + pi/2)), S.Integers)
def test_image_piecewise(): f = Piecewise((x, x <= -1), (1/x**2, x <= 5), (x**3, True)) f1 = Piecewise((0, x <= 1), (1, x <= 2), (2, True)) assert imageset(x, f, Interval(-5, 5)) == Union(Interval(-5, -1), Interval(S(1)/25, oo)) assert imageset(x, f1, Interval(1, 2)) == FiniteSet(0, 1)
def test_image_FiniteSet(): x = Symbol('x', real=True) assert imageset(x, 2*x, FiniteSet(1, 2, 3)) == FiniteSet(2, 4, 6)
def test_image_EmptySet(): x = Symbol('x', real=True) assert imageset(x, 2*x, S.EmptySet) == S.EmptySet
def test_image_interval(): from sympy.core.numbers import Rational x = Symbol('x', real=True) a = Symbol('a', real=True) assert imageset(x, 2*x, Interval(-2, 1)) == Interval(-4, 2) assert imageset(x, 2*x, Interval(-2, 1, True, False)) == \ Interval(-4, 2, True, False) assert imageset(x, x**2, Interval(-2, 1, True, False)) == \ Interval(0, 4, False, True) assert imageset(x, x**2, Interval(-2, 1)) == Interval(0, 4) assert imageset(x, x**2, Interval(-2, 1, True, False)) == \ Interval(0, 4, False, True) assert imageset(x, x**2, Interval(-2, 1, True, True)) == \ Interval(0, 4, False, True) assert imageset(x, (x - 2)**2, Interval(1, 3)) == Interval(0, 1) assert imageset(x, 3*x**4 - 26*x**3 + 78*x**2 - 90*x, Interval(0, 4)) == \ Interval(-35, 0) # Multiple Maxima assert imageset(x, x + 1/x, Interval(-oo, oo)) == Interval(-oo, -2) \ + Interval(2, oo) # Single Infinite discontinuity assert imageset(x, 1/x + 1/(x-1)**2, Interval(0, 2, True, False)) == \ Interval(Rational(3, 2), oo, False) # Multiple Infinite discontinuities # Test for Python lambda assert imageset(lambda x: 2*x, Interval(-2, 1)) == Interval(-4, 2) assert imageset(Lambda(x, a*x), Interval(0, 1)) == \ ImageSet(Lambda(x, a*x), Interval(0, 1)) assert imageset(Lambda(x, sin(cos(x))), Interval(0, 1)) == \ ImageSet(Lambda(x, sin(cos(x))), Interval(0, 1))
def test_invert_real(): x = Symbol('x', real=True) y = Symbol('y') n = Symbol('n') def ireal(x, s=S.Reals): return Intersection(s, x) 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) assert invert_real(exp(x), y, x) == (x, ireal(FiniteSet(log(y)))) y = Symbol('y', positive=True) 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, ireal(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, ireal(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(-oo, -3), FiniteSet(-a + b - 3), imageset(Lambda(n, n - a - 3), Interval(0, oo))) h2 = Intersection(Interval(-3, oo), FiniteSet(a - b - 3), imageset(Lambda(n, -n + a - 3), Interval(0, oo))) assert invert_real(Abs(Abs(x + 3) - a) - b, 0, x) == (x, Union(h1, h2))
def test_image_Intersection(): x = Symbol('x', real=True) y = Symbol('y', real=True) assert imageset(x, x**2, Interval(-2, 0).intersect(Interval(x, y))) == \ Interval(0, 4).intersect(Interval(Min(x**2, y**2), Max(x**2, y**2)))
def test_image_Union(): x = Symbol('x', real=True) assert imageset(x, x**2, Interval(-2, 0) + FiniteSet(1, 2, 3)) == \ (Interval(0, 4) + FiniteSet(9))
def test_solveset_complex_exp(): from sympy.abc import x, n assert solveset_complex(exp(x) - 1, x) == imageset(Lambda(n, I * 2 * n * pi), S.Integers) assert solveset_complex(exp(x) - I, x) == imageset(Lambda(n, I * (2 * n * pi + pi / 2)), S.Integers)
def test_issue_10113(): f = x**2/(x**2 - 4) assert imageset(x, f, S.Reals) == Union(Interval(-oo, 0), Interval(1, oo, True, True)) assert imageset(x, f, Interval(-2, 2)) == Interval(-oo, 0) assert imageset(x, f, Interval(-2, 3)) == Union(Interval(-oo, 0), Interval(S(9)/5, oo))
def test_imageset(): ints = S.Integers raises(TypeError, lambda: imageset(x, ints)) raises(ValueError, lambda: imageset(x, y, z, ints)) raises(ValueError, lambda: imageset(Lambda(x, cos(x)), y)) assert imageset(cos, ints) == ImageSet(Lambda(x, cos(x)), ints) def f(x): return cos(x) assert imageset(f, ints) == imageset(x, cos(x), ints) f = lambda x: cos(x) assert imageset(f, ints) == ImageSet(Lambda(x, cos(x)), ints) assert imageset(x, 1, ints) == FiniteSet(1) assert imageset(x, y, ints) == FiniteSet(y) clash = Symbol('x', integer=true) assert (str(imageset(lambda x: x + clash, Interval(-2, 1)).lamda.expr) in ('_x + x', 'x + _x')) x1, x2 = symbols("x1, x2") assert imageset(lambda x,y: Add(x,y), Interval(1,2), Interval(2, 3)) == \ ImageSet(Lambda((x1, x2), x1+x2), Interval(1,2), Interval(2,3))