Copy numpy data to UnsafePointer

How can I copy the data of a numpy array to an UnsafePointer in the new mojo version?
1 Reply
sora
sora4mo ago
from memory import memcpy
from python import Python
from sys import sizeof

def main():
np = Python.import_module("numpy")

alias N = 100

a = np.arange(N, dtype=np.float32)
p = a.ctypes.data.unsafe_get_as_pointer[DType.float32]()

q = UnsafePointer[Float32].alloc(N)
memcpy(q, p, N)

l = List(unsafe_pointer=q, size=N, capacity=N)
print(l.__str__())

_ = a
from memory import memcpy
from python import Python
from sys import sizeof

def main():
np = Python.import_module("numpy")

alias N = 100

a = np.arange(N, dtype=np.float32)
p = a.ctypes.data.unsafe_get_as_pointer[DType.float32]()

q = UnsafePointer[Float32].alloc(N)
memcpy(q, p, N)

l = List(unsafe_pointer=q, size=N, capacity=N)
print(l.__str__())

_ = a
Want results from more Discord servers?
Add your server