Repository navigation
Add matrix commands - #18553
Merged
Merged
Add matrix commands#18553
matrix commands#18553Conversation
fdncred
marked this pull request as ready for review
July 10, 2026 21:06
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
I've been thinking about this for a while. Trying to process data that's faster than nushell does but doesn't really need polars. This is what I came up with. The
matrixfamily of commands.This PR introduces a new
matrixcustom value type backed by the ndarray crate, providing high-performance n-dimensional array operations in Nushell. Modeled after the existingsemvercustom value pattern, all matrix operations operate directly onndarray::ArrayD<f64>data with zeroValueconversion overhead.User-facing changes (Release notes)
Added
matrixcustom value and 21 subcommandsA new
matrixcustom value type enables high-performance matrix math in Nushell, backed by the ndarray library for Rust.Constructors:
matrix zeros <dims>— create a matrix filled with zerosmatrix identity <n>— create an n×n identity matrixinto matrix— convert a table/list-of-lists/list-of-records into a matrixAccess:
matrix get-row <index>— extract a row as a listmatrix get-col <index>— extract a column as a list (2D only)matrix set-row <index> <values>— replace a rowmatrix set-col <index> <values>— replace a column (2D only)Arithmetic:
matrix add <matrix|scalar>— element-wise addition with optional--broadcastmatrix subtract <matrix|scalar>— element-wise subtraction with optional--broadcastmatrix scale <scalar>— multiply all elements by a scalar<operator>expressions:$m + $n,$m * 2.0,$m == $nLinear algebra:
matrix multiply <matrix>— dot product (supports 1D×1D, 2D×1D, 1D×2D, 2D×2D)matrix transpose— swap rows and columns (nD reverses all axes)Transforms:
matrix reshape <dims>— change dimensionsmatrix reshape --flatten— flatten to 1DElement-wise: (could rename this to matrix each if map is confusing)
matrix map { |e| ... }— apply a closure to each element, returning a new matrixReductions:
matrix sum/matrix sum --axis <n>— sum all or along an axismatrix mean— arithmetic mean of all elementsmatrix max/matrix max --axis <n>— maximum all or along an axismatrix reduce --fold <init> { |acc e| ... }— fold all elements to a single valueOutput:
matrix into-nu— convert to table (list of lists)matrix into-nu --as-records— convert to table of records with auto-generated column namesExamples
Standard iterator commands redirect to matrix-specific ones
eachon a matrix → errors: "Usematrix mapfor element-wise operations"par-eachon a matrix → errors: "Usematrix mapfor element-wise operations"reduceon a matrix → errors: "Usematrix reduce --fold <initial> { ... }"Benchmarking
All benchmarks use
std/benchwith 100 rounds to eliminate outliers. Each benchmark compares amatrixcommand (backed by ndarray) against the equivalent operation using only nushell's built-in list/table commands.Environment: release build, Apple Silicon M-series, 100×100 matrices (10,000 elements) for all except matrix multiply (50×50).
Results summary
Additional notes