# Seven Lines of Julia (examples sought)

**URL:** https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416
**Category:** General Usage
**Created:** [November 19, 2020, 7:37am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416 "2020-11-19T07:37:29Z")
**Posts on this page:** 20
**Page:** 5

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 9, 2021, 10:39pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/92 "2021-04-09T22:39:37Z")

</div>

> [@FedericoStra](#):
>
> It’s also super slow and wasteful

What do you mean, “superslow”? You’re timing the runtime of `@btime`, that’s absurd.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 10, 2021, 8:38am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/94 "2021-04-10T08:38:22Z")

</div>

Here’s the kind of thing I use a script language for most of the time, and the amount of effort it would take to get anything even similar in Matlab…

```julia
using Unitful, Latexify, Plots, UnitfulLatexify, UnitfulRecipes # Three packages + some glue...
default(fontfamily="Computer Modern")
W = rand(30)u"J"
v = sqrt.(2*W/3u"kg") .|> u"m/s"
plot(W, v; st=:scatter, legend=false, xguide="W", yguide="v", unitformat=latexify)
# I yield my remaining two lines as compensation for the extra package

```

![graph](https://global.discourse-cdn.com/julialang/original/3X/a/1/a13736bf6f97bfda6777f6f55748fa0d54090416.png)

(btw, I’m cheating a bit, had to `]dev Plots` waiting for [CompatHelper: bump compat for "Latexify" to "0.15" · JuliaPlots/Plots.jl@dbe9a2c · GitHub](https://github.com/JuliaPlots/Plots.jl/commit/dbe9a2c52b18bddb6f3ad1289ceb3d50a1cd7019) to be registered)

---

<div class="post-metadata">

### Author: ![FedericoStra](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)
#### Post date: [April 10, 2021, 12:46pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/95 "2021-04-10T12:46:48Z")

</div>

> [@DNF](#):
>
> What do you mean, “superslow”? You’re timing the runtime of `@btime` , that’s absurd.

It’s not absurd. `@btime` measures only the execution time, not the compilation time. I’m using `@time` to demonstrate that the compilation time is _huge_. And you pay the price of recompilation every time you change `n`. Just try it. Using `ComposedFunction` for iterated functions is not the correct way to go.

You don’t have to _believe_ me, try the following:

```julia
julia> using Statistics

julia> F(x) = [mean(x), prod(x)^(1/length(x)), median(x)]
F (generic function with 1 method)

julia> Fn(x, n) = foldl((y,_) -> F(y), 1:n; init=x)
Fn (generic function with 1 method)

julia> Base.:^(f::Function, n) = n==1 ? f : f ∘ f^(n-1)

julia> @time Fn([1.,1,2,3,5], 10^3)
  0.000168 seconds (2.00 k allocations: 219.078 KiB)
3-element Vector{Float64}:
 2.089057949736859
 2.089057949736859
 2.089057949736859

julia> @time (F^(10^3))([1.,1,2,3,5])
  5.416531 seconds (3.22 M allocations: 222.397 MiB, 1.94% gc time, 98.99% compilation time)
3-element Vector{Float64}:
 2.089057949736859
 2.089057949736859
 2.089057949736859

julia> @time (F^(10^4))([1.,1,2,3,5])
# will not complete in a long loooooong time
# and you cannot even interrupt with Ctrl-C
# use `pkill -9 julia` to kill it

```

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [April 10, 2021, 1:03pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/96 "2021-04-10T13:03:45Z")

</div>

I believe `@btime` always execute the function a single time and then throw the time away, and then execute it a bunch times more to get a mean. So timing `@btime` will get this first execution (and if the function was not compiled it will therefore get compilation time) and then will take the time of an arbitrary number of runs that you do not know how many. So I have no idea why you are timing `@btime`.

---

<div class="post-metadata">

### Author: ![FedericoStra](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)
#### Post date: [April 10, 2021, 1:11pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/97 "2021-04-10T13:11:13Z")

</div>

Oh now I see, my mistake: I wanted to use `@time` _instead of_ `@btime`, not in conjunction with. I’ll leave the error as it is.

But still, see my previous answer (which is now correct, and my original intention). The version with `^` is enormously slower due to the insane compilation. With `F^(10^4)` I didn’t even have the patience to let it finish. `foldl` (or a manual implementation) on the other hand is instantaneous to compile and faster to execute (around 4x on my PC). Hence, I see no good reason to do this wasteful fancy composition.

---

<div class="post-metadata">

### Author: ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)
#### Post date: [April 10, 2021, 1:33pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/98 "2021-04-10T13:33:09Z")

</div>

This is straying from the brief since it’s all under the hood… but it does show off how simple it is to install packages and do fun things with it.

```julia
(@v1.6) pkg> add https://github.com/IanButterworth/VideoInTerminal.jl
julia> using VideoInTerminal
julia> showcam()

```

(please report any camera issues to [VideoIO](https://github.com/JuliaIO/VideoIO.jl))

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 10, 2021, 1:42pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/99 "2021-04-10T13:42:49Z")

</div>

I said that it’s unsafe _and_ type piracy, so obviously, one shouldn’t _do_ it. I just showed it because it’s cool.

---

<div class="post-metadata">

### Author: ![FedericoStra](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)
#### Post date: [April 10, 2021, 3:05pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/100 "2021-04-10T15:05:13Z")

</div>

What exactly is _unsafe_ about it?

The `^` notation is actually pretty cool, so I’m not much against the type piracy, but rather the implementation.

To reiterate (no pun intended), my main concern would be the efficiency, especially as regards compilation.

Notice how with your implementation `^` constructs a deeply nested object with a deeply nested type. Try `dump(cos^10)` to see what I’m referring to. After that, have a look at `dump(cos^(10^3))`… Basically, this is a wasteful representation which allocates 10^3 objects with pointers between them and to the same underlying `cos` function.

A better approach would then be to create a specific type to represent iterated functions. See for instance [this gist](https://gist.github.com/FedericoStra/401469b50f2213dc2b37432a64cceba4).

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 10, 2021, 3:39pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/101 "2021-04-10T15:39:28Z")

</div>

> [@FedericoStra](#):
>
> What exactly is _unsafe_ about it?

It doesn’t check for negative `n` and will go into an infinite loop.

Since this little example annoys you so much, I’ll just delete it.

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [April 10, 2021, 3:56pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/102 "2021-04-10T15:56:45Z")

</div>

Please don’t do it. It has its good and bad sides, but it is fine small snippet, and there are people who like it.

---

<div class="post-metadata">

### Author: ![FedericoStra](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)
#### Post date: [April 10, 2021, 4:10pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/103 "2021-04-10T16:10:56Z")

</div>

> [@DNF](#):
>
> Since this little example annoys you so much, I’ll just delete it.

Please please please don’t do it! It doesn’t annoy me, I’m not trying to fight or anything like that. I just wanted to point out the efficiency aspect of it. It can be instructional to understand compile-time/run-time, creation of nested objects with complicated types, etc…

I hope you didn’t take any offence from my posts, and if so I apologize. I wish you a nice weekend 🙂

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [April 11, 2021, 7:57am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/104 "2021-04-11T07:57:53Z")

</div>

PS: Anyone interested in camouflage and digital camouflage? I find this interesting from a statistical perspective (you want to match the spectrum of the background) and the mathematical, almost fractal properties of good camouflage.

 ![camoscaleinvanature](https://global.discourse-cdn.com/julialang/original/3X/4/0/409e0410304a91da28f5267a3d0981b8661a6cfb.jpeg)

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [April 12, 2021, 6:38am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/105 "2021-04-12T06:38:13Z")

</div>

I used to work with camouflage in cephalopods. It’s a relatively complex field with a lot of interesting opinions… I haven’t published anything recently in the field, and I’m sure a lot of new stuff came along since, but you’d need to catch up on things to make sure you’re not redoing stuff that’s already been published. If you like I can put you in touch with some of the experts in the field.

It’s a cool problem that involves:

1. detection versus recognition (being “invisible” versus being unrecognizable)
2. what can the camouflager output in terms of (as you put it) spectrum (how varied is your display) (spatially, chromatically, temporally, etc)
3. the composition of the background you’ll be viewed on (again, spatially, chromatically, temporally)
4. what can the visual systems you’re hiding from resolve (yet again, spatially, chromatically, temporally)?

You can see how this becomes very complicated very fast when you consider that everything changes with time of day, day of year, behavior (being motionless, mimicking leaves moving in the wind), and more. The whole distinction between being detected and being recognized makes everything a lot more complicated too, because you can be perfectly detectable (e.g. bright flamboyant colors) but utterly unrecognizable.

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [April 12, 2021, 7:56am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/106 "2021-04-12T07:56:32Z")

</div>

I see that here the biologist kicks in and thinks about a catalogue of everything that is out there in nature… I am quite aware of that, but I want to think a bit like a physicist here and impose stronger invariance and symmetry on the problem: e.g. statistical spatial stationarity, scale invariance, or conformal invariance as above. More on Zulip: [Zulip](https://julialang.zulipchat.com/#narrow/stream/225582-random/topic/Camouflage/near/234025423)

---

<div class="post-metadata">

### Author: ![FedericoStra](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)
#### Post date: [April 12, 2021, 8:48am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/107 "2021-04-12T08:48:56Z")

</div>

This isn’t anything out of this world, but it showcases Julia’s ability to represent expressions and to easily perform arbitrary precision arithmetic with GMP/MPFR.

**Continued fractions:**

```julia
function contfrac(x::Real, n::Integer)
    n < 1 && return Int[]
    fpart, ipart = modf(x)
    fpart == 0 ? [Int(ipart)] : [Int(ipart); contfrac(1/fpart, n-1)]
end

foldr((x, y) -> :($x + 1 / $y), contfrac(big(π), 25)) |> println
# 3 + 1 / (7 + 1 / (15 + 1 / (1 + 1 / (292 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (2 + 1 / (1 + 1 / (3 + 1 / (1 + 1 / (14 + 1 / (3 + 1 / (3 + 1 / (23 + 1 / (1 + 1 / (1 + 1 / (7 + 1 / (4 + 1 / (35 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / 2)))))))))))))))))))))))

foldr((x, y) -> x + 1 // big(y), contfrac(big(π), 75))
# 13926567982265799805873939967043853503//4432964269365850039583559427823398144

```

If you put the resulting string

```julia
foldr((x,y) -> "$x+\\frac1{$y}", contfrac(big(π), 15)) |> println
# 3+\frac1{7+\frac1{15+...}}

```

in a LaTeX cell you get

3+\frac1{7+\frac1{15+\frac1{1+\frac1{292+\frac1{1+\frac1{1+\frac1{1+\frac1{2+\frac1{1+\frac1{3+\frac1{1+\frac1{14+\frac1{2+\frac1{1}}}}}}}}}}}}}}

> **Addendum**
>
> Of course in general an iterative implementation such as
> 
> ```julia
> function contfrac(x::Real, n::Integer)
> cf = Int[]
> for _ in 1:n
> fpart, ipart = modf(x)
> push!(cf, Int(ipart))
> x = 1 / fpart
> end
> cf
> end
> 
> ```
> 
> would be preferred, because it reduces allocations and avoids stack overflows. This was however a bit too long for the seven lines restriction. Notice that in the case of continued fractions, to actually overflow the stack while performing _meaningful_ computations requires working with a quite large precision. The default precision 256 of `BigFloat` allows to correctly compute only the first 76 terms of the continued fraction of `π`.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [April 12, 2021, 9:54am UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/108 "2021-04-12T09:54:31Z")

</div>

Well you _could_ also use a reduce operation 🙂

```julia
contfrac(x, n) = foldl(1:n, init=(Int[], x)) do (l, r), _
       f, i = modf(r)
       return [l; Int(i)], 1/f
end |> first

julia> contfrac(π, 5)
5-element Vector{Int64}:
   3
   7
  15
   1
 292

```

This could go on one line too (sort of):

```julia
contfrac(x, n) = foldl(((l, r), _) -> let (f, i) = modf(r); ([l; Int(i)], 1/f) end, 1:n, init=(Int[], x))[1]

julia> contfrac(π, 5)
5-element Vector{Int64}:
   3
   7
  15
   1
 292

```

---

<div class="post-metadata">

### Author: ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)
#### Post date: [April 12, 2021, 1:11pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/109 "2021-04-12T13:11:19Z")

</div>

Very nice 😄 Back in the day when I needed continued fractions, I also added `\struct`s to the TeX code, so that the numbers didn’t keep getting smaller.

**Edit:** It was very politely pointed out to me that it’s _de rigeur_ in this thread to provide code. Although it’s not only Julia that lets you do this, I really like the swap semantics in Julia. The following is code that computes a random permutation of an input vector in six lines:

```julia
function random_permutation(a)
    for m = 1:length(a)-1
        l = m+rand(0:length(a)-m)
        a[l],a[m]=a[m],a[l]
    end
end

```

As part of a project where I’m porting some FORTRAN (FORTRAN IV!) code to Julia, I really like that Julia is not only much more succinct but also much more general.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [June 29, 2021, 11:09pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/110 "2021-06-29T23:09:48Z")

</div>

[Dilbert](https://en.wikipedia.org/wiki/Dilbert) comic strips do not need introduction:

```julia
using Gumbo, HTTP, FileIO, Dates, Plots; gr()

function dilbert(t::Date = today() - Day(1))
    r = HTTP.get("https://dilbert.com/strip/"*string(t))
    r_parsed = parsehtml(String(r.body))
    webaddress = r_parsed.root[1][26].attributes["content"]
    download(webaddress) |> load |> x -> display(plot(x,dpi=300, ticks=nothing))
end
dilbert(s::String) = let t = rand(Date(1990,1,1):Day(1):today() - Day(1)); dilbert(t); println(t) end

```

Then call: `dilbert()`

 ![dilbert_yesterday](https://global.discourse-cdn.com/julialang/original/3X/9/a/9aae2c9a81aa4d2a1f80b9ad1b617406ebbf227f.png)

Or: `dilbert(Date(2021,06,26))`

 ![dilbert_20210626](https://global.discourse-cdn.com/julialang/original/3X/3/d/3d29f9862421e926fcc3acc6baaa24ffc9224b22.png)

Or better, do repeat calls to: `dilbert("rand")`

 ![dilbert_rand](https://global.discourse-cdn.com/julialang/original/3X/1/6/16dd7ab520d40657a4d94aa31c6925bcf71ce02c.jpeg)

_ **NB:** edited random function, thanks to @Syx_Pek_

---

<div class="post-metadata">

### Author: ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)
#### Post date: [June 30, 2021, 10:39pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/111 "2021-06-30T22:39:37Z")

</div>

I recommend the following improvement to the last line.

```julia
dilbert(s::String) = let t = rand(Date(1990,1,1):Day(1):today() - Day(1)); dilbert(t); println(t) end

```

---

<div class="post-metadata">

### Author: ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)
#### Post date: [July 2, 2021, 7:47pm UTC](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/112 "2021-07-02T19:47:21Z")

</div>

[Kuramoto model](https://en.wikipedia.org/wiki/Kuramoto_model) (ten lines)

Kuramoto model is a mathematical model of synchronization. Consider oscillators with random periods. (In tems of the following code, the random periods are denoted by 2\pi/\omega[i] and \omega[i]'s are generated by Normal(1, 2).) The model describes the synchronization of the oscillators:

- If the strength K of synchronization is less than a certain critical value K\_c, then no synchronization will occur.

- Synchronization begins when the strength K of synchronization exceeds the ctitical value K\_c.

The critical value K\_c is determined by the probability distribution dist of \omega[i]'s: K\_c = π/2/pdf(dist, mean(dist)).

The video below looks like fireflies are synchronizing their rhythmic flashes.

```julia
a = 1.3; tmax = 25.0; using Distributions, DifferentialEquations, Plots
kuramoto!(dθ, θ, param, t) = (N = length(θ); (K, ω) = param; 
    for i in 1:N dθ[i] = ω[i] + K*mean(sin(θ[j] - θ[i]) for j in 1:N) end)
(m, n) = (32, 16); (d, v) = (Normal(0, 2), 1.0); K_c = 2/π/pdf(d, 0)
θ₀ = 2π*rand(m, n); tspan = (0.0, tmax); K = a*K_c; ω = rand(d, m, n) .+ v
sol = solve(ODEProblem(kuramoto!, θ₀, tspan, (K, ω)))
anim = @animate for t in [fill(0, 20); 0:0.1:tmax; fill(tmax, 20)]
    plot(size=(400, 220), title="Kuramoto model K = $(a)K_c: t = $t", titlefontsize=10)
    heatmap!(sin.(sol(t))'; c=:bwr, colorbar=false, frame=false, ticks=false)
end; gif(anim, "kuramoto$(a).gif")

```

Result  
 ![kuramoto1.3](https://global.discourse-cdn.com/julialang/original/3X/a/5/a5c1ca709eee8a3e642ecec34454ce089c0ab44c.gif)

For the readability, expand the above compressed code, in an appropriate environment, by

````julia
quote
a = 1.3; tmax = 25.0; using Distributions, DifferentialEquations, Plots
kuramoto!(dθ, θ, param, t) = (N = length(θ); (K, ω) = param; 
    for i in 1:N dθ[i] = ω[i] + K*mean(sin(θ[j] - θ[i]) for j in 1:N) end)
(m, n) = (32, 16); (d, v) = (Normal(0, 2), 1.0); K_c = 2/π/pdf(d, 0)
θ₀ = 2π*rand(m, n); tspan = (0.0, tmax); K = a*K_c; ω = rand(d, m, n) .+ v
sol = solve(ODEProblem(kuramoto!, θ₀, tspan, (K, ω)))
anim = @animate for t in [fill(0, 20); 0:0.1:tmax; fill(tmax, 20)]
    plot(size=(400, 220), title="Kuramoto model K = $(a)K_c: t = $t", titlefontsize=10)
    heatmap!(sin.(sol(t))'; c=:bwr, colorbar=false, frame=false, ticks=false)
end; gif(anim, "kuramoto$(a).gif")
end |> Base.remove_linenums! |> x -> display("text/markdown", "```julia\n$x\n```")

````

Expanded Code

```julia
begin
    a = 1.3
    tmax = 25.0
    using Distributions, DifferentialEquations, Plots
    kuramoto!(dθ, θ, param, t) = begin
            N = length(θ)
            (K, ω) = param
            for i = 1:N
                dθ[i] = ω[i] + K * mean((sin(θ[j] - θ[i]) for j = 1:N))
            end
        end
    (m, n) = (32, 16)
    (d, v) = (Normal(0, 2), 1.0)
    K_c = (2 / π) / pdf(d, 0)
    θ₀ = (2π) * rand(m, n)
    tspan = (0.0, tmax)
    K = a * K_c
    ω = rand(d, m, n) .+ v
    sol = solve(ODEProblem(kuramoto!, θ₀, tspan, (K, ω)))
    anim = @animate(for t = [fill(0, 20); 0:0.1:tmax; fill(tmax, 20)]
                plot(size = (400, 220), title = "Kuramoto model K = $(a)K_c: t = $(t)", titlefontsize = 10)
                heatmap!((sin.(sol(t)))'; c = :bwr, colorbar = false, frame = false, ticks = false)
            end)
    gif(anim, "kuramoto$(a).gif")
end

```

[Previous page](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416.md?page=4)

[Next page](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416.md?page=6)
