From a59d48cec38d917f037509845a9916989b519d88 Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Tue, 23 Jul 2019 12:49:42 +0200 Subject: [PATCH 1/6] ENH Add Array2::from_diag --- src/impl_constructors.rs | 14 ++++++++++++++ tests/array.rs | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index 2355a1249..062170470 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -171,6 +171,20 @@ where } eye } + + /// Create a 2D matrix from its diagonal + pub fn from_diag(diag: &ArrayBase) -> Self + where + S: DataMut, + A: Clone + Zero + One, + { + let mut eye = Self::zeros((diag.len(), diag.len())); + for (idx, val) in diag.iter().enumerate() { + eye[[idx, idx]] = val.clone(); + } + eye + } + } #[cfg(not(debug_assertions))] diff --git a/tests/array.rs b/tests/array.rs index 7de26eefb..d6d610d25 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -1954,6 +1954,16 @@ fn test_array_clone_same_view() { assert_eq!(a, b); } + +#[test] +fn test_array2_from_diag() { + let diag = arr1(&[0, 1, 2]); + let x = Array2::from_diag(&diag); + let x_exp = arr2(&[[0, 0, 0], [0, 1, 0], [0, 0, 2]]); + assert_eq!(x, x_exp); +} + + #[test] fn array_macros() { // array From 0c982f8da02a377787997e99d867252489c1521f Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Tue, 23 Jul 2019 12:56:45 +0200 Subject: [PATCH 2/6] Lint --- src/impl_constructors.rs | 1 - tests/array.rs | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index 062170470..14473623f 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -184,7 +184,6 @@ where } eye } - } #[cfg(not(debug_assertions))] diff --git a/tests/array.rs b/tests/array.rs index d6d610d25..93e43a41d 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -1954,7 +1954,6 @@ fn test_array_clone_same_view() { assert_eq!(a, b); } - #[test] fn test_array2_from_diag() { let diag = arr1(&[0, 1, 2]); @@ -1963,7 +1962,6 @@ fn test_array2_from_diag() { assert_eq!(x, x_exp); } - #[test] fn array_macros() { // array From a2dbf71b1dffb782d9a1a615eb1f78f3872124fb Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Tue, 23 Jul 2019 16:29:10 +0200 Subject: [PATCH 3/6] Address review comments --- src/impl_constructors.rs | 14 +++++++------- tests/array.rs | 6 ++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index 14473623f..63b653593 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -173,16 +173,16 @@ where } /// Create a 2D matrix from its diagonal - pub fn from_diag(diag: &ArrayBase) -> Self + pub fn from_diag(diag: &ArrayBase) -> Self where + A: Clone + Zero, S: DataMut, - A: Clone + Zero + One, + S2: Data, { - let mut eye = Self::zeros((diag.len(), diag.len())); - for (idx, val) in diag.iter().enumerate() { - eye[[idx, idx]] = val.clone(); - } - eye + let n = diag.len(); + let mut arr = Self::zeros((n, n)); + arr.diag_mut().assign(&diag); + arr } } diff --git a/tests/array.rs b/tests/array.rs index 93e43a41d..de7b0a721 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -1960,6 +1960,12 @@ fn test_array2_from_diag() { let x = Array2::from_diag(&diag); let x_exp = arr2(&[[0, 0, 0], [0, 1, 0], [0, 0, 2]]); assert_eq!(x, x_exp); + + // check 0 length array + let diag = Array1::::zeros(0); + let x = Array2::from_diag(&diag); + assert_eq!(x.ndim(), 2); + assert_eq!(x.shape(), [0, 0]); } #[test] From 9797796438456b21232a160bbf3e97fbea68ee7f Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Wed, 24 Jul 2019 11:48:40 +0200 Subject: [PATCH 4/6] Add example and panic conditions --- src/impl_constructors.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index 63b653593..6c0f7a1a3 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -173,6 +173,17 @@ where } /// Create a 2D matrix from its diagonal + /// + /// **Panics** if `diag.len() * diag.len()` would overflow `isize`. + /// + /// ```rust + /// use ndarray::{Array2, arr1, arr2}; + /// + /// # #[cfg(feature = "approx")] { + /// let diag = arr1(&[1, 2]); + /// let array = Array2::from_diag(&diag); + /// assert_eq!(array, arr2(&[[1, 0], [0, 2]])); + /// # } pub fn from_diag(diag: &ArrayBase) -> Self where A: Clone + Zero, From 100c80607ef4b9616a45b6e3312525b117dbcddb Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Thu, 25 Jul 2019 23:00:10 +0200 Subject: [PATCH 5/6] Remove cfg(feature = "approx") --- src/impl_constructors.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index 6c0f7a1a3..78f62ed91 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -179,11 +179,9 @@ where /// ```rust /// use ndarray::{Array2, arr1, arr2}; /// - /// # #[cfg(feature = "approx")] { /// let diag = arr1(&[1, 2]); /// let array = Array2::from_diag(&diag); /// assert_eq!(array, arr2(&[[1, 0], [0, 2]])); - /// # } pub fn from_diag(diag: &ArrayBase) -> Self where A: Clone + Zero, From b23a2a919b50e35f7ffd0ed2154e093f92cea536 Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Fri, 2 Aug 2019 13:55:30 -0400 Subject: [PATCH 6/6] Add missing closing code fence --- src/impl_constructors.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index 78f62ed91..f8589f595 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -182,6 +182,7 @@ where /// let diag = arr1(&[1, 2]); /// let array = Array2::from_diag(&diag); /// assert_eq!(array, arr2(&[[1, 0], [0, 2]])); + /// ``` pub fn from_diag(diag: &ArrayBase) -> Self where A: Clone + Zero,