Skip to content

Make getB more batchable #883

@purepani

Description

@purepani

Currently, it seems that magpy.getB is pretty restricted in how it can work with shapes. Based on the documentation, it can essentially only work with one extra dimension. It would be useful to be able to work with any number of extra dimensions. That way, you can easily calculate a ton of field values at once with broadcasting.
For example, the following is currently not possible, but should be:

import magpylib as magpy
import numpy as np

positions = np.arange((10, 20, 3))
B = magpy.getB(
     source='Cylinder',
     observers=positions,
     polarization=(0, 0, 1),
     dimension=(1, 1)
) # shape is (10, 20, 3)

positions = np.arange((10, 3))
polarization = np.arange((20, 3))
dimension = np.arange((30, 40, 2))
B = magpy.getB(
     source='Cylinder',
     observers=positions[:, None, None, None],
     polarization=polarization[None, :, None, None],
     dimension=dimension[None, None, :, :]
) # shape is (10, 20, 30, 40, 3)

I am guessing that the reason is due to the fact that the same limitation applies to scipy.spacial.transform.Rotation class, but this should be able to be worked around with reshaping before and after rotation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions