Chaos and noise
Melodies from dynamical systems and from noise: the logistic map, a picture-making map of the plane, and the statistics of music itself. Press ▶ Play and watch the picture. Then move the controls: the sound changes on the next loop. Each example's code is folded away under it: open Show the code to read or change it.
import%20math%0Afrom%20itertools%20import%20combinations%0A%0Aimport%20marimo%20as%20mo%0Aimport%20numpy%20as%20np%0Afrom%20eddy%20import%20n%2C%20note%2C%20s%2C%20scale%2C%20sequence%2C%20silence%2C%20slowcat%2C%20stack%2C%20timecat%0Afrom%20eddy.euclid%20import%20bjorklund%0Afrom%20eddy.marimo%20import%20Picture%2C%20Player%2C%20cursor%2C%20play%2C%20render%0A%0A%0Adef%20hits(bits%2C%20sound)%3A%0A%20%20%20%20%22%22%22A%20row%20of%200s%20and%201s%20as%20a%20rhythm%3A%20%60sound%60%20on%20every%201%2C%20a%20rest%20on%20every%200.%22%22%22%0A%20%20%20%20return%20s(%22%20%22.join(sound%20if%20bit%20else%20%22~%22%20for%20bit%20in%20bits))%0A%0A%0Adef%20steps(values)%3A%0A%20%20%20%20%22%22%22A%20list%20of%20numbers%20as%20equal%20steps%2C%20for%20%60note(...)%60%20or%20%60scale(...)%60.%20None%20is%20a%20rest.%22%22%22%0A%20%20%20%20return%20sequence(*%5B%22~%22%20if%20value%20is%20None%20else%20value%20for%20value%20in%20values%5D)
Show the setup code 18 lines import math
from itertools import combinations
import marimo as mo
import numpy as np
from eddy import n , note , s , scale , sequence , silence , slowcat , stack , timecat
from eddy.euclid import bjorklund
from eddy.marimo import Picture , Player , cursor , play , render
def hits ( bits , sound ):
"""A row of 0s and 1s as a rhythm: `sound` on every 1, a rest on every 0."""
return s ( " " . join ( sound if bit else "~" for bit in bits ))
def steps ( values ):
"""A list of numbers as equal steps, for `note(...)` or `scale(...)`. None is a rest."""
return sequence ( * [ "~" if value is None else value for value in values ])
Chaos you can dial in
The logistic map takes a number \(x\) between 0 and 1 and replaces it, over and over, with
\[
x_{n+1} = r \, x_n \, (1 - x_n)
\]
For small \(r\) the numbers settle on one value. Turn \(r\) up and they alternate between two
values, then four, then eight, and beyond about 3.57 they never settle at all, apart from
islands of order (try 3.83). Jeff Pressing's
Nonlinear Maps as Generators of Musical Design (1988)
used maps like this to compose.
Each number becomes a note of a scale, so the picture and the melody are the same thing.
Drag the orange line across the plot while it plays (or click anywhere on it): the dots
show the new melody at once, and you hear it from the next loop.
r_picker%20%3D%20mo.ui.anywidget(LogisticPicker(settled%2C%20r%3D3.5))%0Achaos_player%20%3D%20Player()%0Amo.vstack(%5Br_picker%2C%20mo.ui.anywidget(chaos_player)%5D)
Show the code 3 lines r_picker = mo . ui . anywidget ( LogisticPicker ( settled , r = 3.5 ))
chaos_player = Player ()
mo . vstack ([ r_picker , mo . ui . anywidget ( chaos_player )])
_r%20%3D%20np.linspace(2.8%2C%204.0%2C%20240)%0A_x%20%3D%20np.full_like(_r%2C%200.3)%0Afor%20_%20in%20range(400)%3A%20%20%23%20let%20them%20settle%0A%20%20%20%20_x%20%3D%20_r%20*%20_x%20*%20(1%20-%20_x)%0Asettled%20%3D%20%5B%5D%0Afor%20_%20in%20range(48)%3A%20%20%23%20then%20keep%2048%20values%20each%0A%20%20%20%20_x%20%3D%20_r%20*%20_x%20*%20(1%20-%20_x)%0A%20%20%20%20settled%20%2B%3D%20zip(_r.tolist()%2C%20_x.tolist())
Show the code 8 lines _r = np . linspace ( 2.8 , 4.0 , 240 )
_x = np . full_like ( _r , 0.3 )
for _ in range ( 400 ): # let them settle
_x = _r * _x * ( 1 - _x )
settled = []
for _ in range ( 48 ): # then keep 48 values each
_x = _r * _x * ( 1 - _x )
settled += zip ( _r . tolist (), _x . tolist ())
_r%20%3D%20r_picker.value%5B%22r%22%5D%0A_x%2C%20_orbit%20%3D%200.3%2C%20%5B%5D%0Afor%20_%20in%20range(300%20%2B%2032)%3A%0A%20%20%20%20_x%20%3D%20_r%20*%20_x%20*%20(1%20-%20_x)%0A%20%20%20%20_orbit.append(_x)%0A_orbit%20%3D%20_orbit%5B-32%3A%5D%20%20%23%20skip%20the%20run-in%20and%20keep%20where%20it%20ends%20up%0A%0A%23%200..1%20becomes%20three%20octaves%20of%20a%20seven-note%20scale%0A_tune%20%3D%20n(scale(%22dorian%22%2C%20steps(%5Bint(x%20*%2021)%20for%20x%20in%20_orbit%5D))%20-%2012).s(%22pluck%22).slow(2)%0A%0A%23%20the%20cursor%20rings%20each%20dot%20on%20the%20plot%20as%20its%20note%20plays%3A%2016%20notes%20per%20cycle%0Achaos_player.load(%0A%20%20%20%20render(stack(_tune%2C%20s(%22bd*4%22).gain(0.7))%2C%20cycles%3D2%2C%20cps%3D0.5)%2C%0A%20%20%20%20r_picker.widget.picture(_orbit%2C%20each%3D1%20%2F%2016)%2C%0A)%0A%0A_period%20%3D%20next(%0A%20%20%20%20(p%20for%20p%20in%20(1%2C%202%2C%204%2C%208%2C%2016%2C%203%2C%206%2C%2012%2C%205%2C%2010)%20if%20all(abs(a%20-%20b)%20%3C%201e-4%20for%20a%2C%20b%20in%20zip(_orbit%2C%20_orbit%5Bp%3A%5D)))%2C%0A%20%20%20%20None%2C%0A)%0Amo.md(%0A%20%20%20%20f%22**r%20%3D%20%7B_r%3Ag%7D%3A**%20%22%0A%20%20%20%20%2B%20(%0A%20%20%20%20%20%20%20%20%22the%20melody%20never%20repeats.%22%0A%20%20%20%20%20%20%20%20if%20_period%20is%20None%0A%20%20%20%20%20%20%20%20else%20%22one%20note%2C%20over%20and%20over.%22%0A%20%20%20%20%20%20%20%20if%20_period%20%3D%3D%201%0A%20%20%20%20%20%20%20%20else%20f%22the%20melody%20repeats%20every%20%7B_period%7D%20notes.%22%0A%20%20%20%20)%0A%20%20%20%20%2B%20%22%20Grey%3A%20where%20the%20map%20settles%20for%20each%20r.%20Orange%3A%20the%20line%20you%20drag%2C%20and%20the%20notes%20you%20hear.%22%0A)
Show the code 31 lines _r = r_picker . value [ "r" ]
_x , _orbit = 0.3 , []
for _ in range ( 300 + 32 ):
_x = _r * _x * ( 1 - _x )
_orbit . append ( _x )
_orbit = _orbit [ - 32 :] # skip the run-in and keep where it ends up
# 0..1 becomes three octaves of a seven-note scale
_tune = n ( scale ( "dorian" , steps ([ int ( x * 21 ) for x in _orbit ])) - 12 ) . s ( "pluck" ) . slow ( 2 )
# the cursor rings each dot on the plot as its note plays: 16 notes per cycle
chaos_player . load (
render ( stack ( _tune , s ( "bd*4" ) . gain ( 0.7 )), cycles = 2 , cps = 0.5 ),
r_picker . widget . picture ( _orbit , each = 1 / 16 ),
)
_period = next (
( p for p in ( 1 , 2 , 4 , 8 , 16 , 3 , 6 , 12 , 5 , 10 ) if all ( abs ( a - b ) < 1e-4 for a , b in zip ( _orbit , _orbit [ p :]))),
None ,
)
mo . md (
f "**r = { _r : g } :** "
+ (
"the melody never repeats."
if _period is None
else "one note, over and over."
if _period == 1
else f "the melody repeats every { _period } notes."
)
+ " Grey: where the map settles for each r. Orange: the line you drag, and the notes you hear."
)
Orbits on a loop: Simone Conradi's map
The physicist Simone Conradi draws with dynamical systems in Python
(@S_Conradi , notebooks ). One
of the best known of those pictures comes from a map of the plane that fits on one line:
\[
x_{n+1} = \sin\!\big(x_n^2 - y_n^2 + a\big), \qquad y_{n+1} = \cos\!\big(2\,x_n y_n + b\big)
\]
The two arguments are the real and imaginary parts of \((x + iy)^2\) , so this is a cousin of
the map \(z \to z^2 + c\) behind Julia sets. Take thousands of starting points, apply the
map over and over, and mark everywhere they land: the folds and spirals appear by
themselves. Paul Bourke catalogued it
as the Simone attractor .
Let \(a\) and \(b\) drift and the picture breathes. Here they walk once round a circle, one
step per bar,
\[
a_k = a + \rho \cos\frac{2\pi k}{8}, \qquad b_k = b + \rho \sin\frac{2\pi k}{8}, \qquad k = 0, \dots, 7
\]
so after eight bars picture and music are back where they began: a loop.
In every picture one starting point is followed for 16 steps (the orange dots), and those
16 points are the bar's melody. The higher the point, the higher the note; the further
right, the further right in your headphones. Where the path spirals into a bright spot,
the tune settles on one note.
orbit_a%20%3D%20mo.ui.slider(0%2C%206.28%2C%20step%3D0.01%2C%20value%3D5.46%2C%20label%3D%22a%22%2C%20show_value%3DTrue%2C%20debounce%3DTrue)%0Aorbit_b%20%3D%20mo.ui.slider(0%2C%206.28%2C%20step%3D0.01%2C%20value%3D4.55%2C%20label%3D%22b%22%2C%20show_value%3DTrue%2C%20debounce%3DTrue)%0Aorbit_radius%20%3D%20mo.ui.slider(%0A%20%20%20%200%2C%201%2C%20step%3D0.05%2C%20value%3D0.4%2C%20label%3D%22%CF%81%2C%20the%20size%20of%20the%20loop%22%2C%20show_value%3DTrue%2C%20debounce%3DTrue%0A)%0Aorbit_player%20%3D%20Player()%0Amo.vstack(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20mo.hstack(%5Borbit_a%2C%20orbit_b%2C%20orbit_radius%5D%2C%20justify%3D%22start%22%2C%20gap%3D1.5%2C%20wrap%3DTrue)%2C%0A%20%20%20%20%20%20%20%20mo.ui.anywidget(orbit_player)%2C%0A%20%20%20%20%5D%0A)
Show the code 12 lines orbit_a = mo . ui . slider ( 0 , 6.28 , step = 0.01 , value = 5.46 , label = "a" , show_value = True , debounce = True )
orbit_b = mo . ui . slider ( 0 , 6.28 , step = 0.01 , value = 4.55 , label = "b" , show_value = True , debounce = True )
orbit_radius = mo . ui . slider (
0 , 1 , step = 0.05 , value = 0.4 , label = "ρ, the size of the loop" , show_value = True , debounce = True
)
orbit_player = Player ()
mo . vstack (
[
mo . hstack ([ orbit_a , orbit_b , orbit_radius ], justify = "start" , gap = 1.5 , wrap = True ),
mo . ui . anywidget ( orbit_player ),
]
)
picture 1, step 1 picture 1, step 2 picture 1, step 3 picture 1, step 4 picture 1, step 5 picture 1, step 6 picture 1, step 7 picture 1, step 8 picture 1, step 9 picture 1, step 10 picture 1, step 11 picture 1, step 12 picture 1, step 13 picture 1, step 14 picture 1, step 15 picture 1, step 16 1 picture 2, step 1 picture 2, step 2 picture 2, step 3 picture 2, step 4 picture 2, step 5 picture 2, step 6 picture 2, step 7 picture 2, step 8 picture 2, step 9 picture 2, step 10 picture 2, step 11 picture 2, step 12 picture 2, step 13 picture 2, step 14 picture 2, step 15 picture 2, step 16 2 picture 3, step 1 picture 3, step 2 picture 3, step 3 picture 3, step 4 picture 3, step 5 picture 3, step 6 picture 3, step 7 picture 3, step 8 picture 3, step 9 picture 3, step 10 picture 3, step 11 picture 3, step 12 picture 3, step 13 picture 3, step 14 picture 3, step 15 picture 3, step 16 3 picture 4, step 1 picture 4, step 2 picture 4, step 3 picture 4, step 4 picture 4, step 5 picture 4, step 6 picture 4, step 7 picture 4, step 8 picture 4, step 9 picture 4, step 10 picture 4, step 11 picture 4, step 12 picture 4, step 13 picture 4, step 14 picture 4, step 15 picture 4, step 16 4 picture 5, step 1 picture 5, step 2 picture 5, step 3 picture 5, step 4 picture 5, step 5 picture 5, step 6 picture 5, step 7 picture 5, step 8 picture 5, step 9 picture 5, step 10 picture 5, step 11 picture 5, step 12 picture 5, step 13 picture 5, step 14 picture 5, step 15 picture 5, step 16 5 picture 6, step 1 picture 6, step 2 picture 6, step 3 picture 6, step 4 picture 6, step 5 picture 6, step 6 picture 6, step 7 picture 6, step 8 picture 6, step 9 picture 6, step 10 picture 6, step 11 picture 6, step 12 picture 6, step 13 picture 6, step 14 picture 6, step 15 picture 6, step 16 6 picture 7, step 1 picture 7, step 2 picture 7, step 3 picture 7, step 4 picture 7, step 5 picture 7, step 6 picture 7, step 7 picture 7, step 8 picture 7, step 9 picture 7, step 10 picture 7, step 11 picture 7, step 12 picture 7, step 13 picture 7, step 14 picture 7, step 15 picture 7, step 16 7 picture 8, step 1 picture 8, step 2 picture 8, step 3 picture 8, step 4 picture 8, step 5 picture 8, step 6 picture 8, step 7 picture 8, step 8 picture 8, step 9 picture 8, step 10 picture 8, step 11 picture 8, step 12 picture 8, step 13 picture 8, step 14 picture 8, step 15 picture 8, step 16 8 Read the pictures like a comic strip, left to right and top to bottom: one per bar. Blue: where the 30,000 points go. Orange: the one point you hear, 16 steps of it.
def%20conradi(a%2C%20b%2C%20x%2C%20y%2C%20count)%3A%0A%20%20%20%20%22%22%22Simone%20Conradi's%20map%2C%20applied%20%60count%60%20times.%20x%20and%20y%20can%20be%20single%20numbers%20or%20whole%20arrays%20of%20points.%22%22%22%0A%20%20%20%20visited%20%3D%20%5B%5D%0A%20%20%20%20for%20_%20in%20range(count)%3A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20np.sin(x%20*%20x%20-%20y%20*%20y%20%2B%20a)%2C%20np.cos(2%20*%20x%20*%20y%20%2B%20b)%0A%20%20%20%20%20%20%20%20visited.append((x%2C%20y))%0A%20%20%20%20return%20visited%0A%0A%0A%23%20eight%20bars%3A%20(a%2C%20b)%20walks%20once%20round%20a%20circle%2C%20so%20the%20last%20bar%20leads%20back%20into%20the%20first%0A_loop%20%3D%20%5B%0A%20%20%20%20(orbit_a.value%20%2B%20orbit_radius.value%20*%20math.cos(angle)%2C%20orbit_b.value%20%2B%20orbit_radius.value%20*%20math.sin(angle))%0A%20%20%20%20for%20angle%20in%20np.linspace(0%2C%202%20*%20math.pi%2C%208%2C%20endpoint%3DFalse)%0A%5D%0A%0A%23%20the%20pictures%3A%20where%2030%2C000%20random%20starting%20points%20go%20in%2050%20steps%0A_crowd%20%3D%20np.random.default_rng(0).uniform(-1%2C%201%2C%20(2%2C%2030_000))%0A_pictures%20%3D%20%5Bink(conradi(a%2C%20b%2C%20*_crowd%2C%20count%3D50))%20for%20a%2C%20b%20in%20_loop%5D%0A%0A%23%20the%20melodies%3A%20where%20one%20starting%20point%20goes%20in%2016%20steps%0A_paths%20%3D%20%5B%5B(float(x)%2C%20float(y))%20for%20x%2C%20y%20in%20conradi(a%2C%20b%2C%20-0.6%2C%200.8%2C%20count%3D16)%5D%20for%20a%2C%20b%20in%20_loop%5D%0A_tune%20%3D%20slowcat(%0A%20%20%20%20*%5B%0A%20%20%20%20%20%20%20%20n(scale(%22dorian%22%2C%20steps(%5Bround(7%20*%20(y%20%2B%201))%20for%20_%2C%20y%20in%20path%5D))%20-%2012)%20%20%23%20height%20is%20pitch%0A%20%20%20%20%20%20%20%20.s(%22pluck%22)%0A%20%20%20%20%20%20%20%20.pan(steps(%5B(x%20%2B%201)%20%2F%202%20for%20x%2C%20_%20in%20path%5D))%20%20%23%20left%20and%20right%20are%20left%20and%20right%0A%20%20%20%20%20%20%20%20for%20path%20in%20_paths%0A%20%20%20%20%5D%0A).room(0.3)%0A%0A_picture%20%3D%20tiles(_pictures%2C%20_paths%2C%20each%3D1)%20%20%23%20one%20picture%20per%20cycle%0Aorbit_player.load(render(stack(_tune%2C%20s(%22bd*2%22).gain(0.6))%2C%20cycles%3D8%2C%20cps%3D0.5)%2C%20_picture)%0A%0Amo.vstack(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20_picture%2C%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Read%20the%20pictures%20like%20a%20comic%20strip%2C%20left%20to%20right%20and%20top%20to%20bottom%3A%20one%20per%20bar.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Blue%3A%20where%20the%2030%2C000%20points%20go.%20Orange%3A%20the%20one%20point%20you%20hear%2C%2016%20steps%20of%20it.%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D%0A)
Show the code 42 lines def conradi ( a , b , x , y , count ):
"""Simone Conradi's map, applied `count` times. x and y can be single numbers or whole arrays of points."""
visited = []
for _ in range ( count ):
x , y = np . sin ( x * x - y * y + a ), np . cos ( 2 * x * y + b )
visited . append (( x , y ))
return visited
# eight bars: (a, b) walks once round a circle, so the last bar leads back into the first
_loop = [
( orbit_a . value + orbit_radius . value * math . cos ( angle ), orbit_b . value + orbit_radius . value * math . sin ( angle ))
for angle in np . linspace ( 0 , 2 * math . pi , 8 , endpoint = False )
]
# the pictures: where 30,000 random starting points go in 50 steps
_crowd = np . random . default_rng ( 0 ) . uniform ( - 1 , 1 , ( 2 , 30_000 ))
_pictures = [ ink ( conradi ( a , b , * _crowd , count = 50 )) for a , b in _loop ]
# the melodies: where one starting point goes in 16 steps
_paths = [[( float ( x ), float ( y )) for x , y in conradi ( a , b , - 0.6 , 0.8 , count = 16 )] for a , b in _loop ]
_tune = slowcat (
* [
n ( scale ( "dorian" , steps ([ round ( 7 * ( y + 1 )) for _ , y in path ])) - 12 ) # height is pitch
. s ( "pluck" )
. pan ( steps ([( x + 1 ) / 2 for x , _ in path ])) # left and right are left and right
for path in _paths
]
) . room ( 0.3 )
_picture = tiles ( _pictures , _paths , each = 1 ) # one picture per cycle
orbit_player . load ( render ( stack ( _tune , s ( "bd*2" ) . gain ( 0.6 )), cycles = 8 , cps = 0.5 ), _picture )
mo . vstack (
[
_picture ,
mo . md (
"Read the pictures like a comic strip, left to right and top to bottom: one per bar. "
"Blue: where the 30,000 points go. Orange: the one point you hear, 16 steps of it."
),
]
)
Neither random nor predictable: 1/f music
In 1975 Richard Voss and John Clarke measured
how pitch and loudness wander in recordings, from Bach to rock radio, and found the same
statistics everywhere. The power of the wandering at frequency \(f\) falls off as
\[
S(f) \propto \frac{1}{f^{\beta}}: \qquad \beta = 0 \ \text{(white)}, \qquad
\beta = 1 \ \text{(pink, or } 1/f\text{)}, \qquad \beta = 2 \ \text{(brown)}
\]
and music is pink. In white noise every note is a fresh roll of the dice, with no
memory. In brown noise every note is a small step from the last one, with too much
memory. Pink sits in between.
Voss's recipe for pink, which Martin Gardner made famous in Scientific American in 1978,
uses several dice \(d_0, d_1, d_2, \dots\) Die \(d_k\) is re-rolled only every \(2^k\) notes,
and note number \(i\) is their sum,
\[
\text{note}_i = \sum_{k=0}^{4} d_k\big(\lfloor i / 2^k \rfloor\big)
\]
so it has surprises at every time scale. Compare the three.
colour%20%3D%20mo.ui.radio(%5B%22white%22%2C%20%22pink%20(1%2Ff)%22%2C%20%22brown%22%5D%2C%20value%3D%22pink%20(1%2Ff)%22%2C%20inline%3DTrue)%0Areroll%20%3D%20mo.ui.button(value%3D0%2C%20on_click%3Dlambda%20count%3A%20count%20%2B%201%2C%20label%3D%22%F0%9F%8E%B2%20roll%20again%22)%0Anoise_player%20%3D%20Player()%0Amo.vstack(%5Bmo.hstack(%5Bcolour%2C%20reroll%5D%2C%20justify%3D%22start%22%2C%20gap%3D1.5)%2C%20mo.ui.anywidget(noise_player)%5D)
Show the code 4 lines colour = mo . ui . radio ([ "white" , "pink (1/f)" , "brown" ], value = "pink (1/f)" , inline = True )
reroll = mo . ui . button ( value = 0 , on_click = lambda count : count + 1 , label = "🎲 roll again" )
noise_player = Player ()
mo . vstack ([ mo . hstack ([ colour , reroll ], justify = "start" , gap = 1.5 ), mo . ui . anywidget ( noise_player )])
16 2 note 0: 11 note 1: 7 note 2: 4 note 3: 4 note 4: 13 note 5: 11 note 6: 13 note 7: 13 note 8: 12 note 9: 10 note 10: 14 note 11: 10 note 12: 12 note 13: 11 note 14: 13 note 15: 14 note 16: 6 note 17: 6 note 18: 9 note 19: 10 note 20: 4 note 21: 2 note 22: 5 note 23: 7 note 24: 12 note 25: 10 note 26: 14 note 27: 16 note 28: 13 note 29: 13 note 30: 15 note 31: 15 note 32: 10 note 33: 10 note 34: 12 note 35: 9 note 36: 7 note 37: 9 note 38: 8 note 39: 8 note 40: 10 note 41: 9 note 42: 11 note 43: 12 note 44: 10 note 45: 9 note 46: 12 note 47: 12 note 48: 7 note 49: 7 note 50: 9 note 51: 8 note 52: 5 note 53: 8 note 54: 3 note 55: 6 note 56: 7 note 57: 8 note 58: 11 note 59: 10 note 60: 8 note 61: 9 note 62: 10 note 63: 8 64 notes of pink noise, as steps of a pentatonic scale.
def%20white(count%2C%20rng)%3A%0A%20%20%20%20%22%22%22No%20memory%3A%20every%20note%20is%20a%20fresh%20roll.%22%22%22%0A%20%20%20%20return%20%5Bint(value)%20for%20value%20in%20rng.integers(0%2C%2021%2C%20count)%5D%0A%0A%0Adef%20brown(count%2C%20rng)%3A%0A%20%20%20%20%22%22%22Too%20much%20memory%3A%20every%20note%20is%20a%20small%20step%20from%20the%20last.%22%22%22%0A%20%20%20%20level%2C%20notes%20%3D%2010%2C%20%5B%5D%0A%20%20%20%20for%20_%20in%20range(count)%3A%0A%20%20%20%20%20%20%20%20level%20%3D%20min(20%2C%20max(0%2C%20level%20%2B%20int(rng.integers(-2%2C%203))))%0A%20%20%20%20%20%20%20%20notes.append(level)%0A%20%20%20%20return%20notes%0A%0A%0Adef%20pink(count%2C%20rng%2C%20dice%3D5)%3A%0A%20%20%20%20%22%22%22Voss's%20dice%3A%20die%20k%20is%20re-rolled%20every%202**k%20notes%2C%20and%20the%20note%20is%20the%20sum%20of%20the%20dice.%22%22%22%0A%20%20%20%20faces%2C%20notes%20%3D%20%5B0%5D%20*%20dice%2C%20%5B%5D%0A%20%20%20%20for%20i%20in%20range(count)%3A%0A%20%20%20%20%20%20%20%20for%20k%20in%20range(dice)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20i%20%25%202**k%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20faces%5Bk%5D%20%3D%20int(rng.integers(0%2C%205))%0A%20%20%20%20%20%20%20%20notes.append(sum(faces))%0A%20%20%20%20return%20notes%0A%0A%0A_make%20%3D%20%7B%22white%22%3A%20white%2C%20%22pink%20(1%2Ff)%22%3A%20pink%2C%20%22brown%22%3A%20brown%7D%5Bcolour.value%5D%0A_notes%20%3D%20_make(64%2C%20np.random.default_rng(reroll.value))%0A%0A_tune%20%3D%20n(scale(%22majpent%22%2C%20steps(_notes))%20-%2024).s(%22pluck%22).room(0.3).slow(4)%0A_picture%20%3D%20curve(_notes%2C%20span%3D(0%2C%204))%0Anoise_player.load(render(_tune%2C%20cycles%3D4%2C%20cps%3D0.5)%2C%20_picture)%0A%0Amo.vstack(%5B_picture%2C%20mo.md(f%2264%20notes%20of%20%7Bcolour.value.split()%5B0%5D%7D%20noise%2C%20as%20steps%20of%20a%20pentatonic%20scale.%22)%5D)
Show the code 33 lines def white ( count , rng ):
"""No memory: every note is a fresh roll."""
return [ int ( value ) for value in rng . integers ( 0 , 21 , count )]
def brown ( count , rng ):
"""Too much memory: every note is a small step from the last."""
level , notes = 10 , []
for _ in range ( count ):
level = min ( 20 , max ( 0 , level + int ( rng . integers ( - 2 , 3 ))))
notes . append ( level )
return notes
def pink ( count , rng , dice = 5 ):
"""Voss's dice: die k is re-rolled every 2**k notes, and the note is the sum of the dice."""
faces , notes = [ 0 ] * dice , []
for i in range ( count ):
for k in range ( dice ):
if i % 2 ** k == 0 :
faces [ k ] = int ( rng . integers ( 0 , 5 ))
notes . append ( sum ( faces ))
return notes
_make = { "white" : white , "pink (1/f)" : pink , "brown" : brown }[ colour . value ]
_notes = _make ( 64 , np . random . default_rng ( reroll . value ))
_tune = n ( scale ( "majpent" , steps ( _notes )) - 24 ) . s ( "pluck" ) . room ( 0.3 ) . slow ( 4 )
_picture = curve ( _notes , span = ( 0 , 4 ))
noise_player . load ( render ( _tune , cycles = 4 , cps = 0.5 ), _picture )
mo . vstack ([ _picture , mo . md ( f "64 notes of { colour . value . split ()[ 0 ] } noise, as steps of a pentatonic scale." )])
import%20base64%0Aimport%20struct%0Aimport%20uuid%0Aimport%20zlib%0A%0Aimport%20anywidget%0Aimport%20traitlets%0A%0ASTYLE%20%3D%20%22%22%22%3Cstyle%3E%0A.mm%20%7B%20--s1%3A%232a78d6%3B%20--s2%3A%23eb6834%3B%20--s3%3A%231baf7a%3B%20--s4%3A%23eda100%3B%20--ink%3A%230b0b0b%3B%20--ink2%3A%2352514e%3B%0A%20%20--muted%3A%23898781%3B%20--grid%3A%23e1e0d9%3B%20--off%3Argba(11%2C11%2C11%2C.08)%3B%20--surface%3Avar(--background%2C%20%23fff)%3B%0A%20%20--glow%3A%231c5cab%3B%20--panel%3Argba(11%2C11%2C11%2C.05)%3B%20--eddy-cursor%3Avar(--ink)%3B%0A%20%20font%3A12px%2F1.5%20system-ui%2C%20-apple-system%2C%20%22Segoe%20UI%22%2C%20sans-serif%3B%20color%3Avar(--ink2)%3B%20flex%3Anone%3B%20%7D%0A.dark%20.mm%20%7B%20--s1%3A%233987e5%3B%20--s2%3A%23d95926%3B%20--s3%3A%23199e70%3B%20--s4%3A%23c98500%3B%20--ink%3A%23f0efec%3B%20--ink2%3A%23c3c2b7%3B%0A%20%20--grid%3A%232c2c2a%3B%20--off%3Argba(255%2C255%2C255%2C.1)%3B%20--surface%3Avar(--background%2C%20%231a1a19)%3B%0A%20%20--glow%3A%239ec5f4%3B%20--panel%3Argba(0%2C0%2C0%2C.45)%3B%20%7D%0A.mm%20svg%20%7B%20display%3Ablock%3B%20max-width%3A100%25%3B%20height%3Aauto%3B%20overflow%3Avisible%3B%20%7D%0A.mm%20text%20%7B%20fill%3Avar(--ink2)%3B%20font%3A11px%20system-ui%2C%20-apple-system%2C%20%22Segoe%20UI%22%2C%20sans-serif%3B%20%7D%0A.mm-key%20%7B%20display%3Aflex%3B%20flex-wrap%3Awrap%3B%20gap%3A4px%2016px%3B%20margin-top%3A8px%3B%20%7D%0A.mm-key%20i%20%7B%20display%3Ainline-block%3B%20width%3A10px%3B%20height%3A10px%3B%20border-radius%3A2px%3B%20margin-right%3A6px%3B%20%7D%0A%3C%2Fstyle%3E%22%22%22%0A%0A%0Adef%20figure(body%2C%20key%3DNone%2C%20marks%3D())%3A%0A%20%20%20%20%22%22%22Wrap%20a%20drawing%20in%20the%20shared%20styles%20and%20return%20it%20as%20an%20eddy%20Picture%2C%20with%20its%20cursor%20%60marks%60.%0A%20%20%20%20%60key%60%20is%20a%20legend%3A%20a%20list%20of%20(colour%2C%20label).%22%22%22%0A%20%20%20%20legend%20%3D%20%22%22%0A%20%20%20%20if%20key%3A%0A%20%20%20%20%20%20%20%20swatches%20%3D%20%22%22.join(f%22%3Cspan%3E%3Ci%20style%3D'background%3Avar(--%7Bc%7D)'%3E%3C%2Fi%3E%7Blabel%7D%3C%2Fspan%3E%22%20for%20c%2C%20label%20in%20key)%0A%20%20%20%20%20%20%20%20legend%20%3D%20f%22%3Cdiv%20class%3D'mm-key'%3E%7Bswatches%7D%3C%2Fdiv%3E%22%0A%20%20%20%20return%20Picture(f%22%7BSTYLE%7D%3Cdiv%20class%3D'mm'%3E%7Bbody%7D%7Blegend%7D%3C%2Fdiv%3E%22%2C%20marks)%0A%0A%0Adef%20blocks(rows%2C%20labels%3DNone%2C%20unit%3DNone%2C%20width%3D600%2C%20key%3DNone%2C%20times%3DNone%2C%20every%3DNone)%3A%0A%20%20%20%20%22%22%22Rows%20of%20blocks%20laid%20end%20to%20end.%20A%20block%20is%20(length%2C%20colour)%20or%20(length%2C%20colour%2C%20tooltip)%3B%0A%20%20%20%20colour%20None%20draws%20an%20empty%20slot.%20%60times%60%20gives%20each%20row%20the%20cycles%20during%20which%20it%20sounds%2C%0A%20%20%20%20as%20(from%2C%20to)%20or%20a%20list%20of%20them%3A%20the%20cursor%20crosses%20the%20row%20in%20that%20time.%22%22%22%0A%20%20%20%20total%20%3D%20max(sum(block%5B0%5D%20for%20block%20in%20row)%20for%20row%20in%20rows)%0A%20%20%20%20left%20%3D%200%20if%20labels%20is%20None%20else%2012%20%2B%206.2%20*%20max(len(str(label))%20for%20label%20in%20labels)%0A%20%20%20%20unit%20%3D%20unit%20or%20min(24%2C%20(width%20-%20left)%20%2F%20total)%0A%20%20%20%20parts%2C%20marks%20%3D%20%5B%5D%2C%20%5B%5D%0A%20%20%20%20for%20r%2C%20row%20in%20enumerate(rows)%3A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20left%2C%20r%20*%2018%0A%20%20%20%20%20%20%20%20if%20labels%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bleft%20-%208%3A.0f%7D'%20y%3D'%7By%20%2B%2012%7D'%20text-anchor%3D'end'%3E%7Blabels%5Br%5D%7D%3C%2Ftext%3E%22)%0A%20%20%20%20%20%20%20%20for%20length%2C%20colour%2C%20*tip%20in%20row%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20fill%20%3D%20f%22var(--%7Bcolour%7D)%22%20if%20colour%20else%20%22var(--off)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20title%20%3D%20f%22%3Ctitle%3E%7Btip%5B0%5D%7D%3C%2Ftitle%3E%22%20if%20tip%20else%20%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Crect%20x%3D'%7Bx%20%2B%201%3A.1f%7D'%20y%3D'%7By%20%2B%201%7D'%20width%3D'%7Bmax(length%20*%20unit%20-%202%2C%201)%3A.1f%7D'%20height%3D'14'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22rx%3D'2'%20fill%3D'%7Bfill%7D'%3E%7Btitle%7D%3C%2Frect%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20x%20%2B%3D%20length%20*%20unit%0A%20%20%20%20%20%20%20%20if%20times%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20t0%2C%20t1%20in%20times%5Br%5D%20if%20isinstance(times%5Br%5D%2C%20list)%20else%20%5Btimes%5Br%5D%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marks.append(cursor.sweep(t0%2C%20t1%2C%20x0%3Dleft%2C%20x1%3Dx%2C%20top%3Dy%20-%201%2C%20bottom%3Dy%20%2B%2017%2C%20every%3Devery))%0A%20%20%20%20w%2C%20h%20%3D%20left%20%2B%20total%20*%20unit%2C%20len(rows)%20*%2018%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bw%3A.0f%7D'%20viewBox%3D'0%200%20%7Bw%3A.0f%7D%20%7Bh%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20key%2C%20marks)%0A%0A%0Adef%20ring(points%2C%20tick_labels%3DNone%2C%20ticks%3D12%2C%20polygon%3DFalse%2C%20size%3D240%2C%20key%3DNone%2C%20hand%3DNone%2C%20lit%3D())%3A%0A%20%20%20%20%22%22%22Dots%20on%20a%20circle.%20A%20point%20is%20(turn%2C%20colour)%20or%20(turn%2C%20colour%2C%20tooltip)%3B%20turn%200%20is%20the%20top%20and%0A%20%20%20%201%20is%20once%20round%2C%20clockwise.%20%60polygon%60%20joins%20the%20dots.%20%60hand%60%20is%20the%20number%20of%20cycles%20in%20which%20a%0A%20%20%20%20clock%20hand%20goes%20round%20once%3B%20%60lit%60%20is%20a%20list%20of%20(from%2C%20to%2C%20turn)%20that%20rings%20a%20spot%20while%20it%20sounds.%22%22%22%0A%20%20%20%20centre%2C%20radius%20%3D%20size%20%2F%202%2C%20size%20%2F%202%20-%2036%0A%0A%20%20%20%20def%20at(turn%2C%20r%3Dradius)%3A%0A%20%20%20%20%20%20%20%20angle%20%3D%202%20*%20math.pi%20*%20turn%0A%20%20%20%20%20%20%20%20return%20centre%20%2B%20r%20*%20math.sin(angle)%2C%20centre%20-%20r%20*%20math.cos(angle)%0A%0A%20%20%20%20parts%20%3D%20%5Bf%22%3Ccircle%20cx%3D'%7Bcentre%7D'%20cy%3D'%7Bcentre%7D'%20r%3D'%7Bradius%7D'%20fill%3D'none'%20stroke%3D'var(--grid)'%2F%3E%22%5D%0A%20%20%20%20for%20tick%20in%20range(ticks)%3A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20at(tick%20%2F%20ticks)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ccircle%20cx%3D'%7Bx%3A.1f%7D'%20cy%3D'%7By%3A.1f%7D'%20r%3D'2'%20fill%3D'var(--muted)'%2F%3E%22)%0A%20%20%20%20%20%20%20%20if%20tick_labels%20and%20tick_labels%5Btick%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20at(tick%20%2F%20ticks%2C%20radius%20%2B%2020)%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bx%3A.1f%7D'%20y%3D'%7By%20%2B%204%3A.1f%7D'%20text-anchor%3D'middle'%3E%7Btick_labels%5Btick%5D%7D%3C%2Ftext%3E%22)%0A%20%20%20%20if%20polygon%20and%20len(points)%20%3E%201%3A%0A%20%20%20%20%20%20%20%20outline%20%3D%20%22%20%22.join(f%22%7Bx%3A.1f%7D%2C%7By%3A.1f%7D%22%20for%20x%2C%20y%20in%20(at(point%5B0%5D)%20for%20point%20in%20points))%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cpolygon%20points%3D'%7Boutline%7D'%20fill%3D'var(--s1)'%20fill-opacity%3D'.1'%20stroke%3D'var(--s1)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke-width%3D'2'%20stroke-linejoin%3D'round'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20for%20turn%2C%20colour%2C%20*tip%20in%20points%3A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20at(turn)%0A%20%20%20%20%20%20%20%20title%20%3D%20f%22%3Ctitle%3E%7Btip%5B0%5D%7D%3C%2Ftitle%3E%22%20if%20tip%20else%20%22%22%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ccircle%20cx%3D'%7Bx%3A.1f%7D'%20cy%3D'%7By%3A.1f%7D'%20r%3D'5'%20fill%3D'var(--%7Bcolour%7D)'%20stroke%3D'var(--surface)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22stroke-width%3D'2'%3E%7Btitle%7D%3C%2Fcircle%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20marks%20%3D%20%5Bcursor.dot(t0%2C%20t1%2C%20*at(turn)%2C%20r%3D9)%20for%20t0%2C%20t1%2C%20turn%20in%20lit%5D%0A%20%20%20%20if%20hand%3A%0A%20%20%20%20%20%20%20%20marks.append(cursor.hand(0%2C%20hand%2C%20cx%3Dcentre%2C%20cy%3Dcentre%2C%20r%3Dradius%20%2B%208%2C%20every%3Dhand))%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bsize%7D'%20viewBox%3D'0%200%20%7Bsize%7D%20%7Bsize%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20key%2C%20marks)%0A%0A%0Adef%20curve(values%2C%20marks%3D()%2C%20width%3D600%2C%20height%3D140%2C%20span%3DNone)%3A%0A%20%20%20%20%22%22%22A%20melody%20as%20a%20stepped%20line%2C%20one%20step%20per%20note.%20%60marks%60%20are%20the%20indexes%20of%20notes%20to%20dot.%0A%20%20%20%20%60span%60%20is%20the%20(from%2C%20to)%20cycles%20in%20which%20the%20cursor%20crosses%20the%20whole%20line.%22%22%22%0A%20%20%20%20low%2C%20high%20%3D%20min(values)%2C%20max(values)%0A%20%20%20%20step%20%3D%20(width%20-%2028)%20%2F%20len(values)%0A%0A%20%20%20%20def%20x(i)%3A%0A%20%20%20%20%20%20%20%20return%2028%20%2B%20i%20*%20step%0A%0A%20%20%20%20def%20y(value)%3A%0A%20%20%20%20%20%20%20%20return%208%20%2B%20(height%20-%2016)%20*%20(high%20-%20value)%20%2F%20((high%20-%20low)%20or%201)%0A%0A%20%20%20%20path%20%3D%20%22%22.join(f%22%7B'L'%20if%20i%20else%20'M'%7D%7Bx(i)%3A.1f%7D%2C%7By(v)%3A.1f%7DH%7Bx(i%20%2B%201)%3A.1f%7D%22%20for%20i%2C%20v%20in%20enumerate(values))%0A%20%20%20%20parts%20%3D%20%5B%0A%20%20%20%20%20%20%20%20f%22%3Ctext%20x%3D'20'%20y%3D'%7By(high)%20%2B%204%3A.0f%7D'%20text-anchor%3D'end'%3E%7Bhigh%7D%3C%2Ftext%3E%22%2C%0A%20%20%20%20%20%20%20%20f%22%3Ctext%20x%3D'20'%20y%3D'%7By(low)%20%2B%204%3A.0f%7D'%20text-anchor%3D'end'%3E%7Blow%7D%3C%2Ftext%3E%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20if%20low%20%3C%200%20%3C%20high%3A%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Cline%20x1%3D'28'%20x2%3D'%7Bwidth%7D'%20y1%3D'%7By(0)%3A.1f%7D'%20y2%3D'%7By(0)%3A.1f%7D'%20stroke%3D'var(--grid)'%2F%3E%22)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'20'%20y%3D'%7By(0)%20%2B%204%3A.0f%7D'%20text-anchor%3D'end'%3E0%3C%2Ftext%3E%22)%0A%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20f%22%3Cpath%20d%3D'%7Bpath%7D'%20fill%3D'none'%20stroke%3D'var(--s1)'%20stroke-width%3D'2'%20stroke-linejoin%3D'round'%20%22%0A%20%20%20%20%20%20%20%20%22stroke-linecap%3D'round'%2F%3E%22%0A%20%20%20%20)%0A%20%20%20%20for%20i%20in%20marks%3A%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ccircle%20cx%3D'%7Bx(i%20%2B%200.5)%3A.1f%7D'%20cy%3D'%7By(values%5Bi%5D)%3A.1f%7D'%20r%3D'4'%20fill%3D'var(--s2)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke%3D'var(--surface)'%20stroke-width%3D'2'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20for%20i%2C%20value%20in%20enumerate(values)%3A%20%20%23%20invisible%20columns%20that%20carry%20the%20tooltips%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Crect%20x%3D'%7Bx(i)%3A.1f%7D'%20y%3D'0'%20width%3D'%7Bstep%3A.1f%7D'%20height%3D'%7Bheight%7D'%20fill%3D'transparent'%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ctitle%3Enote%20%7Bi%7D%3A%20%7Bvalue%7D%3C%2Ftitle%3E%3C%2Frect%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20sweep%20%3D%20%5Bcursor.sweep(*span%2C%20x0%3D28%2C%20x1%3Dwidth%2C%20top%3D0%2C%20bottom%3Dheight)%5D%20if%20span%20else%20%5B%5D%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bwidth%7D'%20viewBox%3D'0%200%20%7Bwidth%7D%20%7Bheight%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20None%2C%20sweep)%0A%0A%0Adef%20cloud(dots%2C%20x_range%2C%20marker%3DNone%2C%20marker_dots%3D()%2C%20ticks%3D()%2C%20width%3D600%2C%20height%3D220%2C%20each%3DNone)%3A%0A%20%20%20%20%22%22%22Many%20small%20dots%20(x%2C%20y)%2C%20with%20y%20from%200%20to%201.%20%60marker%60%20draws%20a%20vertical%20line%20at%20that%20x%2C%0A%20%20%20%20carrying%20the%20bigger%20%60marker_dots%60%20(y%20values).%20%60each%60%20is%20how%20many%20cycles%20each%20of%20those%20lasts%0A%20%20%20%20when%20they%20are%20played%20in%20order.%22%22%22%0A%20%20%20%20x0%2C%20x1%20%3D%20x_range%0A%0A%20%20%20%20def%20px(x)%3A%0A%20%20%20%20%20%20%20%20return%20(x%20-%20x0)%20%2F%20(x1%20-%20x0)%20*%20width%0A%0A%20%20%20%20def%20py(y)%3A%0A%20%20%20%20%20%20%20%20return%206%20%2B%20(1%20-%20y)%20*%20(height%20-%2012)%0A%0A%20%20%20%20seen%20%3D%20sorted(%7B(round(px(x))%2C%20round(py(y)))%20for%20x%2C%20y%20in%20dots%7D)%0A%20%20%20%20parts%20%3D%20%5B%0A%20%20%20%20%20%20%20%20f%22%3Cpath%20d%3D'%7B''.join(f'M%7Bx%7D%2C%7By%7Dh.01'%20for%20x%2C%20y%20in%20seen)%7D'%20fill%3D'none'%20stroke%3D'var(--muted)'%20%22%0A%20%20%20%20%20%20%20%20%22stroke-width%3D'1.6'%20stroke-linecap%3D'round'%2F%3E%22%2C%0A%20%20%20%20%20%20%20%20f%22%3Cline%20x1%3D'0'%20x2%3D'%7Bwidth%7D'%20y1%3D'%7Bheight%7D'%20y2%3D'%7Bheight%7D'%20stroke%3D'var(--grid)'%2F%3E%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20for%20tick%20in%20ticks%3A%0A%20%20%20%20%20%20%20%20anchor%20%3D%20%22start%22%20if%20px(tick)%20%3C%2012%20else%20%22end%22%20if%20px(tick)%20%3E%20width%20-%2012%20else%20%22middle%22%20%20%23%20keep%20edge%20labels%20inside%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bpx(tick)%3A.0f%7D'%20y%3D'%7Bheight%20%2B%2014%7D'%20text-anchor%3D'%7Banchor%7D'%3E%7Btick%3Ag%7D%3C%2Ftext%3E%22)%0A%20%20%20%20marks%20%3D%20%5B%5D%0A%20%20%20%20if%20marker%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cline%20x1%3D'%7Bpx(marker)%3A.1f%7D'%20x2%3D'%7Bpx(marker)%3A.1f%7D'%20y1%3D'0'%20y2%3D'%7Bheight%7D'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke%3D'var(--s2)'%20stroke-width%3D'2'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20value%20in%20sorted(set(round(value%2C%203)%20for%20value%20in%20marker_dots))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ccircle%20cx%3D'%7Bpx(marker)%3A.1f%7D'%20cy%3D'%7Bpy(value)%3A.1f%7D'%20r%3D'4'%20fill%3D'var(--s2)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22stroke%3D'var(--surface)'%20stroke-width%3D'2'%3E%3Ctitle%3Ex%20%3D%20%7Bvalue%3A.3f%7D%3C%2Ftitle%3E%3C%2Fcircle%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20if%20each%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20marks%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cursor.dot(k%20*%20each%2C%20(k%20%2B%201)%20*%20each%2C%20x%3Dpx(marker)%2C%20y%3Dpy(value))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20k%2C%20value%20in%20enumerate(marker_dots)%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bwidth%7D'%20viewBox%3D'0%200%20%7Bwidth%7D%20%7Bheight%20%2B%2018%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20None%2C%20marks)%0A%0A%0Adef%20braid(rows%2C%20lead%3DNone%2C%20width%3D600%2C%20per_cycle%3DNone%2C%20rests%3D0)%3A%0A%20%20%20%20%22%22%22Change-ringing%20rows%20as%20paths%3A%20one%20line%20per%20bell%2C%20rows%20left%20to%20right%2C%20first%20place%20at%20the%20top.%0A%20%20%20%20With%20%60per_cycle%60%20rows%20and%20%60rests%60%20silent%20steps%20in%20each%20cycle%2C%20the%20cursor%20rings%20each%20bell%20as%20it%20sounds.%22%22%22%0A%20%20%20%20step%20%3D%20(width%20-%2036)%20%2F%20(len(rows)%20-%201)%0A%20%20%20%20places%20%3D%20len(rows%5B0%5D)%0A%0A%20%20%20%20def%20x(column)%3A%0A%20%20%20%20%20%20%20%20return%2018%20%2B%20column%20*%20step%0A%0A%20%20%20%20def%20y(place)%3A%0A%20%20%20%20%20%20%20%20return%2024%20%2B%20place%20*%2024%0A%0A%20%20%20%20parts%2C%20marks%20%3D%20%5B%5D%2C%20%5B%5D%0A%20%20%20%20if%20lead%3A%0A%20%20%20%20%20%20%20%20for%20column%20in%20range(0%2C%20len(rows)%20-%201%2C%20lead)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cline%20x1%3D'%7Bx(column)%3A.1f%7D'%20x2%3D'%7Bx(column)%3A.1f%7D'%20y1%3D'14'%20y2%3D'%7By(places%20-%201)%20%2B%2010%7D'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22stroke%3D'var(--grid)'%2F%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bx(column)%20%2B%204%3A.1f%7D'%20y%3D'10'%3Elead%20%7Bcolumn%20%2F%2F%20lead%20%2B%201%7D%3C%2Ftext%3E%22)%0A%20%20%20%20for%20bell%20in%20sorted(rows%5B0%5D)%3A%0A%20%20%20%20%20%20%20%20points%20%3D%20%5B(x(column)%2C%20y(row.index(bell)))%20for%20column%2C%20row%20in%20enumerate(rows)%5D%0A%20%20%20%20%20%20%20%20line%20%3D%20%22%20%22.join(f%22%7Bpx%3A.1f%7D%2C%7Bpy%3A.1f%7D%22%20for%20px%2C%20py%20in%20points)%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cpolyline%20points%3D'%7Bline%7D'%20fill%3D'none'%20stroke%3D'var(--s%7Bbell%7D)'%20stroke-width%3D'2'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke-linejoin%3D'round'%20stroke-linecap%3D'round'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20column%2C%20(px%2C%20py)%20in%20enumerate(points)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ccircle%20cx%3D'%7Bpx%3A.1f%7D'%20cy%3D'%7Bpy%3A.1f%7D'%20r%3D'4'%20fill%3D'var(--s%7Bbell%7D)'%20stroke%3D'var(--surface)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22stroke-width%3D'2'%3E%3Ctitle%3Erow%20%7Bcolumn%20%2B%201%7D%3A%20bell%20%7Bbell%7D%20in%20place%20%7Brows%5Bcolumn%5D.index(bell)%20%2B%201%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%3C%2Ftitle%3E%3C%2Fcircle%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'4'%20y%3D'%7Bpoints%5B0%5D%5B1%5D%20%2B%204%3A.0f%7D'%20text-anchor%3D'middle'%3E%7Bbell%7D%3C%2Ftext%3E%22)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bwidth%20-%204%7D'%20y%3D'%7Bpoints%5B-1%5D%5B1%5D%20%2B%204%3A.0f%7D'%20text-anchor%3D'middle'%3E%7Bbell%7D%3C%2Ftext%3E%22)%0A%20%20%20%20if%20per_cycle%3A%0A%20%20%20%20%20%20%20%20beats%20%3D%20per_cycle%20*%20places%20%2B%20rests%20%20%23%20steps%20in%20one%20cycle%0A%20%20%20%20%20%20%20%20for%20column%20in%20range(len(rows))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20place%20in%20range(places)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20start%20%3D%20column%20%2F%2F%20per_cycle%20%2B%20((column%20%25%20per_cycle)%20*%20places%20%2B%20place)%20%2F%20beats%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marks.append(cursor.dot(start%2C%20start%20%2B%201%20%2F%20beats%2C%20x%3Dx(column)%2C%20y%3Dy(place)))%0A%20%20%20%20h%20%3D%20y(places%20-%201)%20%2B%2016%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bwidth%7D'%20viewBox%3D'0%200%20%7Bwidth%7D%20%7Bh%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20None%2C%20marks)%0A%0A%0Adef%20lattice(triangles%2C%20vertices%2C%20width%3D760%2C%20each%3DNone)%3A%0A%20%20%20%20%22%22%22A%20walk%20on%20a%20triangular%20grid.%20%60triangles%60%20is%20%5B(three%20(i%2C%20j)%20corners%2C%20label)%5D%20in%20walk%20order%20and%0A%20%20%20%20%60vertices%60%20maps%20(i%2C%20j)%20to%20the%20label%20drawn%20at%20that%20grid%20point.%20%60each%60%20is%20how%20many%20cycles%20each%0A%20%20%20%20triangle%20lasts%3A%20the%20cursor%20outlines%20the%20one%20that%20is%20sounding.%22%22%22%0A%0A%20%20%20%20def%20flat(i%2C%20j)%3A%20%20%23%20grid%20point%20to%20the%20plane%3A%2060-degree%20rows%0A%20%20%20%20%20%20%20%20return%20i%20%2B%20j%20%2F%202%2C%20-j%20*%20math.sqrt(3)%20%2F%202%0A%0A%20%20%20%20spots%20%3D%20%5Bflat(*corner)%20for%20corner%20in%20vertices%5D%0A%20%20%20%20x0%2C%20x1%20%3D%20min(x%20for%20x%2C%20_%20in%20spots)%2C%20max(x%20for%20x%2C%20_%20in%20spots)%0A%20%20%20%20y0%2C%20y1%20%3D%20min(y%20for%20_%2C%20y%20in%20spots)%2C%20max(y%20for%20_%2C%20y%20in%20spots)%0A%20%20%20%20unit%20%3D%20min(64%2C%20(width%20-%2036)%20%2F%20(x1%20-%20x0))%0A%0A%20%20%20%20def%20at(i%2C%20j)%3A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20flat(i%2C%20j)%0A%20%20%20%20%20%20%20%20return%2018%20%2B%20(x%20-%20x0)%20*%20unit%2C%2018%20%2B%20(y%20-%20y0)%20*%20unit%0A%0A%20%20%20%20parts%2C%20marks%2C%20seen%20%3D%20%5B%5D%2C%20%5B%5D%2C%20set()%0A%20%20%20%20for%20index%2C%20(three%2C%20label)%20in%20enumerate(triangles)%3A%0A%20%20%20%20%20%20%20%20points%20%3D%20%5Bat(*corner)%20for%20corner%20in%20three%5D%0A%20%20%20%20%20%20%20%20if%20each%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20marks.append(cursor.outline(index%20*%20each%2C%20(index%20%2B%201)%20*%20each%2C%20points))%0A%20%20%20%20%20%20%20%20if%20frozenset(three)%20in%20seen%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20seen.add(frozenset(three))%0A%20%20%20%20%20%20%20%20cx%2C%20cy%20%3D%20sum(x%20for%20x%2C%20_%20in%20points)%20%2F%203%2C%20sum(y%20for%20_%2C%20y%20in%20points)%20%2F%203%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cpolygon%20points%3D'%7B'%20'.join(f'%7Bx%3A.1f%7D%2C%7By%3A.1f%7D'%20for%20x%2C%20y%20in%20points)%7D'%20fill%3D'var(--s1)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22fill-opacity%3D'%7B0.45%20if%20index%20%3D%3D%200%20else%200.14%7D'%20stroke%3D'var(--surface)'%20stroke-width%3D'2'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke-linejoin%3D'round'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ctext%20x%3D'%7Bcx%3A.1f%7D'%20y%3D'%7Bcy%20%2B%204%3A.1f%7D'%20text-anchor%3D'middle'%20style%3D'fill%3Avar(--ink)'%3E%7Blabel%7D%3C%2Ftext%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20for%20corner%2C%20label%20in%20vertices.items()%3A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20at(*corner)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ccircle%20cx%3D'%7Bx%3A.1f%7D'%20cy%3D'%7By%3A.1f%7D'%20r%3D'9'%20fill%3D'var(--surface)'%2F%3E%22)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bx%3A.1f%7D'%20y%3D'%7By%20%2B%204%3A.1f%7D'%20text-anchor%3D'middle'%20style%3D'font-size%3A10px'%3E%7Blabel%7D%3C%2Ftext%3E%22)%0A%20%20%20%20w%2C%20h%20%3D%2036%20%2B%20(x1%20-%20x0)%20*%20unit%2C%2036%20%2B%20(y1%20-%20y0)%20*%20unit%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bw%3A.0f%7D'%20viewBox%3D'0%200%20%7Bw%3A.0f%7D%20%7Bh%3A.0f%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20None%2C%20marks)%0A%0A%0Adef%20ink(visited%2C%20size%3D300)%3A%0A%20%20%20%20%22%22%22Count%20how%20often%20each%20pixel%20of%20the%20square%20from%20-1%20to%201%20is%20visited%2C%20and%20return%20the%20counts%20as%20a%0A%20%20%20%20greyscale%20PNG%20(a%20data%20URI)%3A%20the%20more%20visits%2C%20the%20brighter.%20%60visited%60%20is%20a%20list%20of%20(x%2C%20y)%20arrays.%22%22%22%0A%20%20%20%20counts%20%3D%20np.zeros(size%20*%20size)%0A%20%20%20%20for%20x%2C%20y%20in%20visited%3A%0A%20%20%20%20%20%20%20%20column%20%3D%20np.minimum(((x%20%2B%201)%20%2F%202%20*%20size).astype(int)%2C%20size%20-%201)%0A%20%20%20%20%20%20%20%20row%20%3D%20np.minimum(((1%20-%20y)%20%2F%202%20*%20size).astype(int)%2C%20size%20-%201)%0A%20%20%20%20%20%20%20%20counts%20%2B%3D%20np.bincount(row%20*%20size%20%2B%20column%2C%20minlength%3Dsize%20*%20size)%0A%20%20%20%20counts%20%3D%20np.log1p(counts)%0A%20%20%20%20grey%20%3D%20(255%20*%20(counts%20%2F%20(counts.max()%20or%201))%20**%200.5).astype(np.uint8).reshape(size%2C%20size)%0A%0A%20%20%20%20def%20chunk(tag%2C%20data)%3A%0A%20%20%20%20%20%20%20%20return%20struct.pack(%22%3EI%22%2C%20len(data))%20%2B%20tag%20%2B%20data%20%2B%20struct.pack(%22%3EI%22%2C%20zlib.crc32(tag%20%2B%20data))%0A%0A%20%20%20%20png%20%3D%20(%0A%20%20%20%20%20%20%20%20b%22%5Cx89PNG%5Cr%5Cn%5Cx1a%5Cn%22%0A%20%20%20%20%20%20%20%20%2B%20chunk(b%22IHDR%22%2C%20struct.pack(%22%3EIIBBBBB%22%2C%20size%2C%20size%2C%208%2C%200%2C%200%2C%200%2C%200))%0A%20%20%20%20%20%20%20%20%2B%20chunk(b%22IDAT%22%2C%20zlib.compress(b%22%22.join(b%22%5Cx00%22%20%2B%20row.tobytes()%20for%20row%20in%20grey)%2C%209))%0A%20%20%20%20%20%20%20%20%2B%20chunk(b%22IEND%22%2C%20b%22%22)%0A%20%20%20%20)%0A%20%20%20%20return%20%22data%3Aimage%2Fpng%3Bbase64%2C%22%20%2B%20base64.b64encode(png).decode()%0A%0A%0Adef%20tiles(images%2C%20paths%2C%20columns%3D4%2C%20width%3D760%2C%20gap%3D6%2C%20each%3DNone)%3A%0A%20%20%20%20%22%22%22A%20grid%20of%20square%20pictures%20of%20the%20plane%20from%20-1%20to%201%2C%20each%20with%20a%20path%20of%20dots%20drawn%20over%20it.%0A%20%20%20%20%60images%60%20are%20greyscale%20data%20URIs%2C%20shown%20as%20ink%20in%20the%20theme's%20colour%3B%20%60paths%60%20are%20lists%20of%20(x%2C%20y).%0A%20%20%20%20%60each%60%20is%20how%20many%20cycles%20each%20picture%20lasts%3A%20the%20cursor%20frames%20it%20and%20follows%20its%20path.%22%22%22%0A%20%20%20%20side%20%3D%20(width%20-%20(columns%20-%201)%20*%20gap)%20%2F%20columns%0A%20%20%20%20mask%20%3D%20f%22mm-%7Buuid.uuid4().hex%5B%3A8%5D%7D%22%0A%20%20%20%20parts%2C%20marks%20%3D%20%5B%5D%2C%20%5B%5D%0A%20%20%20%20for%20k%2C%20(image%2C%20path)%20in%20enumerate(zip(images%2C%20paths))%3A%0A%20%20%20%20%20%20%20%20left%2C%20top%20%3D%20(k%20%25%20columns)%20*%20(side%20%2B%20gap)%2C%20(k%20%2F%2F%20columns)%20*%20(side%20%2B%20gap)%0A%20%20%20%20%20%20%20%20points%20%3D%20%5B(left%20%2B%20(x%20%2B%201)%20%2F%202%20*%20side%2C%20top%20%2B%20(1%20-%20y)%20%2F%202%20*%20side)%20for%20x%2C%20y%20in%20path%5D%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Crect%20x%3D'%7Bleft%3A.1f%7D'%20y%3D'%7Btop%3A.1f%7D'%20width%3D'%7Bside%3A.1f%7D'%20height%3D'%7Bside%3A.1f%7D'%20rx%3D'4'%20fill%3D'var(--panel)'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cmask%20id%3D'%7Bmask%7D-%7Bk%7D'%3E%3Cimage%20href%3D'%7Bimage%7D'%20x%3D'%7Bleft%3A.1f%7D'%20y%3D'%7Btop%3A.1f%7D'%20width%3D'%7Bside%3A.1f%7D'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22height%3D'%7Bside%3A.1f%7D'%2F%3E%3C%2Fmask%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Crect%20x%3D'%7Bleft%3A.1f%7D'%20y%3D'%7Btop%3A.1f%7D'%20width%3D'%7Bside%3A.1f%7D'%20height%3D'%7Bside%3A.1f%7D'%20fill%3D'var(--glow)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22mask%3D'url(%23%7Bmask%7D-%7Bk%7D)'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cpolyline%20points%3D'%7B'%20'.join(f'%7Bx%3A.1f%7D%2C%7By%3A.1f%7D'%20for%20x%2C%20y%20in%20points)%7D'%20fill%3D'none'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke%3D'var(--s2)'%20stroke-width%3D'.75'%20stroke-opacity%3D'.3'%20stroke-linejoin%3D'round'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20step%2C%20(x%2C%20y)%20in%20enumerate(points)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ccircle%20cx%3D'%7Bx%3A.1f%7D'%20cy%3D'%7By%3A.1f%7D'%20r%3D'2.5'%20fill%3D'var(--s2)'%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ctitle%3Epicture%20%7Bk%20%2B%201%7D%2C%20step%20%7Bstep%20%2B%201%7D%3C%2Ftitle%3E%3C%2Fcircle%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bleft%20%2B%207%3A.1f%7D'%20y%3D'%7Btop%20%2B%2015%3A.1f%7D'%3E%7Bk%20%2B%201%7D%3C%2Ftext%3E%22)%0A%20%20%20%20%20%20%20%20if%20each%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20box%20%3D%20%5B(left%2C%20top)%2C%20(left%20%2B%20side%2C%20top)%2C%20(left%20%2B%20side%2C%20top%20%2B%20side)%2C%20(left%2C%20top%20%2B%20side)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20marks.append(cursor.outline(k%20*%20each%2C%20(k%20%2B%201)%20*%20each%2C%20box))%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20step%2C%20(x%2C%20y)%20in%20enumerate(points)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20start%20%3D%20(k%20%2B%20step%20%2F%20len(points))%20*%20each%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marks.append(cursor.dot(start%2C%20start%20%2B%20each%20%2F%20len(points)%2C%20x%3Dx%2C%20y%3Dy))%0A%20%20%20%20rows%20%3D%20-(-len(images)%20%2F%2F%20columns)%0A%20%20%20%20h%20%3D%20rows%20*%20side%20%2B%20(rows%20-%201)%20*%20gap%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bwidth%7D'%20viewBox%3D'0%200%20%7Bwidth%7D%20%7Bh%3A.0f%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20None%2C%20marks)%0A%0Aclass%20LogisticPicker(anywidget.AnyWidget)%3A%0A%20%20%20%20%22%22%22The%20logistic%20map's%20bifurcation%20diagram%2C%20with%20an%20orange%20line%20you%20drag%20sideways%20to%20pick%20r.%0A%0A%20%20%20%20The%20line%20and%20the%20orbit's%20dots%20move%20in%20the%20browser%20as%20you%20drag%3B%20%60r%60%20is%20sent%20to%20Python%0A%20%20%20%20when%20you%20let%20go.%20%60picture(orbit%2C%20each)%60%20is%20the%20Picture%20a%20Player%20follows%3A%20its%20cursor%0A%20%20%20%20rings%20the%20orbit's%20dots%20on%20this%20plot%2C%20one%20after%20another.%0A%20%20%20%20%22%22%22%0A%0A%20%20%20%20_esm%20%3D%20r%22%22%22%0A%20%20%20%20function%20render(%7B%20model%2C%20el%20%7D)%20%7B%0A%20%20%20%20%20%20const%20NS%20%3D%20%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3B%0A%20%20%20%20%20%20const%20root%20%3D%20document.createElement(%22div%22)%3B%0A%20%20%20%20%20%20root.className%20%3D%20%22lp%22%3B%0A%20%20%20%20%20%20root.id%20%3D%20model.get(%22target%22)%3B%0A%20%20%20%20%20%20root.innerHTML%20%3D%20model.get(%22html%22)%3B%0A%20%20%20%20%20%20el.appendChild(root)%3B%0A%20%20%20%20%20%20const%20svg%20%3D%20root.querySelector(%22svg%22)%3B%0A%20%20%20%20%20%20const%20%5Blo%2C%20hi%5D%20%3D%20model.get(%22x_range%22)%2C%20%5BrMin%2C%20rMax%2C%20step%5D%20%3D%20model.get(%22limits%22)%3B%0A%20%20%20%20%20%20const%20W%20%3D%20model.get(%22width%22)%2C%20H%20%3D%20model.get(%22height%22)%3B%0A%20%20%20%20%20%20const%20px%20%3D%20r%20%3D%3E%20(r%20-%20lo)%20%2F%20(hi%20-%20lo)%20*%20W%2C%20py%20%3D%20x%20%3D%3E%206%20%2B%20(1%20-%20x)%20*%20(H%20-%2012)%3B%0A%20%20%20%20%20%20const%20layer%20%3D%20document.createElementNS(NS%2C%20%22g%22)%3B%0A%20%20%20%20%20%20svg.appendChild(layer)%3B%0A%20%20%20%20%20%20svg.setAttribute(%22tabindex%22%2C%20%220%22)%3B%0A%20%20%20%20%20%20svg.setAttribute(%22role%22%2C%20%22slider%22)%3B%0A%20%20%20%20%20%20svg.setAttribute(%22aria-label%22%2C%20%22r%3A%20drag%20the%20orange%20line%22)%3B%0A%20%20%20%20%20%20svg.setAttribute(%22aria-valuemin%22%2C%20rMin)%3B%0A%20%20%20%20%20%20svg.setAttribute(%22aria-valuemax%22%2C%20rMax)%3B%0A%0A%20%20%20%20%20%20let%20r%20%3D%20model.get(%22r%22)%2C%20dragging%20%3D%20false%3B%0A%20%20%20%20%20%20function%20orbit(r)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20the%20same%20steps%20as%20in%20Python%0A%20%20%20%20%20%20%20%20let%20x%20%3D%200.3%3B%20const%20out%20%3D%20%5B%5D%3B%0A%20%20%20%20%20%20%20%20for%20(let%20i%20%3D%200%3B%20i%20%3C%20300%20%2B%2032%3B%20i%2B%2B)%20%7B%20x%20%3D%20r%20*%20x%20*%20(1%20-%20x)%3B%20if%20(i%20%3E%3D%20300)%20out.push(x)%3B%20%7D%0A%20%20%20%20%20%20%20%20return%20%5B...new%20Set(out.map(v%20%3D%3E%20v.toFixed(3)))%5D.map(Number)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20function%20draw()%20%7B%0A%20%20%20%20%20%20%20%20const%20x%20%3D%20px(r)%3B%0A%20%20%20%20%20%20%20%20const%20dots%20%3D%20orbit(r).map(v%20%3D%3E%0A%20%20%20%20%20%20%20%20%20%20%60%3Ccircle%20cx%3D%22%24%7Bx%7D%22%20cy%3D%22%24%7Bpy(v)%7D%22%20r%3D%224%22%20fill%3D%22var(--s2)%22%20stroke%3D%22var(--surface)%22%20stroke-width%3D%222%22%2F%3E%60).join(%22%22)%3B%0A%20%20%20%20%20%20%20%20layer.innerHTML%20%3D%0A%20%20%20%20%20%20%20%20%20%20%60%3Cline%20x1%3D%22%24%7Bx%7D%22%20x2%3D%22%24%7Bx%7D%22%20y1%3D%220%22%20y2%3D%22%24%7BH%7D%22%20stroke%3D%22var(--s2)%22%20stroke-width%3D%222%22%2F%3E%60%20%2B%0A%20%20%20%20%20%20%20%20%20%20%60%3Cline%20x1%3D%22%24%7Bx%7D%22%20x2%3D%22%24%7Bx%7D%22%20y1%3D%220%22%20y2%3D%22%24%7BH%7D%22%20stroke%3D%22transparent%22%20stroke-width%3D%2218%22%20style%3D%22cursor%3Aew-resize%22%2F%3E%60%20%2B%0A%20%20%20%20%20%20%20%20%20%20dots%20%2B%0A%20%20%20%20%20%20%20%20%20%20%60%3Crect%20x%3D%22%24%7Bx%20-%2030%7D%22%20y%3D%22-22%22%20width%3D%2260%22%20height%3D%2218%22%20rx%3D%229%22%20fill%3D%22var(--s2)%22%20style%3D%22cursor%3Aew-resize%22%2F%3E%60%20%2B%0A%20%20%20%20%20%20%20%20%20%20%60%3Ctext%20x%3D%22%24%7Bx%7D%22%20y%3D%22-9%22%20text-anchor%3D%22middle%22%20style%3D%22fill%3A%23fff%3Bfont-weight%3A600%22%3Er%20%3D%20%24%7Br.toFixed(3).replace(%2F0%2B%24%2F%2C%20%22%22).replace(%2F%5C.%24%2F%2C%20%22%22)%7D%3C%2Ftext%3E%60%3B%0A%20%20%20%20%20%20%20%20svg.setAttribute(%22aria-valuenow%22%2C%20r)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20const%20clamp%20%3D%20v%20%3D%3E%20Math.min(rMax%2C%20Math.max(rMin%2C%20Math.round(v%20%2F%20step)%20*%20step))%3B%0A%20%20%20%20%20%20function%20fromPointer(e)%20%7B%0A%20%20%20%20%20%20%20%20const%20pt%20%3D%20svg.createSVGPoint()%3B%0A%20%20%20%20%20%20%20%20pt.x%20%3D%20e.clientX%3B%20pt.y%20%3D%20e.clientY%3B%0A%20%20%20%20%20%20%20%20const%20p%20%3D%20pt.matrixTransform(svg.getScreenCTM().inverse())%3B%0A%20%20%20%20%20%20%20%20r%20%3D%20clamp(lo%20%2B%20p.x%20%2F%20W%20*%20(hi%20-%20lo))%3B%0A%20%20%20%20%20%20%20%20draw()%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20function%20commit()%20%7B%0A%20%20%20%20%20%20%20%20if%20(Math.abs(r%20-%20model.get(%22r%22))%20%3E%201e-9)%20%7B%20model.set(%22r%22%2C%20r)%3B%20model.save_changes()%3B%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20svg.addEventListener(%22pointerdown%22%2C%20e%20%3D%3E%20%7B%20dragging%20%3D%20true%3B%20svg.setPointerCapture(e.pointerId)%3B%20fromPointer(e)%3B%20%7D)%3B%0A%20%20%20%20%20%20svg.addEventListener(%22pointermove%22%2C%20e%20%3D%3E%20%7B%20if%20(dragging)%20fromPointer(e)%3B%20%7D)%3B%0A%20%20%20%20%20%20const%20release%20%3D%20()%20%3D%3E%20%7B%20if%20(dragging)%20%7B%20dragging%20%3D%20false%3B%20commit()%3B%20%7D%20%7D%3B%0A%20%20%20%20%20%20svg.addEventListener(%22pointerup%22%2C%20release)%3B%0A%20%20%20%20%20%20svg.addEventListener(%22pointercancel%22%2C%20release)%3B%0A%20%20%20%20%20%20let%20keyTimer%3B%0A%20%20%20%20%20%20svg.addEventListener(%22keydown%22%2C%20e%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20const%20k%20%3D%20%7B%20ArrowLeft%3A%20-1%2C%20ArrowDown%3A%20-1%2C%20ArrowRight%3A%201%2C%20ArrowUp%3A%201%20%7D%5Be.key%5D%3B%0A%20%20%20%20%20%20%20%20if%20(!k)%20return%3B%0A%20%20%20%20%20%20%20%20e.preventDefault()%3B%0A%20%20%20%20%20%20%20%20r%20%3D%20clamp(r%20%2B%20k%20*%20step%20*%20(e.shiftKey%20%3F%2010%20%3A%201))%3B%0A%20%20%20%20%20%20%20%20draw()%3B%0A%20%20%20%20%20%20%20%20clearTimeout(keyTimer)%3B%20keyTimer%20%3D%20setTimeout(commit%2C%20300)%3B%0A%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20model.on(%22change%3Ar%22%2C%20()%20%3D%3E%20%7B%20if%20(!dragging)%20%7B%20r%20%3D%20model.get(%22r%22)%3B%20draw()%3B%20%7D%20%7D)%3B%0A%0A%20%20%20%20%20%20%2F%2F%20light%20or%20dark%2C%20from%20the%20text%20colour%20the%20page%20gives%20this%20widget%0A%20%20%20%20%20%20const%20theme%20%3D%20()%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20const%20m%20%3D%20getComputedStyle(el).color.match(%2F%5B%5Cd.%5D%2B%2Fg)%3B%0A%20%20%20%20%20%20%20%20root.classList.toggle(%22dark%22%2C%20!!m%20%26%26%200.299%20*%20m%5B0%5D%20%2B%200.587%20*%20m%5B1%5D%20%2B%200.114%20*%20m%5B2%5D%20%3E%20140)%3B%0A%20%20%20%20%20%20%7D%3B%0A%20%20%20%20%20%20theme()%3B%0A%20%20%20%20%20%20const%20themeTimer%20%3D%20setInterval(theme%2C%201500)%3B%0A%20%20%20%20%20%20draw()%3B%0A%20%20%20%20%20%20return%20()%20%3D%3E%20clearInterval(themeTimer)%3B%0A%20%20%20%20%7D%0A%20%20%20%20export%20default%20%7B%20render%20%7D%3B%0A%20%20%20%20%22%22%22%0A%20%20%20%20_css%20%3D%20%22%22%22%0A%20%20%20%20.lp%20%7B%20padding-top%3A%2024px%3B%20%7D%0A%20%20%20%20.lp%20svg%20%7B%20touch-action%3A%20none%3B%20cursor%3A%20crosshair%3B%20outline%3A%20none%3B%20%7D%0A%20%20%20%20.lp%20svg%3Afocus-visible%20%7B%20outline%3A%202px%20solid%20var(--s2)%3B%20outline-offset%3A%204px%3B%20border-radius%3A%204px%3B%20%7D%0A%20%20%20%20%22%22%22%0A%20%20%20%20r%20%3D%20traitlets.Float(3.5).tag(sync%3DTrue)%0A%20%20%20%20html%20%3D%20traitlets.Unicode(%22%22).tag(sync%3DTrue)%0A%20%20%20%20target%20%3D%20traitlets.Unicode(%22%22).tag(sync%3DTrue)%0A%20%20%20%20x_range%20%3D%20traitlets.List(%5B2.8%2C%204.0%5D).tag(sync%3DTrue)%0A%20%20%20%20limits%20%3D%20traitlets.List(%5B2.8%2C%203.99%2C%200.005%5D).tag(sync%3DTrue)%0A%20%20%20%20width%20%3D%20traitlets.Float(600).tag(sync%3DTrue)%0A%20%20%20%20height%20%3D%20traitlets.Float(220).tag(sync%3DTrue)%0A%0A%20%20%20%20def%20__init__(self%2C%20settled%2C%20r%3D3.5%2C%20x_range%3D(2.8%2C%204.0)%2C%20width%3D600%2C%20height%3D220)%3A%0A%20%20%20%20%20%20%20%20backdrop%20%3D%20cloud(settled%2C%20x_range%2C%20ticks%3D%5B2.8%2C%203.0%2C%203.2%2C%203.4%2C%203.6%2C%203.8%2C%204.0%5D%2C%20width%3Dwidth%2C%20height%3Dheight)%0A%20%20%20%20%20%20%20%20super().__init__(%0A%20%20%20%20%20%20%20%20%20%20%20%20r%3Dr%2C%20html%3Dbackdrop.html%2C%20target%3Df%22mm-%7Buuid.uuid4().hex%5B%3A10%5D%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x_range%3Dlist(x_range)%2C%20width%3Dwidth%2C%20height%3Dheight%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20def%20picture(self%2C%20orbit%2C%20each)%3A%0A%20%20%20%20%20%20%20%20%22%22%22A%20Picture%20on%20this%20plot%20whose%20cursor%20rings%20each%20value%20of%20%60orbit%60%20in%20turn%2C%20%60each%60%20cycles%20apiece.%22%22%22%0A%20%20%20%20%20%20%20%20(lo%2C%20hi)%2C%20w%2C%20h%20%3D%20self.x_range%2C%20self.width%2C%20self.height%0A%20%20%20%20%20%20%20%20x%20%3D%20(self.r%20-%20lo)%20%2F%20(hi%20-%20lo)%20*%20w%0A%20%20%20%20%20%20%20%20marks%20%3D%20%5Bcursor.dot(k%20*%20each%2C%20(k%20%2B%201)%20*%20each%2C%20x%3Dx%2C%20y%3D6%20%2B%20(1%20-%20v)%20*%20(h%20-%2012))%20for%20k%2C%20v%20in%20enumerate(orbit)%5D%0A%20%20%20%20%20%20%20%20picture%20%3D%20Picture(%22%22%2C%20marks)%0A%20%20%20%20%20%20%20%20picture.id%20%3D%20self.target%20%20%23%20the%20player%20draws%20on%20this%20widget's%20plot%0A%20%20%20%20%20%20%20%20return%20picture