本文整理汇总了Python中scipy.diag方法的典型用法代码示例。如果您正苦于以下问题:Python scipy.diag方法的具体用法?Python scipy.diag怎么用?Python scipy.diag使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy
的用法示例。
在下文中一共展示了scipy.diag方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_complex_lu
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def test_complex_lu(self):
"""Getting factors of complex matrix"""
umfpack = um.UmfpackContext("zi")
for A in self.complex_matrices:
umfpack.numeric(A)
(L,U,P,Q,R,do_recip) = umfpack.lu(A)
L = L.todense()
U = U.todense()
A = A.todense()
if not do_recip:
R = 1.0/R
R = matrix(diag(R))
P = eye(A.shape[0])[P,:]
Q = eye(A.shape[1])[:,Q]
assert_array_almost_equal(P*R*A*Q,L*U)
示例2: test_real_lu
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def test_real_lu(self):
"""Getting factors of real matrix"""
umfpack = um.UmfpackContext("di")
for A in self.real_matrices:
umfpack.numeric(A)
(L,U,P,Q,R,do_recip) = umfpack.lu(A)
L = L.todense()
U = U.todense()
A = A.todense()
if not do_recip:
R = 1.0/R
R = matrix(diag(R))
P = eye(A.shape[0])[P,:]
Q = eye(A.shape[1])[:,Q]
assert_array_almost_equal(P*R*A*Q,L*U)
示例3: test_complex_lu
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def test_complex_lu(self):
# Getting factors of complex matrix
umfpack = um.UmfpackContext("zi")
for A in self.complex_matrices:
umfpack.numeric(A)
(L,U,P,Q,R,do_recip) = umfpack.lu(A)
L = L.todense()
U = U.todense()
A = A.todense()
if not do_recip:
R = 1.0/R
R = matrix(diag(R))
P = eye(A.shape[0])[P,:]
Q = eye(A.shape[1])[:,Q]
assert_array_almost_equal(P*R*A*Q,L*U)
示例4: test_complex_int64_lu
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def test_complex_int64_lu(self):
# Getting factors of complex matrix with long indices
umfpack = um.UmfpackContext("zl")
for A in self.complex_int64_matrices:
umfpack.numeric(A)
(L,U,P,Q,R,do_recip) = umfpack.lu(A)
L = L.todense()
U = U.todense()
A = A.todense()
if not do_recip:
R = 1.0/R
R = matrix(diag(R))
P = eye(A.shape[0])[P,:]
Q = eye(A.shape[1])[:,Q]
assert_array_almost_equal(P*R*A*Q,L*U)
示例5: test_real_lu
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def test_real_lu(self):
# Getting factors of real matrix
umfpack = um.UmfpackContext("di")
for A in self.real_matrices:
umfpack.numeric(A)
(L,U,P,Q,R,do_recip) = umfpack.lu(A)
L = L.todense()
U = U.todense()
A = A.todense()
if not do_recip:
R = 1.0/R
R = matrix(diag(R))
P = eye(A.shape[0])[P,:]
Q = eye(A.shape[1])[:,Q]
assert_array_almost_equal(P*R*A*Q,L*U)
示例6: test_real_int64_lu
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def test_real_int64_lu(self):
# Getting factors of real matrix with long indices
umfpack = um.UmfpackContext("dl")
for A in self.real_int64_matrices:
umfpack.numeric(A)
(L,U,P,Q,R,do_recip) = umfpack.lu(A)
L = L.todense()
U = U.todense()
A = A.todense()
if not do_recip:
R = 1.0/R
R = matrix(diag(R))
P = eye(A.shape[0])[P,:]
Q = eye(A.shape[1])[:,Q]
assert_array_almost_equal(P*R*A*Q,L*U)
示例7: _quartimax_obj
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def _quartimax_obj(self, loadings):
"""
Quartimax function objective.
Parameters
----------
loadings : array-like
The loading matrix
Returns
-------
gradient_dict : dict
A dictionary with
- grad : np.array
The gradient.
- criterion : float
The value of the criterion for the objective.
"""
gradient = -loadings**3
criterion = -np.sum(np.diag(np.dot((loadings**2).T, loadings**2))) / 4
return {'grad': gradient, 'criterion': criterion}
示例8: _oblimin_obj
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def _oblimin_obj(self, loadings):
"""
The Oblimin function objective.
Parameters
----------
loadings : array-like
The loading matrix
Returns
-------
gradient_dict : dict
A dictionary with
- grad : np.array
The gradient.
- criterion : float
The value of the criterion for the objective.
"""
X = np.dot(loadings**2, np.eye(loadings.shape[1]) != 1)
if (self.gamma != 0):
p = loadings.shape[0]
X = np.diag(np.full(1, p)) - np.dot(np.zeros((p, p)), X)
gradient = loadings * X
criterion = np.sum(loadings**2 * X) / 4
return {'grad': gradient, 'criterion': criterion}
示例9: ElasticRod
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def ElasticRod(n):
# Fixed-free elastic rod
L = 1.0
le = L/n
rho = 7.85e3
S = 1.e-4
E = 2.1e11
mass = rho*S*le/6.
k = E*S/le
A = k*(diag(r_[2.*ones(n-1),1])-diag(ones(n-1),1)-diag(ones(n-1),-1))
B = mass*(diag(r_[4.*ones(n-1),2])+diag(ones(n-1),1)+diag(ones(n-1),-1))
return A,B
示例10: MikotaPair
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def MikotaPair(n):
# Mikota pair acts as a nice test since the eigenvalues
# are the squares of the integers n, n=1,2,...
x = arange(1,n+1)
B = diag(1./x)
y = arange(n-1,0,-1)
z = arange(2*n-1,0,-2)
A = diag(z)-diag(y,-1)-diag(y,1)
return A,B
示例11: MikotaPair
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def MikotaPair(n):
# Mikota pair acts as a nice test since the eigenvalues
# are the squares of the integers n, n=1,2,...
x = np.arange(1,n+1)
B = diag(1./x)
y = np.arange(n-1,0,-1)
z = np.arange(2*n-1,0,-2)
A = diag(z)-diag(y,-1)-diag(y,1)
return A,B
示例12: _equamax_obj
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def _equamax_obj(self, loadings):
"""
Equamax function objective.
Parameters
----------
loadings : array-like
The loading matrix
Returns
-------
gradient_dict : dict
A dictionary with
- grad : np.array
The gradient.
- criterion : float
The value of the criterion for the objective.
"""
p, k = loadings.shape
N = np.ones(k) - np.eye(k)
M = np.ones(p) - np.eye(p)
loadings_squared = loadings**2
f1 = (1 - self.kappa) * np.sum(np.diag(np.dot(loadings_squared.T,
np.dot(loadings_squared, N)))) / 4
f2 = self.kappa * np.sum(np.diag(np.dot(loadings_squared.T,
np.dot(M, loadings_squared)))) / 4
gradient = ((1 - self.kappa) * loadings * np.dot(loadings_squared, N) +
self.kappa * loadings * np.dot(M, loadings_squared))
criterion = f1 + f2
return {'grad': gradient, 'criterion': criterion}
示例13: _check_fiedler
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import diag [as 别名]
def _check_fiedler(n, p):
# This is not necessarily the recommended way to find the Fiedler vector.
np.random.seed(1234)
col = np.zeros(n)
col[1] = 1
A = toeplitz(col)
D = np.diag(A.sum(axis=1))
L = D - A
# Compute the full eigendecomposition using tricks, e.g.
# http://www.cs.yale.edu/homes/spielman/561/2009/lect02-09.pdf
tmp = np.pi * np.arange(n) / n
analytic_w = 2 * (1 - np.cos(tmp))
analytic_V = np.cos(np.outer(np.arange(n) + 1/2, tmp))
_check_eigen(L, analytic_w, analytic_V)
# Compute the full eigendecomposition using eigh.
eigh_w, eigh_V = eigh(L)
_check_eigen(L, eigh_w, eigh_V)
# Check that the first eigenvalue is near zero and that the rest agree.
assert_array_less(np.abs([eigh_w[0], analytic_w[0]]), 1e-14)
assert_allclose(eigh_w[1:], analytic_w[1:])
# Check small lobpcg eigenvalues.
X = analytic_V[:, :p]
lobpcg_w, lobpcg_V = lobpcg(L, X, largest=False)
assert_equal(lobpcg_w.shape, (p,))
assert_equal(lobpcg_V.shape, (n, p))
_check_eigen(L, lobpcg_w, lobpcg_V)
assert_array_less(np.abs(np.min(lobpcg_w)), 1e-14)
assert_allclose(np.sort(lobpcg_w)[1:], analytic_w[1:p])
# Check large lobpcg eigenvalues.
X = analytic_V[:, -p:]
lobpcg_w, lobpcg_V = lobpcg(L, X, largest=True)
assert_equal(lobpcg_w.shape, (p,))
assert_equal(lobpcg_V.shape, (n, p))
_check_eigen(L, lobpcg_w, lobpcg_V)
assert_allclose(np.sort(lobpcg_w), analytic_w[-p:])
# Look for the Fiedler vector using good but not exactly correct guesses.
fiedler_guess = np.concatenate((np.ones(n//2), -np.ones(n-n//2)))
X = np.vstack((np.ones(n), fiedler_guess)).T
lobpcg_w, lobpcg_V = lobpcg(L, X, largest=False)
# Mathematically, the smaller eigenvalue should be zero
# and the larger should be the algebraic connectivity.
lobpcg_w = np.sort(lobpcg_w)
assert_allclose(lobpcg_w, analytic_w[:2], atol=1e-14)