From 9c3a93fb9365ff163794cb8432f4f97e0fc72ff6 Mon Sep 17 00:00:00 2001 From: Michael Milton Date: Tue, 3 Aug 2021 02:23:37 +1000 Subject: [PATCH 1/6] Add generic pow method --- src/numeric/impl_numeric.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/numeric/impl_numeric.rs b/src/numeric/impl_numeric.rs index da170c9e7..5871feff4 100644 --- a/src/numeric/impl_numeric.rs +++ b/src/numeric/impl_numeric.rs @@ -8,7 +8,7 @@ #[cfg(feature = "std")] use num_traits::Float; -use num_traits::{self, FromPrimitive, Zero}; +use num_traits::{self, FromPrimitive, Zero, PrimInt, Pow}; use std::ops::{Add, Div, Mul}; use crate::imp_prelude::*; @@ -418,4 +418,39 @@ where { self.var_axis(axis, ddof).mapv_into(|x| x.sqrt()) } + + /// Raises each item in the array to the same power + /// + /// # Example + /// ``` + /// use ndarray::{array}; + /// + /// let a = array![ + /// [1., 2.], + /// [3., 4.], + /// ]; + /// assert_eq!(a.pow(2.), array![ + /// [1., 4.], + /// [9., 16.], + /// ]); + /// ``` + /// ``` + /// use ndarray::{array}; + /// + /// let a = array![ + /// [1., 4.], + /// [9., 16.], + /// ]; + /// assert_eq!(a.pow(0.5), array![ + /// [1., 2.], + /// [3., 4.], + /// ]); + /// ``` + pub fn pow(&self, pow: B) -> Array<>::Output, D> + where + A: Pow + Clone, + B: Copy + { + self.mapv(|a| a.pow(pow)) + } } From 88b4ff7b34d1faa902e5938336a9f96ecbb902e7 Mon Sep 17 00:00:00 2001 From: Michael Milton Date: Tue, 3 Aug 2021 02:25:01 +1000 Subject: [PATCH 2/6] Remove unused trait --- src/numeric/impl_numeric.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/numeric/impl_numeric.rs b/src/numeric/impl_numeric.rs index 5871feff4..277f2f60c 100644 --- a/src/numeric/impl_numeric.rs +++ b/src/numeric/impl_numeric.rs @@ -8,7 +8,7 @@ #[cfg(feature = "std")] use num_traits::Float; -use num_traits::{self, FromPrimitive, Zero, PrimInt, Pow}; +use num_traits::{self, FromPrimitive, Zero, Pow}; use std::ops::{Add, Div, Mul}; use crate::imp_prelude::*; From c4019e690fcb4c3aaa75905acdd566a5706712a3 Mon Sep 17 00:00:00 2001 From: Michael Milton Date: Tue, 3 Aug 2021 11:07:41 +1000 Subject: [PATCH 3/6] Require std feature --- src/numeric/impl_numeric.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/numeric/impl_numeric.rs b/src/numeric/impl_numeric.rs index 277f2f60c..64e57cbd4 100644 --- a/src/numeric/impl_numeric.rs +++ b/src/numeric/impl_numeric.rs @@ -426,26 +426,27 @@ where /// use ndarray::{array}; /// /// let a = array![ - /// [1., 2.], - /// [3., 4.], + /// [1, 2], + /// [3, 4], /// ]; - /// assert_eq!(a.pow(2.), array![ - /// [1., 4.], - /// [9., 16.], + /// assert_eq!(a.pow(2), array![ + /// [1, 4], + /// [9, 16], /// ]); /// ``` /// ``` /// use ndarray::{array}; /// /// let a = array![ - /// [1., 4.], - /// [9., 16.], + /// [1, 4], + /// [9, 16], /// ]; /// assert_eq!(a.pow(0.5), array![ - /// [1., 2.], - /// [3., 4.], + /// [1, 2], + /// [3, 4], /// ]); /// ``` + #[cfg(feature = "std")] pub fn pow(&self, pow: B) -> Array<>::Output, D> where A: Pow + Clone, From 56afb80c88b732f2c5ef3bfa784a0337e97e6721 Mon Sep 17 00:00:00 2001 From: Michael Milton Date: Tue, 3 Aug 2021 11:41:21 +1000 Subject: [PATCH 4/6] Only use Pow trait when std feature enabled --- src/numeric/impl_numeric.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/numeric/impl_numeric.rs b/src/numeric/impl_numeric.rs index 64e57cbd4..bcd690c8e 100644 --- a/src/numeric/impl_numeric.rs +++ b/src/numeric/impl_numeric.rs @@ -7,8 +7,8 @@ // except according to those terms. #[cfg(feature = "std")] -use num_traits::Float; -use num_traits::{self, FromPrimitive, Zero, Pow}; +use num_traits::{Float, Pow}; +use num_traits::{self, FromPrimitive, Zero}; use std::ops::{Add, Div, Mul}; use crate::imp_prelude::*; From ea66e3ba38405808ad55f54682d6690e5e4d5a51 Mon Sep 17 00:00:00 2001 From: Michael Milton Date: Tue, 3 Aug 2021 11:53:26 +1000 Subject: [PATCH 5/6] Can't raise int to int power --- src/numeric/impl_numeric.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/numeric/impl_numeric.rs b/src/numeric/impl_numeric.rs index bcd690c8e..421e9fafa 100644 --- a/src/numeric/impl_numeric.rs +++ b/src/numeric/impl_numeric.rs @@ -426,24 +426,24 @@ where /// use ndarray::{array}; /// /// let a = array![ - /// [1, 2], - /// [3, 4], + /// [1., 2.], + /// [3., 4.], /// ]; /// assert_eq!(a.pow(2), array![ - /// [1, 4], - /// [9, 16], + /// [1., 4.], + /// [9., 16.], /// ]); /// ``` /// ``` /// use ndarray::{array}; /// /// let a = array![ - /// [1, 4], - /// [9, 16], + /// [1., 4.], + /// [9., 16.], /// ]; /// assert_eq!(a.pow(0.5), array![ - /// [1, 2], - /// [3, 4], + /// [1., 2.], + /// [3., 4.], /// ]); /// ``` #[cfg(feature = "std")] From 193c98764093cb5e796d3d39b5ce078b8defc943 Mon Sep 17 00:00:00 2001 From: Michael Milton Date: Tue, 3 Aug 2021 12:10:27 +1000 Subject: [PATCH 6/6] Update numpy docs to use new pow method --- src/doc/ndarray_for_numpy_users/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/doc/ndarray_for_numpy_users/mod.rs b/src/doc/ndarray_for_numpy_users/mod.rs index bedd1c8b5..246a3724f 100644 --- a/src/doc/ndarray_for_numpy_users/mod.rs +++ b/src/doc/ndarray_for_numpy_users/mod.rs @@ -390,7 +390,7 @@ //! //! //! -//! [`a.mapv(|a| a.powi(3))`][.mapv()] +//! [`a.pow(3)`][.pow()] //! //! //! @@ -404,7 +404,7 @@ //! //! //! -//! [`a.mapv(f64::sqrt)`][.mapv()] +//! [`a.pow(0.5)`][.pow()] //! //! //! @@ -627,6 +627,7 @@ //! [NdProducer]: ../../trait.NdProducer.html //! [::ones()]: ../../struct.ArrayBase.html#method.ones //! [.outer_iter()]: ../../struct.ArrayBase.html#method.outer_iter +//! [.pow()]: ../../struct.ArrayBase.html#method.pow //! [::range()]: ../../struct.ArrayBase.html#method.range //! [.raw_dim()]: ../../struct.ArrayBase.html#method.raw_dim //! [.reversed_axes()]: ../../struct.ArrayBase.html#method.reversed_axes