CRPropa / CRPropa3

CRPropa is a public astrophysical simulation framework for propagating extraterrestrial ultra-high energy particles. https://crpropa.github.io/CRPropa3/
https://crpropa.desy.de
GNU General Public License v3.0
68 stars 67 forks source link

numpy vectorize Vector3d() #371

Open saikatxdas opened 2 years ago

saikatxdas commented 2 years ago

Hi, I have a usage question regarding the usage of Vector3 class.

Before the change of data type for Vector3 class to array interface was made, it was possible to vectorize the PyObject to store arrays for x, y, z to create an array of Vector3d objects.

vec3d = np.vectorize(Vector3d)
dir = vec3d(x,y,z)

where x, y, z are numpy arrays derived from the direction data in CRPropa output. But now, this process yields the following error TypeError: Wrong number or type of arguments for overloaded function 'new_Vector3d' So, I tried using arrays of x,y,z values stacked in an array, i.e., data = np.stack((x,y,z), axis=1), but still I get the same error. A for loop will also do the job, but can I still use numpy vectorize in some way?

lukasmerten commented 1 year ago

I can confirm an issue but get a different error message.

python: 3.9.2 crpropa: (3.2+) e4d90eab

from crpropa import Vector3d
import numpy as np
x = np.ones(10)
vec3d = np.vectorize(Vector3d)
vec3d(x)

TypeError                                 Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 vec3d(x)

File /rest/venvs/crp_docu/lib/python3.9/site-packages/numpy/lib/function_base.py:2304, in vectorize.__call__(self, *args, **kwargs)
   2301     vargs = [args[_i] for _i in inds]
   2302     vargs.extend([kwargs[_n] for _n in names])
-> 2304 return self._vectorize_call(func=func, args=vargs)

File /rest/venvs/crp_docu/lib/python3.9/site-packages/numpy/lib/function_base.py:2390, in vectorize._vectorize_call(self, func, args)
   2387 outputs = ufunc(*inputs)
   2389 if ufunc.nout == 1:
-> 2390     res = asanyarray(outputs, dtype=otypes[0])
   2391 else:
   2392     res = tuple([asanyarray(x, dtype=t)
   2393                  for x, t in zip(outputs, otypes)])

TypeError: float() argument must be a string or a number, not 'crpropa.Vector3d'