Skip to content

Implement float math functions for two arrays #1516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/numeric/impl_float_maths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#[cfg(feature = "std")]
use num_traits::Float;
#[cfg(feature = "std")]
use std::borrow::Borrow;

use crate::imp_prelude::*;

Expand Down Expand Up @@ -50,6 +52,18 @@ macro_rules! binary_ops {
};
}

#[cfg(feature = "std")]
macro_rules! binary_ops_array {
($($(#[$meta:meta])* fn $fn_use:ident($ty:ty) as $fn_name:ident)+) => {
$($(#[$meta])*
#[must_use = "method returns a new array and does not mutate the original value"]
pub fn $fn_name<P>(&self, rhs: P) -> Array<A, D>
where P: $crate::IntoNdProducer<Dim = D>, <P as $crate::IntoNdProducer>::Item: Borrow<$ty> {
$crate::Zip::from(self).and(rhs).map_collect(|a, b| A::$fn_use(*a, *b.borrow()))
})+
};
}

/// # Element-wise methods for float arrays
///
/// Element-wise math functions for any array type that contains float number.
Expand Down Expand Up @@ -157,6 +171,24 @@ where
fn abs_sub(A)
/// Length of the hypotenuse of a right-angle triangle of each element
fn hypot(A)
/// Four quadrant arctangent of each element in radians.
fn atan2(A)
}
binary_ops_array! {
/// Integer power of each element
///
/// This function is generally faster than using float power.
fn powi(i32) as powi_all
/// Float power of each element.
fn powf(A) as powf_all
/// Logarithm of each element with respect to an arbitrary base.
fn log(A) as log_all
/// The positive difference between given number and each element.
fn abs_sub(A) as abs_sub_all
/// Length of the hypotenuse of a right-angle triangle of each element
fn hypot(A) as hypot_all
/// Four quadrant arctangent of each element in radians.
fn atan2(A) as atan2_all
}

/// Square (two powers) of each element.
Expand Down
37 changes: 37 additions & 0 deletions tests/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,40 @@ fn diff_panic_axis_out_of_bounds()
let data = array![1, 2, 4, 7];
data.diff(1, Axis(2));
}

#[test]
#[cfg(feature = "std")]
fn test_boolean_op_is_nan()
{
let data = array![1.0, 2.0, 4.0, 7.0, f64::NAN];
let expected = array![false, false, false, false, true];
assert_eq!(data.is_nan(), expected);
}

#[test]
#[cfg(feature = "std")]
fn test_unary_op_round()
{
let data = array![0.0, 0.1, 0.5, 0.9, 1.0, 1.5];
let expected = array![0.0, 0.0, 1.0, 1.0, 1.0, 2.0];
assert_eq!(data.round(), expected);
}

#[test]
#[cfg(feature = "std")]
fn test_binary_op_powi()
{
let data = array![1.0, 2.0, 4.0, 7.0];
let expected = array![1.0, 8.0, 64.0, 343.0];
assert_eq!(data.powi(3), expected);
}

#[test]
#[cfg(feature = "std")]
fn test_binary_op_hypot_all()
{
let a = array![[3.0, 5.0], [8.0, 7.0]];
let b = array![[4.0, 12.0], [15.0, 24.0]];
let expected = array![[5.0, 13.0], [17.0, 25.0]];
assert_eq!(a.hypot_all(&b), expected);
}