Constrained tuning: Difference between revisions

m Computation: here's a stable version of the code
m Another pass on style: matrices are italic following Wikipedia practice. Eigenmonzo -> unit eigenmonzo. Misc. cleanup
Line 1: Line 1:
'''Constrained tunings''' are tuning [[optimization]] techniques using the constraint of some purely tuned intervals (i.e. [[eigenmonzo]]s, or unchanged-intervals). '''CTE tuning''' ('''constrained Tenney-Euclidean tuning''') is the most typical instance. It has a more sophisticated variant, '''CTWE tuning''' ('''constrained Tenney-Weil-Euclidean tuning'''), a.k.a. '''KE tuning''' ('''Kees-Euclidean tuning'''). These two tunings will be the focus of this article. Otherwise normed tunings can be defined and computed analogously.  
'''Constrained tunings''' are tuning [[optimization]] techniques using the constraint of some purely tuned intervals (i.e. [[eigenmonzo|unit eigenmonzos, or unchanged-intervals]]). '''CTE tuning''' ('''constrained Tenney-Euclidean tuning''') is the most typical instance. It has a more sophisticated variant, '''CTWE tuning''' ('''constrained Tenney-Weil-Euclidean tuning'''), a.k.a. '''KE tuning''' ('''Kees-Euclidean tuning'''). These two tunings will be the focus of this article. Otherwise normed tunings can be defined and computed analogously.  


All constrained tunings are standard temperament optimization problems. Specifically, as [[TE tuning]] can be viewed as a [[Wikipedia: Least squares|least squares problem]], CTE tuning can be viewed as an equality-constrained least squares problem.  
All constrained tunings are standard temperament optimization problems. Specifically, as [[TE tuning]] can be viewed as a {{w|least squares|least squares problem}}, CTE tuning can be viewed as an equality-constrained least squares problem.  


The most common subject of constraint is the octave, which is assumed unless specified otherwise. For higher-rank temperaments, it may make sense to add multiple constraints, such as a pure-2.3 (pure-octave pure-fifth) constrained tuning. For a rank-''r'' temperament, specifying a rank-''m'' constraint list will yield ''r'' - ''m'' [[Wikipedia: Degrees of freedom|degrees of freedom]] to be optimized.  
The most common subject of constraint is the octave, which is assumed unless specified otherwise. For higher-rank temperaments, it may make sense to add multiple constraints, such as a pure-2.3 (pure-octave pure-fifth) constrained tuning. For a rank-''r'' temperament, specifying a rank-''m'' constraint list will yield ''r'' - ''m'' {{w|degrees of freedom}} to be optimized.  


== Definition ==
== Definition ==
Given a temperament [[mapping]] V and the [[just tuning map]] J, we specify a weight–skew transformation, represented by transformation matrix X, and a ''q''-norm. Suppose the tuning is constrained by the eigenmonzo list M<sub>C</sub>. The goal is to find the generator list G by
Given a temperament [[mapping]] ''V'' and the [[just tuning map]] ''J'', we specify a weight–skew transformation, represented by transformation matrix ''X'', and a ''q''-norm. Suppose the tuning is constrained by the eigenmonzo list ''M''<sub>''I''</sub>. The goal is to find the generator list ''G'' by


Minimize
Minimize
Line 14: Line 14:
subject to
subject to


<math>\displaystyle (GV - J)M_{\rm C} = O </math>
<math>\displaystyle (GV - J)M_I = O </math>


where (·)<sub>X</sub> denotes the variable in the weight–skew transformed space, found by
where (·)<sub>''X''</sub> denotes the variable in the weight–skew transformed space, found by


<math>\displaystyle
<math>\displaystyle
Line 26: Line 26:


The problem is feasible if
The problem is feasible if
# rank (M<sub>C</sub>) ≤ rank (V), and
# rank (''M''<sub>''I''</sub>) ≤ rank (''V''), and
# The subgroups of M<sub>C</sub> and N (V) are [[Wikipedia:linear independence|linearly independent]].
# The subgroups of ''M''<sub>''I''</sub> and N (''V'') are {{w|linear independence|linearly independent}}.


== Computation ==
== Computation ==
As a standard optimization problem, numerous algorithms exist to solve it, such as [[Wikipedia: Sequential quadratic programming|sequential quadratic programming]], to name one. [[Flora Canou]]'s [https://github.com/FloraCanou/temperament_evaluator/blob/v0.26.4/te_optimizer_legacy.py tuning optimizer] is such an implementation in [https://www.python.org Python]. Note: it uses [https://scipy.org/ Scipy].  
As a standard optimization problem, numerous algorithms exist to solve it, such as {{w|sequential quadratic programming}}, to name one. [[Flora Canou]]'s [https://github.com/FloraCanou/temperament_evaluator/blob/v0.26.4/te_optimizer_legacy.py tuning optimizer] is such an implementation in [https://www.python.org Python]. Note: it uses [https://scipy.org/ Scipy].  


{{Databox| Code |
{{Databox| Code |
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
# © 2020-2023 Flora Canou | Version 0.26.4
# © 2020-2023 Flora Canou | Version 0.27.2
# This work is licensed under the GNU General Public License version 3.
# This work is licensed under the GNU General Public License version 3.


Line 56: Line 56:
         self.order = order
         self.order = order


     def __get_weight (self, subgroup):
     def __get_tuning_weight (self, subgroup):
         match self.wtype:
         match self.wtype:
             case "tenney":
             case "tenney":
Line 70: Line 70:
         return np.diag (weight_vec**self.wamount)
         return np.diag (weight_vec**self.wamount)


     def __get_skew (self, subgroup):
     def __get_tuning_skew (self, subgroup):
         if self.skew == 0:
         if self.skew == 0:
             return np.eye (len (subgroup))
             return np.eye (len (subgroup))
Line 82: Line 82:
             r*np.ones ((len (subgroup), 1)), axis = 1)
             r*np.ones ((len (subgroup), 1)), axis = 1)


     def weightskewed (self, main, subgroup):
     def tuning_x (self, main, subgroup):
         return main @ self.__get_weight (subgroup) @ self.__get_skew (subgroup)
         return main @ self.__get_tuning_weight (subgroup) @ self.__get_tuning_skew (subgroup)


def __get_subgroup (main, subgroup):
def __get_subgroup (main, subgroup):
Line 96: Line 96:
     return main, subgroup
     return main, subgroup


def __error (gen, vals, just_tuning_map, order):
def optimizer_main (breeds, subgroup = None, norm = Norm (),  
    return linalg.norm (gen @ vals - just_tuning_map, ord = order)
 
def optimizer_main (vals, subgroup = None, norm = Norm (),  
         cons_monzo_list = None, des_monzo = None, show = True):
         cons_monzo_list = None, des_monzo = None, show = True):
     # NOTE: "map" is a reserved word
     # NOTE: "map" is a reserved word
     # optimization is preferably done in the unit of octaves, but for precision reasons
     # optimization is preferably done in the unit of octaves, but for precision reasons
     vals, subgroup = __get_subgroup (vals, subgroup)
     breeds, subgroup = __get_subgroup (breeds, subgroup)


     just_tuning_map = SCALAR.CENT*np.log2 (subgroup)
     just_tuning_map = SCALAR.CENT*np.log2 (subgroup)
     vals_x = norm.weightskewed (vals, subgroup)
     breeds_x = norm.tuning_x (breeds, subgroup)
     just_tuning_map_x = norm.weightskewed (just_tuning_map, subgroup)
     just_tuning_map_x = norm.tuning_x (just_tuning_map, subgroup)
     if norm.order == 2 and cons_monzo_list is None: #simply using lstsq for better performance
     if norm.order == 2 and cons_monzo_list is None: #simply using lstsq for better performance
         res = linalg.lstsq (vals_x.T, just_tuning_map_x)
         res = linalg.lstsq (breeds_x.T, just_tuning_map_x)
         gen = res[0]
         gen = res[0]
         print ("Euclidean tuning without constraints, solved using lstsq. ")
         print ("Euclidean tuning without constraints, solved using lstsq. ")
     else:
     else:
         gen0 = [SCALAR.CENT]*vals.shape[0] #initial guess
         gen0 = [SCALAR.CENT]*breeds.shape[0] #initial guess
         cons = () if cons_monzo_list is None else {
         cons = () if cons_monzo_list is None else {
             'type': 'eq',  
             'type': 'eq',  
             'fun': lambda gen: (gen @ vals - just_tuning_map) @ cons_monzo_list
             'fun': lambda gen: (gen @ breeds - just_tuning_map) @ cons_monzo_list
         }
         }
         res = optimize.minimize (__error, gen0, args = (vals_x, just_tuning_map_x, norm.order),  
         res = optimize.minimize (lambda gen: linalg.norm (gen @ breeds_x - just_tuning_map_x, ord = norm.order), gen0,  
             method = "SLSQP", options = {'ftol': 1e-9}, constraints = cons)
             method = "SLSQP", options = {'ftol': 1e-9}, constraints = cons)
         print (res.message)
         print (res.message)
Line 129: Line 126:
         if np.asarray (des_monzo).ndim > 1 and np.asarray (des_monzo).shape[1] != 1:
         if np.asarray (des_monzo).ndim > 1 and np.asarray (des_monzo).shape[1] != 1:
             raise IndexError ("only one destretch target is allowed. ")
             raise IndexError ("only one destretch target is allowed. ")
         elif (tempered_size := gen @ vals @ des_monzo) == 0:
         elif (tempered_size := gen @ breeds @ des_monzo) == 0:
             raise ZeroDivisionError ("destretch target is in the nullspace. ")
             raise ZeroDivisionError ("destretch target is in the nullspace. ")
         else:
         else:
             gen *= (just_tuning_map @ des_monzo)/tempered_size
             gen *= (just_tuning_map @ des_monzo)/tempered_size


     tempered_tuning_map = gen @ vals
     tempered_tuning_map = gen @ breeds
     mistuning_map = tempered_tuning_map - just_tuning_map
     error_map = tempered_tuning_map - just_tuning_map


     if show:
     if show:
         print (f"Generators: {gen} (¢)",
         print (f"Generators: {gen} (¢)",
             f"Tuning map: {tempered_tuning_map} (¢)",
             f"Tuning map: {tempered_tuning_map} (¢)",
             f"Mistuning map: {mistuning_map} (¢)", sep = "\n")
             f"Error map: {error_map} (¢)", sep = "\n")


     return gen, tempered_tuning_map, mistuning_map
     return gen, tempered_tuning_map, error_map


optimiser_main = optimizer_main
optimiser_main = optimizer_main
Line 152: Line 149:


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
vals = np.array ([[1, 0, -4, -13], [0, 1, 4, 10]])
breeds = np.array ([[1, 0, -4, -13], [0, 1, 4, 10]])
optimizer_main (vals, cons_monzo_list = np.transpose ([1] + [0]*(vals.shape[1] - 1)))
optimizer_main (breeds, cons_monzo_list = np.transpose ([1] + [0]*(breeds.shape[1] - 1)))
</syntaxhighlight>
</syntaxhighlight>


Line 162: Line 159:
Generators: [1200.    1896.9521] (¢)
Generators: [1200.    1896.9521] (¢)
Tuning map: [1200.    1896.9521 2787.8085 3369.5214] (¢)
Tuning map: [1200.    1896.9521 2787.8085 3369.5214] (¢)
Mistuning map: [ 0.    -5.0029  1.4948  0.6955] (¢)
Error map: [ 0.    -5.0029  1.4948  0.6955] (¢)
</pre>
</pre>


Analytical solutions exist for Euclidean (''L''<sup>2</sup>) tunings, see [[Constrained tuning/Analytical solution to constrained Euclidean tunings]]. It can also be solved in the [[wikipedia: Lagrange multiplier|method of Lagrange multiplier]]. The solution is given by
Analytical solutions exist for Euclidean (''L''<sup>2</sup>) tunings, see [[Constrained tuning/Analytical solution to constrained Euclidean tunings]]. It can also be solved in the {{w|Lagrange multiplier|method of Lagrange multiplier}}. The solution is given by


<math>\displaystyle
<math>\displaystyle
\begin{bmatrix}
\begin{bmatrix}
G^{\mathsf T}  \\
G^{\mathsf T}  \\
\Lambda^{\mathsf T}
\mathit\Lambda^{\mathsf T}
\end{bmatrix}
\end{bmatrix}
=
=
Line 184: Line 181:
</math>
</math>


which is almost an analytical solution. Notice we introduced the vector of lagrange multipliers Λ, with length equal to the number of constraints. The lagrange multipliers have no concrete meaning for the resulting tuning, so they can be discarded.
which is almost an analytical solution. Notice we introduced the vector of lagrange multipliers ''Λ'', with length equal to the number of constraints. The lagrange multipliers have no concrete meaning for the resulting tuning, so they can be discarded.


== CTE tuning vs CTWE tuning ==
== CTE tuning vs CTWE tuning ==
Line 204: Line 201:


== Special constraint ==
== Special constraint ==
The special eigenmonzo Xj, where j is the all-ones monzo, has the effect of removing the weighted–skewed tuning bias. This eigenmonzo is actually proportional to the monzo of the extra dimension introduced by the skew. In other words, it forces the extra dimension to be pure, and therefore, the skew will have no effect with this constrained tuning.  
The special eigenmonzo ''X'''''j''', where '''j''' is the all-ones monzo, has the effect of removing the weighted–skewed tuning bias. This eigenmonzo is actually proportional to the monzo of the extra dimension introduced by the skew. In other words, it forces the extra dimension to be pure, and therefore, the skew will have no effect with this constrained tuning.  


It can be regarded as a distinct optimum. In the case of Tenney weighting, it is the '''TOCTE tuning''' ('''Tenney ones constrained Tenney-Euclidean tuning''').  
It can be regarded as a distinct optimum. In the case of Tenney weighting, it is the '''TOCTE tuning''' ('''Tenney ones constrained Tenney-Euclidean tuning''').  
Line 219: Line 216:
<math>\displaystyle n = 1/g = \operatorname {mean} (V_X)</math>
<math>\displaystyle n = 1/g = \operatorname {mean} (V_X)</math>


Unlike TE or TOP, the optimal edo number space in TOC is linear with respect to V. That is, if V = ''α''V<sub>1</sub> + ''β''V<sub>2</sub>, then
Unlike TE or TOP, the optimal edo number space in TOC is linear with respect to ''V''. That is, if ''V'' = ''αV''<sub>1</sub> + ''βV''<sub>2</sub>, then


<math>\displaystyle
<math>\displaystyle
Line 230: Line 227:
</math>
</math>


As a result, the [[Relative interval error #Linearity|relative error space]] is also linear with respect to V.  
As a result, the [[Relative interval error #Linearity|relative error space]] is also linear with respect to ''V''.  


For example, the relative errors of 12ettoc5 (12et in 5-limit TOC) is
For example, the relative errors of 12ettoc5 (12et in 5-limit TOC) is
Line 250: Line 247:


== Systematic name ==
== Systematic name ==
In D&D's guide to RTT, the [[Dave Keenan & Douglas Blumeyer's guide to RTT: alternative complexities #Naming|systematic name]] for the CTE tuning scheme is ''[[Dave Keenan %26 Douglas Blumeyer%27s guide to RTT: all-interval tuning schemes #Held-octave minimax-.28E.29S|held-octave minimax-ES]]'', and the systematic name for the CTWE tuning scheme is ''[[Dave Keenan %26 Douglas Blumeyer%27s guide to RTT: tuning fundamentals #Held-intervals|held-octave]] [[Dave Keenan %26 Douglas Blumeyer%27s guide to RTT: alternative complexities #Tunings used in 7|minimax-E-lils-S]]''.
In [[D&D's guide|D&D's guide to RTT]], the [[Dave Keenan & Douglas Blumeyer's guide to RTT: alternative complexities #Naming|systematic name]] for the CTE tuning scheme is ''[[Dave Keenan %26 Douglas Blumeyer%27s guide to RTT: all-interval tuning schemes #Held-octave minimax-.28E.29S|held-octave minimax-ES]]'', and the systematic name for the CTWE tuning scheme is ''[[Dave Keenan %26 Douglas Blumeyer%27s guide to RTT: tuning fundamentals #Held-intervals|held-octave]] [[Dave Keenan %26 Douglas Blumeyer%27s guide to RTT: alternative complexities #Tunings used in 7|minimax-E-lils-S]]''.


[[Category:Terms]]
[[Category:Terms]]
[[Category:Math]]
[[Category:Math]]
[[Category:Regular temperament tuning]]
[[Category:Regular temperament tuning]]