def __new__(cls, array):
array = asarray(array)
assert numpy.prod(array.shape)
# Handle children with shape
child_shape = array.flat[0].shape
assert all(elem.shape == child_shape for elem in array.flat)
if child_shape:
# Destroy structure
direct_array = numpy.empty(array.shape + child_shape, dtype=object)
for alpha in numpy.ndindex(array.shape):
for beta in numpy.ndindex(child_shape):
direct_array[alpha + beta] = Indexed(array[alpha], beta)
array = direct_array
# Constant folding
if all(isinstance(elem, Constant) for elem in array.flat):
return Literal(numpy.vectorize(attrgetter('value'))(array))
self = super(ListTensor, cls).__new__(cls)
self.array = array
return self
评论列表
文章目录