fail_for_unit_mismatch

fail_for_unit_mismatch#

class saiunit.math.fail_for_unit_mismatch(obj1, obj2=None, error_message=None, **error_arrays)#

Compare the units of two objects.

If the units do not share the same dimension a UnitMismatchError is raised.

Parameters:
  • obj1 ({array-like, Quantity}) – The first object to compare.

  • obj2 ({array-like, Quantity}, optional) – The second object to compare. If None, assume it to be unitless.

  • error_message (str, optional) – An error message used in the UnitMismatchError. May contain {name} placeholders filled from error_arrays.

  • **error_arrays (dict mapping str to Quantity) – Arrays in this dictionary will be converted using the _short_str helper and inserted into error_message.

Return type:

tuple[saiunit.Unit, saiunit.Unit]

Returns:

  • unit1 (Unit) – The physical unit of obj1.

  • unit2 (Unit) – The physical unit of obj2 (or UNITLESS).

Raises:

UnitMismatchError – If the dimensions of obj1 and obj2 do not match (or, if obj2 is None, when obj1 is not unitless).

Notes

Implements special checking for 0, treating it as having “any dimensions”.

Examples

>>> import saiunit as u
>>> u1, u2 = u.fail_for_unit_mismatch(3.0 * u.mV, 5.0 * u.volt)
>>> u1.has_same_dim(u2)
True