ndarray take power element by element

43 Views Asked by At

In Rust, (3.0).powf(4.0) will take the power of 3^4. ndarray supports many operators, like array1 + array2. How do you take the elementwise power?

1

There are 1 best solutions below

0
Liel Fridman On

You can use the map function to apply anything you like per cell. For example, with powf:

use ndarray;

fn main() {
    let arr = ndarray::array![[1, 2], [3, 4], [6, 7]];
    println!("{:?}", arr);
    let arr = arr.map(|&item| (item as f32).powf(4.0));
    println!("{:?}", arr);
}