OK, so I have a work around here:
from sympy.physics import units as u
class MyVars:
def __init__(self):
'''create a dict of units of interest'''
self.my_units = {}
self.my_units[u.V] = 'V'
self.my_units[u.W] = 'W'
self.my_units[u.A] = 'A'
self.my_units[u.Hz] = 'Hz'
self.my_units[u.percent] = '\%'
self.my_units[u.F] = 'F'
self.my_units[u.s] = 's'
def identify_unit(self, value):
'''
Identify the unit from ones in the list of interest
and then split the value in a number and the new unit.
Here "value" is a standard "sympy + sympy.physics.units" var.
Return None if the unit received is not in the list.
'''
for my_unit in self.my_units.keys():
aux = value.as_coefficient(my_unit)
if aux:
if aux.is_number:
return aux, self.units[unit]
return None
def change_factor(self, value):
'''
value is in the form got from "identify_unit" above:
value[0]: value itself;
value[1]: unit from the list of interest.
Here, I adjust the value and unit in case of M (mega),
k (kilo), m (milli) and \mu (micro), and return it.
'''
aux = abs(value)
if aux >= u.mega:
return value / (10 ** 6), "M" + value[1]
elif aux >= u.kilo:
return value[0] / (10 ** 3), "k" + value[1]
elif aux >= 1:
return value[0], value[1]
elif aux >= u.milli:
return value[0] * (10 ** 3), "m" + value[1]
elif aux >= u.micro:
return value[0] * (10 ** 6), "\mu{}" + value[1]
So, that is the solution that I am using right now. It is far away from a
complete and useful set of helper functions and units. However, if you think
that it has potentials to be part of the Sympy project, just guide me through
the process and let me know what more is needed.
Cheers,
Thiago