MCPcopy Create free account
hub / github.com/SingleRust/SingleRust / convert_array

Function convert_array

src/memory/utils/mod.rs:532–546  ·  view source on GitHub ↗

Convert dense ndarray between numeric types. Transforms a dense array from one numeric type to another, preserving the multidimensional structure. Handles dynamic dimensionality efficiently. ## Parameters `array` - Source array with dynamic dimensions ## Type Parameters `T` - Source numeric type `U` - Target floating-point type ## Returns New array with same shape but converted element type

(
    array: ArrayBase<OwnedRepr<T>, Dim<IxDynImpl>>,
)

Source from the content-addressed store, hash-verified

530/// ## Returns
531/// New array with same shape but converted element type
532fn convert_array<T, U>(
533 array: ArrayBase<OwnedRepr<T>, Dim<IxDynImpl>>,
534) -> anyhow::Result<ArrayBase<OwnedRepr<U>, Dim<IxDynImpl>>>
535where
536 T: NumericOps + NumCast + Copy,
537 U: NumericOps + NumCast + Copy + Float,
538{
539 let shape = array.raw_dim(); // Keep the dynamic dimension
540
541 let (vec, _) = array.into_raw_vec_and_offset();
542
543 let new_values: Vec<U> = vec.into_iter().map(|x| NumCast::from(x).unwrap()).collect();
544
545 Ok(ArrayBase::from_shape_vec(shape, new_values)?)
546}
547
548/// Create a Polars DataFrame from a HashMap of numeric vectors.
549///

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected