Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numpy vectorize Vector3d() #371

Open
saikatxdas opened this issue Oct 9, 2021 · 1 comment
Open

numpy vectorize Vector3d() #371

saikatxdas opened this issue Oct 9, 2021 · 1 comment

Comments

@saikatxdas
Copy link

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
Copy link
Member

lukasmerten commented Feb 13, 2023

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

python: 3.9.2
crpropa: (3.2+) e4d90ea

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants