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 dot method caused by dot product with complex vectors #14

Closed yoshoku closed 6 years ago

yoshoku commented 6 years ago

I found a bug on the Numo::Linalg.dot method. The bug occurs when performing dot product with complex vectors.

> require 'numo/narray'
=> true
> x=Numo::DComplex.new(3).rand
=> Numo::DComplex#shape=[3]
[0.0617545+0.373067i, 0.794815+0.201042i, 0.116041+0.344032i]
> y=Numo::DComplex.new(3).rand
=> Numo::DComplex#shape=[3]
[0.539948+0.737815i, 0.165089+0.0508827i, 0.108065+0.0687079i]
> x.dot(y)
=> (-0.13202269608393907+0.365782771213446i)
>
*
> require 'numo/linalg/autoloader'
=> true
> x.dot(y)
NoMethodError: undefined method `zdot' for Numo::Linalg::Blas:Module
…

The BLAS function that performs dot product of complex vectors are zdotu and cdotu. Thus, I fixed those functions to be called when given a complex vector to the dot method.

> require 'numo/linalg/autoloader'
=> true
> x=Numo::DComplex.new(3).rand
=> Numo::DComplex#shape=[3]
[0.0617545+0.373067i, 0.794815+0.201042i, 0.116041+0.344032i]
> y=Numo::DComplex.new(3).rand
=> Numo::DComplex#shape=[3]
[0.539948+0.737815i, 0.165089+0.0508827i, 0.108065+0.0687079i]
> x.dot(y)
=> (-0.13202269608393907+0.36578277121344605i)