# Solving inequations

is there a package to solve inequations in Julia?
2x + 3 <= x + 7

Thanks

1 Like

I’m not sure if there is a package for it. You can do it with Symbolics, though.

``````julia> using Symbolics

julia> function solve_inequality(g, x)
f = ~(g.val.arguments...)
xeq = Symbolics.solve_for(f, x)
if substitute(g, x=>xeq+1).val
return g.val.f(xeq, x)
else
return g.val.f(x, xeq)
end
end

julia> @variables x
(x,)

julia> g = 2x + 3 <= x + 7
3 + 2x <= 7 + x

julia> solve_inequality(g, x)
x <= 4.0
``````

Note: You’ll probably want to put some kind of check to make sure it even is an inequality if you want to be serious about this. This is just a quick and dirty solution.

6 Likes

Thanks for the solution. I am a matlab user and its more easy to solve this kind of problem with matlab. I thought that would be some package to do it in a more clean way in julia, guess not. Thanks for solution tho.
xD

One of the other symbolics packages might have this already implemented, I’m just not familiar with most of them. SymPy.jl might be a good place to check.

1 Like

It looks like this is how you’d do it in SymPy:

``````julia> using SymPy

julia> @syms x
(x,)

julia> g = LessThan(2x + 3, x + 7)
2⋅x + 3 ≤ x + 7

julia> solve(g, x)
x ≤ 4 ∧ -∞ < x
``````
7 Likes

wow. Now its much better. Thank for all you help Jonniedie. I am already reading SymPy documentation. Thanks mate, you made my day.

1 Like

I think you can also do “easy” inequalities with `JuMP.jl`:

``````using JuMP

model = Model()

@variable(model, x)
@constraint(model, 2x + 3 <= x + 7)
# x ≤ 4.0
``````
3 Likes

this is also a good answer. Thanks man.

1 Like

@YingboMa
Is this something that can be done easily in Symbolics at this point, or is it on the roadmap?