You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wonder if it is possible to have a function to convert a list of Quantity into an array of Quantity object?
>>> import numpy as np
>>> import quantities as pq
>>> a = 1 * pq.e
>>> a
array(1.) * e
>>> b = [a, a]
>>> b
[array(1.) * e, array(1.) * e]
>>> np.array(b)
array([1., 1.])
So I wonder if there could be a function
>>> pq.array(b)
array([1., 1.]) * e
The text was updated successfully, but these errors were encountered:
I'm using the following function for this use-case (the name of it could be improved, though)
defquantitylistrescale(seq, unit=None):
"""Convert list of quantities to a rescaled quantity array Examples -------- >>> import quantities as pq >>> a = 3 * pq.m; b = 2.5 * pq.mm >>> quantitylistrescale([a, b], 'cm') array([3.0e+02, 2.5e-01]) * cm If no ``unit`` is given, the elements will be rescaled to the unit of the first quantity. >>> quantitylistrescale([a, b]) array([3.0e+00, 2.5e-03]) * m >>> quantitylistrescale([]) Traceback (most recent call last): ... StopIteration >>> quantitylistrescale([3 * pq.m, 4 * pq.V]) Traceback (most recent call last): ... ValueError: Unable to convert between units of "V" and "m" """assertall(isinstance(item, pq.Quantity) foriteminseq), (
f"{seq} contains objects which are not of type pq.Quantity")
ifunitisNone:
first=next(iter(seq))
unit=first.dimensionality.stringrescaled_seq= [item.rescale(unit).magnitudeforiteminseq]
returnpq.Quantity(rescaled_seq, unit)
I'm using a similar function (without type checking as I know what is in the array) but I felt that it might be good to have it in the package, like pq.array().
I wonder if it is possible to have a function to convert a list of Quantity into an array of Quantity object?
So I wonder if there could be a function
The text was updated successfully, but these errors were encountered: