# Seven Lines of Julia (examples sought)

Nice observation that the “wrong” solution is so good… Bernstein von Mises theorem strikes again.

4 Likes

Love everyone’s contributions… Hate to say it but I hard a hard time finding something around 7 LOC to submit :D. But, I did think of something I did a while back.

``````mutable struct βLASSO
η::Float32
λ::Float32
β::Float32
end

βLASSO(η = Float32(0.01), λ = Float32(0.009), β = Float32(50.0)) = βLASSO(η, λ, β)

function apply!(o::βLASSO, x, Δ)
Δ = o.η .* ( Δ .+ ( o.λ .* sign.( Δ ) ) )
Δ = Δ .* Float32.( abs.( x.data ) .> ( opt.β * opt.λ ) )
return Δ
end
``````

(full sloppy gist here: blasso gif.jl · GitHub)

Summary:
I was reading the following paper: https://arxiv.org/pdf/2007.13657.pdf around when it was first printed. The paper describes a projected gradient descent method for a penalized(β) LASSO regularization. In < 30min I was able to write my own optimizer, implement the majority of the paper and play with the loss function using Flux.jl. Below is a synthetic example of their loss function properly selecting a single variable of interest while minimizing the contributions of the others.

Why Julia:
The tools in the ecosystem and the language facilitate quickly implementing cutting edge stuff. Plus, unicode support for readability!

7 Likes

@juliohm, the nice plot took very long time to display in my laptop (2nd time plotting it was also very slow). Any recommendation on this?

It may be an issue with Plots.jl @rafael.guerra, I had to lock the version to some old version in the documentation for example because plots became too slow. Are you also using the GR backend? We can try to address this in Zulip or in a private message.

Like with Julia, experts say that Shakespearean language can be tricky but that acting it out makes it fun and understandable. On the same vein, the following MIT list of Shakespearean insults might be a good way to learn both languages:

``````using CSV, DataFrames, HTTP
f = CSV.File(HTTP.get("http://web.mit.edu/dryfoo/Funny-pages/shakespeare-insult-kit.html").body);
sk = DataFrame(f[17:66])  # TODO: findfirst "artless" word to set range
r = size(sk,1)
sk = DataFrame([split(sk[i,1]) for i in 1:r])  # output = 3 rows x 50 columns
Shakespeare() = println("Thou "*sk[1,rand(1:r)]*" "*sk[2,rand(1:r)]*" "*sk[3,rand(1:r)])
``````

Then call Shakespeare:

``````julia> Shakespeare()
julia> Shakespeare()
Thou villainous motley-minded giglet
``````
17 Likes

With 15 lines I could write a code to perform a particle simulation with periodic boundary conditions, a langevin thermostat, and a quadratic potential between the particles, and produce an animation:

``````using Plots ; ENV["GKSwstype"]="nul"
const N, τ, Δt, λ, T, k = 100, 1000, 0.01, 1e-3, 0.26, 1e-6
const x, v, f = -0.5 .+ rand(3,N), -0.01 .+ 0.02*randn(3,N), zeros(3,N)
wrap(x,y) = (x-y) > 0.5 ? (x-y)-1 : ( (x-y) < -0.5 ? (x-y)+1 : (x-y) )
anim = @animate for t in 1:τ
f .= 0
for i in 1:N-1, j in i+1:N
f[:,i] .+= wrap.(x[:,i],x[:,j]) .- λ .* v[:,i]
f[:,j] .+= wrap.(x[:,j],x[:,i]) .- λ .* v[:,j]
end
x .= wrap.(x .+ v*Δt .+ (f/2)*Δt^2,zeros(3))
v .= v .+ f*Δt .+ sqrt.(2*λ*k*T*Δt)*randn()
scatter(x[1,:],x[2,:],x[3,:],label="",lims=(-0.5,0.5),aspect_ratio=1,framestyle=:box)
end
gif(anim,"anim.gif",fps=10)
``````

As Tamas mentioned, if we go to a few more lines, this could include an actual Lennard-Jones potential and be parallelized. And that without using any package besides Plots.

55 Likes

I love watching particles dance! Awesome Leandro!

1 Like

Infinizoom! (Background: Simulates a Brownian motion and uses the Brownian self-similarity Wiener process - Wikipedia combined with simple data-augmentation/Brownian bridge sampling to zoom in infinitely.)

``````using Makie
dt = 0.0005; ϵ = 0.1; t = collect(-1.0:dt:1.0); n = length(t)
x = sqrt(dt)*cumsum(randn(n)); x .-= x[end÷2]
T, X = Node(t), Node(x)
p = lines(T, X); display(p)
xlims!(-1.0, 1.0); ylims!(-1.5,1.5)
while true
t .*= (1 + ϵ)
x .*= sqrt(1 + ϵ)
if t[end] > 2.0
t .= collect(-1.0:dt:1.0)
x[1:2:end] = x[n÷4+1:3n÷4+1]
x[2:2:end] = (x[1:2:end-2]+x[3:2:end])/2 + sqrt(dt)/2*randn(n÷2)
x[end÷2+1] = 0
end
T[] = t; X[] = x; sleep(0.02)
end
``````

22 Likes

@FedericoStra, found interesting to perform a zoom-in loop and recompute your fractal and to use animated gif technique shown by @lmiq to create:

``````xc, yc = -0.55, 0.61;
x0, x1 = xc - 2, xc + 2; y0, y1 = yc - 2, yc + 2;
anim = @animate for t in 1:50
x, y = range(x0, x1; length=1000), range(y0, y1; length=1000)
heatmap(x, y, -log.(mandelbrot.(x' .+ y .* im));aspect_ratio=1,border=:none,legend=:none);
x0, x1 = (15x0 + x1)/16, (15x1 + x0)/16;  y0, y1 = (15y0 + y1)/16, (15y1 + y0)/16
end
gif(anim,"mandelbrot_zooms.gif",fps=10)
``````
15 Likes

for reference, what is the extended code that gives your example (a) parallelized (b) with the actual L-J potential (c) both

1 Like

Nice! I was thinking of doing something similar, but I couldn’t find the time, nor fit it in less than 7 lines (you cheated! ).

I think that, to make it better, the number of iterations in `mandelbrot` has to increase the more one zooms in, otherwise the boundary of the fractal becomes too imprecise. Here is a complete version that does so:

``````using Plots
exprange(start, stop, len) = exp.(range(log(start), log(stop), length=len))
function mandelbrot(z; lim=75) w = z
for n = 1:lim;  abs2(w) < 4 ? w = w^2 + z : return n end
lim + 1
end
x₀, y₀ = -0.5626805, 0.6422555
anim = @animate for (r, l) in zip(exprange(2, 1.35e-6, 120), exprange(100, 2500, 120))
x = range(x₀-r, x₀+r; length=600); y = range(y₀-r, y₀+r; length=600);
heatmap(x, y, -log.(log.(mandelbrot.(x' .+ y .* im; lim=round(l))));
legend=:none, border=:none, ticks=:none, size=(600,600), ratio=1)
end
g = gif(anim; fps=12)
``````

Apparently I cannot attach the animated GIF file (which you can find here), so I include only the last frame:

10 Likes

Calculating a cross-correlation in any number of dimensions:

``````for I in CartesianIndices(img)
for J in CartesianIndices(kernel)    # kernel with centered indices
if I+J in CartesianIndices(img)
filtered[I] += img[I+J] * kernel[J]
end
end
end
``````

The code in action:

``````using OffsetArrays
using Makie

# A 3D "image" of random values (either 0 or 1)
img = rand([0.0, 1.0], 20, 60, 40)

# Plot
s1 = volume(img, algorithm=:iso, resolution=(800, 500))

# Function to generate an N-dimensional kernel of uniform values that sum to 1
# The center of the hypercube will have indices (0, 0, ..., 0)
uniform_kernel(n, dim) = OffsetArray(fill(1/n^dim, fill(n, dim)...), fill(-n÷2-1, dim)...)

# 5x5x5 array of uniform values that sum to 1
kernel = uniform_kernel(5, 3) # 3 dimensions

filtered = zero(img)

for I in CartesianIndices(img)
for J in CartesianIndices(kernel)
if I+J in CartesianIndices(img)
filtered[I] += img[I+J] * kernel[J]
end
end
end

s2 = volume(filtered, algorithm=:iso, resolution=(800, 500))
``````

Original image:

Filtered image:

14 Likes

What about a simulation with balls bouncing against each other?
Or a simulation of moving people getting infected with a virus when they meet other people.

I have one of those, but not in a compact code.

From here: GitHub - m3g/CKP

2 Likes

@LaurentPlagne posted this beautiful example:

See Plot a circle with a given radius with Plots.jl. I don’t know if he has published the code for it though.

12 Likes
``````] activate .
instantiate
``````

This is some of my favorite Julia code.

19 Likes

Thank you for the comment

Unfortunately I don’t think that it can fit the 7 lines example list (>1000 SLOC)…

We use this example to show how to structure a not-too-small project (PkgSkeleton, types, modules, sub-modules, tests, export, dependencies, multiple dispatch, profile, //isme …) in our Julia training lectures.

Although I enjoy reading the examples in this thread, I would second @Tamas_Papp saying that Julia’s power is more strongly demonstrated through slightly larger examples.

We must take some time to refactor the package from its training material form and publish it : now I have an extra motivation to see if a Julia wizard could compact it significantly

6 Likes

Side note: Love the color scheme. What are you using for it?

1 Like

I think seven lines of code is excellent. It showcases what Julia can do, and hopefully shows how Jukia can express scientific notation or equations in a compact way.

However one small pleading from me. Let’s now go down the road of Obfuscated Perl. Back in the day when Perl was a dominant language for Web programming, and many other things, there were a lot of smart people involved. Still are probably! There were regular ‘Obfuscated Perl’ competitions.
Also the whitespace as code stuff (where you translate code into …err… meaningful whitespace ) etc.
I know I am being like the Grinch but IMHO these did the language no good

Don’t get me wrong - there is nothing bad about using a language feature but please, please explain what it does so other people can learn.

3 Likes

I recently started using SatelliteToolbox.jl, so here is my submission. This code calculates the location (latitude, longitude, altitude) of the ISS “right now”. The readability suffers a bit trying to stick to seven, but it’s not too bad. This is based off of the example here (see the link for full details).

``````using Dates, SatelliteToolbox