# NumPy support¶

The magnitude of a Pint quantity can be of any numerical type and you are free to choose it according to your needs. In numerical applications, it is quite convenient to use NumPy ndarray and therefore they are supported by Pint.

First, we import the relevant packages:

>>> import numpy as np
>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> Q_ = ureg.Quantity


and then we create a quantity the standard way

>>> legs1 = Q_(np.asarray([3., 4.]), 'meter')
>>> print(legs1)
[ 3.  4.] meter


or we use the property that Pint converts iterables into NumPy ndarrays to simply write:

>>> legs1 = [3., 4.] * ureg.meter
>>> print(legs1)
[ 3.  4.] meter


All usual Pint methods can be used with this quantity. For example:

>>> print(legs1.to('kilometer'))
[ 0.003  0.004] kilometer
>>> print(legs1.dimensionality)
[length]
>>> legs1.to('joule')
Traceback (most recent call last):
...
DimensionalityError: Cannot convert from 'meter' ([length]) to 'joule' ([length] ** 2 * [mass] / [time] ** 2)


NumPy functions are supported by Pint. For example if we define:

>>> legs2 = [400., 300.] * ureg.centimeter
>>> print(legs2)
[ 400.  300.] centimeter


we can calculate the hypotenuse of the right triangles with legs1 and legs2.

>>> hyps = np.hypot(legs1, legs2)
>>> print(hyps)
[ 5.  5.] meter


Notice that before the np.hypot was used, the numerical value of legs2 was internally converted to the units of legs1 as expected.

Similarly, when you apply a function that expects angles in radians, a conversion is applied before the requested calculation:

>>> angles = np.arccos(legs2/hyps)
>>> print(angles)


You can convert the result to degrees using the corresponding NumPy function:

>>> print(np.rad2deg(angles))
[ 36.86989765  53.13010235] degree


Applying a function that expects angles to a quantity with a different dimensionality results in an error:

>>> np.arccos(legs2)
Traceback (most recent call last):
...
DimensionalityError: Cannot convert from 'centimeter' ([length]) to 'dimensionless' (dimensionless)


## Support¶

The following ufuncs can be applied to a Quantity object:

• Math operations: add, subtract, multiply, divide, logaddexp, logaddexp2, true_divide, floor_divide, negative, remainder mod, fmod, absolute, rint, sign, conj, exp, exp2, log, log2, log10, expm1, log1p, sqrt, square, reciprocal
• Trigonometric functions: sin, cos, tan, arcsin, arccos, arctan, arctan2, hypot, sinh, cosh, tanh, arcsinh, arccosh, arctanh, deg2rad, rad2deg
• Comparison functions: greater, greater_equal, less, less_equal, not_equal, equal
• Floating functions: isreal,iscomplex, isfinite, isinf, isnan, signbit, copysign, nextafter, modf, ldexp, frexp, fmod, floor, ceil, trunc

And the following NumPy ndarray methods and functions:

• sum, fill, reshape, transpose, flatten, ravel, squeeze, take, put, repeat, sort, argsort, diagonal, compress, nonzero, searchsorted, max, argmax, min, argmin, ptp, clip, round, trace, cumsum, mean, var, std, prod, cumprod, conj, conjugate, flatten

Quantity is not a subclass of ndarray. This might change in the future, but for this reason functions that call numpy.asanyarray are currently not supported. These functions are:

• unwrap, trapz, diff, ediff1d, fix, gradient, cross, ones_like