ndarray icon indicating copy to clipboard operation
ndarray copied to clipboard

Memory order like another array

Open nilgoyette opened this issue 4 years ago • 2 comments
trafficstars

One of my function needs to create a new array with the same order as the input. I know I can use clone() when both arrays have the same dimension, but when it's not the case, it's more complex.

fn binary_dilation(mask: &Array3<bool>) -> Array3<bool> {
    let (width, height, depth) = mask.dim();
    let dim = (width + etc., ...);
    let mut new_mask = if mask.is_standard_layout() {
        Array3::from_elem(dim, false)
    } else {
        Array3::from_elem(dim.f(), false)
    };
    // Do something
}

Is there a one-liner for this? Something like

Array3::from_elem(dim.order_of(mask), false)

or anytning that doesn't require an if?

nilgoyette avatar Sep 20 '21 16:09 nilgoyette

We want to make a one-liner for this. The plan is to use the new shape enums from .to_shape() (0.15.x) for all the shape constructors in the coming versions. As a part of that, we want what you're proposing, too.

bluss avatar Sep 20 '21 18:09 bluss

Oh, excellent news. I'll use the ugly condition until then :)

A related problem that I have is that is_standard_layout only tells if the array is in 'c' order when the array is contiguous in memory. If I zip on a non-contiguous view and an owned image, am I wrong to believe that using the same order for both images is better? Is there a way to know for sure the order, even if the array is non-contiguous?

nilgoyette avatar Sep 21 '21 14:09 nilgoyette