magpylib / magpylib

Python package for computation of magnetic fields of magnets, currents and moments.
https://magpylib.readthedocs.io/en/latest/
BSD 2-Clause "Simplified" License
218 stars 34 forks source link

docs minifix #770

Closed OrtnerMichael closed 2 days ago

OrtnerMichael commented 4 weeks ago

A memory efficient way to create a grid with numpy by broadcasting into an allocated array - no respective creation of X and Y grids as in the docs example here

grid = np.zeros((100,100,3))
grid[:,:,(0,2)] = np.mgrid[-1:1:100j, -1:1:100j].transpose((2, 1, 0))
Alexboiboi commented 3 weeks ago

Hi @OrtnerMichael,

I don't see much benefit in terms of memory here, especially with the B-field computation coming afterwards.

BTW I fixed the code in your comment above to make it work (you had 101 instead of 100 in np.zeros(...

Alexboiboi commented 3 weeks ago

You can also make it a one-liner:

grid = np.mgrid[-1:1:100j, 0:0:1j, -1:1:100j].T[:,0]
OrtnerMichael commented 3 weeks ago

this is very nice !