# Plotting sets using natural mathematical set expressions

**URL:** <https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582>\
**Category:** Optimization (Mathematical)\
**Tags:** package, plotting, polyhedra\
**Created:** [December 13, 2022, 1:43am UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582 "2022-12-13T01:43:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tapyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tapyu/32/37182_2.png) [@tapyu](https://discourse.julialang.org/u/tapyu)\
**Post date:** [December 13, 2022, 1:43am UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582/1 "2022-12-13T01:43:24Z")

</div>

In convex optimization, it is quite common to come across a n-dimensional set formally written like that, for example,

C = \left\{ (x\_1, x\_2, \cdots, x\_n, t) \in \mathbb{R}^{n+1} \mid \mathbf{x} \in \mathbb{R}^{n}, \lVert\mathbf{x}\rVert\_{p} \leq t \right\} \subseteq \mathbb{R}^{n+1}

It would be amazing to have a Julia package that we give the set as it is written mathematically and get plot of the set (in a 3D space, at most, of course…). The output woud be something like that (this is not the resulting plot of the previous set, btw)

![image](https://global.discourse-cdn.com/julialang/original/3X/f/8/f8142c6cf43a54a7a78872279c87c64c42cfd9f8.jpeg)

Is there a package that fill this gap? If the answer is no, what is the most convenient way to get the plot of such set in Julia?

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [December 13, 2022, 2:38am UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582/2 "2022-12-13T02:38:08Z")

</div>

You may be interested in the combination of [Polyhedra.jl](https://github.com/JuliaPolyhedra/Polyhedra.jl)

- [Optimization · Polyhedra](https://juliapolyhedra.github.io/Polyhedra.jl/stable/optimization/#Creating-a-polyhedron-from-the-feasible-set-of-a-JuMP-model)
- [Plot · Polyhedra](https://juliapolyhedra.github.io/Polyhedra.jl/stable/plot/)

with Makie or [MeshCat.jl](https://github.com/rdeits/MeshCat.jl#polyhedra).

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 13, 2022, 3:04am UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582/3 "2022-12-13T03:04:14Z")

</div>

That’s a nice idea. Maybe aim for software that would give some kind of visual representation for some subset of mathematical definitions. It would be so valuable for students. It must be doable at some level (and probably errors) with current AI.

---

<div class="post-metadata">

**Author:** ![tapyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tapyu/32/37182_2.png) [@tapyu](https://discourse.julialang.org/u/tapyu)\
**Post date:** [December 13, 2022, 1:05pm UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582/4 "2022-12-13T13:05:23Z")

</div>

Thanks to @jd-foster , I could gather required packages to plot sets. It is not that hard, but the user must be acquaint with different Julia’s packages, which makes it more confusing for beginners… I started off with a simpler set:

\left\{\mathbf{x} \in \mathbb{R}^3 \mid \mathbf{0} \preceq \mathbf{x} \preceq \mathbf{1} \right\}

My Julia code is

```julia
import JuMP, CDDLib, Polyhedra, GLMakie, Makie

n = 3
m = JuMP.Model()
JuMP.@variable(m, 0 ≤ x[1:n] ≤ 1) # put your mathematical set here!
poly = Polyhedra.polyhedron(m, CDDLib.Library(:exact))
m = Polyhedra.Mesh(poly)
Makie.mesh(m, color=:blue)

```

It yields this beautiful (but simple) 3D interactive plot  
 ![cube](https://global.discourse-cdn.com/julialang/original/3X/4/2/42ee1581c41b846323236dab22badad2a768f78b.gif)

I am not sure how these packages relate each other, but as far as I noticed:

- GLMakie is the render backend of Makie
- Polyhedra is responsible for creating polyhedra on Julia
- JuMP seems to be a optimization package, which is weird as we are not optimizing anything here… Furthermore, it seems to have an overlap with the [`Convex.jl`](https://jump.dev/Convex.jl/stable/), which I personally prefer…

I have no idea what the purpose of CDDLib is…

---

<div class="post-metadata">

**Author:** ![tapyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tapyu/32/37182_2.png) [@tapyu](https://discourse.julialang.org/u/tapyu)\
**Post date:** [December 13, 2022, 1:59pm UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582/5 "2022-12-13T13:59:59Z")

</div>

However, for a minimally more complicated set, e.g.,

\left\{ (x,y) \in \mathbb{R}\_{++}^{2} \mid x/y \leq 1 \right\}

Julia code fails in plotting it trivially…

```julia
import JuMP, CDDLib, Polyhedra, Plots
m = JuMP.Model()
JuMP.@variable(m, 0 ≤ x[1:2]) # it is not possible to define opened intervals
JuMP.@constraint(m, x[1] ≤ x[2])
poly = Polyhedra.polyhedron(m, CDDLib.Library(:exact))
Plots.plot(poly)

```

It prompts the following warning

```julia
ERROR: Rays not supported yet in the 2D plotting recipe.

```

If there is a way to plot nonbounded set, it is not so simple as I predicted. We need to truncate the set in order to plot it…

```julia
import JuMP, CDDLib, Polyhedra, Plots
m = JuMP.Model()
JuMP.@variable(m, 0 ≤ x[1:2] ≤ 3) # it is not possible to define opened intervals, the value 3 was arbitrary
JuMP.@constraint(m, x[1] ≤ x[2])
poly = Polyhedra.polyhedron(m, CDDLib.Library(:exact))
Plots.plot(poly)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/a/ca3fa2241304161416766ff64f23051118a14f5e.png)

---

<div class="post-metadata">

**Author:** ![tapyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tapyu/32/37182_2.png) [@tapyu](https://discourse.julialang.org/u/tapyu)\
**Post date:** [December 13, 2022, 3:03pm UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582/6 "2022-12-13T15:03:51Z")

</div>

Ok, this is harder when the constraint happens to be nonlinear… For example, I wrote this set

\left\{ (x,y) \in \mathbb{R}\_{+}^{2} \mid xy \leq 1 \right\}

in Julia:

```julia
import JuMP, CDDLib, Polyhedra, Plots
m = JuMP.Model()
JuMP.@variable(m, 0.001 ≤ x[1:2] ≤ 3) # it is not possible to define opened intervals, the value 3 was arbitrary
JuMP.@NLconstraint(m, x[1]*x[2] ≤ 1)
poly = Polyhedra.polyhedron(m, CDDLib.Library(:exact))
Plots.plot(poly)

```

But it produced this nonsense set

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/5/75245388cd24f27039d7dca3b9dc84cbf828849f.png)

I do not know where is the error. However, I suppose that, since this set is not a polyhedron anymore, the command `Polyhedra.polyhedron(m, CDDLib.Library(:exact))` produces a wrong set. how to solve it?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [December 13, 2022, 6:05pm UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582/7 "2022-12-13T18:05:24Z")

</div>

The name of `Polyhedra.jl` gives the game away; it does not support nonlinear functions.

As you’ve found out, the current setup is quite limited. It supports only finite volume polyhedra in 2 or 3 dimensions. If you want something closer to your set syntax, you’ll likely need to write this yourself. I don’t know if there is anything off-the-shelf that can do what you want.

To answer some of your other questions:

- JuMP is used as the modeling layer. It doesn’t always have to be used for optimization.
- CDDlib is used to generate the list of vertices of the polyhedron from the constraint representation in JuMP

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 13, 2022, 9:26pm UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582/8 "2022-12-13T21:26:58Z")

</div>

Just in case, to plot this one:

\left\{ (x,y) \in \mathbb{R}^{2} \mid xy \leq 1 \right\}

You could use the `ImplicitEquations.jl` package:

```julia
using ImplicitEquations, Plots
f(x,y) = x * y
plot(f ≦ 1, fc=:blues, widen=false) # \leqq[tab]

```

 ![Plots_ImplicitEquations](https://global.discourse-cdn.com/julialang/original/3X/d/b/db3c6c56b9c4ccb2e78da2136254a8627a959f6b.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 13, 2022, 10:54pm UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582/9 "2022-12-13T22:54:11Z")

</div>

And this one:

\left\{ (x,y) \in \mathbb{R}\_{++}^{2} \mid x/y \leq 1 \right\}

It could possibly be plotted as follows:

```julia
using ImplicitEquations, Plots
f(x,y) = x / y
g(x,y) = x
h(x,y) = y
plot((f ≦ 1) & (g ≫ 0) & (h ≫ 0), fc=:reds, widen=false) # \leqq[tab] , \gg[tab]

```

 ![Plots_ImplicitEquations2](https://global.discourse-cdn.com/julialang/original/3X/a/d/ad86f3b04bbc206b49d710dc86a847b3174a46c8.png)

---

<div class="post-metadata">

**Author:** ![tapyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tapyu/32/37182_2.png) [@tapyu](https://discourse.julialang.org/u/tapyu)\
**Post date:** [December 13, 2022, 11:08pm UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582/10 "2022-12-13T23:08:56Z")

</div>

@rafael.guerra `ImplicitEquations` seems much more powerful than `Polyhedra` since the former allows more nonlinear sets (the necessity of requiring \gg instead \> is kind of bewildering, but I will study more this pkg). Anyway, thank you for the sharing.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 13, 2022, 11:10pm UTC](https://discourse.julialang.org/t/plotting-sets-using-natural-mathematical-set-expressions/91582/11 "2022-12-13T23:10:10Z")

</div>

> [@tapyu](#):
>
> the necessity of requiring `≫` instead `>` is kind of bewildering

According to the author of the package, this is to avoid type pyracy.
