Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New additions and fixed typos #91

Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
8967613
implemented diagflat for k < 0
shivasankarka Aug 5, 2024
b523040
added tri() method
shivasankarka Aug 5, 2024
36a74a0
added tril function
shivasankarka Aug 9, 2024
dfbb580
added descriptions
shivasankarka Aug 9, 2024
4a5e59d
fixed typos in ndarray
shivasankarka Aug 12, 2024
da8c42e
gitignore doesn't work!
shivasankarka Aug 12, 2024
2145aef
changed quick sort to be an inplace method
shivasankarka Aug 15, 2024
d062460
fixed inplace sorting
shivasankarka Aug 15, 2024
b282dbf
Merge remote-tracking branch 'upstream/experimental' into experimental
shivasankarka Aug 15, 2024
5b869b8
changed internal sort method to sorted (inplace)
shivasankarka Aug 15, 2024
5aebfe9
added dot function base case
shivasankarka Aug 16, 2024
0523f4a
added inplace flatten
shivasankarka Aug 16, 2024
4c1945f
added inplace flatten and new alloc flatten methods
shivasankarka Aug 16, 2024
8b22ed9
fixed typos and formatted
shivasankarka Aug 16, 2024
b054d7b
added trace for 2D arrays
shivasankarka Aug 16, 2024
b1fe89a
removed tril triu temporarily
shivasankarka Aug 16, 2024
b9bfc18
fixed docstrings for trace
shivasankarka Aug 16, 2024
a757869
test
shivasankarka Aug 17, 2024
1157c0c
added constructor method to initialize from numpy arrays
shivasankarka Aug 22, 2024
6d20792
added transpose T() internal method for 2d arrays
shivasankarka Aug 22, 2024
54f080c
removed shape requirement for constructor with numpy input
shivasankarka Aug 22, 2024
f959052
fixed typo in dot function
shivasankarka Aug 22, 2024
deb7b17
fixed tranpose arr.T() method
shivasankarka Aug 22, 2024
10e05bf
updated test.mojo
shivasankarka Aug 22, 2024
a6aebb3
Merge branch 'random_arrays' into experimental
shivasankarka Sep 6, 2024
0eaf1ff
Merge branch 'random_arrays' into experimental
shivasankarka Sep 7, 2024
be75f2c
Merge remote-tracking branch 'upstream/experimental' into experimental
shivasankarka Sep 7, 2024
249db05
fixed spacing between fn, all simdwidthof[] parameters are renamed to…
shivasankarka Sep 8, 2024
96d0ecc
fixed unit test errors
shivasankarka Sep 8, 2024
1c52fc5
fixed random module compile time randint error
shivasankarka Sep 8, 2024
f5f3c09
fixed docstring error in solve.mojo
shivasankarka Sep 9, 2024
205ac11
added magic support with predefined tasks
shivasankarka Sep 9, 2024
66f53be
added more tasks to .toml
shivasankarka Sep 9, 2024
9f183f9
fixed docstring error in ndarray
shivasankarka Sep 9, 2024
6fff544
Merge remote-tracking branch 'upstream/experimental' into experimental
shivasankarka Sep 9, 2024
061ce03
fixed linting error, added readme to .toml
shivasankarka Sep 9, 2024
4d4583e
added instructions for final check in contributing
shivasankarka Sep 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Please follow the Mojo standard library style guide for all contributions. Consi
- Write concise, well-documented code.
- Adhere to formatting conventions for indentation, spacing, and line breaks.

Additionally refer to `style guide.md` for docstring and nameing conventions.
Additionally refer to `style guide.md` for docstring and naming conventions.

## Pull Requests

Expand Down
39 changes: 20 additions & 19 deletions numojo/core/_array_funcs.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ 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 opt_nelts = simdwidthof[dtype]()
alias width = simdwidthof[dtype]()

@parameter
fn closure[simdwidth: Int](i: Int):
var simd_data = array.load[width=opt_nelts](i)
result_array.store[width=opt_nelts](
i, func[dtype, opt_nelts](simd_data)
fn closure[simd_width: Int](i: Int):
var simd_data = array.load[width=simd_width](i)
result_array.store[width=simd_width](
i, func[dtype, simd_width](simd_data)
)

vectorize[closure, opt_nelts](array.num_elements())
vectorize[closure, width](array.num_elements())

return result_array

Expand Down Expand Up @@ -69,17 +69,18 @@ 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 opt_nelts = simdwidthof[dtype]()
alias width = simdwidthof[dtype]()

@parameter
fn closure[simdwidth: Int](i: Int):
var simd_data1 = array1.load[width=opt_nelts](i)
var simd_data2 = array2.load[width=opt_nelts](i)
result_array.store[width=opt_nelts](
i, func[dtype, opt_nelts](simd_data1, simd_data2)
fn closure[simd_width: Int](i: Int):
var simd_data1 = array1.load[width=simd_width](i)
var simd_data2 = array2.load[width=simd_width](i)
result_array.store[width=simd_width](
i, func[dtype, simd_width](simd_data1, simd_data2)
)

vectorize[closure, opt_nelts](result_array.num_elements())
vectorize[closure, width](result_array.num_elements())

return result_array


Expand All @@ -105,14 +106,14 @@ fn math_func_one_array_one_SIMD_in_one_array_out[
"""

var result_array: NDArray[dtype] = NDArray[dtype](array.shape())
alias opt_nelts = simdwidthof[dtype]()
alias width = simdwidthof[dtype]()

@parameter
fn closure[simdwidth: Int](i: Int):
var simd_data1 = array.load[width=opt_nelts](i)
result_array.store[width=opt_nelts](
i, func[dtype, opt_nelts](simd_data1, scalar)
fn closure[simd_width: Int](i: Int):
var simd_data1 = array.load[width=simd_width](i)
result_array.store[width=simd_width](
i, func[dtype, simd_width](simd_data1, scalar)
)

vectorize[closure, opt_nelts](result_array.num_elements())
vectorize[closure, width](result_array.num_elements())
return result_array
129 changes: 72 additions & 57 deletions numojo/core/array_creation_routines.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ Array creation routine.
"""
# ===----------------------------------------------------------------------=== #
# ARRAY CREATION ROUTINES
# Last updated: 2024-06-16
# Last updated: 2024-09-08
# ===----------------------------------------------------------------------=== #


"""
# TODO (In order of priority)
1) Add function overload for List, VariadicList types
2) Implement axis argument for the NDArray creation functions
3) Implement Row/Column Major option
1) Implement axis argument for the NDArray creation functions
"""

from algorithm import parallelize
from builtin.math import pow

from .ndarray import NDArray, NDArrayShape
from .utility_funcs import is_inttype, is_floattype
from .ndarray_utils import _get_index


# ===------------------------------------------------------------------------===#
Expand Down Expand Up @@ -48,13 +46,6 @@ fn arange[
Returns:
A NDArray of datatype `dtype` with elements ranging from `start` to `stop` incremented with `step`.
"""
# if (is_floattype[dtype]() and is_inttype[dtype]()) or (
# is_inttype[dtype]() and is_inttype[dtype]()
# ):
# raise Error(
# "Both input and output datatypes cannot be integers. If the input is a float, the output must also be a float."
# )

var num: Int = ((stop - start) / step).__int__()
var result: NDArray[dtype] = NDArray[dtype](NDArrayShape(num, size=num))
for idx in range(num):
Expand All @@ -66,11 +57,8 @@ fn arange[
# ===------------------------------------------------------------------------===#
# Linear Spacing NDArray Generation
# ===------------------------------------------------------------------------===#


# I think defaulting parallelization to False is better
fn linspace[
dtype: DType
dtype: DType = DType.float64
](
start: Scalar[dtype],
stop: Scalar[dtype],
Expand Down Expand Up @@ -98,12 +86,6 @@ fn linspace[
A NDArray of datatype `dtype` with elements ranging from `start` to `stop` with num elements.

"""
# if (is_inttype[dtype]() and is_inttype[dtype]()) or (
# is_floattype[dtype]() and is_inttype[dtype]()
# ):
# raise Error(
# "Both input and output datatypes cannot be integers. If the input is a float, the output must also be a float."
# )
constrained[not dtype.is_integral()]()
if parallel:
return _linspace_parallel[dtype](start, stop, num, endpoint)
Expand All @@ -112,7 +94,7 @@ fn linspace[


fn _linspace_serial[
dtype: DType
dtype: DType = DType.float64
](
start: SIMD[dtype, 1],
stop: SIMD[dtype, 1],
Expand Down Expand Up @@ -150,7 +132,7 @@ fn _linspace_serial[


fn _linspace_parallel[
dtype: DType
dtype: DType = DType.float64
](
start: SIMD[dtype, 1], stop: SIMD[dtype, 1], num: Int, endpoint: Bool = True
) raises -> NDArray[dtype]:
Expand Down Expand Up @@ -197,7 +179,7 @@ fn _linspace_parallel[
# Logarithmic Spacing NDArray Generation
# ===------------------------------------------------------------------------===#
fn logspace[
dtype: DType
dtype: DType = DType.float64
](
start: Scalar[dtype],
stop: Scalar[dtype],
Expand Down Expand Up @@ -227,12 +209,6 @@ fn logspace[
- A NDArray of `dtype` with `num` logarithmic spaced elements between `start` and `stop`.
"""
constrained[not dtype.is_integral()]()
# if (is_inttype[dtype]() and is_inttype[dtype]()) or (
# is_floattype[dtype]() and is_inttype[dtype]()
# ):
# raise Error(
# "Both input and output datatypes cannot be integers. If the input is a float, the output must also be a float."
# )
if parallel:
return _logspace_parallel[dtype](
start,
Expand All @@ -252,7 +228,7 @@ fn logspace[


fn _logspace_serial[
dtype: DType
dtype: DType = DType.float64
](
start: Scalar[dtype],
stop: Scalar[dtype],
Expand Down Expand Up @@ -290,7 +266,7 @@ fn _logspace_serial[


fn _logspace_parallel[
dtype: DType
dtype: DType = DType.float64
](
start: Scalar[dtype],
stop: Scalar[dtype],
Expand Down Expand Up @@ -339,7 +315,7 @@ fn _logspace_parallel[

# ! Outputs wrong values for Integer type, works fine for float type.
fn geomspace[
dtype: DType
dtype: DType = DType.float64
](
start: Scalar[dtype],
stop: Scalar[dtype],
Expand All @@ -365,13 +341,6 @@ fn geomspace[
A NDArray of `dtype` with `num` geometrically spaced elements between `start` and `stop`.
"""
constrained[not dtype.is_integral()]()
# if (is_inttype[dtype]() and is_inttype[dtype]()) or (
# is_floattype[dtype]() and is_inttype[dtype]()
# ):
# raise Error(
# "Both input and output datatypes cannot be integers. If the input is a float, the output must also be a float."
# )

var a: Scalar[dtype] = start

if endpoint:
Expand All @@ -392,9 +361,7 @@ fn geomspace[
# ===------------------------------------------------------------------------===#
# Commonly used NDArray Generation routines
# ===------------------------------------------------------------------------===#


fn empty[dtype: DType](*shape: Int) raises -> NDArray[dtype]:
fn empty[dtype: DType = DType.float64](*shape: Int) raises -> NDArray[dtype]:
"""
Generate a NDArray of given shape with arbitrary values.

Expand All @@ -410,7 +377,7 @@ fn empty[dtype: DType](*shape: Int) raises -> NDArray[dtype]:
return NDArray[dtype](shape=shape)


fn zeros[dtype: DType](*shape: Int) raises -> NDArray[dtype]:
fn zeros[dtype: DType = DType.float64](*shape: Int) raises -> NDArray[dtype]:
"""
Generate a NDArray of zeros with given shape.

Expand All @@ -426,7 +393,7 @@ fn zeros[dtype: DType](*shape: Int) raises -> NDArray[dtype]:
return NDArray[dtype](shape, fill=SIMD[dtype, 1](0))


fn eye[dtype: DType](N: Int, M: Int) raises -> NDArray[dtype]:
fn eye[dtype: DType = DType.float64](N: Int, M: Int) raises -> NDArray[dtype]:
"""
Return a 2-D NDArray with ones on the diagonal and zeros elsewhere.

Expand All @@ -447,7 +414,7 @@ fn eye[dtype: DType](N: Int, M: Int) raises -> NDArray[dtype]:
return result


fn identity[dtype: DType](N: Int) raises -> NDArray[dtype]:
fn identity[dtype: DType = DType.float64](N: Int) raises -> NDArray[dtype]:
"""
Generate an identity matrix of size N x N.

Expand All @@ -467,7 +434,7 @@ fn identity[dtype: DType](N: Int) raises -> NDArray[dtype]:
return result


fn ones[dtype: DType](*shape: Int) raises -> NDArray[dtype]:
fn ones[dtype: DType = DType.float64](*shape: Int) raises -> NDArray[dtype]:
"""
Generate a NDArray of ones with given shape filled with ones.

Expand All @@ -488,7 +455,7 @@ fn ones[dtype: DType](*shape: Int) raises -> NDArray[dtype]:


fn full[
dtype: DType
dtype: DType = DType.float64
](*shape: Int, fill_value: Scalar[dtype]) raises -> NDArray[dtype]:
"""
Generate a NDArray of `fill_value` with given shape.
Expand All @@ -507,7 +474,7 @@ fn full[


fn full[
dtype: DType
dtype: DType = DType.float64
](shape: VariadicList[Int], fill_value: Scalar[dtype]) raises -> NDArray[dtype]:
"""
Generate a NDArray of `fill_value` with given shape.
Expand All @@ -527,7 +494,7 @@ fn full[


fn diagflat[
dtype: DType
dtype: DType = DType.float64
](inout v: NDArray[dtype], k: Int = 0) raises -> NDArray[dtype]:
"""
Generate a 2-D NDArray with the flattened input as the diagonal.
Expand All @@ -549,16 +516,64 @@ fn diagflat[
for i in range(n):
print(n * i + i + k)
result.store(n * i + i + k, v.data[i])
if k > 0:
for i in range(n):
result.store(n * i + i + k, v.data[i])
else:
for i in range(n):
result.store(
result.ndshape.ndsize - 1 - (n * i + i + abs(k)),
v.data[v.ndshape.ndsize - 1 - i],
)
return result


fn tri[
dtype: DType = DType.float64
](N: Int, M: Int, k: Int = 0) raises -> NDArray[dtype]:
"""
Generate a 2-D NDArray with ones on and below the k-th diagonal.

Parameters:
dtype: Datatype of the NDArray elements.

Args:
N: Number of rows in the matrix.
M: Number of columns in the matrix.
k: Diagonal offset.

Returns:
A 2-D NDArray with ones on and below the k-th diagonal.
"""
var result: NDArray[dtype] = NDArray[dtype](N, M, fill=SIMD[dtype, 1](0))
for i in range(N):
for j in range(M):
if i >= j - k:
result.store(i, j, val=Scalar[dtype](1))
return result


fn tri():
pass
# fn tril[dtype: DType = DType.float64](inout m: NDArray[dtype], k: Int = 0) raises:
# """
# Zero out elements above the k-th diagonal.

# Parameters:
# dtype: Datatype of the NDArray elements.

fn tril():
pass
# Args:
# m: NDArray to be zeroed out.
# k: Diagonal offset.
# """
# var index: List[Int] = List[Int]()
# for _ in range(m.ndshape.ndlen):
# index.append(0)

# for i in range(m.ndshape[-1]):
# for j in range(m.ndshape[-2]):
# var idx: Int = _get_index(index, m.ndshape)
# if i >= j - k:
# m.data[idx] = Scalar[dtype](0)
# index[-2] += 1

fn triu():
pass
# fn triu():
# pass
Loading
Loading