Give MathCell arithmetic ops implementations when MathCell is left value by SparrowLii · Pull Request #1011 · rust-ndarray/ndarray · GitHub
Skip to content
Open
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
82 changes: 82 additions & 0 deletions src/impl_methods.rs
83 changes: 83 additions & 0 deletions src/impl_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,51 @@ impl<'a, A, S, D, B> $trt<B> for &'a ArrayBase<S, D>
self.map(move |elt| elt.clone() $operator x.clone())
}
}

/// Perform elementwise
#[doc=$doc]
/// between `self` and `rhs`,
/// and return the result.
///
/// `self` must be a view of `MathCell`.
///
/// If their shapes disagree, `rhs` is broadcast to the shape of `self`.
///
/// **Panics** if broadcasting isn’t possible.
impl<'a, A, B, S, D, E> $trt<&'a ArrayBase<S, E>> for ArrayView<'a, MathCell<A>, D>
where
A: Copy + $trt<B, Output=A>,
B: Clone,
S: Data<Elem=B>,
D: Dimension,
E: Dimension,
{
type Output = ArrayView<'a, MathCell<A>, D>;
fn $mth(self, rhs: &ArrayBase<S, E>) -> Self::Output
{
self.zip_cell_with(rhs, |x, y| x.clone() $operator y.clone());
self
}
}

/// Perform elementwise
#[doc=$doc]
/// between `self` and the scalar `x`,
/// and return the result (based on `self`).
///
/// `self` must be a view of `MathCell`.
impl<'a, A, D, B> $trt<B> for ArrayView<'a, MathCell<A>, D>
where
A: Copy + $trt<B, Output=A>,
D: Dimension,
B: ScalarOperand,
{
type Output = ArrayView<'a, MathCell<A>, D>;
fn $mth(self, y: B) -> ArrayView<'a, MathCell<A>, D> {
self.zip_cell_with_elem(&y, |x, y| x.clone() $operator y.clone());
self
}
}
);
);

Expand Down Expand Up @@ -287,6 +332,7 @@ impl<'a, S, D> $trt<&'a ArrayBase<S, D>> for $scalar
mod arithmetic_ops {
use super::*;
use crate::imp_prelude::*;
use crate::MathCell;

use num_complex::Complex;
use std::ops::*;
Expand Down Expand Up @@ -429,6 +475,7 @@ mod arithmetic_ops {
mod assign_ops {
use super::*;
use crate::imp_prelude::*;
use crate::MathCell;

macro_rules! impl_assign_op {
($trt:ident, $method:ident, $doc:expr) => {
Expand Down Expand Up @@ -466,6 +513,42 @@ mod assign_ops {
});
}
}

#[doc=$doc]
/// If their shapes disagree, `rhs` is broadcast to the shape of `self`.
///
/// **Panics** if broadcasting isn’t possible.
impl<'a, A, B, S, D, E> $trt<&'a ArrayBase<S, E>> for ArrayView<'a, MathCell<A>, D>
where
A: Copy + $trt<B>,
B: Clone,
S: Data<Elem = B>,
D: Dimension,
E: Dimension,
{
fn $method(&mut self, rhs: &ArrayBase<S, E>) {
self.zip_cell_with(rhs, |x, y| {
let mut x = x.clone();
x.$method(y.clone());
x
});
}
}

#[doc=$doc]
impl<'a, A, D> $trt<A> for ArrayView<'a, MathCell<A>, D>
where
A: Copy + ScalarOperand + $trt<A>,
D: Dimension,
{
fn $method(&mut self, rhs: A) {
self.zip_cell_with_elem(&rhs, |x, y| {
let mut x = x.clone();
x.$method(y.clone());
x
});
}
}
};
}

Expand Down
47 changes: 46 additions & 1 deletion src/math_cell.rs