EP49: L-Systems & Algorithmic Composition
Overview / 概述
A tree grows one rule at a time — yet from just two substitution rules, an infinite self-similar structure emerges. This is the central insight of Lindenmayer systems (L-systems): simple parallel string-rewriting rules, iterated repeatedly, produce fractal geometry and — with an appropriate interpretation scheme — fractal musical structure.
中文: “一棵树的生长规则,竟然能’长出’一首赋格?”
L-systems were invented in 1968 by the Hungarian biologist Aristid Lindenmayer to model the development of filamentous plants. The key innovation is parallel rewriting: at each step, every symbol in the string is simultaneously replaced by its expansion. This is in sharp contrast to context-free grammars (EP16), which expand one nonterminal at a time in a sequential derivation. The result of parallel rewriting is that each local piece of the string is a miniature copy of the whole — the hallmark of self-similarity.
The same mathematical machinery that generates the Koch snowflake, the Sierpiński triangle, and the branching patterns of ferns can be mapped, via a turtle interpretation, onto pitch sequences, rhythmic structures, and harmonic branching. This episode develops the formal theory of L-systems, computes their fractal dimension using the Hausdorff-Besicovitch formula, and connects the Fibonacci sequence and the golden angle to natural and musical proportion.
Prerequisites / 前置知识
- Context-Free Grammars and Formal Languages (EP16) — sequential string rewriting, derivation trees, and the distinction between terminal and nonterminal symbols; L-systems are the parallel analogue of CFGs.
Definitions
A deterministic context-free L-system (D0L-system) is a triple
where:
- is a finite alphabet of symbols.
- is the axiom (initial string), a non-empty word over .
- is the production function, assigning to each symbol a replacement string .
The system evolves by parallel rewriting: given current string , the next string is
where juxtaposition denotes string concatenation. The string at step is denoted , with .
Worked example — the Fibonacci L-system. Let , , and production rules , . The first five generations are:
| Step | String | Length |
|---|---|---|
| A | 1 | |
| AB | 2 | |
| ABA | 3 | |
| ABAAB | 5 | |
| ABAABABA | 8 |
The lengths 1, 2, 3, 5, 8, … are consecutive Fibonacci numbers. This is not coincidental — it follows directly from the structure of the rewriting rules.
中文: “你认出来了吗?这是斐波那契数列——每一步的长度是前两步之和。”
A turtle interpretation maps an L-system string over an extended alphabet to a geometric path (or a musical sequence) via the following state machine. The turtle carries a state : position and heading angle .
| Symbol | Geometric action | Musical action |
|---|---|---|
| Move forward by step , drawing a segment | Play one note at current pitch | |
| Turn left by angle | Raise pitch by interval | |
| Turn right by angle | Lower pitch by interval | |
| Push onto stack | Begin a chord branch (push state) | |
| Pop from stack | End chord branch (pop state) |
The stack mechanism (bracket pair ) enables branching structure — the defining feature that allows L-systems to model trees (and polyphonic music) rather than just linear curves.
Worked example — Koch curve. Let , axiom , rule , turning angle . After three iterations the turtle traces the boundary of the Koch snowflake, a fractal with Hausdorff dimension .
中文: “Koch 曲线的规则:F 替换为 F 加 F 减 F 减 F 加 F。迭代三次,得到分形雪花边界。”
For a self-similar set generated by copies of itself, each scaled by factor (the self-similarity ratio), the similarity dimension (which coincides with the Hausdorff dimension for geometrically regular L-system outputs) is
For a line segment: , , giving . For a filled square: , , giving . For the Koch curve: , , giving .
The golden ratio is . The golden angle is
Equivalently, . In phyllotaxis (the arrangement of leaves, seeds, or scales on a plant), each successive element is placed at angular offset from the previous one. The resulting spiral pattern exhibits Fibonacci numbers of arms in adjacent directions.
中文: “这个角度是三百六十度除以黄金比的平方,等价于三百六十度乘以二减黄金比,约等于一百三十七点五度——因为黄金比是’最难被有理数逼近的无理数',按黄金角排列能在所有尺度上避免聚集。”
Main Theorems / 主要定理
Let with , . Denote by and the number of ’s and ’s in , and let be the total length. Then:
so is the -th Fibonacci number. Moreover, .
The key is to track and separately. Under one parallel rewriting step, each produces one and one , while each produces one . Therefore:
It follows that . Since , a simpler path: and (verifiable by induction), so . The ratio is a classical result from the characteristic equation , whose positive root is .
中文: “L 系统是并行重写:每一步同时替换所有字符。并行重写产生了自相似结构——每个局部都是整体的缩小版。这是植物生长的基本机制。”
The Koch curve, generated by the L-system with rule and angle , has Hausdorff dimension
In particular, is strictly between 1 (line) and 2 (plane).
After one application of the rule, each segment of length is replaced by segments each of length (the scaling ratio is because the original segment is divided into thirds, with the middle third replaced by two sides of an equilateral triangle). By the Moran equation for self-similar sets with a single scaling ratio, the similarity dimension satisfies
Under the open set condition (which holds for the Koch curve by geometric inspection), the similarity dimension equals the Hausdorff dimension.
Numerical Examples
Fractal dimension comparison table:
| Structure | L-system rule | copies | Scaling | |
|---|---|---|---|---|
| Line segment | 2 | 1/2 | 1.000 | |
| Koch curve | 4 | 1/3 | 1.262 | |
| Sierpiński triangle | (gasket L-system) | 3 | 1/2 | 1.585 |
| Space-filling curve | Hilbert curve limit | 4 | 1/2 | 2.000 |
Fibonacci L-system string length:
For the rules , , the string length at step is (the -th Fibonacci number):
The ratio converges to exponentially fast: by step , the ratio agrees with to four decimal places.
Golden angle phyllotaxis: In a sunflower with 89 clockwise spirals and 55 counterclockwise spirals, the ratio . With 144 seeds placed at successive golden-angle offsets, covering a disk of radius , the packing density approaches — close to optimal hexagonal packing, achieved without any global coordination.
Musical Connection / 音乐联系
The same rules, grown into sound
The turtle interpretation maps the same L-system alphabet to music: becomes a note event, and become ascending and descending pitch steps (by a chosen interval ), and bracket pairs produce simultaneous harmonic branches (chords or ornaments).
With the Fibonacci rules , and the musical assignment quarter note, eighth note, the resulting rhythm at each generation is a longer version of the pattern at the previous generation — rhythmic self-similarity. The ratio of quarter to eighth notes at step is : the golden ratio emerges in the rhythmic proportion.
中文: “音乐版本:F 是一个音符,加号是升调,减号是降调,方括号是和弦分支。同一规则,生成旋律结构。”
Fractal dimension as musical complexity: The Hausdorff dimension of an L-system’s geometric output provides a heuristic measure of structural complexity for its musical counterpart. A melody generated by a Koch-like rule () has more local variation than a simple ascending scale (), but less global filling than a random walk (). This analogy is heuristic: the direct relationship between and perceived musical complexity has not been established in the music cognition literature.
中文: “L 系统生成的旋律,其自相似程度可以用分形维度来类比量化——维度越高,结构越复杂(这是一种启发性类比,分形维度与感知复杂度的直接关系尚未在音乐认知文献中建立)。”
Golden ratio in musical proportion: The golden ratio appears as a structural proportion in several celebrated compositions. Bartók’s Music for Strings, Percussion and Celesta (1936) has been analyzed (Lendvai 1971) as placing the climactic entry of the fugue subject at the golden section of the movement. Whether Bartók deliberately applied or the effect arises from Fibonacci-structured phrase lengths is debated; the mathematical structure is present regardless of compositional intent.
Dividing a phrase or a form by the golden ratio produces sections of length , so that the shorter section is to the longer as the longer is to the whole — the defining property of :
Connection to EP16 (CFGs): The parallel/sequential distinction (Theorem 49.2) connects directly to
. CFGs model hierarchical musical syntax — phrase structure, cadential patterns — through sequential derivation trees. L-systems extend this to growth processes where all sub-structures develop simultaneously, producing the emergent self-similarity absent in CFG derivations.
中文: “自相似性不是复杂算法的结果,而是简单规则多次迭代的涌现属性。”
Limits and Open Questions / 局限性与开放问题
-
Stochastic and context-sensitive L-systems. The D0L-systems in this episode are deterministic and context-free. Real biological growth involves randomness (stochastic L-systems, SL-systems) and neighbor-dependent rules (context-sensitive L-systems, 2L-systems). The musical analogues — probabilistic melody generation and context-aware harmonization — remain largely unexplored as a unified formal framework.
-
Fractal dimension and music perception. The Hausdorff dimension is a geometric invariant of the L-system’s output curve. Claiming that directly measures perceived musical complexity requires a psychoacoustic bridge that does not yet exist in the literature. Controlled listening experiments comparing melodies of equal length but differing have not been systematically conducted.
-
Turtle interpretation is non-canonical. The mapping from L-system alphabet to musical parameters (which symbol = note, which = pitch shift, what interval , what time unit) is chosen by the composer, not determined by mathematics. Different interpretations of the same L-system produce entirely different musical surfaces. There is no principled theory of which interpretations preserve musically meaningful structure.
-
Inversibility. Given a musical passage, no general algorithm recovers the L-system that generated it, because many L-systems produce the same string at a given iteration and the turtle interpretation is lossy. The inverse problem — L-system inference from a musical surface — is computationally hard (NP-hard in general for string compression problems).
Two melodies generated by different L-systems but with the same Hausdorff dimension are rated as equally “complex” or “ornate” by naive listeners in a forced-choice experiment, controlling for tempo, range, and mode.
Falsification criterion: A controlled listening study presenting pairs of L-system melodies matched on but differing in production rules finds a statistically significant preference for one over the other, or a significant difference in perceived complexity ratings (e.g., on a paired -test). Such a result would demonstrate that is not the relevant perceptual invariant, motivating a search for a dimensionless musical complexity measure beyond fractal geometry.
- Golden ratio in composition: design or coincidence? The appearances of in analyses of Bartók, Debussy, and others (Lendvai 1971; Howat 1983) remain contested. Without access to compositional sketches or explicit composer statements, it is impossible to distinguish deliberate golden-section planning from the analyst’s selection bias (finding where Fibonacci-approximated phrase lengths happen to appear).
Academic References / 参考文献
-
Lindenmayer, A. (1968). Mathematical models for cellular interactions in development, Parts I and II. Journal of Theoretical Biology 18(3), 280–315.
-
Prusinkiewicz, P. & Lindenmayer, A. (1990). The Algorithmic Beauty of Plants. Springer-Verlag, New York. Free PDF: http://algorithmicbotany.org/papers/
-
Prusinkiewicz, P. & Hanan, J. (1989). Lindenmayer Systems, Fractals, and Plants. Lecture Notes in Biomathematics 79. Springer.
-
Mandelbrot, B. B. (1982). The Fractal Geometry of Nature. W.H. Freeman, New York.
-
Falconer, K. (2003). Fractal Geometry: Mathematical Foundations and Applications, 2nd ed. Wiley.
-
Lendvai, E. (1971). Béla Bartók: An Analysis of His Music. Kahn & Averill, London.
-
Howat, R. (1983). Debussy in Proportion: A Musical Analysis. Cambridge University Press.
-
Steinhaus, H. (1950). Sur la division des segments. Colloquium Mathematicae 2, 298–302. (Origin of the three-distance theorem.)
-
Slater, N. B. (1950). The distribution of the integers for which . Proceedings of the Cambridge Philosophical Society 46(4), 525–534.
-
Johnson, T. A. (1996). Applying Klee’s “Pedagogical Sketchbook” to the Analysis of Music. Music Theory Spectrum 18(2), 190–208.
-
Hsü, K. J. & Hsü, A. J. (1990). Fractal geometry of music. Proceedings of the National Academy of Sciences 87(3), 938–941.
-
Brothers, H. J. (2007). Structural scaling in Bach’s cello suite no. 3. Fractals 15(1), 89–95.
-
Rozenberg, G. & Salomaa, A. (Eds.) (1980). The Mathematical Theory of L Systems. Academic Press.
-
Allouche, J.-P. & Shallit, J. (2003). Automatic Sequences: Theory, Applications, Generalizations. Cambridge University Press. Ch. 1–2 (morphisms and fixed points of parallel rewriting).
-
Voss, R. F. & Clarke, J. (1975). “1/f noise” in music and speech. Nature 258, 317–318.