numo-narray icon indicating copy to clipboard operation
numo-narray copied to clipboard

[Feature] Support broadcast_to

Open sonots opened this issue 6 years ago • 0 comments

In the case of numpy, we can create a contiguous array from a broadcasted array as:

In [11]: a = np.arange(3).reshape(3, 1)

In [12]: a.strides
Out[12]: (8, 8)

In [13]: np.broadcast_to(a, (3, 3)).flags
Out[13]:
  C_CONTIGUOUS : False
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : False
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

In [14]: np.broadcast_to(a, (3, 3)).strides
Out[14]: (8, 0)

In [15]: np.broadcast_to(a, (3, 3)).copy().flags
Out[15]:
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

In [16]: np.broadcast_to(a, (3, 3)).copy().strides
Out[16]: (24, 8)

Since some operations for non-contigous array is extremely slow, we sometimes want to create such contiguous array beforehand.

sonots avatar Apr 26 '18 04:04 sonots