Rhythm
Five rhythms that come from a piece of math. Each one has its formula, the few lines of Python that turn it into a pattern, and a picture whose cursor moves with the sound. 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 ])
One algorithm, a rhythm and a scale
Euclid's algorithm for the greatest common divisor, run on hits and steps , spreads
\(k\) hits over \(n\) steps as evenly as it can. Up to a rotation, hit number \(i\) lands on step
\[
h_i = \left\lfloor \frac{i \cdot n}{k} \right\rfloor, \qquad i = 0, 1, \dots, k - 1
\]
Godfried Toussaint showed in 2005 that this
generates a large family of traditional rhythms ;
it is the "bd(3,8)" of eddy's mini-notation. Music theorists had met the same shapes in
pitch: Clough and Douthett's maximally even sets
(1991) are the scales that spread their notes as evenly as possible over the 12 semitones.
So one function gives both. Spread 7 over 12 and read the slots as time: a West African
bell pattern. Read the same slots as semitones: a diatonic scale. Below you hear both at
once, the scale climbing in its own rhythm, while the hand goes round the circle once
per bar.
euclid_hits%20%3D%20mo.ui.slider(1%2C%2011%2C%20value%3D7%2C%20label%3D%22hits%20out%20of%2012%22%2C%20show_value%3DTrue)%0Aeuclid_player%20%3D%20Player()%0Amo.vstack(%5Beuclid_hits%2C%20mo.ui.anywidget(euclid_player)%5D)
Show the code 3 lines euclid_hits = mo . ui . slider ( 1 , 11 , value = 7 , label = "hits out of 12" , show_value = True )
euclid_player = Player ()
mo . vstack ([ euclid_hits , mo . ui . anywidget ( euclid_player )])
C D♭ D E♭ E F F♯ G A♭ A B♭ B C D E♭ F G A♭ B♭ 7 of 12 , as even as they can be:
x . x x . x . x x . x .
Clockwise from the top, the circle is one bar of 12 steps. It is also one octave of 12 semitones, and read that way the dots are a diatonic scale (the shape of the piano's white keys).
_bits%20%3D%20bjorklund(euclid_hits.value%2C%2012)%20%20%23%207%20hits%3A%20%5B1%2C%200%2C%201%2C%201%2C%200%2C%201%2C%200%2C%201%2C%201%2C%200%2C%201%2C%200%5D%0A%0A%23%20the%2012%20slots%20as%20time%3A%20a%20bell%20pattern%0A_bell%20%3D%20hits(_bits%2C%20%22cb%22).gain(0.6)%0A%23%20the%2012%20slots%20as%20semitones%3A%20a%20scale%2C%20each%20note%20played%20at%20its%20own%20slot%0A_climb%20%3D%20note(steps(%5Bi%20if%20bit%20else%20None%20for%20i%2C%20bit%20in%20enumerate(_bits)%5D)).s(%22pluck%22)%0A%0A_names%20%3D%20%5B%22C%22%2C%20%22D%E2%99%AD%22%2C%20%22D%22%2C%20%22E%E2%99%AD%22%2C%20%22E%22%2C%20%22F%22%2C%20%22F%E2%99%AF%22%2C%20%22G%22%2C%20%22A%E2%99%AD%22%2C%20%22A%22%2C%20%22B%E2%99%AD%22%2C%20%22B%22%5D%0A_picture%20%3D%20ring(%5B(i%20%2F%2012%2C%20%22s1%22%2C%20_names%5Bi%5D)%20for%20i%2C%20bit%20in%20enumerate(_bits)%20if%20bit%5D%2C%20_names%2C%20polygon%3DTrue%2C%20hand%3D1)%0Aeuclid_player.load(render(stack(_bell%2C%20_climb%2C%20s(%22bd*4%22).gain(0.8))%2C%20cycles%3D2%2C%20cps%3D0.5)%2C%20_picture)%0A%0A_shapes%20%3D%20%7B%0A%20%20%20%201%3A%20%22a%20single%20note%22%2C%0A%20%20%20%202%3A%20%22a%20tritone%22%2C%0A%20%20%20%203%3A%20%22an%20augmented%20triad%22%2C%0A%20%20%20%204%3A%20%22a%20diminished%20seventh%20chord%22%2C%0A%20%20%20%205%3A%20%22a%20pentatonic%20scale%20(the%20shape%20of%20the%20piano's%20black%20keys)%22%2C%0A%20%20%20%206%3A%20%22the%20whole-tone%20scale%22%2C%0A%20%20%20%207%3A%20%22a%20diatonic%20scale%20(the%20shape%20of%20the%20piano's%20white%20keys)%22%2C%0A%20%20%20%208%3A%20%22the%20octatonic%20scale%22%2C%0A%20%20%20%209%3A%20%22Messiaen's%20third%20mode%22%2C%0A%20%20%20%2010%3A%20%22Messiaen's%20seventh%20mode%22%2C%0A%20%20%20%2011%3A%20%22the%20chromatic%20scale%20with%20one%20note%20missing%22%2C%0A%7D%0Amo.hstack(%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%20f%22**%7Beuclid_hits.value%7D%20of%2012**%2C%20as%20even%20as%20they%20can%20be%3A%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%60%7B'%20'.join('x'%20if%20bit%20else%20'.'%20for%20bit%20in%20_bits)%7D%60%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Clockwise%20from%20the%20top%2C%20the%20circle%20is%20one%20bar%20of%2012%20steps.%20It%20is%20also%20one%20octave%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22of%2012%20semitones%2C%20and%20read%20that%20way%20the%20dots%20are%20%7B_shapes%5Beuclid_hits.value%5D%7D.%22%0A%20%20%20%20%20%20%20%20).style(%7B%22max-width%22%3A%20%22440px%22%7D)%2C%0A%20%20%20%20%5D%2C%0A%20%20%20%20justify%3D%22start%22%2C%0A%20%20%20%20align%3D%22center%22%2C%0A%20%20%20%20gap%3D2%2C%0A)
Show the code 38 lines _bits = bjorklund ( euclid_hits . value , 12 ) # 7 hits: [1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0]
# the 12 slots as time: a bell pattern
_bell = hits ( _bits , "cb" ) . gain ( 0.6 )
# the 12 slots as semitones: a scale, each note played at its own slot
_climb = note ( steps ([ i if bit else None for i , bit in enumerate ( _bits )])) . s ( "pluck" )
_names = [ "C" , "D♭" , "D" , "E♭" , "E" , "F" , "F♯" , "G" , "A♭" , "A" , "B♭" , "B" ]
_picture = ring ([( i / 12 , "s1" , _names [ i ]) for i , bit in enumerate ( _bits ) if bit ], _names , polygon = True , hand = 1 )
euclid_player . load ( render ( stack ( _bell , _climb , s ( "bd*4" ) . gain ( 0.8 )), cycles = 2 , cps = 0.5 ), _picture )
_shapes = {
1 : "a single note" ,
2 : "a tritone" ,
3 : "an augmented triad" ,
4 : "a diminished seventh chord" ,
5 : "a pentatonic scale (the shape of the piano's black keys)" ,
6 : "the whole-tone scale" ,
7 : "a diatonic scale (the shape of the piano's white keys)" ,
8 : "the octatonic scale" ,
9 : "Messiaen's third mode" ,
10 : "Messiaen's seventh mode" ,
11 : "the chromatic scale with one note missing" ,
}
mo . hstack (
[
_picture ,
mo . md (
f "** { euclid_hits . value } of 12**, as even as they can be: \n\n "
f "` { ' ' . join ( 'x' if bit else '.' for bit in _bits ) } ` \n\n "
f "Clockwise from the top, the circle is one bar of 12 steps. It is also one octave "
f "of 12 semitones, and read that way the dots are { _shapes [ euclid_hits . value ] } ."
) . style ({ "max-width" : "440px" }),
],
justify = "start" ,
align = "center" ,
gap = 2 ,
)
Counting rhythms: Fibonacci before Fibonacci
How many rhythms fill \(n\) beats, if a syllable is either short (1 beat) or long (2 beats)?
Indian scholars of poetry counted them. The idea goes back to
Pingala 's treatise on Sanskrit metre, and
Virahāṅka and Hemachandra (about 1150) spelled out the rule: a rhythm of \(n\) beats is a
rhythm of \(n - 1\) beats plus a short syllable, or one of \(n - 2\) beats plus a long one.
\[
R(n) = R(n - 1) + R(n - 2), \qquad R(1) = 1, \quad R(2) = 2
\]
So each count is the sum of the two before it, half a century before Fibonacci's rabbits.
Rachel Hall's Math for Poets and Drummers tells the story.
The recursion below is that rule, word for word. You hear every rhythm in turn, one row
per cycle.
meter_beats%20%3D%20mo.ui.slider(2%2C%206%2C%20value%3D5%2C%20label%3D%22beats%22%2C%20show_value%3DTrue%2C%20debounce%3DTrue)%0Ameter_player%20%3D%20Player()%0Amo.vstack(%5Bmeter_beats%2C%20mo.ui.anywidget(meter_player)%5D)
Show the code 3 lines meter_beats = mo . ui . slider ( 2 , 6 , value = 5 , label = "beats" , show_value = True , debounce = True )
meter_player = Player ()
mo . vstack ([ meter_beats , mo . ui . anywidget ( meter_player )])
long, 2 beats short, 1 beat
5 beats can be filled in 8 ways. For 1, 2, 3, … beats the counts are 1, 2, 3, 5, 8, 13, 21, 34, …
def%20meters(beats)%3A%0A%20%20%20%20%22%22%22Every%20way%20to%20fill%20%60beats%60%20with%20short%20(1)%20and%20long%20(2)%20syllables.%22%22%22%0A%20%20%20%20if%20beats%20%3C%3D%201%3A%0A%20%20%20%20%20%20%20%20return%20%5B%5B1%5D%20*%20beats%5D%0A%20%20%20%20return%20%5B%5B1%2C%20*rest%5D%20for%20rest%20in%20meters(beats%20-%201)%5D%20%2B%20%5B%5B2%2C%20*rest%5D%20for%20rest%20in%20meters(beats%20-%202)%5D%0A%0A%0A_all%20%3D%20meters(meter_beats.value)%0A%0A%23%20one%20rhythm%20per%20cycle%3A%20a%20low%20tom%20for%20a%20long%20syllable%2C%20a%20high%20tom%20for%20a%20short%20one%0A_bars%20%3D%20%5Btimecat(*%5B(length%2C%20s(%22lt%22%20if%20length%20%3D%3D%202%20else%20%22ht%22))%20for%20length%20in%20meter%5D)%20for%20meter%20in%20_all%5D%0A%0A_picture%20%3D%20blocks(%0A%20%20%20%20%5B%5B(length%2C%20%22s1%22%20if%20length%20%3D%3D%202%20else%20%22s2%22)%20for%20length%20in%20meter%5D%20for%20meter%20in%20_all%5D%2C%0A%20%20%20%20key%3D%5B(%22s1%22%2C%20%22long%2C%202%20beats%22)%2C%20(%22s2%22%2C%20%22short%2C%201%20beat%22)%5D%2C%0A%20%20%20%20times%3D%5B(row%2C%20row%20%2B%201)%20for%20row%20in%20range(len(_all))%5D%2C%20%20%23%20row%20k%20sounds%20during%20cycle%20k%0A)%0Ameter_player.load(render(slowcat(*_bars)%2C%20cycles%3Dlen(_all)%2C%20cps%3D4%20%2F%20meter_beats.value)%2C%20_picture)%0A%0A_counts%20%3D%20%5Blen(meters(beats))%20for%20beats%20in%20range(1%2C%209)%5D%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%20f%22**%7Bmeter_beats.value%7D%20beats%20can%20be%20filled%20in%20%7Blen(_all)%7D%20ways.**%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22For%201%2C%202%2C%203%2C%20%E2%80%A6%20beats%20the%20counts%20are%20%7B'%2C%20'.join(map(str%2C%20_counts))%7D%2C%20%E2%80%A6%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D%0A)
Show the code 29 lines def meters ( beats ):
"""Every way to fill `beats` with short (1) and long (2) syllables."""
if beats <= 1 :
return [[ 1 ] * beats ]
return [[ 1 , * rest ] for rest in meters ( beats - 1 )] + [[ 2 , * rest ] for rest in meters ( beats - 2 )]
_all = meters ( meter_beats . value )
# one rhythm per cycle: a low tom for a long syllable, a high tom for a short one
_bars = [ timecat ( * [( length , s ( "lt" if length == 2 else "ht" )) for length in meter ]) for meter in _all ]
_picture = blocks (
[[( length , "s1" if length == 2 else "s2" ) for length in meter ] for meter in _all ],
key = [( "s1" , "long, 2 beats" ), ( "s2" , "short, 1 beat" )],
times = [( row , row + 1 ) for row in range ( len ( _all ))], # row k sounds during cycle k
)
meter_player . load ( render ( slowcat ( * _bars ), cycles = len ( _all ), cps = 4 / meter_beats . value ), _picture )
_counts = [ len ( meters ( beats )) for beats in range ( 1 , 9 )]
mo . vstack (
[
_picture ,
mo . md (
f "** { meter_beats . value } beats can be filled in { len ( _all ) } ways.** "
f "For 1, 2, 3, … beats the counts are { ', ' . join ( map ( str , _counts )) } , …"
),
]
)
The Rhythmicon: harmony, slowed down
The composer Henry Cowell argued in New Musical Resources (1930) that rhythm and harmony
are the same ratios at different speeds: "three against two" is a perfect fifth (3:2)
slowed down until you can count it. In 1931 Leon Theremin built him the
Rhythmicon , often called the first drum
machine. Its keys play the harmonic series of one note, and the \(k\) -th harmonic pulses
\(k\) times per cycle:
\[
f_k = k \cdot f_1, \qquad \text{pitch}_k = 12 \log_2 k \ \text{semitones above the first}, \qquad
\text{pulse}_k = k \ \text{per cycle}
\]
In eddy that is one list comprehension. Note that the pitches are not whole numbers of
semitones.
harmonics%20%3D%20mo.ui.slider(1%2C%2012%2C%20value%3D6%2C%20label%3D%22harmonics%22%2C%20show_value%3DTrue)%0Arhythmicon_player%20%3D%20Player()%0Amo.vstack(%5Bharmonics%2C%20mo.ui.anywidget(rhythmicon_player)%5D)
Show the code 3 lines harmonics = mo . ui . slider ( 1 , 12 , value = 6 , label = "harmonics" , show_value = True )
rhythmicon_player = Player ()
mo . vstack ([ harmonics , mo . ui . anywidget ( rhythmicon_player )])
1× harmonic 1: 1 times per cycle 2× harmonic 2: 2 times per cycle harmonic 2: 2 times per cycle 3× harmonic 3: 3 times per cycle harmonic 3: 3 times per cycle harmonic 3: 3 times per cycle 4× harmonic 4: 4 times per cycle harmonic 4: 4 times per cycle harmonic 4: 4 times per cycle harmonic 4: 4 times per cycle 5× harmonic 5: 5 times per cycle harmonic 5: 5 times per cycle harmonic 5: 5 times per cycle harmonic 5: 5 times per cycle harmonic 5: 5 times per cycle 6× harmonic 6: 6 times per cycle harmonic 6: 6 times per cycle harmonic 6: 6 times per cycle harmonic 6: 6 times per cycle harmonic 6: 6 times per cycle harmonic 6: 6 times per cycle
Any two rows are a polyrhythm and, as pitches, an interval: 2 and 1 an octave, 3 and 2 a fifth, 4 and 3 a fourth, 5 and 4 a major third.
_voices%20%3D%20%5B%0A%20%20%20%20note(12%20*%20math.log2(k)%20-%2012)%20%20%23%20harmonic%20k%20of%20a%20low%20C%20...%0A%20%20%20%20.s(%22pluck%22)%0A%20%20%20%20.fast(k)%20%20%23%20...%20k%20times%20per%20cycle%0A%20%20%20%20.gain(1%20%2F%20math.sqrt(k))%0A%20%20%20%20.pan(0.2%20%2B%200.6%20*%20(k%20%25%202))%0A%20%20%20%20for%20k%20in%20range(1%2C%20harmonics.value%20%2B%201)%0A%5D%0A%0A_picture%20%3D%20blocks(%0A%20%20%20%20%5B%5B(1%20%2F%20k%2C%20%22s1%22%2C%20f%22harmonic%20%7Bk%7D%3A%20%7Bk%7D%20times%20per%20cycle%22)%5D%20*%20k%20for%20k%20in%20range(1%2C%20harmonics.value%20%2B%201)%5D%2C%0A%20%20%20%20labels%3D%5Bf%22%7Bk%7D%C3%97%22%20for%20k%20in%20range(1%2C%20harmonics.value%20%2B%201)%5D%2C%0A%20%20%20%20unit%3D540%2C%0A%20%20%20%20times%3D%5B(0%2C%201)%5D%20*%20harmonics.value%2C%20%20%23%20every%20row%20spans%20the%20one%20cycle%0A)%0Arhythmicon_player.load(render(stack(*_voices)%2C%20cycles%3D1%2C%20cps%3D0.25)%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%22Any%20two%20rows%20are%20a%20polyrhythm%20and%2C%20as%20pitches%2C%20an%20interval%3A%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%222%20and%201%20an%20octave%2C%203%20and%202%20a%20fifth%2C%204%20and%203%20a%20fourth%2C%205%20and%204%20a%20major%20third.%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D%0A)
Show the code 26 lines _voices = [
note ( 12 * math . log2 ( k ) - 12 ) # harmonic k of a low C ...
. s ( "pluck" )
. fast ( k ) # ... k times per cycle
. gain ( 1 / math . sqrt ( k ))
. pan ( 0.2 + 0.6 * ( k % 2 ))
for k in range ( 1 , harmonics . value + 1 )
]
_picture = blocks (
[[( 1 / k , "s1" , f "harmonic { k } : { k } times per cycle" )] * k for k in range ( 1 , harmonics . value + 1 )],
labels = [ f " { k } ×" for k in range ( 1 , harmonics . value + 1 )],
unit = 540 ,
times = [( 0 , 1 )] * harmonics . value , # every row spans the one cycle
)
rhythmicon_player . load ( render ( stack ( * _voices ), cycles = 1 , cps = 0.25 ), _picture )
mo . vstack (
[
_picture ,
mo . md (
"Any two rows are a polyrhythm and, as pitches, an interval: "
"2 and 1 an octave, 3 and 2 a fifth, 4 and 3 a fourth, 5 and 4 a major third."
),
]
)
Clapping Music: a rhythm against its own rotations
Steve Reich's Clapping Music (1972) is
one pattern \(a\) of 12 steps. One performer claps it for the whole piece. The other claps
the same pattern, but every few bars jumps one step ahead, until twelve jumps later the
two are together again. After \(k\) jumps the second part, and the number of claps that
land together, are
\[
b_k(t) = a\big((t + k) \bmod 12\big), \qquad
\text{together}(k) = \sum_{t=0}^{11} a(t)\, a\big((t + k) \bmod 12\big)
\]
The second part is just the list rotated by \(k\) . No rotation equals the original, so all
twelve bars sound different. Here each shift lasts one bar (Reich asks for eight or
twelve).
shift 0 shift 1 shift 2 shift 3 shift 4 shift 5 shift 6 shift 7 shift 8 shift 9 shift 10 shift 11 shift 12 both clap only the first performer only the second
Claps that land together, bar by bar: 8, 4, 5, 6, 5, 6, 4, 6, 5, 6, 5, 4, 8.
clap%20%3D%20%5B1%2C%201%2C%201%2C%200%2C%201%2C%201%2C%200%2C%201%2C%200%2C%201%2C%201%2C%200%5D%0Ashifted%20%3D%20%5Bclap%5Bk%20%25%2012%20%3A%5D%20%2B%20clap%5B%3A%20k%20%25%2012%5D%20for%20k%20in%20range(13)%5D%20%20%23%20the%20second%20performer%2C%20bar%20by%20bar%0A%0A_piece%20%3D%20slowcat(*%5Bstack(hits(clap%2C%20%22cp%22).pan(0.2)%2C%20hits(row%2C%20%22cp%3A1%22).pan(0.8))%20for%20row%20in%20shifted%5D)%0A%0A_colour%20%3D%20%7B(1%2C%201)%3A%20%22ink%22%2C%20(1%2C%200)%3A%20%22s1%22%2C%20(0%2C%201)%3A%20%22s2%22%2C%20(0%2C%200)%3A%20None%7D%0A_picture%20%3D%20blocks(%0A%20%20%20%20%5B%5B(1%2C%20_colour%5Bpair%5D)%20for%20pair%20in%20zip(clap%2C%20row)%5D%20for%20row%20in%20shifted%5D%2C%0A%20%20%20%20labels%3D%5Bf%22shift%20%7Bk%7D%22%20for%20k%20in%20range(13)%5D%2C%0A%20%20%20%20key%3D%5B(%22ink%22%2C%20%22both%20clap%22)%2C%20(%22s1%22%2C%20%22only%20the%20first%20performer%22)%2C%20(%22s2%22%2C%20%22only%20the%20second%22)%5D%2C%0A%20%20%20%20times%3D%5B(k%2C%20k%20%2B%201)%20for%20k%20in%20range(13)%5D%2C%0A)%0A_together%20%3D%20%5Bsum(a%20%26%20b%20for%20a%2C%20b%20in%20zip(clap%2C%20row))%20for%20row%20in%20shifted%5D%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(f%22Claps%20that%20land%20together%2C%20bar%20by%20bar%3A%20%7B'%2C%20'.join(map(str%2C%20_together))%7D.%22)%2C%0A%20%20%20%20%20%20%20%20play(_piece%2C%20cycles%3D13%2C%20cps%3D0.5%2C%20picture%3D_picture)%2C%0A%20%20%20%20%5D%0A)
Show the code 20 lines clap = [ 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 1 , 0 ]
shifted = [ clap [ k % 12 :] + clap [: k % 12 ] for k in range ( 13 )] # the second performer, bar by bar
_piece = slowcat ( * [ stack ( hits ( clap , "cp" ) . pan ( 0.2 ), hits ( row , "cp:1" ) . pan ( 0.8 )) for row in shifted ])
_colour = {( 1 , 1 ): "ink" , ( 1 , 0 ): "s1" , ( 0 , 1 ): "s2" , ( 0 , 0 ): None }
_picture = blocks (
[[( 1 , _colour [ pair ]) for pair in zip ( clap , row )] for row in shifted ],
labels = [ f "shift { k } " for k in range ( 13 )],
key = [( "ink" , "both clap" ), ( "s1" , "only the first performer" ), ( "s2" , "only the second" )],
times = [( k , k + 1 ) for k in range ( 13 )],
)
_together = [ sum ( a & b for a , b in zip ( clap , row )) for row in shifted ]
mo . vstack (
[
_picture ,
mo . md ( f "Claps that land together, bar by bar: { ', ' . join ( map ( str , _together )) } ." ),
play ( _piece , cycles = 13 , cps = 0.5 , picture = _picture ),
]
)
Canons that tile time
A rhythmic canon is one motif \(A\) played by several voices that enter at the times in a
set \(B\) . It tiles when every step of the loop is hit by exactly one voice: no gaps and
no collisions. In symbols, the loop of 12 steps splits into a direct sum,
\[
A \oplus B = \mathbb{Z}_{12}: \quad \text{every step } t \text{ is } a + b \pmod{12}
\text{ for exactly one pair } a \in A,\ b \in B
\]
The mathematician Dan Tudor Vuza worked out the theory in 1991, and Tom Johnson composed
with it, as he describes in
Tiling in My Music .
Python can simply try every set of entry times. Pick a motif: the voices come in one by
one, each on its own note, and together they fill all 12 steps.
canon_motif%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%22x%20x%20.%20.%20.%20x%22%2C%0A%20%20%20%20%20%20%20%20%22x%20.%20x%20.%20.%20.%20.%20x%22%2C%0A%20%20%20%20%20%20%20%20%22x%20.%20.%20.%20.%20x%20.%20x%22%2C%0A%20%20%20%20%20%20%20%20%22x%20.%20x%20.%20x%22%2C%0A%20%20%20%20%20%20%20%20%22x%20x%20.%20x%20.%20.%20x%22%2C%0A%20%20%20%20%20%20%20%20%22x%20.%20x%20.%20.%20x%20.%20x%22%2C%0A%20%20%20%20%20%20%20%20%22x%20x%20.%20.%20.%20.%20x%20x%22%2C%0A%20%20%20%20%5D%2C%0A%20%20%20%20value%3D%22x%20x%20.%20.%20.%20x%22%2C%0A%20%20%20%20label%3D%22motif%22%2C%0A)%0Acanon_player%20%3D%20Player()%0Amo.vstack(%5Bcanon_motif%2C%20mo.ui.anywidget(canon_player)%5D)
Show the code 15 lines canon_motif = mo . ui . dropdown (
[
"x x . . . x" ,
"x . x . . . . x" ,
"x . . . . x . x" ,
"x . x . x" ,
"x x . x . . x" ,
"x . x . . x . x" ,
"x x . . . . x x" ,
],
value = "x x . . . x" ,
label = "motif" ,
)
canon_player = Player ()
mo . vstack ([ canon_motif , mo . ui . anywidget ( canon_player )])
voice 1, enters at 0 voice 2, enters at 3 voice 3, enters at 6 voice 4, enters at 9 together
1 way(s) to tile 12 steps with this motif; this is the first. A canon in which neither the motif nor the entries repeat within the loop needs at least 72 steps .
def%20tilings(motif%2C%20size)%3A%0A%20%20%20%20%22%22%22Every%20set%20of%20entry%20times%20at%20which%20copies%20of%20%60motif%60%20hit%20each%20step%20of%20the%20loop%20exactly%20once.%22%22%22%0A%20%20%20%20found%20%3D%20%5B%5D%0A%20%20%20%20for%20later%20in%20combinations(range(1%2C%20size)%2C%20size%20%2F%2F%20len(motif)%20-%201)%3A%0A%20%20%20%20%20%20%20%20entries%20%3D%20(0%2C%20*later)%0A%20%20%20%20%20%20%20%20if%20sorted((entry%20%2B%20step)%20%25%20size%20for%20entry%20in%20entries%20for%20step%20in%20motif)%20%3D%3D%20list(range(size))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20found.append(entries)%0A%20%20%20%20return%20found%0A%0A%0A_motif%20%3D%20%5Bi%20for%20i%2C%20mark%20in%20enumerate(canon_motif.value.split())%20if%20mark%20%3D%3D%20%22x%22%5D%0A_entries%20%3D%20tilings(_motif%2C%2012)%5B0%5D%0A_voices%20%3D%20%5B%5B(step%20-%20entry)%20%25%2012%20in%20_motif%20for%20step%20in%20range(12)%5D%20for%20entry%20in%20_entries%5D%0A%0A%23%20each%20voice%20plays%20the%20motif%20on%20its%20own%20note%20of%20a%20chord%0A_parts%20%3D%20%5B%0A%20%20%20%20note(steps(%5Btone%20if%20on%20else%20None%20for%20on%20in%20voice%5D)).s(%22pluck%22).pan(0.2%20%2B%200.2%20*%20v)%0A%20%20%20%20for%20v%2C%20(voice%2C%20tone)%20in%20enumerate(zip(_voices%2C%20%5B-12%2C%203%2C%207%2C%2010%5D))%0A%5D%0A%23%20the%20voices%20enter%20one%20by%20one%2C%20then%20stay%20together%0A_canon%20%3D%20slowcat(*%5Bstack(*_parts%5B%3A%20k%20%2B%201%5D)%20for%20k%20in%20range(len(_parts))%5D%2C%20stack(*_parts)%2C%20stack(*_parts))%0A_cycles%20%3D%20len(_parts)%20%2B%202%0A%0A_owner%20%3D%20%5Bnext(v%20for%20v%2C%20voice%20in%20enumerate(_voices)%20if%20voice%5Bstep%5D)%20for%20step%20in%20range(12)%5D%0A_picture%20%3D%20blocks(%0A%20%20%20%20%5B%5B(1%2C%20f%22s%7Bv%20%2B%201%7D%22%20if%20on%20else%20None)%20for%20on%20in%20voice%5D%20for%20v%2C%20voice%20in%20enumerate(_voices)%5D%0A%20%20%20%20%2B%20%5B%5B(1%2C%20f%22s%7Bv%20%2B%201%7D%22)%20for%20v%20in%20_owner%5D%5D%2C%0A%20%20%20%20labels%3D%5Bf%22voice%20%7Bv%20%2B%201%7D%2C%20enters%20at%20%7Bentry%7D%22%20for%20v%2C%20entry%20in%20enumerate(_entries)%5D%20%2B%20%5B%22together%22%5D%2C%0A%20%20%20%20%23%20voice%20v%20joins%20in%20cycle%20v%3B%20the%20last%20row%20lights%20up%20once%20everyone%20is%20in%0A%20%20%20%20times%3D%5B%5B(c%2C%20c%20%2B%201)%20for%20c%20in%20range(v%2C%20_cycles)%5D%20for%20v%20in%20%5B*range(len(_parts))%2C%20len(_parts)%20-%201%5D%5D%2C%0A)%0Acanon_player.load(render(_canon%2C%20cycles%3D_cycles%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%20f%22%7Blen(tilings(_motif%2C%2012))%7D%20way(s)%20to%20tile%2012%20steps%20with%20this%20motif%3B%20this%20is%20the%20first.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22A%20canon%20in%20which%20neither%20the%20motif%20nor%20the%20entries%20repeat%20within%20the%20loop%20needs%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%5Bat%20least%2072%20steps%5D(https%3A%2F%2Farxiv.org%2Fabs%2F1304.6609).%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D%0A)
Show the code 43 lines def tilings ( motif , size ):
"""Every set of entry times at which copies of `motif` hit each step of the loop exactly once."""
found = []
for later in combinations ( range ( 1 , size ), size // len ( motif ) - 1 ):
entries = ( 0 , * later )
if sorted (( entry + step ) % size for entry in entries for step in motif ) == list ( range ( size )):
found . append ( entries )
return found
_motif = [ i for i , mark in enumerate ( canon_motif . value . split ()) if mark == "x" ]
_entries = tilings ( _motif , 12 )[ 0 ]
_voices = [[( step - entry ) % 12 in _motif for step in range ( 12 )] for entry in _entries ]
# each voice plays the motif on its own note of a chord
_parts = [
note ( steps ([ tone if on else None for on in voice ])) . s ( "pluck" ) . pan ( 0.2 + 0.2 * v )
for v , ( voice , tone ) in enumerate ( zip ( _voices , [ - 12 , 3 , 7 , 10 ]))
]
# the voices enter one by one, then stay together
_canon = slowcat ( * [ stack ( * _parts [: k + 1 ]) for k in range ( len ( _parts ))], stack ( * _parts ), stack ( * _parts ))
_cycles = len ( _parts ) + 2
_owner = [ next ( v for v , voice in enumerate ( _voices ) if voice [ step ]) for step in range ( 12 )]
_picture = blocks (
[[( 1 , f "s { v + 1 } " if on else None ) for on in voice ] for v , voice in enumerate ( _voices )]
+ [[( 1 , f "s { v + 1 } " ) for v in _owner ]],
labels = [ f "voice { v + 1 } , enters at { entry } " for v , entry in enumerate ( _entries )] + [ "together" ],
# voice v joins in cycle v; the last row lights up once everyone is in
times = [[( c , c + 1 ) for c in range ( v , _cycles )] for v in [ * range ( len ( _parts )), len ( _parts ) - 1 ]],
)
canon_player . load ( render ( _canon , cycles = _cycles , cps = 0.5 ), _picture )
mo . vstack (
[
_picture ,
mo . md (
f " { len ( tilings ( _motif , 12 )) } way(s) to tile 12 steps with this motif; this is the first. "
"A canon in which neither the motif nor the entries repeat within the loop needs "
"[at least 72 steps](https://arxiv.org/abs/1304.6609)."
),
]
)
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