nmatrix icon indicating copy to clipboard operation
nmatrix copied to clipboard

Passing array of arrays as shape leads to bad output

Open v0dro opened this issue 9 years ago • 20 comments

This is the result when passing the shape argument of the .zeroes function as [[3]]:


a=NMatrix.zeroes([[3]], dtype: :complex128)
# => #<NMatrix:0x8669998 shape:[70470990] dtype:complex128 stype:dense>

It also leads to a segmentation fault sometimes if the shape turns out to be TOO big.

v0dro avatar Dec 21 '15 12:12 v0dro

Good catch.

translunar avatar Dec 21 '15 14:12 translunar

Running the above code give me the following error:

[10] pry(main)> a=NMatrix.zeroes([[3]], dtype: :complex128)
TypeError: no implicit conversion of Array into Integer
from /home/ubuntu/workspace/nmatrix/lib/nmatrix/shortcuts.rb:147:in `initialize'

What is it that I am doing wrong here?

lokeshh avatar Dec 22 '15 04:12 lokeshh

It's because you have double brackets (which is the bug @v0dro was highlighting). For correct functionality, you want one of these:

a = NMatrix.zeroes([3], dtype: :complex128)
a = NMatrix.zeroes([3,1], dtype: :complex128)
a = NMatrix.zeroes([1,3], dtype: :complex128)

translunar avatar Dec 22 '15 15:12 translunar

So, what should be correct behavior? I mean if we enter [[3]] as shape what output would it be nice to get?

lokeshh avatar Dec 22 '15 16:12 lokeshh

You shouldn't enter [[3]]. It's nonsensical. But NMatrix should be checking to make sure the first entry in the array is a Fixnum.

translunar avatar Dec 22 '15 16:12 translunar

But NMatrix should be checking to make sure the first entry in the array is a Fixnum.

I don't think I am getting the real picture here. Kindly explain to me given [[3]] is nonsensical, what's the point of checking for it?

Even if we introduce check for this function zeros, what about other functions?

There are other function which could give the same error. For example:

[32] pry(main)> NMatrix.diag([[[3]]])
TypeError: no implicit conversion of Array into Integer
from /home/ubuntu/workspace/nmatrix/lib/nmatrix/shortcuts.rb:271:in `[]='

lokeshh avatar Dec 22 '15 18:12 lokeshh

I'm not really sure what you're asking. We check for lots of user input that is nonsensical because those nonsensical inputs cause unpredictable behavior (such as segmentation faults).

You don't have to include the check in zeros. Zeros just calls the regular constructor, which is written in C, which itself calls a function which interprets the shape argument. We only need to modify that shape interpretation function.

translunar avatar Dec 22 '15 18:12 translunar

Ok. The point of confusion here is that I am not getting unpredictable behaviour at all. I am getting a TypeError every time I pass an invalid shape argument.

lokeshh avatar Dec 23 '15 03:12 lokeshh

Ok that's weird.

What version are you using? Can you freshly clone nmatrix from sciruby and try it out?

v0dro avatar Dec 23 '15 08:12 v0dro

I deleted my old repository from github and my computer and did like you suggested and it's the same. Below I have explained in detail what I did.

I forked the repository https://github.com/SciRuby/nmatrix to https://github.com/lokeshh/nmatrix. Then I cloned it into my computer. I did bundle install, then bundle exec rake compile, then bundle exec rake spec.

Then on the console which I got by executing bundle exec rake pry, I got the same output as earlier:

pry -r './lib/nmatrix.rb'
[1] pry(main)> a = NMatrix.zeros([[3]])
TypeError: no implicit conversion of Array into Integer
from /home/ubuntu/workspace/nmatrix/lib/nmatrix/shortcuts.rb:147:in `initialize'

lokeshh avatar Dec 23 '15 11:12 lokeshh

Oh, I get what you're saying now. Yes, on Ruby 2.2.2 I get the same output as @lokeshh.

@v0dro What version of Ruby are you using?

translunar avatar Dec 23 '15 17:12 translunar

I'm using 2.2.1. Don't know what's causing this :\

v0dro avatar Dec 23 '15 18:12 v0dro

Are you running it in pry, @v0dro?

translunar avatar Dec 23 '15 18:12 translunar

Yep. Still the same.

v0dro avatar Dec 23 '15 18:12 v0dro

I hate to ask you to install a new Ruby, but does it happen with 2.2.2? I'd like to try to isolate it.

translunar avatar Dec 23 '15 18:12 translunar

One could make a spec and run it on travis to see which versions it works on.

wlevine avatar Dec 23 '15 19:12 wlevine

I installed Ruby 2.2.1 via rmv. The output is still the same for me like earlier. I even recompiled using rake compile.

lokeshh avatar Dec 24 '15 15:12 lokeshh

@v0dro Does the behavior persist if you totally erase your build files and re-build?

translunar avatar Dec 24 '15 16:12 translunar

Just give me some time to reproduce this one.

I'm currently working on the FFTW extension and don't want anything that might break my build.

v0dro avatar Dec 25 '15 18:12 v0dro

I got the bug. I have started working on it.

a=NMatrix.diag([3,2,3,4])
b=NMatrix.diag([[3],2,3,4])

both gives the same result also sometimes it gives segfault.

NakulVaidya avatar Mar 12 '17 01:03 NakulVaidya