本文整理汇总了Python中sandbox.util.Util.Util.matrixPowerh方法的典型用法代码示例。如果您正苦于以下问题:Python Util.matrixPowerh方法的具体用法?Python Util.matrixPowerh怎么用?Python Util.matrixPowerh使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sandbox.util.Util.Util
的用法示例。
在下文中一共展示了Util.matrixPowerh方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testMatrixPowerh
# 需要导入模块: from sandbox.util.Util import Util [as 别名]
# 或者: from sandbox.util.Util.Util import matrixPowerh [as 别名]
def testMatrixPowerh(self):
A = numpy.random.rand(10, 10)
A = A.T.dot(A)
tol = 10**-6
A2 = A.dot(A)
lmbda, V = scipy.linalg.eig(A)
A12 = Util.matrixPowerh(A, 0.5)
self.assertTrue(numpy.linalg.norm(A12.dot(A12) - A) < tol)
self.assertTrue(numpy.linalg.norm(numpy.linalg.inv(A) - Util.matrixPowerh(A, -1)) < tol)
self.assertTrue(numpy.linalg.norm(A - Util.matrixPowerh(A, 1)) < tol)
self.assertTrue(numpy.linalg.norm(A2 - Util.matrixPowerh(A, 2)) < tol)
self.assertTrue(numpy.linalg.norm(numpy.linalg.inv(A).dot(numpy.linalg.inv(A)) - Util.matrixPowerh(A, -2)) < tol)
#Now lets test on a low rank matrix
lmbda[5:] = 0
A = V.dot(numpy.diag(lmbda)).dot(numpy.linalg.inv(V))
A2 = A.dot(A)
A12 = Util.matrixPowerh(A, 0.5)
Am12 = Util.matrixPowerh(A, -0.5)
self.assertTrue(numpy.linalg.norm(numpy.linalg.pinv(A) - Util.matrixPowerh(A, -1)) < tol)
self.assertTrue(numpy.linalg.norm(numpy.linalg.pinv(A) - Am12.dot(Am12)) < tol)
self.assertTrue(numpy.linalg.norm(A12.dot(A12) - A) < tol)
self.assertTrue(numpy.linalg.norm(A - Util.matrixPowerh(A, 1)) < tol)
self.assertTrue(numpy.linalg.norm(A2 - Util.matrixPowerh(A, 2)) < tol)
示例2: eigpsd
# 需要导入模块: from sandbox.util.Util import Util [as 别名]
# 或者: from sandbox.util.Util.Util import matrixPowerh [as 别名]
def eigpsd(X, n):
"""
Find the eigenvalues and eigenvectors of a positive semi-definite symmetric matrix.
The input matrix X can be a numpy array or a scipy sparse matrix. In the case that
n==X.shape[0] we convert to an ndarray.
:param X: The matrix to find the eigenvalues of.
:type X: :class:`ndarray`
:param n: If n is an int, then it is the number of columns to sample otherwise n is an array of column indices.
:return lmbda: The set of eigenvalues
:return V: The matrix of eigenvectors as a ndarray
"""
if type(n) == int:
n = min(n, X.shape[0])
inds = numpy.sort(numpy.random.permutation(X.shape[0])[0:n])
elif type(n) == numpy.ndarray:
inds = numpy.sort(n)
else:
raise ValueError("Invalid n value: " + str(n))
invInds = numpy.setdiff1d(numpy.arange(X.shape[0]), inds)
if inds.shape[0] == X.shape[0] and (inds == numpy.arange(X.shape[0])).all():
if scipy.sparse.issparse(X):
X = numpy.array(X.todense())
lmbda, V = Util.safeEigh(X)
return lmbda, V
tmp = X[inds, :]
A = tmp[:, inds]
B = tmp[:, invInds]
if scipy.sparse.issparse(X):
A = numpy.array(A.todense())
BB = numpy.array((B.dot(B.T)).todense())
else:
BB = B.dot(B.T)
# Following line is very slow
# Am12 = scipy.linalg.sqrtm(numpy.linalg.pinv(A))
Am12 = Util.matrixPowerh(A, -0.5)
S = A + Am12.dot(BB).dot(Am12)
S = (S.T + S) / 2
lmbda, U = Util.safeEigh(S)
tol = 10 ** -10
lmbdaN = lmbda.copy()
lmbdaN[numpy.abs(lmbda) < tol] = 0
lmbdaN[numpy.abs(lmbda) > tol] = lmbdaN[numpy.abs(lmbda) > tol] ** -0.5
V = X[:, inds].dot(Am12.dot(U) * lmbdaN)
return lmbda, V