Feature Request: Unpacking Array<[A; N], D> into Array<A, D::Larger>
まだ誰も着手していません。
評価
調査の方向性
Start by reviewing the Array, Dimension, mapv, raw_dim, strides, and from_shape_vec APIs shown in the proposal, along with existing dimension-changing operations. Determine how the requested unpacking should fit the ndarray API, including the offset and layout cases raised by the example. Done means the proposed Array<[A; N], D> transformation is integrated with documented behavior and coverage for its supported cases.
索引モデルが issue の本文から書いたものです。
説明
In certain scenarios, it is convenient to create a new dimension by unpacking arrays. For example:
let angles = array![0.1, 0.2, 0.3];
let vectors = angles.mapv(|a| [a.cos(), a.sin()]);
// Here, `vectors` is of type `Array1<[f64; 2]>`, but an `Array2<f64>` is preferred.
To address this, I have written an extension trait:
pub trait ArrayUnpackExt<A, D, const N: usize>
where
D: Dimension,
{
fn unpack(self) -> Array<A, D::Larger>;
}
impl<A, D, const N: usize> ArrayUnpackExt<A, D, N> for Array<[A; N], D>
where
D: Dimension,
{
fn unpack(self) -> Array<A, D::Larger> {
assert!(mem::size_of::<[A; N]>() == mem::size_of::<A>() * N);
let ndim = self.ndim();
let mut sh = self.raw_dim().insert_axis(Axis(ndim));
sh[ndim] = N;
let mut st = D::zeros(ndim);
self.strides().iter().enumerate().for_each(|(i, &v)| {
st[i] = (v * (N as isize)) as usize;
});
let st = st.insert_axis(Axis(ndim));
let (vec, off) = self.into_raw_vec_and_offset();
match off {
Some(0) => {
let vec = {
let mut vec = mem::ManuallyDrop::new(vec);
let ptr = vec.as_mut_ptr() as *mut A;
let len = vec.len() * N;
let cap = vec.capacity() * N;
unsafe { Vec::from_raw_parts(ptr, len, cap) }
};
unsafe { Array::from_shape_vec_unchecked(sh.strides(st), vec) }
}
_ => unreachable!(), // Unable to handle non-zero offsets even with unsafe code
}
}
}
I believe this functionality would be a valuable addition to the ndarray crate, allowing users to easily transform arrays into higher dimensions when needed. I hope you consider embedding this feature into the library.
Thank you for your attention and consideration.
- 主要言語
- Rust
- スター
- 4.3k
- フォーク
- 391
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
rust-ndarray/ndarray のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
rust-ndarray/ndarray#1612 · コメント 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
rust-ndarray/ndarray#1617 · コメント 1 件 ·
-
bug good first issue
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
rust-ndarray/ndarray#1615 · コメント 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
rust-ndarray/ndarray#1610 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
rust-ndarray/ndarray#1609 ·
rust-ndarray/ndarray の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
issue
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
agentic-workflows
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
web-infra-dev/rspack#15847 ·