Skip to content

Commit e2d8008

Browse files
committed
treat zero dimension like numpy does
1 parent 5eec58d commit e2d8008

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

src/dimension/broadcast.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ where
2323
// (https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html#general-broadcasting-rules).
2424
//
2525
// Zero dimension element is not in the original rules of broadcasting.
26-
// We currently treat it as the same as 1. Especially, when one side is
27-
// zero with one side is empty, or both sides are zero, the result will
28-
// remain zero.
26+
// We currently treat it like any other number greater than 1. As numpy does.
2927
for i in 0..shape1.ndim() {
3028
out_slice[i] = s1[i];
3129
}
3230
for i in 0..shape2.ndim() {
33-
if out_slice[i + k] != s2[i] && s2[i] != 0 {
34-
if out_slice[i + k] <= 1 {
31+
if out_slice[i + k] != s2[i] {
32+
if out_slice[i + k] == 1 {
3533
out_slice[i + k] = s2[i]
3634
} else if s2[i] != 1 {
3735
return Err(from_kind(ErrorKind::IncompatibleShape));

src/numeric/impl_numeric.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ where
243243
/// **Panics** if `axis` is out of bounds.
244244
pub fn sum_axis(&self, axis: Axis) -> Array<A, D::Smaller>
245245
where
246-
A: Copy + Zero + Add<Output = A>,
246+
A: Clone + Zero + Add<Output = A>,
247247
D: RemoveAxis,
248248
{
249249
let n = self.len_of(axis);
@@ -285,7 +285,7 @@ where
285285
/// ```
286286
pub fn mean_axis(&self, axis: Axis) -> Option<Array<A, D::Smaller>>
287287
where
288-
A: Copy + Zero + FromPrimitive + Add<Output = A> + Div<Output = A>,
288+
A: Clone + Zero + FromPrimitive + Add<Output = A> + Div<Output = A>,
289289
D: RemoveAxis,
290290
{
291291
let axis_length = self.len_of(axis);

tests/array.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1589,19 +1589,19 @@ fn test_broadcast_shape() {
15891589
test_co(
15901590
&Dim([2, 1, 2]).into_dyn(),
15911591
&Dim(0),
1592-
Ok(Dim([2, 1, 2]).into_dyn()),
1592+
Err(ShapeError::from_kind(ErrorKind::IncompatibleShape)),
15931593
);
15941594
test_co(
15951595
&Dim([2, 1, 1]),
1596-
&Dim([0, 0, 0, 3, 4]),
1596+
&Dim([0, 0, 1, 3, 4]),
15971597
Ok(Dim([0, 0, 2, 3, 4])),
15981598
);
15991599
test_co(&Dim([0]), &Dim([0, 0, 0]), Ok(Dim([0, 0, 0])));
1600-
test_co(&Dim(1), &Dim([1, 0, 0]), Ok(Dim([1, 0, 1])));
1600+
test_co(&Dim(1), &Dim([1, 0, 0]), Ok(Dim([1, 0, 0])));
16011601
test_co(
16021602
&Dim([1, 3, 0, 1, 1]),
16031603
&Dim([1, 2, 3, 1]),
1604-
Ok(Dim([1, 3, 2, 3, 1])),
1604+
Err(ShapeError::from_kind(ErrorKind::IncompatibleShape)),
16051605
);
16061606
}
16071607

0 commit comments

Comments
 (0)