Commit b7b4742f authored by peastman's avatar peastman
Browse files

Fixed a bug in unit conversion for sequences

parent c9c5997b
...@@ -41,12 +41,12 @@ Quantity(value=1.0, unit=calorie) ...@@ -41,12 +41,12 @@ Quantity(value=1.0, unit=calorie)
>>> print(joule.conversion_factor_to(calorie)) >>> print(joule.conversion_factor_to(calorie))
0.239005736138 0.239005736138
>>> c.in_units_of(joules) >>> c.in_units_of(joules)
Quantity(value=4.1840000000000002, unit=joule) Quantity(value=4.184, unit=joule)
>>> j = 1.0*joules >>> j = 1.0*joules
>>> j >>> j
Quantity(value=1.0, unit=joule) Quantity(value=1.0, unit=joule)
>>> j.in_units_of(calories) >>> j.in_units_of(calories)
Quantity(value=0.23900573613766729, unit=calorie) Quantity(value=0.2390057361376673, unit=calorie)
>>> j/joules >>> j/joules
1.0 1.0
>>> print(j/calories) >>> print(j/calories)
...@@ -58,7 +58,7 @@ Quantity(value=0.23900573613766729, unit=calorie) ...@@ -58,7 +58,7 @@ Quantity(value=0.23900573613766729, unit=calorie)
>>> c**2 >>> c**2
Quantity(value=1.0, unit=calorie**2) Quantity(value=1.0, unit=calorie**2)
>>> (c**2).in_units_of(joule*joule) >>> (c**2).in_units_of(joule*joule)
Quantity(value=17.505856000000001, unit=joule**2) Quantity(value=17.505856, unit=joule**2)
>>> ScaledUnit(1000.0, kelvin, "kilokelvin", "kK") >>> ScaledUnit(1000.0, kelvin, "kilokelvin", "kK")
ScaledUnit(factor=1000.0, master=kelvin, name='kilokelvin', symbol='kK') ScaledUnit(factor=1000.0, master=kelvin, name='kilokelvin', symbol='kK')
...@@ -252,12 +252,12 @@ Collections of numbers can also be used as values. ...@@ -252,12 +252,12 @@ Collections of numbers can also be used as values.
>>> print(s4) >>> print(s4)
((1, 2, 3), (4, 5, 6)) cm ((1, 2, 3), (4, 5, 6)) cm
>>> print(s4 / millimeters) >>> print(s4 / millimeters)
[(10.0, 20.0, 30.0), (40.0, 50.0, 60.0)] ((10.0, 20.0, 30.0), (40.0, 50.0, 60.0))
>>> t = (1,2,3) * centimeters >>> t = (1,2,3) * centimeters
>>> print(t) >>> print(t)
(1, 2, 3) cm (1, 2, 3) cm
>>> print(t / millimeters) >>> print(t / millimeters)
[10.0, 20.0, 30.0] (10.0, 20.0, 30.0)
Numpy examples are commented out because not all systems have numpy installed Numpy examples are commented out because not all systems have numpy installed
# >>> import numpy # >>> import numpy
...@@ -772,7 +772,7 @@ Examples ...@@ -772,7 +772,7 @@ Examples
Creating a new ScaledUnit: Creating a new ScaledUnit:
>>> mC = milli * ScaledUnit(4.184, joule, "calorie", "cal") >>> mC = milli * ScaledUnit(4.184, joule, "calorie", "cal")
>>> mC >>> mC
ScaledUnit(factor=0.0041840000000000002, master=joule, name='millicalorie', symbol='mcal') ScaledUnit(factor=0.004184, master=joule, name='millicalorie', symbol='mcal')
Creating a new Unit: Creating a new Unit:
>>> ms = milli * second >>> ms = milli * second
...@@ -795,7 +795,7 @@ Examples ...@@ -795,7 +795,7 @@ Examples
Formatting of Quantities Formatting of Quantities
>>> x = 5.439999999 * picosecond >>> x = 5.439999999 * picosecond
>>> x >>> x
Quantity(value=5.4399999990000003, unit=picosecond) Quantity(value=5.439999999, unit=picosecond)
>>> x.format("%.3f") >>> x.format("%.3f")
'5.440 ps' '5.440 ps'
......
...@@ -545,10 +545,7 @@ class Quantity(object): ...@@ -545,10 +545,7 @@ class Quantity(object):
pass pass
if factor_is_identity: if factor_is_identity:
# No multiplication required # No multiplication required
if (self.unit is new_unit): result = Quantity(copy.deepcopy(self._value), new_unit)
result = self
else:
result = Quantity(self._value, new_unit)
else: else:
try: try:
# multiply operator, if it exists, is preferred # multiply operator, if it exists, is preferred
...@@ -558,18 +555,7 @@ class Quantity(object): ...@@ -558,18 +555,7 @@ class Quantity(object):
value = factor * self._value # works for number, numpy.array, or vec3, e.g. value = factor * self._value # works for number, numpy.array, or vec3, e.g.
result = Quantity(value, new_unit) result = Quantity(value, new_unit)
except TypeError: except TypeError:
# list * float fails with TypeError value = copy.deepcopy(self._value)
# Presumably a list type
# deep copy
value = self._value[:] # deep copy
# convert tuple to list
try:
value[0] = value[0] # tuple is immutable
except TypeError:
# convert immutable tuple to list
value = []
for i in self._value:
value.append(i)
result = Quantity(self._scale_sequence(value, factor, post_multiply), new_unit) result = Quantity(self._scale_sequence(value, factor, post_multiply), new_unit)
if (new_unit.is_dimensionless()): if (new_unit.is_dimensionless()):
return result._value return result._value
...@@ -579,20 +565,29 @@ class Quantity(object): ...@@ -579,20 +565,29 @@ class Quantity(object):
def _scale_sequence(self, value, factor, post_multiply): def _scale_sequence(self, value, factor, post_multiply):
try: try:
if post_multiply: if post_multiply:
if isinstance(self._value, tuple): value = value*factor
value = tuple([x*factor for x in value])
else:
for i in range(len(value)):
value[i] = value[i]*factor
else: else:
if isinstance(self._value, tuple): value = factor*value
value = tuple([factor*x for x in value]) except TypeError:
try:
if post_multiply:
if isinstance(value, tuple):
value = tuple([x*factor for x in value])
else:
for i in range(len(value)):
value[i] = value[i]*factor
else:
if isinstance(value, tuple):
value = tuple([factor*x for x in value])
else:
for i in range(len(value)):
value[i] = factor*value[i]
except TypeError as ex:
if isinstance(value, tuple):
value = tuple([self._scale_sequence(x, factor, post_multiply) for x in value])
else: else:
for i in range(len(value)): for i in range(len(value)):
value[i] = factor*value[i] value[i] = self._scale_sequence(value[i], factor, post_multiply)
except TypeError as ex:
for i in range(len(value)):
value[i] = self._scale_sequence(value[i], factor, post_multiply)
return value return value
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment