import numpy as np import matplotlib.pyplot as plt from etraj.etraj import Vector # tests for vector class print("\n-----------Tests for Vector class--------------") print("\n-----------------------------------------------") print("\n(1) Constructors") print("\n(1a) Basic Constructor") print(">>> v = Vector()") print(">>> print(v)") v = Vector() print(v) print("\n(1b) Constructor with dim") print(">>> dim = 6") print(">>> v = Vector(dim)") print(">>> print(v)") dim = 6 v = Vector(dim) print(v) print("\n(1c) Constructor with name and dim") print(">>> name = 'v'") print(">>> dim = 5") print(">>> v = Vector(name,dim)") print(">>> print(v)") name = 'v' dim = 5
import etraj.etraj as et # tests for matrix class print("\n-------Tests for DDOT speed vs. Scipy--------") print("\n----------------------------------------------") native_times = [] scipy_times = [] dims = [] N = 1 num_iters = 1000 for i in range(num_iters): # generate random vector x_vec = np.random.normal(0, 1, N) x_et = Vector('x', x_vec) y_vec = np.random.normal(0, 1, N) y_et = Vector('y', y_vec) # run dgemm in ET start = time.time() s = et.ddot(x_et, y_et) end = time.time() native_times.append(end - start) # run dgemv in scipy start = time.time() s = blas.ddot(x_vec, y_vec) end = time.time() scipy_times.append(end - start)
from scipy.linalg import blas import etraj.etraj as et # tests for vector class print("\n-------Tests for Level 1 BLAS functions--------") print("\n-----------------------------------------------") print("\n(1) DSWAP") print(">>> v = Vector('v',[1,2,3])") print(">>> u = Vector('u',[3,2,1])") print(">>> print(v)") print(">>> print(u)") print(">>> et.dswap(v,u)") print(">>> print(v)") print(">>> print(u)") v = Vector('v', [1, 2, 3]) u = Vector('u', [3, 2, 1]) print(v) print(u) et.dswap(v, u) print(v) print(u) print("\n(2) DSCAL") print(">>> v = Vector('v',[1,2,3,4])") print(">>> s = 5.3") print(">>> print(v)") print(">>> et.dscal(v,s)") print(">>> print(v)") v = Vector('v', [1, 2, 3, 4]) s = 5.3
f_derr = [] mon = Monomial(g.get_dim(),n) for i in range(len(x)): point = g[i] print("Point: ",point) neighbors = g.get_neighbors(i) print("Neighbors: ", neighbors) b = [] for j in range(len(neighbors)): b.append(mon.taylor_monomial_expansion(point,g[neighbors[j]])) b_matrix = Matrix('b',b) print("B Matrix: ") print(b_matrix) f_neighbors = [f[m] for m in neighbors] v = Vector(f_neighbors) print("Vector: ") print(v) print("References to b_matrix: ", sys.getrefcount(b_matrix)) print("Location of b_matrix: ", hex(id(b_matrix))) print("References to v: ", sys.getrefcount(v)) print("Location of v: ", hex(id(v))) f_app = et.dgels(b_matrix,v) f_der.append(f_app[1]) f_derr.append(f_app[2]) print("Location of b_matrix", hex(id(b_matrix))) print("Location of v", hex(id(v))) print("Location of g", hex(id(g))) print("References to g: ", sys.getrefcount(g)) print("References to b: ", sys.getrefcount(b))
import time import etraj.etraj as et # tests for matrix class print("\n-------Tests for DGEMV speed vs. Scipy--------") print("\n----------------------------------------------") native_times = [] scipy_times = [] dims = [] N = 2 num_iters = 1000 for i in range(num_iters): # generate random vector x_vec = np.random.normal(0, 1, N) x_et = Vector('x', x_vec) # generate random matrix A = [[np.random.normal(0, 1, 1)[0] for j in range(N)] for i in range(N)] A_et = Matrix('A', A) alpha = 1.0 # run dgemv in ET start = time.time() y_et = et.dgemv(alpha, A_et, x_et) end = time.time() native_times.append(end - start) # run dgemv in scipy start = time.time() y_scipy = blas.dgemv(alpha, A, x_vec) end = time.time()
+ " [3,5,2],\n" + " [4,2,5],\n" + " [5,4,3]]") print(">>> b = [1,2,3,4,5]") print(">>> A = Matrix('A',a)") print(">>> v = Matrix('v',b)") print(">>> u = et.dgels(A,B)") print(">>> print(C)") a = [[1,1,1], [2,3,4], [3,5,2], [4,2,5], [5,4,3]] b = [1,2,3,4,5] A = Matrix('A',a) v = Vector('v',b) u = et.dgels(A,v) print(A) print(v) print(u) ''' Same examples as (1b) and (1c) except using DGELSY as the driver. ''' print("\n(1d) - DGELSY") print("Solve the linear system ac = b, with\n") print(">>> a = [[1,1,1],\n" + " [2,3,4],\n" + " [3,5,2],\n" + " [4,2,5],\n"
from etraj.etraj import Matrix, Vector from scipy.linalg import blas import etraj.etraj as et # tests for matrix class print("\n-------Tests for Level 2 BLAS functions--------") print("\n-----------------------------------------------") print("\n(1a) - DGEMV") print(">>> x = Vector('x',[1,2])") print(">>> A = Matrix('A',[[1,1],[1,1]])") print(">>> alpha = 1.1") print(">>> y = et.dgemv(alpha,A,x)") print(">>> print(y)") x = Vector('x', [1, 2]) A = Matrix('A', [[1, 1], [1, 1]]) alpha = 1.1 y = et.dgemv(alpha, A, x) print(y) print("\nScipy version") print(">>> x = [1,2]") print(">>> A = [[1,1],[1,1]]") print(">>> y_scipy = blas.dgemv(alpha,A,x)") print(">>> print(y_scipy)") x = [1, 2] A = [[1, 1], [1, 1]] y_scipy = blas.dgemv(alpha, A, x) print(y_scipy) print("\n(1b) - DGEMV with y and beta")