From 6c42477d7ade92ac1122ea7c74d9a32d8a501093 Mon Sep 17 00:00:00 2001 From: Jonathan Harker Date: Wed, 2 Oct 2024 08:43:41 +1300 Subject: [PATCH] Add decimal number (bitmask), from Goyette (2012) --- setclass/setclass.py | 10 ++++++++++ setclass/tests/test_setclass.py | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/setclass/setclass.py b/setclass/setclass.py index 7aedbab..b8598cc 100644 --- a/setclass/setclass.py +++ b/setclass/setclass.py @@ -82,6 +82,16 @@ class SetClass(list): """Returns the brightness of the set class, defined as the sum of the pitch class values.""" return sum(self.pitch_classes) + @cache_property + def decimal(self) -> int: + """ + Returns the decimal value of the pitch classes expressed as a binary bit mask, i.e. the sum + of 2ⁱ where i is each pitch class value. For example, {0,1,4,6} has a binary value of + 000001010011, which is the decimal value 83. + — Goyette (2012) p. 25, citing Brinkman (1986). + """ + return sum([2**i for i in self.pitch_classes]) + @cache_property def adjacency_intervals(self) -> list(int): """Adjacency intervals between the pitch classes, used for Leonard notation subscripts.""" diff --git a/setclass/tests/test_setclass.py b/setclass/tests/test_setclass.py index 9841aae..e5d6a99 100644 --- a/setclass/tests/test_setclass.py +++ b/setclass/tests/test_setclass.py @@ -86,6 +86,11 @@ def test_brightness(): assert f520.brightness == 20 +def test_decimal(): + "Set classes have a unique decimal number (sum of 2 raised to each normalised pitch class value)" + assert f520.decimal == 355 + + def test_pitch_classes(): "Set classes have pitch classes" assert len(f520) == 5 @@ -153,6 +158,10 @@ def test_complement_brightness(): assert f520.complement.brightness == 32 +def test_complement_decimal(): + assert f520.complement.decimal == 935 + + def test_complement_pitch_classes(): assert len(f520.complement) == 7 assert len(f520.complement.pitch_classes) == 7