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

Concatenate empty arrays #128

Closed kojix2 closed 5 years ago

kojix2 commented 5 years ago

Hello

I realized that an empty array could not be concatenated.

require 'numo/narray'
a = Numo::DFloat.new(0)
b = Numo::DFloat.new(3).seq

a.concatenate b # RangeError (0...0 is out of range for size=3)
Numo::DFloat.hstack([a,b]) # same error
b.concatenate a # RangeError (3...3 is out of range for size=3)
Numo::DFloat.hstack([b,a]) # same error

Ruby Array can concatenate empty arrays.

a = []
b = [0,1,2]
a.concat b

it seems that the method that change the size of the narray is written in Ruby, not C. Methods related to concatenate, such as Numo::SFloat.dstack, can be very slow when used with Cumo. So, I don't think NArray needs to behave exactly like Ruby Array. It's okay .

However, it might be useful to concatenate an empty array.

masa16 commented 5 years ago

This issue is fixed, https://github.com/ruby-numo/numo-narray/commit/08bc8a77e3d36c3422b99de54a2a547a3def133a But more work is needed to support numpy-like empty array.

>>> a = numpy.arange(6).reshape(2,3)
>>> a[:,0:1]
array([[0],
       [3]])
>>> a[:,0:0]
array([], shape=(2, 0), dtype=int64)
kojix2 commented 5 years ago

Well, is this an empty array with the fixed shape? I had never thought about it. Mathematics is always out of range for my brain size. I think this issue has been resolved. Thank you.

kojix2 commented 5 years ago

Ah, I Got it.

a = Numo::DFloat.new(2,2).seq
#=> Numo::DFloat#shape=[2,2]
#[[0, 1], 
# [2, 3]]

b = a[a<0]
# should be
# => Numo::DFloat#shape=[2,2](empty)

Numo::DFloat.dstack([a,b])
# should be
#=>Numo::DFloat#shape=[2,2]
#[[0, 1], 
# [2, 3]]
#or
#=>Numo::DFloat#shape=[2,2,1]
#[[[0], 
#  [1]], 
# [[2], 
#  [3]]]

I see. This may be useful.

masa16 commented 5 years ago

Ah, already supported.

> a = Numo::DFloat.new(2,2).seq
=> Numo::DFloat#shape=[2,2]
[[0, 1], 
 [2, 3]]

> b = a[0..0,0..]
=> Numo::DFloat(view)#shape=[1,2]
[[0, 1]]

> c = a[[],0..]
=> Numo::DFloat(view)#shape=[0,2][]

> Numo::DFloat.vstack([a,b,c])
=> Numo::DFloat#shape=[3,2]
[[0, 1], 
 [2, 3], 
 [0, 1]]

But not well tested.

BTW a[a<0] should return 1-dimensional array:

> a = Numo::DFloat.new(2,2).seq
=> Numo::DFloat#shape=[2,2]
[[0, 1], 
 [2, 3]]

> a[a>0]
=> Numo::DFloat(view)#shape=[3]
[1, 2, 3]

> a[a<0]
=> Numo::DFloat(view)#shape=[0][]
kojix2 commented 5 years ago

BTW a[a<0] should return 1-dimensional array

I see...

a[a>1]
# Numo::DFloat(view)#shape=[2]
# [2, 3]

shoud be 1-dimensional array. OK. I understand.