def __init__(self, arg1, shape=None, dtype=None, copy=False): dict.__init__(self) spmatrix.__init__(self) self.dtype = getdtype(dtype, default=float) if isinstance(arg1, tuple) and isshape(arg1): # (M,N) M, N = arg1 self.shape = (M, N) elif isspmatrix(arg1): # Sparse ctor if isspmatrix_dok(arg1) and copy: arg1 = arg1.copy() else: arg1 = arg1.todok() if dtype is not None: arg1 = arg1.astype(dtype) self.update(arg1) self.shape = arg1.shape self.dtype = arg1.dtype else: # Dense ctor try: arg1 = np.asarray(arg1) except: raise TypeError('invalid input format') if len(arg1.shape)!=2: raise TypeError('expected rank <=2 dense array or matrix') from coo import coo_matrix self.update( coo_matrix(arg1, dtype=dtype).todok() ) self.shape = arg1.shape self.dtype = arg1.dtype
def __init__(self, arg1, shape=None, dtype=None, copy=False): dict.__init__(self) spmatrix.__init__(self) self.dtype = getdtype(dtype, default=float) if isinstance(arg1, tuple) and isshape(arg1): # (M,N) M, N = arg1 self.shape = (M, N) elif isspmatrix(arg1): # Sparse ctor if isspmatrix_dok(arg1) and copy: arg1 = arg1.copy() else: arg1 = arg1.todok() if dtype is not None: arg1 = arg1.astype(dtype) self.update(arg1) self.shape = arg1.shape self.dtype = arg1.dtype else: # Dense ctor try: arg1 = np.asarray(arg1) except: raise TypeError('invalid input format') if len(arg1.shape) != 2: raise TypeError('expected rank <=2 dense array or matrix') from coo import coo_matrix self.update(coo_matrix(arg1, dtype=dtype).todok()) self.shape = arg1.shape self.dtype = arg1.dtype
def __init__(self, arg1, shape=None, dtype=None, copy=False): spmatrix.__init__(self) self.dtype = getdtype(dtype, arg1, default=float) # First get the shape if isspmatrix(arg1): if isspmatrix_lil(arg1) and copy: A = arg1.copy() else: A = arg1.tolil() if dtype is not None: A = A.astype(dtype) self.shape = A.shape self.dtype = A.dtype self.rows = A.rows self.data = A.data elif isinstance(arg1, tuple): if isshape(arg1): if shape is not None: raise ValueError('invalid use of shape parameter') M, N = arg1 self.shape = (M, N) self.rows = np.empty((M, ), dtype=object) self.data = np.empty((M, ), dtype=object) for i in range(M): self.rows[i] = [] self.data[i] = [] else: raise TypeError('unrecognized lil_matrix constructor usage') else: #assume A is dense try: A = np.asmatrix(arg1) except TypeError: raise TypeError('unsupported matrix type') else: from csr import csr_matrix A = csr_matrix(A, dtype=dtype).tolil() self.shape = A.shape self.dtype = A.dtype self.rows = A.rows self.data = A.data
def __init__(self, arg1, shape=None, dtype=None, copy=False): spmatrix.__init__(self) self.dtype = getdtype(dtype, arg1, default=float) # First get the shape if isspmatrix(arg1): if isspmatrix_lil(arg1) and copy: A = arg1.copy() else: A = arg1.tolil() if dtype is not None: A = A.astype(dtype) self.shape = A.shape self.dtype = A.dtype self.rows = A.rows self.data = A.data elif isinstance(arg1,tuple): if isshape(arg1): if shape is not None: raise ValueError('invalid use of shape parameter') M, N = arg1 self.shape = (M,N) self.rows = np.empty((M,), dtype=object) self.data = np.empty((M,), dtype=object) for i in range(M): self.rows[i] = [] self.data[i] = [] else: raise TypeError('unrecognized lil_matrix constructor usage') else: #assume A is dense try: A = np.asmatrix(arg1) except TypeError: raise TypeError('unsupported matrix type') else: from csr import csr_matrix A = csr_matrix(A, dtype=dtype).tolil() self.shape = A.shape self.dtype = A.dtype self.rows = A.rows self.data = A.data
def __init__(self): spmatrix.__init__(self)