Question on LazySets and Plots: translate not defined

Hi all,

after following It’s all Set tutorial on JuliaCon2021 I have found a speed bump when I try this:

using Plots, LazySets, LaTeXStrings

p(args...; kwargs...) = plot(xlims=(0,4), ylims=(0,4), ratio=1, lab="",
                        xlab=L"x_{1}", ylab=L"x_{2}", args...; kwargs...)
p!(args...; kwargs...) = plot!(lab="", args...; kwargs...);

X = BallInf([1.5, 2.0], 1.0)
v = [1.5, -1.0]
Y = translate(X, v)

# arrows
p([0, v[1]], [0, v[2]], linecolor=:gray, arrow-:arrow, linestyle=:dot, width=2)
p!([high(X, 1), high(Y, 1)], [high(X, 2), high(Y, 2)], linecolor=:gray,
    arrow=:arrow, linestyle=:dot, width=2)
p!([center(X,1), center(Y, 1)], [center(X, 2), center(Y, 2)], linecolor=:gray,
    arrow=:arrow, linestyle=:dot, width=2)

# sets
p!(X, lab="X")
p!(Singleton(v), alpha=0.8, lab="v")
p!(Y, lab="X + v")

I got an error of:
WARNING: both LazySets and Plots export “translate”; uses of it in module Main must be qualified
ERROR: LoadError: UndefVarError: translate not defined
Stacktrace:
** [1] top-level scope**
** @ ~/LasthrimProjection/plot.jl:9**
** [2] include(fname::String)**
** @ Base.MainInclude ./client.jl:451**
** [3] top-level scope**
** @ REPL[4]:1**
in expression starting at /home/browni/LasthrimProjection/plot.jl:9

Is translate a function of another package? In every row I write the using LazySets, Plots,... again because I might not run the first row at the notebook.

As the warning says, both Plots and LazySets define different functions that have the same name translate, and both are exported by those packages, so there’s a conflict. When you call translate(X, v), Julia doesn’t know which translate you’re referring to - whether the one from Plots or the one from LazySets - so there’s an error.

You can fix the error by changing the Y = translate(X, v) line to Y = LazySets.translate(X, v) to specify that that’s the function you want.

Or better, if you’re only using plot and plot! functions from Plots.jl, you can do:

using LazySets, LaTeXStrings
using Plots: plots, plots!

at the beginning to avoid the conflict in the first place.

Yes, both packages contain the method translate.

?LazySets.translate
translate(hs::HalfSpace, v::AbstractVector; [share]::Bool=false)
Translate (i.e., shift) a half-space by a given vector.

Input
hs – half-space
v – translation vector
share – (optional, default: false) flag for sharing unmodified parts of the original set representation
Output
A translated half-space.

and
?Plots.translate

translate(shape, x, y = x)
translate!(shape, x, y = x)
#Translate a Shape in space.

Hence you should replace translate in your code either by LazySets.translate or by Plots.translate.

I try with:

Y = LazySets.translate(X, v)

I get:

MethodError: no method matching -(::typeof(arrow), ::Symbol)
Closest candidates are:
** -(::Union{MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarQuadraticFunction{T}}, ::T) where T at ~/.julia/packages/MathOptInterface/YDdD3/src/Utilities/functions.jl:1725**
** -(::ChainRulesCore.AbstractThunk, ::Any) at ~/.julia/packages/ChainRulesCore/ctmSK/src/tangent_types/thunks.jl:34**
** -(::ChainRulesCore.NoTangent, ::Any) at ~/.julia/packages/ChainRulesCore/ctmSK/src/tangent_arithmetic.jl:61**
** …**

Stacktrace:
** [1] top-level scope**
** @ In[15]:13**
** [2] eval**
** @ ./boot.jl:373 [inlined]**
** [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)**
** @ Base ./loading.jl:1196**

When I try

Y = Plots.translate(X, v)

I get:

MethodError: no method matching translate(::BallInf{Float64, Vector{Float64}}, ::Vector{Float64})

Stacktrace:
** [1] top-level scope**
** @ In[16]:10**
** [2] eval**
** @ ./boot.jl:373 [inlined]**
** [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)**
** @ Base ./loading.jl:1196**

This sounds like it’s just from the typo: in the line

 p([0, v[1]], [0, v[2]], linecolor=:gray, arrow-:arrow, linestyle=:dot, width=2)

instead of arrow = :arrow you have arrow-:arrow.

I fixed it, but still got this:

MethodError: no method matching translate(::BallInf{Float64, Vector{Float64}}, ::Vector{Float64})

Have you removed the originial using line and replaced it with:

using LazySets, LaTeXStrings
using Plots: plots, plots!

as suggested? And if so, have you restarted the notebook after you did that?

This is the code now:

using LazySets, LaTeXStrings
using Plots: plots, plots!

p(args...; kwargs...) = plot(xlims=(0,4), ylims=(0,4), ratio=1, lab="",
                        xlab=L"x_{1}", ylab=L"x_{2}", args...; kwargs...)
p!(args...; kwargs...) = plot!(lab="", args...; kwargs...);

X = BallInf([1.5, 2.0], 1.0)
v = [1.5, -1.0]
Y = Plots.translate(X, v)

# arrows
p([0, v[1]], [0, v[2]], linecolor=:gray, arrow=:arrow, linestyle=:dot, width=2)
p!([high(X, 1), high(Y, 1)], [high(X, 2), high(Y, 2)], linecolor=:gray,
    arrow=:arrow, linestyle=:dot, width=2)
p!([center(X,1), center(Y, 1)], [center(X, 2), center(Y, 2)], linecolor=:gray,
    arrow=:arrow, linestyle=:dot, width=2)

# sets
p!(X, lab="X")
p!(Singleton(v), alpha=0.8, lab="v")
p!(Y, lab="X + v")

This is the error after that code:
UndefVarError: plots not defined

Stacktrace:
** [1] eval**
** @ ./boot.jl:373 [inlined]**
** [2] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)**
** @ Base ./loading.jl:1196**

I have restarted it too.

plots and plots! don’t exist, the functions are called plot and plot!

It is really confusing here, sorry for I am not catching it quite right, but the function is Plots:

I try again:

using LazySets, LaTeXStrings
using Plots: plots, plots!

p(args...; kwargs...) = plots(xlims=(0,4), ylims=(0,4), ratio=1, lab="",
                        xlab=L"x_{1}", ylab=L"x_{2}", args...; kwargs...)
p!(args...; kwargs...) = plots!(lab="", args...; kwargs...);

X = BallInf([1.5, 2.0], 1.0)
v = [1.5, -1.0]
Y = LazySets.translate(X, v)

# arrows
p([0, v[1]], [0, v[2]], linecolor=:gray, arrow=:arrow, linestyle=:dot, width=2)
p!([high(X, 1), high(Y, 1)], [high(X, 2), high(Y, 2)], linecolor=:gray,
    arrow=:arrow, linestyle=:dot, width=2)
p!([center(X,1), center(Y, 1)], [center(X, 2), center(Y, 2)], linecolor=:gray,
    arrow=:arrow, linestyle=:dot, width=2)

# sets
p!(X, lab="X")
p!(Singleton(v), alpha=0.8, lab="v")
p!(Y, lab="X + v")

Get:

UndefVarError: plots not defined

Stacktrace:
** [1] eval**
** @ ./boot.jl:373 [inlined]**
** [2] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)**
** @ Base ./loading.jl:1196**

When checking the packages:

]st

      Status `~/LasthrimProjection/Project.toml`
  [3391f64e] CDDLib v0.7.0
  [13f3f980] CairoMakie v0.5.10
  [39dd38d3] Dierckx v0.5.2
  [b4f34e82] Distances v0.10.7
  [d997a800] Implicit3DPlotting v0.2.3 `https://github.com/matthiashimmelmann/Implicit3DPlotting.jl.git#main`
  [a98d9a8b] Interpolations v0.14.4
  [d1acc4aa] IntervalArithmetic v0.20.7
  [b964fa9f] LaTeXStrings v1.3.0
  [b4f0291d] LazySets v2.0.0
  [ae8d54c2] Luxor v3.5.0
  [429524aa] Optim v1.7.1
  [f0f68f2c] PlotlyJS v0.18.8
  [91a5bcdd] Plots v1.31.7
  [67491407] Polyhedra v0.6.17
  [438e738f] PyCall v1.93.1
  [ce6b1742] RDatasets v0.7.7
  [9ec6d097] TruthTables v0.4.1

No it’s not. Make sure to run your code line-by-line in a fresh Julia session whenever posting here to make sure you understand where your errors appear and that you have no hidden state.

julia> using Plots: plots # should be plot
ERROR: UndefVarError: plots not defined
2 Likes

I get it works now, thanks @nilshg !

1 Like

How to know I have hidden state?

By hidden state I just mean old definitions hanging around - so the longer you’ve got a session running the more likely it is that you’ve got some hidden state. Then when someone asks you to try something it might fail for you, e.g. because you have overwritten some definition in Base. A classic is something like:

julia> sum = 5+5 # seems like a sensible variable name?
10

julia> sum([1, 2, 3]) # Oh no!
ERROR: MethodError: objects of type Int64 are not callable

Therefore it’s always useful to try things in a fresh Julia session when interacting with people on here, to ensure that other people can recreate what you’re seeing locally.

2 Likes

Oops, that’s on me. Somehow had a brainfart between writing “if you’re only using plot and plot! functions from Plots.jl” and the using line right after it, and managed to misspell it. Thanks for catching it!

1 Like