import sys import time import numpy as np import testclass n0 = 2000 n1 = 2000 print("===== Pythran =====") time0 = time.time() import matrix_multiply time1 = time.time() A = [[((i + j * 0.1) * 0.1) for i in xrange(n0)] for j in xrange(n1)] B = [[((j + i * 0.1) * 0.1) for i in xrange(n0)] for j in xrange(n1)] time2 = time.time() mat = matrix_multiply.matrix_multiply(A, B) time3 = time.time() N = np.linalg.norm(mat, ord='fro') time4 = time.time() print("init. time: %fs" % (time1 - time0)) print("fill. time: %fs" % (time2 - time1)) print("mult. time: %fs" % (time3 - time2)) print("norm. time: %fs" % (time4 - time3)) print("TOTAL time: %fs" % (time4 - time0)) print("result : %20.14e" % (N)) print("===== Numpy 1a =====") time0 = time.time() A = np.empty((n0, n1)) B = np.empty((n1, n0)) for i in range(n0):
def transform(self, type, x, y="R"): ''' Usage: Polygon.transform("R", theta) By calling this way, the object is rotated about the origin by the angle given by theta in degrees. Rotation is done anticlockwise. Polygon.transform("S", x_factor, y_factor) This scales the Object with respect to the standard axis given by the factors x_factor for x-axis and y_favctor for y-axis. Both accept floating point numbers Polygon.transform("T", x_add, y_add) This translates the object by x_add units in the x-axis direction and y_add units in the y-axis direction. All of the three transformations are performed as matrix multiplications in homogeneous coordinates in 2 dimensions represented as (n+1)-tuples. ''' if y == "R": #Rotation transform_matrix = [[cos(x), -sin(x), 0], [sin(x), cos(x), 0], [0, 0, 1]] else: if type == "S": #Scaling transform_matrix = [[x, 0, 0], [0, y, 0], [0, 0, 1]] elif type == "T": #Transformation transform_matrix = [[1, 0, x], [0, 1, y], [0, 0, 1]] x_list = [] y_list = [] self.max_x = -inf self.max_y = -inf #perform transformation on eevery point in the polygon for i in range(len(self.coordinates["x"])): coord_matrix = [[self.coordinates["x"][i]], [self.coordinates["y"][i]], [1]] updated_coord_matrix = matrix_multiply(transform_matrix, coord_matrix) x_list.append(updated_coord_matrix[0][0]) y_list.append(updated_coord_matrix[1][0]) #Update the max values of x and y coordinates to display the polygon to be used for plotting. if abs(updated_coord_matrix[0][0]) > self.max_x: self.max_x = abs(updated_coord_matrix[0][0]) if abs(updated_coord_matrix[1][0]) > self.max_y: self.max_y = abs(updated_coord_matrix[1][0]) #For Circle type object, which is a special case of ellipse, three points, namely, the center, the major axis vertex and the minor axis vertex need to be calculated to keep track of the major axis and minor axis values between transformations if not self.type: coord_matrix = [[self.special_points["P"][0]], [self.special_points["P"][1]], [1]] updated_coord_matrix = matrix_multiply(transform_matrix, coord_matrix) self.special_points["P"] = [ updated_coord_matrix[0][0], updated_coord_matrix[1][0] ] coord_matrix = [[self.special_points["Q"][0]], [self.special_points["Q"][1]], [1]] updated_coord_matrix = matrix_multiply(transform_matrix, coord_matrix) self.special_points["Q"] = [ updated_coord_matrix[0][0], updated_coord_matrix[1][0] ] coord_matrix = [[self.special_points["C"][0]], [self.special_points["C"][1]], [1]] updated_coord_matrix = matrix_multiply(transform_matrix, coord_matrix) self.special_points["C"] = [ updated_coord_matrix[0][0], updated_coord_matrix[1][0] ] self.coordinates["x"] = x_list self.coordinates["y"] = y_list
def test_dimension(self): left = np.array([[1, 2], [3, 4]]) right = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) with self.assertRaises(Exception): matrix_multiply(left, right)
def test_uneven(self): left = np.array([[2, 0, 4], [0, 8, 0]]) right = np.array([[1], [0], [3]]) np.testing.assert_array_equal(np.array([[14], [0]]), matrix_multiply(left, right))
def test_identity(self): left = np.array([[1, 0], [0, 1]]) right = np.array([[5, 2], [3, 4]]) np.testing.assert_array_equal(right, matrix_multiply(left, right))
def test_dot(self): left = np.array([[1, 0, 2]]) right = np.array([[5], [0], [3]]) np.testing.assert_array_equal(np.array([[11]]), matrix_multiply(left, right))