def rand_dp_nc_matrix(*args, **kwargs): dp_mat, nc_mat = None, None if len(kwargs) == 0: dp_mat, nc_mat = dp.Matrix(*args), nc.Matrix(*args) else: dp_mat, nc_mat = dp.Matrix(*args, **kwargs), nc.Matrix(*args, **kwargs) return dp_mat, nc_mat
def test_set(self): # TODO: YOUR CODE HERE mat1 = nc.Matrix(3, 3) mat2 = nc.Matrix(3, 3, 1) for i in range(0, 2): for j in range(0, 2): self.assertEqual(mat1.get(i, j), mat2.set(i, j, 0)) pass
def addTime(): print("=" * 60) print("{:^54}".format("test Add speed")) print("{:>34}{:>10}{:>10}".format(" ", "numc", "NumPy")) print("=" * 60) size = [1000, 2000, 5000, 10000, 20000] for s in size: mat1 = nc.Matrix(s, s, rand=True) mat2 = nc.Matrix(s, s, rand=True) start = time.thread_time() res = mat1 + mat2 end = time.thread_time() t1 = end - start
def rand_dp_nc_matrix(rows, cols, low=0, high=1, seed=0): return dp.Matrix(rows, cols, low=low, high=high, rand=True, seed=seed), nc.Matrix(rows, cols, low=low, high=high, rand=True, seed=seed)
def subTime(): print("=" * 60) print("{:^59}".format("test Subtract speed")) print("{:>39}{:>10}{:>10}".format(" ", "numc", "NumPy")) print("=" * 60) size = [100, 1000, 2000, 5000, 10000] for s in size: mat1 = nc.Matrix(s, s, rand=True) mat2 = nc.Matrix(s, s, rand=True) start = time.time() res = mat1 - mat2 end = time.time() t1 = end - start mat1 = np.random.random((s, s)) mat2 = np.random.random((s, s)) start = time.time() res = mat1 - mat2 end = time.time() print("Subtract two {:6d} by {:6d} matrices {:10.3f} {:10.3f}".format( s, s, t1, end - start))
def mulTime(): print("=" * 60) print("{:^59}".format("test Multiplication speed")) print("{:>39}{:>10}{:>10}".format(" ", "numc", "NumPy")) print("=" * 60) size = [100, 1000, 2000, 5000] for s in size: mat1 = nc.Matrix(s, s, rand=True) mat2 = nc.Matrix(s, s, rand=True) start = time.time() res = mat1 * mat2 end = time.time() t1 = end - start mat1 = np.random.random((s, s)) mat2 = np.random.random((s, s)) start = time.time() res = mat1 @ mat2 end = time.time() print("Multiply two {:6d} by {:6d} matrices {:10.3f} {:10.3f}".format( s, s, t1, end - start))
def negTime(): print("=" * 60) print("{:^55}".format("test Negation speed")) print("{:>35}{:>10}{:>10}".format(" ", "numc", "NumPy")) print("=" * 60) size = [100, 1000, 2000, 5000, 10000] for s in size: mat1 = nc.Matrix(s, s, rand=True) start = time.time() res = -mat1 end = time.time() t1 = end - start mat1 = np.random.random((s, s)) start = time.time() res = np.negative(mat1) end = time.time() print("Negate a {:6d} by {:6d} matrices {:10.3f} {:10.3f}".format( s, s, t1, end - start))
def test_large_pow(self): # TODO: YOUR CODE HERE size = 4 mat1 = nc.Matrix(size, size, 0.3) nc_start = time.time() mat2 = mat1**20 for i in range(size): for j in range(size): assert mat2[i][j] == approx(9.584399981118684) mat3 = mat1**50 for i in range(size): for j in range(size): assert mat3[i][j] == approx(2275.1095375005348) mat1 = mat1**100 for i in range(size): for j in range(size): assert mat1[i][j] == approx(20704493.630503587) nc_end = time.time() print('{} Seconds'.format(nc_end - nc_start))
def absTime(): print("=" * 60) print("{:^57}".format("test Abs speed")) print("{:>37}{:>10}{:>10}".format(" ", "numc", "NumPy")) print("=" * 60) size = [100, 1000, 2000, 5000, 10000] for s in size: mat1 = nc.Matrix(s, s, rand=True) start = time.time() res = abs(mat1) end = time.time() t1 = end - start mat1 = np.random.random((s, s)) start = time.time() res = np.abs(mat1) end = time.time() print("Absolute a {:6d} by {:6d} matrices {:10.3f} {:10.3f}".format( s, s, t1, end - start))
def powTime(): print("=" * 65) print("{:^65}".format("test Power speed")) print("{:>45}{:>10}{:>10}".format(" ", "numc", "NumPy")) print("=" * 65) #size = [100, 1000, 2000, 5000, 10000] size = [100, 1000] pth = [0, 1, 2, 5, 10, 100] for s in size: for p in pth: mat1 = nc.Matrix(s, s, rand=True) start = time.time() res = mat1**p end = time.time() t1 = end - start mat1 = np.random.random((s, s)) start = time.time() res = np.power(mat1, p) end = time.time() print("{:5d}th power a {:5d} by {:6d} matrices {:10.3f} {:10.3f}". format(p, s, s, t1, end - start))
import numc as nc # This creates a 3 * 3 matrix with entries all zeros mat1 = nc.Matrix(3, 3) # This creates a 2 * 3 matrix with entries all ones mat2 = nc.Matrix(3, 3, 1) # This creates a 2 * 3 matrix with first row 1, 2, 3, second row 4, 5, 6 mat3 = nc.Matrix([[1, 2, 3], [4, 5, 6]]) # This creates a 1 * 2 matrix with entries 4, 5 mat4 = nc.Matrix(1, 2, [4, 5]) # mat1 print(mat1[0]) # this gives the 0th row of mat1, should print out [[0.0], [0.0], [0.0]] print(mat1[0][1]) # this should print out 0 mat1[0][1] = 5 print(mat1) # this should print out [[0.0, 5.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] mat1[0] = [4, 5, 6] print(mat1) # this should print out [[4.0, 5.0, 6.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] # mat2 print(mat2) # [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]] mat2[1][1] = 2 print(mat2[1]) # [[1.0], [2.0], [1.0]] # You can change a value in a slice, and that will change the original matrix print(mat2) # [[1.0, 1.0, 1.0], [1.0, 2.0, 1.0], [1.0, 1.0, 1.0]] mat2_slice = mat2[0] # [[1.0], [1.0], [1.0]] mat2_slice[0] = 5 print(mat2_slice) # [[5.0], [1.0], [1.0]]
import numc as nc m1 = nc.Matrix(3, 3, 0.1) m2 = nc.Matrix(3, 3, 1.0) print("m1 + m2 = ") print(m1 + m2) print("m1 - m2 = ") print(m1 - m2) print("m1 * m2 = ") print(m1 * m2) print("-m1 = ") print(-m1) print("abs(-m1) = ") print(abs(-m1)) print("m1 ** 0 = ") print(m1**0) print("m1 ** 2 = ") print(m1**2) print("m1.get(1, 1): ", m1.get(1, 1)) print("set m1[1][1] = 2") m1.set(1, 1, 2) print("m1.get(1, 1): ", m1.get(1, 1))
import numc as nc testMat = nc.Matrix(3, 3) expect1 = [0.0, 0.0, 0.0] actual1 = testMat[0] print("Should be", expect1, "it was", actual1) expect2 = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] actual2 = testMat[0:2] print("Should be", expect2, "it was", actual2) expect3 = [[0.0, 0.0], [0.0, 0.0]] actual3 = testMat[0:2, 0:2] print("Should be", expect3, "it was", actual3) expect4 = [0.0, 0.0] actual4 = testMat[0:2, 0] print("Should be", expect4, "it was", actual4) expect5 = [0.0, 0.0] actual5 = testMat[0, 0:2] print("Should be", expect5, "it was", actual5) print("Should be", 0.0, "it was", testMat[0, 0]) #not sure why this doesnt do 0.0? testMat2 = nc.Matrix(1, 3) expect6 = 0.0 actual6 = testMat2[0] print("Should be", expect6, "it was", actual6) expect7 = [0.0, 0.0] actual7 = testMat2[0:2] print("Should be", expect7, "it was", actual7) testMat3 = nc.Matrix(3, 3, 2) expect8 = 2.0 actual8 = testMat3[0][1] print("Should be", expect8, "it was", actual8) expect9 = 2.0
def dp_nc_matrix(*args, **kwargs): if len(kwargs) > 0: return dp.Matrix(*args, **kwargs), nc.Matrix(*args, **kwargs) else: return dp.Matrix(*args), nc.Matrix(*args)
import numc as nc import dumbpy as dp import pdb import random import utils a = nc.Matrix(17, 5, 3.3) da = dp.Matrix(17, 5, 3.3) b = nc.Matrix(5, 17, 9) db = dp.Matrix(5, 17, 9) c = nc.Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, -9.9]) dc = dp.Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, -9.9]) e = nc.Matrix(1, 2, [1, 2]) f = nc.Matrix(1, 1, [3]) g = nc.Matrix(2, 2, [1, 2, 3, -4]) a.get(0, 0) # pdb.set_trace() # b[0] # a[1] # #a[0:0] # #a[0,0] # db[-1:] # a[0:1] = [1,1] # c[0:2,1] # print("mul check") # print("b*c == db * dc", b*c == db*dc) #set sanity z = nc.Matrix(2, 2, [1.1, 2.2, 3.3, -4.4]) x = dp.Matrix(2, 2, [1.1, 2.2, 3.3, -4.4])
def rand_dp_nc_matrix(rows, cols, seed=0): return dp.Matrix(rows, cols, rand=True, seed=seed), nc.Matrix(rows, cols, rand=True, seed=seed)
a[4] = [5,4,6] except IndexError: print("dumbpy index error seen") try: b[4] = [5,4,6] except IndexError: print("numc index error seen") except: print("numc index error mismatch") ''' #*********** #SLICE SLICE ERROR a = dp.Matrix(3,3,2) b = nc.Matrix(3,3,2) try: a[0:1, 0:2] = [5] except ValueError: print("dumbpy value error seen") try: b[0:1, 0:2] = [5] except ValueError: print("TRUE VAL ERROR") except: print("FALSE VAL ERROR") #~~~~~ try: a[0:1, 0:2] = 78
lst6.append([random.uniform(-i, j)]) lst7 = [] for i in range(50): sub = [] for j in range(50): sub.append(random.uniform(-i, j)) lst7.append(sub) lst8 = [] for i in range(50): lst8.append([random.uniform(-i, j)]) print("Lists Made") fast_mat_large = numc.Matrix(lst1) fast_vec_large = numc.Matrix(lst2) fast_mat_med = numc.Matrix(lst3) fast_vec_med = numc.Matrix(lst4) fast_mat_weird = numc.Matrix(lst5) fast_vec_weird = numc.Matrix(lst6) fast_mat_small = numc.Matrix(lst7) fast_vec_small = numc.Matrix(lst8) slow_mat_large = dumbpy.Matrix(lst1) slow_vec_large = dumbpy.Matrix(lst2) slow_mat_med = dumbpy.Matrix(lst3)
import numc as nc mat = nc.Matrix(9, 9) print(mat[5, 1:-1])