Commit 14df6a03 authored by peastman's avatar peastman
Browse files

Merge pull request #538 from peastman/master

Fixed a bug in unit conversion for sequences
parents c37345e5 b7b4742f
......@@ -41,12 +41,12 @@ Quantity(value=1.0, unit=calorie)
>>> print(joule.conversion_factor_to(calorie))
0.239005736138
>>> c.in_units_of(joules)
Quantity(value=4.1840000000000002, unit=joule)
Quantity(value=4.184, unit=joule)
>>> j = 1.0*joules
>>> j
Quantity(value=1.0, unit=joule)
>>> j.in_units_of(calories)
Quantity(value=0.23900573613766729, unit=calorie)
Quantity(value=0.2390057361376673, unit=calorie)
>>> j/joules
1.0
>>> print(j/calories)
......@@ -58,7 +58,7 @@ Quantity(value=0.23900573613766729, unit=calorie)
>>> c**2
Quantity(value=1.0, unit=calorie**2)
>>> (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(factor=1000.0, master=kelvin, name='kilokelvin', symbol='kK')
......@@ -252,12 +252,12 @@ Collections of numbers can also be used as values.
>>> print(s4)
((1, 2, 3), (4, 5, 6)) cm
>>> 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
>>> print(t)
(1, 2, 3) cm
>>> 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
# >>> import numpy
......@@ -772,7 +772,7 @@ Examples
Creating a new ScaledUnit:
>>> mC = milli * ScaledUnit(4.184, joule, "calorie", "cal")
>>> 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:
>>> ms = milli * second
......@@ -795,7 +795,7 @@ Examples
Formatting of Quantities
>>> x = 5.439999999 * picosecond
>>> x
Quantity(value=5.4399999990000003, unit=picosecond)
Quantity(value=5.439999999, unit=picosecond)
>>> x.format("%.3f")
'5.440 ps'
......
......@@ -545,10 +545,7 @@ class Quantity(object):
pass
if factor_is_identity:
# No multiplication required
if (self.unit is new_unit):
result = self
else:
result = Quantity(self._value, new_unit)
result = Quantity(copy.deepcopy(self._value), new_unit)
else:
try:
# multiply operator, if it exists, is preferred
......@@ -558,18 +555,7 @@ class Quantity(object):
value = factor * self._value # works for number, numpy.array, or vec3, e.g.
result = Quantity(value, new_unit)
except TypeError:
# list * float fails with TypeError
# 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)
value = copy.deepcopy(self._value)
result = Quantity(self._scale_sequence(value, factor, post_multiply), new_unit)
if (new_unit.is_dimensionless()):
return result._value
......@@ -579,18 +565,27 @@ class Quantity(object):
def _scale_sequence(self, value, factor, post_multiply):
try:
if post_multiply:
if isinstance(self._value, tuple):
value = value*factor
else:
value = factor*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(self._value, tuple):
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:
for i in range(len(value)):
value[i] = self._scale_sequence(value[i], factor, post_multiply)
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