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

未关闭
#1,227 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-ndarray/ndarray 的其他 Issue

查看 rust-ndarray/ndarray 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。