[Python] How to apply rotations to structured numpy arrays?

Discussão em 'Python' iniciado por Stack, Junho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using structured arrays to store atoms data produced by LAMMPS (I'm using a structured array that follows its format). I need to rotate the positions:

    import numpy as np

    transform = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.float64)
    dtype = np.dtype([("x", np.float64), ("y", np.float64), ("z", np.float64)])

    atoms = np.array(
    [
    (0.0, 0.0, 0.0),
    (1.0, 0.0, 0.0),
    (0.0, 1.0, 0.0),
    (1.0, 1.0, 1.0),
    ],
    dtype=dtype,
    )

    atoms[["x", "y", "z"]] = atoms[["x", "y", "z"]] @ transform.T



    But this produces:

    Traceback (most recent call last):
    File "c:\Users\acgc99\Desktop\rotation.py", line 16, in <module>
    atoms[["x", "y", "z"]] = atoms[["x", "y", "z"]] @ transform.T
    ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
    numpy._core._exceptions._UFuncNoLoopError: ufunc 'matmul' did not contain a loop with signature matching types (dtype([('x', '<f8'), ('y', '<f8'), ('z', '<f8')]), dtype('float64')) -> None


    I can convert to unstructured arrays, but I guess that doing that change multiple times is not efficient when working with tens of millions of atoms.

    Continue reading...

Compartilhe esta Página