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

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

Closed
duskmoon314 opened this issue Jun 11, 2024 · 4 comments
Closed

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

duskmoon314 opened this issue Jun 11, 2024 · 4 comments

Comments

@duskmoon314
Copy link

#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
Copy link
Collaborator

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
Copy link
Author

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.

@Quba1
Copy link

Quba1 commented Aug 18, 2024

(just accidentally found this issue)

From a user's perspective there's already a lot of methods for ArrayBase and diluting them for more similarity with dynamically typed NumPy will only make the methods more difficult to navigate and discover.

If this functionality were added via generics it could be nice to have, but I guess generics would be very hard to implement.

And personally I find @nilgoyette solution sufficient. Iterators are a staple feature of Rust and are used everywhere. That solution is a little bit verbose but also idiomatic. So I would say, Rust doesn't have to be like Python I guess...

@akern40
Copy link
Collaborator

akern40 commented Aug 19, 2024

I'm gonna agree with @nilgoyette that this gets close to muddying the waters of the API for a function that could be implemented as a one-liner. At some point (when ndarray is more stable?) we could discuss whether there should be a crate that provides support for the Python Array API, but I don't think that time is now.

With two maintainer votes for closing and the OP's thumbs-up on the alternative implementation, I'm going to close. Maintainers, please re-open if you think that's a mistake.

@akern40 akern40 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants