go-ruby-matrix

Ruby's matrix stdlib in pure Go — Matrix & Vector with exact arithmetic, MRI-compatible, no cgo.

pure Go · zero cgo matrix stdlib Matrix & Vector exact Integer / Rational / Float Num numeric tower determinant · inverse · rank MRI to_s / inspect 100% coverage 6 arches
Documentation GitHub
Documentation (MkDocs Material + mike) License: BSD-3-Clause Go 1.26.4+ Coverage 100%

go-ruby-matrix is a pure-Go (no cgo) reimplementation of Ruby's matrix standard library — Matrix and Vector with MRI 4.0.5-faithful behaviour. It matches MRI's to_s / inspect formatting, its error classes, and — crucially — its exact arithmetic: an Integer matrix's determinant stays an Integer, and inverse produces exact Rationals (Matrix[[1,2],[3,4]].inverseMatrix[[(-2/1), (1/1)], [(3/2), (-1/2)]]) — without any Ruby runtime. Entries flow through a Num tower built on math/big, reproducing MRI's inverse_from Gauss-Jordan step for step. It is the matrix backend for go-embedded-ruby, a standalone sibling of go-ruby-regexp, go-ruby-erb and go-ruby-yaml — differential-tested against MRI, 100% coverage, CI green across 6 arches and 3 OSes.

The Num numeric tower ready

Entries flow through Num*big.Int for Integers, *big.Rat for Rationals, float64 for Floats — with Ruby’s promotion: Integer op Integer stays Integer, any Rational stays Rational even when whole, any Float dominates.

Constructors & accessors ready

New / Build / Identity / Zero / Diagonal / Scalar / RowVector / ColumnVector / Rows / Columns / HStack / VStack, plus RowCount / ColumnCount / At / Row / Column / Each / Minor / FirstMinor / ToA.

Exact operations ready

Add / Sub / Neg / Mul (matrix·matrix, ·scalar, ·vector) / Div / Pow / Transpose / Trace / Determinant / Inverse / Rank — exact over the tower, so an Integer matrix’s determinant stays Integer and inverse produces exact Rationals.

Predicates & formatting ready

Square / IsDiagonal / Symmetric / Orthogonal / Singular / Regular / LowerTriangular / UpperTriangular / IsZero, Eql (==) and Hash, and ToS / Inspect matching MRI’s Matrix[[…], […]] / Matrix.empty(r, c) formatting.

Vector ready

Elements / At / Size / Add / Sub / Mul / InnerProduct (dot) / CrossProduct / Magnitude (norm) / Normalize / Each / Map / Angle / Eql / Independent, with the same exact numeric tower and MRI-faithful errors.

MRI oracle & coverage ready

Deterministic ruby-free tests hold coverage at 100%; a differential oracle compares every operation’s ToS against ruby -rmatrix … .inspect, including the exact-arithmetic results and the Ruby-faithful float formatter. Green across all six 64-bit Go arches and three OSes, cgo disabled.

A faithful port of Ruby's Matrix and Vector in pure Go, cgo disabled, so it cross-compiles and embeds anywhere. Arithmetic is exact wherever MRI's is: a Num tower on math/big keeps Integer and Rational entries exact, reproducing MRI's Gauss-Jordan inverse step for step. Validated differentially against the system ruby -rmatrix — every operation's inspect compared to MRI. It is a standalone, reusable module and the matrix backend for the sibling org github.com/go-embedded-ruby.