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

Incorrect construction of SimpleArray from a discontiguous slice of ndarray #432

Closed
xingularity opened this issue Oct 19, 2024 · 5 comments
Assignees
Labels
array Multi-dimensional array implementation bug Something isn't working

Comments

@xingularity
Copy link

xingularity commented Oct 19, 2024

When a SimpleArray is constructed through the "array" argument in Python and the numpy array passed to this argument is a subset using ndarray accessor, the SimpleArray is constructed with wrong section of data from the original ndarray. Following code is a demonstration.

from modmesh import SimpleArrayFloat64
import numpy as np
a = np.array([[1,2,3,4,5], [10,20,30,40,50],[100,200,300,400,500]])

Now we have

array([[  1,   2,   3,   4,   5],
       [ 10,  20,  30,  40,  50],
       [100, 200, 300, 400, 500]])

If we construct a SimpleArray with

b = SimpleArrayFloat64(array=a[:, 4])
print(b.ndarray)

The print result is [ 5., 10., 20.].
We should expect a SimpleArray contains [5, 50, 500] instead of [ 5., 10., 20.]

@yungyuc yungyuc added array Multi-dimensional array implementation bug Something isn't working labels Oct 19, 2024
@yungyuc
Copy link
Member

yungyuc commented Oct 19, 2024

The SimpleArray constructor expects a contiguous memory buffer and disregard the strides set in ndarray. Before SimpleArray supports the same stride as ndarray, it should throw an exception when the input ndarray is not contiguous.

For now, please use b = SimpleArrayFloat64(array=a[:,4].copy()) to work around.

@xingularity
Copy link
Author

Thanks @yungyuc, I already used that workaround in my code.

@yungyuc yungyuc changed the title Cannot correctly construct SimpleArray from a subset of numpy array using its accessor Incorrect construction of SimpleArray from a discontiguous slice of ndarray Oct 19, 2024
@yungyuc
Copy link
Member

yungyuc commented Oct 19, 2024

@tigercosmos Would you have time to take a look?

@tigercosmos
Copy link
Collaborator

I can help support this starting from the beginning of November.

@yungyuc
Copy link
Member

yungyuc commented Nov 30, 2024

Fixed by #438

@yungyuc yungyuc closed this as completed Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
array Multi-dimensional array implementation bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants