ComPWA / compwa.github.io

Source code for the ComPWA Organization website
https://compwa.github.io
Apache License 2.0
5 stars 4 forks source link

Concatenate 4-momentum objects from `vector` package #257

Closed shenvitor closed 6 months ago

shenvitor commented 6 months ago

I did not find a way to do Concatenation in 4-momentum objects from vector package directly

Here is the temporary solution:

Just for the record. Maybe we will be finding a better solution soon(?)

shenvitor commented 6 months ago

The previous way to address the problem is possible and not bad.

But now the strategy has changed. To do the concatenation for each vector in 4-vector one by one, and not do it converted to np.ndarray then all-in-one and convert back.

"Added" since e.g. p1. px, p1.py, p1.pz, and p1.e is directly a numpy array. Thus no conversion is needed The latest solution for this is something like this

def concatenate_data(
    data1: tuple[MomentumNumpy4D, ...],
    data2: tuple[MomentumNumpy4D, ...],
) -> tuple[MomentumNumpy4D, ...]:
    return tuple(concatenate_vectors((pi1, pj2)) for pi1, pj2 in zip(data1, data2))

def concatenate_vectors(vectors: tuple[MomentumNumpy4D]) -> MomentumNumpy4D:
    return vector.array(
        {
            "px": np.concatenate([p.px for p in vectors]),
            "py": np.concatenate([p.py for p in vectors]),
            "pz": np.concatenate([p.pz for p in vectors]),
            "E": np.concatenate([p.e for p in vectors]),
        }
    )
shenvitor commented 6 months ago

Done!

redeboer commented 6 months ago

Thus no conversion is needed

Behind the scenes, concatenate_vectors() is still converting between numpy and the vector package, so performance-wise this is not a good solution. But for the notebook, this is fine, the implementation in that function can be replaced by a cleaner, more efficient solution later.

shenvitor commented 6 months ago

Thus no conversion is needed

Behind the scenes, concatenate_vectors() is still converting between numpy and the vector package, so performance-wise this is not a good solution. But for the notebook, this is fine, the implementation in that function can be replaced by a cleaner, more efficient solution later.

Agree. Conversion is still there at the end.