Display function returning BoundsError

I am trying to display a plot using the code below, however i`m getting the following BoundsError

 BoundsError: attempt to access 256-element Vector{Float64} at index [257]

using SpecialFunctions
using AssociatedLegendrePolynomials
using Plots
setprecision(167)


function besbeam(psiamp, axiconang, order, rho, phi, z) 
    k = big(2 * pi) / big(1.54/1000) #fixed wavelength 1.54mm
    kz = k * big(cosd(axiconang))
    krho = k * big(sind(axiconang))
    return big(psiamp * besselj(order, krho * rho) * exp(order * im * phi) * exp(kz * im * z))
end

function makeplotbes(nx, ny, ry, rx, psiamp)
    z = 0
    x = range(-rx, rx, nx)
    y = range(-ry, ry, ny)
    axang = [deg2rad(1), deg2rad(10), deg2rad(40)]
    ord = [0, 1, 5]
    htmaps = [(heatmap([(abs(besbeam(psiamp, big(axiconang), order, big(sqrt(i^2 + j^2)), big(acos(i / big(sqrt(i^2 + j^2)))), z))) for i in x, j in y])) for order in ord, axiconang in axang]
    gr()
    display(plot(vec(htmaps)..., layout = (3, 3)))
end

makeplotbes(200, 200, 20, 20, 1)


Unrelated to your question, but this lines (and all the lines below) is not really doing what you think it does: big promotes its argument to arbitrary precision, but if you perform some operations in double precision before converting to BigFloat then you get a highly inaccurate result in arbitrary precision. Instead you want to convert your operands to BigFloat and then perform the operations. You can do something like

big(2) * big(pi) / (big(154) / big(100000))

or maybe simplify to

big(100000) * big(pi) / big(77)

The same in the lines below. Note also that you can use cis(x) instead of exp(im *x).

1 Like

I think there’s a bug with heatmap and BigFloat:

julia> heatmap(rand(BigFloat, 200, 200))
Error showing value of type Plots.Plot{Plots.GRBackend}:
ERROR: BoundsError: attempt to access 256-element Vector{Float64} at index [257]

If I change your code to convert the output of besbeam to Float64 instead, I get a pretty picture:

Don’t know if this is what you’re expecting, but if it is maybe that’s a sign you don’t need BigFloat at all (given as Mose says it’s not really working the way you think it is in your current code anyway).

1 Like

Turns out this is a known issue:

https://github.com/JuliaPlots/Plots.jl/issues/4270

2 Likes

Since it’s a know issue with Plots.jl (but only for BigFloat, I would suggest not using it, maybe rather Float128 from a package or) I would suggest a different plotting package, e.g. Makie.jl. That seems to be the future, there are also many others, e.g. GR.jl directly (what Plots.jl uses by default, it’s an abstraction layer for many), or pyplot (very fast), also VegaLite if I recall. Or PythonPlot.jl.

1 Like

Fixed by fix `BigFloat` oob by t-bltg · Pull Request #145 · JuliaPlots/PlotUtils.jl · GitHub.

pl

3 Likes

Or just 100000 * big(pi) / 77. Promotion will do the rest for you.

1 Like

Hey, you’re right. Thank you very much, i’ll change it

Wow, i spent hours trying to understand what i was doing wrong thank you so much for explaining that it is a bug, turns out i’m not crazy after all :slight_smile:

heeey nice, it worked! Thank you

yea i need to study and learn more about type hierarchy and promotion for julia. Thank you for the valuable advice.