-
Notifications
You must be signed in to change notification settings - Fork 478
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
Implement np.array/np.asarray #1037
Comments
unfortunately, array creation functions (such as In your example, though, I think you can (and should) work around that by using In [2]: def do_calculation(x, N):
...: ary = x * np.arange(N)
...: ary2 = ary * 2
...: return ary2
...:
...: do_calculation(3, 10), do_calculation(3 * u.meter, 10)
Out[2]:
(array([ 0, 6, 12, 18, 24, 30, 36, 42, 48, 54]),
array([ 0, 6, 12, 18, 24, 30, 36, 42, 48, 54]) <Unit('meter')>) |
In addition to @keewis's great answer, it might be worth mentioning that |
It should be noted that NEP 35 seems to open the possibility to override the array creation routine. |
Let's say I have a function like the following where doing the calculation involves doing a list comprehension which generates a list, and I want to turn it back into a numpy array before returning. The units of each element of the list comprehension will have the same units as some input
x
: ifx
is a normal Python number/numpy array, then I can turn the list into an array with a simplenp.array(lst)
ornp.asarray(lst)
. However, ifx
has units, this fails.My goal is to avoid introducing any pint-dependent code in
do_calculation
(e.g., let's say I have a big library of Python functions that calculate various physical formulae, part of the beauty of pint is if I pass in Quantities as inputs to these, the result is automatically carries units, but the library itself has no knowledge or dependency on pint; this list comprehension issue is the only place hangup).Ideally,
np.array
/np.asarray
would dispatch topint.Quantities.from_sequence
when any member of the input iterable is apint.Quantity
, but I'm not sure that NEP18 provides for that kind of thing.Example:
Is this something that will eventually be addressed by improvements in NEP18 support? Is there another best practice I should be following?
The text was updated successfully, but these errors were encountered: