Skip to content
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

Is there a way to use index_axis on a reference to ArrayBase? #620

Closed
LukeMathWalker opened this issue Apr 20, 2019 · 3 comments
Closed
Labels

Comments

@LukeMathWalker
Copy link
Member

I have been reviewing #606 and to better understand how the code works I was trying to rewrite it (it usually helps me).
The skeleton of what I was trying to do looks like this:

fn format_array<A, S, D, F>(
    view: &ArrayBase<S, D>,
    f: &mut fmt::Formatter,
    mut format: F,
    limit: Ix) -> fmt::Result
where
    F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result,
    D: Dimension,
    S: Data<Elem=A>,
{
    match view.shape() {
        [] => format(view.iter().next().unwrap(), f)?,
        [_] => format_1d_array(&view, f, format, limit)?,
        shape => {
            let first_axis_length = shape[0];
            let indexes_to_be_printed: Vec<Option<usize>> = [...]
            write!(f, "[")?;
            for index in indexes_to_be_printed {
                match index {
                    Some(i) => format_array(
                        &view.index_axis(Axis(0), i), f, format, limit
                    )?,
                    None => {
                        writeln!(f, "...,")?
                    }
                }
            }
            write!(f, "]")?;
        }
    }
    Ok(())
}

If I try to compile it, I obviously get this error:

error[E0277]: the trait bound `D: dimension::remove_axis::RemoveAxis` is not satisfied
   --> src/arrayformat.rs:111:31
    |
111 |                         &view.index_axis(Axis(0), i), f, format, limit
    |                               ^^^^^^^^^^ the trait `dimension::remove_axis::RemoveAxis` is not implemented for `D`
    |
    = help: consider adding a `where D: dimension::remove_axis::RemoveAxis` bound

At this point I'd have liked to work with a dynamical dimension: get a "dynamically dimensioned" view of my ArrayBase reference and take the risk of a panic when using index_array (if I try to pass invalid parameters).
Is this even possible?
Looking at the signature of into_dyn, it seems I need to consume the array in the conversion, which is something I can't do.
Is there an alternative I can use that could work with this function skeleton?

@LukeMathWalker
Copy link
Member Author

It's when I write stuff down that I get the "aha" moment.
This

let dyn_view = view.view().into_dyn()

should do the job.
I am still curious to know if there is a better way though.

@jturner314
Copy link
Member

jturner314 commented Apr 25, 2019

Yes, I think .view().into_dyn() is the best way right now. We could add a .dyn_view() method or something to make sure the shape/strides don't get copied twice (assuming the compiler doesn't optimize out the extra copy anyway), but unless it's called in a long, tight loop, the cost of an extra copy should be negligible anyway.

@LukeMathWalker
Copy link
Member Author

Yeah, I think we should be fine. Thanks for confirming!

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

No branches or pull requests

2 participants