fail_for_dimension_mismatch

fail_for_dimension_mismatch#

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

Compare the dimensions of two objects.

If the dimensions do not match a DimensionMismatchError 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 dimensionless.

  • error_message (str, optional) – An error message that is used in the DimensionMismatchError. May contain {name} placeholders that will be 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.

Returns:

  • dim1 (Dimension) – The physical dimension of obj1.

  • dim2 (Dimension) – The physical dimension of obj2 (or DIMENSIONLESS).

Raises:

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

Notes

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

Examples

>>> import saiunit as u
>>> d1, d2 = u.fail_for_dimension_mismatch(3.0 * u.volt, 5.0 * u.volt)
>>> d1 == d2
True