-
Notifications
You must be signed in to change notification settings - Fork 326
Implement co-broadcasting in operator overloading #898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f7a9491
Fix co_broadcast in operator overloading
SparrowLii 5e77eed
Add BroadCastShape<Ix0> to the Dimension definition
SparrowLii e8b29e1
add BroadCastShape<<Self as Dimension>::Smaller>
SparrowLii f7c9da1
use uninitialized to avoid traversing or cloning
SparrowLii 0561c13
Use MaybeUninitSubst and Zip to avoid uninitialized(), rename BroadCa…
SparrowLii fc969bd
treat zero dimension like numpy does
SparrowLii a19149b
rebase and use map_collect_owned in impl_ops.rs
SparrowLii b1f4f95
use izip in index loop in broadcast.rs
SparrowLii c9eb88e
Update documentation and function names
SparrowLii e3b73cc
Update documentation and function names
SparrowLii b623239
Add function broadcast_with
SparrowLii c24e602
Modify the docs and visibility of broadcast_with
SparrowLii 16f382b
TEST: Remove unused imports in tests/broadcast.rs
bluss b39593e
FIX: Rename BroadcastShape to DimMax
bluss 38f7341
FIX: Remove broadcast_shape from the DimMax trait
bluss 03cfdfc
MAINT: Fix clippy warnings for broadcast_with
bluss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
use crate::error::*; | ||
use crate::{Dimension, Ix0, Ix1, Ix2, Ix3, Ix4, Ix5, Ix6, IxDyn}; | ||
|
||
/// Calculate the common shape for a pair of array shapes, that they can be broadcasted | ||
/// to. Return an error if the shapes are not compatible. | ||
/// | ||
/// Uses the [NumPy broadcasting rules] | ||
// (https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html#general-broadcasting-rules). | ||
pub(crate) fn co_broadcast<D1, D2, Output>(shape1: &D1, shape2: &D2) -> Result<Output, ShapeError> | ||
where | ||
D1: Dimension, | ||
D2: Dimension, | ||
Output: Dimension, | ||
{ | ||
let (k, overflow) = shape1.ndim().overflowing_sub(shape2.ndim()); | ||
// Swap the order if d2 is longer. | ||
if overflow { | ||
return co_broadcast::<D2, D1, Output>(shape2, shape1); | ||
} | ||
// The output should be the same length as shape1. | ||
let mut out = Output::zeros(shape1.ndim()); | ||
for (out, s) in izip!(out.slice_mut(), shape1.slice()) { | ||
*out = *s; | ||
} | ||
for (out, s2) in izip!(&mut out.slice_mut()[k..], shape2.slice()) { | ||
if *out != *s2 { | ||
if *out == 1 { | ||
*out = *s2 | ||
} else if *s2 != 1 { | ||
return Err(from_kind(ErrorKind::IncompatibleShape)); | ||
} | ||
} | ||
} | ||
Ok(out) | ||
} | ||
|
||
pub trait DimMax<Other: Dimension> { | ||
/// The resulting dimension type after broadcasting. | ||
type Output: Dimension; | ||
} | ||
|
||
/// Dimensions of the same type remain unchanged when co_broadcast. | ||
/// So you can directly use D as the resulting type. | ||
/// (Instead of <D as DimMax<D>>::BroadcastOutput) | ||
impl<D: Dimension> DimMax<D> for D { | ||
type Output = D; | ||
} | ||
|
||
macro_rules! impl_broadcast_distinct_fixed { | ||
($smaller:ty, $larger:ty) => { | ||
impl DimMax<$larger> for $smaller { | ||
type Output = $larger; | ||
} | ||
|
||
impl DimMax<$smaller> for $larger { | ||
type Output = $larger; | ||
} | ||
}; | ||
} | ||
|
||
impl_broadcast_distinct_fixed!(Ix0, Ix1); | ||
impl_broadcast_distinct_fixed!(Ix0, Ix2); | ||
impl_broadcast_distinct_fixed!(Ix0, Ix3); | ||
impl_broadcast_distinct_fixed!(Ix0, Ix4); | ||
impl_broadcast_distinct_fixed!(Ix0, Ix5); | ||
impl_broadcast_distinct_fixed!(Ix0, Ix6); | ||
impl_broadcast_distinct_fixed!(Ix1, Ix2); | ||
impl_broadcast_distinct_fixed!(Ix1, Ix3); | ||
impl_broadcast_distinct_fixed!(Ix1, Ix4); | ||
impl_broadcast_distinct_fixed!(Ix1, Ix5); | ||
impl_broadcast_distinct_fixed!(Ix1, Ix6); | ||
impl_broadcast_distinct_fixed!(Ix2, Ix3); | ||
impl_broadcast_distinct_fixed!(Ix2, Ix4); | ||
impl_broadcast_distinct_fixed!(Ix2, Ix5); | ||
impl_broadcast_distinct_fixed!(Ix2, Ix6); | ||
impl_broadcast_distinct_fixed!(Ix3, Ix4); | ||
impl_broadcast_distinct_fixed!(Ix3, Ix5); | ||
impl_broadcast_distinct_fixed!(Ix3, Ix6); | ||
impl_broadcast_distinct_fixed!(Ix4, Ix5); | ||
impl_broadcast_distinct_fixed!(Ix4, Ix6); | ||
impl_broadcast_distinct_fixed!(Ix5, Ix6); | ||
impl_broadcast_distinct_fixed!(Ix0, IxDyn); | ||
impl_broadcast_distinct_fixed!(Ix1, IxDyn); | ||
impl_broadcast_distinct_fixed!(Ix2, IxDyn); | ||
impl_broadcast_distinct_fixed!(Ix3, IxDyn); | ||
impl_broadcast_distinct_fixed!(Ix4, IxDyn); | ||
impl_broadcast_distinct_fixed!(Ix5, IxDyn); | ||
impl_broadcast_distinct_fixed!(Ix6, IxDyn); | ||
|
||
|
||
#[cfg(test)] | ||
#[cfg(feature = "std")] | ||
mod tests { | ||
use super::co_broadcast; | ||
use crate::{Dimension, Dim, DimMax, ShapeError, Ix0, IxDynImpl, ErrorKind}; | ||
|
||
#[test] | ||
fn test_broadcast_shape() { | ||
fn test_co<D1, D2>( | ||
d1: &D1, | ||
d2: &D2, | ||
r: Result<<D1 as DimMax<D2>>::Output, ShapeError>, | ||
) where | ||
D1: Dimension + DimMax<D2>, | ||
D2: Dimension, | ||
{ | ||
let d = co_broadcast::<D1, D2, <D1 as DimMax<D2>>::Output>(&d1, d2); | ||
assert_eq!(d, r); | ||
} | ||
test_co(&Dim([2, 3]), &Dim([4, 1, 3]), Ok(Dim([4, 2, 3]))); | ||
test_co( | ||
&Dim([1, 2, 2]), | ||
&Dim([1, 3, 4]), | ||
Err(ShapeError::from_kind(ErrorKind::IncompatibleShape)), | ||
); | ||
test_co(&Dim([3, 4, 5]), &Ix0(), Ok(Dim([3, 4, 5]))); | ||
let v = vec![1, 2, 3, 4, 5, 6, 7]; | ||
test_co( | ||
&Dim(vec![1, 1, 3, 1, 5, 1, 7]), | ||
&Dim([2, 1, 4, 1, 6, 1]), | ||
Ok(Dim(IxDynImpl::from(v.as_slice()))), | ||
); | ||
let d = Dim([1, 2, 1, 3]); | ||
test_co(&d, &d, Ok(d)); | ||
test_co( | ||
&Dim([2, 1, 2]).into_dyn(), | ||
&Dim(0), | ||
Err(ShapeError::from_kind(ErrorKind::IncompatibleShape)), | ||
); | ||
test_co( | ||
&Dim([2, 1, 1]), | ||
&Dim([0, 0, 1, 3, 4]), | ||
Ok(Dim([0, 0, 2, 3, 4])), | ||
); | ||
test_co(&Dim([0]), &Dim([0, 0, 0]), Ok(Dim([0, 0, 0]))); | ||
test_co(&Dim(1), &Dim([1, 0, 0]), Ok(Dim([1, 0, 0]))); | ||
test_co( | ||
&Dim([1, 3, 0, 1, 1]), | ||
&Dim([1, 2, 3, 1]), | ||
Err(ShapeError::from_kind(ErrorKind::IncompatibleShape)), | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.