Share 
Follow 
AboutSource CodeIssues
Date:
2010-09-01 22:55
Priority:
3
State:
Open
Submitted by:
John Chodera (jchodera)
Assigned to:
Nobody (None)
Summary:
Problems when Quantity wraps numpy array in expression with unit-less numpy quantities

Detailed description
I recently ran into a problem involving Quantity objects wrapping numpy arrays interacting with un-wrapped numpy numbers. Observe:

>>> import numpy
>>> import simtk.unit as units
>>> x = numpy.float32(1.0)
>>> y = units.Quantity(numpy.ones([3], numpy.float64), units.kilocalories_per_mole)
>>> x / y[0]
1.0

# Wait a second...that should have units!
# Note that there's no problem if the Quantity is not wrapping a numpy array:

>>> z = 1.0 * units.kilocalories_per_mole
>>> x / z
Quantity(value=1.0, unit=mole/kilocalorie)

# But interacting with standard Python floats is fine:

>>> w = 1.0
>>> w / y[0]
Quantity(value=1.0, unit=mole/kilocalorie)

Add A Comment: Notepad

No Comments Have Been Posted

No Changes Have Been Made to This Item

Feedback