diff --git a/numojo/__init__.mojo b/numojo/__init__.mojo index 9488b874..bf3aa64e 100644 --- a/numojo/__init__.mojo +++ b/numojo/__init__.mojo @@ -3,7 +3,7 @@ NuMojo is a library for numerical computing in Mojo 🔥 similar to NumPy, SciPy in Python. """ -alias __version__: String = "V0.8.0" +comptime __version__: String = "V0.8.0" # ===----------------------------------------------------------------------=== # # Import core types @@ -76,9 +76,9 @@ from numojo.core.error import ( # Objects from numojo.routines.constants import Constants -alias pi = numojo.routines.constants.Constants.pi -alias e = numojo.routines.constants.Constants.e -alias c = numojo.routines.constants.Constants.c +comptime pi = numojo.routines.constants.Constants.pi +comptime e = numojo.routines.constants.Constants.e +comptime c = numojo.routines.constants.Constants.c # Functions # TODO Make explicit imports of each individual function in future @@ -235,5 +235,5 @@ from numojo.routines.searching import argmax, argmin # ===----------------------------------------------------------------------=== # # Alias for users # For ease of use, the name of the types may not follow the Mojo convention, -# e.g., lower case can also be used for alias of structs. +# e.g., lower case can also be used for comptime of structs. # ===----------------------------------------------------------------------=== # diff --git a/numojo/core/__init__.mojo b/numojo/core/__init__.mojo index b8cf4ec5..c95fa5ac 100644 --- a/numojo/core/__init__.mojo +++ b/numojo/core/__init__.mojo @@ -65,6 +65,6 @@ from .error import ( ArithmeticError, ) -alias idx = Item -alias shape = NDArrayShape -alias Shape = NDArrayShape +comptime idx = Item +comptime shape = NDArrayShape +comptime Shape = NDArrayShape diff --git a/numojo/core/complex/complex_dtype.mojo b/numojo/core/complex/complex_dtype.mojo index 38c88c5a..00589a33 100644 --- a/numojo/core/complex/complex_dtype.mojo +++ b/numojo/core/complex/complex_dtype.mojo @@ -15,52 +15,52 @@ from sys import CompilationTarget from sys.info import bit_width_of, size_of from sys.intrinsics import _type_is_eq -alias _mIsSigned = UInt8(1) -alias _mIsInteger = UInt8(1 << 7) -alias _mIsNotInteger = UInt8(~(1 << 7)) -alias _mIsFloat = UInt8(1 << 6) +comptime _mIsSigned = UInt8(1) +comptime _mIsInteger = UInt8(1 << 7) +comptime _mIsNotInteger = UInt8(~(1 << 7)) +comptime _mIsFloat = UInt8(1 << 6) # rust like aliases for complex data types. -alias ci8 = ComplexDType.int8 -"""Data type alias for ComplexDType.int8""" -alias ci16 = ComplexDType.int16 -"""Data type alias for ComplexDType.int16""" -alias ci32 = ComplexDType.int32 -"""Data type alias for ComplexDType.int32""" -alias ci64 = ComplexDType.int64 -"""Data type alias for ComplexDType.int64""" -alias ci128 = ComplexDType.int128 -"""Data type alias for ComplexDType.int128""" -alias ci256 = ComplexDType.int256 -"""Data type alias for ComplexDType.int256""" -alias cint = ComplexDType.int -"""Data type alias for ComplexDType.int""" -alias cu8 = ComplexDType.uint8 -"""Data type alias for ComplexDType.uint8""" -alias cu16 = ComplexDType.uint16 -"""Data type alias for ComplexDType.uint16""" -alias cu32 = ComplexDType.uint32 -"""Data type alias for ComplexDType.uint32""" -alias cu64 = ComplexDType.uint64 -"""Data type alias for ComplexDType.uint64""" -alias cu128 = ComplexDType.uint128 -"""Data type alias for ComplexDType.uint128""" -alias cu256 = ComplexDType.uint256 -"""Data type alias for ComplexDType.uint256""" -alias cuint = ComplexDType.uint -"""Data type alias for ComplexDType.uint""" -alias cbf16 = ComplexDType.bfloat16 -"""Data type alias for ComplexDType.bfloat16""" -alias cf16 = ComplexDType.float16 -"""Data type alias for ComplexDType.float16""" -alias cf32 = ComplexDType.float32 -"""Data type alias for ComplexDType.float32""" -alias cf64 = ComplexDType.float64 -"""Data type alias for ComplexDType.float64""" -alias cboolean = ComplexDType.bool -"""Data type alias for ComplexDType.bool""" -alias cinvalid = ComplexDType.invalid -"""Data type alias for ComplexDType.invalid""" +comptime ci8 = ComplexDType.int8 +"""Data type comptime for ComplexDType.int8""" +comptime ci16 = ComplexDType.int16 +"""Data type comptime for ComplexDType.int16""" +comptime ci32 = ComplexDType.int32 +"""Data type comptime for ComplexDType.int32""" +comptime ci64 = ComplexDType.int64 +"""Data type comptime for ComplexDType.int64""" +comptime ci128 = ComplexDType.int128 +"""Data type comptime for ComplexDType.int128""" +comptime ci256 = ComplexDType.int256 +"""Data type comptime for ComplexDType.int256""" +comptime cint = ComplexDType.int +"""Data type comptime for ComplexDType.int""" +comptime cu8 = ComplexDType.uint8 +"""Data type comptime for ComplexDType.uint8""" +comptime cu16 = ComplexDType.uint16 +"""Data type comptime for ComplexDType.uint16""" +comptime cu32 = ComplexDType.uint32 +"""Data type comptime for ComplexDType.uint32""" +comptime cu64 = ComplexDType.uint64 +"""Data type comptime for ComplexDType.uint64""" +comptime cu128 = ComplexDType.uint128 +"""Data type comptime for ComplexDType.uint128""" +comptime cu256 = ComplexDType.uint256 +"""Data type comptime for ComplexDType.uint256""" +comptime cuint = ComplexDType.uint +"""Data type comptime for ComplexDType.uint""" +comptime cbf16 = ComplexDType.bfloat16 +"""Data type comptime for ComplexDType.bfloat16""" +comptime cf16 = ComplexDType.float16 +"""Data type comptime for ComplexDType.float16""" +comptime cf32 = ComplexDType.float32 +"""Data type comptime for ComplexDType.float32""" +comptime cf64 = ComplexDType.float64 +"""Data type comptime for ComplexDType.float64""" +comptime cboolean = ComplexDType.bool +"""Data type comptime for ComplexDType.bool""" +comptime cinvalid = ComplexDType.invalid +"""Data type comptime for ComplexDType.invalid""" # ===----------------------------------------------------------------------=== # # Implements the Complex Datatype. @@ -102,89 +102,89 @@ struct ComplexDType( # Aliases # ===-------------------------------------------------------------------===# # Refer to DType documentation for details on each data type. - alias _mlir_type = __mlir_type.`!kgen.dtype` - alias invalid = ComplexDType( + comptime _mlir_type = __mlir_type.`!kgen.dtype` + comptime invalid = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias bool = ComplexDType( + comptime bool = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias int = ComplexDType( + comptime int = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias uint = ComplexDType( + comptime uint = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias _uint1 = ComplexDType( + comptime _uint1 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias _uint2 = ComplexDType( + comptime _uint2 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias _uint4 = ComplexDType( + comptime _uint4 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias uint8 = ComplexDType( + comptime uint8 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias int8 = ComplexDType( + comptime int8 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias uint16 = ComplexDType( + comptime uint16 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias int16 = ComplexDType( + comptime int16 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias uint32 = ComplexDType( + comptime uint32 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias int32 = ComplexDType( + comptime int32 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias uint64 = ComplexDType( + comptime uint64 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias int64 = ComplexDType( + comptime int64 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias uint128 = ComplexDType( + comptime uint128 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias int128 = ComplexDType( + comptime int128 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias uint256 = ComplexDType( + comptime uint256 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias int256 = ComplexDType( + comptime int256 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias float8_e3m4 = ComplexDType( + comptime float8_e3m4 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias float8_e4m3fn = ComplexDType( + comptime float8_e4m3fn = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias float8_e4m3fnuz = ComplexDType( + comptime float8_e4m3fnuz = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias float8_e5m2 = ComplexDType( + comptime float8_e5m2 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias float8_e5m2fnuz = ComplexDType( + comptime float8_e5m2fnuz = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias bfloat16 = ComplexDType( + comptime bfloat16 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias float16 = ComplexDType( + comptime float16 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias float32 = ComplexDType( + comptime float32 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) - alias float64 = ComplexDType( + comptime float64 = ComplexDType( mlir_value=__mlir_attr.`#kgen.dtype.constant : !kgen.dtype` ) diff --git a/numojo/core/complex/complex_ndarray.mojo b/numojo/core/complex/complex_ndarray.mojo index 5d509303..cf94ac35 100644 --- a/numojo/core/complex/complex_ndarray.mojo +++ b/numojo/core/complex/complex_ndarray.mojo @@ -115,7 +115,7 @@ struct ComplexNDArray[cdtype: ComplexDType = ComplexDType.float64]( # Aliases # ===----------------------------------------------------------------------===# - alias dtype: DType = cdtype._dtype # corresponding real data type + comptime dtype: DType = cdtype._dtype # corresponding real data type # ===----------------------------------------------------------------------===# # FIELDS @@ -4066,7 +4066,7 @@ struct _ComplexNDArrayIter[ forward: The iteration direction. `False` is backwards. """ # The equivalent DType of the ComplexDType - alias dtype: DType = cdtype._dtype + comptime dtype: DType = cdtype._dtype # FIELDS var index: Int diff --git a/numojo/core/complex/complex_simd.mojo b/numojo/core/complex/complex_simd.mojo index 9c23f775..75b5d340 100644 --- a/numojo/core/complex/complex_simd.mojo +++ b/numojo/core/complex/complex_simd.mojo @@ -20,10 +20,10 @@ from math import sqrt from numojo.core.complex.complex_dtype import ComplexDType -# ComplexScalar alias is for internal purposes -alias ComplexScalar[cdtype: ComplexDType] = ComplexSIMD[cdtype, width=1] -# CScalar is short alias for ComplexScalar for user convenience -alias CScalar[cdtype: ComplexDType] = ComplexSIMD[cdtype, width=1] +# ComplexScalar comptime is for internal purposes +comptime ComplexScalar[cdtype: ComplexDType] = ComplexSIMD[cdtype, width=1] +# CScalar is short comptime for ComplexScalar for user convenience +comptime CScalar[cdtype: ComplexDType] = ComplexSIMD[cdtype, width=1] @register_passable("trivial") @@ -55,7 +55,7 @@ struct ComplexSIMD[cdtype: ComplexDType, width: Int = 1]( """ # FIELDS - alias dtype: DType = cdtype._dtype # the corresponding DType + comptime dtype: DType = cdtype._dtype # the corresponding DType # The underlying data real and imaginary parts of the complex number. var re: SIMD[Self.dtype, width] var im: SIMD[Self.dtype, width] diff --git a/numojo/core/data_container.mojo b/numojo/core/data_container.mojo index b27aee00..3eda4141 100644 --- a/numojo/core/data_container.mojo +++ b/numojo/core/data_container.mojo @@ -3,14 +3,14 @@ # # TODO: fields in traits are not supported yet by Mojo # Currently use `get_ptr()` to get pointer, in future, use `ptr` directly. -# var ptr: LegacyUnsafePointer[Scalar[dtype]] +# var ptr: LegacyUnsafePointer[Scalar[Self.dtype]] # ===----------------------------------------------------------------------=== from memory import LegacyUnsafePointer struct DataContainer[dtype: DType](): - var ptr: LegacyUnsafePointer[Scalar[dtype]] + var ptr: LegacyUnsafePointer[Scalar[Self.dtype]] fn __init__(out self, size: Int): """ @@ -21,9 +21,9 @@ struct DataContainer[dtype: DType](): `ndarray.flags['OWN_DATA']` should be set as True. The memory should be freed by `__del__`. """ - self.ptr = LegacyUnsafePointer[Scalar[dtype]]().alloc(size) + self.ptr = LegacyUnsafePointer[Scalar[Self.dtype]]().alloc(size) - fn __init__(out self, ptr: LegacyUnsafePointer[Scalar[dtype]]): + fn __init__(out self, ptr: LegacyUnsafePointer[Scalar[Self.dtype]]): """ Do not use this if you know what it means. If the pointer is associated with another array, it might cause @@ -38,5 +38,5 @@ struct DataContainer[dtype: DType](): fn __moveinit__(out self, deinit other: Self): self.ptr = other.ptr - fn get_ptr(self) -> LegacyUnsafePointer[Scalar[dtype]]: + fn get_ptr(self) -> LegacyUnsafePointer[Scalar[Self.dtype]]: return self.ptr diff --git a/numojo/core/datatypes.mojo b/numojo/core/datatypes.mojo index d4c106ea..f1773336 100644 --- a/numojo/core/datatypes.mojo +++ b/numojo/core/datatypes.mojo @@ -7,44 +7,44 @@ Datatypes Module - Implements datatypes aliases, conversions # ===----------------------------------------------------------------------=== # # Rust-like or numpy-like data type alias -alias i8 = DType.int8 -"""Data type alias for DType.int8""" -alias i16 = DType.int16 -"""Data type alias for DType.int16""" -alias i32 = DType.int32 -"""Data type alias for DType.int32""" -alias i64 = DType.int64 -"""Data type alias for DType.int64""" -alias i128 = DType.int128 -"""Data type alias for DType.int128""" -alias i256 = DType.int256 -"""Data type alias for DType.int256""" -alias int = DType.int -"""Data type alias for DType.int""" -alias uint = DType.int -"""Data type alias for DType.uint""" -alias u8 = DType.uint8 -"""Data type alias for DType.uint8""" -alias u16 = DType.uint16 -"""Data type alias for DType.uint16""" -alias u32 = DType.uint32 -"""Data type alias for DType.uint32""" -alias u64 = DType.uint64 -"""Data type alias for DType.uint64""" -alias u128 = DType.uint128 -"""Data type alias for DType.uint128""" -alias u256 = DType.uint256 -"""Data type alias for DType.uint256""" -alias f16 = DType.float16 -"""Data type alias for DType.float16""" -alias bf16 = DType.bfloat16 -"""Data type alias for DType.bfloat16""" -alias f32 = DType.float32 -"""Data type alias for DType.float32""" -alias f64 = DType.float64 -"""Data type alias for DType.float64""" -alias boolean = DType.bool -"""Data type alias for DType.bool""" +comptime i8 = DType.int8 +"""Data type comptime for DType.int8""" +comptime i16 = DType.int16 +"""Data type comptime for DType.int16""" +comptime i32 = DType.int32 +"""Data type comptime for DType.int32""" +comptime i64 = DType.int64 +"""Data type comptime for DType.int64""" +comptime i128 = DType.int128 +"""Data type comptime for DType.int128""" +comptime i256 = DType.int256 +"""Data type comptime for DType.int256""" +comptime int = DType.int +"""Data type comptime for DType.int""" +comptime uint = DType.int +"""Data type comptime for DType.uint""" +comptime u8 = DType.uint8 +"""Data type comptime for DType.uint8""" +comptime u16 = DType.uint16 +"""Data type comptime for DType.uint16""" +comptime u32 = DType.uint32 +"""Data type comptime for DType.uint32""" +comptime u64 = DType.uint64 +"""Data type comptime for DType.uint64""" +comptime u128 = DType.uint128 +"""Data type comptime for DType.uint128""" +comptime u256 = DType.uint256 +"""Data type comptime for DType.uint256""" +comptime f16 = DType.float16 +"""Data type comptime for DType.float16""" +comptime bf16 = DType.bfloat16 +"""Data type comptime for DType.bfloat16""" +comptime f32 = DType.float32 +"""Data type comptime for DType.float32""" +comptime f64 = DType.float64 +"""Data type comptime for DType.float64""" +comptime boolean = DType.bool +"""Data type comptime for DType.bool""" # ===----------------------------------------------------------------------=== # @@ -55,13 +55,13 @@ alias boolean = DType.bool # struct TypeCoercion: # """Handles type coercion using a promotion matrix approach.""" -# alias ranks: List[DType] = List[DType]( +# comptime ranks: List[DType] = List[DType]( # i8, u8, f16, i16, u16, f32, i32, u32, i64, u64, f64 # ) -# alias int_ranks: List[DType] = List[DType]( +# comptime int_ranks: List[DType] = List[DType]( # i8, u8, i16, u16, i32, u32, i64, u64 # ) -# alias float_ranks: List[DType] = List[DType](f16, f32, f64) +# comptime float_ranks: List[DType] = List[DType](f16, f32, f64) # @parameter # @staticmethod @@ -114,8 +114,8 @@ alias boolean = DType.bool # @staticmethod # fn coerce_mixed_ints[T1: DType, T2: DType]() -> DType: # """Coerces a signed and unsigned integer type.""" -# alias signed = T1 if T1.is_signed() else T2 -# alias unsigned = T2 if T1.is_signed() else T1 +# comptime signed = T1 if T1.is_signed() else T2 +# comptime unsigned = T2 if T1.is_signed() else T1 # # If unsigned fits in signed, use signed # if unsigned.sizeof() < signed.sizeof(): @@ -220,13 +220,13 @@ fn _concise_dtype_str(dtype: DType) -> String: return "Unknown" -# alias ranks: List[DType] = List[DType]( +# comptime ranks: List[DType] = List[DType]( # i8, u8, f16, i16, u16, f32, i32, u32, i64, u64, f64 # ) -# alias int_ranks: List[DType] = List[DType]( +# comptime int_ranks: List[DType] = List[DType]( # i8, u8, i16, u16, i32, u32, i64, u64 # ) -# alias float_ranks: List[DType] = List[DType](f16, f32, f64) +# comptime float_ranks: List[DType] = List[DType](f16, f32, f64) # @parameter # fn get_type_rank[dtype: DType]() -> Int: @@ -272,8 +272,8 @@ fn _concise_dtype_str(dtype: DType) -> String: # @parameter # fn coerce_mixed_ints[T1: DType, T2: DType]() -> DType: # """Coerces a signed and unsigned integer type.""" -# alias signed = T1 if T1.is_signed() else T2 -# alias unsigned = T2 if T1.is_signed() else T1 +# comptime signed = T1 if T1.is_signed() else T2 +# comptime unsigned = T2 if T1.is_signed() else T1 # # If unsigned fits in signed, use signed # if unsigned.sizeof() < signed.sizeof(): diff --git a/numojo/core/error.mojo b/numojo/core/error.mojo index 7af2f834..db579972 100644 --- a/numojo/core/error.mojo +++ b/numojo/core/error.mojo @@ -76,9 +76,9 @@ struct NumojoError[ # Error Category Constants # ===----------------------------------------------------------------------===# # common error categories, might expand in future -alias IndexError = NumojoError[category="IndexError"] -alias ShapeError = NumojoError[category="ShapeError"] -alias BroadcastError = NumojoError[category="BroadcastError"] -alias MemoryError = NumojoError[category="MemoryError"] -alias ValueError = NumojoError[category="ValueError"] -alias ArithmeticError = NumojoError[category="ArithmeticError"] +comptime IndexError = NumojoError[category="IndexError"] +comptime ShapeError = NumojoError[category="ShapeError"] +comptime BroadcastError = NumojoError[category="BroadcastError"] +comptime MemoryError = NumojoError[category="MemoryError"] +comptime ValueError = NumojoError[category="ValueError"] +comptime ArithmeticError = NumojoError[category="ArithmeticError"] diff --git a/numojo/core/item.mojo b/numojo/core/item.mojo index 57a33d42..8966b0e3 100644 --- a/numojo/core/item.mojo +++ b/numojo/core/item.mojo @@ -18,8 +18,8 @@ from numojo.core.traits.indexer_collection_element import ( IndexerCollectionElement, ) -# simple alias for users. Use `Item` internally. -alias item = Item +# simple comptime for users. Use `Item` internally. +comptime item = Item @register_passable @@ -31,7 +31,7 @@ struct Item( """ # Aliases - alias _type: DType = DType.int + comptime _type: DType = DType.int # Fields var _buf: UnsafePointer[Scalar[Self._type]] diff --git a/numojo/core/matrix.mojo b/numojo/core/matrix.mojo index 72bbcdc1..d1942e3f 100644 --- a/numojo/core/matrix.mojo +++ b/numojo/core/matrix.mojo @@ -92,13 +92,13 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( - [x] `Matrix.variance` and `mat.statistics.variance` (`var` is primitive) """ - alias width: Int = simd_width_of[dtype]() # + comptime width: Int = simd_width_of[Self.dtype]() # """Vector size of the data type.""" - var _buf: DataContainer[dtype] + var _buf: DataContainer[Self.dtype] """Data buffer of the items in the Matrix.""" - var buf_type: BufType + var buf_type: Self.BufType """View information of the Matrix.""" var shape: Tuple[Int, Int] @@ -132,7 +132,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( (Fortran-style) layout. Defaults to "C". """ constrained[ - BufType.is_own_data(), + Self.BufType.is_own_data(), "Buffer type must be OwnData to create matrix that owns data.", ]() @@ -142,8 +142,8 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( else: self.strides = (1, shape[0]) self.size = shape[0] * shape[1] - self._buf = DataContainer[dtype](size=self.size) - self.buf_type = BufType() + self._buf = DataContainer[Self.dtype](size=self.size) + self.buf_type = Self.BufType() self.flags = Flags( self.shape, self.strides, owndata=True, writeable=True ) @@ -158,7 +158,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( Construct a matrix from matrix. """ constrained[ - BufType.is_own_data(), + Self.BufType.is_own_data(), "Buffer type must be OwnData to create matrix that owns data.", ]() self = data^ @@ -166,13 +166,13 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( @always_inline("nodebug") fn __init__( out self, - data: NDArray[dtype], + data: NDArray[Self.dtype], ) raises: """ Construct a matrix from array. """ constrained[ - BufType.is_own_data(), + Self.BufType.is_own_data(), "Buffer type must be OwnData to create matrix that owns data.", ]() if data.ndim == 1: @@ -186,8 +186,8 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( else: raise Error(String("Shape too large to be a matrix.")) - self._buf = DataContainer[dtype](self.size) - self.buf_type = BufType() + self._buf = DataContainer[Self.dtype](self.size) + self.buf_type = Self.BufType() self.flags = Flags( self.shape, self.strides, owndata=True, writeable=True ) @@ -211,7 +211,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( shape: Tuple[Int, Int], strides: Tuple[Int, Int], offset: Int, - ptr: UnsafePointer[Scalar[dtype]], + ptr: UnsafePointer[Scalar[Self.dtype]], ): """ Initialize Matrix that does not own the data. @@ -224,14 +224,14 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( ptr: Pointer to the data buffer of the original array. """ constrained[ - BufType.is_ref_data(), + Self.BufType.is_ref_data(), "Buffer type must be RefData to create matrix view.", ]() self.shape = shape self.strides = strides self.size = shape[0] * shape[1] self._buf = DataContainer(ptr=ptr.offset(offset)) - self.buf_type = BufType() + self.buf_type = Self.BufType() self.flags = Flags( self.shape, self.strides, owndata=False, writeable=False ) @@ -244,9 +244,9 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( self.shape = (other.shape[0], other.shape[1]) self.strides = (other.strides[0], other.strides[1]) self.size = other.size - self._buf = DataContainer[dtype](other.size) + self._buf = DataContainer[Self.dtype](other.size) memcpy(dest=self._buf.ptr, src=other._buf.ptr, count=other.size) - self.buf_type = BufType() + self.buf_type = Self.BufType() self.flags = Flags( other.shape, other.strides, owndata=True, writeable=True ) @@ -270,11 +270,11 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( if owndata and self.buf_type.is_own_data(): self._buf.ptr.free() - fn create_copy(self) raises -> Matrix[dtype, OwnData]: + fn create_copy(self) raises -> Matrix[Self.dtype, OwnData]: """ Create a copy of the matrix with OwnData buffer type. """ - var result = Matrix[dtype, OwnData]( + var result = Matrix[Self.dtype, OwnData]( shape=self.shape, order=self.order() ) if self.flags.C_CONTIGUOUS: @@ -299,7 +299,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( idx_norm = dim + idx_norm return idx_norm - fn __getitem__(self, x: Int, y: Int) raises -> Scalar[dtype]: + fn __getitem__(self, x: Int, y: Int) raises -> Scalar[Self.dtype]: """ Return the scalar at the index. @@ -330,7 +330,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( # TODO: temporarily renaming all view returning functions to be `get` or `set` due to a Mojo bug with overloading `__getitem__` and `__setitem__` with different argument types. Created an issue in Mojo GitHub fn get( ref self, x: Int - ) raises -> Matrix[dtype, RefData[ImmutOrigin.cast_from[origin_of(self)]]]: + ) raises -> Matrix[Self.dtype, RefData[ImmutOrigin.cast_from[origin_of(self)]]]: """ Return the corresponding row at the index. @@ -338,7 +338,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( x: The row number. """ constrained[ - BufType.is_own_data(), + Self.BufType.is_own_data(), "Buffer type must be OwnData to get a reference row.", ]() if x >= self.shape[0] or x < -self.shape[0]: @@ -364,7 +364,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( return res^ # for creating a copy of the row. - fn __getitem__(self, var x: Int) raises -> Matrix[dtype, OwnData]: + fn __getitem__(self, var x: Int) raises -> Matrix[Self.dtype, OwnData]: """ Return the corresponding row at the index. @@ -384,7 +384,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( ) ) var x_norm = self.normalize(x, self.shape[0]) - var result = Matrix[dtype, OwnData]( + var result = Matrix[Self.dtype, OwnData]( shape=(1, self.shape[1]), order=self.order() ) if self.flags.C_CONTIGUOUS: @@ -398,12 +398,12 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( fn get( ref self, x: Slice, y: Slice - ) -> Matrix[dtype, RefData[ImmutOrigin.cast_from[origin_of(self)]]]: + ) -> Matrix[Self.dtype, RefData[ImmutOrigin.cast_from[origin_of(self)]]]: """ Get item from two slices. """ constrained[ - BufType.is_own_data(), + Self.BufType.is_own_data(), "Buffer type must be OwnData to get a reference row.", ]() start_x, end_x, step_x = x.indices(self.shape[0]) @@ -428,7 +428,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( return res^ # for creating a copy of the slice. - fn __getitem__(self, x: Slice, y: Slice) -> Matrix[dtype]: + fn __getitem__(self, x: Slice, y: Slice) -> Matrix[Self.dtype]: """ Get item from two slices. """ @@ -443,7 +443,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( var range_x = range(start_x, end_x, step_x) var range_y = range(start_y, end_y, step_y) - var B = Matrix[dtype]( + var B = Matrix[Self.dtype]( shape=(len(range_x), len(range_y)), order=self.order() ) var row = 0 @@ -458,13 +458,13 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( fn get( ref self, x: Slice, var y: Int - ) raises -> Matrix[dtype, RefData[ImmutOrigin.cast_from[origin_of(self)]]]: + ) raises -> Matrix[Self.dtype, RefData[ImmutOrigin.cast_from[origin_of(self)]]]: """ Get item from one slice and one int. """ # we could remove this constraint if we wanna allow users to create views from views. But that may complicate the origin tracking? constrained[ - BufType.is_own_data(), + Self.BufType.is_own_data(), "Buffer type must be OwnData to get a reference slice.", ]() if y >= self.shape[1] or y < -self.shape[1]: @@ -498,7 +498,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( return res^ # for creating a copy of the slice. - fn __getitem__(self, x: Slice, var y: Int) -> Matrix[dtype, OwnData]: + fn __getitem__(self, x: Slice, var y: Int) -> Matrix[Self.dtype, OwnData]: """ Get item from one slice and one int. """ @@ -510,7 +510,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( var step_x: Int start_x, end_x, step_x = x.indices(self.shape[0]) var range_x = range(start_x, end_x, step_x) - var res = Matrix[dtype, OwnData]( + var res = Matrix[Self.dtype, OwnData]( shape=( len(range_x), 1, @@ -525,12 +525,12 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( fn get( ref self, var x: Int, y: Slice - ) raises -> Matrix[dtype, RefData[ImmutOrigin.cast_from[origin_of(self)]]]: + ) raises -> Matrix[Self.dtype, RefData[ImmutOrigin.cast_from[origin_of(self)]]]: """ Get item from one int and one slice. """ constrained[ - BufType.is_own_data(), + Self.BufType.is_own_data(), "Buffer type must be OwnData to get a reference slice.", ]() if x >= self.shape[0] or x < -self.shape[0]: @@ -565,7 +565,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( return res^ # for creating a copy of the slice. - fn __getitem__(self, var x: Int, y: Slice) raises -> Matrix[dtype]: + fn __getitem__(self, var x: Int, y: Slice) raises -> Matrix[Self.dtype]: """ Get item from one int and one slice. """ @@ -582,7 +582,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( start_y, end_y, step_y = y.indices(self.shape[1]) var range_y = range(start_y, end_y, step_y) - var B = Matrix[dtype](shape=(1, len(range_y)), order=self.order()) + var B = Matrix[Self.dtype](shape=(1, len(range_y)), order=self.order()) var col = 0 for j in range_y: B._store(0, col, self._load(x, j)) @@ -590,18 +590,18 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( return B^ - fn __getitem__(self, indices: List[Int]) raises -> Matrix[dtype, OwnData]: + fn __getitem__(self, indices: List[Int]) raises -> Matrix[Self.dtype, OwnData]: """ Get item by a list of integers. """ var ncol = self.shape[1] var nrow = len(indices) - var res = Matrix.zeros[dtype](shape=(nrow, ncol)) + var res = Matrix.zeros[Self.dtype](shape=(nrow, ncol)) for i in range(nrow): res.__setitem__(i, self[indices[i]]) return res^ - fn load[width: Int = 1](self, idx: Int) raises -> SIMD[dtype, width]: + fn load[width: Int = 1](self, idx: Int) raises -> SIMD[Self.dtype, width]: """ Returns a SIMD element with width `width` at the given index. @@ -623,7 +623,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( var idx_norm = self.normalize(idx, self.size) return self._buf.ptr.load[width=width](idx_norm) - fn _load[width: Int = 1](self, x: Int, y: Int) -> SIMD[dtype, width]: + fn _load[width: Int = 1](self, x: Int, y: Int) -> SIMD[Self.dtype, width]: """ `__getitem__` with width. Unsafe: No boundary check! @@ -632,14 +632,14 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( x * self.strides[0] + y * self.strides[1] ) - fn _load[width: Int = 1](self, idx: Int) -> SIMD[dtype, width]: + fn _load[width: Int = 1](self, idx: Int) -> SIMD[Self.dtype, width]: """ `__getitem__` with width. Unsafe: No boundary check! """ return self._buf.ptr.load[width=width](idx) - fn __setitem__(self, x: Int, y: Int, value: Scalar[dtype]) raises: + fn __setitem__(self, x: Int, y: Int, value: Scalar[Self.dtype]) raises: """ Return the scalar at the index. @@ -666,7 +666,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( x_norm * self.strides[0] + y_norm * self.strides[1], value ) - fn __setitem__(self, var x: Int, value: Matrix[dtype, **_]) raises: + fn __setitem__(self, var x: Int, value: Matrix[Self.dtype, **_]) raises: """ Set the corresponding row at the index with the given matrix. @@ -717,7 +717,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( for j in range(self.shape[1]): self._store(x, j, value._load(0, j)) - fn set(self, var x: Int, value: Matrix[dtype, **_]) raises: + fn set(self, var x: Int, value: Matrix[Self.dtype, **_]) raises: """ Set the corresponding row at the index with the given matrix. @@ -768,7 +768,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( for j in range(self.shape[1]): self._store(x, j, value._load(0, j)) - fn __setitem__(self, x: Slice, y: Int, value: Matrix[dtype, **_]) raises: + fn __setitem__(self, x: Slice, y: Int, value: Matrix[Self.dtype, **_]) raises: """ Set item from one slice and one int. """ @@ -799,7 +799,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( self._store(i, y_norm, value._load(row, 0)) row += 1 - fn set(self, x: Slice, y: Int, value: Matrix[dtype, **_]) raises: + fn set(self, x: Slice, y: Int, value: Matrix[Self.dtype, **_]) raises: """ Set item from one slice and one int. """ @@ -830,7 +830,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( self._store(i, y_norm, value._load(row, 0)) row += 1 - fn __setitem__(self, x: Int, y: Slice, value: Matrix[dtype, **_]) raises: + fn __setitem__(self, x: Int, y: Slice, value: Matrix[Self.dtype, **_]) raises: """ Set item from one int and one slice. """ @@ -861,7 +861,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( self._store(x_norm, j, value._load(0, col)) col += 1 - fn set(self, x: Int, y: Slice, value: Matrix[dtype, **_]) raises: + fn set(self, x: Int, y: Slice, value: Matrix[Self.dtype, **_]) raises: """ Set item from one int and one slice. """ @@ -892,7 +892,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( self._store(x_norm, j, value._load(0, col)) col += 1 - fn __setitem__(self, x: Slice, y: Slice, value: Matrix[dtype, **_]) raises: + fn __setitem__(self, x: Slice, y: Slice, value: Matrix[Self.dtype, **_]) raises: """ Set item from two slices. """ @@ -925,7 +925,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( col += 1 row += 1 - fn set(self, x: Slice, y: Slice, value: Matrix[dtype, **_]) raises: + fn set(self, x: Slice, y: Slice, value: Matrix[Self.dtype, **_]) raises: """ Set item from two slices. """ @@ -958,14 +958,14 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( col += 1 row += 1 - fn _store[width: Int = 1](self, x: Int, y: Int, simd: SIMD[dtype, width]): + fn _store[width: Int = 1](self, x: Int, y: Int, simd: SIMD[Self.dtype, width]): """ `__setitem__` with width. Unsafe: No boundary check! """ self._buf.ptr.store(x * self.strides[0] + y * self.strides[1], simd) - fn _store_idx[width: Int = 1](self, idx: Int, val: SIMD[dtype, width]): + fn _store_idx[width: Int = 1](self, idx: Int, val: SIMD[Self.dtype, width]): """ `__setitem__` with width. Unsafe: No boundary check! @@ -976,7 +976,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( # Other dunders and auxiliary methods # ===-------------------------------------------------------------------===# - fn __iter__(self) raises -> _MatrixIter[origin_of(self), dtype, BufType]: + fn __iter__(self) raises -> _MatrixIter[origin_of(self), dtype, Self.BufType]: """Iterate over elements of the Matrix, returning copied value. Example: @@ -991,7 +991,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( An iterator of Matrix elements. """ - return _MatrixIter[origin_of(self), dtype, BufType]( + return _MatrixIter[origin_of(self), dtype, Self.BufType]( matrix=self, length=self.shape[0], ) @@ -1004,7 +1004,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( fn __reversed__( self, - ) raises -> _MatrixIter[origin_of(self), dtype, BufType, forward=False]: + ) raises -> _MatrixIter[origin_of(self), dtype, Self.BufType, forward=False]: """Iterate backwards over elements of the Matrix, returning copied value. @@ -1012,7 +1012,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( A reversed iterator of Matrix elements. """ - return _MatrixIter[origin_of(self), dtype, BufType, forward=False]( + return _MatrixIter[origin_of(self), dtype, Self.BufType, forward=False]( matrix=self, length=self.shape[0], ) @@ -1089,26 +1089,26 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( # ===-------------------------------------------------------------------===# fn __add__( - read self, read other: Matrix[dtype, *_] - ) raises -> Matrix[dtype, OwnData]: + read self, read other: Matrix[Self.dtype, *_] + ) raises -> Matrix[Self.dtype, OwnData]: if (self.shape[0] == other.shape[0]) and ( self.shape[1] == other.shape[1] ): return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__add__ + Self.dtype, SIMD.__add__ ](self, other) elif (self.shape[0] < other.shape[0]) or ( self.shape[1] < other.shape[1] ): return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__add__ - ](broadcast_to[dtype](self, other.shape, self.order()), other) + Self.dtype, SIMD.__add__ + ](broadcast_to[Self.dtype](self, other.shape, self.order()), other) else: return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__add__ - ](self, broadcast_to[dtype](other, self.shape, self.order())) + Self.dtype, SIMD.__add__ + ](self, broadcast_to[Self.dtype](other, self.shape, self.order())) - fn __add__(self, other: Scalar[dtype]) raises -> Matrix[dtype, **_]: + fn __add__(self, other: Scalar[Self.dtype]) raises -> Matrix[Self.dtype, **_]: """Add matrix to scalar. ```mojo @@ -1117,9 +1117,9 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(A + 2) ``` """ - return self + broadcast_to[dtype](other, self.shape, self.order()) + return self + broadcast_to[Self.dtype](other, self.shape, self.order()) - fn __radd__(self, other: Scalar[dtype]) raises -> Matrix[dtype, **_]: + fn __radd__(self, other: Scalar[Self.dtype]) raises -> Matrix[Self.dtype, **_]: """ Right-add. @@ -1129,29 +1129,29 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(2 + A) ``` """ - return broadcast_to[dtype](other, self.shape, self.order()) + self + return broadcast_to[Self.dtype](other, self.shape, self.order()) + self fn __sub__( - read self, read other: Matrix[dtype, *_] - ) raises -> Matrix[dtype, **_]: + read self, read other: Matrix[Self.dtype, *_] + ) raises -> Matrix[Self.dtype, **_]: if (self.shape[0] == other.shape[0]) and ( self.shape[1] == other.shape[1] ): return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__sub__ + Self.dtype, SIMD.__sub__ ](self, other) elif (self.shape[0] < other.shape[0]) or ( self.shape[1] < other.shape[1] ): return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__sub__ + Self.dtype, SIMD.__sub__ ](broadcast_to(self.copy(), other.shape, self.order()), other) else: return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__sub__ + Self.dtype, SIMD.__sub__ ](self, broadcast_to(other.copy(), self.shape, self.order())) - fn __sub__(self, other: Scalar[dtype]) raises -> Matrix[dtype, **_]: + fn __sub__(self, other: Scalar[Self.dtype]) raises -> Matrix[Self.dtype, **_]: """Subtract matrix by scalar. ```mojo @@ -1160,9 +1160,9 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(A - 2) ``` """ - return self - broadcast_to[dtype](other, self.shape, self.order()) + return self - broadcast_to[Self.dtype](other, self.shape, self.order()) - fn __rsub__(self, other: Scalar[dtype]) raises -> Matrix[dtype, **_]: + fn __rsub__(self, other: Scalar[Self.dtype]) raises -> Matrix[Self.dtype, **_]: """ Right-sub. @@ -1172,27 +1172,27 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(2 - A) ``` """ - return broadcast_to[dtype](other, self.shape, self.order()) - self + return broadcast_to[Self.dtype](other, self.shape, self.order()) - self - fn __mul__(self, other: Matrix[dtype, **_]) raises -> Matrix[dtype, **_]: + fn __mul__(self, other: Matrix[Self.dtype, **_]) raises -> Matrix[Self.dtype, **_]: if (self.shape[0] == other.shape[0]) and ( self.shape[1] == other.shape[1] ): return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__mul__ + Self.dtype, SIMD.__mul__ ](self, other) elif (self.shape[0] < other.shape[0]) or ( self.shape[1] < other.shape[1] ): return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__mul__ + Self.dtype, SIMD.__mul__ ](broadcast_to(self.copy(), other.shape, self.order()), other) else: return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__mul__ + Self.dtype, SIMD.__mul__ ](self, broadcast_to(other.copy(), self.shape, self.order())) - fn __mul__(self, other: Scalar[dtype]) raises -> Matrix[dtype, **_]: + fn __mul__(self, other: Scalar[Self.dtype]) raises -> Matrix[Self.dtype, **_]: """Mutiply matrix by scalar. ```mojo @@ -1201,9 +1201,9 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(A * 2) ``` """ - return self * broadcast_to[dtype](other, self.shape, self.order()) + return self * broadcast_to[Self.dtype](other, self.shape, self.order()) - fn __rmul__(self, other: Scalar[dtype]) raises -> Matrix[dtype, **_]: + fn __rmul__(self, other: Scalar[Self.dtype]) raises -> Matrix[Self.dtype, **_]: """ Right-mul. @@ -1213,36 +1213,36 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(2 * A) ``` """ - return broadcast_to[dtype](other, self.shape, self.order()) * self + return broadcast_to[Self.dtype](other, self.shape, self.order()) * self fn __truediv__( - self, other: Matrix[dtype, **_] - ) raises -> Matrix[dtype, **_]: + self, other: Matrix[Self.dtype, **_] + ) raises -> Matrix[Self.dtype, **_]: if (self.shape[0] == other.shape[0]) and ( self.shape[1] == other.shape[1] ): return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__truediv__ + Self.dtype, SIMD.__truediv__ ](self, other) elif (self.shape[0] < other.shape[0]) or ( self.shape[1] < other.shape[1] ): return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__truediv__ + Self.dtype, SIMD.__truediv__ ](broadcast_to(self.copy(), other.shape, self.order()), other) else: return _arithmetic_func_matrix_matrix_to_matrix[ - dtype, SIMD.__truediv__ + Self.dtype, SIMD.__truediv__ ](self, broadcast_to(other.copy(), self.shape, self.order())) - fn __truediv__(self, other: Scalar[dtype]) raises -> Matrix[dtype, **_]: + fn __truediv__(self, other: Scalar[Self.dtype]) raises -> Matrix[Self.dtype, **_]: """Divide matrix by scalar.""" - return self / broadcast_to[dtype](other, self.shape, order=self.order()) + return self / broadcast_to[Self.dtype](other, self.shape, order=self.order()) # Shouldn't we do the operation inplace? - fn __pow__(self, rhs: Scalar[dtype]) raises -> Matrix[dtype, **_]: + fn __pow__(self, rhs: Scalar[Self.dtype]) raises -> Matrix[Self.dtype, **_]: """Power of items.""" - var result: Matrix[dtype, OwnData] = Matrix[dtype, OwnData]( + var result: Matrix[Self.dtype, OwnData] = Matrix[Self.dtype, OwnData]( shape=self.shape, order=self.order() ) for i in range(self.size): @@ -1250,26 +1250,26 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( return result^ fn __lt__( - self, other: Matrix[dtype, **_] + self, other: Matrix[Self.dtype, **_] ) raises -> Matrix[DType.bool, **_]: if (self.shape[0] == other.shape[0]) and ( self.shape[1] == other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.lt]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.lt]( self, other ) elif (self.shape[0] < other.shape[0]) or ( self.shape[1] < other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.lt]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.lt]( broadcast_to(self.copy(), other.shape, self.order()), other ) else: - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.lt]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.lt]( self, broadcast_to(other.copy(), self.shape, self.order()) ) - fn __lt__(self, other: Scalar[dtype]) raises -> Matrix[DType.bool, **_]: + fn __lt__(self, other: Scalar[Self.dtype]) raises -> Matrix[DType.bool, **_]: """Matrix less than scalar. ```mojo @@ -1278,29 +1278,29 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(A < 2) ``` """ - return self < broadcast_to[dtype](other, self.shape, self.order()) + return self < broadcast_to[Self.dtype](other, self.shape, self.order()) fn __le__( - self, other: Matrix[dtype, **_] + self, other: Matrix[Self.dtype, **_] ) raises -> Matrix[DType.bool, **_]: if (self.shape[0] == other.shape[0]) and ( self.shape[1] == other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.le]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.le]( self, other ) elif (self.shape[0] < other.shape[0]) or ( self.shape[1] < other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.le]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.le]( broadcast_to(self.copy(), other.shape, self.order()), other ) else: - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.le]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.le]( self, broadcast_to(other.copy(), self.shape, self.order()) ) - fn __le__(self, other: Scalar[dtype]) raises -> Matrix[DType.bool, **_]: + fn __le__(self, other: Scalar[Self.dtype]) raises -> Matrix[DType.bool, **_]: """Matrix less than and equal to scalar. ```mojo @@ -1309,29 +1309,29 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(A <= 2) ``` """ - return self <= broadcast_to[dtype](other, self.shape, self.order()) + return self <= broadcast_to[Self.dtype](other, self.shape, self.order()) fn __gt__( - self, other: Matrix[dtype, **_] + self, other: Matrix[Self.dtype, **_] ) raises -> Matrix[DType.bool, **_]: if (self.shape[0] == other.shape[0]) and ( self.shape[1] == other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.gt]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.gt]( self, other ) elif (self.shape[0] < other.shape[0]) or ( self.shape[1] < other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.gt]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.gt]( broadcast_to(self.copy(), other.shape, self.order()), other ) else: - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.gt]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.gt]( self, broadcast_to(other.copy(), self.shape, self.order()) ) - fn __gt__(self, other: Scalar[dtype]) raises -> Matrix[DType.bool, **_]: + fn __gt__(self, other: Scalar[Self.dtype]) raises -> Matrix[DType.bool, **_]: """Matrix greater than scalar. ```mojo @@ -1340,29 +1340,29 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(A > 2) ``` """ - return self > broadcast_to[dtype](other, self.shape, self.order()) + return self > broadcast_to[Self.dtype](other, self.shape, self.order()) fn __ge__( - self, other: Matrix[dtype, **_] + self, other: Matrix[Self.dtype, **_] ) raises -> Matrix[DType.bool, **_]: if (self.shape[0] == other.shape[0]) and ( self.shape[1] == other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.ge]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.ge]( self, other ) elif (self.shape[0] < other.shape[0]) or ( self.shape[1] < other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.ge]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.ge]( broadcast_to(self.copy(), other.shape, self.order()), other ) else: - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.ge]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.ge]( self, broadcast_to(other.copy(), self.shape, self.order()) ) - fn __ge__(self, other: Scalar[dtype]) raises -> Matrix[DType.bool, **_]: + fn __ge__(self, other: Scalar[Self.dtype]) raises -> Matrix[DType.bool, **_]: """Matrix greater than and equal to scalar. ```mojo @@ -1371,29 +1371,29 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(A >= 2) ``` """ - return self >= broadcast_to[dtype](other, self.shape, self.order()) + return self >= broadcast_to[Self.dtype](other, self.shape, self.order()) fn __eq__( - self, other: Matrix[dtype, **_] + self, other: Matrix[Self.dtype, **_] ) raises -> Matrix[DType.bool, **_]: if (self.shape[0] == other.shape[0]) and ( self.shape[1] == other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.eq]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.eq]( self, other ) elif (self.shape[0] < other.shape[0]) or ( self.shape[1] < other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.eq]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.eq]( broadcast_to(self.copy(), other.shape, self.order()), other ) else: - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.eq]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.eq]( self, broadcast_to(other.copy(), self.shape, self.order()) ) - fn __eq__(self, other: Scalar[dtype]) raises -> Matrix[DType.bool, **_]: + fn __eq__(self, other: Scalar[Self.dtype]) raises -> Matrix[DType.bool, **_]: """Matrix less than and equal to scalar. ```mojo @@ -1402,29 +1402,29 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(A == 2) ``` """ - return self == broadcast_to[dtype](other, self.shape, self.order()) + return self == broadcast_to[Self.dtype](other, self.shape, self.order()) fn __ne__( - self, other: Matrix[dtype, **_] + self, other: Matrix[Self.dtype, **_] ) raises -> Matrix[DType.bool, **_]: if (self.shape[0] == other.shape[0]) and ( self.shape[1] == other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.ne]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.ne]( self, other ) elif (self.shape[0] < other.shape[0]) or ( self.shape[1] < other.shape[1] ): - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.ne]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.ne]( broadcast_to(self.copy(), other.shape, self.order()), other ) else: - return _logic_func_matrix_matrix_to_matrix[dtype, SIMD.ne]( + return _logic_func_matrix_matrix_to_matrix[Self.dtype, SIMD.ne]( self, broadcast_to(other.copy(), self.shape, self.order()) ) - fn __ne__(self, other: Scalar[dtype]) raises -> Matrix[DType.bool, **_]: + fn __ne__(self, other: Scalar[Self.dtype]) raises -> Matrix[DType.bool, **_]: """Matrix less than and equal to scalar. ```mojo @@ -1433,34 +1433,34 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( print(A != 2) ``` """ - return self != broadcast_to[dtype](other, self.shape, self.order()) + return self != broadcast_to[Self.dtype](other, self.shape, self.order()) - fn __matmul__(self, other: Matrix[dtype, **_]) raises -> Matrix[dtype, **_]: + fn __matmul__(self, other: Matrix[Self.dtype, **_]) raises -> Matrix[Self.dtype, **_]: return numojo.linalg.matmul(self, other) # ===-------------------------------------------------------------------===# # Core methods # ===-------------------------------------------------------------------===# - fn all(self) -> Scalar[dtype]: + fn all(self) -> Scalar[Self.dtype]: """ Test whether all array elements evaluate to True. """ return numojo.logic.all(self) - fn all(self, axis: Int) raises -> Matrix[dtype, OwnData]: + fn all(self, axis: Int) raises -> Matrix[Self.dtype, OwnData]: """ Test whether all array elements evaluate to True along axis. """ - return numojo.logic.all[dtype](self, axis=axis) + return numojo.logic.all[Self.dtype](self, axis=axis) - fn any(self) -> Scalar[dtype]: + fn any(self) -> Scalar[Self.dtype]: """ Test whether any array elements evaluate to True. """ return numojo.logic.any(self) - fn any(self, axis: Int) raises -> Matrix[dtype, OwnData]: + fn any(self, axis: Int) raises -> Matrix[Self.dtype, OwnData]: """ Test whether any array elements evaluate to True along axis. """ @@ -1513,7 +1513,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( res._buf.ptr[i] = self._buf.ptr[i].cast[asdtype]() return res^ - fn cumprod(self) raises -> Matrix[dtype, OwnData]: + fn cumprod(self) raises -> Matrix[Self.dtype, OwnData]: """ Cumprod of flattened matrix. @@ -1526,7 +1526,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( """ return numojo.math.cumprod(self.copy()) - fn cumprod(self, axis: Int) raises -> Matrix[dtype, OwnData]: + fn cumprod(self, axis: Int) raises -> Matrix[Self.dtype, OwnData]: """ Cumprod of Matrix along the axis. @@ -1543,13 +1543,13 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( """ return numojo.math.cumprod(self.copy(), axis=axis) - fn cumsum(self) raises -> Matrix[dtype, OwnData]: + fn cumsum(self) raises -> Matrix[Self.dtype, OwnData]: return numojo.math.cumsum(self.copy()) - fn cumsum(self, axis: Int) raises -> Matrix[dtype, OwnData]: + fn cumsum(self, axis: Int) raises -> Matrix[Self.dtype, OwnData]: return numojo.math.cumsum(self.copy(), axis=axis) - fn fill(self, fill_value: Scalar[dtype]): + fn fill(self, fill_value: Scalar[Self.dtype]): """ Fill the matrix with value. @@ -1559,17 +1559,17 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( self._buf.ptr[i] = fill_value # * Make it inplace? - fn flatten(self) -> Matrix[dtype, OwnData]: + fn flatten(self) -> Matrix[Self.dtype, OwnData]: """ Return a flattened copy of the matrix. """ - var res = Matrix[dtype, OwnData]( + var res = Matrix[Self.dtype, OwnData]( shape=(1, self.size), order=self.order() ) memcpy(dest=res._buf.ptr, src=self._buf.ptr, count=res.size) return res^ - fn inv(self) raises -> Matrix[dtype, OwnData]: + fn inv(self) raises -> Matrix[Self.dtype, OwnData]: """ Inverse of matrix. """ @@ -1584,13 +1584,13 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( order = "C" return order - fn max(self) raises -> Scalar[dtype]: + fn max(self) raises -> Scalar[Self.dtype]: """ Find max item. It is first flattened before sorting. """ return numojo.math.extrema.max(self) - fn max(self, axis: Int) raises -> Matrix[dtype, OwnData]: + fn max(self, axis: Int) raises -> Matrix[Self.dtype, OwnData]: """ Find max item along the given axis. """ @@ -1615,25 +1615,25 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( """ return numojo.statistics.mean[returned_dtype](self, axis=axis) - fn min(self) raises -> Scalar[dtype]: + fn min(self) raises -> Scalar[Self.dtype]: """ Find min item. It is first flattened before sorting. """ return numojo.math.extrema.min(self) - fn min(self, axis: Int) raises -> Matrix[dtype, OwnData]: + fn min(self, axis: Int) raises -> Matrix[Self.dtype, OwnData]: """ Find min item along the given axis. """ return numojo.math.extrema.min(self, axis=axis) - fn prod(self) -> Scalar[dtype]: + fn prod(self) -> Scalar[Self.dtype]: """ Product of all items in the Matrix. """ return numojo.math.prod(self) - fn prod(self, axis: Int) raises -> Matrix[dtype]: + fn prod(self, axis: Int) raises -> Matrix[Self.dtype]: """ Product of items in a Matrix along the axis. @@ -1650,7 +1650,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( """ return numojo.math.prod(self, axis=axis) - fn reshape(self, shape: Tuple[Int, Int]) raises -> Matrix[dtype]: + fn reshape(self, shape: Tuple[Int, Int]) raises -> Matrix[Self.dtype]: """ Change shape and size of matrix and return a new matrix. """ @@ -1660,7 +1660,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( "Cannot reshape matrix of size {} into shape ({}, {})." ).format(self.size, shape[0], shape[1]) ) - var res = Matrix[dtype](shape=shape, order="C") + var res = Matrix[Self.dtype](shape=shape, order="C") if self.flags.F_CONTIGUOUS: var temp = self.reorder_layout() memcpy(dest=res._buf.ptr, src=temp._buf.ptr, count=res.size) @@ -1674,7 +1674,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( Change shape and size of matrix in-place. """ if shape[0] * shape[1] > self.size: - var other = Matrix[dtype, Self.BufType](shape=shape) + var other = Matrix[Self.dtype, Self.BufType](shape=shape) if self.flags.C_CONTIGUOUS: memcpy(dest=other._buf.ptr, src=self._buf.ptr, count=self.size) for i in range(self.size, other.size): @@ -1701,7 +1701,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( else: self.strides[1] = shape[0] - fn round(self, decimals: Int) raises -> Matrix[dtype]: + fn round(self, decimals: Int) raises -> Matrix[Self.dtype]: return numojo.math.rounding.round(self, decimals=decimals) fn std[ @@ -1727,7 +1727,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( """ return numojo.statistics.std[returned_dtype](self, axis=axis, ddof=ddof) - fn sum(self) -> Scalar[dtype]: + fn sum(self) -> Scalar[Self.dtype]: """ Sum up all items in the Matrix. @@ -1740,7 +1740,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( """ return numojo.math.sum(self) - fn sum(self, axis: Int) raises -> Matrix[dtype, OwnData]: + fn sum(self, axis: Int) raises -> Matrix[Self.dtype, OwnData]: """ Sum up the items in a Matrix along the axis. @@ -1757,7 +1757,7 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( """ return numojo.math.sum(self, axis=axis) - fn trace(self) raises -> Scalar[dtype]: + fn trace(self) raises -> Scalar[Self.dtype]: """ Trace of matrix. """ @@ -1769,19 +1769,19 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( """ return issymmetric(self) - fn transpose(self) -> Matrix[dtype, OwnData]: + fn transpose(self) -> Matrix[Self.dtype, OwnData]: """ Transpose of matrix. """ return transpose(self) - fn reorder_layout(self) raises -> Matrix[dtype, Self.BufType]: + fn reorder_layout(self) raises -> Matrix[Self.dtype, Self.BufType]: """ Reorder_layout matrix. """ return reorder_layout(self) - fn T(self) -> Matrix[dtype, OwnData]: + fn T(self) -> Matrix[Self.dtype, OwnData]: return transpose(self) fn variance[ @@ -1813,13 +1813,13 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( # To other data types # ===-------------------------------------------------------------------===# - fn to_ndarray(self) raises -> NDArray[dtype]: + fn to_ndarray(self) raises -> NDArray[Self.dtype]: """Create `NDArray` from `Matrix`. It makes a copy of the buffer of the matrix. """ - var ndarray: NDArray[dtype] = NDArray[dtype]( + var ndarray: NDArray[Self.dtype] = NDArray[Self.dtype]( shape=List[Int](self.shape[0], self.shape[1]), order="C" ) memcpy(dest=ndarray._buf.ptr, src=self._buf.ptr, count=ndarray.size) @@ -1840,36 +1840,36 @@ struct Matrix[dtype: DType = DType.float64, BufType: Buffered = OwnData]( # Implement a dictionary for this later var numpyarray: PythonObject var np_dtype = np.float64 - if dtype == DType.float16: + if Self.dtype == DType.float16: np_dtype = np.float16 - elif dtype == DType.float32: + elif Self.dtype == DType.float32: np_dtype = np.float32 - elif dtype == DType.int64: + elif Self.dtype == DType.int64: np_dtype = np.int64 - elif dtype == DType.int32: + elif Self.dtype == DType.int32: np_dtype = np.int32 - elif dtype == DType.int16: + elif Self.dtype == DType.int16: np_dtype = np.int16 - elif dtype == DType.int8: + elif Self.dtype == DType.int8: np_dtype = np.int8 - elif dtype == DType.uint64: + elif Self.dtype == DType.uint64: np_dtype = np.uint64 - elif dtype == DType.uint32: + elif Self.dtype == DType.uint32: np_dtype = np.uint32 - elif dtype == DType.uint16: + elif Self.dtype == DType.uint16: np_dtype = np.uint16 - elif dtype == DType.uint8: + elif Self.dtype == DType.uint8: np_dtype = np.uint8 - elif dtype == DType.bool: + elif Self.dtype == DType.bool: np_dtype = np.bool_ - elif dtype == DType.int: + elif Self.dtype == DType.int: np_dtype = np.int64 var order = "C" if self.flags.C_CONTIGUOUS else "F" numpyarray = np.empty(np_arr_dim, dtype=np_dtype, order=order) var pointer_d = numpyarray.__array_interface__["data"][ 0 - ].unsafe_get_as_pointer[dtype]() + ].unsafe_get_as_pointer[Self.dtype]() memcpy(dest=pointer_d, src=self._buf.ptr, count=self.size) return numpyarray^ @@ -2179,7 +2179,7 @@ fn _arithmetic_func_matrix_matrix_to_matrix[ For example: `__add__`, `__sub__`, etc. """ - alias simd_width = simd_width_of[dtype]() + comptime simd_width = simd_width_of[dtype]() if A.order() != B.order(): raise Error( String("Matrix order {} does not match {}.").format( @@ -2221,7 +2221,7 @@ fn _arithmetic_func_matrix_to_matrix[ For example: `sin`, `cos`, etc. """ - alias simd_width: Int = simd_width_of[dtype]() + comptime simd_width: Int = simd_width_of[dtype]() var C: Matrix[dtype] = Matrix[dtype](shape=A.shape, order=A.order()) @@ -2245,7 +2245,7 @@ fn _logic_func_matrix_matrix_to_matrix[ """ Matrix[dtype] & Matrix[dtype] -> Matrix[bool] """ - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() if A.order() != B.order(): raise Error( diff --git a/numojo/core/ndarray.mojo b/numojo/core/ndarray.mojo index af700ace..a5d38f7b 100644 --- a/numojo/core/ndarray.mojo +++ b/numojo/core/ndarray.mojo @@ -122,7 +122,7 @@ struct NDArray[dtype: DType = DType.float64]( Writable, ): # TODO: NDArray[dtype: DType = DType.float64, - # Buffer: Bufferable[dtype] = DataContainer[dtype]] + # Buffer: Bufferable[Self.dtype] = DataContainer[Self.dtype]] """The N-dimensional array (NDArray). Parameters: @@ -140,10 +140,10 @@ struct NDArray[dtype: DType = DType.float64]( - The order of the array: Row vs Columns major """ - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[Self.dtype]() """Vector size of the data type.""" - var _buf: DataContainer[dtype] + var _buf: DataContainer[Self.dtype] """Data buffer of the items in the NDArray.""" var ndim: Int """Number of Dimensions.""" @@ -182,7 +182,7 @@ struct NDArray[dtype: DType = DType.float64]( self.shape = NDArrayShape(shape) self.size = self.shape.size_of_array() self.strides = NDArrayStrides(shape, order=order) - self._buf = DataContainer[dtype](self.size) + self._buf = DataContainer[Self.dtype](self.size) self.flags = Flags( self.shape, self.strides, owndata=True, writeable=True ) @@ -238,7 +238,7 @@ struct NDArray[dtype: DType = DType.float64]( self.ndim = self.shape.ndim self.size = self.shape.size_of_array() self.strides = NDArrayStrides(strides=strides) - self._buf = DataContainer[dtype](self.size) + self._buf = DataContainer[Self.dtype](self.size) memset_zero(self._buf.ptr, self.size) self.flags = Flags( self.shape, self.strides, owndata=True, writeable=True @@ -271,14 +271,14 @@ struct NDArray[dtype: DType = DType.float64]( self.ndim = ndim self.size = size self.flags = flags - self._buf = DataContainer[dtype](self.size) + self._buf = DataContainer[Self.dtype](self.size) self.print_options = PrintOptions() # for creating views (unsafe!) fn __init__( out self, shape: NDArrayShape, - ref buffer: UnsafePointer[Scalar[dtype]], + ref buffer: UnsafePointer[Scalar[Self.dtype]], offset: Int, strides: NDArrayStrides, ) raises: @@ -315,7 +315,7 @@ struct NDArray[dtype: DType = DType.float64]( self.shape = other.shape self.size = other.size self.strides = other.strides - self._buf = DataContainer[dtype](self.size) + self._buf = DataContainer[Self.dtype](self.size) memcpy(dest=self._buf.ptr, src=other._buf.ptr, count=other.size) self.flags = Flags( c_contiguous=other.flags.C_CONTIGUOUS, @@ -358,10 +358,10 @@ struct NDArray[dtype: DType = DType.float64]( # Getter dunders and other getter methods # # 1. Basic Indexing Operations - # fn _getitem(self, *indices: Int) -> Scalar[dtype] # Direct unsafe getter - # fn _getitem(self, indices: List[Int]) -> Scalar[dtype] # Direct unsafe getter - # fn __getitem__(self) raises -> SIMD[dtype, 1] # Get 0d array value - # fn __getitem__(self, index: Item) raises -> SIMD[dtype, 1] # Get by coordinate list + # fn _getitem(self, *indices: Int) -> Scalar[Self.dtype] # Direct unsafe getter + # fn _getitem(self, indices: List[Int]) -> Scalar[Self.dtype] # Direct unsafe getter + # fn __getitem__(self) raises -> SIMD[Self.dtype, 1] # Get 0d array value + # fn __getitem__(self, index: Item) raises -> SIMD[Self.dtype, 1] # Get by coordinate list # # 2. Single Index Slicing # fn __getitem__(self, idx: Int) raises -> Self # Get by single index @@ -378,14 +378,14 @@ struct NDArray[dtype: DType = DType.float64]( # fn __getitem__(self, mask: List[Bool]) raises -> Self # Get by boolean list # # 5. Low-level Access - # fn item(self, var index: Int) raises -> Scalar[dtype] # Get item by linear index - # fn item(self, *index: Int) raises -> Scalar[dtype] # Get item by coordinates - # fn load(self, var index: Int) raises -> Scalar[dtype] # Load with bounds check - # fn load[width: Int](self, index: Int) raises -> SIMD[dtype, width] # Load SIMD value - # fn load[width: Int](self, *indices: Int) raises -> SIMD[dtype, width] # Load SIMD at coordinates + # fn item(self, var index: Int) raises -> Scalar[Self.dtype] # Get item by linear index + # fn item(self, *index: Int) raises -> Scalar[Self.dtype] # Get item by coordinates + # fn load(self, var index: Int) raises -> Scalar[Self.dtype] # Load with bounds check + # fn load[width: Int](self, index: Int) raises -> SIMD[Self.dtype, width] # Load SIMD value + # fn load[width: Int](self, *indices: Int) raises -> SIMD[Self.dtype, width] # Load SIMD at coordinates # ===-------------------------------------------------------------------===# - fn _getitem(self, *indices: Int) -> Scalar[dtype]: + fn _getitem(self, *indices: Int) -> Scalar[Self.dtype]: """ Get item at indices and bypass all boundary checks. ***UNSAFE!*** No boundary checks made, for internal use only. @@ -413,7 +413,7 @@ struct NDArray[dtype: DType = DType.float64]( index_of_buffer += indices[i] * Int(self.strides._buf[i]) return self._buf.ptr[index_of_buffer] - fn _getitem(self, indices: List[Int]) -> Scalar[dtype]: + fn _getitem(self, indices: List[Int]) -> Scalar[Self.dtype]: """ Get item at indices and bypass all boundary checks. ***UNSAFE!*** No boundary checks made, for internal use only. @@ -441,7 +441,7 @@ struct NDArray[dtype: DType = DType.float64]( index_of_buffer += indices[i] * Int(self.strides._buf[i]) return self._buf.ptr[index_of_buffer] - fn __getitem__(self) raises -> SIMD[dtype, 1]: + fn __getitem__(self) raises -> SIMD[Self.dtype, 1]: """ Gets the value of the 0-D array. @@ -475,7 +475,7 @@ struct NDArray[dtype: DType = DType.float64]( ) return self._buf.ptr[] - fn __getitem__(self, index: Item) raises -> SIMD[dtype, 1]: + fn __getitem__(self, index: Item) raises -> SIMD[Self.dtype, 1]: """ Get the value at the index list. @@ -601,13 +601,13 @@ struct NDArray[dtype: DType = DType.float64]( # 1-D -> scalar (0-D array wrapper) if self.ndim == 1: - return creation._0darray[dtype](self._buf.ptr[norm]) + return creation._0darray[Self.dtype](self._buf.ptr[norm]) var out_shape = self.shape[1:] var alloc_order = String("C") if self.flags.F_CONTIGUOUS: alloc_order = String("F") - var result = NDArray[dtype](shape=out_shape, order=alloc_order) + var result = NDArray[Self.dtype](shape=out_shape, order=alloc_order) # Fast path for C-contiguous arrays if self.flags.C_CONTIGUOUS: @@ -626,7 +626,7 @@ struct NDArray[dtype: DType = DType.float64]( # perhaps move these to a utility module fn _copy_first_axis_slice( - self, src: NDArray[dtype], norm_idx: Int, mut dst: NDArray[dtype] + self, src: NDArray[Self.dtype], norm_idx: Int, mut dst: NDArray[Self.dtype] ): """Generic stride-based copier for first-axis slice (works for any layout). """ @@ -812,7 +812,7 @@ struct NDArray[dtype: DType = DType.float64]( var narr: Self = Self(offset=noffset, shape=nshape, strides=nstrides) var index: List[Int] = List[Int](length=ndims, fill=0) - _traverse_iterative[dtype]( + _traverse_iterative[Self.dtype]( self, narr, nshape, ncoefficients, nstrides, noffset, index, 0 ) @@ -938,7 +938,7 @@ struct NDArray[dtype: DType = DType.float64]( var narr: Self = Self(offset=noffset, shape=nshape, strides=nstrides) var index: List[Int] = List[Int](length=ndims, fill=0) - _traverse_iterative[dtype]( + _traverse_iterative[Self.dtype]( self, narr, nshape, ncoefficients, nstrides, noffset, index, 0 ) @@ -1172,7 +1172,7 @@ struct NDArray[dtype: DType = DType.float64]( var narr: Self if count_int == self.ndim: - narr = creation._0darray[dtype](self._getitem(indices)) + narr = creation._0darray[Self.dtype](self._getitem(indices)) return narr^ if n_slices < self.ndim: @@ -1231,7 +1231,7 @@ struct NDArray[dtype: DType = DType.float64]( # var shape = indices.shape.join(self.shape._pop(0)) var shape = indices.shape.join(self.shape._pop(0)) - var result = NDArray[dtype](shape) + var result = NDArray[Self.dtype](shape) var size_per_item = self.size // self.shape[0] # Fill in the values @@ -1363,7 +1363,7 @@ struct NDArray[dtype: DType = DType.float64]( if mask.item(i): len_of_result += 1 - var result = NDArray[dtype](shape=NDArrayShape(len_of_result)) + var result = NDArray[Self.dtype](shape=NDArrayShape(len_of_result)) var offset = 0 for i in range(mask.size): @@ -1390,7 +1390,7 @@ struct NDArray[dtype: DType = DType.float64]( var shape = self.shape shape._buf[0] = len_of_result - var result = NDArray[dtype](shape) + var result = NDArray[Self.dtype](shape) var size_per_item = self.size // self.shape[0] # Fill in the values @@ -1474,7 +1474,7 @@ struct NDArray[dtype: DType = DType.float64]( fn item( self, var index: Int ) raises -> ref [self._buf.ptr.origin, self._buf.ptr.address_space] Scalar[ - dtype + Self.dtype ]: """ Return the scalar at the coordinates. @@ -1561,7 +1561,7 @@ struct NDArray[dtype: DType = DType.float64]( fn item( self, *index: Int ) raises -> ref [self._buf.ptr.origin, self._buf.ptr.address_space] Scalar[ - dtype + Self.dtype ]: """ Return the scalar at the coordinates. @@ -1636,7 +1636,7 @@ struct NDArray[dtype: DType = DType.float64]( ) return (self._buf.ptr + _get_offset(index, self.strides))[] - fn load(self, var index: Int) raises -> Scalar[dtype]: + fn load(self, var index: Int) raises -> Scalar[Self.dtype]: """ Safely retrieve i-th item from the underlying buffer. @@ -1680,14 +1680,14 @@ struct NDArray[dtype: DType = DType.float64]( " ({})." ).format(self.size), location=String( - "NDArray.load(index: Int) -> Scalar[dtype]" + "NDArray.load(index: Int) -> Scalar[Self.dtype]" ), ) ) return self._buf.ptr[index] - fn load[width: Int = 1](self, var index: Int) raises -> SIMD[dtype, width]: + fn load[width: Int = 1](self, var index: Int) raises -> SIMD[Self.dtype, width]: """ Safely loads a SIMD element of size `width` at `index` from the underlying buffer. @@ -1718,14 +1718,14 @@ struct NDArray[dtype: DType = DType.float64]( ).format(self.size), location=String( "NDArray.load[width: Int = 1](index: Int) ->" - " SIMD[dtype, width]" + " SIMD[Self.dtype, width]" ), ) ) return self._buf.ptr.load[width=width](index) - fn load[width: Int = 1](self, *indices: Int) raises -> SIMD[dtype, width]: + fn load[width: Int = 1](self, *indices: Int) raises -> SIMD[Self.dtype, width]: """ Safely loads SIMD element of size `width` at given variadic indices from the underlying buffer. @@ -1762,7 +1762,7 @@ struct NDArray[dtype: DType = DType.float64]( ).format(self.ndim), location=String( "NDArray.load[width: Int = 1](*indices: Int) ->" - " SIMD[dtype, width]" + " SIMD[Self.dtype, width]" ), ) ) @@ -1785,7 +1785,7 @@ struct NDArray[dtype: DType = DType.float64]( ).format(self.shape[i]), location=String( "NDArray.load[width: Int = 1](*indices: Int) ->" - " SIMD[dtype, width]" + " SIMD[Self.dtype, width]" ), ) ) @@ -1800,10 +1800,10 @@ struct NDArray[dtype: DType = DType.float64]( # Setter dunders and other setter methods # Basic Setter Methods - # fn _setitem(self, *indices: Int, val: Scalar[dtype]) # Direct unsafe setter + # fn _setitem(self, *indices: Int, val: Scalar[Self.dtype]) # Direct unsafe setter # fn __setitem__(mut self, idx: Int, val: Self) raises # Set by single index - # fn __setitem__(mut self, index: Item, val: Scalar[dtype]) raises # Set by coordinate list - # fn __setitem__(mut self, mask: NDArray[DType.bool], value: Scalar[dtype]) # Set by boolean mask + # fn __setitem__(mut self, index: Item, val: Scalar[Self.dtype]) raises # Set by coordinate list + # fn __setitem__(mut self, mask: NDArray[DType.bool], value: Scalar[Self.dtype]) # Set by boolean mask # Slice-based Setters # fn __setitem__(mut self, *slices: Slice, val: Self) raises # Set by variable slices @@ -1812,16 +1812,16 @@ struct NDArray[dtype: DType = DType.float64]( # Index-based Setters # fn __setitem__(self, indices: NDArray[DType.int], val: NDArray) raises # Set by index array - # fn __setitem__(mut self, mask: NDArray[DType.bool], val: NDArray[dtype]) # Set by boolean mask array + # fn __setitem__(mut self, mask: NDArray[DType.bool], val: NDArray[Self.dtype]) # Set by boolean mask array # Helper Methods - # fn itemset(mut self, index: Variant[Int, List[Int]], item: Scalar[dtype]) # Set single item - # fn store(self, var index: Int, val: Scalar[dtype]) raises # Store with bounds checking - # fn store[width: Int](mut self, index: Int, val: SIMD[dtype, width]) # Store SIMD value - # fn store[width: Int = 1](mut self, *indices: Int, val: SIMD[dtype, width])# Store SIMD at coordinates + # fn itemset(mut self, index: Variant[Int, List[Int]], item: Scalar[Self.dtype]) # Set single item + # fn store(self, var index: Int, val: Scalar[Self.dtype]) raises # Store with bounds checking + # fn store[width: Int](mut self, index: Int, val: SIMD[Self.dtype, width]) # Store SIMD value + # fn store[width: Int = 1](mut self, *indices: Int, val: SIMD[Self.dtype, width])# Store SIMD at coordinates # ===-------------------------------------------------------------------===# - fn _setitem(self, *indices: Int, val: Scalar[dtype]): + fn _setitem(self, *indices: Int, val: Scalar[Self.dtype]): """ (UNSAFE! for internal use only.) Set item at indices and bypass all boundary checks. @@ -1958,7 +1958,7 @@ struct NDArray[dtype: DType = DType.float64]( # perhaps move these to a utility module fn _write_first_axis_slice( - self, dst: NDArray[dtype], norm_idx: Int, src: NDArray[dtype] + self, dst: NDArray[Self.dtype], norm_idx: Int, src: NDArray[Self.dtype] ): var out_ndim = src.ndim var total = src.size @@ -1984,7 +1984,7 @@ struct NDArray[dtype: DType = DType.float64]( src_off += c * stride_src dst._buf.ptr[dst_off] = src._buf.ptr[src_off] - fn __setitem__(mut self, var index: Item, val: Scalar[dtype]) raises: + fn __setitem__(mut self, var index: Item, val: Scalar[Self.dtype]) raises: """ Sets the value at the index list. @@ -2014,7 +2014,7 @@ struct NDArray[dtype: DType = DType.float64]( "Pass exactly {} indices (one per dimension)." ).format(self.ndim), location=String( - "NDArray.__setitem__(index: Item, val: Scalar[dtype])" + "NDArray.__setitem__(index: Item, val: Scalar[Self.dtype])" ), ) ) @@ -2032,7 +2032,7 @@ struct NDArray[dtype: DType = DType.float64]( ).format(self.shape[i]), location=String( "NDArray.__setitem__(index: Item, val:" - " Scalar[dtype])" + " Scalar[Self.dtype])" ), ) ) @@ -2044,7 +2044,7 @@ struct NDArray[dtype: DType = DType.float64]( # only works if array is called as array.__setitem__(), mojo compiler doesn't parse it implicitly fn __setitem__( - mut self, mask: NDArray[DType.bool], value: Scalar[dtype] + mut self, mask: NDArray[DType.bool], value: Scalar[Self.dtype] ) raises: """ Sets the value of the array at the indices where the mask is true. @@ -2079,7 +2079,7 @@ struct NDArray[dtype: DType = DType.float64]( ).format(self.shape), location=String( "NDArray.__setitem__(mask: NDArray[DType.bool], value:" - " Scalar[dtype])" + " Scalar[Self.dtype])" ), ) ) @@ -2252,7 +2252,7 @@ struct NDArray[dtype: DType = DType.float64]( for _ in range(ndims): index.append(0) - _traverse_iterative_setter[dtype]( + _traverse_iterative_setter[Self.dtype]( val, self, nshape, ncoefficients, nstrides, noffset, index ) @@ -2324,7 +2324,7 @@ struct NDArray[dtype: DType = DType.float64]( # TODO: fix this setter, add bound checks. Not sure about it's use case. fn __setitem__( - mut self, index: NDArray[DType.int], val: NDArray[dtype] + mut self, index: NDArray[DType.int], val: NDArray[Self.dtype] ) raises: """ Returns the items of the array from an array of indices. @@ -2410,16 +2410,16 @@ struct NDArray[dtype: DType = DType.float64]( ) ) - # var new_arr: NDArray[dtype] = NDArray[dtype](output_shape) + # var new_arr: NDArray[Self.dtype] = NDArray[Self.dtype](output_shape) for i in range(index.size): print("index.item(i)", index.item(i)) self.__setitem__(idx=Int(index.item(i)), val=val) # for i in range(len(index)): - # self.store(Int(index.load(i)), rebind[Scalar[dtype]](val.load(i))) + # self.store(Int(index.load(i)), rebind[Scalar[Self.dtype]](val.load(i))) fn __setitem__( - mut self, mask: NDArray[DType.bool], val: NDArray[dtype] + mut self, mask: NDArray[DType.bool], val: NDArray[Self.dtype] ) raises: """ Sets the value of the array at the indices where the mask is true. @@ -2456,7 +2456,7 @@ struct NDArray[dtype: DType = DType.float64]( self._buf.ptr.store(i, val._buf.ptr.load(i)) fn itemset( - mut self, index: Variant[Int, List[Int]], item: Scalar[dtype] + mut self, index: Variant[Int, List[Int]], item: Scalar[Self.dtype] ) raises: """Set the scalar at the coordinates. @@ -2533,7 +2533,7 @@ struct NDArray[dtype: DType = DType.float64]( " {})." ).format(self.size), location=String( - "NDArray.itemset(index: Int, item: Scalar[dtype])" + "NDArray.itemset(index: Int, item: Scalar[Self.dtype])" ), ) ) @@ -2552,7 +2552,7 @@ struct NDArray[dtype: DType = DType.float64]( ).format(self.ndim), location=String( "NDArray.itemset(index: List[Int], item:" - " Scalar[dtype])" + " Scalar[Self.dtype])" ), ) ) @@ -2570,13 +2570,13 @@ struct NDArray[dtype: DType = DType.float64]( ).format(self.shape[i]), location=String( "NDArray.itemset(index: List[Int], item:" - " Scalar[dtype])" + " Scalar[Self.dtype])" ), ) ) self._buf.ptr.store(_get_offset(indices, self.strides), item) - fn store(self, var index: Int, val: Scalar[dtype]) raises: + fn store(self, var index: Int, val: Scalar[Self.dtype]) raises: """ Safely store a scalar to i-th item of the underlying buffer. @@ -2612,14 +2612,14 @@ struct NDArray[dtype: DType = DType.float64]( " ({})." ).format(self.size), location=String( - "NDArray.store(index: Int, val: Scalar[dtype])" + "NDArray.store(index: Int, val: Scalar[Self.dtype])" ), ) ) self._buf.ptr[index] = val - fn store[width: Int](mut self, index: Int, val: SIMD[dtype, width]) raises: + fn store[width: Int](mut self, index: Int, val: SIMD[Self.dtype, width]) raises: """ Safely stores SIMD element of size `width` at `index` of the underlying buffer. @@ -2662,7 +2662,7 @@ struct NDArray[dtype: DType = DType.float64]( fn store[ width: Int = 1 - ](mut self, *indices: Int, val: SIMD[dtype, width]) raises: + ](mut self, *indices: Int, val: SIMD[Self.dtype, width]) raises: """ Safely stores SIMD element of size `width` at given variadic indices of the underlying buffer. @@ -2696,7 +2696,7 @@ struct NDArray[dtype: DType = DType.float64]( ).format(self.ndim), location=String( "NDArray.store[width: Int](*indices: Int, val:" - " SIMD[dtype, width])" + " SIMD[Self.dtype, width])" ), ) ) @@ -2715,7 +2715,7 @@ struct NDArray[dtype: DType = DType.float64]( ).format(self.shape[i]), location=String( "NDArray.store[width: Int](*indices: Int, val:" - " SIMD[dtype, width])" + " SIMD[Self.dtype, width])" ), ) ) @@ -2770,13 +2770,13 @@ struct NDArray[dtype: DType = DType.float64]( Examples: ```console - > var A = NDArray[dtype](6, random=True) + > var A = NDArray[Self.dtype](6, random=True) > print(Int(A)) Unhandled exception caught during execution: Only 0-D arrays or length-1 arrays can be converted to scalars mojo: error: execution exited with a non-zero result: 1 - > var B = NDArray[dtype](1, 1, random=True) + > var B = NDArray[Self.dtype](1, 1, random=True) > print(Int(B)) 14 ```. @@ -2831,13 +2831,13 @@ struct NDArray[dtype: DType = DType.float64]( raise Error( "ndarray:NDArrray:__pos__: pos does not accept bool type arrays" ) - return self * Scalar[dtype](-1.0) + return self * Scalar[Self.dtype](-1.0) # maybe they don't need conversion with astype. # @always_inline("nodebug") # fn __eq__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: NDArray[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise equivalence. @@ -2859,7 +2859,7 @@ struct NDArray[dtype: DType = DType.float64]( # @always_inline("nodebug") # fn __eq__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: Scalar[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise equivalence. @@ -2889,10 +2889,10 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.equal[dtype](self, other) + return comparison.equal[Self.dtype](self, other) @always_inline("nodebug") - fn __eq__(self, other: SIMD[dtype, 1]) raises -> NDArray[DType.bool]: + fn __eq__(self, other: SIMD[Self.dtype, 1]) raises -> NDArray[DType.bool]: """ Itemwise equivalence between scalar and Array. @@ -2902,12 +2902,12 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.equal[dtype](self, other) + return comparison.equal[Self.dtype](self, other) # @always_inline("nodebug") # fn __ne__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: NDArray[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise nonequivelence. @@ -2929,7 +2929,7 @@ struct NDArray[dtype: DType = DType.float64]( # @always_inline("nodebug") # fn __ne__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: Scalar[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise nonequivelence between scalar and Array. @@ -2949,7 +2949,7 @@ struct NDArray[dtype: DType = DType.float64]( # ) @always_inline("nodebug") - fn __ne__(self, other: SIMD[dtype, 1]) raises -> NDArray[DType.bool]: + fn __ne__(self, other: SIMD[Self.dtype, 1]) raises -> NDArray[DType.bool]: """ Itemwise nonequivelence. @@ -2959,10 +2959,10 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.not_equal[dtype](self, other) + return comparison.not_equal[Self.dtype](self, other) @always_inline("nodebug") - fn __ne__(self, other: NDArray[dtype]) raises -> NDArray[DType.bool]: + fn __ne__(self, other: NDArray[Self.dtype]) raises -> NDArray[DType.bool]: """ Itemwise nonequivelence between scalar and Array. @@ -2972,12 +2972,12 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.not_equal[dtype](self, other) + return comparison.not_equal[Self.dtype](self, other) # @always_inline("nodebug") # fn __lt__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: Scalar[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise less than. @@ -2999,7 +2999,7 @@ struct NDArray[dtype: DType = DType.float64]( # @always_inline("nodebug") # fn __lt__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: NDArray[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise less than between scalar and Array. @@ -3019,7 +3019,7 @@ struct NDArray[dtype: DType = DType.float64]( # ) @always_inline("nodebug") - fn __lt__(self, other: SIMD[dtype, 1]) raises -> NDArray[DType.bool]: + fn __lt__(self, other: SIMD[Self.dtype, 1]) raises -> NDArray[DType.bool]: """ Itemwise less than. @@ -3029,10 +3029,10 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.less[dtype](self, other) + return comparison.less[Self.dtype](self, other) @always_inline("nodebug") - fn __lt__(self, other: NDArray[dtype]) raises -> NDArray[DType.bool]: + fn __lt__(self, other: NDArray[Self.dtype]) raises -> NDArray[DType.bool]: """ Itemwise less than between scalar and Array. @@ -3042,12 +3042,12 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.less[dtype](self, other) + return comparison.less[Self.dtype](self, other) # @always_inline("nodebug") # fn __le__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: Scalar[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise less than or equal to. @@ -3069,7 +3069,7 @@ struct NDArray[dtype: DType = DType.float64]( # @always_inline("nodebug") # fn __le__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: NDArray[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise less than or equal to between scalar and Array. @@ -3089,7 +3089,7 @@ struct NDArray[dtype: DType = DType.float64]( # ) @always_inline("nodebug") - fn __le__(self, other: SIMD[dtype, 1]) raises -> NDArray[DType.bool]: + fn __le__(self, other: SIMD[Self.dtype, 1]) raises -> NDArray[DType.bool]: """ Itemwise less than or equal to. @@ -3099,10 +3099,10 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.less_equal[dtype](self, other) + return comparison.less_equal[Self.dtype](self, other) @always_inline("nodebug") - fn __le__(self, other: NDArray[dtype]) raises -> NDArray[DType.bool]: + fn __le__(self, other: NDArray[Self.dtype]) raises -> NDArray[DType.bool]: """ Itemwise less than or equal to between scalar and Array. @@ -3112,12 +3112,12 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.less_equal[dtype](self, other) + return comparison.less_equal[Self.dtype](self, other) # @always_inline("nodebug") # fn __gt__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: Scalar[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise greater than. @@ -3139,7 +3139,7 @@ struct NDArray[dtype: DType = DType.float64]( # @always_inline("nodebug") # fn __gt__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: NDArray[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise greater than between scalar and Array. @@ -3159,7 +3159,7 @@ struct NDArray[dtype: DType = DType.float64]( # ) @always_inline("nodebug") - fn __gt__(self, other: SIMD[dtype, 1]) raises -> NDArray[DType.bool]: + fn __gt__(self, other: SIMD[Self.dtype, 1]) raises -> NDArray[DType.bool]: """ Itemwise greater than. @@ -3169,10 +3169,10 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.greater[dtype](self, other) + return comparison.greater[Self.dtype](self, other) @always_inline("nodebug") - fn __gt__(self, other: NDArray[dtype]) raises -> NDArray[DType.bool]: + fn __gt__(self, other: NDArray[Self.dtype]) raises -> NDArray[DType.bool]: """ Itemwise greater than between scalar and Array. @@ -3182,12 +3182,12 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.greater[dtype](self, other) + return comparison.greater[Self.dtype](self, other) # @always_inline("nodebug") # fn __ge__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: Scalar[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise greater than or equal to. @@ -3209,7 +3209,7 @@ struct NDArray[dtype: DType = DType.float64]( # @always_inline("nodebug") # fn __ge__[ # OtherDtype: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDtype](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDtype](), # ](self, other: NDArray[OtherDtype]) raises -> NDArray[DType.bool]: # """ # Itemwise greater than or equal to between scalar and Array. @@ -3229,7 +3229,7 @@ struct NDArray[dtype: DType = DType.float64]( # ) @always_inline("nodebug") - fn __ge__(self, other: SIMD[dtype, 1]) raises -> NDArray[DType.bool]: + fn __ge__(self, other: SIMD[Self.dtype, 1]) raises -> NDArray[DType.bool]: """ Itemwise greater than or equal to. @@ -3239,10 +3239,10 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.greater_equal[dtype](self, other) + return comparison.greater_equal[Self.dtype](self, other) @always_inline("nodebug") - fn __ge__(self, other: NDArray[dtype]) raises -> NDArray[DType.bool]: + fn __ge__(self, other: NDArray[Self.dtype]) raises -> NDArray[DType.bool]: """ Itemwise greater than or equal to between Array and Array. @@ -3252,7 +3252,7 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An array of boolean values. """ - return comparison.greater_equal[dtype](self, other) + return comparison.greater_equal[Self.dtype](self, other) # ===-------------------------------------------------------------------===# # ARITHMETIC OPERATORS @@ -3260,7 +3260,7 @@ struct NDArray[dtype: DType = DType.float64]( # fn __add__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array + scalar`. @@ -3282,7 +3282,7 @@ struct NDArray[dtype: DType = DType.float64]( # fn __add__[ # OtherDType: DType, - # ResultDType: DType = result[dtype, OtherDType](), + # ResultDType: DType = result[Self.dtype, OtherDType](), # ](self, other: NDArray[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array + array`. @@ -3291,21 +3291,21 @@ struct NDArray[dtype: DType = DType.float64]( # self.astype[ResultDType](), other.astype[ResultDType]() # ) - fn __add__(self, other: Scalar[dtype]) raises -> Self: + fn __add__(self, other: Scalar[Self.dtype]) raises -> Self: """ Enables `array + scalar`. """ - return math.add[dtype](self, other) + return math.add[Self.dtype](self, other) fn __add__(self, other: Self) raises -> Self: """ Enables `array + array`. """ - return math.add[dtype](self, other) + return math.add[Self.dtype](self, other) # fn __radd__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `scalar + array`. @@ -3314,14 +3314,14 @@ struct NDArray[dtype: DType = DType.float64]( # self.astype[ResultDType](), other.cast[ResultDType]() # ) - fn __radd__(mut self, other: SIMD[dtype, 1]) raises -> Self: + fn __radd__(mut self, other: SIMD[Self.dtype, 1]) raises -> Self: """ Enables `scalar + array`. """ - return math.add[dtype](self, other) + return math.add[Self.dtype](self, other) # TODO make an inplace version of arithmetic functions for the i dunders - fn __iadd__(mut self, other: SIMD[dtype, 1]) raises: + fn __iadd__(mut self, other: SIMD[Self.dtype, 1]) raises: """ Enables `array += scalar`. """ @@ -3333,13 +3333,13 @@ struct NDArray[dtype: DType = DType.float64]( """ Enables `array *= array`. """ - self = _af.math_func_2_array_in_one_array_out[dtype, SIMD.__add__]( + self = _af.math_func_2_array_in_one_array_out[Self.dtype, SIMD.__add__]( self, other ) # fn __sub__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array - scalar`. @@ -3350,7 +3350,7 @@ struct NDArray[dtype: DType = DType.float64]( # fn __sub__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: NDArray[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array - array`. @@ -3359,21 +3359,21 @@ struct NDArray[dtype: DType = DType.float64]( # self.astype[ResultDType](), other.astype[ResultDType]() # ) - fn __sub__(self, other: Scalar[dtype]) raises -> Self: + fn __sub__(self, other: Scalar[Self.dtype]) raises -> Self: """ Enables `array - scalar`. """ - return math.sub[dtype](self, other) + return math.sub[Self.dtype](self, other) fn __sub__(self, other: Self) raises -> Self: """ Enables `array - array`. """ - return math.sub[dtype](self, other) + return math.sub[Self.dtype](self, other) # fn __rsub__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `scalar - array`. @@ -3382,13 +3382,13 @@ struct NDArray[dtype: DType = DType.float64]( # other.cast[ResultDType](), self.astype[ResultDType]() # ) - fn __rsub__(mut self, other: SIMD[dtype, 1]) raises -> Self: + fn __rsub__(mut self, other: SIMD[Self.dtype, 1]) raises -> Self: """ Enables `scalar - array`. """ - return math.sub[dtype](other, self) + return math.sub[Self.dtype](other, self) - fn __isub__(mut self, other: SIMD[dtype, 1]) raises: + fn __isub__(mut self, other: SIMD[Self.dtype, 1]) raises: """ Enables `array -= scalar`. """ @@ -3405,7 +3405,7 @@ struct NDArray[dtype: DType = DType.float64]( # fn __mul__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array * scalar`. @@ -3416,7 +3416,7 @@ struct NDArray[dtype: DType = DType.float64]( # fn __mul__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: NDArray[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array * array`. @@ -3425,21 +3425,21 @@ struct NDArray[dtype: DType = DType.float64]( # self.astype[ResultDType](), other.astype[ResultDType]() # ) - fn __mul__(self, other: Scalar[dtype]) raises -> Self: + fn __mul__(self, other: Scalar[Self.dtype]) raises -> Self: """ Enables `array * scalar`. """ - return math.mul[dtype](self, other) + return math.mul[Self.dtype](self, other) fn __mul__(self, other: Self) raises -> Self: """ Enables `array * array`. """ - return math.mul[dtype](self, other) + return math.mul[Self.dtype](self, other) # fn __rmul__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `scalar * array`. @@ -3448,13 +3448,13 @@ struct NDArray[dtype: DType = DType.float64]( # self.astype[ResultDType](), other.cast[ResultDType]() # ) - fn __rmul__(mut self, other: SIMD[dtype, 1]) raises -> Self: + fn __rmul__(mut self, other: SIMD[Self.dtype, 1]) raises -> Self: """ Enables `scalar * array`. """ - return math.mul[dtype](self, other) + return math.mul[Self.dtype](self, other) - fn __imul__(mut self, other: SIMD[dtype, 1]) raises: + fn __imul__(mut self, other: SIMD[Self.dtype, 1]) raises: """ Enables `array *= scalar`. """ @@ -3473,13 +3473,13 @@ struct NDArray[dtype: DType = DType.float64]( """ Element-wise inverse (~ or not), only for bools and integral types. """ - return bitwise.invert[dtype](self) + return bitwise.invert[Self.dtype](self) fn __pow__(self, p: Int) -> Self: return self._elementwise_pow(p) # Shouldn't this be inplace? - fn __pow__(self, rhs: Scalar[dtype]) raises -> Self: + fn __pow__(self, rhs: Scalar[Self.dtype]) raises -> Self: """Power of items.""" var result: Self = self.copy() for i in range(self.size): @@ -3530,7 +3530,7 @@ struct NDArray[dtype: DType = DType.float64]( # fn __truediv__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array / scalar`. @@ -3541,7 +3541,7 @@ struct NDArray[dtype: DType = DType.float64]( # fn __truediv__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: NDArray[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array / array`. @@ -3550,19 +3550,19 @@ struct NDArray[dtype: DType = DType.float64]( # self.astype[ResultDType](), other.astype[ResultDType]() # ) - fn __truediv__(self, other: SIMD[dtype, 1]) raises -> Self: + fn __truediv__(self, other: SIMD[Self.dtype, 1]) raises -> Self: """ Enables `array / scalar`. """ - return math.div[dtype](self, other) + return math.div[Self.dtype](self, other) fn __truediv__(self, other: Self) raises -> Self: """ Enables `array / array`. """ - return math.div[dtype](self, other) + return math.div[Self.dtype](self, other) - fn __itruediv__(mut self, s: SIMD[dtype, 1]) raises: + fn __itruediv__(mut self, s: SIMD[Self.dtype, 1]) raises: """ Enables `array /= scalar`. """ @@ -3576,7 +3576,7 @@ struct NDArray[dtype: DType = DType.float64]( # fn __rtruediv__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, s: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `scalar / array`. @@ -3585,15 +3585,15 @@ struct NDArray[dtype: DType = DType.float64]( # s.cast[ResultDType](), self.astype[ResultDType]() # ) - fn __rtruediv__(self, s: SIMD[dtype, 1]) raises -> Self: + fn __rtruediv__(self, s: SIMD[Self.dtype, 1]) raises -> Self: """ Enables `scalar / array`. """ - return math.div[dtype](s, self) + return math.div[Self.dtype](s, self) # fn __floordiv__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array // scalar`. @@ -3604,7 +3604,7 @@ struct NDArray[dtype: DType = DType.float64]( # fn __floordiv__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: NDArray[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array // array`. @@ -3613,19 +3613,19 @@ struct NDArray[dtype: DType = DType.float64]( # self.astype[ResultDType](), other.astype[ResultDType]() # ) - fn __floordiv__(self, other: SIMD[dtype, 1]) raises -> Self: + fn __floordiv__(self, other: SIMD[Self.dtype, 1]) raises -> Self: """ Enables `array // scalar`. """ - return math.floor_div[dtype](self, other) + return math.floor_div[Self.dtype](self, other) fn __floordiv__(self, other: Self) raises -> Self: """ Enables `array // array`. """ - return math.floor_div[dtype](self, other) + return math.floor_div[Self.dtype](self, other) - fn __ifloordiv__(mut self, s: SIMD[dtype, 1]) raises: + fn __ifloordiv__(mut self, s: SIMD[Self.dtype, 1]) raises: """ Enables `array //= scalar`. """ @@ -3639,7 +3639,7 @@ struct NDArray[dtype: DType = DType.float64]( # fn __rfloordiv__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `scalar // array`. @@ -3648,15 +3648,15 @@ struct NDArray[dtype: DType = DType.float64]( # other.cast[ResultDType](), self.astype[ResultDType]() # ) - fn __rfloordiv__(self, other: SIMD[dtype, 1]) raises -> Self: + fn __rfloordiv__(self, other: SIMD[Self.dtype, 1]) raises -> Self: """ Enables `scalar // array`. """ - return math.floor_div[dtype](other, self) + return math.floor_div[Self.dtype](other, self) # fn __mod__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array % scalar`. @@ -3667,7 +3667,7 @@ struct NDArray[dtype: DType = DType.float64]( # fn __mod__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: NDArray[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `array % array`. @@ -3676,39 +3676,39 @@ struct NDArray[dtype: DType = DType.float64]( # self.astype[ResultDType](), other.astype[ResultDType]() # ) - fn __mod__(mut self, other: SIMD[dtype, 1]) raises -> Self: + fn __mod__(mut self, other: SIMD[Self.dtype, 1]) raises -> Self: """ Enables `array % scalar`. """ - return math.mod[dtype](self, other) + return math.mod[Self.dtype](self, other) - fn __mod__(mut self, other: NDArray[dtype]) raises -> Self: + fn __mod__(mut self, other: NDArray[Self.dtype]) raises -> Self: """ Enables `array % array`. """ - return math.mod[dtype](self, other) + return math.mod[Self.dtype](self, other) - fn __imod__(mut self, other: SIMD[dtype, 1]) raises: + fn __imod__(mut self, other: SIMD[Self.dtype, 1]) raises: """ Enables `array %= scalar`. """ - self = math.mod[dtype](self, other) + self = math.mod[Self.dtype](self, other) - fn __imod__(mut self, other: NDArray[dtype]) raises: + fn __imod__(mut self, other: NDArray[Self.dtype]) raises: """ Enables `array %= array`. """ - self = math.mod[dtype](self, other) + self = math.mod[Self.dtype](self, other) - fn __rmod__(mut self, other: SIMD[dtype, 1]) raises -> Self: + fn __rmod__(mut self, other: SIMD[Self.dtype, 1]) raises -> Self: """ Enables `scalar % array`. """ - return math.mod[dtype](other, self) + return math.mod[Self.dtype](other, self) # fn __rmod__[ # OtherDType: DType, - # ResultDType: DType = TypeCoercion.result[dtype, OtherDType](), + # ResultDType: DType = TypeCoercion.result[Self.dtype, OtherDType](), # ](self, other: Scalar[OtherDType]) raises -> NDArray[ResultDType]: # """ # Enables `scalar % array`. @@ -4080,8 +4080,8 @@ struct NDArray[dtype: DType = DType.float64]( edge_items: Int, mut indices: Item, mut negative_sign: Bool, # whether there should be a negative sign - mut max_value: Scalar[dtype], # maximum absolute value of the items - mut min_value: Scalar[dtype], # minimum absolute value of the items + mut max_value: Scalar[Self.dtype], # maximum absolute value of the items + mut min_value: Scalar[Self.dtype], # minimum absolute value of the items current_axis: Int = 0, ) raises: """ @@ -4244,7 +4244,7 @@ struct NDArray[dtype: DType = DType.float64]( """ return creation.astype[target](self) - fn clip(self, a_min: Scalar[dtype], a_max: Scalar[dtype]) -> Self: + fn clip(self, a_min: Scalar[Self.dtype], a_max: Scalar[Self.dtype]) -> Self: """ Limit the values in an array between [a_min, a_max]. If a_min is greater than a_max, the value is equal to a_max. @@ -4345,7 +4345,7 @@ struct NDArray[dtype: DType = DType.float64]( # """ # return Self.__copyinit__(self) - fn cumprod(self) raises -> NDArray[dtype]: + fn cumprod(self) raises -> NDArray[Self.dtype]: """ Returns cumprod of all items of an array. The array is flattened before cumprod. @@ -4353,9 +4353,9 @@ struct NDArray[dtype: DType = DType.float64]( Returns: Cumprod of all items of an array. """ - return numojo.math.cumprod[dtype](self) + return numojo.math.cumprod[Self.dtype](self) - fn cumprod(self, axis: Int) raises -> NDArray[dtype]: + fn cumprod(self, axis: Int) raises -> NDArray[Self.dtype]: """ Returns cumprod of array by axis. @@ -4365,9 +4365,9 @@ struct NDArray[dtype: DType = DType.float64]( Returns: Cumprod of array by axis. """ - return numojo.math.cumprod[dtype](self.copy(), axis=axis) + return numojo.math.cumprod[Self.dtype](self.copy(), axis=axis) - fn cumsum(self) raises -> NDArray[dtype]: + fn cumsum(self) raises -> NDArray[Self.dtype]: """ Returns cumsum of all items of an array. The array is flattened before cumsum. @@ -4375,9 +4375,9 @@ struct NDArray[dtype: DType = DType.float64]( Returns: Cumsum of all items of an array. """ - return numojo.math.cumsum[dtype](self) + return numojo.math.cumsum[Self.dtype](self) - fn cumsum(self, axis: Int) raises -> NDArray[dtype]: + fn cumsum(self, axis: Int) raises -> NDArray[Self.dtype]: """ Returns cumsum of array by axis. @@ -4387,7 +4387,7 @@ struct NDArray[dtype: DType = DType.float64]( Returns: Cumsum of array by axis. """ - return numojo.math.cumsum[dtype](self.copy(), axis=axis) + return numojo.math.cumsum[Self.dtype](self.copy(), axis=axis) fn diagonal(self, offset: Int = 0) raises -> Self: """ @@ -4406,7 +4406,7 @@ struct NDArray[dtype: DType = DType.float64]( """ return numojo.linalg.diagonal(self, offset=offset) - fn fill(mut self, val: Scalar[dtype]): + fn fill(mut self, val: Scalar[Self.dtype]): """ Fill all items of array with value. @@ -4569,7 +4569,7 @@ struct NDArray[dtype: DType = DType.float64]( dimension=normalized_dim, ) - fn max(self) raises -> Scalar[dtype]: + fn max(self) raises -> Scalar[Self.dtype]: """ Finds the max value of an array. When no axis is given, the array is flattened before sorting. @@ -4696,7 +4696,7 @@ struct NDArray[dtype: DType = DType.float64]( """ return median[returned_dtype](self, axis) - fn min(self) raises -> Scalar[dtype]: + fn min(self) raises -> Scalar[Self.dtype]: """ Finds the min value of an array. When no axis is given, the array is flattened before sorting. @@ -4803,7 +4803,7 @@ struct NDArray[dtype: DType = DType.float64]( """ return self.size - fn prod(self) raises -> Scalar[dtype]: + fn prod(self) raises -> Scalar[Self.dtype]: """ Product of all array elements. @@ -4874,7 +4874,7 @@ struct NDArray[dtype: DType = DType.float64]( # TODO: make it inplace? fn reshape( self, shape: NDArrayShape, order: String = "C" - ) raises -> NDArray[dtype]: + ) raises -> NDArray[Self.dtype]: """ Returns an array of the same data with a new shape. @@ -4919,7 +4919,7 @@ struct NDArray[dtype: DType = DType.float64]( Returns: An NDArray. """ - return rounding.tround[dtype](self) + return rounding.tround[Self.dtype](self) fn row(self, id: Int) raises -> Self: """Get the ith row of the matrix. @@ -5019,7 +5019,7 @@ struct NDArray[dtype: DType = DType.float64]( return std[returned_dtype](self, axis=axis, ddof=ddof) - fn sum(self) raises -> Scalar[dtype]: + fn sum(self) raises -> Scalar[Self.dtype]: """ Returns sum of all array elements. @@ -5070,14 +5070,14 @@ struct NDArray[dtype: DType = DType.float64]( """ return numojo.routines.manipulation.transpose(self.copy()) - fn tolist(self) -> List[Scalar[dtype]]: + fn tolist(self) -> List[Scalar[Self.dtype]]: """ Converts NDArray to a 1-D List. Returns: A 1-D List. """ - var result: List[Scalar[dtype]] = List[Scalar[dtype]]() + var result: List[Scalar[Self.dtype]] = List[Scalar[Self.dtype]]() for i in range(self.size): result.append(self._buf.ptr[i]) return result^ @@ -5091,7 +5091,7 @@ struct NDArray[dtype: DType = DType.float64]( """ return to_numpy(self) - # fn to_tensor(self) raises -> Tensor[dtype]: + # fn to_tensor(self) raises -> Tensor[Self.dtype]: # """ # Convert array to tensor of the same dtype. @@ -5125,7 +5125,7 @@ struct NDArray[dtype: DType = DType.float64]( # TODO: add axis parameter fn trace( self, offset: Int = 0, axis1: Int = 0, axis2: Int = 1 - ) raises -> NDArray[dtype]: + ) raises -> NDArray[Self.dtype]: """ Computes the trace of a ndarray. @@ -5137,7 +5137,7 @@ struct NDArray[dtype: DType = DType.float64]( Returns: The trace of the ndarray. """ - return numojo.linalg.trace[dtype](self, offset, axis1, axis2) + return numojo.linalg.trace[Self.dtype](self, offset, axis1, axis2) # TODO: Remove the underscore in the method name when view is supported. fn _transpose(self) raises -> Self: @@ -5159,7 +5159,7 @@ struct NDArray[dtype: DType = DType.float64]( fn unsafe_ptr( ref self, ) -> UnsafePointer[ - Scalar[dtype], + Scalar[Self.dtype], mut = Origin(origin_of(self)).mut, origin = origin_of(self), ]: @@ -5211,7 +5211,7 @@ struct NDArray[dtype: DType = DType.float64]( return variance[returned_dtype](self, axis=axis, ddof=ddof) # TODO: Remove this methods, but add it into routines. - fn vdot(self, other: Self) raises -> SIMD[dtype, 1]: + fn vdot(self, other: Self) raises -> SIMD[Self.dtype, 1]: """ Inner product of two vectors. @@ -5237,7 +5237,7 @@ struct NDArray[dtype: DType = DType.float64]( ) ) - var sum = Scalar[dtype](0) + var sum = Scalar[Self.dtype](0) for i in range(self.size): sum = sum + self.load(i) * other.load(i) return sum @@ -5314,7 +5314,7 @@ struct _NDArrayIter[ """ var index: Int - var ptr: UnsafePointer[Scalar[dtype]] + var ptr: UnsafePointer[Scalar[Self.dtype]] var dimension: Int var length: Int var shape: NDArrayShape @@ -5323,7 +5323,7 @@ struct _NDArrayIter[ var ndim: Int var size_of_item: Int - fn __init__(out self, read a: NDArray[dtype], read dimension: Int) raises: + fn __init__(out self, read a: NDArray[Self.dtype], read dimension: Int) raises: """ Initialize the iterator. @@ -5359,8 +5359,8 @@ struct _NDArrayIter[ fn __iter__(self) -> Self: return self.copy() - fn __next__(mut self) raises -> NDArray[dtype]: - var result: NDArray[dtype] = NDArray[dtype]( + fn __next__(mut self) raises -> NDArray[Self.dtype]: + var result: NDArray[Self.dtype] = NDArray[Self.dtype]( self.shape._pop(self.dimension) ) var current_index: Int = self.index @@ -5404,7 +5404,7 @@ struct _NDArrayIter[ else: return self.index - fn ith(self, index: Int) raises -> NDArray[dtype]: + fn ith(self, index: Int) raises -> NDArray[Self.dtype]: """ Gets the i-th array of the iterator. @@ -5424,7 +5424,7 @@ struct _NDArrayIter[ ) if self.ndim > 1: - var result = NDArray[dtype](self.shape._pop(self.dimension)) + var result = NDArray[Self.dtype](self.shape._pop(self.dimension)) for offset in range(self.size_of_item): var remainder = offset @@ -5445,7 +5445,7 @@ struct _NDArrayIter[ return result^ else: # 0-D array - var result: NDArray[dtype] = numojo.creation._0darray[dtype]( + var result: NDArray[Self.dtype] = numojo.creation._0darray[Self.dtype]( self.ptr[index] ) return result^ @@ -5496,7 +5496,7 @@ struct _NDAxisIter[ ``` """ - var ptr: UnsafePointer[Scalar[dtype]] + var ptr: UnsafePointer[Scalar[Self.dtype]] var axis: Int var order: String var length: Int @@ -5514,7 +5514,7 @@ struct _NDAxisIter[ fn __init__( out self, - read a: NDArray[dtype], + read a: NDArray[Self.dtype], axis: Int, order: String, ) raises: @@ -5585,8 +5585,8 @@ struct _NDAxisIter[ else: return self.index - fn __next__(mut self) raises -> NDArray[dtype]: - var res = NDArray[dtype](Shape(self.size_of_item)) + fn __next__(mut self) raises -> NDArray[Self.dtype]: + var res = NDArray[Self.dtype](Shape(self.size_of_item)) var current_index = self.index @parameter @@ -5636,7 +5636,7 @@ struct _NDAxisIter[ return res^ - fn ith(self, index: Int) raises -> NDArray[dtype]: + fn ith(self, index: Int) raises -> NDArray[Self.dtype]: """ Gets the i-th 1-d array of the iterator. @@ -5655,7 +5655,7 @@ struct _NDAxisIter[ ).format(index, self.length) ) - var elements: NDArray[dtype] = NDArray[dtype](Shape(self.size_of_item)) + var elements: NDArray[Self.dtype] = NDArray[Self.dtype](Shape(self.size_of_item)) var remainder: Int = index * self.size_of_item var item: Item = Item(ndim=self.ndim, initialized=True) @@ -5699,7 +5699,7 @@ struct _NDAxisIter[ fn ith_with_offsets( self, index: Int - ) raises -> Tuple[NDArray[DType.int], NDArray[dtype]]: + ) raises -> Tuple[NDArray[DType.int], NDArray[Self.dtype]]: """ Gets the i-th 1-d array of the iterator and the offsets (in C-order) of its elements. @@ -5714,7 +5714,7 @@ struct _NDAxisIter[ var offsets: NDArray[DType.int] = NDArray[DType.int]( Shape(self.size_of_item) ) - var elements: NDArray[dtype] = NDArray[dtype](Shape(self.size_of_item)) + var elements: NDArray[Self.dtype] = NDArray[Self.dtype](Shape(self.size_of_item)) if (index >= self.length) or (index < 0): raise Error( @@ -5784,7 +5784,7 @@ struct _NDIter[is_mutable: Bool, //, origin: Origin[is_mutable], dtype: DType]( It can be constructed by `NDArray.nditer()` method. """ - var ptr: UnsafePointer[Scalar[dtype]] + var ptr: UnsafePointer[Scalar[Self.dtype]] var length: Int var ndim: Int var shape: NDArrayShape @@ -5796,7 +5796,7 @@ struct _NDIter[is_mutable: Bool, //, origin: Origin[is_mutable], dtype: DType]( var order: String """Order to traverse the array.""" - fn __init__(out self, a: NDArray[dtype], order: String, axis: Int) raises: + fn __init__(out self, a: NDArray[Self.dtype], order: String, axis: Int) raises: self.length = a.size self.order = order self.axis = axis @@ -5832,7 +5832,7 @@ struct _NDIter[is_mutable: Bool, //, origin: Origin[is_mutable], dtype: DType]( else: return False - fn __next__(mut self) raises -> Scalar[dtype]: + fn __next__(mut self) raises -> Scalar[Self.dtype]: var current_index = self.index self.index += 1 @@ -5859,7 +5859,7 @@ struct _NDIter[is_mutable: Bool, //, origin: Origin[is_mutable], dtype: DType]( return self.ptr[_get_offset(indices, self.strides)] - fn ith(self, index: Int) raises -> Scalar[dtype]: + fn ith(self, index: Int) raises -> Scalar[Self.dtype]: """ Gets the i-th element of the iterator. diff --git a/numojo/core/ndshape.mojo b/numojo/core/ndshape.mojo index 65831193..4ea6b59d 100644 --- a/numojo/core/ndshape.mojo +++ b/numojo/core/ndshape.mojo @@ -13,8 +13,8 @@ from memory import LegacyUnsafePointer as UnsafePointer from numojo.core.error import IndexError, ShapeError, ValueError -alias Shape = NDArrayShape -"""An alias of the NDArrayShape.""" +comptime Shape = NDArrayShape +"""An comptime of the NDArrayShape.""" @register_passable @@ -32,7 +32,7 @@ struct NDArrayShape( """ # Aliases - alias _type: DType = DType.int + comptime _type: DType = DType.int # Fields var _buf: UnsafePointer[Scalar[Self._type]] diff --git a/numojo/core/ndstrides.mojo b/numojo/core/ndstrides.mojo index 83f0db33..b50b5139 100644 --- a/numojo/core/ndstrides.mojo +++ b/numojo/core/ndstrides.mojo @@ -13,9 +13,9 @@ from memory import LegacyUnsafePointer as UnsafePointer from numojo.core.error import IndexError, ValueError -alias strides = NDArrayStrides -alias Strides = NDArrayStrides -"""An alias of the NDArrayStrides.""" +comptime strides = NDArrayStrides +comptime Strides = NDArrayStrides +"""An comptime of the NDArrayStrides.""" @register_passable @@ -31,7 +31,7 @@ struct NDArrayStrides( """ # Aliases - alias _type: DType = DType.int + comptime _type: DType = DType.int # Fields var _buf: UnsafePointer[Scalar[Self._type]] diff --git a/numojo/core/traits/indexer_collection_element.mojo b/numojo/core/traits/indexer_collection_element.mojo index d37e0e77..ef36d0cb 100644 --- a/numojo/core/traits/indexer_collection_element.mojo +++ b/numojo/core/traits/indexer_collection_element.mojo @@ -1,4 +1,4 @@ -alias IndexerCollectionElement = Indexer & Copyable & Movable +comptime IndexerCollectionElement = Indexer & Copyable & Movable # trait IndexerCollectionElement(Copyable, Indexer, Movable): # """The IndexerCollectionElement trait denotes a trait composition diff --git a/numojo/routines/constants.mojo b/numojo/routines/constants.mojo index d28ecccd..aa5ace2a 100644 --- a/numojo/routines/constants.mojo +++ b/numojo/routines/constants.mojo @@ -10,7 +10,7 @@ Constants struct Constants(AnyType, Copyable, Movable): """Define constants. - Use alias for compile time evaluation of indefinite precision. + Use comptime for compile time evaluation of indefinite precision. ```mojo import numojo as nm fn main(): @@ -24,10 +24,10 @@ struct Constants(AnyType, Copyable, Movable): ``` """ - alias c = 299_792_458 - alias pi = 3.1415926535897932384626433832795028841971693937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555954930381966446229489 - alias e = 2.71828182845904523536028747135266249775724609375 - alias hbar = 1.0545718176461563912626e-34 + comptime c = 299_792_458 + comptime pi = 3.1415926535897932384626433832795028841971693937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555954930381966446229489 + comptime e = 2.71828182845904523536028747135266249775724609375 + comptime hbar = 1.0545718176461563912626e-34 fn __init__(out self): """ diff --git a/numojo/routines/creation.mojo b/numojo/routines/creation.mojo index 770a89dd..f6f2ed57 100644 --- a/numojo/routines/creation.mojo +++ b/numojo/routines/creation.mojo @@ -323,7 +323,7 @@ fn _linspace_parallel[ A NDArray of `dtype` with `num` linearly spaced elements between `start` and `stop`. """ var result: NDArray[dtype] = NDArray[dtype](NDArrayShape(num)) - alias nelts = simd_width_of[dtype]() + comptime nelts = simd_width_of[dtype]() if endpoint: var denominator: SIMD[dtype, 1] = Scalar[dtype](num) - 1.0 @@ -413,7 +413,7 @@ fn _linspace_serial[ Returns: A ComplexNDArray of `dtype` with `num` linearly spaced elements between `start` and `stop`. """ - alias dtype: DType = cdtype._dtype + comptime dtype: DType = cdtype._dtype var result: ComplexNDArray[cdtype] = ComplexNDArray[cdtype](Shape(num)) if endpoint: @@ -464,8 +464,8 @@ fn _linspace_parallel[ Returns: A ComplexNDArray of `dtype` with `num` linearly spaced elements between `start` and `stop`. """ - alias dtype: DType = cdtype._dtype - alias nelts = simd_width_of[dtype]() + comptime dtype: DType = cdtype._dtype + comptime nelts = simd_width_of[dtype]() var result: ComplexNDArray[cdtype] = ComplexNDArray[cdtype](Shape(num)) if endpoint: @@ -734,7 +734,7 @@ fn _logspace_serial[ Returns: A ComplexNDArray of `dtype` with `num` logarithmic spaced elements between `start` and `stop`. """ - alias dtype: DType = cdtype._dtype + comptime dtype: DType = cdtype._dtype var result: ComplexNDArray[cdtype] = ComplexNDArray[cdtype]( NDArrayShape(num) ) @@ -789,7 +789,7 @@ fn _logspace_parallel[ Returns: A ComplexNDArray of `dtype` with `num` logarithmic spaced elements between `start` and `stop`. """ - alias dtype: DType = cdtype._dtype + comptime dtype: DType = cdtype._dtype var result: ComplexNDArray[cdtype] = ComplexNDArray[cdtype]( NDArrayShape(num) ) @@ -924,7 +924,7 @@ fn geomspace[ constrained[ not cdtype.is_integral(), "Int type will result to precision errors." ]() - alias dtype: DType = cdtype._dtype + comptime dtype: DType = cdtype._dtype var a: ComplexSIMD[cdtype] = start if endpoint: @@ -2244,7 +2244,7 @@ fn astype[ A ComplexNDArray with the same shape and strides as `a` but with elements casted to `target`. """ - alias target_dtype: DType = target._dtype + comptime target_dtype: DType = target._dtype return ComplexNDArray[target]( re=astype[target_dtype](a._re), im=astype[target_dtype](a._im), @@ -2423,7 +2423,7 @@ fn array[ dtype: DType = DType.float64 ](text: String, order: String = "C",) raises -> NDArray[dtype]: """ - This reload is an alias of `fromstring`. + This reload is an comptime of `fromstring`. """ return fromstring[dtype](text, order) @@ -2608,7 +2608,7 @@ fn array[ Returns: A ComplexNDArray constructed from real and imaginary data, shape and order. """ - alias dtype: DType = cdtype._dtype + comptime dtype: DType = cdtype._dtype var len = Int(len(real.shape)) var shape: List[Int] = List[Int]() if real.shape != imag.shape: diff --git a/numojo/routines/io/formatting.mojo b/numojo/routines/io/formatting.mojo index aec7a0a5..d02a64ec 100644 --- a/numojo/routines/io/formatting.mojo +++ b/numojo/routines/io/formatting.mojo @@ -4,24 +4,24 @@ from utils.numerics import isnan, isinf from numojo.core.utility import is_inttype, is_floattype -alias DEFAULT_PRECISION = 4 -alias DEFAULT_SUPPRESS_SMALL = False -alias DEFAULT_SEPARATOR = " " -alias DEFAULT_PADDING = "" -alias DEFAULT_EDGE_ITEMS = 2 -alias DEFAULT_THRESHOLD = 15 -alias DEFAULT_LINE_WIDTH = 75 -alias DEFAULT_SIGN = False -alias DEFAULT_FLOAT_FORMAT = "fixed" -alias DEFAULT_COMPLEX_FORMAT = "parentheses" -alias DEFAULT_NAN_STRING = "nan" -alias DEFAULT_INF_STRING = "inf" -alias DEFAULT_FORMATTED_WIDTH = 6 -alias DEFAULT_EXPONENT_THRESHOLD = 4 -alias DEFAULT_SUPPRESS_SCIENTIFIC = False +comptime DEFAULT_PRECISION = 4 +comptime DEFAULT_SUPPRESS_SMALL = False +comptime DEFAULT_SEPARATOR = " " +comptime DEFAULT_PADDING = "" +comptime DEFAULT_EDGE_ITEMS = 2 +comptime DEFAULT_THRESHOLD = 15 +comptime DEFAULT_LINE_WIDTH = 75 +comptime DEFAULT_SIGN = False +comptime DEFAULT_FLOAT_FORMAT = "fixed" +comptime DEFAULT_COMPLEX_FORMAT = "parentheses" +comptime DEFAULT_NAN_STRING = "nan" +comptime DEFAULT_INF_STRING = "inf" +comptime DEFAULT_FORMATTED_WIDTH = 6 +comptime DEFAULT_EXPONENT_THRESHOLD = 4 +comptime DEFAULT_SUPPRESS_SCIENTIFIC = False # placeholder, we can use this global var option in future when Mojo supports global options -alias GLOBAL_PRINT_OPTIONS = PrintOptions( +comptime GLOBAL_PRINT_OPTIONS = PrintOptions( precision=DEFAULT_PRECISION, suppress_small=DEFAULT_SUPPRESS_SMALL, separator=DEFAULT_SEPARATOR, diff --git a/numojo/routines/linalg/decompositions.mojo b/numojo/routines/linalg/decompositions.mojo index 7eb8e267..77317389 100644 --- a/numojo/routines/linalg/decompositions.mojo +++ b/numojo/routines/linalg/decompositions.mojo @@ -17,8 +17,8 @@ from numojo.routines.creation import zeros, eye, full fn _compute_householder[ dtype: DType ](mut H: Matrix[dtype], mut R: Matrix[dtype], work_index: Int) raises -> None: - alias simd_width = simd_width_of[dtype]() - alias sqrt2: Scalar[dtype] = 1.4142135623730951 + comptime simd_width = simd_width_of[dtype]() + comptime sqrt2: Scalar[dtype] = 1.4142135623730951 var rRows = R.shape[0] @parameter @@ -84,7 +84,7 @@ fn _apply_householder[ ) raises -> None: var aRows = A.shape[0] var aCols = A.shape[1] - alias simdwidth = simd_width_of[dtype]() + comptime simdwidth = simd_width_of[dtype]() for j in range(column_start, aCols): var dot: SIMD[dtype, 1] = 0.0 diff --git a/numojo/routines/linalg/products.mojo b/numojo/routines/linalg/products.mojo index efaac811..f08ff9b7 100644 --- a/numojo/routines/linalg/products.mojo +++ b/numojo/routines/linalg/products.mojo @@ -85,7 +85,7 @@ fn dot[ The dot product of two arrays. """ - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() if array1.ndim == array2.ndim == 1: var result: NDArray[dtype] = NDArray[dtype](NDArrayShape(array1.size)) @@ -123,7 +123,7 @@ fn matmul_tiled_unrolled_parallelized[ """ Matrix multiplication vectorized, tiled, unrolled, and parallelized. """ - alias width = max(simd_width_of[dtype](), 16) + comptime width = max(simd_width_of[dtype](), 16) var result: NDArray[dtype] = zeros[dtype](Shape(A.shape[0], B.shape[1])) var t0 = A.shape[0] var t1 = A.shape[1] @@ -146,12 +146,12 @@ fn matmul_tiled_unrolled_parallelized[ * B._buf.ptr.load[width=simd_width](k * t2 + (n + x)), ) - alias unroll_factor = tile_x // width + comptime unroll_factor = tile_x // width vectorize[ dot, width, size=tile_x, unroll_factor=unroll_factor ]() - alias tile_size = 4 + comptime tile_size = 4 tile[calc_tile, width * tile_size, tile_size](t1, t2) parallelize[calculate_A_rows](t0, t0) @@ -214,7 +214,7 @@ fn matmul_2darray[ matrices. """ - alias width = max(simd_width_of[dtype](), 16) + comptime width = max(simd_width_of[dtype](), 16) if A.ndim * B.ndim == 1: return matmul_1darray(A, B) @@ -372,7 +372,7 @@ fn matmul[ ``` """ - alias width = max(simd_width_of[dtype](), 16) + comptime width = max(simd_width_of[dtype](), 16) if A.shape[1] != B.shape[0]: raise Error( diff --git a/numojo/routines/logic/truth.mojo b/numojo/routines/logic/truth.mojo index a3d6064f..8ba15f73 100644 --- a/numojo/routines/logic/truth.mojo +++ b/numojo/routines/logic/truth.mojo @@ -20,7 +20,7 @@ fn all[dtype: DType](A: Matrix[dtype, **_]) -> Scalar[dtype]: A: Matrix. """ var res = Scalar[dtype](1) - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() @parameter fn cal_and[width: Int](i: Int): @@ -37,7 +37,7 @@ fn all[ Test whether all array elements evaluate to True along axis. """ - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() if axis == 0: var B = Matrix.ones[dtype](shape=(1, A.shape[1])) @@ -86,7 +86,7 @@ fn allt(array: NDArray[DType.bool]) raises -> Scalar[DType.bool]: A boolean scalar """ var result = Scalar[DType.bool](True) - # alias opt_nelts: Int = simd_width_of[DType.bool]() + # comptime opt_nelts: Int = simd_width_of[DType.bool]() # @parameter # fn vectorize_sum[simd_width: Int](idx: Int) -> None: @@ -110,7 +110,7 @@ fn any(array: NDArray[DType.bool]) raises -> Scalar[DType.bool]: A boolean scalar """ var result = Scalar[DType.bool](False) - # alias opt_nelts: Int = simd_width_of[DType.bool]() + # comptime opt_nelts: Int = simd_width_of[DType.bool]() # @parameter # fn vectorize_sum[simd_width: Int](idx: Int) -> None: @@ -132,7 +132,7 @@ fn any[dtype: DType](A: Matrix[dtype, **_]) -> Scalar[dtype]: A: Matrix. """ var res = Scalar[dtype](0) - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() @parameter fn cal_and[width: Int](i: Int): @@ -149,7 +149,7 @@ fn any[ Test whether any array elements evaluate to True along axis. """ - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() if axis == 0: var B = Matrix.zeros[dtype](shape=(1, A.shape[1])) diff --git a/numojo/routines/math/_array_funcs.mojo b/numojo/routines/math/_array_funcs.mojo index ba98ffe0..e247920c 100644 --- a/numojo/routines/math/_array_funcs.mojo +++ b/numojo/routines/math/_array_funcs.mojo @@ -29,7 +29,7 @@ fn math_func_1_array_in_one_array_out[ A new NDArray that is the result of applying the function to the NDArray. """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simd_width: Int](i: Int): @@ -69,7 +69,7 @@ fn math_func_2_array_in_one_array_out[ raise Error("Shape Mismatch error shapes must match for this function") var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simd_width: Int](i: Int): @@ -106,7 +106,7 @@ fn math_func_one_array_one_SIMD_in_one_array_out[ """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simd_width: Int](i: Int): diff --git a/numojo/routines/math/_math_funcs.mojo b/numojo/routines/math/_math_funcs.mojo index b739206d..19f1afc5 100644 --- a/numojo/routines/math/_math_funcs.mojo +++ b/numojo/routines/math/_math_funcs.mojo @@ -64,7 +64,7 @@ struct Vectorized(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() # var op_count:Int =0 @parameter @@ -112,7 +112,7 @@ struct Vectorized(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -153,7 +153,7 @@ struct Vectorized(Backend): return result_array^ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -203,7 +203,7 @@ struct Vectorized(Backend): ](array1, array2[]) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -246,7 +246,7 @@ struct Vectorized(Backend): return result_array^ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -289,7 +289,7 @@ struct Vectorized(Backend): return result_array^ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -325,7 +325,7 @@ struct Vectorized(Backend): var result_array: NDArray[DType.bool] = NDArray[DType.bool]( array1.shape ) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -361,7 +361,7 @@ struct Vectorized(Backend): var result_array: NDArray[DType.bool] = NDArray[DType.bool]( array1.shape ) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -383,7 +383,7 @@ struct Vectorized(Backend): ], ](self, array: NDArray[dtype]) raises -> NDArray[DType.bool]: var result_array: NDArray[DType.bool] = NDArray[DType.bool](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -400,7 +400,7 @@ struct Vectorized(Backend): ], ](self, array: NDArray[dtype], intval: Int) raises -> NDArray[dtype]: var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -480,7 +480,7 @@ struct VectorizedUnroll[unroll_factor: Int = 1](Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -524,7 +524,7 @@ struct VectorizedUnroll[unroll_factor: Int = 1](Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -558,7 +558,7 @@ struct VectorizedUnroll[unroll_factor: Int = 1](Backend): A a new NDArray that is NDArray with the function func applied. """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -600,7 +600,7 @@ struct VectorizedUnroll[unroll_factor: Int = 1](Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -637,7 +637,7 @@ struct VectorizedUnroll[unroll_factor: Int = 1](Backend): """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -674,7 +674,7 @@ struct VectorizedUnroll[unroll_factor: Int = 1](Backend): """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -702,7 +702,7 @@ struct VectorizedUnroll[unroll_factor: Int = 1](Backend): var result_array: NDArray[DType.bool] = NDArray[DType.bool]( array1.shape ) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -731,7 +731,7 @@ struct VectorizedUnroll[unroll_factor: Int = 1](Backend): var result_array: NDArray[DType.bool] = NDArray[DType.bool]( array1.shape ) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -753,7 +753,7 @@ struct VectorizedUnroll[unroll_factor: Int = 1](Backend): ], ](self, array: NDArray[dtype]) raises -> NDArray[DType.bool]: var result_array: NDArray[DType.bool] = NDArray[DType.bool](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -770,7 +770,7 @@ struct VectorizedUnroll[unroll_factor: Int = 1](Backend): ], ](self, array: NDArray[dtype], intval: Int) raises -> NDArray[dtype]: var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -826,7 +826,7 @@ struct Parallelized(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = 1 + comptime width = 1 var num_cores: Int = num_physical_cores() var comps_per_core: Int = array1.size // num_cores @@ -892,7 +892,7 @@ struct Parallelized(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = 1 + comptime width = 1 var num_cores: Int = num_physical_cores() var comps_per_core: Int = array1.size // num_cores @@ -945,7 +945,7 @@ struct Parallelized(Backend): A a new NDArray that is NDArray with the function func applied. """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = 1 + comptime width = 1 var num_cores: Int = num_physical_cores() var comps_per_core: Int = array.size // num_cores @@ -1003,7 +1003,7 @@ struct Parallelized(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = 1 + comptime width = 1 var num_cores: Int = num_physical_cores() var comps_per_core: Int = array1.size // num_cores @@ -1059,7 +1059,7 @@ struct Parallelized(Backend): """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = 1 + comptime width = 1 var num_cores: Int = num_physical_cores() var comps_per_core: Int = array.size // num_cores @@ -1105,7 +1105,7 @@ struct Parallelized(Backend): """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = 1 + comptime width = 1 var num_cores: Int = num_physical_cores() var comps_per_core: Int = array.size // num_cores @@ -1142,7 +1142,7 @@ struct Parallelized(Backend): var result_array: NDArray[DType.bool] = NDArray[DType.bool]( array1.shape ) - alias width = 1 + comptime width = 1 var num_cores: Int = num_physical_cores() var comps_per_core: Int = array1.size // num_cores @@ -1190,7 +1190,7 @@ struct Parallelized(Backend): var result_array: NDArray[DType.bool] = NDArray[DType.bool]( array1.shape ) - alias width = 1 + comptime width = 1 var num_cores: Int = num_physical_cores() var comps_per_core: Int = array1.size // num_cores @@ -1224,7 +1224,7 @@ struct Parallelized(Backend): ], ](self, array: NDArray[dtype]) raises -> NDArray[DType.bool]: var result_array: NDArray[DType.bool] = NDArray[DType.bool](array.shape) - alias width = 1 + comptime width = 1 var num_cores: Int = num_physical_cores() var comps_per_core: Int = array.size // num_cores @@ -1258,7 +1258,7 @@ struct Parallelized(Backend): ], ](self, array: NDArray[dtype], intval: Int) raises -> NDArray[dtype]: var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -1314,7 +1314,7 @@ struct VectorizedParallelized(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() var num_cores: Int = num_physical_cores() var comps_per_core: Int = array1.size // num_cores var comps_remainder: Int = array1.size % num_cores @@ -1391,7 +1391,7 @@ struct VectorizedParallelized(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = 1 + comptime width = 1 var num_cores: Int = num_physical_cores() var comps_per_core: Int = array1.size // num_cores var comps_remainder: Int = array1.size % num_cores @@ -1452,7 +1452,7 @@ struct VectorizedParallelized(Backend): A a new NDArray that is NDArray with the function func applied. """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() var num_cores: Int = num_physical_cores() var comps_per_core: Int = array.size // num_cores var comps_remainder: Int = array.size % num_cores @@ -1516,7 +1516,7 @@ struct VectorizedParallelized(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() var num_cores: Int = num_physical_cores() var comps_per_core: Int = array1.size // num_cores var comps_remainder: Int = array1.size % num_cores @@ -1581,7 +1581,7 @@ struct VectorizedParallelized(Backend): """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() var num_cores: Int = num_physical_cores() var comps_per_core: Int = array.size // num_cores var comps_remainder: Int = array.size % num_cores @@ -1642,7 +1642,7 @@ struct VectorizedParallelized(Backend): """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() var num_cores: Int = num_physical_cores() var comps_per_core: Int = array.size // num_cores var comps_remainder: Int = array.size % num_cores @@ -1694,7 +1694,7 @@ struct VectorizedParallelized(Backend): var result_array: NDArray[DType.bool] = NDArray[DType.bool]( array1.shape ) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() var num_cores: Int = num_physical_cores() var comps_per_core: Int = array1.size // num_cores var comps_remainder: Int = array1.size % num_cores @@ -1756,7 +1756,7 @@ struct VectorizedParallelized(Backend): var result_array: NDArray[DType.bool] = NDArray[DType.bool]( array1.shape ) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() var num_cores: Int = num_physical_cores() var comps_per_core: Int = array1.size // num_cores var comps_remainder: Int = array1.size % num_cores @@ -1802,7 +1802,7 @@ struct VectorizedParallelized(Backend): ], ](self, array: NDArray[dtype]) raises -> NDArray[DType.bool]: var result_array: NDArray[DType.bool] = NDArray[DType.bool](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() var num_cores: Int = num_physical_cores() var comps_per_core: Int = array.size // num_cores var comps_remainder: Int = array.size % num_cores @@ -1842,7 +1842,7 @@ struct VectorizedParallelized(Backend): ], ](self, array: NDArray[dtype], intval: Int) raises -> NDArray[dtype]: var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() @parameter fn closure[simdwidth: Int](i: Int): @@ -1903,7 +1903,7 @@ struct VectorizedParallelized(Backend): # "Shape Mismatch error shapes must match for this function" # ) # var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) -# alias width = simd_width_of[dtype]() +# comptime width = simd_width_of[dtype]() # # #var num_cores: Int = num_physical_cores() # # var simd_ops_per_core: Int = width * (array1.size // width) // num_cores # var comps_per_core: Int = array1.size // num_cores @@ -1979,7 +1979,7 @@ struct VectorizedParallelized(Backend): # "Shape Mismatch error shapes must match for this function" # ) # var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) -# alias width = 1 +# comptime width = 1 # # var num_cores: Int = num_physical_cores() # var comps_per_core: Int = array1.size // num_cores # var comps_remainder: Int = array1.size % num_cores @@ -2036,7 +2036,7 @@ struct VectorizedParallelized(Backend): # A a new NDArray that is NDArray with the function func applied. # """ # var result_array: NDArray[dtype] = NDArray[dtype](array.shape) -# alias width = simd_width_of[dtype]() +# comptime width = simd_width_of[dtype]() # # var num_cores: Int = num_physical_cores() # var comps_per_core: Int = array.size // num_cores # var comps_remainder: Int = array.size % num_cores @@ -2098,7 +2098,7 @@ struct VectorizedParallelized(Backend): # "Shape Mismatch error shapes must match for this function" # ) # var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) -# alias width = simd_width_of[dtype]() +# comptime width = simd_width_of[dtype]() # # var num_cores: Int = num_physical_cores() # var comps_per_core: Int = array1.size // num_cores # var comps_remainder: Int = array1.size % num_cores @@ -2158,7 +2158,7 @@ struct VectorizedParallelized(Backend): # A a new NDArray that is NDArray with the function func applied. # """ # var result_array: NDArray[dtype] = NDArray[dtype](array.shape) -# alias width = simd_width_of[dtype]() +# comptime width = simd_width_of[dtype]() # var comps_per_core: Int = array.size // num_cores # var comps_remainder: Int = array.size % num_cores # var remainder_offset: Int = num_cores * comps_per_core @@ -2207,7 +2207,7 @@ struct VectorizedParallelized(Backend): # var result_array: NDArray[DType.bool] = NDArray[DType.bool]( # array1.shape # ) -# alias width = simd_width_of[dtype]() +# comptime width = simd_width_of[dtype]() # # var num_cores: Int = num_physical_cores() # var comps_per_core: Int = array1.size // num_cores # var comps_remainder: Int = array1.size % num_cores @@ -2265,7 +2265,7 @@ struct VectorizedParallelized(Backend): # var result_array: NDArray[DType.bool] = NDArray[DType.bool]( # array1.shape # ) -# alias width = simd_width_of[dtype]() +# comptime width = simd_width_of[dtype]() # # var num_cores: Int = num_physical_cores() # var comps_per_core: Int = array1.size // num_cores # var comps_remainder: Int = array1.size % num_cores @@ -2311,7 +2311,7 @@ struct VectorizedParallelized(Backend): # var result_array: NDArray[DType.bool] = NDArray[DType.bool]( # array.shape # ) -# alias width = simd_width_of[dtype]() +# comptime width = simd_width_of[dtype]() # # var num_cores: Int = num_physical_cores() # var comps_per_core: Int = array.size // num_cores # var comps_remainder: Int = array.size % num_cores @@ -2349,7 +2349,7 @@ struct VectorizedParallelized(Backend): # ], # ](self, array: NDArray[dtype], intval: Int) raises -> NDArray[dtype]: # var result_array: NDArray[dtype] = NDArray[dtype](array.shape) -# alias width = simd_width_of[dtype]() +# comptime width = simd_width_of[dtype]() # @parameter # fn closure[simdwidth: Int](i: Int): @@ -2404,7 +2404,7 @@ struct Naive(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(array1.size): var simd_data1 = array1._buf.ptr.load[width=1](i) @@ -2445,7 +2445,7 @@ struct Naive(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(array1.size): var simd_data1 = array1._buf.ptr.load[width=1](i) @@ -2704,7 +2704,7 @@ struct VectorizedVerbose(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(0, width * (array1.size // width), width): var simd_data1 = array1._buf.ptr.load[width=width](i) var simd_data2 = array2._buf.ptr.load[width=width](i) @@ -2756,7 +2756,7 @@ struct VectorizedVerbose(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(0, width * (array1.size // width), width): var simd_data1 = array1._buf.ptr.load[width=width](i) var simd_data2 = array2._buf.ptr.load[width=width](i) @@ -2798,7 +2798,7 @@ struct VectorizedVerbose(Backend): A new NDArray that is NDArray with the function func applied. """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(0, width * (array.size // width), width): var simd_data = array._buf.ptr.load[width=width](i) result_array.store[width=width](i, func[dtype, width](simd_data)) @@ -2843,7 +2843,7 @@ struct VectorizedVerbose(Backend): "Shape Mismatch error shapes must match for this function" ) var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(0, width * (array1.size // width), width): var simd_data1 = array1._buf.ptr.load[width=width](i) var simd_data2 = array2._buf.ptr.load[width=width](i) @@ -2886,7 +2886,7 @@ struct VectorizedVerbose(Backend): A a new NDArray that is NDArray with the function func applied. """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(0, width * (array.size // width), width): var simd_data1 = array._buf.ptr.load[width=width](i) var simd_data2 = scalar @@ -2929,7 +2929,7 @@ struct VectorizedVerbose(Backend): A a new NDArray that is NDArray with the function func applied. """ var result_array: NDArray[dtype] = NDArray[dtype](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(0, width * (array.size // width), width): var simd_data1 = array._buf.ptr.load[width=width](i) var simd_data2 = scalar @@ -2964,7 +2964,7 @@ struct VectorizedVerbose(Backend): var result_array: NDArray[DType.bool] = NDArray[DType.bool]( array1.shape ) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(0, width * (array1.size // width), width): var simd_data1 = array1._buf.ptr.load[width=width](i) var simd_data2 = array2._buf.ptr.load[width=width](i) @@ -3004,7 +3004,7 @@ struct VectorizedVerbose(Backend): var result_array: NDArray[DType.bool] = NDArray[DType.bool]( array1.shape ) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(0, width * (array1.size // width), width): var simd_data1 = array1._buf.ptr.load[width=width](i) var simd_data2 = SIMD[dtype, width](scalar) @@ -3034,7 +3034,7 @@ struct VectorizedVerbose(Backend): ], ](self, array: NDArray[dtype]) raises -> NDArray[DType.bool]: var result_array: NDArray[DType.bool] = NDArray[DType.bool](array.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(0, width * (array.size // width), width): var simd_data = array._buf.ptr.load[width=width](i) result_array.store[width=width](i, func[dtype, width](simd_data)) @@ -3055,7 +3055,7 @@ struct VectorizedVerbose(Backend): ], ](self, array1: NDArray[dtype], intval: Int) raises -> NDArray[dtype]: var result_array: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() for i in range(0, width * (array1.size // width), width): var simd_data1 = array1._buf.ptr.load[width=width](i) diff --git a/numojo/routines/math/exponents.mojo b/numojo/routines/math/exponents.mojo index 22e016e7..789dafe8 100644 --- a/numojo/routines/math/exponents.mojo +++ b/numojo/routines/math/exponents.mojo @@ -11,7 +11,7 @@ from utils import Variant import numojo.routines.math._math_funcs as _mf from numojo.core.ndarray import NDArray -alias ln = log +comptime ln = log """ Natural Log equivelent to log """ diff --git a/numojo/routines/math/extrema.mojo b/numojo/routines/math/extrema.mojo index e126ec2c..300087a2 100644 --- a/numojo/routines/math/extrema.mojo +++ b/numojo/routines/math/extrema.mojo @@ -60,7 +60,7 @@ fn extrema_1d[ Max value. """ - alias simd_width = builtin_max(simd_width_of[dtype](), 64) + comptime simd_width = builtin_max(simd_width_of[dtype](), 64) var value = a._buf.ptr[0] @parameter @@ -461,7 +461,7 @@ fn minimum[ """ var result: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() if array1.shape != array2.shape: raise Error("array shapes are not the same") @@ -496,7 +496,7 @@ fn maximum[ """ var result: NDArray[dtype] = NDArray[dtype](array1.shape) - alias width = simd_width_of[dtype]() + comptime width = simd_width_of[dtype]() if array1.shape != array2.shape: raise Error("array shapes are not the same") diff --git a/numojo/routines/math/products.mojo b/numojo/routines/math/products.mojo index dc0c7829..c8a70721 100644 --- a/numojo/routines/math/products.mojo +++ b/numojo/routines/math/products.mojo @@ -32,7 +32,7 @@ fn prod[dtype: DType](A: NDArray[dtype]) raises -> Scalar[dtype]: Scalar. """ - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() var res = Scalar[dtype](1) @parameter @@ -91,7 +91,7 @@ fn prod[dtype: DType](A: Matrix[dtype, **_]) -> Scalar[dtype]: A: Matrix. """ var res = Scalar[dtype](1) - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() @parameter fn cal_vec[width: Int](i: Int): @@ -118,7 +118,7 @@ fn prod[dtype: DType](A: Matrix[dtype, **_], axis: Int) raises -> Matrix[dtype]: ``` """ - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() if axis == 0: var B = Matrix.ones[dtype](shape=(1, A.shape[1])) @@ -240,7 +240,7 @@ fn cumprod[ print(mat.cumprod(A)) ``` """ - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() var result: Matrix[dtype] = Matrix.zeros[dtype](A.shape, "C") if A.flags.C_CONTIGUOUS: @@ -275,7 +275,7 @@ fn cumprod[ print(mat.cumprod(A, axis=1)) ``` """ - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() var order: String = "C" if A.flags.C_CONTIGUOUS else "F" var result: Matrix[dtype] = Matrix.zeros[dtype](A.shape, order) diff --git a/numojo/routines/math/sums.mojo b/numojo/routines/math/sums.mojo index 132a7303..452f1540 100644 --- a/numojo/routines/math/sums.mojo +++ b/numojo/routines/math/sums.mojo @@ -30,7 +30,7 @@ fn sum[dtype: DType](A: NDArray[dtype]) -> Scalar[dtype]: Scalar. """ - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() var result: Scalar[dtype] = Scalar[dtype](0) @parameter @@ -125,7 +125,7 @@ fn sum[dtype: DType](A: Matrix[dtype, **_]) -> Scalar[dtype]: ``` """ var res = Scalar[dtype](0) - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() @parameter fn cal_vec[width: Int](i: Int): @@ -154,7 +154,7 @@ fn sum[ ``` """ - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() if axis == 0: var B = Matrix.zeros[dtype](shape=(1, A.shape[1]), order=A.order()) @@ -339,7 +339,7 @@ fn cumsum[ ``` """ - alias width: Int = simd_width_of[dtype]() + comptime width: Int = simd_width_of[dtype]() var order = "C" if A.flags.C_CONTIGUOUS else "F" var result: Matrix[dtype, OwnData] = Matrix.zeros[dtype](A.shape, order) memcpy(dest=result._buf.ptr, src=A._buf.ptr, count=A.size) diff --git a/numojo/routines/sorting.mojo b/numojo/routines/sorting.mojo index 835d91f6..123d7ce5 100644 --- a/numojo/routines/sorting.mojo +++ b/numojo/routines/sorting.mojo @@ -385,7 +385,7 @@ fn binary_sort[ @parameter if dtype != array.dtype: - alias dtype = array.dtype + comptime dtype = array.dtype var result: NDArray[dtype] = NDArray[dtype](array.shape) for i in range(array.size):