comparison Syncopation models/basic_functions.py @ 20:b959c2acb927

Refactored all models except for KTH, all past testing except for SG.
author csong <csong@eecs.qmul.ac.uk>
date Tue, 07 Apr 2015 19:05:07 +0100
parents 9030967a05f8
children b6daddeefda9
comparison
equal deleted inserted replaced
19:9030967a05f8 20:b959c2acb927
47 divisors.append(i) 47 divisors.append(i)
48 return divisors 48 return divisors
49 49
50 # The find_divisor function returns a list of all possible divisors for a length of sequence. 50 # The find_divisor function returns a list of all possible divisors for a length of sequence.
51 def find_prime_factors(number): 51 def find_prime_factors(number):
52 prime_factors = find_divisor(number) 52 primeFactors = find_divisor(number)
53 53
54 def is_prime(num): 54 # remove 1 because 1 is not prime number
55 if num < 2: 55 del primeFactors[0]
56 return False 56
57 if num == 2: 57 # reversely traverse all the divisors list and once find a non-prime then delete
58 return True 58 for i in range(len(primeFactors)-1,0,-1):
59 else: 59 # print primeFactors[i], is_prime(primeFactors[i])
60 for div in range(2,num): 60 if not is_prime(primeFactors[i]):
61 if num % div == 0: 61 del primeFactors[i]
62 return False 62
63 return True 63 return primeFactors
64 64
65 for i in range(len(prime_factors)-1,0,-1): 65 def is_prime(number):
66 if is_prime(prime_factors[i]) == False: 66 isPrime = True
67 del prime_factors[i] 67 # 0 or 1 is not prime numbers
68 68 if number < 2:
69 return prime_factors 69 isPrime = False
70 # 2 is the only even prime number
71 elif number == 2:
72 pass
73 # all the other even numbers are non-prime
74 elif number % 2 == 0:
75 isPrime = False
76 else:
77 for odd in range(3, int(math.sqrt(number) + 1), 2):
78 if number % odd == 0:
79 isPrime = False
80 return isPrime
70 81
71 # The min_timeSpan function searches for the shortest possible time-span representation for a sequence. 82 # The min_timeSpan function searches for the shortest possible time-span representation for a sequence.
72 def get_min_timeSpan(seq): 83 def get_min_timeSpan(seq):
73 min_ts = [1] 84 minTimeSpan = [1]
74 for d in find_divisor(len(seq)): 85 for d in find_divisor(len(seq)):
75 segments = subdivide(seq,d) 86 segments = subdivide(seq,d)
76 if len(segments)!=0: 87 if len(segments)!=0:
77 del min_ts[:] 88 del minTimeSpan[:]
78 for s in segments: 89 for s in segments:
79 min_ts.append(s[0]) 90 minTimeSpan.append(s[0])
80 if sum(min_ts) == sum(seq): 91 if sum(minTimeSpan) == sum(seq):
81 break 92 break
82 return min_ts 93 return minTimeSpan
83 94
84 # get_note_indices returns all the indices of all the notes in this sequence 95 # get_note_indices returns all the indices of all the notes in this sequence
85 def get_note_indices(seq): 96 def get_note_indices(sequence):
86 note_indices = [] 97 noteIndices = []
87 98
88 for index in range(len(seq)): 99 for index in range(len(sequence)):
89 if seq[index] != 0: 100 if sequence[index] != 0:
90 note_indices.append(index) 101 noteIndices.append(index)
91 102
92 return note_indices 103 return noteIndices
93 104
94 # The get_H returns a sequence of metrical weight for a certain metrical level (horizontal), 105 # The get_H returns a sequence of metrical weight for a certain metrical level (horizontal),
95 # given the sequence of metrical weights in a hierarchy (vertical) and a sequence of subdivisions. 106 # given the sequence of metrical weights in a hierarchy (vertical) and a sequence of subdivisions.
96 def get_H(weightSequence,subdivisionSequence, level): 107 def get_H(weightSequence,subdivisionSequence, level):
97 H = [] 108 H = []
178 # return vseq 189 # return vseq
179 190
180 191
181 # # testing 192 # # testing
182 # print find_prime_factors(10) 193 # print find_prime_factors(10)
194 # print find_prime_factors(2)
195 # print find_prime_factors(12)
196
197
198 # print is_prime(1) # False
199 # print is_prime(2) # True
200 # print is_prime(3) # True
201 # print is_prime(29) # True
202 # print is_prime(345) # False
203 # print is_prime(999979) # True
204 # print is_prime(999981) # False