Skip to content
Prototype numeric matrix library for Ruby via SciRuby
C++ Ruby C Shell
Pull request Compare This branch is 363 commits behind SciRuby:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Fast Numerical Linear Algebra Library for Ruby


NMatrix is a fast numerical linear algebra library for Ruby, with dense and sparse matrices, written mostly in C and C++. It is part of the SciRuby project.

NMatrix was inspired by NArray, by Masahiro Tanaka.


To install the latest stable version:

gem install nmatrix

However, you will need to install ATLAS with CBLAS (C interface to BLAS) first. Detailed directions can be found here. The requirements for NMatrix are:

  • ATLAS, preferably with CLAPACK (see here for details)

  • a version of GCC or clang which supports C++0x or C++11

  • Ruby 1.9.3+

  • packable 1.3.5 (used for I/O)

If you want to obtain the latest (development) code, you should generally do:

git clone
cd nmatrix/
bundle install
bundle exec rake compile
bundle exec rake repackage
gem install pkg/nmatrix-0.1.0.rc5.gem

Detailed instructions are available for Mac and Linux. We are currently working on Mavericks (Mac OS X) installation instructions, but in general, you'll need Homebrew and should probably use +brew install gcc48+ instead of using the install script.


If you have a suggestion or want to add documentation for any class or method in NMatrix, please open an issue or send a pull request with the changes.

You can find the complete API documentation on our website.


Create a new NMatrix from a ruby Array:

>> require 'nmatrix'
>>[2, 3], [0, 1, 2, 3, 4, 5], dtype: :int64)
=> [
    [0, 1, 2],
    [3, 4, 5]

Create a new NMatrix using the N shortcut:

>> m = N[ [2, 3, 4], [7, 8, 9] ]
=> [
    [2, 3, 4],
    [7, 8, 9]
>> m.inspect
=> #<NMatrix:0x007f8e121b6cf8shape:[2,3] dtype:int32 stype:dense>

The above output requires that you have a pretty-print-enabled console such as Pry; otherwise, you'll see the output given by inspect.

If you want to learn more about how to create a matrix, read the guide in our wiki.

Again, you can find the complete API documentation on our website.


Read the instructions in if you want to help NMatrix.


The following features exist in the current version of NMatrix (0.1.0.rc1):

  • Matrix and vector storage containers: dense, yale, list (more to come)

  • Data types: byte (uint8), int8, int16, int32, int64, float32, float64, complex64, complex128, rational64, rational128, Ruby object

  • Interconversion between storage and data types

  • Element-wise and right-hand-scalar operations and comparisons for all matrix types

  • Matrix-matrix multiplication for dense (with and without ATLAS) and yale

  • Matrix-vector multiplication for dense (with and without ATLAS)

  • Lots of enumerators (each, each_with_indices, each_row, each_column, each_rank, map, etc.)

  • Matrix slicing by copy and reference (for dense, yale, and list)

  • Native reading and writing of dense and yale matrices

    • Optional compression for dense matrices with symmetry or triangularity: symmetric, skew, hermitian, upper, lower

  • Input/output:

    • Matlab .MAT v5 file input

    • MatrixMarket file input/output

    • Harwell-Boeing and Fortran file input

    • Point Cloud Library PCD file input

  • C and C++ API

  • BLAS internal implementations (no library) and ATLAS (with library) access:

    • Level 1: xROT, xROTG (BLAS dtypes only), xASUM, xNRM2, IxAMAX

    • Level 2: xGEMV

    • Level 3: xGEMM, xTRSM

  • LAPACK ATLAS access:

    • xGETRF, xGETRI, xGETRS, xGESV (Gaussian elimination)

    • xPOTRF, xPOTRI, xPOTRS, xPOSV (Cholesky factorization)


  • LAPACK-less internal implementations (no LAPACK needed and working on non-BLAS dtypes):

    • xGETRF

    • xLASWP, xSCAL

    • xLAUUM (no LAPACK needed, but BLAS dtypes only)

  • LAPACK (non-ATLAS) access:

    • xGESVD, xGESDD (singular value decomposition)

    • xGEEV (eigenvalue decomposition of a asymmetric square matrices)

  • LU decomposition

  • Matrix inversions

  • Determinant calculation for BLAS dtypes

  • Traces

  • Ruby/GSL interoperability (requires SciRuby’s fork of rb-gsl)

  • slice assignments, e.g.,

    x[1..3,0..4] = some_other_matrix

Planned Features (Short-to-Medium Term)

See the issues tracker for a list of planned features or to request new ones.


Copyright © 2012–15, John Woods and the Ruby Science Foundation.

All rights reserved.

NMatrix, along with SciRuby, is licensed under the BSD 2-clause license. See LICENSE.txt for details.


Support a SciRuby Fellow:

Something went wrong with that request. Please try again.