Skip to content

Commit cc8cb29

Browse files
committed
Merge pull request #5 from bluss/rendarray
Rendarray
2 parents df6434b + c02049b commit cc8cb29

File tree

4 files changed

+103
-24
lines changed

4 files changed

+103
-24
lines changed

Cargo.toml

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
[package]
22

3-
name = "ndarray"
4-
version = "0.0.1"
3+
name = "rendarray"
4+
version = "0.1.0"
55
authors = ["bluss"]
66
repository = "https://github.com/bluss/rust-ndarray"
77
documentation = "http://bluss.github.io/rust-ndarray/"
88

9+
[lib]
10+
name = "ndarray"
11+
912
[dependencies.num]
1013
version = "0.1"
1114
features = ["complex"]
@@ -22,3 +25,7 @@ version = "0.4"
2225
version = "0.4"
2326
optional = true
2427

28+
29+
[features]
30+
31+
assign_ops = []

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ DOCCRATES = ndarray
33
# deps to delete the generated docs
44
RMDOCS =
55

6-
FEATURES =
6+
FEATURES = assign_ops
77

88
VERSIONS = $(patsubst %,target/VERS/%,$(DOCCRATES))
99

@@ -12,7 +12,7 @@ docs: mkdocs subst $(RMDOCS)
1212
# https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
1313
$(VERSIONS): Cargo.toml
1414
mkdir -p $(@D)
15-
cargo pkgid $(@F) | sed -e "s/.*#\(\|.*:\)//" > "$@"
15+
cargo pkgid rendarray | sed -e "s/.*#\(\|.*:\)//" > "$@"
1616

1717
$(DOCCRATES): %: target/VERS/%
1818
# Put in the crate version into the docs

custom.css

+5
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ pre.trait .where::before {
1818
content: '\a ';
1919
}
2020

21+
.docblock code {
22+
background-color: inherit;
23+
font-weight: bold;
24+
padding: 0 0.1em;
25+
}

src/lib.rs

+87-20
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
//! or linear algebra. `Array` is a good container.
1616
//! - There is no integration with linear algebra packages (at least not yet).
1717
//!
18+
//! ## Crate feature flags
19+
//!
20+
//! - `assign_ops`
21+
//! - Optional, requires nightly
22+
//! - Enables the compound assignment operators
23+
//!
24+
#![cfg_attr(feature = "assign_ops", feature(augmented_assignments,
25+
op_assign_traits))]
1826

1927
#[cfg(feature = "serde")]
2028
extern crate serde;
@@ -42,6 +50,7 @@ pub use indexes::Indexes;
4250

4351
use iterators::Baseiter;
4452

53+
4554
pub mod linalg;
4655
mod arraytraits;
4756
#[cfg(feature = "serde")]
@@ -1102,18 +1111,18 @@ impl<A, D> Array<A, D> where
11021111
}
11031112
}
11041113

1105-
impl<'a, A, D, E> $trt<Array<A, E>> for Array<A, D> where
1106-
A: Clone + $trt<A, Output=A>,
1107-
D: Dimension,
1108-
E: Dimension,
1114+
/// Perform an elementwise arithmetic operation between **self** and **other**,
1115+
/// and return the result.
1116+
///
1117+
/// If their shapes disagree, **other** is broadcast to the shape of **self**.
1118+
///
1119+
/// **Panics** if broadcasting isn't possible.
1120+
impl<'a, A, D, E> $trt<Array<A, E>> for Array<A, D>
1121+
where A: Clone + $trt<A, Output=A>,
1122+
D: Dimension,
1123+
E: Dimension,
11091124
{
11101125
type Output = Array<A, D>;
1111-
/// Perform an elementwise arithmetic operation between **self** and **other**,
1112-
/// and return the result.
1113-
///
1114-
/// If their shapes disagree, **other** is broadcast to the shape of **self**.
1115-
///
1116-
/// **Panics** if broadcasting isn't possible.
11171126
fn $mth (mut self, other: Array<A, E>) -> Array<A, D>
11181127
{
11191128
// FIXME: Can we co-broadcast arrays here? And how?
@@ -1131,18 +1140,18 @@ impl<'a, A, D, E> $trt<Array<A, E>> for Array<A, D> where
11311140
}
11321141
}
11331142

1134-
impl<'a, A, D, E> $trt<&'a Array<A, E>> for &'a Array<A, D> where
1135-
A: Clone + $trt<A, Output=A>,
1136-
D: Dimension,
1137-
E: Dimension,
1143+
/// Perform an elementwise arithmetic operation between **self** and **other**,
1144+
/// and return the result.
1145+
///
1146+
/// If their shapes disagree, **other** is broadcast to the shape of **self**.
1147+
///
1148+
/// **Panics** if broadcasting isn't possible.
1149+
impl<'a, A, D, E> $trt<&'a Array<A, E>> for &'a Array<A, D>
1150+
where A: Clone + $trt<A, Output=A>,
1151+
D: Dimension,
1152+
E: Dimension,
11381153
{
11391154
type Output = Array<A, D>;
1140-
/// Perform an elementwise arithmetic operation between **self** and **other**,
1141-
/// and return the result.
1142-
///
1143-
/// If their shapes disagree, **other** is broadcast to the shape of **self**.
1144-
///
1145-
/// **Panics** if broadcasting isn't possible.
11461155
fn $mth (self, other: &'a Array<A, E>) -> Array<A, D>
11471156
{
11481157
// FIXME: Can we co-broadcast arrays here? And how?
@@ -1176,6 +1185,64 @@ impl_binary_op!(BitXor, bitxor, ibitxor, ibitxor_scalar);
11761185
impl_binary_op!(Shl, shl, ishl, ishl_scalar);
11771186
impl_binary_op!(Shr, shr, ishr, ishr_scalar);
11781187

1188+
#[cfg(feature = "assign_ops")]
1189+
mod assign_ops {
1190+
use super::*;
1191+
1192+
use std::ops::{
1193+
AddAssign,
1194+
SubAssign,
1195+
MulAssign,
1196+
DivAssign,
1197+
RemAssign,
1198+
BitAndAssign,
1199+
BitOrAssign,
1200+
BitXorAssign,
1201+
};
1202+
1203+
1204+
macro_rules! impl_assign_op {
1205+
($trt:ident, $method:ident) => {
1206+
1207+
/// Perform an elementwise in place arithmetic operation between **self** and **other**,
1208+
///
1209+
/// If their shapes disagree, **other** is broadcast to the shape of **self**.
1210+
///
1211+
/// **Panics** if broadcasting isn't possible.
1212+
///
1213+
/// **Requires `feature = "assign_ops"`**
1214+
impl<'a, A, D, E> $trt<&'a Array<A, E>> for Array<A, D>
1215+
where A: Clone + $trt<A>,
1216+
D: Dimension,
1217+
E: Dimension,
1218+
{
1219+
fn $method(&mut self, other: &Array<A, E>) {
1220+
if self.shape() == other.shape() {
1221+
for (x, y) in self.iter_mut().zip(other.iter()) {
1222+
x.$method(y.clone());
1223+
}
1224+
} else {
1225+
let other_iter = other.broadcast_iter_unwrap(self.dim());
1226+
for (x, y) in self.iter_mut().zip(other_iter) {
1227+
x.$method(y.clone());
1228+
}
1229+
}
1230+
}
1231+
}
1232+
1233+
};
1234+
}
1235+
1236+
impl_assign_op!(AddAssign, add_assign);
1237+
impl_assign_op!(SubAssign, sub_assign);
1238+
impl_assign_op!(MulAssign, mul_assign);
1239+
impl_assign_op!(DivAssign, div_assign);
1240+
impl_assign_op!(RemAssign, rem_assign);
1241+
impl_assign_op!(BitAndAssign, bitand_assign);
1242+
impl_assign_op!(BitOrAssign, bitor_assign);
1243+
impl_assign_op!(BitXorAssign, bitxor_assign);
1244+
}
1245+
11791246
impl<A: Clone + Neg<Output=A>, D: Dimension>
11801247
Array<A, D>
11811248
{

0 commit comments

Comments
 (0)