Skip to content

Implement Neg and Not for &ArrayBase #374

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 3 commits into from
Oct 31, 2017
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
26 changes: 26 additions & 0 deletions src/impl_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ mod arithmetic_ops {
}
}

impl<'a, A, S, D> Neg for &'a ArrayBase<S, D>
where &'a A: 'a + Neg<Output=A>,
S: Data<Elem=A>,
D: Dimension
{
type Output = Array<A, D>;
/// Perform an elementwise negation of reference `self` and return the
/// result as a new `Array`.
fn neg(self) -> Array<A, D> {
self.map(Neg::neg)
}
}

impl<A, S, D> Not for ArrayBase<S, D>
where A: Clone + Not<Output=A>,
S: DataOwned<Elem=A> + DataMut,
Expand All @@ -306,6 +319,19 @@ mod arithmetic_ops {
self
}
}

impl<'a, A, S, D> Not for &'a ArrayBase<S, D>
where &'a A: 'a + Not<Output=A>,
S: Data<Elem=A>,
D: Dimension
{
type Output = Array<A, D>;
/// Perform an elementwise unary not of reference `self` and return the
/// result as a new `Array`.
fn not(self) -> Array<A, D> {
self.map(Not::not)
}
}
}

mod assign_ops {
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ pub type Ixs = isize;
///
/// Since the trait implementations are hard to overview, here is a summary.
///
/// ### Binary Operators with Two Arrays
///
/// Let `A` be an array or view of any kind. Let `B` be an array
/// with owned storage (either `Array` or `RcArray`).
/// Let `C` be an array with mutable data (either `Array`, `RcArray`
Expand All @@ -542,6 +544,8 @@ pub type Ixs = isize;
/// - `B @ &A` which consumes `B`, updates it with the result, and returns it
/// - `C @= &A` which performs an arithmetic operation in place
///
/// ### Binary Operators with Array and Scalar
///
/// The trait [`ScalarOperand`](trait.ScalarOperand.html) marks types that can be used in arithmetic
/// with arrays directly. For a scalar `K` the following combinations of operands
/// are supported (scalar can be on either the left or right side, but
Expand All @@ -551,6 +555,15 @@ pub type Ixs = isize;
/// - `B @ K` or `K @ B` which consumes `B`, updates it with the result and returns it
/// - `C @= K` which performs an arithmetic operation in place
///
/// ### Unary Operators
///
/// Let `A` be an array or view of any kind. Let `B` be an array with owned
/// storage (either `Array` or `RcArray`). The following operands are supported
/// for an arbitrary unary operator denoted by `@` (it can be `-` or `!`).
///
/// - `@&A` which produces a new `Array`
/// - `@B` which consumes `B`, updates it with the result, and returns it
///
/// ## Broadcasting
///
/// Arrays support limited *broadcasting*, where arithmetic operations with
Expand Down
15 changes: 10 additions & 5 deletions tests/oper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,32 @@ use ndarray::Si;
use ndarray::{Ix, Ixs};

use std::fmt;
use std::ops::Neg;
use num_traits::Float;

fn test_oper(op: &str, a: &[f32], b: &[f32], c: &[f32])
{
let aa = rcarr1(a);
let bb = rcarr1(b);
let cc = rcarr1(c);
test_oper_arr(op, aa.clone(), bb.clone(), cc.clone());
test_oper_arr::<f32, _>(op, aa.clone(), bb.clone(), cc.clone());
let dim = (2, 2);
let aa = aa.reshape(dim);
let bb = bb.reshape(dim);
let cc = cc.reshape(dim);
test_oper_arr(op, aa.clone(), bb.clone(), cc.clone());
test_oper_arr::<f32, _>(op, aa.clone(), bb.clone(), cc.clone());
let dim = (1, 2, 1, 2);
let aa = aa.reshape(dim);
let bb = bb.reshape(dim);
let cc = cc.reshape(dim);
test_oper_arr(op, aa.clone(), bb.clone(), cc.clone());
test_oper_arr::<f32, _>(op, aa.clone(), bb.clone(), cc.clone());
}

fn test_oper_arr<A: NdFloat + fmt::Debug, D: ndarray::Dimension>
(op: &str, mut aa: RcArray<A,D>, bb: RcArray<A, D>, cc: RcArray<A, D>)
fn test_oper_arr<A, D>(op: &str, mut aa: RcArray<A, D>, bb: RcArray<A, D>, cc: RcArray<A, D>)
where
A: NdFloat,
for<'a> &'a A: Neg<Output=A>,
D: Dimension,
{
match op {
"+" => {
Expand Down Expand Up @@ -61,6 +65,7 @@ fn test_oper_arr<A: NdFloat + fmt::Debug, D: ndarray::Dimension>
assert_eq!(aa, cc);
},
"neg" => {
assert_eq!(-&aa, cc);
assert_eq!(-aa.clone(), cc);
},
_ => panic!()
Expand Down