[AstroPy] Trouble when creating a vectorial Quantity from its components
Juan Luis Cano
juanlu001 at gmail.com
Sat Dec 28 07:09:47 EST 2013
On 12/28/2013 12:49 PM, Evert Rol wrote:
> 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?
Well, that's a good idea! It will do the trick for me, not worried about
corner cases right now. Thanks!
> 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