def test_time_size(): """A test that compares the time cost changes of Strassen Algorithm and common multiplication as the sizes of matrices increase. """ for size in range(300): X = np.random.rand(size, size) Y = np.random.rand(size, size) t_strassen_begin = time.clock() strassen.strassen(X, Y) t_strassen_end = time.clock() t_strassen = t_strassen_end - t_strassen_begin t_mat_mul_begin = time.clock() mat_mul(X, Y) t_mat_mul_end = time.clock() t_mat_mul = t_mat_mul_end - t_mat_mul_begin print(size, t_strassen, t_mat_mul)
def test_time_algorithm(): """A test that compare the time cost of different algorithms. """ size = 256 X = np.random.rand(size, size) Y = np.random.rand(size, size) t_strassen_begin = time.clock() strassen.strassen(X, Y) t_strassen_end = time.clock() print(t_strassen_end - t_strassen_begin) t_mat_mul_begin = time.clock() mat_mul(X, Y) t_mat_mul_end = time.clock() print(t_mat_mul_end - t_mat_mul_begin) t_np_begin = time.clock() np.dot(X, Y) t_np_end = time.clock() print(t_np_end - t_np_begin)
def test_validation(): """A simple test for the validation of the algorithm. """ X = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]) Y = np.array([[1, 3, 5, 7], [2, 4, 6, 8], [3, 5, 7, 9], [4, 6, 8, 10], [5, 7, 9, 11]]) print(strassen.strassen(X, Y)) print(np.dot(X, Y))
def testStrassenOdd(self): A = [[1, 2, 3], [2, 3, 4], [3, 4, 5]] B = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] expected = [[6, 6, 6], [9, 9, 9], [12, 12, 12]] self.assertEqual(strassen.strassen(A, B), expected)
def testStrassenEven(self): A = [[1, 1], [1, 1]] B = [[1, 2], [3, 4]] expected = [[4, 6], [4, 6]] self.assertEqual(strassen.strassen(A, B), expected)
def strassen_seuil(matrix_a, matrix_b, seuil): return strassen(matrix_a, matrix_b, seuil)
def test_strassen_multiplication(self): expected_matrix = [[130, 100, 81, 106], [86, 78, 49, 72], [475, 547, 329, 538], [115, 86, 73, 101]] self.assertEqual(strassen(self.big_m_1, self.big_m_2), expected_matrix)
def create_new_matrices(self): self.matrix_a = generate_random_matrix(self.size) self.matrix_b = generate_random_matrix(self.size) self.matrix_c = strassen(self.matrix_a, self.matrix_b)
choices=["conv", "strassen", "strassenSeuil"]) parser.add_argument("-e1", type=str) parser.add_argument("-e2", type=str) parser.add_argument("-p", action="store_true") parser.add_argument("-t", action="store_true") args = parser.parse_args() m1 = util.readMatrix(args.e1) m2 = util.readMatrix(args.e2) init = time.time() if args.a == "conv": m3 = conv.conv(m1, m2) elif args.a == "strassen": m3 = strassen.strassen(m1, m2) elif args.a == "strassenSeuil": m3 = strassenSeuil.strassenSeuil(m1, m2, 2) end = time.time() if args.p: s = str(m3) s = re.sub(r"], ", "\n", s) s = re.sub(r"[\[\],]+", " ", s) s = re.sub(r"^ ", "", s, flags=re.MULTILINE) s = re.sub(r"\.*", "", s) print(s) if args.t: print((end - init) * 1000)
#!/usr/bin/python # Core Library modules from math import ceil, log from optparse import OptionParser from strassen import print_matrix, read, strassen if __name__ == "__main__": parser = OptionParser() parser.add_option( "-i", dest="filename", default="2000.in", help="input file with two matrices", metavar="FILE", ) (options, args) = parser.parse_args() A, B = read(options.filename) C = strassen(A, B) print_matrix(C)
if __name__ == '__main__': # Get the command line arguments infile = sys.argv[1] outfile = sys.argv[2] # Reinitialize output file if it already exists with open(outfile, 'w+') as file: file.write('') # Do the things try: for size, matrixA, matrixB in read(infile): productB = multiply(matrixA, matrixB) print("Successfully multiplied!") productS = strassen(matrixA, matrixB) print("Successfully (Strassen) multiplied!") assert (productB == productS) # sanity check write(matrixA, matrixB, productB, productS, outfile) # Need to tell the user so they know to check the file except FileNotFoundError as e: print(str(e)) # Let the user know an error was encountered (should be on their end) except Exception as e: print("Something went wrong. The input data is likely has a " + \ "formatting issue.") print("Make sure the input only consists of " + \
def testStrassenOdd(self): A = [[1,2,3],[2,3,4],[3,4,5]] B = [[1,1,1],[1,1,1],[1,1,1]] expected = [[6,6,6],[9,9,9],[12,12,12]] self.assertEqual(strassen.strassen(A,B), expected)
def testStrassenEven(self): A = [[1,1],[1,1]] B = [[1,2],[3,4]] expected = [[4,6],[4,6]] self.assertEqual(strassen.strassen(A,B), expected)