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[_current_target()]() else (2 * simd_width_of[DType.float32]())__del__is_trivial = False__moveinit__is_trivial = False__copyinit__is_trivial = False
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, Stringable, Writable
Methods
__init__
fn __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
fn __init__(out self, height: Int, width: Int, data: UnsafePointer[Float32, MutAnyOrigin] = UnsafePointer[True, Float32, MutAnyOrigin, AddressSpace.GENERIC](), order: String = "c")
Args:
- height (
Int) - width (
Int) - data (
UnsafePointer) - order (
String) - self (
Self)
Returns:
Self
fn __init__(out self, def_input: List[List[Float32]])
Args:
- def_input (
List) - self (
Self)
Returns:
Self
Raises:
__copyinit__
@staticmethod
fn __copyinit__(out self, other: Self)
Args:
- other (
Self) - self (
Self)
Returns:
Self
__moveinit__
@staticmethod
fn __moveinit__(out self, deinit existing: Self)
Args:
- existing (
Self) - self (
Self)
Returns:
Self
__del__
fn __del__(deinit self)
Args:
- self (
Self)
__getitem__
fn __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 __getitem__(self, row: Int) -> Self
The pattern to access a row: [row] .
Args:
- self (
Self) - row (
Int)
Returns:
Self
Raises:
fn __getitem__(self, row: Int, *, unsafe: Bool) -> Self
Args:
- self (
Self) - row (
Int) - unsafe (
Bool)
Returns:
Self
fn __getitem__(self, row: Int, offset: Bool, start_i: Int) -> Self
Args:
- self (
Self) - row (
Int) - offset (
Bool) - start_i (
Int)
Returns:
Self
Raises:
fn __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 __getitem__(self, row: String, column: Int, *, unsafe: Bool) -> Self
Args:
- self (
Self) - row (
String) - column (
Int) - unsafe (
Bool)
Returns:
Self
fn __getitem__(self, offset: Bool, start_i: Int, column: Int) -> Self
Args:
- self (
Self) - offset (
Bool) - start_i (
Int) - column (
Int)
Returns:
Self
Raises:
fn __getitem__(self, rows: Self) -> Self
Args:
- self (
Self) - rows (
Self)
Returns:
Self
Raises:
fn __getitem__(self, row: String, columns: Self) -> Self
Args:
- self (
Self) - row (
String) - columns (
Self)
Returns:
Self
Raises:
fn __getitem__(self, rows: List[Int]) -> Self
Args:
- self (
Self) - rows (
List)
Returns:
Self
Raises:
fn __getitem__(self, rows: List[Scalar[DType.index]]) -> Self
Args:
- self (
Self) - rows (
List)
Returns:
Self
Raises:
fn __getitem__(self, row: String, columns: List[Int]) -> Self
Args:
- self (
Self) - row (
String) - columns (
List)
Returns:
Self
Raises:
fn __getitem__(self, row: String, columns: List[Scalar[DType.index]]) -> Self
Args:
- self (
Self) - row (
String) - columns (
List)
Returns:
Self
Raises:
__setitem__
fn __setitem__(mut self, row: Int, column: Int, val: Float32)
Args:
- self (
Self) - row (
Int) - column (
Int) - val (
Float32)
Raises:
fn __setitem__(mut self, row: Int, val: Self)
Args:
- self (
Self) - row (
Int) - val (
Self)
Raises:
fn __setitem__(mut self, row: Int, val: Self, *, unsafe: Bool)
Args:
- self (
Self) - row (
Int) - val (
Self) - unsafe (
Bool)
fn __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 __setitem__(mut self, row: String, column: Int, val: Self)
Args:
- self (
Self) - row (
String) - column (
Int) - val (
Self)
Raises:
fn __setitem__(mut self, row: String, column: Int, val: Self, *, unsafe: Bool)
Args:
- self (
Self) - row (
String) - column (
Int) - val (
Self) - unsafe (
Bool)
fn __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 __neg__(self) -> Self
Args:
- self (
Self)
Returns:
Self
__lt__
fn __lt__(self, rhs: Float32) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Float32)
Returns:
List
__le__
fn __le__(self, rhs: Float32) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Float32)
Returns:
List
__eq__
fn __eq__(self, rhs: Float32) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Float32)
Returns:
List
fn __eq__(self, rhs: Self) -> Bool
Args:
- self (
Self) - rhs (
Self)
Returns:
Bool
__ne__
fn __ne__(self, rhs: Float32) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Float32)
Returns:
List
fn __ne__(self, rhs: Self) -> Bool
Args:
- self (
Self) - rhs (
Self)
Returns:
Bool
__gt__
fn __gt__(self, rhs: Float32) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Float32)
Returns:
List
__ge__
fn __ge__(self, rhs: Float32) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Float32)
Returns:
List
__add__
fn __add__(self, rhs: Self) -> Self
Args:
- self (
Self) - rhs (
Self)
Returns:
Self
Raises:
fn __add__(self, rhs: Float32) -> Self
Args:
- self (
Self) - rhs (
Float32)
Returns:
Self
__sub__
fn __sub__(self, rhs: Self) -> Self
Args:
- self (
Self) - rhs (
Self)
Returns:
Self
Raises:
fn __sub__(self, rhs: Float32) -> Self
Args:
- self (
Self) - rhs (
Float32)
Returns:
Self
__mul__
fn __mul__(self, rhs: Self) -> Self
Args:
- self (
Self) - rhs (
Self)
Returns:
Self
Raises:
fn __mul__(self, rhs: Float32) -> Self
Args:
- self (
Self) - rhs (
Float32)
Returns:
Self
__truediv__
fn __truediv__(self, rhs: Self) -> Self
Args:
- self (
Self) - rhs (
Self)
Returns:
Self
Raises:
fn __truediv__(self, rhs: Float32) -> Self
Args:
- self (
Self) - rhs (
Float32)
Returns:
Self
__pow__
fn __pow__(self, p: Int) -> Self
Args:
- self (
Self) - p (
Int)
Returns:
Self
__radd__
fn __radd__(self, lhs: Float32) -> Self
Args:
- self (
Self) - lhs (
Float32)
Returns:
Self
__rsub__
fn __rsub__(self, lhs: Float32) -> Self
Args:
- self (
Self) - lhs (
Float32)
Returns:
Self
__rmul__
fn __rmul__(self, lhs: Float32) -> Self
Args:
- self (
Self) - lhs (
Float32)
Returns:
Self
__rtruediv__
fn __rtruediv__(self, lhs: Float32) -> Self
Args:
- self (
Self) - lhs (
Float32)
Returns:
Self
__iadd__
fn __iadd__(mut self, rhs: Self)
Args:
- self (
Self) - rhs (
Self)
Raises:
fn __iadd__(mut self, rhs: Float32)
Args:
- self (
Self) - rhs (
Float32)
__isub__
fn __isub__(mut self, rhs: Self)
Args:
- self (
Self) - rhs (
Self)
Raises:
fn __isub__(mut self, rhs: Float32)
Args:
- self (
Self) - rhs (
Float32)
__imul__
fn __imul__(mut self, rhs: Self)
Args:
- self (
Self) - rhs (
Self)
Raises:
fn __imul__(mut self, rhs: Float32)
Args:
- self (
Self) - rhs (
Float32)
__itruediv__
fn __itruediv__(mut self, rhs: Self)
Args:
- self (
Self) - rhs (
Self)
Raises:
fn __itruediv__(mut self, rhs: Float32)
Args:
- self (
Self) - rhs (
Float32)
__ipow__
fn __ipow__(mut self, rhs: Int)
Args:
- self (
Self) - rhs (
Int)
load
fn 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
fn 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
fn load_columns(self, _range: Int) -> Self
Args:
- self (
Self) - _range (
Int)
Returns:
Self
Raises:
load_rows
fn load_rows(self, _range: Int) -> Self
Args:
- self (
Self) - _range (
Int)
Returns:
Self
Raises:
get_per_row
fn get_per_row(self, columns: Self) -> Self
Args:
- self (
Self) - columns (
Self)
Returns:
Self
Raises:
set_per_row
fn set_per_row(mut self, columns: Self, rhs: Self)
Args:
- self (
Self) - columns (
Self) - rhs (
Self)
Raises:
__len__
fn __len__(self) -> Int
Args:
- self (
Self)
Returns:
Int
ele_eq
fn ele_eq(self, rhs: Self) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Self)
Returns:
List
ele_ne
fn ele_ne(self, rhs: Self) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Self)
Returns:
List
ele_gt
fn ele_gt(self, rhs: Self) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Self)
Returns:
List
ele_ge
fn ele_ge(self, rhs: Self) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Self)
Returns:
List
ele_lt
fn ele_lt(self, rhs: Self) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Self)
Returns:
List
ele_le
fn ele_le(self, rhs: Self) -> List[Scalar[DType.bool]]
Args:
- self (
Self) - rhs (
Self)
Returns:
List
ele_mul
fn ele_mul(self, rhs: Self) -> Self
Args:
- self (
Self) - rhs (
Self)
Returns:
Self
Raises:
where
fn where(self, cmp: List[Scalar[DType.bool]], _true: Float32, _false: Float32) -> Self
Args:
- self (
Self) - cmp (
List) - _true (
Float32) - _false (
Float32)
Returns:
Self
fn where(self, cmp: List[Scalar[DType.bool]], _true: Self, _false: Float32) -> Self
Args:
- self (
Self) - cmp (
List) - _true (
Self) - _false (
Float32)
Returns:
Self
fn where(self, cmp: List[Scalar[DType.bool]], _true: Float32, _false: Self) -> Self
Args:
- self (
Self) - cmp (
List) - _true (
Float32) - _false (
Self)
Returns:
Self
fn 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
fn argwhere_l(self, cmp: List[Scalar[DType.bool]]) -> List[Int]
Args:
- self (
Self) - cmp (
List)
Returns:
List
C_transpose
fn C_transpose(self) -> Self
Args:
- self (
Self)
Returns:
Self
F_transpose
fn F_transpose(self) -> Self
Args:
- self (
Self)
Returns:
Self
T
fn T(self) -> Self
Args:
- self (
Self)
Returns:
Self
asorder
fn asorder(self, order: String) -> Self
Args:
- self (
Self) - order (
String)
Returns:
Self
cumsum
fn cumsum(self) -> Self
Args:
- self (
Self)
Returns:
Self
sum
fn sum(self) -> Float32
Args:
- self (
Self)
Returns:
Float32
Raises:
fn sum(self, axis: Int) -> Self
Args:
- self (
Self) - axis (
Int)
Returns:
Self
Raises:
mean
fn mean(self) -> Float32
Args:
- self (
Self)
Returns:
Float32
Raises:
fn mean(self, axis: Int) -> Self
Args:
- self (
Self) - axis (
Int)
Returns:
Self
Raises:
mean_weighted
fn mean_weighted(self, weights: Self, size: Float32) -> Float32
Args:
- self (
Self) - weights (
Self) - size (
Float32)
Returns:
Float32
Raises:
mean_slow
fn mean_slow(self) -> Float32
Args:
- self (
Self)
Returns:
Float32
Raises:
mean_slow0
fn mean_slow0(self) -> Self
Args:
- self (
Self)
Returns:
Self
Raises:
std
fn std(self, correction: Bool = False) -> Float32
Args:
- self (
Self) - correction (
Bool)
Returns:
Float32
Raises:
fn std(self, _mean: Float32, correction: Bool = False) -> Float32
Args:
- self (
Self) - _mean (
Float32) - correction (
Bool)
Returns:
Float32
Raises:
fn std(self, axis: Int, correction: Bool = False) -> Self
Args:
- self (
Self) - axis (
Int) - correction (
Bool)
Returns:
Self
Raises:
fn 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 std_slow(self, _mean: Float32) -> Float32
Args:
- self (
Self) - _mean (
Float32)
Returns:
Float32
Raises:
fn std_slow(self, axis: Int, _mean: Self) -> Self
Args:
- self (
Self) - axis (
Int) - _mean (
Self)
Returns:
Self
Raises:
abs
fn abs(self) -> Self
Args:
- self (
Self)
Returns:
Self
log
fn log(self) -> Self
Args:
- self (
Self)
Returns:
Self
sqrt
fn sqrt(self) -> Self
Args:
- self (
Self)
Returns:
Self
exp
fn exp(self) -> Self
Args:
- self (
Self)
Returns:
Self
argmin
fn argmin(self) -> Int
Args:
- self (
Self)
Returns:
Int
fn argmin(self, axis: Int) -> List[Int]
Args:
- self (
Self) - axis (
Int)
Returns:
List
argmax
fn argmax(self) -> Int
Args:
- self (
Self)
Returns:
Int
fn argmax(self, axis: Int) -> List[Int]
Args:
- self (
Self) - axis (
Int)
Returns:
List
argmax_f
fn argmax_f(self, axis: Int) -> Self
Args:
- self (
Self) - axis (
Int)
Returns:
Self
argsort
fn argsort[ascending: Bool = True](self) -> List[Scalar[DType.index]]
Parameters:
- ascending (
Bool)
Args:
- self (
Self)
Returns:
List
Raises:
argsort_inplace
fn argsort_inplace[ascending: Bool = True](mut self) -> List[Scalar[DType.index]]
Parameters:
- ascending (
Bool)
Args:
- self (
Self)
Returns:
List
Raises:
min
fn min(self) -> Float32
Args:
- self (
Self)
Returns:
Float32
Raises:
fn min(self, axis: Int) -> Self
Args:
- self (
Self) - axis (
Int)
Returns:
Self
Raises:
max
fn max(self) -> Float32
Args:
- self (
Self)
Returns:
Float32
Raises:
fn max(self, axis: Int) -> Self
Args:
- self (
Self) - axis (
Int)
Returns:
Self
Raises:
reshape
fn reshape(self, height: Int, width: Int) -> Self
Args:
- self (
Self) - height (
Int) - width (
Int)
Returns:
Self
lu_factor
@staticmethod
fn lu_factor(mut A, piv: UnsafePointer[Int, MutAnyOrigin], N: Int)
Args:
- A (
Self) - piv (
UnsafePointer) - N (
Int)
Raises:
lu_solve
@staticmethod
fn 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
fn solve(var A, b: Self) -> Self
Args:
- A (
Self) - b (
Self)
Returns:
Self
Raises:
inv
fn inv(self) -> Self
Args:
- self (
Self)
Returns:
Self
Raises:
eye
@staticmethod
fn eye(n: Int, order: String = "c") -> Self
Args:
- n (
Int) - order (
String)
Returns:
Self
norm
fn norm(self) -> Float32
Args:
- self (
Self)
Returns:
Float32
Raises:
outer
fn outer(self, rhs: Self) -> Self
Args:
- self (
Self) - rhs (
Self)
Returns:
Self
Raises:
concatenate
fn concatenate(self, rhs: Self, axis: Int) -> Self
Args:
- self (
Self) - rhs (
Self) - axis (
Int)
Returns:
Self
Raises:
bincount
fn bincount(self) -> List[Int]
Args:
- self (
Self)
Returns:
List
Raises:
fn bincount(self, weights: Self) -> List[Int]
Args:
- self (
Self) - weights (
Self)
Returns:
List
Raises:
unique
fn unique(self) -> List[List[Int]]
Args:
- self (
Self)
Returns:
List
fn unique(self, weights: Self) -> List[List[Int]]
Args:
- self (
Self) - weights (
Self)
Returns:
List
is_uniquef
fn is_uniquef(self) -> Int
Args:
- self (
Self)
Returns:
Int
zeros
@staticmethod
fn zeros(height: Int, width: Int, order: String = "c") -> Self
Args:
- height (
Int) - width (
Int) - order (
String)
Returns:
Self
ones
@staticmethod
fn ones(height: Int, width: Int, order: String = "c") -> Self
Args:
- height (
Int) - width (
Int) - order (
String)
Returns:
Self
full
@staticmethod
fn 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 fill_zero(self)
Args:
- self (
Self)
fill
fn fill(self, val: Float32)
Args:
- self (
Self) - val (
Float32)
random
@staticmethod
fn random(height: Int, width: Int, order: String = "c") -> Self
Args:
- height (
Int) - width (
Int) - order (
String)
Returns:
Self
rand_choice
@staticmethod
fn rand_choice(arang: Int, size: Int, replace: Bool = True, seed: Bool = True) -> List[Scalar[DType.index]]
Args:
- arang (
Int) - size (
Int) - replace (
Bool) - seed (
Bool)
Returns:
List
Raises:
linspace
@staticmethod
fn 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
fn 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 to_numpy(self) -> PythonObject
Converts the matrix to a numpy array.
Args:
- self (
Self)
Returns:
PythonObject: The numpy array.
Raises:
cast_ptr
fn cast_ptr[des: DType](self) -> UnsafePointer[Scalar[des], MutExternalOrigin]
Parameters:
- des (
DType)
Args:
- self (
Self)
Returns:
UnsafePointer
write_to
fn write_to[W: Writer](self, mut writer: W)
Parameters:
- W (
Writer)
Args:
- self (
Self) - writer (
W)
__str__
fn __str__(self) -> String
Args:
- self (
Self)
Returns:
String