ruby-numo / numo-linalg

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

Fix the UPCAST reference processing in the blas_char method #12

Closed yoshoku closed 6 years ago

yoshoku commented 6 years ago

Problem

I found that performing the dot method with DFloat and DComplex fails on Numo::Linalg.

> require 'numo/narray'
=> true
> a=Numo::DFloat.new(2,2).rand
=> Numo::DFloat#shape=[2,2]
[[0.0617545, 0.373067],
 [0.794815, 0.201042]]
> b=Numo::DComplex.new(2,2).rand
=> Numo::DComplex#shape=[2,2]
[[0.116041+0.344032i, 0.539948+0.737815i],
 [0.165089+0.0508827i, 0.108065+0.0687079i]]
> a.dot(b)
=> Numo::DComplex#shape=[2,2]
[[0.0687551+0.0402282i, 0.0736595+0.071196i],
 [0.125421+0.283672i, 0.450884+0.60024i]]

> require ‘numo/linalg/autoloader’
=> true
> a.dot(b)
TypeError: invalid data type for BLAS/LAPACK
…

I am sorry, there was a mistake in copying from my irb prompt. I fixed that at July 18, 2018.

Solution

I thought that the cause of this failure lies in the reference processing of UPCAST in the blas_char method. The specification about UPCAST is written as follows; When the Int8 type not included in Numo::Int32::UPCAST, it checks Numo::Int8::UPCAST. However, in the blas method, if the type is not included in UPCAST, it returns nil and stops referring. Thus, I fixed to refer to UPCAST for each other.

In addition, I fixed the type checking in the eigh method to simple codes.

Test

> require 'numo/linalg/autoloader'
=> true
> a=Numo::DFloat.new(2,2).rand
=> Numo::DFloat#shape=[2,2]
[[0.0617545, 0.373067],
 [0.794815, 0.201042]]
> b=Numo::DComplex.new(2,2).rand
=> Numo::DComplex#shape=[2,2]
[[0.116041+0.344032i, 0.539948+0.737815i],
 [0.165089+0.0508827i, 0.108065+0.0687079i]]
> a.dot(b)
=> Numo::DComplex#shape=[2,2]
[[0.0687551+0.0402282i, 0.0736595+0.071196i],
 [0.125421+0.283672i, 0.450884+0.60024i]]