Saturday, August 3, 2024

Mesopotamian Logarithm Algorithm

It's been some time since I wrote this article, and upon re-reading it, I’d like to clarify something to avoid potential misinterpretation: I’ve named it the Mesopotamian Logarithm Algorithm because the algorithm and visualizations I used are inspired by Mesopotamian music theory. While I analyze some of the cuneiform tablets and replicate their logic, this is a modified version of their scale generation method. It is not a claim that they used this process for calculating logarithms.

A Musical Approach to Convergents of Continued Fractions

This article explores a method for calculating logarithms deeply rooted in musical principles first documented in ancient Mesopotamian cuneiform tablets. While superficially similar to existing methods involving continued fractions, such as Shanks' logarithm algorithm, this approach takes a fundamentally musical perspective. Instead of directly computing intermediate terms of the continued fraction, it operates on musical intervals, directly identifying convergents of the logarithm. This connection between music and logarithms emerges from the traditional musical concept of the "comma." Although this algorithm might not surpass existing methods in efficiency across all cases, it incorporates various optimizations within its design. What makes this algorithm particularly intriguing is the surprisingly early demonstration of modern mathematical concepts in the context of ancient Mesopotamian music theory.


Musical Theory Background

Music theory is a diverse field with various schools of thought, ranging from traditional to experimental approaches. This diversity mirrors the essence of music itself, which defies a singular, absolute definition. At its core, music is deeply connected with human expression, art, and culture. Despite philosophical differences, certain processes in music, particularly in tuning theory, are universally grounded in mathematical concepts.

A commonly used technique for constructing tuning systems, known as "chaining/stacking and reducing/folding," forms the basis of the algorithm discussed in this article. This approach has been utilized by various cultures throughout history, notably in the Pythagorean scale. Documented by Boethius, Ptolemy, and others, this scale was later expanded upon by the medieval music theorist Guido d'Arezzo. Remarkably, this identical pitch calculation and scale generation process is also found in Chinese music theory under the name sanfen-sunyi, or up-down generation in English. And an even earlier application of this method is apparent in Mesopotamian music, highlighting the universality of interval reduction and equivalence concepts.

While these three distinct cultures developed and recorded this system, they likely influenced one another through historical interactions like Greek visits to Egypt or Magi priests traveling to China. Similar scales have been discovered in other geographically distant cultures. Although the process wasn't explicitly documented, pre-Columbian American and West African instruments feature diatonic or pentatonic scales.

The Mesopotamian Musical Tuning System

This ancient civilization not only calculated the same 3-limit 12-tone system, the diatonic scale, and its modes, but their methods and visualizations align with modern mathematical concepts from group theory and modular arithmetic, incorporating intervals and class matrices. A comprehensive analysis of cuneiform tablets, their translations, and their relation to cosmology extends far beyond the scope of this text. For a detailed study, refer to this excellent paper on the topic. Here i provide a concise overview pertinent to the algorithm.

The tablet features a dial and a table, both of which have modern counterparts in music theory. The table corresponds to what we now call a "class matrix," used for analyzing chords, scales, modes, and tuning patterns on instruments. This is the same purpose it serves on the tablet. It differs from the "interval matrix," which doesn't indicate exact pitches but rather their classes after construction. The dial serves as another method for displaying interval relationships, similar to the circle of fifths.  Many modern virtual synthesizers utilize a similar user interface for displaying microtonal scales.

While the tablet doesn't explicitly mention octaves (1:2) or fifths (2:3), the dial displays the names of the strings on a lyre. A corresponding tablet provides step-by-step instructions for tuning this lyre to achieve all the diatonic scale modes found in the table. This suggests a clear understanding of string length ratios and harmonics, a concept further supported by the earliest lutes, which also featured movable frets.

There is, however, no consensus on the regularity of the heptagon star depicted on the tablet. If the points were equidistant, the tuning instructions would be incorrect and unnecessary, as the scales would be congruent. In my opinion, the heptagon star is irregular, and the double circle within which it is inscribed likely represents an octave – a common practice in dial user interfaces to indicate the period.

It's crucial to recognize that we are considering three heptagon stars. One is regular, and rotating it leads to an identical star, which would correspond to 7-EDO (7 equal divisions of the octave) if octaves were our focus. The other two are irregular, with minimal differences, barely perceptible in a perfect graphic, which emerge from the comma. We are comparing the 3-limit 12-tone system and the 12-tone temperament. Studying these congruences involves the use of interval matrices, which lies outside the initial goal of this text.  We will revisit this topic after explaining the "chain" and "reduce" operations.

The tablet shows three possible star shapes. The first, a regular heptagon, corresponds to 7edo. Tuning instructions suggest adjusting two strings to achieve different modes, which in 7edo results in arriving at notes already present, effectively removing notes rather than changing step sizes. These instructions don’t apply to this star, which involves locating the tritone, transforming it to a fifth, and relocating the tritone on another string, keeping five strings in tune with a different base to achieve a new diatonic mode.
The second star, irregular, is constructed by connecting seven points on a 12-sided polygon by fifths, returning to the origin in the last step. This generates the first seven elements in Z/12Z+ with generator 7. The instructions apply to this star and the group perspective. Starting with a set generated by 7: {7, 2, 9, 4, 11, 6, 1}, shifting all elements by -2 results in five numbers remaining the same: {5, 0, 7, 2, 9, 4, 11}.
The third star, another irregular shape, is generated in the circle group with generator log2(3). The group is infinite, and seven steps are drawn, with the last step highlighting the Pythagorean comma misalignment. There is no direct evidence that Mesopotamians knew this exact value; their theory was more practical than technical. The second star is the most likely representation.
While these historical materials might predate the formalization of group theory in modern math, CBS1766 reveals how a similar understanding of musical principles, utilizing cyclical patterns, generators, and group shifts, might have already existed in Mesopotamian musical practices.


Interval/Ratio Reduction

Considering the inverse relationship between string length and frequency, the process remains similar regardless of the starting point, with arithmetic operations adjusted accordingly. I will utilize the frequency interpretation and Western music terminology (octave, fifth, third, scale, tuning, comma, etc.)

While the generalization of this scaling operation is not limited to specific values and can even extend to complex numbers, musical intervals are real numbers, often rational. For pitch calculations, we categorize them into subharmonics \((0,1)\), fundamental \([1]\), and harmonics \((1, +∞)\). The concept of interval reduction stems from human perception and the notion of octave equivalence, which aligns with the modern concept of chroma, recognizing pitches in a 1:2 ratio as possessing the same "color."

For example, we identify the fifth (2:3) as a representative of the third harmonic (1:3) but reduced to the octave (1:2). Similarly, a major third (4:5) is the octave representative of the fifth harmonic (1:5).  Reduction separates intervals, or numbers, into classes. The fifth (2:3) not only represents the third harmonic (1:3) in octave space, but it also represents the sixth (1:6), twelfth (1:12), twenty-fourth (1:24), etc.

This universal operation in music can be represented using various notations. In modern music theory, this may be notated with the mod operator, as it returns a remainder – in this case, a ratio or intervallic remainder.

The classic mod operator can be defined as:

\(a \bmod b = \tilde{a}\),
Subtract \(b\) from \(a\) until \(a < b\). For example: \(14 \bmod 5 = 4\).

The normalized ratio mod operator can be defined as:

\(a \bmod 1:b = \tilde{a}\),
Multiply or divide \(a\) by \(b\) until \(a\) falls within the range \((1, b]\).  For example: \(48 \bmod 1:2 = 1.5\).

This bears a strong resemblance to the first part of Shanks' algorithm (although there is no evidence of his interest in music theory).  He describes this aspect of the process for integers \(b > a > 1\) as finding \(a^n < b < a^{n+1}\), essentially the same reduction or scaling process: \(2^5 < 48 < 2^6\), so  \( 48/2^5 = 1.5\)

Generalizing reduction, it can be seen as a mapping, transforming a number into another range. While you can perform this operation with other ranges, normalization is required.  For example, \(7 \bmod 4:5\) is calculated as \(7 \bmod 1:5/4 = 1.12\).

The Chain/Stack

The chaining operation mathematically corresponds to an exponential sequence.  A chain of third harmonics, expressed in terms of frequency ratios, is represented by \(\{3^0, 3^1, 3^2, 3^3, 3^4, \ldots\}\).  These numbers, as musical intervals, form a 1:3 ratio, representing consecutive third harmonics: \(\{1, 3, 9, 27, 81, \ldots\}\).

The 12-tone Pythagorean/Chinese/Mesopotamian scale, constructed in this manner, is known in modern theory as the 3-limit 12-tone system. It chains 12 fifths (or third harmonics) and reduces them by the octave. Its tempered/truncated modern version is the 12-EDO (12 equal divisions of the octave) or 12-tone equal temperament, created to compensate for the emergence of commas – a crucial component of the algorithm in both Shanks' and Mesopotamian methods.

Commas

Traditionally, the term "comma" refers to a small and "problematic" interval, a famous example is known as the "Pythagorean comma." This arises when constructing a scale using the octave (1:2) and the third harmonic (1:3).  After stacking and reducing 12 notes, the next calculated note almost returns to the starting point \(3^{12} \bmod 1:2 = 3^{12}/2^{19} = 531441/524288\), making the set of pitches nearly cyclical. This happens because no integers \(n\) and \(m\) exist such that \(2^n = 3^m\), as both 2 and 3 are prime numbers.

However, the concept of a comma has been expanded to define relative ratios within a set, not necessarily small. For example, in the set {1, 2, 3, 4, 5}, the consecutive commas are {1:2, 2:3, 3:4, 4:5}, representing the ratio between consecutive intervals but it can refer to any pair of intervals in the set. This includes consecutive intervals, but also, for instance, the comma 3:5, 2:4, 2:5, etc... within that set.

For this algorithm, we are focusing on detecting the traditional comma, indicating an almost-perfect alignment of the exponential sequences of the base and the argument. This alignment signifies the discovery of a convergent.

The Algorithm: Example and Convergents

To illustrate the algorithm's implications, let's examine \(\log_2(3)\) using the convergent \(\frac{19}{12}\), the octave (1:2), the third harmonic (1:3), and the chromatic scale. This example highlights the interplay between continuous and discrete processes, between music and ancient cosmology.

Consider the denominator of the convergent as the order of an additive group \(\mathbb{Z/12Z}\) and the numerator as a generator (19%12 = 7) , the sequence of elements matches the exponents in the size-ordered reduced links in the chain of \(3^x\). Specifically, if you calculate the first 12 powers of \(3\), reduce them by \(1:2\), and order them by size as \((3^x \times 2^y)\), the sequence of \(x\)'s corresponds to the elements in \(\mathbb{Z/12Z}\) generated by \(7\). Similarly, the sequence of \(y\)'s corresponds to the first 12 elements in \(\mathbb{Z/19Z}\) generated by in this case \(11\). 

                                                    log2(3)                                     log2(3/2)
~Decimal  ~Ratio          3 ^ x   *   2 ^ y    |  (3/2) ^ x   *   2 ^ (y%x)
1
1.014     524288:531441   3 ^ 12  *   2 ^ -19  |  (3/2) ^ 12  *   2 ^ -7
1.068     2048:2187       3 ^ 7   *   2 ^ -11  |  (3/2) ^ 7   *   2 ^ -4    
1.125     8:9             3 ^ 2   *   2 ^ -3   |  (3/2) ^ 2   *   2 ^ -1    
1.201     16384:19683     3 ^ 9   *   2 ^ -14  |  (3/2) ^ 9   *   2 ^ -5    ♂︎
1.266     64:81           3 ^ 4   *   2 ^ -6   |  (3/2) ^ 4   *   2 ^ -2    
1.352     131072:177147   3 ^ 11  *   2 ^ -17  |  (3/2) ^ 11  *   2 ^ -6    
1.424     512:729         3 ^ 6   *   2 ^ -9   |  (3/2) ^ 6   *   2 ^ -3    ♀︎
1.5       2:3             3 ^ 1   *   2 ^ -1   |  (3/2) ^ 1   *   2 ^  0    
1.602     4096:6561       3 ^ 8   *   2 ^ -12  |  (3/2) ^ 8   *   2 ^ -4
1.688     16:27           3 ^ 3   *   2 ^ -4   |  (3/2) ^ 3   *   2 ^ -1
1.802     32768:59049     3 ^ 10  *   2 ^ -15  |  (3/2) ^ 10  *   2 ^ -5
1.898     128:243         3 ^ 5   *   2 ^ -7   |  (3/2) ^ 5   *   2 ^ -2
2                         

\(\mathbb{Z/12Z}\) = 7,  2,  9, 4, 11, 6, 1,  8, 3, 10, 5, 0
\(\mathbb{Z/19Z}\) = 11, 3, 14, 6, 17, 9, 1, 12, 4, 15, 7, ...

This live demo calculates a few example convergents and renders a simple one, illustrating the tablet logic.


Obtaining Convergents

The implementation (detailed below) includes optimizations that may make its logic less clear. Here’s a simplified explanation:

It iteratively computes a chain of powers (exponential sequence) from the input value (the argument), and each "link" is reduced to fall within the interval range \((1, \text{base}]\). The distances between consecutive reduced links, the commas, are then passed to a condition: if the ratio of the normalized largest/smallest commas is less than \(2\), a convergent is found. The current step of the iteration provides the denominator, while the numerator is obtained by counting how many times the links were multiplied or divided until they fell within the range.

One optimization involves readjusting the upper and lower bounds of the links being evaluated, reducing the need to keep and test every comma except two. When commas pass, the previous reduced link approximates the comma between the first reduced link and the base. For clarity:

As an example take \(\log_2(10)\),

the first reduced link is \( 10^1/2^3 \), the comma ( \(H\) in the code) is \(\frac{2}{1} / \frac{10}{8} = \frac{8}{5} = 1.6 \)

As each convergent for \(\log_2(10) ≈ \frac{10}{3} ≈ \frac{93}{28} ≈ \frac{196}{59}\ldots\) indicates that,

\(10^{3}/2^{10} ≈ 10^{28}/2^{93} ≈ 10^{59}/2^{196} ≈ 1\) (progressively better)

The previous link added, indicates

\( 10^{2}/2^{6} ≈ 10^{27}/2^{89} ≈ 10^{58}/2^{192} ≈ 1.6 \),

And the next link, for example \( 10^{29} \bmod 1:2 = 10^{29} / 2^{96} ≈ \frac{10}{8} = 1.25\), approximates the first link again.

Knowing this the evaluated link becomes a new bound, lower or upper depending on which side of this value \(H\) fell.

Another optimization addresses precision, for example, to compute \(\log_2(5)\) as \(4495889/1936274\) using the simple logic explained above, you would need to calculate \(5^{1936274}\), which exceeds 32-bit integer limits. The solution is to continue the chain from its reduced link, preserving the order of commas and avoiding such exponents. This allows precision to be maintained longer.

Variations for Precision and Musical Applications

While the denominators in a convergent of \(\log_b(a)\) in music are used as an equal division of \(b\) approximating intervals generated by \(a\) in \(b\), not all work for this purpose. For example, the next convergent of \(\log_2(3)\) after \(\frac{19}{12}\) is \(\frac{65}{41}\), but 41 equal divisions of the octave don't approximate 41 consecutive reduced fifths. The subsequent convergent, \(\frac{84}{53}\), does function as needed. A variation of the algorithm uses the golden ratio (Φ) instead of 2 in the condition, selecting convergents that correspond only to uniformly distributed commas. Another way to visualize it is that a chain of \(2^{1/53}\) keeps a close approximation, in phase, with the powers of \(3\) up to \(3^{53}\) and powers of \(2\) up to \(2^{84}\).

41 fifths don't fill the octave:


as 12 fifths:


or 53 fifths:


Essentially, this variation ignores convergents with poor approximations, implicitly constructs additive groups, and identifies their generators.


Python Implementation




(Another slight optimization, which is not implemented, avoids the use of the min and max functions in every iteration when the condition is met. The indices of the minimum and maximum values, once found, remain the same. Therefore, these indices can be calculated in the first step and then reused. However, since the functions are performed on an array of two elements, saving any sort-like function has minimal impact on speed; it just interrupts the flow of the code.)
(DRAFT)

No comments:

Post a Comment

Interval Reduction

This page is dedicated to the interval reduction operation, a foundational concept in music theory that I’ve explained briefly in other arti...