Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
termoshtt opened this issue Jun 28, 2017 · 2 comments
Open

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

termoshtt opened this issue Jun 28, 2017 · 2 comments

Comments

@termoshtt
Copy link
Member

termoshtt commented Jun 28, 2017

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.

@bluss
Copy link
Member

bluss commented Jul 8, 2017

Sure, we should add such a function if possible.

@bluss
Copy link
Member

bluss commented Jul 8, 2017

Indeed you need some implementation specific tricks to transfer the stride correctly, so it's not really a good idea as it is, out of crate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants