rust-ndarray / ndarray

ndarray: an N-dimensional array with array views, multidimensional slicing, and efficient operations
https://docs.rs/ndarray/
Apache License 2.0
3.43k stars 295 forks source link

[Feature Request] More powerful `power` like `numpy.power` #1392

Open duskmoon314 opened 4 weeks ago

duskmoon314 commented 4 weeks ago

1042 has added element-wise power methods powi and powf. But they only accept one parameter.

In NumPy, numpy.power receives two arrays, x1 and x2. The latter could be a number or an array and performs power with different exponents.

An example from: https://numpy.org/doc/stable/reference/generated/numpy.power.html#numpy.power

x1 = np.arange(6)
x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
np.power(x1, x2)
# array([  0.,   1.,   8.,  27.,  16.,   5.])

Though we may archive this using an iterator, a more powerful function might fill the gap for users coming from NumPy.

nilgoyette commented 3 weeks ago

I think this falls under the "too many methods" problem, but other maintainers may have a different opinion.

You might already know, but what you request can be coded like this

let ans = Zip::from(&x1).and(&x2).map_collect(|&a, &b| a.powi(b));  // Or powf
duskmoon314 commented 3 weeks ago

I think this falls under the "too many methods" problem

Indeed. Adding more methods would be convenient when people want to port some Python code to Rust. I opened this issue while trying to port some Python code and found out I needed to use iterators.

However, this would also make implementation and maintenance more complex and may confuse users. I genuinely think more discussion should be taken.