-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
ENH: broadcast_arrays()
with option to not repeat arrays
#28098
Comments
This function you seem to want is not really broadcasting, but more changing the number of dimensions, equivalent to
This is simple enough that I'm not sure it is worth adding (note that Another function that may help in cases like these is p.s. Your specific problem is probably just a simplest version of what you ran into, but it could be solved by using
|
This function is certainly acceptable, but I would like to clarify what it would be better to check if the shapes are broadcastable in advance to prevent unnecessary calculations. Furthermore, such a function can be used for vectorized arguments as well without increasing computational cost; Consider a function def gfast(r, thetaandphi): # (3, 1) and (4, 2)
"""r.shape and theta.shape[:-1] should be broadcastable and theta.shape[-1] should be 2"""
if thetaandphi.shape[-1] != 2:
raise ValueError()
r, thetaandphi = np.broadcast_arrays(r[..., None], thetaandphi, no_repeat=True) # (3, 1, 1) and (1, 4, 2)
r = r[..., 0] # (3, 1) # Thanks to no_repeat, this is not repeated AS WELL.
# Therefore, by using the above function, we are killing two birds with one stone.
sintheta = np.sin(thetaandphi[..., 0])
direction = np.stack([sintheta * np.cos(thetaandphi[..., 1]), sintheta * np.sin(thetaandphi[..., 1]), np.cos(thetaandphi[..., 0])])
return r * direction Without using such a function, At any rate, if such a function were available in the standard API, users might be able to create functions with batch support (and/or vectorized arguments) more comfortably. (Although it would be difficult to do so) |
I tend to agree with Marten that this doesn't seem all that common, and getting to the identical dimension (appending them) is easy enough.
with inputs You might want something like The thing that makes it very hard for me to even give a hint, is that I am not sure how to document what the function returns? The broadcast of
While that may make sense, doing so seems unnecessary in most use-cases. Error cases like this should only be taken on programming issues (normally). If the user really can run into shape mismatch errors, it might be that they should check ahead of time (rather than this function). |
the inputs are actually
In my understanding
It is quite simple, for the last example the return shape is
I strongly hope that this could be incorporated into the standard API. If not, is there a more appropriate place to implement this? |
Proposed new feature or change:
Consider a function$f(r, \theta) = [r \cos \theta, r \sin \theta]$ and when
r.shape == (3, 1) and theta.shape == (4,)
thus the shape of the result is supposed to be(2, 3, 4)
(or(3, 4, 2)
) by broadcasting.Where
broadcast_arrays(*arrays, no_repeat=True)
doesmax([a.ndim for a in arrays])
Apparently the latter function
ffast
is less computationally demanding thanfslow
. Hence, it would be useful to have such an option.The text was updated successfully, but these errors were encountered: