I am trying to iterate by rows over a Numpy Array. The array is accessed thru PyO3 and I think the library acess to the underlying C object just fine but I can't seem to find the reference for a more complex SingleIteratorBuilder that helps me access to the array with by rows.
This is the documentation page: https://docs.rs/numpy/0.12.1/numpy/npyiter/struct.NpySingleIterBuilder.html#method.readwrite (I see the project is still on its infancy)
This is my code in rust that is compiled into a python module
#[macro_use]
extern crate std;
extern crate ndarray;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use numpy::*;
#[pyfunction]
fn array_print(_py: Python<'_>, matrix1: &PyArray2<i32> ){
let matrix1_iter = NpySingleIterBuilder::readonly(matrix1.readonly()).build().unwrap();
for i in matrix1_iter{
println!("{}", i);
};
}
/// A Python module implemented in Rust.
#[pymodule]
fn algo_match(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(array_print, m)?)?;
Ok(())
}
And the python code for this case...
#matrixes is the name of the compiled Rust module
import matrixes as am
arr1 = np.array([[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]], ndmin=2)
am.array_print(arr1)
that returns what I was just saying
~project>python use_algorithm.py
1
2
3
4
5
6
1
2
3
4
5
6
Of course I have tried to use ndarray and have the entire pyArray copied into a new object but that is the worse case scenario because my matrixes are supposed to be large both on rows and columns. Duplicating data is perhaps the worse option.