[AstroPy] Trouble when creating a vectorial Quantity from its components
Evert Rol
evert.rol at gmail.com
Sat Dec 28 06:49:57 EST 2013
Hi Juan,
> Is there any way I can avoid doing this?
>
> ```
>>>> r = [p.si.value * np.cos(nu), p.si.value * np.sin(nu), 0] *
> p.si.unit; r
> <Quantity [ 882947.59285893, 469471.56278589, 0. ] m>
> ```
>
> because it might get quite verbose if there are more magnitudes.
Can't you simply do
>>> r = np.array([np.cos(nu), np.sin(nu), 0]) * p; r
<Quantity [ 882.94759286, 469.47156279, 0. ] km>
So just move p outside the array. Or is that still to verbose?
From your example, I'm assuming p is always the same. If not, you can make p an array of just lengths and multiple that with np.array([np.cos(nu), np.sin(nu), 0]):
>>> p = np.array([1000, 2000, 3000]) * u.km
>>> r = np.array([np.cos(nu), np.sin(nu), 0]) * p; r
<Quantity [ 882.94759286, 938.94312557, 0. ] km>
Cheers,
Evert
More information about the AstroPy
mailing list