ruby-numo / numo-narray

Ruby/Numo::NArray - New NArray class library
http://ruby-numo.github.io/narray/
BSD 3-Clause "New" or "Revised" License
415 stars 41 forks source link

[Feature] Support broadcast_to #96

Open sonots opened 6 years ago

sonots commented 6 years ago

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.