You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
add associated constructor to Dimension trait #1330
I am working on the ndarray-interp crate, which is generic over the number of dimensions. This causes difficulties when the I need to construct a Dimension that is bigger than 6 (IxDyn).
Specifically the method interp_array needs to generically construct its return array of Dimension <Dq as DimAdd<D::Smaller>>::Output where Dq: Dimension + DimAdd<D::Smaller>.
This works fine for static dimensions as I can use the Default trait to create the correct Dimension. But when <Dq as DimAdd<D::Smaller>>::Output happens to be IxDyn this does not work because the information about the number of dimensions is lost.
This is my current, very ugly solution:
let shape = match <DqasDimAdd<D::Smaller>>::Output::NDIM{Some(_) => {letmut dim = <DqasDimAdd<D::Smaller>>::Output::default();
dim.as_array_view_mut().into_iter()// ... set the correct axis lenghts;
dim
}None => {let lenghts:Vec<usize> = ;// ... collect the correct axis lenghtslet dyn_dim = IxDyn(&lenghts);(&dyn_dim as&dynAny).downcast_ref::<<DqasDimAdd<D::Smaller>>::Output>().unwrap_or_else(|| unimplemented!()).clone()}};
This requires ndarray to guarantee that Dimension::NDIM is only ever None when the underlying type is IxDyn. If there is ever a impl Dimension for NewType this code will probably brake.
I might have missed something, but there does not seem to be a good solution for this problem currently.
I suggest extending the Dimenion trait, possibly by requiring Dimension: TryFrom<&[Ix]> or adding a associated constructor:
traitDimension{/// try to create a new Dimension from the provided slice/// fails when `Self::NDIM.is_some_and(|ndim| ndim != ix.len())`fntry_new(ix:&[Ix]) -> Option<Self>;}
I heave not done much with Ndarray in the last year, so I cannot tell for sure. But I believe the issue still stands. In general, I think this would be a useful function.
Activity
akern40 commentedon May 20, 2025
@jonasBoss this is an old issue, is this still a problem?
akern40 commentedon May 20, 2025
I was surprised that
Dimensiondoesn't already have this capability! I'd be happy to support it.jonasBoss commentedon Jun 8, 2025
I heave not done much with Ndarray in the last year, so I cannot tell for sure. But I believe the issue still stands. In general, I think this would be a useful function.
SomeB1oody commentedon Jul 20, 2026
I have a project that needs this function recently. It should be easy to implement. I will try to implement it myself and submit a PR.
feat: add `Dimension::from_slice` (#1608)