Skip to content
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

Provide element-wise math functions for floats #992

Closed
KmolYuan opened this issue May 6, 2021 · 0 comments · Fixed by #1042
Closed

Provide element-wise math functions for floats #992

KmolYuan opened this issue May 6, 2021 · 0 comments · Fixed by #1042

Comments

@KmolYuan
Copy link
Contributor

KmolYuan commented May 6, 2021

This is an improvement to reduce Array::mapv calling.

use num_traits::Float;

// For Array<Float>
impl<A, S, D> ArrayBase<S, D>
where
    A: Float,
    S: RawData<Elem = A>,
    D: Dimension,
{
    pub fn square(&self) -> Array<A, D> {
        self.mapv(|v| v * v)
    }
    pub fn sqrt(&self) -> Array<A, D> {
        self.mapv(A::sqrt)
    }
    // abs, pow, sin, cos, round, ceil, etc.
    ...
}

The following is a little part of my code, a lot of mappings were used here.
(solved by a local plug-in trait)

let dt = dxy.square().sum_axis(Axis(1)).sqrt();
let phi_n = &phi * n as f64;
let cos_phi_n = (phi_n.slice(s![1..]).cos() - phi_n.slice(s![..-1]).cos()) / &dt;
let sin_phi_n = (phi_n.slice(s![1..]).sin() - phi_n.slice(s![..-1]).sin()) / &dt;

I hope it can be officially supported.

@bluss bluss changed the title Provides element-wise math functions for floats Provide element-wise math functions for floats May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant