convert_to_units
Convert quantities to specified units using sympy.physics.units. Supports SI, CGS, and compound units for precise unit transformations in symbolic algebra.
Instructions
Converts a quantity to the given target units using sympy.physics.units.convert_to.
Args:
expr_key: The key of the expression (previously introduced) to convert.
target_units: List of unit names as strings (e.g., ["meter", "1/second"]).
unit_system: Optional unit system (from UnitSystem enum). Defaults to SI.
The following units are available by default:
SI base units: meter, second, kilogram, ampere, kelvin, mole, candela
Length: kilometer, millimeter
Mass: gram
Energy: joule
Force: newton
Pressure: pascal
Power: watt
Electric: coulomb, volt, ohm, farad, henry
Constants: speed_of_light, gravitational_constant, planck
IMPORTANT: For compound units like meter/second, you must separate the numerator and
denominator into separate units in the list. For example:
- For meter/second: use ["meter", "1/second"]
- For newton*meter: use ["newton", "meter"]
- For kilogram*meter²/second²: use ["kilogram", "meter**2", "1/second**2"]
Example:
# Convert speed of light to kilometers per hour
expr_key = introduce_expression("speed_of_light")
result = convert_to_units(expr_key, ["kilometer", "1/hour"])
# Returns approximately 1.08e9 kilometer/hour
# Convert gravitational constant to CGS units
expr_key = introduce_expression("gravitational_constant")
result = convert_to_units(expr_key, ["centimeter**3", "1/gram", "1/second**2"], UnitSystem.CGS)
SI prefixes (femto, pico, nano, micro, milli, centi, deci, deca, hecto, kilo, mega, giga, tera)
can be used directly with base units.
Returns:
A key for the converted expression, or an error message.
Input Schema
Name | Required | Description | Default |
---|---|---|---|
expr_key | Yes | ||
target_units | Yes | ||
unit_system | No |
Input Schema (JSON Schema)
{
"$defs": {
"UnitSystem": {
"enum": [
"MKS",
"MKSA",
"natural",
"SI",
"cgs"
],
"title": "UnitSystem",
"type": "string"
}
},
"properties": {
"expr_key": {
"title": "Expr Key",
"type": "string"
},
"target_units": {
"items": {},
"title": "Target Units",
"type": "array"
},
"unit_system": {
"anyOf": [
{
"$ref": "#/$defs/UnitSystem"
},
{
"type": "null"
}
],
"default": null
}
},
"required": [
"expr_key",
"target_units"
],
"title": "convert_to_unitsArguments",
"type": "object"
}