Skip to content

Work around compiler issues with .broadcast() and .remove_axis() #216

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 7 commits into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ addons:
- libopenblas-dev
script:
- |
cargo build --verbose &&
cargo test --verbose &&
([ -z "$FEATURES" ] || cargo build --verbose --features "$FEATURES") &&
([ -z "$FEATURES" ] || cargo test --verbose --features "$FEATURES") &&
cargo build --verbose --features "$FEATURES" &&
cargo test --verbose --features "$FEATURES" &&
cargo test --release --verbose --features "" &&
([ "$BENCH" != 1 ] || cargo bench --no-run --verbose --features "$FEATURES")
2 changes: 1 addition & 1 deletion ndarray-tests/tests/accuracy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn reference_mat_mul<A, S, S2>(lhs: &ArrayBase<S, (Ix, Ix)>, rhs: &ArrayBase<S2,
}
}
unsafe {
ArrayBase::from_vec_dim_unchecked((m, n), res_elems)
ArrayBase::from_shape_vec_unchecked((m, n), res_elems)
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[cfg(feature = "rustc-serialize")]
use serialize::{Encodable, Encoder, Decodable, Decoder};

use std::hash;
use std::iter::FromIterator;
Expand Down
18 changes: 18 additions & 0 deletions src/dimension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,8 @@ pub trait RemoveAxis : Dimension {
}

macro_rules! impl_shrink(
($_a:ident, ) => {}; // implement this case manually below
($_a:ident, $_b:ident, ) => {}; // implement this case manually below
($from:ident, $($more:ident,)*) => (
impl RemoveAxis for ($from $(,$more)*)
{
Expand All @@ -665,6 +667,22 @@ impl RemoveAxis for ($from $(,$more)*)
)
);

impl RemoveAxis for Ix {
type Smaller = ();
#[inline]
fn remove_axis(&self, _: Axis) { }
}

impl RemoveAxis for (Ix, Ix) {
type Smaller = Ix;
#[inline]
fn remove_axis(&self, axis: Axis) -> Ix {
let axis = axis.axis();
debug_assert!(axis < self.ndim());
if axis == 0 { self.1 } else { self.0 }
}
}

macro_rules! impl_shrink_recursive(
($ix:ident, ) => (impl_shrink!($ix,););
($ix1:ident, $($ix:ident,)*) => (
Expand Down
6 changes: 3 additions & 3 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,9 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
}

{
let mut new_stride_iter = new_stride.slice_mut().iter_mut().rev();
for ((er, es), dr) in from.slice().iter().rev()
.zip(stride.slice().iter().rev())
.zip(new_stride_iter.by_ref())
.zip(new_stride.slice_mut().iter_mut().rev())
{
/* update strides */
if *dr == *er {
Expand All @@ -914,7 +913,8 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
}

/* set remaining strides to zero */
for dr in new_stride_iter {
let tail_len = to.ndim() - from.ndim();
for dr in &mut new_stride.slice_mut()[..tail_len] {
*dr = 0;
}
}
Expand Down
36 changes: 35 additions & 1 deletion tests/broadcast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

extern crate ndarray;

use ndarray::{RcArray, Dimension};
use ndarray::prelude::*;

#[test]
fn broadcast_1()
Expand Down Expand Up @@ -49,3 +49,37 @@ fn test_add_incompat()
let incompat = RcArray::from_elem(3, 1.0f32);
a += &incompat;
}

#[test]
fn test_broadcast() {
let (_, n, k) = (16, 16, 16);
let x1 = 1.;
// b0 broadcast 1 -> n, k
let x = Array::from_vec(vec![x1]);
let b0 = x.broadcast((n, k)).unwrap();
// b1 broadcast n -> n, k
let b1 = Array::from_elem(n, x1);
let b1 = b1.broadcast((n, k)).unwrap();
// b2 is n, k
let b2 = Array::from_elem((n, k), x1);

println!("b0=\n{:?}", b0);
println!("b1=\n{:?}", b1);
println!("b2=\n{:?}", b2);
assert_eq!(b0, b1);
assert_eq!(b0, b2);
}

#[test]
fn test_broadcast_1d() {
let n = 16;
let x1 = 1.;
// b0 broadcast 1 -> n
let x = Array::from_vec(vec![x1]);
let b0 = x.broadcast(n).unwrap();
let b2 = Array::from_elem(n, x1);

println!("b0=\n{:?}", b0);
println!("b2=\n{:?}", b2);
assert_eq!(b0, b2);
}