nmatrix
nmatrix copied to clipboard
Passing array of arrays as shape leads to bad output
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.
Good catch.
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?
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)
So, what should be correct behavior? I mean if we enter [[3]]
as shape what output would it be nice to get?
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.
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 `[]='
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.
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.
Ok that's weird.
What version are you using? Can you freshly clone nmatrix from sciruby and try it out?
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'
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?
I'm using 2.2.1. Don't know what's causing this :\
Are you running it in pry, @v0dro?
Yep. Still the same.
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.
One could make a spec and run it on travis to see which versions it works on.
I installed Ruby 2.2.1 via rmv. The output is still the same for me like earlier. I even recompiled using rake compile
.
@v0dro Does the behavior persist if you totally erase your build files and re-build?
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.
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.