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

Broken NMath.log on a diagonal since 0.9.1.3 #123

Closed Webovani closed 5 years ago

Webovani commented 5 years ago

Hello, I tried example from machine_learning_workbench and was perplexed why it wasn't working for me. After some debugging I found out that Numo::NMath.log method was returning unexpected results when used on a diagonal (DFloat(view) ?)

2.6.1 :001 > require 'numo/narray'
 => true 
2.6.1 :002 > Numo::NArray::VERSION
 => "0.9.1.3" 
2.6.1 :003 > n1 = Numo::DFloat.new(9).fill(1)
 => Numo::DFloat#shape=[9]
[1, 1, 1, 1, 1, 1, 1, 1, 1] 
2.6.1 :004 > Numo::NMath.log(n1)
 => Numo::DFloat#shape=[9]
[0, 0, 0, 0, 0, 0, 0, 0, 0]  # <==== as expected
2.6.1 :005 > n2 = n1.diag.diagonal
 => Numo::DFloat(view)#shape=[9]
[1, 1, 1, 1, 1, 1, 1, 1, 1] 
2.6.1 :006 > Numo::NMath.log(n2)
 => Numo::DFloat#shape=[9]
[0, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf]  # <==== NOPE :(

Rolled back a bit and found out that it is working correctly in 0.9.1.2.

2.6.1 :001 > require 'numo/narray'
 => true 
2.6.1 :002 > Numo::NArray::VERSION
 => "0.9.1.2" 
2.6.1 :003 > n1 = Numo::DFloat.new(9).fill(1)
 => Numo::DFloat#shape=[9]
[1, 1, 1, 1, 1, 1, 1, 1, 1] 
2.6.1 :004 > Numo::NMath.log(n1)
 => Numo::DFloat#shape=[9]
[0, 0, 0, 0, 0, 0, 0, 0, 0]  # <==== as expected
2.6.1 :005 > n2 = n1.diag.diagonal
 => Numo::DFloat(view)#shape=[9]
[1, 1, 1, 1, 1, 1, 1, 1, 1] 
2.6.1 :006 > Numo::NMath.log(n2)
 => Numo::DFloat#shape=[9]
[0, 0, 0, 0, 0, 0, 0, 0, 0]  # <==== as expected
masa16 commented 5 years ago

This bug is fixed by the commit https://github.com/ruby-numo/numo-narray/commit/a6e21997019c29d7bf14f0e6917662dab9d78116 . Thank you for reporting.

Webovani commented 5 years ago

Thanks for quick reaction, much appreciated!