MCPcopy Create free account
hub / github.com/PyO3/rust-numpy / from_vec3

Method from_vec3

src/array.rs:645–671  ·  view source on GitHub ↗

Construct a three-dimensional array from a [`Vec >>`][Vec]. This function checks all dimensions of the inner vectors and returns an error if they are not all equal. # Example ``` use numpy::{PyArray, PyArrayMethods}; use pyo3::Python; use ndarray::array; Python::attach(|py| { let vec3 = vec![ vec![vec![111, 112], vec![121, 122]], vec![vec![211, 212], vec![221, 222]], ]; let pyarray =

(
        py: Python<'py>,
        v: &[Vec<Vec<T>>],
    )

Source from the content-addressed store, hash-verified

643 /// });
644 /// ```
645 pub fn from_vec3<'py>(
646 py: Python<'py>,
647 v: &[Vec<Vec<T>>],
648 ) -> Result<Bound<'py, Self>, FromVecError> {
649 let len2 = v.first().map_or(0, |v| v.len());
650 let len3 = v.first().map_or(0, |v| v.first().map_or(0, |v| v.len()));
651 let dims = [v.len(), len2, len3];
652 // SAFETY: The result of `Self::new` is always safe to drop.
653 unsafe {
654 let array = Self::new(py, dims, false);
655 let mut data_ptr = array.data();
656 for v in v {
657 if v.len() != len2 {
658 cold();
659 return Err(FromVecError::new(v.len(), len2));
660 }
661 for v in v {
662 if v.len() != len3 {
663 cold();
664 return Err(FromVecError::new(v.len(), len3));
665 }
666 clone_elements(py, v, &mut data_ptr);
667 }
668 }
669 Ok(array)
670 }
671 }
672}
673
674impl<T: Element + AsPrimitive<f64>> PyArray<T, Ix1> {

Callers

nothing calls this directly

Calls 4

coldFunction · 0.85
clone_elementsFunction · 0.85
lenMethod · 0.80
dataMethod · 0.80

Tested by

no test coverage detected