SymPy: Limit symbol/variable to interval
Asked Answered
I

4

20

Using SymPy, is it possible to limit the possible values of a symbol/variable to a certain range? I now I can set some properties while defining symbols, like positive=True, but I need more control, i.e. I need to set it to be in the interval [0,1]. This assumption should then be used for solving, simplifying etc.

Immunology answered 23/10, 2013 at 22:40 Comment(0)
P
11

You can specify the bounds as inequalities such as x >= lb and x <= ub, for example:

from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
solve([x >= 0.5, x <= 3, x**2 - 1], x)

Here we search for a solution of equation x**2 == 1 such that x is in the interval [0.5, 3].

Perfecto answered 24/10, 2013 at 15:40 Comment(1)
Thanks, that works. However, I need the same for multiple variables at the same time, which raises NotImplementedError: only univariate inequalities are supported. Too bad...Immunology
P
7

As for simplification, you want refine. Unfortunately, it doesn't yet support using inequality syntax, so you'll have to use Q.positive or Q.negative (or Q.nonpositive or Q.nonnegative for non-strict inequalities). The most common simplification that it handles is sqrt(x**2) = x if x >= 0.

>>> refine(sqrt((x - 1)**2), Q.positive(x - 1))
x - 1
>>> refine(sqrt((x - 1)**2), Q.positive(x))
Abs(x - 1)

Note in the second case you still get a simpler answer because it at least knows that x - 1 is real under the given assumptions.

If your assumptions are as simple as "x is positive" or "x is negative", the best chance for success is to define it on the Symbol itself, like

>>> Symbol('x', positive=True)
>>> sqrt(x**2)
x
Patterman answered 25/10, 2013 at 1:9 Comment(0)
E
5

Now you can use solveset

In [3]: solveset(x**2 - 1, x, Interval(0.5, 3)) Out[3]: {1}

Edaphic answered 17/7, 2016 at 13:24 Comment(2)
Add some explanation with answer for how this answer help OP in fixing current issueAlbertype
Does that work for multiple variables and inequalities as well?Immunology
R
2

In sympy 1.1.1 I found that this works. It can be easily adapted to your case.

>>> x = Symbol('x', domain=S.Reals)
>>> solve_domain = And(0 <= x, x < 2*pi).as_set()
>>> solve_domain
[0, 2⋅π)
# solve_domain must be evaluated before solveset call
>>> solveset(sin(x), x, solve_domain)
{0, π}

I don't know why things fall apart if you don't evaluate solve_domain before calling solveset, but it's easy to work around once you know.

Registered answered 25/1, 2018 at 22:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.