def test_solve_rational(): assert solveset_real(1/x + 1, x) == FiniteSet(-S.One) assert solveset_real(1/exp(x) - 1, x) == FiniteSet(0) assert solveset_real(x*(1 - 5/x), x) == FiniteSet(5) assert solveset_real(2*x/(x + 2) - 1, x) == FiniteSet(2) assert solveset_real((x**2/(7 - x)).diff(x), x) == \ FiniteSet(S(0), S(14))
def test_uselogcombine_1(): assert solveset_real(log(x - 3) + log(x + 3), x) == \ FiniteSet(sqrt(10)) assert solveset_real(log(x + 1) - log(2*x - 1), x) == FiniteSet(2) assert solveset_real(log(x + 3) + log(1 + 3/x) - 3) == FiniteSet( -3 + sqrt(-12 + exp(3))*exp(S(3)/2)/2 + exp(3)/2, -sqrt(-12 + exp(3))*exp(S(3)/2)/2 - 3 + exp(3)/2)
def test_garbage_input(): raises(ValueError, lambda: solveset_real([x], x)) raises(ValueError, lambda: solveset_real(x, pi)) raises(ValueError, lambda: solveset_real(x, x ** 2)) raises(ValueError, lambda: solveset_complex([x], x)) raises(ValueError, lambda: solveset_complex(x, pi))
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_errorinverses(): assert solveset_real(erf(x) - S.One/2, x) == \ FiniteSet(erfinv(S.One/2)) assert solveset_real(erfinv(x) - 2, x) == \ FiniteSet(erf(2)) assert solveset_real(erfc(x) - S.One, x) == \ FiniteSet(erfcinv(S.One)) assert solveset_real(erfcinv(x) - 2, x) == FiniteSet(erfc(2))
def test_solve_abs(): assert solveset_real(Abs(x) - 2, x) == FiniteSet(-2, 2) assert solveset_real(Abs(x + 3) - 2*Abs(x - 3), x) == \ FiniteSet(1, 9) assert solveset_real(2*Abs(x) - Abs(x - 1), x) == \ FiniteSet(-1, Rational(1, 3)) assert solveset_real(Abs(x - 7) - 8, x) == FiniteSet(-S(1), S(15))
def test_piecewise(): eq = Piecewise((x - 2, Gt(x, 2)), (2 - x, True)) - 3 f = Piecewise(((x - 2) ** 2, x >= 0), (0, True)) assert set(solveset_real(eq, x)) == set(FiniteSet(-1, 5)) absxm3 = Piecewise((x - 3, S(0) <= x - 3), (3 - x, S(0) > x - 3)) y = Symbol("y", positive=True) assert solveset_real(absxm3 - y, x) == FiniteSet(-y + 3, y + 3) assert solveset(f, x, domain=S.Reals) == Union(FiniteSet(2), Interval(-oo, 0, True, True))
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_piecewise(): eq = Piecewise((x - 2, Gt(x, 2)), (2 - x, True)) - 3 assert set(solveset_real(eq, x)) == set(FiniteSet(-1, 5)) absxm3 = Piecewise( (x - 3, S(0) <= x - 3), (3 - x, S(0) > x - 3) ) y = Symbol('y', positive=True) assert solveset_real(absxm3 - y, x) == FiniteSet(-y + 3, y + 3)
def test_solve_polynomial_cv_1a(): """ Test for solving on equations that can be converted to a polynomial equation using the change of variable y -> x**Rational(p, q) """ assert solveset_real(sqrt(x) - 1, x) == FiniteSet(1) assert solveset_real(sqrt(x) - 2, x) == FiniteSet(4) assert solveset_real(x ** Rational(1, 4) - 2, x) == FiniteSet(16) assert solveset_real(x ** Rational(1, 3) - 3, x) == FiniteSet(27) assert solveset_real(x * (x ** (S(1) / 3) - 3), x) == FiniteSet(S(0), S(27))
def test_piecewise(): eq = Piecewise((x - 2, Gt(x, 2)), (2 - x, True)) - 3 assert set(solveset_real(eq, x)) == set(FiniteSet(-1, 5)) absxm3 = Piecewise( (x - 3, S(0) <= x - 3), (3 - x, S(0) > x - 3)) y = Symbol('y', positive=True) assert solveset_real(absxm3 - y, x) == FiniteSet(-y + 3, y + 3) f = Piecewise(((x - 2)**2, x >= 0), (0, True)) assert solveset(f, x, domain=S.Reals) == Union(FiniteSet(2), Interval(-oo, 0, True, True)) assert solveset(Piecewise((x + 1, x > 0), (I, True)) - I, x) == \ Interval(-oo, 0)
def test_solve_polynomial(): assert solveset_real(3 * x - 2, x) == FiniteSet(Rational(2, 3)) assert solveset_real(x ** 2 - 1, x) == FiniteSet(-S(1), S(1)) assert solveset_real(x - y ** 3, x) == FiniteSet(y ** 3) a11, a12, a21, a22, b1, b2 = symbols("a11, a12, a21, a22, b1, b2") assert solveset_real(x ** 3 - 15 * x - 4, x) == FiniteSet(-2 + 3 ** Rational(1, 2), S(4), -2 - 3 ** Rational(1, 2)) assert solveset_real(sqrt(x) - 1, x) == FiniteSet(1) assert solveset_real(sqrt(x) - 2, x) == FiniteSet(4) assert solveset_real(x ** Rational(1, 4) - 2, x) == FiniteSet(16) assert solveset_real(x ** Rational(1, 3) - 3, x) == FiniteSet(27) assert len(solveset_real(x ** 5 + x ** 3 + 1, x)) == 1 assert len(solveset_real(-2 * x ** 3 + 4 * x ** 2 - 2 * x + 6, x)) > 0
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 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 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) unsolved_object = ConditionSet( q, Eq((-2 * sqrt(4 * q**2 * (m - q)**2 + (-m + q)**2) + sqrt( (-2 * m**2 - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) - 1)**2 + (2 * m**2 - 4 * m - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) - 1)**2) * Abs(q)) / Abs(q), 0), S.Reals) assert solveset_real(eq, q) == unsolved_object
def test_rewrite_trigh(): # if this import passes then the test below should also pass from sympy import sech assert solveset_real(sinh(x) + sech(x), x) == FiniteSet( 2 * atanh(-S.Half + sqrt(5) / 2 - sqrt(-2 * sqrt(5) + 2) / 2), 2 * atanh(-S.Half + sqrt(5) / 2 + sqrt(-2 * sqrt(5) + 2) / 2), 2 * atanh(-sqrt(5) / 2 - S.Half + sqrt(2 + 2 * sqrt(5)) / 2), 2 * atanh(-sqrt(2 + 2 * sqrt(5)) / 2 - sqrt(5) / 2 - S.Half))
def test_rewrite_trigh(): # if this import passes then the test below should also pass from sympy import sech assert solveset_real(sinh(x) + sech(x), x) == FiniteSet( 2*atanh(-S.Half + sqrt(5)/2 - sqrt(-2*sqrt(5) + 2)/2), 2*atanh(-S.Half + sqrt(5)/2 + sqrt(-2*sqrt(5) + 2)/2), 2*atanh(-sqrt(5)/2 - S.Half + sqrt(2 + 2*sqrt(5))/2), 2*atanh(-sqrt(2 + 2*sqrt(5))/2 - sqrt(5)/2 - S.Half))
def codomain_interval(f, set_val, *sym): symb = sym[0] df1 = diff(f, symb) df2 = diff(df1, symb) der_zero = solveset_real(df1, symb) der_zero_in_dom = closure_handle(set_val, der_zero) local_maxima = set() local_minima = set() start_val = limit(f, symb, set_val.start) end_val = limit(f, symb, set_val.end, '-') if start_val is S.Infinity or end_val is S.Infinity: local_maxima = set([(oo, True)]) elif start_val is S.NegativeInfinity or end_val is S.NegativeInfinity: local_minima = set([(-oo, True)]) if local_maxima == set(): if start_val > end_val: local_maxima = set([(start_val, set_val.left_open)]) elif start_val < end_val: local_maxima = set([(end_val, set_val.right_open)]) else: local_maxima = set([(start_val, set_val.left_open and set_val.right_open)]) if local_minima == set(): if start_val < end_val: local_minima = set([(start_val, set_val.left_open)]) elif start_val > end_val: local_minima = set([(end_val, set_val.right_open)]) else: local_minima = set([(start_val, set_val.left_open and set_val.right_open)]) for i in der_zero_in_dom: exist = not i in set_val if df2.subs({symb: i}) < 0: local_maxima.add((f.subs({symb: i}), exist)) elif df2.subs({symb: i}) > 0: local_minima.add((f.subs({symb: i}), exist)) maximum = (-oo, True) minimum = (oo, True) for i in local_maxima: if i[0] > maximum[0]: maximum = i elif i[0] == maximum[0]: maximum = (maximum[0], i[1] and maximum[1]) for i in local_minima: if i[0] < minimum[0]: minimum = i elif i[0] == minimum[0]: minimum = (minimum[0], i[1] and minimum[1]) return Union(Interval(minimum[0], maximum[0], minimum[1], maximum[1]))
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 test_solve_abs(): assert solveset_real(Abs(x) - 2, x) == FiniteSet(-2, 2) assert solveset_real(Abs(x + 3) - 2 * Abs(x - 3), x) == FiniteSet(1, 9) assert solveset_real(2 * Abs(x) - Abs(x - 1), x) == FiniteSet(-1, Rational(1, 3)) assert solveset_real(Abs(x - 7) - 8, x) == FiniteSet(-S(1), S(15)) # issue 9565. Note: solveset_real does not solve this as it is # solveset's job to handle Relationals assert solveset(Abs((x - 1) / (x - 5)) <= S(1) / 3, domain=S.Reals) == Interval(-1, 2) # issue #10069 eq = abs(1 / (x - 1)) - 1 > 0 u = Union(Interval.open(0, 1), Interval.open(1, 2)) assert solveset_real(eq, x) == u assert solveset(eq, x, domain=S.Reals) == u raises(ValueError, lambda: solveset(abs(x) - 1, x))
def test_solve_polynomial(): assert solveset_real(3 * x - 2, x) == FiniteSet(Rational(2, 3)) assert solveset_real(x**2 - 1, x) == FiniteSet(-S(1), S(1)) assert solveset_real(x - y**3, x) == FiniteSet(y**3) a11, a12, a21, a22, b1, b2 = symbols('a11, a12, a21, a22, b1, b2') assert solveset_real(x**3 - 15 * x - 4, x) == FiniteSet(-2 + 3**Rational(1, 2), S(4), -2 - 3**Rational(1, 2)) assert solveset_real(sqrt(x) - 1, x) == FiniteSet(1) assert solveset_real(sqrt(x) - 2, x) == FiniteSet(4) assert solveset_real(x**Rational(1, 4) - 2, x) == FiniteSet(16) assert solveset_real(x**Rational(1, 3) - 3, x) == FiniteSet(27) assert len(solveset_real(x**5 + x**3 + 1, x)) == 1 assert len(solveset_real(-2 * x**3 + 4 * x**2 - 2 * x + 6, x)) > 0
def test_solve_abs(): assert solveset_real(Abs(x) - 2, x) == FiniteSet(-2, 2) assert solveset_real(Abs(x + 3) - 2*Abs(x - 3), x) == \ FiniteSet(1, 9) assert solveset_real(2*Abs(x) - Abs(x - 1), x) == \ FiniteSet(-1, Rational(1, 3)) assert solveset_real(Abs(x - 7) - 8, x) == FiniteSet(-S(1), S(15)) # issue 9565. Note: solveset_real does not solve this as it is # solveset's job to handle Relationals assert solveset(Abs((x - 1)/(x - 5)) <= S(1)/3, domain=S.Reals ) == Interval(-1, 2) # issue #10069 assert solveset_real(abs(1/(x - 1)) - 1 > 0, x) == \ ConditionSet(x, Eq((1 - Abs(x - 1))/Abs(x - 1) > 0, 0), S.Reals) assert solveset(abs(1/(x - 1)) - 1 > 0, x, domain=S.Reals ) == Union(Interval.open(0, 1), Interval.open(1, 2))
def getTmOptimal2(v0, vt, p0, pt, delay=0.0): t = symbols('t') a = (12.0 * (t * (p0 - pt + t*v0) - 0.5*(t**2)*(v0 - vt)))/(t**4) b = -(4 * (1.5*(p0 - pt) + t*v0 + 0.5*t*vt))/(t**2) j = ((a*t + b)**3 - b**3) / a djdt = diff(j, t) try: res = solveset_real(djdt, t) except Exception as e: res = solve(djdt, t) print("Calculating tm for v0={}, vt={}, p0={}, pt={}, tm={}".format(v0, vt, p0, pt, float(min(res)))) return float(min(res)) + delay
def test_solve_abs(): assert solveset_real(Abs(x) - 2, x) == FiniteSet(-2, 2) assert solveset_real(Abs(x + 3) - 2*Abs(x - 3), x) == \ FiniteSet(1, 9) assert solveset_real(2*Abs(x) - Abs(x - 1), x) == \ FiniteSet(-1, Rational(1, 3)) assert solveset_real(Abs(x - 7) - 8, x) == FiniteSet(-S(1), S(15)) # issue 9565. Note: solveset_real does not solve this as it is # solveset's job to handle Relationals assert solveset(Abs((x - 1) / (x - 5)) <= S(1) / 3, domain=S.Reals) == Interval(-1, 2) # issue #10069 eq = abs(1 / (x - 1)) - 1 > 0 u = Union(Interval.open(0, 1), Interval.open(1, 2)) assert solveset_real(eq, x) == u assert solveset(eq, x, domain=S.Reals) == u raises(ValueError, lambda: solveset(abs(x) - 1, x))
def test_no_sol(): assert solveset_real(4, x) == EmptySet() assert solveset_real(exp(x), x) == EmptySet() assert solveset_real(x ** 2 + 1, x) == EmptySet() assert solveset_real(-3 * a / sqrt(x), x) == EmptySet() assert solveset_real(1 / x, x) == EmptySet() assert solveset_real(-(1 + x) / (2 + x) ** 2 + 1 / (2 + x), x) == EmptySet()
def test_solveset_sqrt_1(): assert solveset_real(sqrt(5 * x + 6) - 2 - x, x) == FiniteSet(-S(1), S(2)) assert solveset_real(sqrt(x - 1) - x + 7, x) == FiniteSet(10) assert solveset_real(sqrt(x - 2) - 5, x) == FiniteSet(27) assert solveset_real(sqrt(x) - 2 - 5, x) == FiniteSet(49) assert solveset_real(sqrt(x ** 3), x) == FiniteSet(0) assert solveset_real(sqrt(x - 1), x) == FiniteSet(1)
def test_no_sol(): assert solveset_real(4, x) == EmptySet() assert solveset_real(exp(x), x) == EmptySet() assert solveset_real(x**2 + 1, x) == EmptySet() assert solveset_real(-3 * a / sqrt(x), x) == EmptySet() assert solveset_real(1 / x, x) == EmptySet() assert solveset_real(-(1 + x)/(2 + x)**2 + 1/(2 + x), x) == \ EmptySet()
def test_solveset_sqrt_1(): assert solveset_real(sqrt(5*x + 6) - 2 - x, x) == \ FiniteSet(-S(1), S(2)) assert solveset_real(sqrt(x - 1) - x + 7, x) == FiniteSet(10) assert solveset_real(sqrt(x - 2) - 5, x) == FiniteSet(27) assert solveset_real(sqrt(x) - 2 - 5, x) == FiniteSet(49) assert solveset_real(sqrt(x**3), x) == FiniteSet(0) assert solveset_real(sqrt(x - 1), x) == FiniteSet(1)
def _intersect(self, other): from sympy.solvers.diophantine import diophantine if self.base_set is S.Integers: g = None if isinstance(other, ImageSet) and other.base_set is S.Integers: g = other.lamda.expr m = other.lamda.variables[0] elif other is S.Integers: m = g = Dummy('x') if g is not None: f = self.lamda.expr n = self.lamda.variables[0] # Diophantine sorts the solutions according to the alphabetic # order of the variable names, since the result should not depend # on the variable name, they are replaced by the dummy variables # below a, b = Dummy('a'), Dummy('b') f, g = f.subs(n, a), g.subs(m, b) solns_set = diophantine(f - g) if solns_set == set(): return EmptySet() solns = list(diophantine(f - g)) if len(solns) != 1: return # since 'a' < 'b', select soln for n nsol = solns[0][0] t = nsol.free_symbols.pop() return imageset(Lambda(n, f.subs(a, nsol.subs(t, n))), S.Integers) if other == S.Reals: from sympy.solvers.solveset import solveset_real from sympy.core.function import expand_complex if len(self.lamda.variables) > 1: return None f = self.lamda.expr n = self.lamda.variables[0] n_ = Dummy(n.name, real=True) f_ = f.subs(n, n_) re, im = f_.as_real_imag() im = expand_complex(im) return imageset(Lambda(n_, re), self.base_set.intersect( solveset_real(im, n_)))
def _intersect(self, other): from sympy.solvers.diophantine import diophantine if self.base_set is S.Integers: g = None if isinstance(other, ImageSet) and other.base_set is S.Integers: g = other.lamda.expr m = other.lamda.variables[0] elif other is S.Integers: m = g = Dummy('x') if g is not None: f = self.lamda.expr n = self.lamda.variables[0] # Diophantine sorts the solutions according to the alphabetic # order of the variable names, since the result should not depend # on the variable name, they are replaced by the dummy variables # below a, b = Dummy('a'), Dummy('b') f, g = f.subs(n, a), g.subs(m, b) solns_set = diophantine(f - g) if solns_set == set(): return EmptySet() solns = list(diophantine(f - g)) if len(solns) != 1: return # since 'a' < 'b', select soln for n nsol = solns[0][0] t = nsol.free_symbols.pop() return imageset(Lambda(n, f.subs(a, nsol.subs(t, n))), S.Integers) if other == S.Reals: from sympy.solvers.solveset import solveset_real from sympy.core.function import expand_complex if len(self.lamda.variables) > 1: return None f = self.lamda.expr n = self.lamda.variables[0] n_ = Dummy(n.name, real=True) f_ = f.subs(n, n_) re, im = f_.as_real_imag() im = expand_complex(im) return imageset(Lambda(n_, re), self.base_set.intersect(solveset_real(im, n_)))
def test_solve_invert(): assert solveset_real(exp(x) - 3, x) == FiniteSet(log(3)) assert solveset_real(log(x) - 3, x) == FiniteSet(exp(3)) assert solveset_real(3**(x + 2), x) == FiniteSet() assert solveset_real(3**(2 - x), x) == FiniteSet() assert solveset_real(y - b*exp(a/x), x) == Intersection(S.Reals, FiniteSet(a/log(y/b))) # issue 4504 assert solveset_real(2**x - 10, x) == FiniteSet(log(10)/log(2))
def test_solve_invert(): assert solveset_real(exp(x) - 3, x) == FiniteSet(log(3)) assert solveset_real(log(x) - 3, x) == FiniteSet(exp(3)) assert solveset_real(3 ** (x + 2), x) == FiniteSet() assert solveset_real(3 ** (2 - x), x) == FiniteSet() assert solveset_real(y - b * exp(a / x), x) == Intersection(S.Reals, FiniteSet(a / log(y / b))) # issue 4504 assert solveset_real(2 ** x - 10, x) == FiniteSet(log(10) / log(2))
def test_solve_invert(): assert solveset_real(exp(x) - 3, x) == FiniteSet(log(3)) assert solveset_real(log(x) - 3, x) == FiniteSet(exp(3)) assert solveset_real(3**(x + 2), x) == FiniteSet() assert solveset_real(3**(2 - x), x) == FiniteSet() b = Symbol('b', positive=True) y = Symbol('y', positive=True) assert solveset_real(y - b*exp(a/x), x) == FiniteSet(a/log(y/b)) # issue 4504 assert solveset_real(2**x - 10, x) == FiniteSet(log(10)/log(2))
def test_solve_invert(): assert solveset_real(exp(x) - 3, x) == FiniteSet(log(3)) assert solveset_real(log(x) - 3, x) == FiniteSet(exp(3)) assert solveset_real(3**(x + 2), x) == FiniteSet() assert solveset_real(3**(2 - x), x) == FiniteSet() b = Symbol('b', positive=True) y = Symbol('y', positive=True) assert solveset_real(y - b * exp(a / x), x) == FiniteSet(a / log(y / b)) # issue 4504 assert solveset_real(2**x - 10, x) == FiniteSet(log(10) / log(2))
def _intersect(self, other): from sympy import Dummy from sympy.solvers.diophantine import diophantine from sympy.sets.sets import imageset if self.base_set is S.Integers: if isinstance(other, ImageSet) and other.base_set is S.Integers: f, g = self.lamda.expr, other.lamda.expr n, m = self.lamda.variables[0], other.lamda.variables[0] # Diophantine sorts the solutions according to the alphabetic # order of the variable names, since the result should not depend # on the variable name, they are replaced by the dummy variables # below a, b = Dummy("a"), Dummy("b") f, g = f.subs(n, a), g.subs(m, b) solns_set = diophantine(f - g) if solns_set == set(): return EmptySet() solns = list(diophantine(f - g)) if len(solns) == 1: t = list(solns[0][0].free_symbols)[0] else: return None # since 'a' < 'b' return imageset(Lambda(t, f.subs(a, solns[0][0])), S.Integers) if other == S.Reals: from sympy.solvers.solveset import solveset_real from sympy.core.function import expand_complex if len(self.lamda.variables) > 1: return None f = self.lamda.expr n = self.lamda.variables[0] n_ = Dummy(n.name, real=True) f_ = f.subs(n, n_) re, im = f_.as_real_imag() im = expand_complex(im) return imageset(Lambda(n_, re), self.base_set.intersect(solveset_real(im, n_)))
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 ) unsolved_object = ConditionSet( q, Eq( ( -2 * sqrt(4 * q ** 2 * (m - q) ** 2 + (-m + q) ** 2) + sqrt( (-2 * m ** 2 - sqrt(4 * m ** 4 - 4 * m ** 2 + 8 * m + 1) - 1) ** 2 + (2 * m ** 2 - 4 * m - sqrt(4 * m ** 4 - 4 * m ** 2 + 8 * m + 1) - 1) ** 2 ) * Abs(q) ) / Abs(q), 0, ), S.Reals, ) assert solveset_real(eq, q) == unsolved_object
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 + 40/(3*(251 + 3*sqrt(111)*I)**(S(1)/3)) + (251 + 3*sqrt(111)*I)**(S(1)/3)/3,), ((-160 + (1 + sqrt(3)*I)*(10 - (1 + sqrt(3)*I)*(251 + 3*sqrt(111)*I)**(S(1)/3))*(251 + 3*sqrt(111)*I)**(S(1)/3))/Mul(6, (1 + sqrt(3)*I), (251 + 3*sqrt(111)*I)**(S(1)/3), evaluate=False),)]) 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) assert solveset_real(eq, q) == FiniteSet( m**2/2 - sqrt(4*m**4 - 4*m**2 + 8*m + 1)/4 - S(1)/4, m**2/2 + sqrt(4*m**4 - 4*m**2 + 8*m + 1)/4 - S(1)/4)
def solve_beguenstigungsbetrag(gewinn, hebesatz): """ Berechnung des Thesaurierungshöchstbetrags :param gewinn: gewinn = zu versteuerndes Einkommen :param hebesatz: Hebesatz der Gemeinden gem. § 16 GewStG (in Prozent: Bsp. 480) :return: Thesaurierungshöchstbetrag """ eq1 = Eq( B, gewinn - calc_gewst(gewinn, hebesatz, 1) - calc_est(gewinn - B) + calc_anrechnung_gewst(gewinn, hebesatz) - calc_est34a(B) - calc_solz( calc_est(gewinn - B) # BMG für SolZ - calc_anrechnung_gewst(gewinn, hebesatz) # BMG für SolZ + calc_est34a(B), # BMG für SolZ einkommensteuerpflichtig=1)) sol = solveset_real( eq1, B).args[0] # Das erste Argument der Lösung wird ausgegeben return sol
def trust_region_step(g, B, delta): # Calculates the trust region lamb = sp.Symbol('lamb') eigh = np.linalg.eig(B) # B is positive definite if eigh[0][0] > 0 and eigh[0][1] > 0: pMin = -np.linalg.solve(B, g) pMin_norm = np.linalg.norm(pMin, 2) if pMin_norm <= delta: return pMin # B is not positive definite q1T = np.transpose(eigh[1][:, 0]) q2T = np.transpose(eigh[1][:, 1]) # Setting up the equation pLambda = ((np.dot(q1T, g) ** 2) / ((eigh[0][0] + lamb) ** 2)) \ + ((np.dot(q2T, g) ** 2) / ((eigh[0][1] + lamb) ** 2)) \ - delta ** 2 # Symbolically solving lamb_solved = solveset_real(pLambda[0], lamb) lamb_solved = list(lamb_solved) # Getting the lambda for i in range(len(lamb_solved)): # Lambda needs to be strictly positive and greater than negative of smallest eig if lamb_solved[i] > 0 and lamb_solved[i] > -min(eigh[0]): lamb_pos = lamb_solved[i] BlambI = B + lamb_pos * np.identity(2) BlambI = BlambI.astype(np.float) pMin = np.linalg.solve(BlambI, -g) return pMin
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 + 40 / (3 * (251 + 3 * sqrt(111) * I)**(S(1) / 3)) + (251 + 3 * sqrt(111) * I)**(S(1) / 3) / 3, ), ((-160 + (1 + sqrt(3) * I) * (10 - (1 + sqrt(3) * I) * (251 + 3 * sqrt(111) * I)**(S(1) / 3)) * (251 + 3 * sqrt(111) * I)**(S(1) / 3)) / Mul(6, (1 + sqrt(3) * I), (251 + 3 * sqrt(111) * I)**(S(1) / 3), evaluate=False), )]) 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) assert solveset_real(eq, q) == FiniteSet( m**2 / 2 - sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) / 4 - S(1) / 4, m**2 / 2 + sqrt(4 * m**4 - 4 * m**2 + 8 * m + 1) / 4 - S(1) / 4)
def test_solve_only_exp_1(): y = Symbol('y', positive=True, finite=True) assert solveset_real(exp(x) - y, x) == FiniteSet(log(y)) assert solveset_real(exp(x) + exp(-x) - 4, x) == \ FiniteSet(log(-sqrt(3) + 2), log(sqrt(3) + 2)) assert solveset_real(exp(x) + exp(-x) - y, x) != S.EmptySet
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 test_solve_invalid_sol(): assert 0 not in solveset_real(sin(x) / x, x) assert 0 not in solveset_complex((exp(x) - 1) / x, x)
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 test_solve_only_exp_2(): assert solveset_real(exp(x/y)*exp(-z/y) - 2, y) == \ FiniteSet((x - z)/log(2)) assert solveset_real(sqrt(exp(x)) + sqrt(exp(-x)) - 4, x) == \ FiniteSet(2*log(-sqrt(3) + 2), 2*log(sqrt(3) + 2))
def test_atan2(): # The .inverse() method on atan2 works only if x.is_real is True and the # second argument is a real constant assert solveset_real(atan2(x, 2) - pi / 3, x) == FiniteSet(2 * sqrt(3))
def test_units(): assert solveset_real(1 / x - 1 / (2 * cm), x) == FiniteSet(2 * cm)
def test_poly_gens(): assert solveset_real(4**(2*(x**2) + 2*x) - 8, x) == \ FiniteSet(-Rational(3, 2), S.Half)
def test_solveset_real_log(): assert solveset_real(log((x-1)*(x+1)), x) == \ FiniteSet(sqrt(2), -sqrt(2))
def test_atan2(): # The .inverse() method on atan2 works only if x.is_real is True and the # second argument is a real constant assert solveset_real(atan2(x, 2) - pi/3, x) == FiniteSet(2*sqrt(3))
def test_solveset_real_rational(): """Test solveset_real for rational functions""" assert solveset_real((x - y**3) / ((y**2)*sqrt(1 - y**2)), x) \ == FiniteSet(y**3) # issue 4486 assert solveset_real(2 * x / (x + 2) - 1, x) == FiniteSet(2)
def test_solve_invalid_sol(): assert 0 not in solveset_real(sin(x)/x, x) assert 0 not in solveset_complex((exp(x) - 1)/x, x)
def test_uselogcombine_2(): eq = z - log(x) + log(y / (x * (-1 + y**2 / x**2))) assert solveset_real(eq, x) == \ FiniteSet(-sqrt(y*(y - exp(z))), sqrt(y*(y - exp(z))))
def intersection_sets(self, other): # noqa:F811 from sympy.solvers.diophantine import diophantine # Only handle the straight-forward univariate case if (len(self.lamda.variables) > 1 or self.lamda.signature != self.lamda.variables): return None base_set = self.base_sets[0] # Intersection between ImageSets with Integers as base set # For {f(n) : n in Integers} & {g(m) : m in Integers} we solve the # diophantine equations f(n)=g(m). # If the solutions for n are {h(t) : t in Integers} then we return # {f(h(t)) : t in integers}. # If the solutions for n are {n_1, n_2, ..., n_k} then we return # {f(n_i) : 1 <= i <= k}. if base_set is S.Integers: gm = None if isinstance(other, ImageSet) and other.base_sets == (S.Integers, ): gm = other.lamda.expr var = other.lamda.variables[0] # Symbol of second ImageSet lambda must be distinct from first m = Dummy('m') gm = gm.subs(var, m) elif other is S.Integers: m = gm = Dummy('m') if gm is not None: fn = self.lamda.expr n = self.lamda.variables[0] try: solns = list(diophantine(fn - gm, syms=(n, m), permute=True)) except (TypeError, NotImplementedError): # TypeError if equation not polynomial with rational coeff. # NotImplementedError if correct format but no solver. return # 3 cases are possible for solns: # - empty set, # - one or more parametric (infinite) solutions, # - a finite number of (non-parametric) solution couples. # Among those, there is one type of solution set that is # not helpful here: multiple parametric solutions. if len(solns) == 0: return EmptySet elif any(not isinstance(s, int) and s.free_symbols for tupl in solns for s in tupl): if len(solns) == 1: soln, solm = solns[0] (t, ) = soln.free_symbols expr = fn.subs(n, soln.subs(t, n)).expand() return imageset(Lambda(n, expr), S.Integers) else: return else: return FiniteSet(*(fn.subs(n, s[0]) for s in solns)) if other == S.Reals: from sympy.solvers.solveset import solveset_real from sympy.core.function import expand_complex f = self.lamda.expr n = self.lamda.variables[0] n_ = Dummy(n.name, real=True) f_ = f.subs(n, n_) re, im = f_.as_real_imag() im = expand_complex(im) re = re.subs(n_, n) im = im.subs(n_, n) ifree = im.free_symbols lam = Lambda(n, re) if not im: # allow re-evaluation # of self in this case to make # the result canonical pass elif im.is_zero is False: return S.EmptySet elif ifree != {n}: return None else: # univarite imaginary part in same variable base_set = base_set.intersect(solveset_real(im, n)) return imageset(lam, base_set) elif isinstance(other, Interval): from sympy.solvers.solveset import (invert_real, invert_complex, solveset) f = self.lamda.expr n = self.lamda.variables[0] new_inf, new_sup = None, None new_lopen, new_ropen = other.left_open, other.right_open if f.is_real: inverter = invert_real else: inverter = invert_complex g1, h1 = inverter(f, other.inf, n) g2, h2 = inverter(f, other.sup, n) if all(isinstance(i, FiniteSet) for i in (h1, h2)): if g1 == n: if len(h1) == 1: new_inf = h1.args[0] if g2 == n: if len(h2) == 1: new_sup = h2.args[0] # TODO: Design a technique to handle multiple-inverse # functions # Any of the new boundary values cannot be determined if any(i is None for i in (new_sup, new_inf)): return range_set = S.EmptySet if all(i.is_real for i in (new_sup, new_inf)): # this assumes continuity of underlying function # however fixes the case when it is decreasing if new_inf > new_sup: new_inf, new_sup = new_sup, new_inf new_interval = Interval(new_inf, new_sup, new_lopen, new_ropen) range_set = base_set.intersect(new_interval) else: if other.is_subset(S.Reals): solutions = solveset(f, n, S.Reals) if not isinstance(range_set, (ImageSet, ConditionSet)): range_set = solutions.intersect(other) else: return if range_set is S.EmptySet: return S.EmptySet elif isinstance(range_set, Range) and range_set.size is not S.Infinity: range_set = FiniteSet(*list(range_set)) if range_set is not None: return imageset(Lambda(n, f), range_set) return else: return
def test_real_imag_splitting(): a, b = symbols('a b', real=True, finite=True) assert solveset_real(sqrt(a**2 - b**2) - 3, a) == \ FiniteSet(-sqrt(b**2 + 9), sqrt(b**2 + 9)) assert solveset_real(sqrt(a**2 + b**2) - 3, a) != \ S.EmptySet