God evening, I’m new in Julia.
I’m trying to code an algorithm about the building of Number Walls (there’s a video of Mathologer talking about them).
I wrote a code but I don’t know how to make it work since:
- the most of the time it doesn’t give me the right output
-I did some changes in the past resulting in forever loops that’t can’t be stopped.
I program using VS Code, my Computer is a HP with Windows.
My Julia program is updated.
using Plotly, PlotlyJS
using Statistics, ProgressMeter
PlotlyJS.plotlyjs() #gives me error
plotlyjs() #gives me error too
width = 128
height = 128
data = zeros(Float32, width, height)
data[data .== 0] .=NaN
data[1,:] = zeros(Int, width)
data[2,:] = ones(Int, width)
seqq = [0, 1, 1, 0]
w = 0
while w <13
seqq2 = .-1 .* seqq .+ 1
seqq = vcat(seqq,seqq2)
w += 1
end
data[3,:]= seqq[1:width]
riempi_matrice_2 = function()
total_steps = (width - 3) * (width - 2)
p = Progress(total_steps)
for i in 3 : (width-1)
for j in 2:(width-1)
if !isnan(data[i, j-1])
if data[i,j] != 0
if data[i-1, j] != 0
data[i+1, j] = data[i, j]^2 - data[i, j-1] * data[i, j+1] / data[i-1, j]
elseif data[i-1,j] == 0 && data[i-2,j] != 0 #&& j-2>0 && j+2 <= 2000
if j-2>0 && j+2 <= width
data[i+1, j] = (data[i-1,j-1]^2*data[i-1,j+2]+data[i-1,j+1]^2*data[i-1,j-2]-data[i-3,j]*data[i,j]^2)/data[i-2,j]^2
end
elseif data[i-1,j] == 0 && data[i-2,j] == 0 #j-2>0 && j+2 <= 2000 &&
l_lato_finestra = 0
posiz_sul_lato_dal_fondo = 1
for n in 1:(i-2)
if data[i-n,j] == 0
l_lato_finestra +=1
n += 1
else
break
end
end
for n in 1:(i-2)
if data[i-1,j+n] == 0
posiz_sul_lato_dal_fondo +=1
n += 1
else
break
end
end
lato_sinistro = j+posiz_sul_lato_dal_fondo-l_lato_finestra-1
rapp_geom_up = data[i-l_lato_finestra-1,j+1]/data[i-l_lato_finestra-1,j]
rapp_geom_left = data[i-1, lato_sinistro]/data[i-2,lato_sinistro]
rapp_geom_right = data[i-2, j + posiz_sul_lato_dal_fondo]/data[i-1, j + posiz_sul_lato_dal_fondo]
rapp_geom_down = (-1)^l_lato_finestra * rapp_geom_left * rapp_geom_right / rapp_geom_up
data[i+1,j]= (data[i-l_lato_finestra-2,j]/data[i-l_lato_finestra-1,j]*rapp_geom_left+
(-1)^posiz_sul_lato_dal_fondo*data[i-l_lato_finestra+posiz_sul_lato_dal_fondo,lato_sinistro-1]/data[i-l_lato_finestra+posiz_sul_lato_dal_fondo,lato_sinistro]*rapp_geom_up-
(-1)^posiz_sul_lato_dal_fondo*data[i-posiz_sul_lato_dal_fondo,j+posiz_sul_lato_dal_fondo]/data[i-posiz_sul_lato_dal_fondo,j+posiz_sul_lato_dal_fondo]*rapp_geom_down)*
data[i,j]/rapp_geom_right
end
else # data[i,j] ==0
if data[i-1,j] != 0
data[i+1, j] = data[i, j]^2 - data[i, j-1] * data[i, j+1] / data[i-1, j]
else #data[i-1,j] == 0
if data[i-2,j] != 0
if j-2>0 && j+2 <= width
data[i+1, j] = (data[i-1,j-1]^2*data[i-1,j+2]+data[i-1,j+1]^2*data[i-1,j-2]-data[i-3,j]*data[i,j]^2)/data[i-2,j]^2
end
else #data[i-2,j] == 0
if data[i+1,j-1] == 0
data[i+1,j] = 0
else #data[i+1,j-1] != 0
l_lato_finestra = 0
posiz_sul_lato_dal_fondo = 1
for n in 1:(i-2)
if data[i-n,j] == 0
l_lato_finestra += 1
n += 1
else
break
end
end
for n in 1:(i-2)
if data[i-1,j+n] == 0
posiz_sul_lato_dal_fondo += 1
n += 1
else
break
end
end
lato_sinistro = j+posiz_sul_lato_dal_fondo-l_lato_finestra-1
rapp_geom_up = data[i-l_lato_finestra,j+1]/data[i-l_lato_finestra,j]
rapp_geom_left = data[i-1, lato_sinistro]/data[i-2,lato_sinistro]
rapp_geom_right = data[i-2, j + posiz_sul_lato_dal_fondo]/data[i-1, j + posiz_sul_lato_dal_fondo]
rapp_geom_down = (-1)^l_lato_finestra * rapp_geom_left * rapp_geom_right / rapp_geom_up
data[i+1,j]=data[i+1,j-1]/rapp_geom_down
end
end
end
end
end
next!(p)
end
end
end
riempi_matrice_2()
color_matrix = [isodd(abs(data[i,j])) ? "rgb(0, 0, 255)" : (iseven(abs(data[i,j])) ? "rgb(255, 0, 0)" : (isnan(data[i,j]) ? "rgb(0,0,0)" : "rgb(255,255,255)")) for i in 1:height, j in 1:width]
heatmap_plotly = PlotlyJS.plot(PlotlyJS.heatmap(
z = data[1:30,1:60],
color = color_matrix
))
here the code creates a NxN matrix of Nan. I fill the first 3 rows with desired numbers and then, thanks to the function ‘riempi_matrice_2()’ it should fill the remaining matrix. I should eventually obtain a matrix I can display with plotly…but until now it never happened properly.
Thank you in advance for your help and support.
Maxim Tiburziano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.