Heat map in Julia - julia

I am a new user and have no idea how to solve this error:
using AbstractGPs
using LinearAlgebra
using Plots
using Random
using PlutoUI
randomseed=42
Random.seed!(randomseed)
X = rand(datapoints) .* 2 .- 1
PES(x)=x^2
plot(-1:0.01:1, PES, label="Underlying potential", xlabel="Position", ylabel="Energy")
Y = PES.(X)
kernelfunction=SEKernel()
Xrange=range(-3,3; length=100)
plot(heatmap(kernelmatrix(kernelfunction, Xrange); yflip=true))
Stack trace:
MethodError: no method matching Val{:heatmap}(::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char, ::Char)
1. apply_recipe(::Dict{Symbol,Any}, ::Type{Val{:heatmap}}, ::Plots.Plot{Plots.GRBackend})#recipes.jl:49
2. _process_plotrecipe(::Any, ::Any, ::Any, ::Any)#plot_recipe.jl:32
3. _process_plotrecipes!(::Any, ::Any)#plot_recipe.jl:18
4. recipe_pipeline!(::Any, ::Any, ::Any)#RecipesPipeline.jl:81
5. _plot!(::Plots.Plot, ::Any, ::Any)#plot.jl:208
6. #plot#154#plot.jl:91[inlined]
7. #heatmap#406(::Any, ::typeof(Plots.heatmap), ::Any)#RecipesBase.jl:404
8. top-level scope#Local: 2[inlined]

Not sure what you did wrong but this
using AbstractGPs, LinearAlgebra, Plots, Random # removed PlutoUI
randomseed = 42
Random.seed!(randomseed)
datapoints = 100 # Added for code to be self-contained
X = rand(datapoints) .* 2 .- 1
PES(r) = r^2
# removed first plot
Y = PES.(X)
kernelfunction = SEKernel()
Xrange = range(-3, 3; length=100)
heatmap(kernelmatrix(kernelfunction, Xrange); yflip=true) # removed unnecessary call to `plot`
gives me

Related

Interact.jl error: WebIOServer not defined

I am getting this error when using the Interact.jl #manipulate macro in Juno:
UndefVarError: WebIOServer not defined
enter code here
setup_server() at webio.jl:76
show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::MIME{Symbol("application/prs.juno.plotpane+html")}, ::Widget{:manipulate,Any}) at webio.jl:68
show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::String, ::Widget{:manipulate,Any}) at multimedia.jl:109
displayinplotpane(::Widget{:manipulate,Any}) at showdisplay.jl:51
displayandrender(::Widget{:manipulate,Any}) at showdisplay.jl:131
(::Atom.var"#208#213"{String})() at eval.jl:136
#invokelatest#1 at essentials.jl:712 [inlined]
invokelatest at essentials.jl:711 [inlined]
macro expansion at dynamic.jl:24 [inlined]
eval(::String, ::Int64, ::String, ::String, ::Bool) at eval.jl:113
invokelatest(::Any, ::Any, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}}) at essentials.jl:712
invokelatest(::Any, ::Any, ::Vararg{Any,N} where N) at essentials.jl:711
macro expansion at eval.jl:41 [inlined]
(::Atom.var"#188#189")() at task.jl:358
Piece of code (the function generatePlot is defined previously for generating Makie plots):
#manipulate for variable = Dict(string(vars[i])=>vars[i] for i=1:16),
labl = Dict(string(label[i])=>label[i] for i=1:length(label))
generatePlot(variable, labl)
end
Env:
[c601a237] Interact v0.10.3
[ee78f7c6] Makie v0.11.0
[85f8d34a] NCDatasets v0.10.2
[2913bbd2] StatsBase v0.33.0
[0f1e0344] WebIO v0.8.14
Any thoughts on what can be the problem?

Julia using LsqFit for Lorentzian curve fitting

The following code in Julia plots a Lorenztian curve and then uses the curve_fit function to determine the parameters.
using LsqFit
model(x,p)=p[1] ./(p[1]^2 .+(x .-p[2]).^2)
#Test values
p0=[10,50]
tdata=range(-150,stop=150,length=300)
ydata = model(tdata, p0)
fit=curve_fit(model,tdata,ydata,p0)
In this case the result should be exact as I use the model to calculate the y-values and then pass these exact y-values to the curve_fit function. However, Julia returns the error:
InexactError: Int64(NaN)
Int64(::Float64) at float.jl:710
x_of_nans(::Array{Int64,1}, ::Type{T} where T) at NLSolversBase.jl:60
x_of_nans(::Array{Int64,1}) at NLSolversBase.jl:60
NLSolversBase.OnceDifferentiable(::Function, ::Function, ::Function, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}; inplace::Bool) at oncedifferentiable.jl:229
NLSolversBase.OnceDifferentiable(::Function, ::Function, ::Function, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}) at oncedifferentiable.jl:225
NLSolversBase.OnceDifferentiable(::NLSolversBase.var"#ff!#1"{LsqFit.var"#18#20"{typeof(model),StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Array{Float64,1}}}, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}, ::Symbol, ::ForwardDiff.Chunk{2}) at oncedifferentiable.jl:147
NLSolversBase.OnceDifferentiable(::Function, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}, ::Symbol) at oncedifferentiable.jl:96
NLSolversBase.OnceDifferentiable(::Function, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}; inplace::Bool, autodiff::Symbol) at oncedifferentiable.jl:28
(::Core.var"#Type##kw")(::NamedTuple{(:inplace, :autodiff),Tuple{Bool,Symbol}}, ::Type{NLSolversBase.OnceDifferentiable}, ::Function, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}) at oncedifferentiable.jl:26
(::Core.var"#Type##kw")(::NamedTuple{(:inplace, :autodiff),Tuple{Bool,Symbol}}, ::Type{NLSolversBase.OnceDifferentiable}, ::Function, ::Array{Int64,1}, ::Array{Float64,1}) at oncedifferentiable.jl:26
lmfit(::LsqFit.var"#18#20"{typeof(model),StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Array{Float64,1}}, ::Array{Int64,1}, ::Array{Float64,1}; autodiff::Symbol, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at curve_fit.jl:63
lmfit(::Function, ::Array{Int64,1}, ::Array{Float64,1}) at curve_fit.jl:61
curve_fit(::typeof(model), ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Array{Float64,1}, ::Array{Int64,1}; inplace::Bool, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at curve_fit.jl:115
curve_fit(::Function, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Array{Float64,1}, ::Array{Int64,1}) at curve_fit.jl:106
top-level scope at mcconnel_simulation.jl:71
Why is curve_fit unable to solve this easy case?
p0 needs to have float eltype (for example Float64):
julia> p0=[10.0,50.0]
2-element Array{Float64,1}:
10.0
50.0
julia> fit=curve_fit(model,collect(tdata),ydata,p0)
LsqFit.LsqFitResult{Array{Float64,1},Array{Float64,1},Array{Float64,2},Array{Float64,1}}([10.0, 50.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … -2.168404344971009e-19, -6.505213034913027e-19, 2.168404344971009e-19, 4.336808689942018e-19, 2.168404344971009e-19, 2.168404344971009e-19, 0.0, 0.0, 0.0, 0.0], [2.4813278524001437e-5 -2.4875467192362816e-6; 2.506222852629442e-5 -2.52523618286487e-6; … ; 9.896618565083517e-5 2.0199959191107846e-5; 9.704930889082663e-5 1.9605920988595578e-5], true, Float64[])
you can also write p0 = Float64[10,50] to make sure its eltype is Float64.

Why does DifferentialEquations in Julia give me a "no matching method" error for this system of ODEs?

I want to solve a system of seven coupled ODEs in Julia.
My code is:
function bsys!(dv,v,k,t)
dp,thp,dr,thr,dx,thx,phi = v
a = exp(t)
rp = r_y(a)
rr = r_r(a)
rx = r_x(a)
ix = T_a(a)/mx
vy = v_a(a)
tci = (ny_a(a)*vy*(4*pi*alpha*alpha/(3*mx*mx))*sqrt(3/2)*ix)/Hi
H = sqrt((8*pi*G/3)*(rp + rr + rx))/Hi
phidot = -(1 + k*k/(3*H*H*a*a))*phi + 4*pi*G*(rp*dp + rr*dr + rx*dx)/(3*H*H*Hi*Hi)
w = w_a(a)
wd = dw_a(a)
cs2 = cs2_a(a)
wx = wx_a(a)
wdx = dwx_a(a)
cs2x = cs2x_a(a)
dv[1] = -(1+w)*thp/(a*H) - 3*(1+w)*phidot -3*(cs2 - w)*dp + gammatilde*phi/H
dv[2] = -(1-3*w)*thp - k*k*phi/(a*H) - wd*thp/(1 + w) + cs2*k*k*dp/(a*H*(1+w)) + (w/(1+w))*gammatilde*thp/H
dv[3] = -(4/3)*thr/(a*H) - 4*phidot + (rp/rr)*gammatilde*(dp-dr-phi)/H
dv[4] = -k*k*phi/(a*H) + (1/4)*k*k*dr/(a*H) + (rp/rr)*gammatilde*((3/4)*thp - thr)/H
dv[5] = -(1+wx)*thx/(a*H) - 3*(1+wx)*phidot -3*(cs2x - wx)*dx
dv[6] = -(1-3*wx)*thx - k*k*phi/(a*H) - wdx*thx/(1 + wx) + cs2x*k*k*dx/(a*H*(1+wx)) + tci*(thp - thx)/H
dv[7] = phidot
end
tspan = (0.0,3.0)
k = 100*krhtilde
init = (2.0,-0.5*k*k,2.0,-0.5*k*k,2.0,-0.5*k*k,1.0)
prob = ODEProblem(bsys!,init,tspan,k)
sol = solve(prob)
I get the following error:
MethodError: no method matching zero(::NTuple{7,Float64})
Closest candidates are:
zero(!Matched::Type{Missing}) at missing.jl:103
zero(!Matched::Type{LibGit2.GitHash}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\LibGit2\src\oid.jl:220
zero(!Matched::Type{Pkg.Resolve.VersionWeights.VersionWeight}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Pkg\src\resolve\VersionWeights.jl:19
...
Stacktrace:
[1] alg_cache(::Tsit5, ::NTuple{7,Float64}, ::NTuple{7,Float64}, ::Type, ::Type, ::Type, ::NTuple{7,Float64}, ::NTuple{7,Float64}, ::ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Float64, ::Float64, ::Float64, ::Float64, ::Bool, ::Val{true}) at C:\Users\hganj\.julia\packages\OrdinaryDiffEq\8Pn99\src\caches\low_order_rk_caches.jl:349
[2] (::OrdinaryDiffEq.var"#197#198"{NTuple{7,Float64},NTuple{7,Float64},DataType,DataType,DataType,NTuple{7,Float64},NTuple{7,Float64},ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Float64,Float64,Float64,Float64,Bool})(::Tsit5) at C:\Users\hganj\.julia\packages\OrdinaryDiffEq\8Pn99\src\caches\basic_caches.jl:22
[3] map(::OrdinaryDiffEq.var"#197#198"{NTuple{7,Float64},NTuple{7,Float64},DataType,DataType,DataType,NTuple{7,Float64},NTuple{7,Float64},ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Float64,Float64,Float64,Float64,Bool}, ::Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}}) at .\tuple.jl:140
[4] alg_cache(::CompositeAlgorithm{Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}},AutoSwitch{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}, ::NTuple{7,Float64}, ::NTuple{7,Float64}, ::Type, ::Type, ::Type, ::NTuple{7,Float64}, ::NTuple{7,Float64}, ::ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Float64, ::Float64, ::Float64, ::Float64, ::Bool, ::Val{true}) at C:\Users\hganj\.julia\packages\OrdinaryDiffEq\8Pn99\src\caches\basic_caches.jl:21
[5] #__init#329(::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Nothing, ::Bool, ::Bool, ::Bool, ::Bool, ::Nothing, ::Bool, ::Bool, ::Float64, ::Float64, ::Float64, ::Bool, ::Bool, ::Rational{Int64}, ::Nothing, ::Nothing, ::Rational{Int64}, ::Int64, ::Int64, ::Int64, ::Rational{Int64}, ::Bool, ::Int64, ::Nothing, ::Nothing, ::Int64, ::typeof(DiffEqBase.ODE_DEFAULT_NORM), ::typeof(LinearAlgebra.opnorm), ::typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), ::typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Int64, ::String, ::typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), ::Nothing, ::Bool, ::Bool, ::Bool, ::Bool, ::Base.Iterators.Pairs{Symbol,Bool,Tuple{Symbol,Symbol},NamedTuple{(:default_set, :second_time),Tuple{Bool,Bool}}}, ::typeof(DiffEqBase.__init), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::CompositeAlgorithm{Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}},AutoSwitch{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}, ::Array{NTuple{7,Float64},1}, ::Array{Float64,1}, ::Array{Any,1}, ::Type{Val{true}}) at C:\Users\hganj\.julia\packages\OrdinaryDiffEq\8Pn99\src\solve.jl:281
[6] (::DiffEqBase.var"#kw##__init")(::NamedTuple{(:default_set, :second_time),Tuple{Bool,Bool}}, ::typeof(DiffEqBase.__init), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::CompositeAlgorithm{Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}},AutoSwitch{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}, ::Array{NTuple{7,Float64},1}, ::Array{Float64,1}, ::Array{Any,1}, ::Type{Val{true}}) at .\none:0 (repeats 5 times)
[7] #__solve#328(::Base.Iterators.Pairs{Symbol,Bool,Tuple{Symbol,Symbol},NamedTuple{(:default_set, :second_time),Tuple{Bool,Bool}}}, ::typeof(DiffEqBase.__solve), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::CompositeAlgorithm{Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}},AutoSwitch{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}) at C:\Users\hganj\.julia\packages\OrdinaryDiffEq\8Pn99\src\solve.jl:4
[8] (::DiffEqBase.var"#kw##__solve")(::NamedTuple{(:default_set, :second_time),Tuple{Bool,Bool}}, ::typeof(DiffEqBase.__solve), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::CompositeAlgorithm{Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}},AutoSwitch{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}) at .\none:0
[9] #__solve#1(::Bool, ::Base.Iterators.Pairs{Symbol,Bool,Tuple{Symbol},NamedTuple{(:second_time,),Tuple{Bool}}}, ::typeof(DiffEqBase.__solve), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::Nothing) at C:\Users\hganj\.julia\packages\DifferentialEquations\hc2aQ\src\default_solve.jl:7
[10] #__solve at .\none:0 [inlined]
[11] #__solve#448 at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:185 [inlined]
[12] __solve at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:180 [inlined]
[13] #solve_call#443(::Bool, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(DiffEqBase.solve_call), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}) at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:44
[14] solve_call at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:37 [inlined]
[15] #solve#444 at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:67 [inlined]
[16] solve(::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}) at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:51
[17] top-level scope at In[160]:1
The various function calls within the ODE function bsys are working correctly. I think the issue is with the arithmetic I am doing in the expressions to assign values to dv.
Any help on this would be appreciated. I am new to Julia and have no idea what might be going wrong.
I think init needs to be an array instead of a tuple. Try init = [ ... ] instead of init = ( ... ).

How to overload Base.show for custom array types?

Suppose I make my own custom vector type with it's own custom show method:
struct MyVector{T} <: AbstractVector{T}
v::Vector{T}
end
function Base.show(io::IO, v::MyVector{T}) where {T}
println(io, "My custom vector with eltype $T with elements")
for i in eachindex(v)
println(io, " ", v.v[i])
end
end
If I try making one of these objects at the REPL I get unexpected errors related to functions I never intended to call:
julia> MyVector([1, 2, 3])
Error showing value of type MyVector{Int64}:
ERROR: MethodError: no method matching size(::MyVector{Int64})
Closest candidates are:
size(::AbstractArray{T,N}, ::Any) where {T, N} at abstractarray.jl:38
size(::BitArray{1}) at bitarray.jl:77
size(::BitArray{1}, ::Integer) at bitarray.jl:81
...
Stacktrace:
[1] axes at ./abstractarray.jl:75 [inlined]
[2] summary(::IOContext{REPL.Terminals.TTYTerminal}, ::MyVector{Int64}) at ./show.jl:1877
[3] show(::IOContext{REPL.Terminals.TTYTerminal}, ::MIME{Symbol("text/plain")}, ::MyVector{Int64}) at ./arrayshow.jl:316
[4] display(::REPL.REPLDisplay, ::MIME{Symbol("text/plain")}, ::Any) at /Users/mason/julia/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:132
[5] display(::REPL.REPLDisplay, ::Any) at /Users/mason/julia/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:136
[6] display(::Any) at ./multimedia.jl:323
...
Okay, whatever so I'll implement Base.size so it'll leave me alone:
julia> Base.size(v::MyVector) = size(v.v)
julia> MyVector([1, 2, 3])
3-element MyVector{Int64}:
Error showing value of type MyVector{Int64}:
ERROR: getindex not defined for MyVector{Int64}
Stacktrace:
[1] error(::String, ::Type) at ./error.jl:42
[2] error_if_canonical_getindex(::IndexCartesian, ::MyVector{Int64}, ::Int64) at ./abstractarray.jl:991
[3] _getindex at ./abstractarray.jl:980 [inlined]
[4] getindex at ./abstractarray.jl:981 [inlined]
[5] isassigned(::MyVector{Int64}, ::Int64, ::Int64) at ./abstractarray.jl:405
[6] alignment(::IOContext{REPL.Terminals.TTYTerminal}, ::MyVector{Int64}, ::UnitRange{Int64}, ::UnitRange{Int64}, ::Int64, ::Int64, ::Int64) at ./arrayshow.jl:67
[7] print_matrix(::IOContext{REPL.Terminals.TTYTerminal}, ::MyVector{Int64}, ::String, ::String, ::String, ::String, ::String, ::String, ::Int64, ::Int64) at ./arrayshow.jl:186
[8] print_matrix at ./arrayshow.jl:159 [inlined]
[9] print_array at ./arrayshow.jl:308 [inlined]
[10] show(::IOContext{REPL.Terminals.TTYTerminal}, ::MIME{Symbol("text/plain")}, ::MyVector{Int64}) at ./arrayshow.jl:345
[11] display(::REPL.REPLDisplay, ::MIME{Symbol("text/plain")}, ::Any) at /Users/mason/julia/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:132
[12] display(::REPL.REPLDisplay, ::Any) at /Users/mason/julia/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:136
[13] display(::Any) at ./multimedia.jl:323
...
Hmm, now it wants getindex
julia> Base.getindex(v::MyVector, args...) = getindex(v.v, args...)
julia> MyVector([1, 2, 3])
3-element MyVector{Int64}:
1
2
3
What? That wasn't the print formatting I told it to do! what's going on here?
The problem is that in julia, Base defines a method Base.show(io::IO ::MIME"text/plain", X::AbstractArray) which is actually more specific than the Base.show(io::IO, v::MyVector) for the purposes of display. This section of the julia manual describes the sort of custom printing that AbstractArray uses. So if we want to use our custom show method, we instead need to do
julia> function Base.show(io::IO, ::MIME"text/plain", v::MyVector{T}) where {T}
println(io, "My custom vector with eltype $T and elements")
for i in eachindex(v)
println(io, " ", v.v[i])
end
end
julia> MyVector([1, 2, 3])
My custom vector with eltype Int64 and elements
1
2
3
See also: https://discourse.julialang.org/t/extending-base-show-for-array-of-types/31289

JuMP variable declaration: MethodError

I'm following the quickstart guide on JuMP.
My julia version is 0.7, the code is this:
using JuMP
m = Model()
l = zeros(10)
u = ones(10)
##variable(m, x)
##variable(m, 0 <= x[1:10] <= 1)
#variable(m, l<=x[1:10]<=u)
The first two variable macros (commented out) work fine, but the last one produces an error.
MethodError: no method matching constructvariable!(::Model, ::getfield(JuMP, Symbol("#_error#107")){Tuple{Symbol,Expr}}, ::Array{Float64,1}, ::Array{Float64,1}, ::Symbol, ::String, ::Float64)
Closest candidates are:
constructvariable!(::Model, ::Function, !Matched::Number, !Matched::Number, ::Symbol, ::AbstractString, ::Number; extra_kwargs...) at /home/lhk/.julia/packages/JuMP/Xvn0n/src/macros.jl:968
constructvariable!(::Model, ::Function, !Matched::Number, !Matched::Number, ::Symbol, !Matched::Number, !Matched::Array{T,1} where T, !Matched::Array{Float64,1}, !Matched::AbstractString, !Matched::Number; extra_kwargs...) at /home/lhk/.julia/packages/JuMP/Xvn0n/src/macros.jl:961
Stacktrace:
[1] top-level scope at /home/lhk/.julia/packages/JuMP/Xvn0n/src/macros.jl:1259
[2] top-level scope at In[18]:7
How can I have different bounds for each entry in a vector valued variable ?
Argh, this is actually really easy:
l = zeros(10)
u = ones(10)
#variable(m, l[idx] <= x[idx = 1:10] <= u[idx])

Resources