Feature suggestion: impl Default for Zip, and add Zip::with_shape() constructor

オープン
#1,227 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
rust
領域
data

調査の方向性

Start by reviewing the existing Zip::from, Zip::and, and and_broadcast entry points and how they handle shapes and broadcasting. Compare the proposed Default and Zip::with_shape APIs with the motivating example; done means the API direction and resulting behavior are clearly agreed and validated.

索引モデルが issue の本文から書いたものです。

説明

Motivating example: I'm computing 3D positions on a unit sphere from a uniform grid of (longitude, latitude) pairs. This is what it looks like now:

        const SHAPE: (usize, usize) = (HEIGHT, WIDTH);
        let cell_size_lon = TAU / WIDTH as f32;
        let cell_size_lat = PI / HEIGHT as f32;
        let lon = Array1::linspace(cell_size_lon, TAU - cell_size_lon, WIDTH)
            .into_shape((1, WIDTH)).unwrap();
        let lat = Array1::linspace(-FRAC_PI_2 + cell_size_lat, FRAC_PI_2 - cell_size_lat, HEIGHT)
            .into_shape((HEIGHT, 1)).unwrap();
        let pos = Zip::from(lat.mapv(f32::cos).broadcast(SHAPE).unwrap())
            .and(lat.mapv(f32::sin).broadcast(SHAPE).unwrap())
            .and(lon.mapv(f32::cos).broadcast(SHAPE).unwrap())
            .and(lon.mapv(f32::sin).broadcast(SHAPE).unwrap())
            .par_map_collect(|&cos_lat, &sin_lat, &cos_lon, &sin_lon| {
                Vec3::new(cos_lat * sin_lon, sin_lat, cos_lat * cos_lon)
            });

I know about azip! and par_azip! but I don't think the small reduction in character count outweighs the additional "magic" syntax, so I don't use them.

Notice the distinction between Zip::from and Zip::and even though these both serve the same purpose: add another producer to the Zip.

This lopsided distinction could be eliminated if Zip implemented Default (and/or had the customary zero-args new() constructor):

        let pos = Zip::default()
            .and(lat.mapv(f32::cos).broadcast(SHAPE).unwrap())
            .and(lat.mapv(f32::sin).broadcast(SHAPE).unwrap())
            .and(lon.mapv(f32::cos).broadcast(SHAPE).unwrap())
            .and(lon.mapv(f32::sin).broadcast(SHAPE).unwrap())
            .par_map_collect(|&cos_lat, &sin_lat, &cos_lon, &sin_lon| {
                Vec3::new(cos_lat * sin_lon, sin_lat, cos_lat * cos_lon)
            });

Even nicer (for this use case) would be if we could specify the shape once up front in the constructor, so we could use and_broadcast on all producers (not just the 2nd-4th):

        let pos = Zip::with_shape(SHAPE)
            .and_broadcast(lat.mapv(f32::cos))
            .and_broadcast(lat.mapv(f32::sin))
            .and_broadcast(lon.mapv(f32::cos))
            .and_broadcast(lon.mapv(f32::sin))
            .par_map_collect(|&cos_lat, &sin_lat, &cos_lon, &sin_lon| {
                Vec3::new(cos_lat * sin_lon, sin_lat, cos_lat * cos_lon)
            });

I can't tell whether this is at all feasible or desirable, just throwing it out there for your consideration, and so that other users with similar needs can chime in.

主要言語
Rust
スター
4.3k
フォーク
391
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-ndarray/ndarray のほかの issue

rust-ndarray/ndarray の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。