Class in prelude::math auto-imported

Matrix

class Matrix<D>
Source: compiler/src/builtin_docs/prelude/math.hy

A matrix: nested fixed-length numeric rows with matrix operators.

Build one with matrix(rows); D is the nested data type, for example Matrix<[[int; 2]; 2]>. Cells are read with m[row][col]. On Matrix values:

  • * is matrix multiplication.
  • +, - and unary - are element-wise.
  • ==, !=, <, <=, >, >= compare cell by cell and return a Matrix of byte (1 where the test holds). A scalar of the cell type is broadcast (m == 1).
  • &, |, ^, <<, >>, ~ work cell by cell on int or byte cells, with scalar broadcast.
  • /, % and ** are rejected: Matrix is not Num.

Examples#

use tests::assert;
let a = matrix([[1, 2], [3, 4]]);
let b = matrix([[5, 6], [7, 8]]);
let p = a * b;
assert!(p[0][0] == 19);
let s = a + a;
assert!(s[1][1] == 8);
let ones = a == 1;
assert!(ones[0][0] == 1 as byte);
assert!(ones[0][1] == 0 as byte);

See also#

↑ ↓ navigate ↵ open