def test_svd(self): mtx = ch.array(np.random.randn(100).reshape((10, 10))) # Get times for svd from linalg import svd u, s, v = svd(mtx) def setup(): mtx.x = -mtx.x def go_r(): _ = u.r _ = s.r _ = v.r def go_dr(): _ = u.dr_wrt(mtx) _ = s.dr_wrt(mtx) _ = v.dr_wrt(mtx) cht_r = timer(setup, go_r, 20) cht_dr = timer(setup, go_dr, 1) # Get times for numpy svd def go(): u, s, v = np.linalg.svd(mtx.x) npt = timer(setup=None, go=go, n=20) # Compare #print cht_r / npt #print cht_dr / npt self.assertLess(cht_r / npt, 3.3) self.assertLess(cht_dr / npt, 2700)
def test_svd(self): mtx = ch.array(np.random.randn(100).reshape((10,10))) # Get times for svd from linalg import svd u, s, v = svd(mtx) def setup(): mtx.x = -mtx.x def go_r(): _ = u.r _ = s.r _ = v.r def go_dr(): _ = u.dr_wrt(mtx) _ = s.dr_wrt(mtx) _ = v.dr_wrt(mtx) cht_r = timer(setup, go_r, 20) cht_dr = timer(setup, go_dr, 1) # Get times for numpy svd def go(): u,s,v = np.linalg.svd(mtx.x) npt = timer(setup = None, go = go, n = 20) # Compare #print cht_r / npt #print cht_dr / npt self.assertLess(cht_r / npt, 3.3) self.assertLess(cht_dr / npt, 2700)
def test_svd(self): matrix = self.MATRIX_2 S,V,D = np.linalg.svd(matrix) S2, V2, D2 = linalg.svd(matrix) self.assertTrue(self.check_simmilarity(S, S2)) self.assertTrue(self.check_list(V, V2)) self.assertTrue(self.check_simmilarity(D, D2))
def check_simple_complex(self): a = [[1,2,3],[1,2j,3],[2,5,6]] u,s,vh = svd(a) assert_array_almost_equal(dot(conj(transpose(u)),u),identity(3)) assert_array_almost_equal(dot(conj(transpose(vh)),vh),identity(3)) sigma = zeros((u.shape[0],vh.shape[0]),s.dtype.char) for i in range(len(s)): sigma[i,i] = s[i] assert_array_almost_equal(dot(dot(u,sigma),vh),a)
def check_simple_overdet(self): a = [[1,2],[4,5],[3,4]] u,s,vh = svd(a) assert_array_almost_equal(dot(transpose(u),u),identity(3)) assert_array_almost_equal(dot(transpose(vh),vh),identity(2)) sigma = zeros((u.shape[0],vh.shape[0]),s.dtype.char) for i in range(len(s)): sigma[i,i] = s[i] assert_array_almost_equal(dot(dot(u,sigma),vh),a)
def check_random(self): n = 20 m = 15 for i in range(3): for a in [random([n,m]),random([m,n])]: u,s,vh = svd(a) assert_array_almost_equal(dot(transpose(u),u),identity(len(u))) assert_array_almost_equal(dot(transpose(vh),vh),identity(len(vh))) sigma = zeros((u.shape[0],vh.shape[0]),s.dtype.char) for i in range(len(s)): sigma[i,i] = s[i] assert_array_almost_equal(dot(dot(u,sigma),vh),a)
def check_random_complex(self): n = 20 m = 15 for i in range(3): for a in [random([n,m]),random([m,n])]: a = a + 1j*random(list(a.shape)) u,s,vh = svd(a) assert_array_almost_equal(dot(conj(transpose(u)),u),identity(len(u))) # This fails when [m,n] #assert_array_almost_equal(dot(conj(transpose(vh)),vh),identity(len(vh),dtype=vh.dtype.char)) sigma = zeros((u.shape[0],vh.shape[0]),s.dtype.char) for i in range(len(s)): sigma[i,i] = s[i] assert_array_almost_equal(dot(dot(u,sigma),vh),a)
def get_graph_stoich(alpha, beta, complex_names = None, constant_names = None): # number of reactions = number of columns of alpha no_rxn = len(alpha[0]) # number of substance = number of rows of alpha no_sub = len(alpha) # stoichiometry matrix stoich = [] for row_i in range(no_sub): stoich_row = [] for col_i in range(no_rxn): stoich_row.append(beta[row_i][col_i] - alpha[row_i][col_i]) stoich.append(stoich_row) # rank of stoichiometry matrix if no_sub < no_rxn: # need to transpose stoich for svd call stoich_tr = [] for i in range(no_rxn): stoich_tr_row = [] for j in range(no_sub): stoich_tr_row.append(stoich[j][i]) stoich_tr.append(stoich_tr_row) matrix_U, sing_vals, matrix_V_t = linalg.svd(stoich_tr) else: matrix_U, sing_vals, matrix_V_t = linalg.svd(stoich) # tol computation lent from NumPy # https://github.com/numpy/numpy/blob/85b83e6938fa6f5176eaab8e8fd1652b27d53aa0/numpy/linalg/linalg.py#L1513 tol = max(sing_vals) * max(no_rxn, no_sub) * sys.float_info.epsilon stoich_rank = sum([1 for s in sing_vals if s > tol]) # make directed graph G = nx.DiGraph() # add nodes to graph: add all reactions 'w' and substances 's' if constant_names == None: G.add_nodes_from(['w'+str(n) for n in range(1,no_rxn+1)], bipartite=1) else: G.add_nodes_from([constant_names[n-1] for n in range(1,no_rxn+1)], bipartite=1) if complex_names == None: G.add_nodes_from(['s'+str(n) for n in range(1,no_sub+1)], bipartite=0) else: G.add_nodes_from([complex_names[n-1] for n in range(1,no_sub+1)], bipartite=0) # add ('s','w') edges, i.e. positive element in alpha for row_i in range(no_sub): curr_row = alpha[row_i] for col_i in range(no_rxn): curr_coeff = curr_row[col_i] if curr_coeff > 0: if constant_names == None and complex_names == None: G.add_edge('s'+str(row_i+1), 'w'+str(col_i+1), coeff=curr_coeff) elif constant_names == None and complex_names != None: G.add_edge(complex_names[row_i], 'w'+str(col_i+1), coeff=curr_coeff) elif constant_names != None and complex_names == None: G.add_edge('s'+str(row_i+1), constant_names[col_i], coeff=curr_coeff) elif constant_names != None and complex_names != None: G.add_edge(complex_names[row_i], constant_names[col_i], coeff=curr_coeff) else: raise # add ('w','s') edges, i.e. positive element in beta for row_i in range(no_sub): curr_row = beta[row_i] for col_i in range(no_rxn): curr_coeff = curr_row[col_i] if curr_coeff > 0: if constant_names == None and complex_names == None: G.add_edge('w'+str(col_i+1), 's'+str(row_i+1), coeff=curr_coeff) elif constant_names == None and complex_names != None: G.add_edge('w'+str(col_i+1), complex_names[row_i], coeff=curr_coeff) elif constant_names != None and complex_names == None: G.add_edge(constant_names[col_i], 's'+str(row_i+1), coeff=curr_coeff) elif constant_names != None and complex_names != None: G.add_edge(constant_names[col_i], complex_names[row_i], coeff=curr_coeff) else: raise return G, stoich, stoich_rank