def subsets_prime(self, n, prime_algorithm="prime_form_sampaio"): """Returns adjacent and non-adjacent subsets of a given contour grouped by their prime forms. Output is a dictionary where the key is the prime form, and the attribute is csubsets list. >>> Contour([0, 3, 1, 4, 2]).subsets_prime() {(0, 1, 3, 2): [[0, 1, 4, 2]], (0, 2, 1, 3): [[0, 3, 1, 4]], (0, 2, 3, 1): [[0, 3, 4, 2]], (0, 3, 1, 2): [[0, 3, 1, 2]], (1, 3, 0, 2): [[3, 1, 4, 2]]} """ subsets = self.subsets(n) dic = {} for x in subsets: processed = tuple(auxiliary.apply_fn(Contour(x), prime_algorithm)) if processed in dic: z = dic[processed] z.append(x) dic[processed] = z else: dic[processed] = [x] return dic
def unique_prime_form_test(self, prime_algorithm="prime_form_sampaio"): """Returns True if the prime form algorithm returns only one prime form for each contour class. Sampaio prime form algorithm is default. >>> Contour([0, 2, 1, 3, 4]).unique_prime_form_test() True """ p, i, r, ri = self.class_representatives() prime_p = auxiliary.apply_fn(p, prime_algorithm) prime_i = auxiliary.apply_fn(i, prime_algorithm) prime_r = auxiliary.apply_fn(r, prime_algorithm) prime_ri = auxiliary.apply_fn(ri, prime_algorithm) return prime_p == prime_i == prime_r == prime_ri
def __tuple_prime(lst, prime_algorithm): """Returns a tuple with a cseg from a list of c-pitches. >>> __tuple_prime([2, 1, 0]) (0, 1, 2) """ return tuple(auxiliary.apply_fn(Contour(lst), prime_algorithm))
def __repeated_prime_generic(self, prime_algorithm): """Returns prime forms of a repeated cpitch cseg calculated with a given prime_algorithm. """ triangle = self.comparison_matrix().superior_triangle() csegs = matrix.triangle_zero_replace_to_cseg(triangle) return sorted([auxiliary.apply_fn(t, prime_algorithm) for t in csegs])
def csegclass_similarity(cseg1, cseg2, prime_algorithm="prime_form_marvin_laprade"): """Returns Marvin and Laprade (1987) CSIM(_A, _B) with csegclasses representatives comparison. >>> csegclass_similarity(Contour([0, 2, 3, 1]), Contour([3, 1, 0, 2])) 1 """ cseg1_p = auxiliary.apply_fn(cseg1, prime_algorithm) representatives = cseg2.class_representatives() csims = [cseg_similarity(cseg1_p, c) for c in representatives] return sorted(csims, reverse=True)[0]
def class_representatives(self, prime_algorithm="prime_form_marvin_laprade"): """Returns the four csegclass representatives (Marvin and Laprade 1987, p. 237): prime, inversion, and retrograde inversion. >>> Contour([0, 1, 3, 2]).class_representatives() [< 0 1 3 2 >, < 3 2 0 1 >, < 2 3 1 0 >, < 1 0 2 3 >] """ p = auxiliary.apply_fn(Contour(self), prime_algorithm) i = Contour(self).inversion() r = Contour(self).retrogression() ri = Contour(i).retrogression() return [p, i, r, ri]
def segment_class(self, prime_algorithm="prime_form_sampaio"): """Returns contour segment class of a given cseg. Output format is: (cardinality, number, cseg_class, identity under retrograde inversion), like (3, 1, (0, 1, 2), True). >>> Contour([0, 1, 3, 2]).segment_class() (4, 2, < 0 1 3 2 >, False) """ prime_form = auxiliary.apply_fn(self, prime_algorithm) cseg_classes = utils.flatten(build_classes(len(self), prime_algorithm)) for (cardinality, number, cseg_class, ri_identity) in cseg_classes: if tuple(prime_form) == cseg_class: r = cardinality, number, Contour(list(cseg_class)), ri_identity return r
def __class_index(self, vector_method): """Returns a general upward/downward decimal index, that -1.0 means the cseg is completely downward; 1.0 means the cseg is completely upward, and 0 means the cseg is balanced. Accepts Friedmann CCVI and CCVII as vector method. >>> Contour([0, 3, 1, 2]).__class_index('class_vector_i') 0.69999999999999996 """ ups, downs = auxiliary.apply_fn(self, vector_method) total = ups + downs if ups == downs: return 0 elif ups > downs: return ups * 1.0 / total else: return downs * -1.0 / total
def internal_diagonal_classes(cardinality, prime_algorithm="prime_form_marvin_laprade"): """Returns internal diagonal classes of a given cardinality. >>> internal_diagonal_classes(4) [< + + + >, < + + - >, < + - + >] """ permut = [] [permut.append([-1, 1]) for n in range(cardinality)] permut = sorted(utils.flatten(permut)) permut = itertools.permutations(permut, cardinality) # collection coll = set() for el in permut: int_d = auxiliary.apply_fn(InternalDiagonal(el), prime_algorithm) coll.add(tuple(int_d)) r = sorted([InternalDiagonal(list(x)) for x in list(coll)], reverse=True) return r
def __append_op(lst, cseg, factor, op): """Appends cseg, factor, and operation data to a given list. """ lst.append((cseg, factor, op, auxiliary.apply_fn(rotated, op)))