我尝试通过多重继承将我自己的方法和属性添加到稀疏矩阵。但是我发现算术运算符在新类中并没有关闭。如何正确地继承scipy.sparse矩阵?
from scipy.sparse import coo_matrix
import numpy as np
class Info(object):
def __init__(self, *arg, **args):
self.hello = "hello"
def say_hello(self):
print("hello")
class A(coo_matrix, Info):
def __init__(self, *arg, **args):
super(type(self), self).__init__(*arg, **args)
a = A(np.random.randint(2, size=(3,3)))
print("type of a: ",type(a))
a.say_hello()
b = 2*a
print("type of b: ",type(b))
这可能会很棘手。不同的稀疏'格式'被实现为不同的类。每个类都有一个'__init__',但也有转换为其他格式的方法。例如,大多数计算都是用'csr'(或'csc')格式完成的。首先,我建议研究一种主要用作输入法的格式,看看它如何与其他格式相互作用,例如'dia_matrix','dok_matrix','bsr_matrix'或'lil_matrix'。 – hpaulj