Repository: bluss/rust-ndarray (rust)

Issue:
Replace ArrayBase::assign_scalar with ArrayBase::fill
Is the shorter name worth it? The longer name has some consistency (but not much).

cc @SuperFluffy  I heard you wanted to change .assign_scalar. My suggestion is to just deprecate it (without changing it), and adding .fill() instead, that has the signature that you wanted.

Pull request description:
Implement ArrayBase::fill
Also changed the tests and depracated `ArrayBase::assign_scalar`.

Fixes #235 

Patch:
diff --git a/benches/bench1.rs b/benches/bench1.rs
index 4f1b2d6b..5aab12d3 100644
--- a/benches/bench1.rs
+++ b/benches/bench1.rs
@@ -462,7 +462,7 @@ fn assign_scalar_2d_corder(bench: &mut test::Bencher)
     let a = Array::zeros((64, 64));
     let mut a = black_box(a);
     let s = 3.;
-    bench.iter(move || a.assign_scalar(&s))
+    bench.iter(move || a.fill(s))
 }
 
 #[bench]
@@ -472,7 +472,7 @@ fn assign_scalar_2d_cutout(bench: &mut test::Bencher)
     let a = a.slice_mut(s![1..-1, 1..-1]);
     let mut a = black_box(a);
     let s = 3.;
-    bench.iter(move || a.assign_scalar(&s))
+    bench.iter(move || a.fill(s))
 }
 
 #[bench]
@@ -482,7 +482,7 @@ fn assign_scalar_2d_forder(bench: &mut test::Bencher)
     a.swap_axes(0, 1);
     let mut a = black_box(a);
     let s = 3.;
-    bench.iter(move || a.assign_scalar(&s))
+    bench.iter(move || a.fill(s))
 }
 
 #[bench]
@@ -490,7 +490,7 @@ fn assign_zero_2d_corder(bench: &mut test::Bencher)
 {
     let a = Array::zeros((64, 64));
     let mut a = black_box(a);
-    bench.iter(|| a.assign_scalar(&0.))
+    bench.iter(|| a.fill(0.))
 }
 
 #[bench]
@@ -499,7 +499,7 @@ fn assign_zero_2d_cutout(bench: &mut test::Bencher)
     let mut a = Array::zeros((66, 66));
     let a = a.slice_mut(s![1..-1, 1..-1]);
     let mut a = black_box(a);
-    bench.iter(|| a.assign_scalar(&0.))
+    bench.iter(|| a.fill(0.))
 }
 
 #[bench]
@@ -508,7 +508,7 @@ fn assign_zero_2d_forder(bench: &mut test::Bencher)
     let mut a = Array::zeros((64, 64));
     a.swap_axes(0, 1);
     let mut a = black_box(a);
-    bench.iter(|| a.assign_scalar(&0.))
+    bench.iter(|| a.fill(0.))
 }
 
 #[bench]
diff --git a/examples/life.rs b/examples/life.rs
index 292caad3..fdeff61f 100644
--- a/examples/life.rs
+++ b/examples/life.rs
@@ -35,7 +35,7 @@ fn parse(x: &[u8]) -> Board {
 fn iterate(z: &mut Board, scratch: &mut Board) {
     // compute number of neighbors
     let mut neigh = scratch.view_mut();
-    neigh.assign_scalar(&0);
+    neigh.fill(0);
     neigh += &z.slice(s![0..-2, 0..-2]);
     neigh += &z.slice(s![0..-2, 1..-1]);
     neigh += &z.slice(s![0..-2, 2..  ]);
diff --git a/src/free_functions.rs b/src/free_functions.rs
index deae3775..105a2903 100644
--- a/src/free_functions.rs
+++ b/src/free_functions.rs
@@ -76,7 +76,7 @@ pub fn aview2<A, V: FixedInitializer<Elem=A>>(xs: &[V]) -> ArrayView2<A> {
 ///     let mut data = [0; 1024];
 ///     {
 ///         let mut a = aview_mut1(&mut data).into_shape((32, 32)).unwrap();
-///         a.slice_mut(s![.., ..;3]).assign_scalar(&5);
+///         a.slice_mut(s![.., ..;3]).fill(5);
 ///     }
 ///     assert_eq!(&data[..10], [5, 0, 0, 5, 0, 0, 5, 0, 0, 5]);
 /// }
diff --git a/src/impl_methods.rs b/src/impl_methods.rs
index 5b936f4a..cf9f9398 100644
--- a/src/impl_methods.rs
+++ b/src/impl_methods.rs
@@ -1006,12 +1006,20 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
     }
 
     /// Perform an elementwise assigment to `self` from scalar `x`.
+    #[deprecated(note="This method is replaced with ArrayBase::fill and will be removed.")]
     pub fn assign_scalar(&mut self, x: &A)
         where S: DataMut, A: Clone,
     {
         self.unordered_foreach_mut(move |elt| *elt = x.clone());
     }
 
+    /// Perform an elementwise assigment to `self` from element `x`.
+    pub fn fill(&mut self, x: A)
+        where S: DataMut, A: Clone,
+    {
+        self.unordered_foreach_mut(move |elt| *elt = x.clone());
+    }
+
     fn zip_mut_with_same_shape<B, S2, E, F>(&mut self, rhs: &ArrayBase<S2, E>, mut f: F)
         where S: DataMut,
               S2: Data<Elem=B>,
diff --git a/src/linalg/impl_linalg.rs b/src/linalg/impl_linalg.rs
index 0b604fb4..92ec07ee 100644
--- a/src/linalg/impl_linalg.rs
+++ b/src/linalg/impl_linalg.rs
@@ -445,7 +445,7 @@ fn mat_mul_general<A>(alpha: A,
     } else {
         // initialize memory if beta is zero
         if beta.is_zero() {
-            c.assign_scalar(&beta);
+            c.fill(beta);
         }
 
         let mut i = 0;


Test patch:
diff --git a/ndarray-rblas/tests/blas.rs b/ndarray-rblas/tests/blas.rs
index 481b562f..110a17e4 100644
--- a/ndarray-rblas/tests/blas.rs
+++ b/ndarray-rblas/tests/blas.rs
@@ -151,13 +151,13 @@ fn test_dot() {
 
     let b = a.clone();
     let mut res = a.clone();
-    res.assign_scalar(&0.);
+    res.fill(0.);
 
     dot(a.view(), b.view(), &mut res.view_mut()).unwrap();
     println!("{:?}", res);
 
     a.swap_axes(0, 1);
-    res.assign_scalar(&0.);
+    res.fill(0.);
 
     let result = dot(a.view(), b.view(), &mut res.view_mut());
     assert!(result.is_err());
diff --git a/tests/array.rs b/tests/array.rs
index 173567d8..88e3e56e 100644
--- a/tests/array.rs
+++ b/tests/array.rs
@@ -308,7 +308,7 @@ fn assign()
     {
         let mut v = a.view_mut();
         v.islice(&[Si(0, Some(1), 1), S]);
-        v.assign_scalar(&0);
+        v.fill(0);
     }
     assert_eq!(a, arr2(&[[0, 0], [3, 4]]));
 }
diff --git a/tests/oper.rs b/tests/oper.rs
index fbf36cce..4a20ec26 100644
--- a/tests/oper.rs
+++ b/tests/oper.rs
@@ -87,7 +87,7 @@ fn scalar_operations()
         let mut x = a.clone();
         let mut y = arr0(0.);
         x += 1.;
-        y.assign_scalar(&2.);
+        y.fill(2.);
         assert_eq!(x, a + arr0(1.));
         assert_eq!(x, y);
     }
@@ -96,7 +96,7 @@ fn scalar_operations()
         let mut x = b.clone();
         let mut y = rcarr1(&[0., 0.]);
         x += 1.;
-        y.assign_scalar(&2.);
+        y.fill(2.);
         assert_eq!(x, b + arr0(1.));
         assert_eq!(x, y);
     }
@@ -105,7 +105,7 @@ fn scalar_operations()
         let mut x = c.clone();
         let mut y = RcArray::zeros((2, 2));
         x += 1.;
-        y.assign_scalar(&2.);
+        y.fill(2.);
         assert_eq!(x, c + arr0(1.));
         assert_eq!(x, y);
     }