本文整理汇总了Python中sandbox.util.Util.Util.safeSvd方法的典型用法代码示例。如果您正苦于以下问题:Python Util.safeSvd方法的具体用法?Python Util.safeSvd怎么用?Python Util.safeSvd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sandbox.util.Util.Util
的用法示例。
在下文中一共展示了Util.safeSvd方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: eigenAdd2
# 需要导入模块: from sandbox.util.Util import Util [as 别名]
# 或者: from sandbox.util.Util.Util import safeSvd [as 别名]
def eigenAdd2(omega, Q, Y1, Y2, k, debug= False):
"""
Compute an approximation of the eigendecomposition A^*A + Y1Y2^* +Y2Y1^*
in which Y1, Y2 are low rank matrices, Y1^*Y2=0 and A^*A = Q Omega Q*. We
use the rank-k approximation of A^*A: Q_k Omega_k Q_k^* and then find
[A^*A_k + Y1Y2^* + Y2Y1^*]. If debug=False then pi, V are returned which
respectively correspond to all the eigenvalues/eigenvectors of
[A^*A_k + Y1Y2^* + Y2Y1^*].
"""
#logging.debug("< eigenAdd2 >")
Parameter.checkInt(k, 0, float('inf'))
Parameter.checkClass(omega, numpy.ndarray)
Parameter.checkClass(Q, numpy.ndarray)
Parameter.checkClass(Y1, numpy.ndarray)
Parameter.checkClass(Y2, numpy.ndarray)
if not numpy.isrealobj(omega) or not numpy.isrealobj(Q):
logging.warn("Eigenvalues or eigenvectors are not real")
if not numpy.isrealobj(Y1) or not numpy.isrealobj(Y2):
logging.warn("Y1 or Y2 are not real")
if omega.ndim != 1:
raise ValueError("omega must be 1-d array")
if omega.shape[0] != Q.shape[1]:
raise ValueError("Must have same number of eigenvalues and eigenvectors")
if Q.shape[0] != Y1.shape[0]:
raise ValueError("Q must have the same number of rows as Y1 rows")
if Q.shape[0] != Y2.shape[0]:
raise ValueError("Q must have the same number of rows as Y2 rows")
if Y1.shape[1] != Y2.shape[1]:
raise ValueError("Y1 must have the same number of columns as Y2 columns")
if __debug__:
Parameter.checkArray(omega, softCheck=True, arrayInfo="omega as input in eigenAdd2()")
Parameter.checkArray(Q, softCheck=True, arrayInfo="Q as input in eigenAdd2()")
Parameter.checkOrthogonal(Q, tol=EigenUpdater.tol, softCheck=True, arrayInfo="Q as input in eigenAdd2()")
Parameter.checkArray(Y1, softCheck=True, arrayInfo="Y1 as input in eigenAdd2()")
Parameter.checkArray(Y2, softCheck=True, arrayInfo="Y2 as input in eigenAdd2()")
#Get first k eigenvectors/values of A^*A
omega, Q = Util.indEig(omega, Q, numpy.flipud(numpy.argsort(omega))[0:k])
QY1 = Q.conj().T.dot(Y1)
Y1bar = Y1 - Q.dot(QY1)
P1bar, sigma1Bar, Q1bar = Util.safeSvd(Y1bar)
inds = numpy.arange(sigma1Bar.shape[0])[numpy.abs(sigma1Bar)>EigenUpdater.tol]
P1bar, sigma1Bar, Q1bar = Util.indSvd(P1bar, sigma1Bar, Q1bar, inds)
# checks on SVD decomposition of Y1bar
if __debug__:
Parameter.checkArray(QY1, softCheck=True, arrayInfo="QY1 in eigenAdd2()")
Parameter.checkArray(Y1bar, softCheck=True, arrayInfo="Y1bar in eigenAdd2()")
Parameter.checkArray(P1bar, softCheck=True, arrayInfo="P1bar in eigenAdd2()")
if not Parameter.checkOrthogonal(P1bar, tol=EigenUpdater.tol, softCheck=True, arrayInfo="P1bar in eigenAdd2()", investigate=True):
print ("corresponding sigma: ", sigma1Bar)
Parameter.checkArray(sigma1Bar, softCheck=True, arrayInfo="sigma1Bar in eigenAdd2()")
Parameter.checkArray(Q1bar, softCheck=True, arrayInfo="Q1bar in eigenAdd2()")
if not Parameter.checkOrthogonal(Q1bar, tol=EigenUpdater.tol, softCheck=True, arrayInfo="Q1bar in eigenAdd2()"):
print ("corresponding sigma: ", sigma1Bar)
del Y1bar
P1barY2 = P1bar.conj().T.dot(Y2)
QY2 = Q.conj().T.dot(Y2)
Y2bar = Y2 - Q.dot(QY2) - P1bar.dot(P1barY2)
P2bar, sigma2Bar, Q2bar = Util.safeSvd(Y2bar)
inds = numpy.arange(sigma2Bar.shape[0])[numpy.abs(sigma2Bar)>EigenUpdater.tol]
P2bar, sigma2Bar, Q2bar = Util.indSvd(P2bar, sigma2Bar, Q2bar, inds)
# checks on SVD decomposition of Y1bar
if __debug__:
Parameter.checkArray(P1barY2, softCheck=True, arrayInfo="P1barY2 in eigenAdd2()")
Parameter.checkArray(QY2, softCheck=True, arrayInfo="QY2 in eigenAdd2()")
Parameter.checkArray(Y2bar, softCheck=True, arrayInfo="Y2bar in eigenAdd2()")
Parameter.checkArray(P2bar, softCheck=True, arrayInfo="P2bar in eigenAdd2()")
Parameter.checkOrthogonal(P2bar, tol=EigenUpdater.tol, softCheck=True, arrayInfo="P2bar in eigenAdd2()")
Parameter.checkArray(sigma2Bar, softCheck=True, arrayInfo="sigma2Bar in eigenAdd2()")
Parameter.checkArray(Q2bar, softCheck=True, arrayInfo="Q2bar in eigenAdd2()")
Parameter.checkOrthogonal(Q2bar, tol=EigenUpdater.tol, softCheck=True, arrayInfo="Q2bar in eigenAdd2()")
del Y2bar
r = omega.shape[0]
p = Y1.shape[1]
p1 = sigma1Bar.shape[0]
p2 = sigma2Bar.shape[0]
D = numpy.c_[Q, P1bar, P2bar]
del P1bar
del P2bar
# rem: A*s = A.dot(diag(s)) ; A*s[:,new] = diag(s).dot(A)
DStarY1 = numpy.r_[QY1, sigma1Bar[:,numpy.newaxis] * Q1bar.conj().T, numpy.zeros((p2, p))]
DStarY2 = numpy.r_[QY2, P1barY2, sigma2Bar[:,numpy.newaxis] * Q2bar.conj().T]
DStarY1Y2StarD = DStarY1.dot(DStarY2.conj().T)
del DStarY1
del DStarY2
r = omega.shape[0]
F = numpy.zeros((r+p1+p2, r+p1+p2))
#.........这里部分代码省略.........