def _dims_quotient(dimensions: Dimensions,
                   units_env: dict) -> Optional[Dimensions]:
    """
    Returns a Dimensions object representing the element-wise quotient between
    'dimensions' and a defined unit if 'dimensions' is a scalar multiple
    of a defined unit in the global environment variable.
    Returns None otherwise.
    """
    derived = units_env["derived"]
    defined = units_env["defined"]
    all_units = ChainMap(defined, derived)
    potential_inv = None  # A flag to catch a -1 value (an inversion)
    quotient = None
    quotient_result = None
    for dimension_key in all_units.keys():
        if _check_dims_parallel(dimension_key, dimensions):
            quotient = vec.divide(dimensions, dimension_key, ignore_zeros=True)
            mean = vec.mean(quotient, ignore_empty=True)
            if mean == -1:
                potential_inv = quotient
            elif -1 < mean < 1:
                return None  # Ignore parallel dimensions if they are fractional dimensions
            else:
                quotient_result = quotient
    return quotient_result or potential_inv  # Inversion ok, if only option
def _powers_of_derived(dims: Dimensions, units_env: dict) -> Union[int, float]:
    """
    Returns an integer value that represents the exponent of a unit if the
    dimensions
    array is a multiple of one of the defined derived units in dimension_keys.
    Returns None,
    otherwise.
    e.g. a force would have dimensions = [1,1,-2,0,0,0,0] so a Physical object
    that had dimensions = [2,2,-4,0,0,0,0] would really be a force to the power of
    2.
    This function returns the 2, stating that `dims` is the second power of a
    derived dimension in `units_env`.
    """
    quotient_1 = _dims_quotient(dims, units_env)
    quotient_2 = _dims_basis_multiple(dims)
    quotient_1_mean = None
    if quotient_1 is not None:
        quotient_1_mean = vec.mean(quotient_1, ignore_empty=True)

    if quotient_1 is not None and quotient_1_mean != -1:
        power_of_derived = vec.mean(quotient_1, ignore_empty=True)
        base_dimensions = vec.divide(dims, quotient_1, ignore_zeros=True)
        return ((power_of_derived or 1), base_dimensions)
    elif quotient_1_mean == -1 and quotient_2 is not None:  # Situations like Hz and s
        power_of_basis = vec.mean(quotient_2, ignore_empty=True)
        base_dimensions = vec.divide(dims, quotient_2, ignore_zeros=True)
        return ((power_of_basis or 1), base_dimensions)
    elif quotient_1_mean == -1:  # Now we can proceed with an inverse  unit
        power_of_derived = vec.mean(quotient_1, ignore_empty=True)
        base_dimensions = vec.divide(dims, quotient_1, ignore_zeros=True)
        return ((power_of_derived or 1), base_dimensions)
    elif quotient_2 is not None:
        power_of_basis = vec.mean(quotient_2, ignore_empty=True)
        base_dimensions = vec.divide(dims, quotient_2, ignore_zeros=True)
        return ((power_of_basis or 1), base_dimensions)
    else:
        return (1, dims)
Exemple #3
0
def test_mean():
    assert vec.mean(P6) == 2.3333333333333335
    assert vec.mean(D1) == 0
    assert vec.mean(T2) == 2.8223333333333334
    assert vec.mean(P6, True) == 3.5
    assert vec.mean(L1) == 49.5
Exemple #4
0
def cache_vec_mean(tuple_a, ignore_empty):
    """
    Wraps vec.mean with an lru_cache
    """
    return vec.mean(tuple_a, ignore_empty)