Table of Contents

Mojo struct

Matrix

@memory_only
struct Matrix

Native matrix data structure.

Aliases

  • simd_width = (Int(4) * simd_width_of[DType.float32]()) if CompilationTarget.is_apple_silicon() else (Int(2) * simd_width_of[DType.float32]())

Fields

  • height (Int): The number of rows.
  • width (Int): The number of columns.
  • size (Int): The total size.
  • data (Pointer[Float32, MutUntrackedOrigin]): The pointer to the underlying data.
  • order (String): The order of matrix: Row-major -> 'c'; Column-major -> 'f'.

Implemented traits

AnyType, Copyable, Deinitable, ImplicitlyCopyable, Movable, Sized, Writable

Methods

__init__

fn def __init__[src: DType = DType.float32](out self, data: Pointer[Scalar[src], MutUntrackedOrigin], height: Int, width: Int, order: String = "c")

Parameters:

  • src (DType)

Args:

  • data (Pointer[Scalar[src], MutUntrackedOrigin])
  • height (Int)
  • width (Int)
  • order (String)
  • self (Self)

Returns:

Self

fn def __init__(out self, height: Int, width: Int, data: Optional[Pointer[Float32, MutUntrackedOrigin]] = None, order: String = "c")

Args:

  • height (Int)
  • width (Int)
  • data (Optional[Pointer[Float32, MutUntrackedOrigin]])
  • order (String)
  • self (Self)

Returns:

Self

fn def __init__(out self, def_input: List[List[Float32]])

Args:

  • def_input (List[List[Float32]])
  • self (Self)

Returns:

Self

Raises:

fn def __init__(out self, *, copy: Self)

Args:

  • copy (Self)
  • self (Self)

Returns:

Self

fn def __init__(out self, *, deinit move: Self)

Args:

  • move (Self)
  • self (Self)

Returns:

Self

__deinit__

fn def __deinit__(deinit self)

Args:

  • self (Self)

__getitem__

fn def __getitem__(self, row: Int, column: Int) -> Float32

The pattern to access a single value: [row, column] .

Args:

  • self (Self)
  • row (Int)
  • column (Int)

Returns:

Float32

Raises:

fn def __getitem__(self, row: Int) -> Self

The pattern to access a row: [row] .

Args:

  • self (Self)
  • row (Int)

Returns:

Self

Raises:

fn def __getitem__(self, row: Int, *, unsafe: Bool) -> Self

Args:

  • self (Self)
  • row (Int)
  • unsafe (Bool)

Returns:

Self

fn def __getitem__(self, row: Int, offset: Bool, start_i: Int) -> Self

Args:

  • self (Self)
  • row (Int)
  • offset (Bool)
  • start_i (Int)

Returns:

Self

Raises:

fn def __getitem__(self, row: String, column: Int) -> Self

The pattern to access a column: ['', column] .

Args:

  • self (Self)
  • row (String)
  • column (Int)

Returns:

Self

Raises:

fn def __getitem__(self, row: String, column: Int, *, unsafe: Bool) -> Self

Args:

  • self (Self)
  • row (String)
  • column (Int)
  • unsafe (Bool)

Returns:

Self

fn def __getitem__(self, offset: Bool, start_i: Int, column: Int) -> Self

Args:

  • self (Self)
  • offset (Bool)
  • start_i (Int)
  • column (Int)

Returns:

Self

Raises:

fn def __getitem__(self, rows: Self) -> Self

Args:

  • self (Self)
  • rows (Self)

Returns:

Self

Raises:

fn def __getitem__(self, row: String, columns: Self) -> Self

Args:

  • self (Self)
  • row (String)
  • columns (Self)

Returns:

Self

Raises:

fn def __getitem__(self, rows: List[Int]) -> Self

Args:

  • self (Self)
  • rows (List[Int])

Returns:

Self

Raises:

fn def __getitem__(self, row: String, columns: List[Int]) -> Self

Args:

  • self (Self)
  • row (String)
  • columns (List[Int])

Returns:

Self

Raises:

__setitem__

fn def __setitem__(mut self, row: Int, column: Int, val: Float32)

Args:

  • self (Self)
  • row (Int)
  • column (Int)
  • val (Float32)

Raises:

fn def __setitem__(mut self, row: Int, val: Self)

Args:

  • self (Self)
  • row (Int)
  • val (Self)

Raises:

fn def __setitem__(mut self, row: Int, val: Self, *, unsafe: Bool)

Args:

  • self (Self)
  • row (Int)
  • val (Self)
  • unsafe (Bool)
fn def __setitem__(mut self, row: Int, offset: Bool, start_i: Int, val: Self)

Args:

  • self (Self)
  • row (Int)
  • offset (Bool)
  • start_i (Int)
  • val (Self)

Raises:

fn def __setitem__(mut self, row: String, column: Int, val: Self)

Args:

  • self (Self)
  • row (String)
  • column (Int)
  • val (Self)

Raises:

fn def __setitem__(mut self, row: String, column: Int, val: Self, *, unsafe: Bool)

Args:

  • self (Self)
  • row (String)
  • column (Int)
  • val (Self)
  • unsafe (Bool)
fn def __setitem__(mut self, offset: Bool, start_i: Int, column: Int, val: Self)

Args:

  • self (Self)
  • offset (Bool)
  • start_i (Int)
  • column (Int)
  • val (Self)

Raises:

__neg__

fn def __neg__(self) -> Self

Args:

  • self (Self)

Returns:

Self

__lt__

fn def __lt__(self, rhs: Float32) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List[Scalar[DType.bool]]

__le__

fn def __le__(self, rhs: Float32) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List[Scalar[DType.bool]]

__eq__

fn def __eq__(self, rhs: Float32) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List[Scalar[DType.bool]]

fn def __eq__(self, rhs: Self) -> Bool

Args:

  • self (Self)
  • rhs (Self)

Returns:

Bool

__ne__

fn def __ne__(self, rhs: Float32) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List[Scalar[DType.bool]]

fn def __ne__(self, rhs: Self) -> Bool

Args:

  • self (Self)
  • rhs (Self)

Returns:

Bool

__gt__

fn def __gt__(self, rhs: Float32) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List[Scalar[DType.bool]]

__ge__

fn def __ge__(self, rhs: Float32) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List[Scalar[DType.bool]]

__add__

fn def __add__(self, rhs: Self) -> Self

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

fn def __add__(self, rhs: Float32) -> Self

Args:

  • self (Self)
  • rhs (Float32)

Returns:

Self

__sub__

fn def __sub__(self, rhs: Self) -> Self

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

fn def __sub__(self, rhs: Float32) -> Self

Args:

  • self (Self)
  • rhs (Float32)

Returns:

Self

__mul__

fn def __mul__(self, rhs: Self) -> Self

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

fn def __mul__(self, rhs: Float32) -> Self

Args:

  • self (Self)
  • rhs (Float32)

Returns:

Self

__truediv__

fn def __truediv__(self, rhs: Self) -> Self

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

fn def __truediv__(self, rhs: Float32) -> Self

Args:

  • self (Self)
  • rhs (Float32)

Returns:

Self

__pow__

fn def __pow__(self, p: Int) -> Self

Args:

  • self (Self)
  • p (Int)

Returns:

Self

__radd__

fn def __radd__(self, lhs: Float32) -> Self

Args:

  • self (Self)
  • lhs (Float32)

Returns:

Self

__rsub__

fn def __rsub__(self, lhs: Float32) -> Self

Args:

  • self (Self)
  • lhs (Float32)

Returns:

Self

__rmul__

fn def __rmul__(self, lhs: Float32) -> Self

Args:

  • self (Self)
  • lhs (Float32)

Returns:

Self

__rtruediv__

fn def __rtruediv__(self, lhs: Float32) -> Self

Args:

  • self (Self)
  • lhs (Float32)

Returns:

Self

__iadd__

fn def __iadd__(mut self, rhs: Self)

Args:

  • self (Self)
  • rhs (Self)

Raises:

fn def __iadd__(mut self, rhs: Float32)

Args:

  • self (Self)
  • rhs (Float32)

__isub__

fn def __isub__(mut self, rhs: Self)

Args:

  • self (Self)
  • rhs (Self)

Raises:

fn def __isub__(mut self, rhs: Float32)

Args:

  • self (Self)
  • rhs (Float32)

__imul__

fn def __imul__(mut self, rhs: Self)

Args:

  • self (Self)
  • rhs (Self)

Raises:

fn def __imul__(mut self, rhs: Float32)

Args:

  • self (Self)
  • rhs (Float32)

__itruediv__

fn def __itruediv__(mut self, rhs: Self)

Args:

  • self (Self)
  • rhs (Self)

Raises:

fn def __itruediv__(mut self, rhs: Float32)

Args:

  • self (Self)
  • rhs (Float32)

__ipow__

fn def __ipow__(mut self, rhs: Int)

Args:

  • self (Self)
  • rhs (Int)

load

fn def load[nelts: Int](self, y: Int, x: Int) -> SIMD[DType.float32, nelts]

Parameters:

  • nelts (Int)

Args:

  • self (Self)
  • y (Int)
  • x (Int)

Returns:

SIMD[DType.float32, nelts]

store

fn def store[nelts: Int](self, y: Int, x: Int, val: SIMD[DType.float32, nelts])

Parameters:

  • nelts (Int)

Args:

  • self (Self)
  • y (Int)
  • x (Int)
  • val (SIMD[DType.float32, nelts])

load_columns

fn def load_columns(self, _range: Int) -> Self

Args:

  • self (Self)
  • _range (Int)

Returns:

Self

Raises:

load_rows

fn def load_rows(self, _range: Int) -> Self

Args:

  • self (Self)
  • _range (Int)

Returns:

Self

Raises:

get_per_row

fn def get_per_row(self, columns: Self) -> Self

Args:

  • self (Self)
  • columns (Self)

Returns:

Self

Raises:

set_per_row

fn def set_per_row(mut self, columns: Self, rhs: Self)

Args:

  • self (Self)
  • columns (Self)
  • rhs (Self)

Raises:

__len__

fn def __len__(self) -> Int

Args:

  • self (Self)

Returns:

Int

ele_eq

fn def ele_eq(self, rhs: Self) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Self)

Returns:

List[Scalar[DType.bool]]

ele_ne

fn def ele_ne(self, rhs: Self) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Self)

Returns:

List[Scalar[DType.bool]]

ele_gt

fn def ele_gt(self, rhs: Self) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Self)

Returns:

List[Scalar[DType.bool]]

ele_ge

fn def ele_ge(self, rhs: Self) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Self)

Returns:

List[Scalar[DType.bool]]

ele_lt

fn def ele_lt(self, rhs: Self) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Self)

Returns:

List[Scalar[DType.bool]]

ele_le

fn def ele_le(self, rhs: Self) -> List[Scalar[DType.bool]]

Args:

  • self (Self)
  • rhs (Self)

Returns:

List[Scalar[DType.bool]]

ele_mul

fn def ele_mul(self, rhs: Self) -> Self

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

where

fn def where(self, cmp: List[Scalar[DType.bool]], _true: Float32, _false: Float32) -> Self

Args:

  • self (Self)
  • cmp (List[Scalar[DType.bool]])
  • _true (Float32)
  • _false (Float32)

Returns:

Self

fn def where(self, cmp: List[Scalar[DType.bool]], _true: Self, _false: Float32) -> Self

Args:

  • self (Self)
  • cmp (List[Scalar[DType.bool]])
  • _true (Self)
  • _false (Float32)

Returns:

Self

fn def where(self, cmp: List[Scalar[DType.bool]], _true: Float32, _false: Self) -> Self

Args:

  • self (Self)
  • cmp (List[Scalar[DType.bool]])
  • _true (Float32)
  • _false (Self)

Returns:

Self

fn def where(self, cmp: List[Scalar[DType.bool]], _true: Self, _false: Self) -> Self

Args:

  • self (Self)
  • cmp (List[Scalar[DType.bool]])
  • _true (Self)
  • _false (Self)

Returns:

Self

C_transpose

fn def C_transpose(self) -> Self

Args:

  • self (Self)

Returns:

Self

F_transpose

fn def F_transpose(self) -> Self

Args:

  • self (Self)

Returns:

Self

T

fn def T(self) -> Self

Args:

  • self (Self)

Returns:

Self

asorder

fn def asorder(self, order: String) -> Self

Args:

  • self (Self)
  • order (String)

Returns:

Self

cumsum

fn def cumsum(self) -> Self

Args:

  • self (Self)

Returns:

Self

sum

fn def sum(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

fn def sum(self, axis: Int) -> Self

Args:

  • self (Self)
  • axis (Int)

Returns:

Self

Raises:

mean

fn def mean(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

fn def mean(self, axis: Int) -> Self

Args:

  • self (Self)
  • axis (Int)

Returns:

Self

Raises:

mean_weighted

fn def mean_weighted(self, weights: Self, size: Float32) -> Float32

Args:

  • self (Self)
  • weights (Self)
  • size (Float32)

Returns:

Float32

Raises:

mean_slow

fn def mean_slow(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

mean_slow0

fn def mean_slow0(self) -> Self

Args:

  • self (Self)

Returns:

Self

Raises:

std

fn def std(self, correction: Bool = False) -> Float32

Args:

  • self (Self)
  • correction (Bool)

Returns:

Float32

Raises:

fn def std(self, _mean: Float32, correction: Bool = False) -> Float32

Args:

  • self (Self)
  • _mean (Float32)
  • correction (Bool)

Returns:

Float32

Raises:

fn def std(self, axis: Int, correction: Bool = False) -> Self

Args:

  • self (Self)
  • axis (Int)
  • correction (Bool)

Returns:

Self

Raises:

fn def std(self, axis: Int, _mean: Self, correction: Bool = False) -> Self

Args:

  • self (Self)
  • axis (Int)
  • _mean (Self)
  • correction (Bool)

Returns:

Self

Raises:

std_slow

fn def std_slow(self, _mean: Float32) -> Float32

Args:

  • self (Self)
  • _mean (Float32)

Returns:

Float32

Raises:

fn def std_slow(self, axis: Int, _mean: Self) -> Self

Args:

  • self (Self)
  • axis (Int)
  • _mean (Self)

Returns:

Self

Raises:

abs

fn def abs(self) -> Self

Args:

  • self (Self)

Returns:

Self

log

fn def log(self) -> Self

Args:

  • self (Self)

Returns:

Self

sqrt

fn def sqrt(self) -> Self

Args:

  • self (Self)

Returns:

Self

exp

fn def exp(self) -> Self

Args:

  • self (Self)

Returns:

Self

argmin

fn def argmin(self) -> Int

Args:

  • self (Self)

Returns:

Int

fn def argmin(self, axis: Int) -> List[Int]

Args:

  • self (Self)
  • axis (Int)

Returns:

List[Int]

argmax

fn def argmax(self) -> Int

Args:

  • self (Self)

Returns:

Int

fn def argmax(self, axis: Int) -> List[Int]

Args:

  • self (Self)
  • axis (Int)

Returns:

List[Int]

argmax_f

fn def argmax_f(self, axis: Int) -> Self

Args:

  • self (Self)
  • axis (Int)

Returns:

Self

argsort

fn def argsort[ascending: Bool = True](self) -> List[Int]

Parameters:

  • ascending (Bool)

Args:

  • self (Self)

Returns:

List[Int]

Raises:

argsort_inplace

fn def argsort_inplace[ascending: Bool = True](mut self, mut sorted_indices: List[Int])

Parameters:

  • ascending (Bool)

Args:

  • self (Self)
  • sorted_indices (List[Int])

Raises:

min

fn def min(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

fn def min(self, axis: Int) -> Self

Args:

  • self (Self)
  • axis (Int)

Returns:

Self

Raises:

max

fn def max(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

fn def max(self, axis: Int) -> Self

Args:

  • self (Self)
  • axis (Int)

Returns:

Self

Raises:

reshape

fn def reshape(self, height: Int, width: Int) -> Self

Args:

  • self (Self)
  • height (Int)
  • width (Int)

Returns:

Self

lu_factor

@staticmethod
fn def lu_factor(mut A, piv: Pointer[Int, MutAnyOrigin], N: Int)

Args:

  • A (Self)
  • piv (Pointer[Int, MutAnyOrigin])
  • N (Int)

Raises:

lu_solve

@staticmethod
fn def lu_solve(A, piv: Pointer[Int, MutAnyOrigin], b: Self, mut x: Self, N: Int, Mi: Int)

Args:

  • A (Self)
  • piv (Pointer[Int, MutAnyOrigin])
  • b (Self)
  • x (Self)
  • N (Int)
  • Mi (Int)

Raises:

solve

@staticmethod
fn def solve(var A, b: Self) -> Self

Args:

  • A (Self)
  • b (Self)

Returns:

Self

Raises:

inv

fn def inv(self) -> Self

Args:

  • self (Self)

Returns:

Self

Raises:

eye

@staticmethod
fn def eye(var n: Int, order: String = "c") -> Self

Args:

  • n (Int)
  • order (String)

Returns:

Self

norm

fn def norm(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

outer

fn def outer(self, rhs: Self) -> Self

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

concatenate

fn def concatenate(self, rhs: Self, axis: Int) -> Self

Args:

  • self (Self)
  • rhs (Self)
  • axis (Int)

Returns:

Self

Raises:

bincount

fn def bincount(self) -> List[Int]

Args:

  • self (Self)

Returns:

List[Int]

Raises:

fn def bincount(self, weights: Self) -> List[Int]

Args:

  • self (Self)
  • weights (Self)

Returns:

List[Int]

Raises:

unique

fn def unique(self) -> List[List[Int]]

Args:

  • self (Self)

Returns:

List[List[Int]]

fn def unique(self, weights: Self) -> List[List[Int]]

Args:

  • self (Self)
  • weights (Self)

Returns:

List[List[Int]]

is_uniquef

fn def is_uniquef(self) -> Int

Args:

  • self (Self)

Returns:

Int

zeros

@staticmethod
fn def zeros(height: Int, width: Int, order: String = "c") -> Self

Args:

  • height (Int)
  • width (Int)
  • order (String)

Returns:

Self

ones

@staticmethod
fn def ones(height: Int, width: Int, order: String = "c") -> Self

Args:

  • height (Int)
  • width (Int)
  • order (String)

Returns:

Self

full

@staticmethod
fn def full(height: Int, width: Int, val: Float32, order: String = "c") -> Self

Args:

  • height (Int)
  • width (Int)
  • val (Float32)
  • order (String)

Returns:

Self

fill_zero

fn def fill_zero(self)

Args:

  • self (Self)

fill

fn def fill(self, val: Float32)

Args:

  • self (Self)
  • val (Float32)

random

@staticmethod
fn def random(height: Int, width: Int, order: String = "c") -> Self

Args:

  • height (Int)
  • width (Int)
  • order (String)

Returns:

Self

rand_choice

@staticmethod
fn def rand_choice(arang: Int, size: Int, replace: Bool = True, seed: Bool = True) -> List[Int]

Args:

  • arang (Int)
  • size (Int)
  • replace (Bool)
  • seed (Bool)

Returns:

List[Int]

Raises:

from_numpy

@staticmethod
fn def from_numpy(np_arr: PythonObject, order: String = "c") -> Self

Initialize a matrix from a numpy array.

Args:

  • np_arr (PythonObject)
  • order (String)

Returns:

Self: The matrix.

Raises:

to_numpy

fn def to_numpy(self) -> PythonObject

Converts the matrix to a numpy array.

Args:

  • self (Self)

Returns:

PythonObject: The numpy array.

Raises:

cast_ptr

fn def cast_ptr[des: DType](self) -> Pointer[Scalar[des], MutUntrackedOrigin]

Parameters:

  • des (DType)

Args:

  • self (Self)

Returns:

Pointer[Scalar[des], MutUntrackedOrigin]

write_to

fn def write_to[W: Writer](self, mut writer: W)

Parameters:

  • W (Writer)

Args:

  • self (Self)
  • writer (W)

__str__

fn def __str__(self) -> String

Args:

  • self (Self)

Returns:

String