Mojo-Numerics-and-Algorithms-group / NuMojo

NuMojo is a library for numerical computing in Mojo 🔥 similar to numpy in Python.
Apache License 2.0
86 stars 15 forks source link

Error creating new NDArray: unknown keyword arguments: 'data', 'shape' #80

Closed mmenendezg closed 1 month ago

mmenendezg commented 1 month ago

Describe the bug Creating a NDArray by specifying the data and shape raises an error that both keyword arguments are unknown.

To Reproduce

var array = nm.NDArray[nm.f32](data=List[Int](1,2,3,4,5,6), shape=List[Int](2,3))

Error raised:

error: Expression [4]:2:31: no matching function in initialization
var array = nm.NDArray[nm.f32](data=List[Int](1,2,3,4,5,6), shape=List[Int](2,3))
            ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expression [0] wrapper:17:5: candidate not viable: unknown keyword arguments: 'data', 'shape'

Screenshots

image
forFudan commented 1 month ago
var array = nm.NDArray[nm.f32](data=List[Int](1,2,3,4,5,6), shape=List[Int](2,3))

The issue is that the data type of the NDArray is nm.f32 but the data type of the list is Int. They are not compatible. The correct code is:

var array = nm.NDArray[nm.f32](data=List[Float32](1,2,3,4,5,6), shape=List[Int](2,3))
MadAlex1997 commented 1 month ago

As @forFudan said it is a result of Mojo's type system, I think when we are closer to V1.0 the code you wrote should be usable. However, I suspect the time it takes to cast values will make it worth it to match the type of the lists used as data inputs for NDArray to the array type itself.

Thanks for trying out our library, if you want to make a PR or feature request to add type casting of the data list please feel free to do so. All PRs should be made on the experimental branch.

forFudan commented 1 month ago

Hi @mmenendezg In the PR #81, an array can be initiated from a string representation of array. You do not need to indicate the type again. If you are willing to try the experimental branch, this might be of interest to you.

mmenendezg commented 1 month ago

This seems really great! More flexibility is great for the devs!