当前位置: 首页>>代码示例>>Python>>正文


Python Util.matrixPowerh方法代码示例

本文整理汇总了Python中apgl.util.Util.Util.matrixPowerh方法的典型用法代码示例。如果您正苦于以下问题:Python Util.matrixPowerh方法的具体用法?Python Util.matrixPowerh怎么用?Python Util.matrixPowerh使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在apgl.util.Util.Util的用法示例。


在下文中一共展示了Util.matrixPowerh方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testMatrixPowerh

# 需要导入模块: from apgl.util.Util import Util [as 别名]
# 或者: from apgl.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)
开发者ID:awj223,项目名称:Insight-Data-Engineering-Code-Challenge,代码行数:32,代码来源:UtilTest.py

示例2: eigpsd

# 需要导入模块: from apgl.util.Util import Util [as 别名]
# 或者: from apgl.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 = n 
        else: 
            raise ValueError("Invalid n value: " + str(n))
            
        invInds = numpy.setdiff1d(numpy.arange(X.shape[0]), inds)

        if numpy.sort(inds).shape[0] == X.shape[0] and (numpy.sort(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*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
开发者ID:malcolmreynolds,项目名称:APGL,代码行数:58,代码来源:Nystrom.py


注:本文中的apgl.util.Util.Util.matrixPowerh方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。