def levenberg_iteration(z, z_p, z_l, x_p, x_l, f_p, f_l, C, lamb): """ """ x = build_zx(x_p, x_l) J = build_jacobian(z, x_p, x_l, f_p, f_l) #J = build_jacobian_numeric(z, x_p,x_l,f_p,f_l) Jsp = J.tocsr() Csp = C.tocsr() alpha = Jsp.T.dot(Csp.dot(Jsp)) fy_i = [] for i, f in zip(z_p, f_p): l1 = matrix([i[0], i[1], i[2]]).T l2 = f[0](x_p[f[1]], x_p[f[2]]) result = l1 - l2 fy_i += result.T.tolist()[0] for i, f in zip(z_l, f_l): l1 = matrix([[i[0]], [i[1]]]) l2 = f[0](x_p[f[1]], x_l[f[2]]) result = l1 - l2 fy_i += result.T.tolist()[0] fy_i = array(fy_i, dtype=float64).T beta = Jsp.T.dot(Csp.dot(fy_i)) # Augmented alpha alpha.setdiag(sparse.extract_diagonal(alpha) * (1 + lamb)) #### sparse solver return linsolve.spsolve(alpha, beta)
def check_extract_diagonal(self): """ Test extraction of main diagonal from sparse matrices """ L = [] L.append(array([[0,0,3],[1,6,4],[5,2,0]])) L.append(array([[1,2,3]])) L.append(array([[7],[6],[5]])) L.append(array([[2]])) for A in L: assert_array_equal(numpy.diag(A),extract_diagonal(self.spmatrix(A)))
def diag_sparse(A): """ If A is a sparse matrix (e.g. csr_matrix or csc_matrix) - return the diagonal of A as an array Otherwise - return a csr_matrix with A on the diagonal """ if isspmatrix(A): return extract_diagonal(A) else: return csr_matrix((A,arange(len(A)),arange(len(A)+1)),(len(A),len(A)))
def __init__(self, model_graph, node_sizes, clqs, lattice=False): """ Initializes MRF object. model_graph: Numpy array or Scipy.sparse matrix A matrix defining the edges between nodes in the network. If graph[i, j] = 1 there exists a undirected edge from node i to j. node_sizes: List or Int A list of the possible number of values a discrete node can have. If node_sizes[i] = 2, then the discrete node i can have one of 2 possible values, such as True or False. If this parameter is passed as an integer, it indicates that all nodes have the size indicated by the integer. clqs: List of clique objects (cliques.py) A list of the cliques in the MRF. lattice: Bool Lattice is true if this MRF has a lattice graph structure, and false otherwise. """ """Assign the input values to their respective internal data members""" self.lattice = lattice self.num_nodes = model_graph.shape[0] self.cliques = clqs self.node_sizes = node_sizes """Convert the graph to a sparse matrix""" if ((type(model_graph) == type(np.matrix([0]))) or (type(model_graph) == type(np.array([0])))): model_graph = sparse.lil_matrix(model_graph) """In an MRF, all edges are bi-directional""" self.model_graph = model_graph - \ sparse.lil_diags([sparse.extract_diagonal(\ model_graph)], [0], (model_graph.shape[0], \ model_graph.shape[0]))\ + model_graph.T """ Obtain elimination order, which is just the input order in the case of a lattice. """ if self.lattice == True: self.order = range(0, self.model_graph.shape[0]) else: self.order = graph.topological_sort(self.model_graph)
def petsc_binary_write(A, filename): fd = open(filename, "wb") if np.rank(A) == 1: # is vector m = np.shape(A)[0] n = 1 elif np.rank(A) == 2: # is matrix m = np.shape(A)[0] n = np.shape(A)[1] if sparse.issparse(A): majic = 1.2345678910e-30 diag = sparse.extract_diagonal(A) for i in xrange(len(diag)): if diag[i] == 0: diag[i] = majic nz = sparse.spmatrix.getnnz(A) harr = np.array([1211216, m, n, nz]) fwrite(fd, np.size(harr), harr, "i") # nz_per_row = # write nz_per_row # fwrite(fd,n_nz,'int32'); %nonzeros per row # [i,j,s] = find(A'); # fwrite(fd,i-1,'int32'); # for i=1:nz # if s(i) == majic # s(i) = 0; # end # end # fwrite(fd,s,'double'); else: size = m * n harr = np.array([code_vector, size]) # header fwrite(fd, 2, harr, "i", 1) fwrite(fd, size, A, "d", 1) pdb.set_trace() fd.close()