Skip to content
Prasun Anand edited this page Mar 4, 2017 · 2 revisions

NMatrix is supported on Ruby-MRI as well as JRuby. Nmatrix-Architecture

NMatrix for JRuby currently supports:

  1. dtype => float64
  2. stype => dense

Hacking NMatrix for JRuby:

  • The frontend exclusive to NMatrix-JRuby resides in lib/nmatrix/jruby.
  • The backend of NMatrix-JRuby resides in ext/nmatrix_java. The backend is built by building a wrapper around Apache Commons Math.

NMatrix object can be represented as:

NMatrix Representation

@s stores elements, @shape stores the shape of the matrix, while @dtype and @stype store the data type and storage type respectively.

Two Dimensional NMatrix

Linear algebra is mostly about two-dimensional matrices. In NMatrix, when performing calculations in a two-dimensional matrix, a one-dimensional array is converted to a two-dimensional matrix. A two-dimensional matrix is stored in the JRuby implementation as a BlockRealMatrix or Array2DRowRealMatrix.

MatrixGenerate

To learn how to call Java code on JRuby, have a look at this resource..

Why use a Java method instead of Ruby method?

  1. Memory Usage and Garbage Collection: A scientific library is memory intensive and hence, every step counts. The JRuby interpreter doesn’t need to dynamically guess the data type and uses less memory, typically around one-tenth of it. If the memory is properly utilized, when the GC kicks in, the GC has to clear less used memory space.

  2. Speed: Using java method greatly improves the speed — by around 1000 times, when compared to using the Ruby method.