问题描述
我正在尝试将使用 Keops 库生成的 LazyTensor 对象转换为 Numpy 数组。 LazyTensor 对象没有属性可直接将其转换为 Numpy 数组。因此,我使用函数aslinearoperator
作为特技函数,将该符号张量对象转换为scipy线性算子。
>>> A shape (15230,10) A KeOps LazyTensor
>>> from scipy.sparse.linalg import aslinearoperator
>>> L = aslinearoperator(A)
>>> L
>>> <15230x10 _CustomLinearOperator with dtype=float32>
我的问题是如何将n×m LinearOperator 转换为 Numpy数组?
解决方法
您可以使用从MatrixLinearOperator
返回的aslinearoperator
的A
属性,如果它对您实例化L
有用
>>> from scipy.sparse.linalg import aslinearoperator
>>> import numpy as np
>>>
>>> L = aslinearoperator(np.array([[1,2,3],[4,5,6]]))
>>> L.A
array([[1,6]])
否则,只需乘以identity(eye
/ identity
基本上是相同的-我只是不知道眼睛是什么,但确实知道 identity ;))),如果返回了线性运算符的其他子类(我不熟悉Keops及其对象)。
L * np.identity(L.shape[1])
因为身份将是(m,m)
,产品将返回原始矩阵。