"vscode:/vscode.git/clone" did not exist on "d80ddda33bffcd8600f04a014184396f3f6b2c2d"
Commit 47ab0a62 authored by Peter Eastman's avatar Peter Eastman
Browse files

Optimizations to unit conversion

parent 5adaa15e
......@@ -42,6 +42,7 @@ class BaseDimension(object):
if not self.name in BaseDimension._index_by_name.keys():
BaseDimension._index_by_name[name] = BaseDimension._next_unused_index
BaseDimension._next_unused_index += 1
self._index = BaseDimension._index_by_name[name]
def __cmp__(self, other):
"""
......@@ -51,13 +52,13 @@ class BaseDimension(object):
Returns 0 if self == other, -1 if self < other, and 1 if self > other.
"""
return cmp(BaseDimension._index_by_name[self.name], BaseDimension._index_by_name[other.name])
return cmp(self._index, other._index)
def __hash__(self):
"""
Needed for using BaseDimensions as hash keys.
"""
return hash(BaseDimension._index_by_name[self.name])
return self._index
def __repr__(self):
return 'BaseDimension("%s")' % self.name
......
......@@ -89,7 +89,6 @@ class Unit(object):
"""
Yields (BaseDimension, exponent) tuples comprising this unit.
"""
result = {}
# There might be two units with the same dimension? No.
for dimension in sorted(self._all_base_units.iterkeys()):
exponent = 0
......@@ -105,7 +104,6 @@ class Unit(object):
There might be multiple BaseUnits with the same dimension.
"""
result = {}
for dimension in sorted(self._all_base_units.iterkeys()):
for base_unit in sorted(self._all_base_units[dimension].iterkeys()):
exponent = self._all_base_units[dimension][base_unit]
......@@ -526,6 +524,7 @@ class ScaledUnit(object):
class UnitSystem(object):
def __init__(self, units):
self.units = units
self._unit_conversion_cache = {}
# Create a set of base units to be used for dimension conversion
base_units = {}
for unit in self.units:
......@@ -577,6 +576,9 @@ class UnitSystem(object):
def express_unit(self, old_unit):
"""
"""
unit_name = old_unit.get_name()
if unit_name in self._unit_conversion_cache:
return self._unit_conversion_cache[unit_name]
# First express unit in terms of base dimensions found in this unit system
# (plus other dimensions not found)
m = len(self.dimensions)
......@@ -606,6 +608,7 @@ class UnitSystem(object):
found_dims[dim] = base_unit
exponent = other_dims[dim]
new_unit *= Unit({base_unit: exponent})
self._unit_conversion_cache[unit_name] = new_unit
return new_unit
def is_unit(x):
......
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