示例#1
0
def fast_power_iterative(A, n, modulus):
    result = [[dirac(i, j) for i in range(len(A))] for j in range(len(A))]

    while n > 0:
        print n
        if n % 2 == 1:
            result = mult(result, A, modulus)
        A = mult(A, A, modulus)
        n /= 2
    return result
示例#2
0
def fast_power_iterative(A, n, modulus):
    result = [[dirac(i, j) for i in range(len(A))] for j in range(len(A))]
    
    while n > 0:
        print n
        if n % 2 == 1:
            result = mult(result, A, modulus)
        A = mult(A, A, modulus)
        n /= 2
    return result
示例#3
0
def fast_a(n, p):
    A = [[3, 1], [1, 0]]
    exponent_reducing_modulus = (p * p - 1) * (p * p - p)
    reduced_exponent = pow(2, n, exponent_reducing_modulus)
    six_times_answer = mult(fast_power_iterative(A, reduced_exponent, p),
                            [[3], [2]], p)[1][-1] - 5
    answer = crt([(six_times_answer, p), (0, 6)]) / 6
    return answer
示例#4
0
def main(numSets, accuracy, flag, length=3):
    if flag == 2:
        n = int(allio.sanitizeInput("Input the block size here:"))
        mostRandom = int(
            allio.sanitizeInput(
                "Print 1 for purely random, 0 for predefined clusters"))
        increaseN(n, mostRandom, numSets)
        return
    else:
        testnum = allio.sanitizeInput("Input the test number here:")
        filepath = "../Testing/test" + testnum + ".csv"
        points = allio.readFile(filepath)
        #       low = helpers.theMin(points)
        #       high = helpers.theMax(points)
        #       spacing = ()
        #       for i in range(length):
        #           spacing = spacing + ((high[i]-low[i])/numSets,)
        means = []
        n = len(points)
        thePoint = ()
        midpoint = ()
        for j in range(length - 1):
            thePoint = thePoint + (0, )
            midpoint = midpoint + (1 / float(length - 1), )
        thePoint = thePoint + (1, )
        midpoint = midpoint + (0, )
        increment = helpers.add(midpoint, helpers.mult(-1, thePoint))
        #       print thePoint, midpoint, increment
        thePoint = helpers.add(thePoint, helpers.mult(0.25, increment))
        increment = helpers.mult(0.5, increment)
        #       print thePoint, increment
        for i in range(numSets):
            means.append(
                helpers.add(thePoint,
                            helpers.mult(float(i + 0.5) / numSets, increment)))
        means = gen.meanInit(numSets)

#   print means
    pausebuffer = float(allio.sanitizeInput("Input the pause buffer here:"))
    means, sets = doKMeans3(points, means, pausebuffer)
示例#5
0
def main(numSets,accuracy,flag,length=3):
    if flag == 2:
        n = int(sanitizeInput("Input the block size here:")) 
        mostRandom = int(sanitizeInput("Print 1 for purely random, 0 for predefined clusters"))
        increaseN(n,mostRandom,numSets)
        return
    elif flag == 1:
        n = int(sanitizeInput("Input the sample size here:") )
        low = float(sanitizeInput("Input the range minimum here:"))
        high = float(sanitizeInput("Input the range maximum here:"))
        points = []
        theRange = high - low
        spacing = float(theRange)/numSets
        means = []
        thePoint = ()
        midpoint = ()
        for j in range(length-1):
            thePoint = thePoint + (0,)
            midpoint = midpoint + (1/(numSets-1)**0.5,)
        thePoint = thePoint + (1,)
        midpoint = midpoint + (0,)
        increment = helpers.add(thePoint,helpers.mult(-1,midpoint))
        for i in range(numSets):
            means.append(helpers.add(thePoint,helpers.mult(float(i+0.5)/numSets,increment)))
        for i in range(n):
            thePoint = ()
            j = 0
            while j < length:
                thePoint = thePoint + (rand.uniform(low,high),)
                j = j + 1
            points.append(thePoint)
    else:
        testnum = sanitizeInput("Input the test number here:")
        filepath = "../Testing/test"+testnum+".csv"
        points = fileio.readFile(filepath)
#       low = helpers.theMin(points)
#       high = helpers.theMax(points)
#       spacing = ()
#       for i in range(length):
#           spacing = spacing + ((high[i]-low[i])/numSets,)
        means = []
        n = len(points)
        thePoint = ()
        midpoint = ()
        for j in range(length-1):
            thePoint = thePoint + (0,)
            midpoint = midpoint + (1/float(length-1),)
        thePoint = thePoint + (1,)
        midpoint = midpoint + (0,)
        increment = helpers.add(midpoint,helpers.mult(-1,thePoint))
#       print thePoint, midpoint, increment
        thePoint = helpers.add(thePoint,helpers.mult(0.25,increment))
        increment = helpers.mult(0.5,increment)
#       print thePoint, increment
        for i in range(numSets):
            means.append(helpers.add(thePoint,helpers.mult(float(i+0.5)/numSets,increment)))

#   print means
    doKMeans(points,means)
def basic(sa, sb):
    return helpers.mult(sa, sb)
示例#7
0
modulus = 987898789

# For c = 1, the total is 10 * number of values for a and b, which is 10 * L * L
total = L * L * 10
for c in range(L, 1, -1):
	print c
	if c % 2 == 0:

				
		matrix = recurrence_matrix([10, 1])
		total += even_c_counts['n_equals_0'] * T(0, modulus)
		total += even_c_counts[0] * T(1, modulus)
		for exponent in range(1, L + 1):
			matrix = fast_power_iterative(matrix, c, modulus)
			xn = mult(matrix, x0, modulus)

			count = even_c_counts[exponent]

			total += count * xn[-1][0]
			total %= modulus
		'''
		for (key, count) in even_c_counts.items():
			if key == 'n_equals_0':
				total += count * T(0, modulus)
			else:
				total += count * T(c**key, modulus)
			total %= modulus
		'''	
	else:
		matrix = recurrence_matrix([10, 1])
示例#8
0
print len(odd_c_counts), len(even_c_counts)

modulus = 987898789

# For c = 1, the total is 10 * number of values for a and b, which is 10 * L * L
total = L * L * 10
for c in range(L, 1, -1):
    print c
    if c % 2 == 0:

        matrix = recurrence_matrix([10, 1])
        total += even_c_counts['n_equals_0'] * T(0, modulus)
        total += even_c_counts[0] * T(1, modulus)
        for exponent in range(1, L + 1):
            matrix = fast_power_iterative(matrix, c, modulus)
            xn = mult(matrix, x0, modulus)

            count = even_c_counts[exponent]

            total += count * xn[-1][0]
            total %= modulus
        '''
		for (key, count) in even_c_counts.items():
			if key == 'n_equals_0':
				total += count * T(0, modulus)
			else:
				total += count * T(c**key, modulus)
			total %= modulus
		'''
    else:
        matrix = recurrence_matrix([10, 1])
示例#9
0
from helpers import fast_power, mult

n = 10 ** 12
modulus = 10 ** 9

A = [[1, 1, 1, 1, 1, 1], [6, 1, 1, 1, 1, 1], [0, 5, 1, 1, 1, 1], [0, 0, 4, 1, 1, 1], [0, 0, 0, 3, 1, 1], [0, 0, 0, 0, 2, 1]]
x1 = [[7], [0], [0], [0], [0], [0]]

# n - 1 since we have x1 instead of x0.
print sum([i[0] for i in mult(fast_power(A, n - 1, modulus), x1, modulus)]) % modulus