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 diff --git a/src/numeric/impl_numeric.rs b/src/numeric/impl_numeric.rs index da170c9e7..421e9fafa 100644 --- a/src/numeric/impl_numeric.rs +++ b/src/numeric/impl_numeric.rs @@ -7,7 +7,7 @@ // except according to those terms. #[cfg(feature = "std")] -use num_traits::Float; +use num_traits::{Float, Pow}; use num_traits::{self, FromPrimitive, Zero}; use std::ops::{Add, Div, Mul}; @@ -418,4 +418,40 @@ 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.], + /// ]); + /// ``` + #[cfg(feature = "std")] + pub fn pow(&self, pow: B) -> Array<>::Output, D> + where + A: Pow + Clone, + B: Copy + { + self.mapv(|a| a.pow(pow)) + } }