matterhorn103 / quanstants

Intuitive and unastonishing physical quantities, units, constants, and uncertainties in Python
MIT License
1 stars 0 forks source link

Support numpy/ndarray #5

Open matterhorn103 opened 6 months ago

matterhorn103 commented 6 months ago

Essentially this means supporting arrays via one of two strategies:

  1. Developing a quantity numpy dtype with the appropriate properties
  2. Developing a custom array, called quanstants.array or quanstants.ndarray or qarray or quarray

In general there will only be support for arrays of quantities of the same unit. An array will consist of numerical values and a single associated unit.

This should make implementation less complicated and more efficient, as the elements of the array are then purely numerical, and the unit arithmetic only has to be done once per operation. Uncertainties will still have to be tracked though.

I personally need to do more research into the way ndarrays and dtypes work before having an idea as to exactly how the implementation should look.

In any case, a custom array type should follow the Python array API standard.

Sadly decimal numbers are not a supported dtype. This would mean either implementing the behaviour ourselves, or converting the numbers to floats when put into an array.

Applications using numpy are less likely to require the exactness of Decimal anyway, so this seems like a reasonable compromise.

pandas dataframes using numpy arrays would suffer. However, the plan (#6) is to support polars, and then that can be recommended preferentially.

matterhorn103 commented 5 months ago

See https://numpy.org/doc/stable/reference/ufuncs.html and https://numpy.org/neps/nep-0018-array-function-protocol.html

matterhorn103 commented 4 months ago

Only numpy>=2.0 will be supported