-
I was playing implementing use numpy::Element;
use numpy::PyArray1;
use numpy::PyArrayDescr;
use pyo3::prelude::*;
#[repr(C)]
#[derive(Clone, Copy, Debug)]
struct Record {
x: f64,
y: f64,
k: i32,
}
unsafe impl Element for Record {
const IS_COPY: bool = true;
fn get_dtype(py: Python<'_>) -> &PyArrayDescr {
PyArrayDescr::new(py, &[("x", "f8"), ("y", "f8"), ("k", "i4")]).unwrap()
}
}
#[pyfunction]
fn norm(a: &PyArray1<Record>) -> f64 {
println!("{:?}", a);
a.readonly().as_array().fold(0.0, |result, &record| {
println!("result = {result}, record = {record:?}");
result + record.x.powi(record.k) + record.y.powi(record.k)
})
}
#[pymodule]
fn coniferest(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(norm, m)?)?;
Ok(())
} It is what I see when call it with Python: result = norm(np.array([(1.0, 2.0, 2), (3.0, 4.0, 3), (5.0, 6.0, 4)],
dtype=np.dtype([('x', '<f8'), ('y', '<f8'), ('k', '<i4')])))
print(f'{result = }')
Do you have any idea what went wrong here? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
If I also print
|
Beta Was this translation helpful? Give feedback.
-
I'm really sorry, for some reason I missed this part of the documentation
|
Beta Was this translation helpful? Give feedback.
-
See also #186 |
Beta Was this translation helpful? Give feedback.
Just for the records: I was able to make it work with
np.dtype(align=True)
. The full working example: