ruby-numo / numo-linalg

Linear Algebra Library for Ruby/Numo::NArray
BSD 3-Clause "New" or "Revised" License
38 stars 9 forks source link

Fix bug in the solve method when given a driver option other than gen #17

Closed yoshoku closed 6 years ago

yoshoku commented 6 years ago

I found a bug in the solve method. The solve method raises NoMethodError when given a driver option other than 'gen' such as 'sym', 'her', and 'pos'.

> require 'numo/linalg/autoloader'
> a = Numo::DFloat.new(3, 3).rand
> b = Numo::DFloat.new(3).rand
> a = 0.5 * (a + a.transpose)
=> Numo::DFloat#shape=[3,3]
[[0.0617545, 0.287054, 0.667382],
 [0.287054, 0.116041, 0.540924],
 [0.667382, 0.540924, 0.165089]]
> Numo::Linalg.solve(a, b, driver: 'sym')
NoMethodError: undefined method `dsymsv' for Numo::Linalg::Lapack:Module
...

This bug occurs because of an inccorect range of the number of characters to cut out from the driver option. In fact, the solve method tries to call the sysv functions in LAPACK. I fixed this bug and confirmed the solve method works correctly.

> x = Numo::Linalg.solve(a, b, driver: 'sym')
=> Numo::DFloat#shape=[3]
[0.202466, -0.161535, 0.126987]
> (a.dot(x) - b).abs.max
=> 6.938893903907228e-18