Search code examples
ruby-on-railsrubymatrixgradient-descent

Dot Product between 2 matrices in ruby, most efficient way


I am writing a machine learning algorithm in ruby that uses gradient descent and logistic regression.

The algorithms works fine, except that in ruby the dot product between matrices is very slow.

I started using a gem RubyPython that allows you to import python libraries like numpy into ruby and use it's functions.

The performance of numpy is impressive. The application started to run 1000% faster, except that i always get segmentation fault halfway.

Does anyone know any other way to speed up the dot product of 2 matrices in ruby?


Solution

  • Ruby actually has a built in matrix library. You can use it by calling

    require 'matrix'
    

    You create a vector object and take the inner product of it. For example, let's create two vectors

    a = Vector[1,3,-5]
    b = Vector[4,-2,-1]
    
    a.inner_product(b)
    => 3