def testAsLinearOperator(self): try: import scipy.sparse.linalg #Check it works for the eigenvalue solver L = GeneralLinearOperator.asLinearOperator(self.A) k = 10 s, V = scipy.sparse.linalg.eigsh(L, k, which="LA") inds = numpy.flipud(numpy.argsort(s)) s = s[inds] V = V[:, inds] B = self.A.toarray() s2, V2 = numpy.linalg.eigh(B) inds = numpy.flipud(numpy.argsort(s2))[0:k] s2 = s2[inds] V2 = V2[:, inds] nptst.assert_array_almost_equal(s, s2[0:s.shape[0]]) nptst.assert_array_almost_equal((V * s).dot(V.T), (V2 * s2).dot(V2.T)) except: print("Couldn't test asLinearOperator")
def time_ns(): density = 10**-3 ns = var_range * 10**4 times = numpy.zeros((5, ns.shape[0])) for i, n in enumerate(ns): # Generate random sparse matrix inds = numpy.random.randint(n, size=(2, n * n * density)) data = numpy.random.rand(n * n * density) A = scipy.sparse.csc_matrix((data, inds), (n, n)) A_sppy = sppy.csarray(A, storagetype="row") L = GeneralLinearOperator.asLinearOperator(A_sppy, parallel=True) print(A.shape, A.nnz) times[0, i] = time_reps(svds, (A, k), reps) times[1, i] = time_reps(svdp, (A, k), reps) # times[2, i] = time_reps(sparsesvd, (A, k), reps) times[3, i] = time_reps(truncated_svd.fit, (A,), reps) times[4, i] = time_reps(sppy.linalg.rsvd, (L, k, p, n_iter), reps) print(n, density, times[:, i]) plt.figure(1) plt.plot(ns, times[0, :], 'k-', label="ARPACK") plt.plot(ns, times[1, :], 'r-', label="PROPACK") # plt.plot(ns, times[2, :], 'b-', label="SparseSVD") plt.plot(ns, times[3, :], 'k--', label="sklearn RSVD") plt.plot(ns, times[4, :], 'r--', label="sppy RSVD") plt.legend(loc="upper left") plt.xlabel("n") plt.ylabel("time (s)") plt.savefig("time_ns.png", format="png")
def time_ns(): density = 10**-3 ns = var_range * 10**4 times = numpy.zeros((5, ns.shape[0])) for i, n in enumerate(ns): # Generate random sparse matrix inds = numpy.random.randint(n, size=(2, n * n * density)) data = numpy.random.rand(n * n * density) A = scipy.sparse.csc_matrix((data, inds), (n, n)) A_sppy = sppy.csarray(A, storagetype="row") L = GeneralLinearOperator.asLinearOperator(A_sppy, parallel=True) print(A.shape, A.nnz) times[0, i] = time_reps(svds, (A, k), reps) times[1, i] = time_reps(svdp, (A, k), reps) # times[2, i] = time_reps(sparsesvd, (A, k), reps) times[3, i] = time_reps(truncated_svd.fit, (A, ), reps) times[4, i] = time_reps(sppy.linalg.rsvd, (L, k, p, n_iter), reps) print(n, density, times[:, i]) plt.figure(1) plt.plot(ns, times[0, :], 'k-', label="ARPACK") plt.plot(ns, times[1, :], 'r-', label="PROPACK") # plt.plot(ns, times[2, :], 'b-', label="SparseSVD") plt.plot(ns, times[3, :], 'k--', label="sklearn RSVD") plt.plot(ns, times[4, :], 'r--', label="sppy RSVD") plt.legend(loc="upper left") plt.xlabel("n") plt.ylabel("time (s)") plt.savefig("time_ns.png", format="png")
def testAsLinearOperatorSum(self): n = 10 m = 5 density = 0.3 A = rand((n, m), density) B = rand((n, m), density) L = GeneralLinearOperator.asLinearOperator(A) M = GeneralLinearOperator.asLinearOperator(B) N = GeneralLinearOperator.asLinearOperatorSum(L, M) k = 3 V = numpy.random.rand(m, k) W = numpy.random.rand(n, k) U = N.matmat(V) U2 = A.dot(V) + B.dot(V) nptst.assert_array_almost_equal(U, U2) U = N.rmatmat(W) U2 = A.T.dot(W) + B.T.dot(W) nptst.assert_array_almost_equal(U, U2) v = numpy.random.rand(m) w = numpy.random.rand(n) u = N.matvec(v) u2 = A.dot(v) + B.dot(v) nptst.assert_array_almost_equal(u, u2) u = N.rmatvec(w) u2 = A.T.dot(w) + B.T.dot(w) nptst.assert_array_almost_equal(u, u2) #See if we get an error if A, B are different shapes B = rand((m, n), 0.1) M = GeneralLinearOperator.asLinearOperator(B) self.assertRaises(ValueError, GeneralLinearOperator.asLinearOperatorSum, L, M)
def profilePowerIteration2(self): p = 100 q = 5 omega = numpy.random.randn(self.X.shape[1], p) L = GeneralLinearOperator.asLinearOperator(self.X, parallel=True) def run(): Y = L.matmat(omega) for i in range(q): Y = L.rmatmat(Y) Y = L.matmat(Y) ProfileUtils.profile('run()', globals(), locals())
def testAsLinearOperator(self): try: import scipy.sparse.linalg #Check it works for the eigenvalue solver L = GeneralLinearOperator.asLinearOperator(self.A) k = 10 s, V = scipy.sparse.linalg.eigsh(L, k, which="LA") inds = numpy.flipud(numpy.argsort(s)) s = s[inds] V = V[:, inds] B = self.A.toarray() s2, V2 = numpy.linalg.eigh(B) inds = numpy.flipud(numpy.argsort(s2))[0:k] s2 = s2[inds] V2 = V2[:, inds] nptst.assert_array_almost_equal(s, s2[0:s.shape[0]]) nptst.assert_array_almost_equal((V*s).dot(V.T), (V2*s2).dot(V2.T)) except: print("Couldn't test asLinearOperator")