Broadcasting rule allows broadcasting 0d array to empty 1d array

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

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

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
numpy, python
領域
data

調査の方向性

提供された Python の broadcasted_shape 実装と array API のブロードキャスト仕様から始め、次に、示されている 0d から空の 1d へのケースを NumPy の動作と比較します。互換性が意図されたものかどうかを判断し、仕様上の決定または根拠を記録してください。この issue では、更新対象のプロジェクトファイルやテストは指定されていません。

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

説明

Question topic: Broadcasting

Here is an implementation of the broadcasting algorithm from the spec:

import operator

def broadcasted_shape(sh1, sh2):
    if not isinstance(sh1, (tuple, list)) or not isinstance(sh2, (tuple, list)):
        raise TypeError
    shape1 = tuple(operator.index(i) for i in sh1)
    shape2 = tuple(operator.index(i) for i in sh2)
    n1 = len(shape1)
    n2 = len(shape2)
    n = max(n1, n2)
    shape = [0] * n
    i = n - 1
    while i>=0:
        _n1 = n1 - n + i
        d1 = shape1[_n1] if (_n1 >=0) else 1
        _n2 = n2 - n + i
        d2 = shape2[_n2] if (_n2 >=0) else 1
        if d1 == 1:
            shape[i] = d2
        elif d2 == 1 or d2 == d1:
            shape[i] = d1
        else:
            raise ValueError
        i = i - 1
    return tuple(shape)

With this implementation broadcasting from 0d array to empty 1d array is allowed, which is also consistent with NumPy.

I am not sure why this is a logic thing to do other than to stay compatible with NumPy.

In [1]: from broadcast import broadcasted_shape

In [2]: broadcasted_shape((1,), (0,))
Out[2]: (0,)

In [3]: broadcasted_shape(tuple(), (0,))
Out[3]: (0,)

In [4]: import numpy as np

In [5]: np.broadcast_to(np.array(0), (0,)).shape
Out[5]: (0,)

In [6]: np.broadcast_to(np.array([0]), (0,)).shape
Out[6]: (0,)

The purpose of this issue is to discuss this behavior. If this is as designed, please feel free to close.

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

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

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

はじめの一歩

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

data-apis/array-api のほかの issue

data-apis/array-api の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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