-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
DOC: Add explanation of a sparse mesh grid #19776
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
Conversation
70f12e3 to
a53d3f3
Compare
c24ec93 to
6dc3342
Compare
numpy/lib/function_base.py
Outdated
| can use the parameter ``sparse=True`` to save memory and computation time. | ||
| Here, the sparse coordinate grid reduces the memory from 2*101*101 = 20.402 | ||
| to 101+101 = 202 values. Likewise, the expression ``xx**2 + yy**2`` is | ||
| reduced from 20.401 square operations to only 202 square operations, but |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is correct. There's both the squaring and addition operations to consider: doesn't the addition require 101**2 operations?
In any case, my two cents is that this could benefit from less detail as it's easy to lose the forest through the trees here. I'd advocate for chopping out the text from Here,: IMO the best way to provide more detail would be to add a reference to the 2011 NumPy paper, which has an excellent detailed example of broadcasting with "sparse" arrays.
Just my two cents though - leaving the detail is also fine, but we should double check to make sure the numbers are correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was only referring to the square operations. I anticipated that the square operation was more expensive than addition, but that doesn't seem to be the case. So the overall gain is theoretically just a factor 3 (beacuse the sparse sqares can be neglected compared to the addition). A bit more testing also shows that a real gain only appears when x, y have order of 1000 elements or larger, which I find surprisingly high.
Anyway, it's not within my scope to discuss performance characteristics. I've removed everything from "Here" as suggested.
205ea53 to
994b77a
Compare
|
close/reopen |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops - I only meant that the text about performance should be removed, not the example. I think the clearest way to demonstrate what the sparse kwarg does is to show it:
>>> xx, yy = np.meshgrid(x, y)
>>> xx.shape, yy.shape
((101, 101), (101, 101))
>>> xs, ys = np.meshgrid(x, y, sparse=True)
>>> xs.shape, ys.shape
((1, 101), (101, 1))
>>> zs = np.sin(xs**2 + ys**2) / (xs**2 + ys**2)
>>> np.array_equal(z, zs, equal_nan=True)
True
>>> zs.shape
(101, 101)Also, it seems that sparse keeps getting replaced with spare in the pushes.
407ec5e to
5cb82fd
Compare
|
@rossbar Thanks, I've taken your example suggestion and adapted:
|
8555e5c to
20e3fc4
Compare
Personally I'm -1 on this change. This is an example of a very common use-case that demonstrates how these two fundamental packages (numpy and matplotlib) are used together. It'd be a shame to lose that, and I don't really think the 4 extra lines for matplotlib make the example difficult to follow. If you feel strongly about removing it, I'd recommend opening a separate PR for it. |
2ee62f2 to
c3dd580
Compare
Follow-up to numpy#19561.
c3dd580 to
a151314
Compare
|
I've reverted the plot removal, Instead I've moved it to a separate block and added a colorbar. |
|
Thanks @timhoffm ! |
* Add explanation of a spare mesh grid. Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
* Add explanation of a spare mesh grid. Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>

See #19561 (comment).
I've also changed
np.arange(-5, 5, 0.1)tonp.linspace(-5, 5, 101)in the example becausearangefor non-integer steps is generally not recommended.https://numpy.org/doc/stable/reference/generated/numpy.arange.html: