# Convert derived SI units to base units

How can I convert SI units involving temperature (kelvin) to their base units?

Example using units of heat capacity:

# Temperature units multiplied by other units cannot be converted
blah = units.energy.joule/units.temperature.kelvin
blah.convert()


Traceback (click to the left of this block for traceback)

...

ValueError: Cannot convert

I need this output:

units.energy.joule.convert()/units.temperature.kelvin.convert()


kilogram*meter^2/(kelvin*second^2)

There is a discussion of this issue at the link below, but there should be a function to convert to base units in the absence of numerical values. Link: [https://groups.google.com/forum/#!sea...]

Does anyone know how to do this?

edit retag close merge delete

Sort by » oldest newest most voted

This should be a built-in function, indeed. Here's a solution in the meanwhile:

def convert(expr):
op = expr.operator()
ops = expr.operands()
if op:
if len(ops) == 2:
return op(*map(convert, ops))
else:
return op(convert(ops), reduce(op, map(convert, ops[1:])))
else:
return expr.convert() if hasattr(expr, 'convert') else expr


Then:

sage: blah = units.energy.joule/units.temperature.kelvin
sage: convert(blah)
kilogram*meter^2/(kelvin*second^2)

more