本文整理汇总了Python中scipy.sparse.linalg.LinearOperator.rmatvec方法的典型用法代码示例。如果您正苦于以下问题:Python LinearOperator.rmatvec方法的具体用法?Python LinearOperator.rmatvec怎么用?Python LinearOperator.rmatvec使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse.linalg.LinearOperator
的用法示例。
在下文中一共展示了LinearOperator.rmatvec方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _diagonal_operator
# 需要导入模块: from scipy.sparse.linalg import LinearOperator [as 别名]
# 或者: from scipy.sparse.linalg.LinearOperator import rmatvec [as 别名]
def _diagonal_operator(diag):
"""Creates an operator representing a
multiplication with a diagonal matrix"""
diag = diag.ravel()[:, np.newaxis]
def diag_matvec(vec):
if vec.ndim > 1:
return diag * vec
else:
return diag.ravel() * vec
linop = LinearOperator(shape=(len(diag), len(diag)),
matvec=diag_matvec,
rmatvec=diag_matvec,
dtype=np.float64)
linop.matvec = diag_matvec
linop.rmatvec = diag_matvec
return linop
示例2: get_grad_linop
# 需要导入模块: from scipy.sparse.linalg import LinearOperator [as 别名]
# 或者: from scipy.sparse.linalg.LinearOperator import rmatvec [as 别名]
def get_grad_linop(X, Y, invcovB, invcovN, alpha):
"""
Linear operator implementing the gradient of the functional
\frac{1}{2} \|Y - XB\|^2_{\Sigma_n} + \frac{1}{2} \|B\|^2_{\Sigma_s}
which reads
grad_B = X^T(XB - Y)\Sigma_n^{-1} + \lambda B\Sigma_s^{-1}
"""
N, P = X.shape
T = invcovB.shape[0]
if P <= N:
XTX = aslinearoperator(X.T.dot(X))
XTYinvcovN = invcovN.rmatvec(Y.T.dot(X)).T
def matvec(vecB):
XTXB = XTX.matvec(vecB.reshape(T, P).T)
XTXB_invcovN = invcovN.rmatvec(XTXB.T).T
B_incovB = invcovB.rmatvec(vecB.reshape(T, P)).T
result = XTXB_invcovN - XTYinvcovN + alpha * B_incovB
return result.T.ravel()
else:
# raise(Exception)
def matvec(vecB):
XB_minus_Y_invcovN = invcovN.rmatvec(
(X.dot(vecB.reshape(T, P).T) - Y).T).T
XT_XB_minus_Y_invcovN = X.T.dot(XB_minus_Y_invcovN)
B_incovB = invcovB.rmatvec(vecB.reshape(T, P)).T
result = XT_XB_minus_Y_invcovN + alpha * B_incovB
return result.T.ravel()
linop = LinearOperator(shape=tuple([X.shape[1] * Y.shape[1]] * 2),
matvec=matvec,
rmatvec=matvec,
dtype=np.dtype('float64'))
linop.matvec = matvec
linop.rmatvec = matvec
linop.dtype = np.dtype('float64')
return linop
示例3: _woodbury_inverse
# 需要导入模块: from scipy.sparse.linalg import LinearOperator [as 别名]
# 或者: from scipy.sparse.linalg.LinearOperator import rmatvec [as 别名]
def _woodbury_inverse(Ainv, Cinv, U, V):
"""Uses Woodbury Matrix Identity to invert the Matrix
(A + UCV) ^ (-1)
See http://en.wikipedia.org/wiki/Woodbury_matrix_identity"""
def matvec(x):
# this is probably wildly suboptimal, but it works
Ainv_x = Ainv.matvec(x)
Cinv_mat = Cinv.matvec(np.eye(Cinv.shape[0]))
VAinvU = V.dot(Ainv.matvec(U))
inv_Cinv_plus_VAinvU = np.linalg.inv(Cinv_mat + VAinvU)
VAinv_x = V.dot(Ainv_x)
inv_blabla_VAinv_x = inv_Cinv_plus_VAinvU.dot(VAinv_x)
whole_big_block = Ainv.matvec(
U.dot(inv_blabla_VAinv_x))
return Ainv_x - whole_big_block
shape = Ainv.shape
linop = LinearOperator(shape=shape, matvec=matvec)
linop.matvec = matvec
linop.rmatvec = matvec
return linop