Skip to content

Commit 8a2edb7

Browse files
committed
[cs] fix analysis and quick-cs-subsets functions
Inversions of the intervals were not being accounted.
1 parent e289c88 commit 8a2edb7

2 files changed

Lines changed: 46 additions & 22 deletions

File tree

src/erv/constant_structures/brute_force.cljc

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"Find constant structures by brute force"
33
(:require
44
[clojure.math.combinatorics :refer [combinations]]
5-
[erv.constant-structures.core :refer [analyze maybe-round]]
5+
[erv.constant-structures.core :refer [analyze maybe-rationalize]]
66
[erv.utils.conversions :as conv]
77
[erv.utils.core :refer [interval]]
88
[erv.utils.ratios :refer [ratios->scale]]
@@ -92,7 +92,7 @@
9292
(let [[deg1 deg2] (sort deg-pair)
9393
a (:bounded-ratio (nth scale deg1))
9494
b (:bounded-ratio (nth scale deg2))]
95-
(maybe-round (interval a b))))
95+
(maybe-rationalize (interval a b) 10)))
9696

9797
(def ^:private memoized-deg-combinations
9898
(memoize (comp #(map sort %)
@@ -102,22 +102,35 @@
102102

103103
(def ^:private conj-set (fnil conj #{}))
104104

105+
(defn- invert-interval [interval period]
106+
(* period (/ 1 interval)))
107+
105108
(defn- quick-check-cs?
106109
[deg-combinations scale]
107-
(reduce
108-
(fn [data deg-pair]
109-
(let [[deg1 deg2] deg-pair
110-
steps (- deg2 deg1)
111-
interval (get-interval scale deg-pair)
112-
updated-data (update data interval conj-set steps)
113-
interval-steps (updated-data interval)]
114-
(if (> (count interval-steps) 1)
115-
(reduced {})
116-
updated-data)))
117-
{}
118-
deg-combinations))
110+
(let [period (-> scale first :bounding-period)
111+
size (count scale)]
112+
(reduce
113+
(fn [data deg-pair]
114+
(let [[deg1 deg2] deg-pair
115+
steps (- deg2 deg1)
116+
interval (get-interval scale deg-pair)
117+
inversion-steps (- size steps)
118+
inversion (invert-interval interval period)
119+
updated-data (-> data
120+
(update interval conj-set steps)
121+
(update inversion conj-set inversion-steps))
122+
interval-steps (updated-data interval)]
123+
(when (> inversion period)
124+
(timbre/error "Error in calculation, `inversion` cannot be larger than period"))
125+
(if (> (count interval-steps) 1)
126+
(reduced {})
127+
updated-data)))
128+
{}
129+
deg-combinations)))
119130

120131
(defn quick-cs-subsets
132+
"Calculate all CS subset of the given sizes.
133+
FIXME only really works for JI scales."
121134
[cs-sizes scale]
122135
(eduction
123136
(mapcat #(combinations (range (count scale)) %))

src/erv/constant_structures/core.cljc

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,33 @@
1313
(round2 6 n))
1414
:cljs (round2 6 n)))
1515

16+
(defn maybe-rationalize
17+
[n decimals]
18+
#?(:clj (if (rational? n)
19+
n
20+
(rationalize (round2 decimals n)))
21+
;; TODO implement
22+
:cljs (round2 6 n)))
23+
1624
(defn get-intervals
17-
[note-pair]
25+
[scale-size note-pair]
1826
(->> note-pair
1927
((fn [[a b]]
20-
{(maybe-round (interval (:bounded-ratio a)
21-
(:bounded-ratio b)))
22-
{:steps #{(- (:index b)
23-
(:index a))}
24-
:intervals [[a b]]}}))))
28+
(let [period (:bounding-period a)
29+
intvl (maybe-round (interval (:bounded-ratio a)
30+
(:bounded-ratio b)))
31+
inversion (* period (/ 1 intvl))]
32+
{intvl {:steps #{(- (:index b) (:index a))}
33+
:intervals [[a b]]}
34+
inversion {:steps #{(- scale-size (- (:index b) (:index a)))}
35+
:intervals [[b a]]}})))))
2536

2637
(defn analyze
2738
[scale]
2839
(let [interval-data (->> (combo/combinations
2940
(map-indexed #(assoc %2 :index %1) scale)
3041
2)
31-
(map get-intervals)
42+
(map (partial get-intervals (count scale)))
3243
(apply merge-with (partial merge-with concat))
3344
(map (fn [[interval data]]
3445
{interval (update data :steps (partial into #{}))}))
@@ -64,7 +75,7 @@
6475
(:scale (cps/make 2 [11 13 5 7]))))
6576

6677
(:constant-structure? (analyze
67-
(:scale (cps/make 2 [1 3 5 7 9]))))
78+
(:scale (cps/make 2 [1 3 5 7]))))
6879
(:constant-structure? (analyze
6980
(:scale (edo/from-pattern [2 2 1 2 2 2 1]))))
7081
#_(:non-cs-intervals (analyze

0 commit comments

Comments
 (0)