def demo(KSolver, mtx, **kwargs): hdr_fmt = '%10s %6s %8s %8s %8s' hdr = hdr_fmt % ('Name', 'Matvec', 'Resid0', 'Resid', 'Error') fmt = '%10s %6d %8.2e %8.2e %8.2e' AA = spmatrix.ll_mat_from_mtx(mtx) A = Op(AA) n = A.shape[0] e = np.ones(n) rhs = A*e if 'logger' in kwargs: logger = kwargs.pop('logger') ks = KSolver(A, reltol=1.0e-8, logger=logger) ks.solve(rhs, guess=1+np.arange(n, dtype=np.float), matvec_max=2*n, **kwargs) err = np.linalg.norm(ks.bestSolution-e)/sqrt(n) print print hdr print '-' * len(hdr) print fmt % (ks.acronym, ks.nMatvec, ks.residNorm0, ks.residNorm, err)
def pycfs_fact(ProblemList, bigp=500): import os if len(ProblemList) == 0: usage() sys.exit(1) header = '%-12s %-6s %-6s %-7s %-6s %-6s %-8s %-10s %-3s' % ( 'Name', 'n', 'nnz(A)', 'nnz(L)', 'den(A)', 'den(L)', 'shift', 'mem', 'p*') lhead = len(header) print header print '-' * lhead fmt = '%-12s %-6d %-6d %-7d %-6.2f %-6.2f %-8g %-10d %-3d' for problem in ProblemList: A = spmatrix.ll_mat_from_mtx(problem) (m, n) = A.shape if m != n: break (aval, arow, acol) = A.find() prob = os.path.basename(problem) if prob[-4:] == '.mtx': prob = prob[:-4] P = pycfs.PycfsContext(A, mem=bigp) L = P.fetch() # Retrieve Cholesky factor nnzA = A.nnz nnzL = L.nnz densityA = 100.0 * nnzA / (n * (n + 1) / 2) densityL = 100.0 * nnzL / (n * (n + 1) / 2) shift = P.shift memory_available = n * bigp p_nnz = (nnzL - nnzA) / n (lval, lrow, lcol) = L.find() print fmt % (prob, n, nnzA, nnzL, densityA, densityL, shift, memory_available, p_nnz) try: import matplotlib.pyplot as plt from pyorder.tools.spy import FastSpy # Plot sparsity patterns left = plt.subplot(121) right = plt.subplot(122) FastSpy(m, n, arow, acol, ax=left) FastSpy(m, n, lrow, lcol, ax=right) plt.show() except: sys.stderr.write('Not plotting sparsity patterns.') sys.stderr.write(' Did you install Matplotlib?\n') print '-' * lhead return None
def pycfs_fact(ProblemList, bigp=500): import os if len(ProblemList) == 0: usage() sys.exit(1) header = '%-12s %-6s %-6s %-7s %-6s %-6s %-8s %-10s %-3s' % ('Name', 'n', 'nnz(A)', 'nnz(L)', 'den(A)', 'den(L)', 'shift', 'mem', 'p*') lhead = len(header) print header print '-' * lhead fmt = '%-12s %-6d %-6d %-7d %-6.2f %-6.2f %-8g %-10d %-3d' for problem in ProblemList: A = spmatrix.ll_mat_from_mtx(problem) (m, n) = A.shape if m != n: break (aval,arow,acol) = A.find() prob = os.path.basename(problem) if prob[-4:] == '.mtx': prob = prob[:-4] P = pycfs.PycfsContext(A, mem=bigp) L = P.fetch() # Retrieve Cholesky factor nnzA = A.nnz nnzL = L.nnz densityA = 100.0 * nnzA/(n*(n+1)/2) densityL = 100.0 * nnzL/(n*(n+1)/2) shift = P.shift memory_available = n*bigp p_nnz = (nnzL-nnzA)/n (lval,lrow,lcol) = L.find() print fmt % (prob, n, nnzA, nnzL, densityA, densityL, shift, memory_available, p_nnz) try: import matplotlib.pyplot as plt from pyorder.tools.spy import FastSpy # Plot sparsity patterns left = plt.subplot(121) right = plt.subplot(122) FastSpy(m,n,arow,acol,ax=left) FastSpy(m,n,lrow,lcol,ax=right) plt.show() except: sys.stderr.write('Not plotting sparsity patterns.') sys.stderr.write(' Did you install Matplotlib and pyorder?\n') print '-' * lhead return None
def ReadMatrix(fname): """ Read matrix from file fname in MatrixMarket format. Alternatively, could read from Ampl nl file. Returns a pointer to the matrix, or None if an error occured. """ H = spmatrix.ll_mat_from_mtx(fname) (n, m) = H.shape if n != m: sys.stderr.write('Hessian matrix must be square') return None if not H.issym: sys.stderr.write('Hessian matrix must be symmetric') return None return H
from nlpy.linalg.pyma57 import PyMa57Context as LBLContext except: from nlpy.linalg.pyma27 import PyMa27Context as LBLContext from pysparse import spmatrix import numpy as np from nlpy.tools.timing import cputime import sys if len(sys.argv) < 3: sys.stderr.write('Please supply two positive definite matrices as input') sys.stderr.write(' in MatrixMarket format.\n') sys.exit(1) # Create symmetric quasi-definite matrix K A = spmatrix.ll_mat_from_mtx(sys.argv[1]) C = spmatrix.ll_mat_from_mtx(sys.argv[2]) nA = A.shape[0] nC = C.shape[0] K = spmatrix.ll_mat_sym(nA + nC, A.nnz + C.nnz + min(nA,nC)) K[:nA,:nA] = A K[nA:,nA:] = C K[nA:,nA:].scale(-1.0) idx = np.arange(min(nA,nC), dtype=np.int) K.put(1, nA+idx, idx) # Create right-hand side rhs=K*e e = np.ones(nA+nC) rhs = np.empty(nA+nC) K.matvec(e,rhs)
def __call__(self, y, **kwargs): "Return the result of applying preconditioner to y" return y/self.diag if __name__ == '__main__': hdr_fmt = '%10s %6s %8s %8s %8s' hdr = hdr_fmt % ('Name', 'Matvec', 'Resid0', 'Resid', 'Error') fmt = '%10s %6d %8.2e %8.2e %8.2e' print hdr print '-' * len(hdr) #AA = spmatrix.ll_mat_from_mtx('mcca.mtx') AA = spmatrix.ll_mat_from_mtx('jpwh_991.mtx') A = sp(matrix=AA) # Create diagonal preconditioner dp = DiagonalPrec(A) n = A.shape[0] e = np.ones(n) rhs = A*e for KSolver in [CGS, TFQMR, BiCGSTAB]: ks = KSolver( lambda v: A*v, #precon = dp, #verbose=False, reltol = 1.0e-8 )
from numpy import * import sys from pysparse import jdsym, spmatrix, itsolvers, precon matfiles = sys.argv[1:5] target_value = eval(sys.argv[3]) eigenval_num = eval(sys.argv[4]) jdtol = eval(sys.argv[5]) max_iter = eval(sys.argv[6]) mat_left = spmatrix.ll_mat_from_mtx(matfiles[0]) mat_right = spmatrix.ll_mat_from_mtx(matfiles[1]) shape = mat_left.shape T = mat_left.copy() T.shift(-target_value, mat_right) K = precon.ssor(T.to_sss(), 1.0, 1) # K is preconditioner. A = mat_left.to_sss() M = mat_right.to_sss() k_conv, lmbd, Q, it, itall = jdsym.jdsym(A, M, K, eigenval_num, target_value, jdtol, max_iter, itsolvers.minres) NEIG = len(lmbd) #for lam in lmbd: # print "value:", lam eivecfile = open("eivecs.dat", "w") N = len(Q[:,0]) print >> eivecfile, N print >> eivecfile, NEIG for ieig in range(len(lmbd)): eivec = Q[:,ieig] print >> eivecfile, lmbd[ieig] # printing eigenvalue for val in eivec: # printing eigenvector print >> eivecfile, val eivecfile.close()
import math import Numeric import pysparse.spmatrix from pysparse import spmatrix, itsolvers, jdsym, precon, superlu test = 1 if test == 1: # Test: compare K=eye with K=None # # results are not the same, ritz-value in 2nd iterations differ path = '/local/home/geus/matrices' A = spmatrix.ll_mat_from_mtx(path + 'edge6x3x5_A.mtx') M = spmatrix.ll_mat_from_mtx(path + 'edge6x3x5_B.mtx') n = A.shape[0] sigma = 25.0 I = spmatrix.ll_mat(n, n) for i in xrange(n): I[i,i] = 1.0 Keye = precon.jacobi(I) k_conv, lmbd, Q, it, it_inner = jdsym.jdsym(A.to_sss(), M.to_sss(), None, 5, sigma, 1e-10, 15, itsolvers.qmrs, jmin=5, jmax=10, eps_tr=1e-4, toldecay=2.0, linitmax=200, clvl=1, strategy=1) k_conv, lmbd, Q, it, it_inner = jdsym.jdsym(A.to_sss(), M.to_sss(), Keye, 5, sigma, 1e-10, 15, itsolvers.qmrs, jmin=5, jmax=10, eps_tr=1e-4, toldecay=2.0, linitmax=200, clvl=1, strategy=1) elif test == 2:
def association_matrix_to_similarity_matrix(self, metric="cosine", dataset="FREESOUND", save_sim=False, training_set=None, out_name_prefix="", is_general_recommender=False): if self.verbose: print "Loading association matrix and tag names, ids files..." try: M = spmatrix.ll_mat_from_mtx(RECOMMENDATION_TMP_DATA_DIR + dataset + "_ASSOCIATION_MATRIX.mtx") resource_ids = load(RECOMMENDATION_TMP_DATA_DIR + dataset + "_RESOURCE_IDS.npy") tag_names = load(RECOMMENDATION_TMP_DATA_DIR + dataset + "_TAG_NAMES.npy") except Exception: raise Exception("Error loading association matrix and tag names, ids data") if metric not in ['cosine', 'binary', 'coocurrence', 'jaccard']: raise Exception("Wrong similarity metric specified") if self.verbose: print "Computing similarity matrix from a resource subset of the whole association matrix..." # Get index of resources to train (usable index for M) resource_id_positions = where(in1d(resource_ids, training_set, assume_unique=True))[0] # Matrix multiplication (only taking in account resources in training set and ALL tags) MM = spmatrix.dot(M[resource_id_positions, :], M[resource_id_positions, :]) # Get similarity matrix sim_matrix = spmatrix.ll_mat(MM.shape[0],MM.shape[0]) non_zero_index = MM.keys() for index in non_zero_index: if metric == 'cosine': sim_matrix[index[0], index[1]] = MM[index[0], index[1]] * (1 / (sqrt(MM[index[0], index[0]]) * sqrt(MM[index[1], index[1]]))) elif metric == 'coocurrence': sim_matrix[index[0], index[1]] = MM[index[0], index[1]] elif metric == 'binary': sim_matrix[index[0], index[1]] = MM[index[0], index[1]]/MM[index[0], index[1]] elif metric == 'jaccard': sim_matrix[index[0], index[1]] = MM[index[0], index[1]] * (1 / (MM[index[0], index[0]] + MM[index[1], index[1]] - MM[index[0], index[1]])) # Clean out similarity matrix (clean tags that are not used) tag_positions = [] for i in range(0, sim_matrix.shape[0]): if sim_matrix[i, i] != 0.0: tag_positions.append(i) # Transform sparse similarity matrix to npy format sim_matrix_npy = mtx2npy(sim_matrix[tag_positions,tag_positions]) tag_names_sim_matrix = tag_names[tag_positions] if save_sim: if not is_general_recommender: # Save sim path = RECOMMENDATION_TMP_DATA_DIR + dataset + "_%s_SIMILARITY_MATRIX_" % out_name_prefix + metric + "_SUBSET.npy" if self.verbose: print "Saving to " + path + "..." save(path, sim_matrix_npy) # Save tag names path = RECOMMENDATION_TMP_DATA_DIR + dataset + "_%s_SIMILARITY_MATRIX_" % out_name_prefix + metric + "_SUBSET_TAG_NAMES.npy" if self.verbose: print "Saving to " + path + "..." save(path, tag_names_sim_matrix) else: # Save sim path = RECOMMENDATION_TMP_DATA_DIR + dataset + "_SIMILARITY_MATRIX_" + metric + ".npy" if self.verbose: print "Saving to " + path + "..." save(path, sim_matrix_npy) # Save tag names path = RECOMMENDATION_TMP_DATA_DIR + dataset + "_SIMILARITY_MATRIX_" + metric + "_TAG_NAMES.npy" if self.verbose: print "Saving to " + path + "..." save(path, tag_names_sim_matrix) return {'SIMILARITY_MATRIX': sim_matrix_npy, 'TAG_NAMES': tag_names_sim_matrix}
(x, r, nr, nr1, t_an, t_sl, neig) = SolveSystem(A, rhs) exact = numpy.arange(5, dtype = 'd') + 1 relres = norms.norm2(x - exact) / norms.norm2(exact) sys.stdout.write(res_fmt % ('Spec sheet',relres,nr,nr1,t_an,t_sl,neig)) # Solve example with Hilbert matrix n = 10 H = Hilbert(n) e = numpy.ones(n, 'd') rhs = numpy.empty(n, 'd') H.matvec(e, rhs) (x, r, nr, nr1, t_an, t_sl, neig) = SolveSystem(H, rhs) relres = norms.norm2(x - e) / norms.norm2(e) sys.stdout.write(res_fmt % ('Hilbert', relres, nr, nr1, t_an, t_sl, neig)) # Process matrices given on the command line for matrix in matrices: M = spmatrix.ll_mat_from_mtx(matrix) (m,n) = M.shape if m != n: break e = numpy.ones(n, 'd') rhs = numpy.empty(n, 'd') M.matvec(e, rhs) (x, r, nr, nr1, t_an, t_sl, neig) = SolveSystem(M, rhs) relres = norms.norm2(x - e) / norms.norm2(e) probname = os.path.basename(matrix) if probname[-4:] == '.mtx': probname = probname[:-4] sys.stdout.write(res_fmt % (probname,relres,nr,nr1,t_an,t_sl,neig)) sys.stderr.write('-' * lhead + '\n')
def loadMatrix(filename): return spmatrix.ll_mat_from_mtx(filename)
printMatrix(spmatrix.matrixmultiply(O, O)) printMatrix(spmatrix.matrixmultiply(Os, O)) print 'Dot product' printMatrix(spmatrix.dot(I, A)) print 'Matrix export' A[:4,:4].export_mtx('A.mtx', 3) As[:4,:4].export_mtx('As.mtx', 3) print open('A.mtx').read() print open('As.mtx').read() print 'Matrix import' printMatrix(spmatrix.ll_mat_from_mtx('A.mtx')) printMatrix(spmatrix.ll_mat_from_mtx('As.mtx')) print 'Conversion to CSR' print A[:4,:4] print A[:4,:4].to_csr() print As[:4,:4].to_csr() print 'update_add_mask operations' ind = Numeric.array([3, 4, 5, 6], 'i') mask = Numeric.array([1, 1, 1, 1], 'i') B = Numeric.ones((4,4), 'd') Ac = A.copy() Ac.update_add_mask(B, ind, ind, mask, mask) A.update_add_mask_sym(B, ind, mask) As.update_add_mask_sym(B, ind, mask)
print L print L.nnz #------------------------------------------------------------ if 0: f = open(os.environ['HOME']+'/matrices/poi2d_300.mtx') t1 = time.clock() L = ll_mat_from_mtx(f) t_read = time.clock() - t1 f.close() print 'time for reading matrix data from file: %.2f sec' % t_read if 1: t1 = time.clock() L = spmatrix.ll_mat_from_mtx(os.environ['HOME']+'/matrices/poi2d_300.mtx') t_read = time.clock() - t1 print 'time for reading matrix data from file: %.2f sec' % t_read #------------------------------------------------------------ L = spmatrix.ll_mat_from_mtx(os.environ['HOME']+'/matrices/node4x3x1_A.mtx') print L.shape, L.nnz A = L.to_sss() class diag_prec: def __init__(self, A): self.shape = A.shape n = self.shape[0] self.dinv = Numeric.zeros(n, 'd')
def test_pycfs(ProblemList, plist=[0, 2, 5, 10], latex=False): if len(ProblemList) == 0: usage() sys.exit(1) if latex: # For LaTeX output fmt = '%-12s & %-5s & %-6s & %-2s & %-4s & %-8s & %-4s & %-8s & %-6s & %-6s\\\\\n' fmt1 = '%-12s & %-5d & %-6d & ' fmt2 = '%-2d & %-4d & %-8.1e & %-4d & %-8.1e & %-6.2f & %-6.2f\\\\\n' hline = '\\hline\n' skip = '&&&' else: # For ASCII output fmt = '%-12s %-5s %-6s %-2s %-4s %-8s %-4s %-8s %-6s %-6s\n' fmt1 = '%-12s %-5d %-6d ' fmt2 = '%-2d %-4d %-8.1e %-4d %-8.1e %-6.2f %-6.2f\n' skip = ' ' * 26 header = fmt % ('Name','Size','nnz','p','info','shift','iter','relResid','fact','solve') lhead = len(header) if not latex: hline = '-' * lhead + '\n' sys.stderr.write(hline + header + hline) time_list = {} # Record timings iter_list = {} # Record number of iterations for problem in ProblemList: A = spmatrix.ll_mat_from_mtx(problem) (m, n) = A.shape if m != n: break prob = os.path.basename(problem) if prob[-4:] == '.mtx': prob = prob[:-4] # Right-hand side is Ae, as in Icfs. e = numpy.ones(n, 'd'); b = numpy.ones(n, 'd'); A.matvec(e, b) sys.stdout.write(fmt1 % (prob, n, A.nnz)) advance = False # Call icfs and pcg tlist = [] ilist = [] for pval in plist: t0 = cputime() P = pycfs.PycfsContext(A, mem=pval) t_fact = cputime() - t0 P.solve(b) t_solve = P.tsolve tlist.append(t_fact + t_solve) ilist.append(P.iter) if advance: sys.stdout.write(skip) sys.stdout.write(fmt2 % (pval, P.info, P.shift, P.iter, P.relres, t_fact, t_solve)) advance = True time_list[prob] = tlist iter_list[prob] = ilist sys.stderr.write(hline) return (time_list,iter_list)