def from_flat(cls, data, copy=True, dtype=None, shape=None): """ Initializes object from flattened data. If copy=False, it tries to recycle the flattened data whenever possible. The caller is responsible for not sharing this data with other mutable objects. Note: This function only normalizes an iterable data for final consumption. """ if cls.__concrete__: if dtype is None or dtype is cls.dtype: new = object.__new__(cls) new.flat = cls.__flat__(data, copy) return new elif cls.size is None: raise TypeError('shapeless types cannot instantiate objects') dtype = dtype or cls.dtype if dtype is Any or dtype is None: data = list(data) dtype = _dtype(data) T = cls.__origin__[cls.shape + (dtype, )] return T.from_flat(data, copy=copy)
def from_flat(cls, data, copy=True, dtype=None, shape=None): """ Initializes object from flattened data. If copy=False, it tries to recycle the flattened data whenever possible. The caller is responsible for not sharing this data with other mutable objects. Note: This function only normalizes an iterable data for final consumption. """ if cls.__concrete__: if dtype is None or dtype is cls.dtype: new = object.__new__(cls) new.flat = cls.__flat__(data, copy) return new elif cls.size is None: raise TypeError('shapeless types cannot instantiate objects') dtype = dtype or cls.dtype if dtype is Any or dtype is None: data = list(data) dtype = _dtype(data) T = cls.__origin__[cls.shape + (dtype,)] return T.from_flat(data, copy=copy)
def __abstract_new__(cls, *args, dtype=None): # @NoSelf """ This function is called when user tries to instantiate an abstract type. It just finds the proper concrete type and instantiate it. """ if dtype is None: dtype = _dtype(args) return cls[len(args), dtype](*args)
def __abstract_new__(cls, *args, shape=None, dtype=None): """ This function is called when user tries to instantiate an abstract type. It just finds the proper concrete type and instantiate it. """ if dtype is None: dtype = _dtype(args) if shape is None: shape = _shape(args) return cls[shape + (dtype, )](*args)
def __abstract_new__(cls, *args, shape=None, dtype=None): """ This function is called when user tries to instantiate an abstract type. It just finds the proper concrete type and instantiate it. """ if dtype is None: dtype = _dtype(args) if shape is None: shape = _shape(args) return cls[shape + (dtype,)](*args)
def from_diag(cls, diag): """ Create diagonal matrix from diagonal terms. """ N = len(diag) data = [0] * (N * N) for i in range(N): data[N * i + i] = diag[i] return cls.__origin__[N, N, _dtype(data)].from_flat(data, copy=False)
def __mul__(self, other): if isinstance(other, (Vec, tuple, list)): other = asvector(other) return asvector([u.dot(other) for u in self.rows()]) elif isinstance(other, number): return self.from_flat([x * other for x in self.flat], copy=False) elif isinstance(other, Mat): cols = list(other.cols()) rows = list(self.rows()) data = sum([[u.dot(v) for u in cols] for v in rows], []) cls = self.__origin__[len(rows), len(cols), _dtype(data)] return cls.from_flat(data, copy=False) else: return NotImplemented