matrix multiply

For filters.

Multiply two matrices using dot product.

Command Type

built-in

Signature

> matrix multiply {flags} (other) ...rest

Flags

  • --swap, -s: Swap the left and right operands.
  • --multall, -a: Multiply the input matrix with all matrices given as arguments, chaining them left to right (e.g., $a | matrix multiply --multall $b $c multiplies all three together).

Parameters

  • other: The other matrix to multiply with.
  • ...rest: Additional matrices for chained multiplication (requires —multall).

Input/output types:

input output
matrix matrix

Examples

Multiply two 2x2 matrices

> [[1 2] [3 4]] | into matrix | matrix multiply ([[1 0] [0 1]] | into matrix) | matrix into-nu | to nuon
[[1.0, 2.0], [3.0, 4.0]]

Multiply a matrix by its inverse gives identity

> matrix identity 2 | matrix multiply (matrix identity 2) | matrix into-nu | to nuon
[[1.0, 0.0], [0.0, 1.0]]

Swap operands with —swap

> [[1 2] [3 4]] | into matrix | matrix multiply --swap ([[0 1] [1 0]] | into matrix) | matrix into-nu | to nuon
[[3.0, 4.0], [1.0, 2.0]]

Chain-multiply three matrices with —multall

> matrix identity 2 | matrix multiply --multall ([[2 0] [0 2]] | into matrix) ([[3 0] [0 3]] | into matrix) | matrix into-nu | to nuon
[[6.0, 0.0], [0.0, 6.0]]

Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.