def get_err_fast_sparse_svd(self, nrow, ncol, density): X = generate_sparse_matrix(shape=(nrow, ncol), density=density) # For debug np.save("/tmp/X_%d_%d.npy" % (nrow, ncol), X) # svd from parsimony fast_sparse_svd = RankOneSparseSVD(max_iter=1000) parsimony_v = fast_sparse_svd.run(X) # return self.get_err_by_np_linalg_svd(parsimony_v, X) return self.get_err_by_sp_sparse_linalg_svds(parsimony_v, X)
def lambda_max(self): """ Largest eigenvalue of the corresponding covariance matrix. From the interface "Eigenvalues". """ if self._lambda_max is None: try: self._lambda_max = self.A().get_singular_values(0) except: pass if self._lambda_max is None: # Note that we can save the state here since lmax(A) does not change. # TODO: This only work if the elements of self._A are scipy.sparse. We # should allow dense matrices as well. if len(self._A) == 3 \ and self._A[1].nnz == 0 and self._A[2].nnz == 0: # TODO: Instead of p, this should really be the number of non-zero # rows of A. self._lambda_max = 2.0 * (1.0 - math.cos(float(self._p - 1) * math.pi / float(self._p))) else: from parsimony.algorithms.nipals import RankOneSparseSVD A = sparse.vstack(self.A()) # TODO: Add max_iter here! v = RankOneSparseSVD().run(A) # , max_iter=max_iter) us = A.dot(v) self._lambda_max = np.sum(us ** 2.0) return self._lambda_max
def lambda_max(self): """ Largest eigenvalue of the corresponding covariance matrix. From the interface "Eigenvalues". """ # Note that we can save the state here since lmax(A) does not change. if len(self._A) == 4 and self._A[2].nnz == 0 and self._A[3].nnz == 0: # if len(self._shape) == 3 \ # and self._shape[0] == 1 and self._shape[1] == 1: # TODO: Instead of p, this should really be the number of non-zero # rows of A. p = self._A[1].shape[0] lmaxTV = 2.0 * (1.0 - math.cos(float(p - 1) * math.pi / float(p))) self._lambda_max = lmaxTV * self.g ** 2.0 + self.l ** 2.0 elif self._lambda_max is None: from parsimony.algorithms.nipals import RankOneSparseSVD A = sparse.vstack(self.A()[1:]) # TODO: Add max_iter here!! v = RankOneSparseSVD().run(A) # , max_iter=max_iter) us = A.dot(v) self._lambda_max = np.sum(us ** 2.0) + self.l ** 2.0 return self._lambda_max
def get_err_fast_sparse_svd(self, nrow, ncol, density): X = generate_sparse_matrix(shape=(nrow, ncol), density=density) # For debug # np.save("/tmp/X_%d_%d.npy" % (nrow, ncol), X) fd = None try: fd, tmpfilename = tempfile.mkstemp(suffix=".npy", prefix="X_%d_%d" % (nrow, ncol)) np.save(tmpfilename, X) finally: if fd is not None: os.close(fd) # svd from parsimony fast_sparse_svd = RankOneSparseSVD(max_iter=1000) parsimony_v = fast_sparse_svd.run(X) # return self.get_err_by_np_linalg_svd(parsimony_v, X) return self.get_err_by_sp_sparse_linalg_svds(parsimony_v, X)
def lambda_max(self): """ Largest eigenvalue of the corresponding covariance matrix. From the interface "Eigenvalues". """ # Note that we can save the state here since lmax(A) does not change. # TODO: This only work if the elements of self._A are scipy.sparse. We # should allow dense matrices as well. if self._lambda_max is None: from parsimony.algorithms.nipals import RankOneSparseSVD A = sparse.vstack(self.A()) # TODO: Add max_iter here! v = RankOneSparseSVD().run(A) # , max_iter=max_iter) us = A.dot(v) self._lambda_max = np.sum(us**2.0) return self._lambda_max