diff 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
line wrap: on
line diff
--- a/Syncopation models/basic_functions.py	Fri Apr 03 22:57:27 2015 +0100
+++ b/Syncopation models/basic_functions.py	Tue Apr 07 19:05:07 2015 +0100
@@ -49,47 +49,58 @@
 
 # The find_divisor function returns a list of all possible divisors for a length of sequence.
 def find_prime_factors(number):
-	prime_factors = find_divisor(number)
+	primeFactors = find_divisor(number)
 	
-	def is_prime(num):
-		if num < 2:
-			return False
-		if num == 2:
-			return True
-		else:
-			for div in range(2,num):
-				if num % div == 0:
-					return False
-		return True
+	# remove 1 because 1 is not prime number
+	del primeFactors[0]
 
-	for i in range(len(prime_factors)-1,0,-1):
-		if is_prime(prime_factors[i]) == False:
-			del prime_factors[i]
+	# reversely traverse all the divisors list and once find a non-prime then delete
+	for i in range(len(primeFactors)-1,0,-1):
+	#	print primeFactors[i], is_prime(primeFactors[i])
+		if not is_prime(primeFactors[i]):
+			del primeFactors[i]
 
-	return prime_factors
+	return primeFactors
+
+def is_prime(number):
+	isPrime = True
+	# 0 or 1 is not prime numbers
+	if number < 2:
+		isPrime = False
+	# 2 is the only even prime number
+	elif number == 2:
+		pass
+	# all the other even numbers are non-prime
+	elif number % 2 == 0:
+		isPrime = False
+	else:
+		for odd in range(3, int(math.sqrt(number) + 1), 2):
+			if number % odd == 0:
+				isPrime = False
+	return isPrime
 
 # The min_timeSpan function searches for the shortest possible time-span representation for a sequence.
 def get_min_timeSpan(seq):
-	min_ts = [1]
+	minTimeSpan = [1]
 	for d in find_divisor(len(seq)):
 		segments = subdivide(seq,d)
 		if len(segments)!=0:
-			del min_ts[:]
+			del minTimeSpan[:]
 			for s in segments:
-				min_ts.append(s[0])
-			if sum(min_ts) == sum(seq):
+				minTimeSpan.append(s[0])
+			if sum(minTimeSpan) == sum(seq):
 				break
-	return min_ts
+	return minTimeSpan
 
 # get_note_indices returns all the indices of all the notes in this sequence
-def get_note_indices(seq):
-	note_indices = []
+def get_note_indices(sequence):
+	noteIndices = []
 
-	for index in range(len(seq)):
-		if seq[index] != 0:
-			note_indices.append(index)
+	for index in range(len(sequence)):
+		if sequence[index] != 0:
+			noteIndices.append(index)
 
-	return note_indices
+	return noteIndices
 
 # The get_H returns a sequence of metrical weight for a certain metrical level (horizontal),
 # given the sequence of metrical weights in a hierarchy (vertical) and a sequence of subdivisions.
@@ -179,4 +190,15 @@
 
 
 # # testing
-# print find_prime_factors(10)
\ No newline at end of file
+# print find_prime_factors(10)
+# print find_prime_factors(2)
+# print find_prime_factors(12)
+
+
+# print is_prime(1)       # False
+# print is_prime(2)       # True
+# print is_prime(3)       # True
+# print is_prime(29)      # True
+# print is_prime(345)     # False
+# print is_prime(999979)  # True
+# print is_prime(999981)  # False
\ No newline at end of file