Table of Contents

Mojo struct

Matrix

@memory_only
struct Matrix

Native matrix data structure.

Aliases

  • simd_width = (4 * simd_width_of[DType.float32]()) if CompilationTarget.is_apple_silicon() else (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 (UnsafePointer[Float32, MutAnyOrigin]): The pointer to the underlying data.
  • order (String): The order of matrix: Row-major -> 'c'; Column-major -> 'f'.

Implemented traits

AnyType, Copyable, ImplicitlyCopyable, ImplicitlyDestructible, Movable, Sized, Writable

Methods

__init__

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

Parameters:

  • src (DType)

Args:

  • data (UnsafePointer)
  • height (Int)
  • width (Int)
  • order (String)
  • self (Self)

Returns:

Self

def __init__(out self, height: Int, width: Int, data: UnsafePointer[Float32, MutAnyOrigin] = UnsafePointer(), order: String = "c")

Args:

  • height (Int)
  • width (Int)
  • data (UnsafePointer)
  • order (String)
  • self (Self)

Returns:

Self

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

Args:

  • def_input (List)
  • self (Self)

Returns:

Self

Raises:

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

Args:

  • copy (Self)
  • self (Self)

Returns:

Self

def __init__(out self, *, deinit take: Self)

Args:

  • take (Self)
  • self (Self)

Returns:

Self

__del__

def __del__(deinit self)

Args:

  • self (Self)

__getitem__

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:

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

The pattern to access a row: [row] .

Args:

  • self (Self)
  • row (Int)

Returns:

Self

Raises:

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

Args:

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

Returns:

Self

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

Args:

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

Returns:

Self

Raises:

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:

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

Args:

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

Returns:

Self

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

Args:

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

Returns:

Self

Raises:

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

Args:

  • self (Self)
  • rows (Self)

Returns:

Self

Raises:

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

Args:

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

Returns:

Self

Raises:

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

Args:

  • self (Self)
  • rows (List)

Returns:

Self

Raises:

def __getitem__(self, rows: List[Scalar[DType.int]]) -> Self

Args:

  • self (Self)
  • rows (List)

Returns:

Self

Raises:

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

Args:

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

Returns:

Self

Raises:

def __getitem__(self, row: String, columns: List[Scalar[DType.int]]) -> Self

Args:

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

Returns:

Self

Raises:

__setitem__

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

Args:

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

Raises:

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

Args:

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

Raises:

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

Args:

  • self (Self)
  • row (Int)
  • val (Self)
  • unsafe (Bool)
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:

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

Args:

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

Raises:

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

Args:

  • self (Self)
  • row (String)
  • column (Int)
  • val (Self)
  • unsafe (Bool)
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__

def __neg__(self) -> Self

Args:

  • self (Self)

Returns:

Self

__lt__

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

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List

__le__

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

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List

__eq__

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

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

Bool

__ne__

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

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

Bool

__gt__

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

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List

__ge__

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

Args:

  • self (Self)
  • rhs (Float32)

Returns:

List

__add__

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

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

Args:

  • self (Self)
  • rhs (Float32)

Returns:

Self

__sub__

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

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

Args:

  • self (Self)
  • rhs (Float32)

Returns:

Self

__mul__

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

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

Args:

  • self (Self)
  • rhs (Float32)

Returns:

Self

__truediv__

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

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

Args:

  • self (Self)
  • rhs (Float32)

Returns:

Self

__pow__

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

Args:

  • self (Self)
  • p (Int)

Returns:

Self

__radd__

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

Args:

  • self (Self)
  • lhs (Float32)

Returns:

Self

__rsub__

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

Args:

  • self (Self)
  • lhs (Float32)

Returns:

Self

__rmul__

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

Args:

  • self (Self)
  • lhs (Float32)

Returns:

Self

__rtruediv__

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

Args:

  • self (Self)
  • lhs (Float32)

Returns:

Self

__iadd__

def __iadd__(mut self, rhs: Self)

Args:

  • self (Self)
  • rhs (Self)

Raises:

def __iadd__(mut self, rhs: Float32)

Args:

  • self (Self)
  • rhs (Float32)

__isub__

def __isub__(mut self, rhs: Self)

Args:

  • self (Self)
  • rhs (Self)

Raises:

def __isub__(mut self, rhs: Float32)

Args:

  • self (Self)
  • rhs (Float32)

__imul__

def __imul__(mut self, rhs: Self)

Args:

  • self (Self)
  • rhs (Self)

Raises:

def __imul__(mut self, rhs: Float32)

Args:

  • self (Self)
  • rhs (Float32)

__itruediv__

def __itruediv__(mut self, rhs: Self)

Args:

  • self (Self)
  • rhs (Self)

Raises:

def __itruediv__(mut self, rhs: Float32)

Args:

  • self (Self)
  • rhs (Float32)

__ipow__

def __ipow__(mut self, rhs: Int)

Args:

  • self (Self)
  • rhs (Int)

load

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

store

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)

load_columns

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

Args:

  • self (Self)
  • _range (Int)

Returns:

Self

Raises:

load_rows

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

Args:

  • self (Self)
  • _range (Int)

Returns:

Self

Raises:

get_per_row

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

Args:

  • self (Self)
  • columns (Self)

Returns:

Self

Raises:

set_per_row

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

Args:

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

Raises:

__len__

def __len__(self) -> Int

Args:

  • self (Self)

Returns:

Int

ele_eq

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

List

ele_ne

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

List

ele_gt

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

List

ele_ge

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

List

ele_lt

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

List

ele_le

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

List

ele_mul

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

where

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

Args:

  • self (Self)
  • cmp (List)
  • _true (Float32)
  • _false (Float32)

Returns:

Self

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

Args:

  • self (Self)
  • cmp (List)
  • _true (Self)
  • _false (Float32)

Returns:

Self

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

Args:

  • self (Self)
  • cmp (List)
  • _true (Float32)
  • _false (Self)

Returns:

Self

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

Args:

  • self (Self)
  • cmp (List)
  • _true (Self)
  • _false (Self)

Returns:

Self

argwhere_l

def argwhere_l(self, cmp: List[Scalar[DType.bool]]) -> List[Int]

Args:

  • self (Self)
  • cmp (List)

Returns:

List

C_transpose

def C_transpose(self) -> Self

Args:

  • self (Self)

Returns:

Self

F_transpose

def F_transpose(self) -> Self

Args:

  • self (Self)

Returns:

Self

T

def T(self) -> Self

Args:

  • self (Self)

Returns:

Self

asorder

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

Args:

  • self (Self)
  • order (String)

Returns:

Self

cumsum

def cumsum(self) -> Self

Args:

  • self (Self)

Returns:

Self

sum

def sum(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

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

Args:

  • self (Self)
  • axis (Int)

Returns:

Self

Raises:

mean

def mean(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

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

Args:

  • self (Self)
  • axis (Int)

Returns:

Self

Raises:

mean_weighted

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

Args:

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

Returns:

Float32

Raises:

mean_slow

def mean_slow(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

mean_slow0

def mean_slow0(self) -> Self

Args:

  • self (Self)

Returns:

Self

Raises:

std

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

Args:

  • self (Self)
  • correction (Bool)

Returns:

Float32

Raises:

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

Args:

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

Returns:

Float32

Raises:

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

Args:

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

Returns:

Self

Raises:

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

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

Args:

  • self (Self)
  • _mean (Float32)

Returns:

Float32

Raises:

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

Args:

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

Returns:

Self

Raises:

abs

def abs(self) -> Self

Args:

  • self (Self)

Returns:

Self

log

def log(self) -> Self

Args:

  • self (Self)

Returns:

Self

sqrt

def sqrt(self) -> Self

Args:

  • self (Self)

Returns:

Self

exp

def exp(self) -> Self

Args:

  • self (Self)

Returns:

Self

argmin

def argmin(self) -> Int

Args:

  • self (Self)

Returns:

Int

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

Args:

  • self (Self)
  • axis (Int)

Returns:

List

argmax

def argmax(self) -> Int

Args:

  • self (Self)

Returns:

Int

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

Args:

  • self (Self)
  • axis (Int)

Returns:

List

argmax_f

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

Args:

  • self (Self)
  • axis (Int)

Returns:

Self

argsort

def argsort[ascending: Bool = True](self) -> List[Scalar[DType.int]]

Parameters:

  • ascending (Bool)

Args:

  • self (Self)

Returns:

List

Raises:

argsort_inplace

def argsort_inplace[ascending: Bool = True](mut self) -> List[Scalar[DType.int]]

Parameters:

  • ascending (Bool)

Args:

  • self (Self)

Returns:

List

Raises:

min

def min(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

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

Args:

  • self (Self)
  • axis (Int)

Returns:

Self

Raises:

max

def max(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

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

Args:

  • self (Self)
  • axis (Int)

Returns:

Self

Raises:

reshape

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

Args:

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

Returns:

Self

lu_factor

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

Args:

  • A (Self)
  • piv (UnsafePointer)
  • N (Int)

Raises:

lu_solve

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

Args:

  • A (Self)
  • piv (UnsafePointer)
  • b (Self)
  • x (Self)
  • N (Int)
  • Mi (Int)

Raises:

solve

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

Args:

  • A (Self)
  • b (Self)

Returns:

Self

Raises:

inv

def inv(self) -> Self

Args:

  • self (Self)

Returns:

Self

Raises:

eye

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

Args:

  • n (Int)
  • order (String)

Returns:

Self

norm

def norm(self) -> Float32

Args:

  • self (Self)

Returns:

Float32

Raises:

outer

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

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

Raises:

concatenate

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

Args:

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

Returns:

Self

Raises:

bincount

def bincount(self) -> List[Int]

Args:

  • self (Self)

Returns:

List

Raises:

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

Args:

  • self (Self)
  • weights (Self)

Returns:

List

Raises:

unique

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

Args:

  • self (Self)

Returns:

List

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

Args:

  • self (Self)
  • weights (Self)

Returns:

List

is_uniquef

def is_uniquef(self) -> Int

Args:

  • self (Self)

Returns:

Int

zeros

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

Args:

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

Returns:

Self

ones

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

Args:

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

Returns:

Self

full

@staticmethod
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

def fill_zero(self)

Args:

  • self (Self)

fill

def fill(self, val: Float32)

Args:

  • self (Self)
  • val (Float32)

random

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

Args:

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

Returns:

Self

rand_choice

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

Args:

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

Returns:

List

Raises:

linspace

@staticmethod
def linspace(start: Float32, stop: Float32, num: Int, order: String = "c") -> Self

Args:

  • start (Float32)
  • stop (Float32)
  • num (Int)
  • order (String)

Returns:

Self

Raises:

from_numpy

@staticmethod
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

def to_numpy(self) -> PythonObject

Converts the matrix to a numpy array.

Args:

  • self (Self)

Returns:

PythonObject: The numpy array.

Raises:

cast_ptr

def cast_ptr[des: DType](self) -> UnsafePointer[Scalar[des], MutExternalOrigin]

Parameters:

  • des (DType)

Args:

  • self (Self)

Returns:

UnsafePointer

write_to

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

Parameters:

  • W (Writer)

Args:

  • self (Self)
  • writer (W)

__str__

def __str__(self) -> String

Args:

  • self (Self)

Returns:

String