我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.linalg.eigvalsh()。
def test_UPLO(self): Klo = np.array([[0, 0], [1, 0]], dtype=np.double) Kup = np.array([[0, 1], [0, 0]], dtype=np.double) tgt = np.array([-1, 1], dtype=np.double) rtol = get_rtol(np.double) # Check default is 'L' w = np.linalg.eigvalsh(Klo) assert_allclose(w, tgt, rtol=rtol) # Check 'L' w = np.linalg.eigvalsh(Klo, UPLO='L') assert_allclose(w, tgt, rtol=rtol) # Check 'l' w = np.linalg.eigvalsh(Klo, UPLO='l') assert_allclose(w, tgt, rtol=rtol) # Check 'U' w = np.linalg.eigvalsh(Kup, UPLO='U') assert_allclose(w, tgt, rtol=rtol) # Check 'u' w = np.linalg.eigvalsh(Kup, UPLO='u') assert_allclose(w, tgt, rtol=rtol)
def do(self, a, b): # note that eigenvalue arrays returned by eig must be sorted since # their order isn't guaranteed. ev = linalg.eigvalsh(a, 'L') evalues, evectors = linalg.eig(a) evalues.sort(axis=-1) assert_allclose(ev, evalues, rtol=get_rtol(ev.dtype)) ev2 = linalg.eigvalsh(a, 'U') assert_allclose(ev2, evalues, rtol=get_rtol(ev.dtype))
def test_types(self): def check(dtype): x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype) w = np.linalg.eigvalsh(x) assert_equal(w.dtype, get_real_dtype(dtype)) for dtype in [single, double, csingle, cdouble]: yield check, dtype
def test_invalid(self): x = np.array([[1, 0.5], [0.5, 1]], dtype=np.float32) assert_raises(ValueError, np.linalg.eigvalsh, x, UPLO="lrong") assert_raises(ValueError, np.linalg.eigvalsh, x, "lower") assert_raises(ValueError, np.linalg.eigvalsh, x, "upper")
def genInvCov(size, low = 0 , upper = 0.6, portion = 0.05): S = np.zeros((size,size)) G = GenRndGnm(PUNGraph, size, int((size*(size-1))*portion)) for EI in G.Edges(): value = (np.random.randint(2) - 0.5)*2*(low + (upper - low)*np.random.rand(1)[0]) # print value S[EI.GetSrcNId(), EI.GetDstNId()] = value S = S + S.T vals = alg.eigvalsh(S) S = S + (0.1 - vals[0])*np.identity(size) return np.matrix(S)
def genInvCov(size, low = 0 , upper = 0.6, portion = 0.05): S = np.zeros((size,size)) # low = abs(low) # upper = abs(upper) G = GenRndGnm(PUNGraph, size, int((size*(size-1))*portion)) for EI in G.Edges(): value = (np.random.randint(2) - 0.5)*2*(low + (upper - low)*np.random.rand(1)[0]) # print value S[EI.GetSrcNId(), EI.GetDstNId()] = value S = S + S.T vals = alg.eigvalsh(S) S = S + (0.1 - vals[0])*np.identity(size) return np.matrix(S)
def genInvCov(size, low = 0 , upper = 0.6, portion = 0.05): S = np.zeros((size,size)) G = GenRndGnm(PUNGraph, size, int((size*(size-1))*portion)) for EI in G.Edges(): value = (np.random.randint(2) - 0.5)*2*(low + (upper - low)*np.random.rand(1)[0]) S[EI.GetSrcNId(), EI.GetDstNId()] = value S = S + S.T vals = alg.eigvalsh(S) S = S + (0.1 - vals[0])*np.identity(size) return np.matrix(S)
def _estimate_kappa(self): y, x, z = self._wy, self._wx, self._wz is_exog = self._regressor_is_exog e = c_[y, x[:, ~is_exog]] x1 = x[:, is_exog] if x1.shape[1] == 0: # No exogenous regressors return 1 ez = e - z @ (pinv(z) @ e) ex1 = e - x1 @ (pinv(x1) @ e) vpmzv_sqinv = inv_sqrth(ez.T @ ez) q = vpmzv_sqinv @ (ex1.T @ ex1) @ vpmzv_sqinv return min(eigvalsh(q))
def legcompanion(c): """Return the scaled companion matrix of c. The basis polynomials are scaled so that the companion matrix is symmetric when `c` is an Legendre basis polynomial. This provides better eigenvalue estimates than the unscaled case and for basis polynomials the eigenvalues are guaranteed to be real if `numpy.linalg.eigvalsh` is used to obtain them. Parameters ---------- c : array_like 1-D array of Legendre series coefficients ordered from low to high degree. Returns ------- mat : ndarray Scaled companion matrix of dimensions (deg, deg). Notes ----- .. versionadded::1.7.0 """ # c is a trimmed copy [c] = pu.as_series([c]) if len(c) < 2: raise ValueError('Series must have maximum degree of at least 1.') if len(c) == 2: return np.array([[-c[0]/c[1]]]) n = len(c) - 1 mat = np.zeros((n, n), dtype=c.dtype) scl = 1./np.sqrt(2*np.arange(n) + 1) top = mat.reshape(-1)[1::n+1] bot = mat.reshape(-1)[n::n+1] top[...] = np.arange(1, n)*scl[:n-1]*scl[1:n] bot[...] = top mat[:, -1] -= (c[:-1]/c[-1])*(scl/scl[-1])*(n/(2*n - 1)) return mat
def hermcompanion(c): """Return the scaled companion matrix of c. The basis polynomials are scaled so that the companion matrix is symmetric when `c` is an Hermite basis polynomial. This provides better eigenvalue estimates than the unscaled case and for basis polynomials the eigenvalues are guaranteed to be real if `numpy.linalg.eigvalsh` is used to obtain them. Parameters ---------- c : array_like 1-D array of Hermite series coefficients ordered from low to high degree. Returns ------- mat : ndarray Scaled companion matrix of dimensions (deg, deg). Notes ----- .. versionadded::1.7.0 """ # c is a trimmed copy [c] = pu.as_series([c]) if len(c) < 2: raise ValueError('Series must have maximum degree of at least 1.') if len(c) == 2: return np.array([[-.5*c[0]/c[1]]]) n = len(c) - 1 mat = np.zeros((n, n), dtype=c.dtype) scl = np.hstack((1., 1./np.sqrt(2.*np.arange(n - 1, 0, -1)))) scl = np.multiply.accumulate(scl)[::-1] top = mat.reshape(-1)[1::n+1] bot = mat.reshape(-1)[n::n+1] top[...] = np.sqrt(.5*np.arange(1, n)) bot[...] = top mat[:, -1] -= scl*c[:-1]/(2.0*c[-1]) return mat
def genMulCov(size, numberOfCov, low, upper, mode, portion = 0.05): S_set = [] Cov_set = [] minEVal_set = [] m = size/3 mm = m/2 # print m, mm S_init = np.zeros((size,size)) for k in range(numberOfCov): S = np.zeros((size,size)) if k == 0: S = genInvCov(size, low, upper, portion) if mode == 5: ind_zero = np.where(spy.sparse.rand(m, size-m, 0.5).todense() == 0) value = np.multiply((np.random.randint(2, size = (m, size -m)) - 0.5)*2,(low + (upper - low)*np.random.rand(m,size -m))) value[ind_zero] = 0 hub = value S[:m, m:size] = hub S[m:size, :m] = hub.T minEVal_set.append(alg.eigvalsh(S)[0]) S_init = S elif mode == 3: #'laplacian' ind1 = range(m) ind2 = np.random.permutation(m) S = np.copy(S_init) S[ind1, :] = S[ind2, :] S[:, ind1] = S[:, ind2] elif mode == 5: #'perturbation' S = np.copy(S_init) ind_zero = np.where(spy.sparse.rand(mm, size-mm, 0.5).todense() == 0) pert = np.multiply((np.random.randint(2, size = (mm, size -mm)) - 0.5)*2,(low + (upper - low)*np.random.rand(mm,size -mm))) pert[ind_zero] = 0 S[:mm, mm:size] = pert S[mm:size, :mm] = pert.T minEVal_set.append(alg.eigvalsh(S)[0]) else: # print 'Activate normal mode' S = genInvCov(size, low, upper, portion) S_set.append(S) for k in range(numberOfCov): if mode == 5: S_set[k] = S_set[k] + (0.1 - min(minEVal_set))*np.identity(size) Cov_set.append(alg.inv(S_set[k])) return S_set, Cov_set
def genMulCov(size, numberOfCov, low, upper, mode, portion = 0.05): S_set = [] Cov_set = [] minEVal_set = [] # low = abs(low) # upper = abs(upper) m = size/3 mm = m/2 # print m, mm S_init = np.zeros((size,size)) for k in range(numberOfCov): S = np.zeros((size,size)) if k == 0: S = genInvCov(size, low, upper, portion) if mode == 5: ind_zero = np.where(spy.sparse.rand(m, size-m, 0.5).todense() == 0) value = np.multiply((np.random.randint(2, size = (m, size -m)) - 0.5)*2,(low + (upper - low)*np.random.rand(m,size -m))) value[ind_zero] = 0 hub = value S[:m, m:size] = hub S[m:size, :m] = hub.T minEVal_set.append(alg.eigvalsh(S)[0]) S_init = S elif mode == 3: #'laplacian' ind1 = range(m) ind2 = np.random.permutation(m) S = np.copy(S_init) S[ind1, :] = S[ind2, :] S[:, ind1] = S[:, ind2] elif mode == 5: #'perturbation' S = np.copy(S_init) ind_zero = np.where(spy.sparse.rand(mm, size-mm, 0.5).todense() == 0) pert = np.multiply((np.random.randint(2, size = (mm, size -mm)) - 0.5)*2,(low + (upper - low)*np.random.rand(mm,size -mm))) pert[ind_zero] = 0 S[:mm, mm:size] = pert S[mm:size, :mm] = pert.T minEVal_set.append(alg.eigvalsh(S)[0]) else: # print 'Activate normal mode' S = genInvCov(size, low, upper, portion) S_set.append(S) for k in range(numberOfCov): if mode == 5: S_set[k] = S_set[k] + (0.1 - min(minEVal_set))*np.identity(size) Cov_set.append(alg.inv(S_set[k])) return S_set, Cov_set
def genMulCov(size, numberOfCov, low, upper, mode, portion = 0.05): S_set = [] Cov_set = [] minEVal_set = [] m = size/3 mm = m/2 S_init = np.zeros((size,size)) for k in range(numberOfCov): S = np.zeros((size,size)) if k == 0: S = genInvCov(size, low, upper, portion) if mode == 5: ind_zero = np.where(spy.sparse.rand(m, size-m, 0.5).todense() == 0) value = np.multiply((np.random.randint(2, size = (m, size -m)) - 0.5)*2,(low + (upper - low)*np.random.rand(m,size -m))) value[ind_zero] = 0 hub = value S[:m, m:size] = hub S[m:size, :m] = hub.T minEVal_set.append(alg.eigvalsh(S)[0]) S_init = S elif mode == 3: #'laplacian' ind1 = range(m) ind2 = np.random.permutation(m) S = np.copy(S_init) S[ind1, :] = S[ind2, :] S[:, ind1] = S[:, ind2] elif mode == 5: #'perturbation' S = np.copy(S_init) ind_zero = np.where(spy.sparse.rand(mm, size-mm, 0.5).todense() == 0) pert = np.multiply((np.random.randint(2, size = (mm, size -mm)) - 0.5)*2,(low + (upper - low)*np.random.rand(mm,size -mm))) pert[ind_zero] = 0 S[:mm, mm:size] = pert S[mm:size, :mm] = pert.T minEVal_set.append(alg.eigvalsh(S)[0]) else: # print 'Activate normal mode' S = genInvCov(size, low, upper, portion) S_set.append(S) for k in range(numberOfCov): if mode == 5: S_set[k] = S_set[k] + (0.1 - min(minEVal_set))*np.identity(size) Cov_set.append(alg.inv(S_set[k])) return S_set, Cov_set