本文整理汇总了Python中scipy.sparse.linalg.LinearOperator.dtype方法的典型用法代码示例。如果您正苦于以下问题:Python LinearOperator.dtype方法的具体用法?Python LinearOperator.dtype怎么用?Python LinearOperator.dtype使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse.linalg.LinearOperator
的用法示例。
在下文中一共展示了LinearOperator.dtype方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_grad_linop
# 需要导入模块: from scipy.sparse.linalg import LinearOperator [as 别名]
# 或者: from scipy.sparse.linalg.LinearOperator import dtype [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