Skip to content
This repository

Dense and sparse linear algebra library for Ruby via SciRuby



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 --pre

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.2+

  • 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.rc4.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.


Carlos Agarie (@agarie) is currently working to improve the documentation. The best way to get help is by posting issues or sending e-mails to our mailing list. You may also email @agarie, or look for `agarie` on #sciruby at if you want to ask questions or offer suggestions.

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

    • 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

    • 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 (requires LAPACK; BLAS dtypes only)

  • Determinant calculation for BLAS dtypes

  • Vector 2-norms

  • 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)

We are nearing the release of NMatrix 0.1.0, our first beta.

These are features planned for NMatrix 0.2.0:

  • LAPACK-free calculation of determinant, trace, and eigenvalues (characteristic polynomial)

  • LAPACK-free matrix inversions

  • tensor products

  • improved file I/O

    • compression of yale symmetries in I/O

  • optimization of non-BLAS data types on BLAS-like operations (e.g., matrix multiplication for rational numbers)


Copyright © 2012–14, 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.