Skip to content

Convert Array<A, D> into ArrayBase<S, D> #325

Open
@termoshtt

Description

@termoshtt

I would like to switch array type to implement a general library

pub fn algorithm<A, Si, So, D>(a: ArrayBase<Si, D>) -> ArrayBase<So, D>
where Si: Data<Elem = A>, So: DataOwned<Elem =A>, D: Dimension
{ ... }

Users of this library can get the result using both Array and RcArray:

let result1: Array2<f64> = algorithm(a);
let result2: RcArray2<f64> = algorithm(a);

However, some algorithm in this crate (e.g. ndarray::linalg::Dot::dot) returns Array,
and thus I try to write a function that convert Array<D> into ArrayBase<S: DataOwned, D> without data copy:

fn generalize<A, S, D>(a: Array<A, D>) -> ArrayBase<S, D>
where
  S: DataOwned<Elem =A>,
  D: Dimension,
{
  ArrayBase::from_shape_vec(a.dim(), a.into_raw_vec())
}

This is an generalization of into_owned and into_shared. This generalize can be compiled, but does not keep its stride.

let a: Array3<f64> = Array::zeros((3, 2, 4).f()); // f-order
let ans = a.clone();
let a: Array3<f64> = generalize(a); // c-order
assert_eq!(a, ans); /// panic!

I cannot find a way to keep strides :< I suspect that I need the strongly typed strides #283
Is it possible to implement it correctly out of this crate? If impossible, please add such function.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions