Пример #1
0
def test_dim_simplify_dimless():
    # TODO: this should be somehow simplified on its own,
    # without the need of calling `dim_simplify`:
    with warns_deprecated_sympy():
        assert dim_simplify(sin(L*L**-1)**2*L).get_dimensional_dependencies()\
               == dimsys_default.get_dimensional_dependencies(L)
    with warns_deprecated_sympy():
        assert dim_simplify(sin(L * L**(-1))**2 * L).get_dimensional_dependencies()\
               == dimsys_default.get_dimensional_dependencies(L)
Пример #2
0
def test_dim_simplify_dimless():
    # TODO: this should be somehow simplified on its own,
    # without the need of calling `dim_simplify`:
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)

        assert dim_simplify(sin(L*L**-1)**2*L).get_dimensional_dependencies()\
               == dimsys_default.get_dimensional_dependencies(L)
        assert dim_simplify(sin(L * L**(-1))**2 * L).get_dimensional_dependencies()\
               == dimsys_default.get_dimensional_dependencies(L)
Пример #3
0
def test_Dimension_definition():
    with warns_deprecated_sympy():
        assert length.get_dimensional_dependencies() == {"length": 1}
    assert dimsys_default.get_dimensional_dependencies(length) == {"length": 1}
    assert length.name == Symbol("length")
    assert length.symbol == Symbol("L")

    halflength = sqrt(length)
    with warns_deprecated_sympy():
        assert halflength.get_dimensional_dependencies() == {"length": S.Half}
    assert dimsys_default.get_dimensional_dependencies(halflength) == {"length": S.Half}
Пример #4
0
def test_Dimension_definition():
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)
        assert length.get_dimensional_dependencies() == {"length": 1}
    assert dimsys_default.get_dimensional_dependencies(length) == {"length": 1}
    assert length.name == Symbol("length")
    assert length.symbol == Symbol("L")

    halflength = sqrt(length)
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)
        assert halflength.get_dimensional_dependencies() == {"length": S.Half}
    assert dimsys_default.get_dimensional_dependencies(halflength) == {"length": S.Half}
Пример #5
0
def _get_conversion_matrix_for_expr(expr, target_units):
    from sympy import Matrix

    expr_dim = Dimension(Quantity.get_dimensional_expr(expr))
    dim_dependencies = dimsys_default.get_dimensional_dependencies(expr_dim, mark_dimensionless=True)
    target_dims = [Dimension(Quantity.get_dimensional_expr(x)) for x in target_units]
    canon_dim_units = {i for x in target_dims for i in dimsys_default.get_dimensional_dependencies(x, mark_dimensionless=True)}
    canon_expr_units = {i for i in dim_dependencies}

    if not canon_expr_units.issubset(canon_dim_units):
        return None

    canon_dim_units = sorted(canon_dim_units)

    camat = Matrix([[dimsys_default.get_dimensional_dependencies(i, mark_dimensionless=True).get(j, 0)  for i in target_dims] for j in canon_dim_units])
    exprmat = Matrix([dim_dependencies.get(k, 0) for k in canon_dim_units])

    res_exponents = camat.solve_least_squares(exprmat, method=None)
    return res_exponents
Пример #6
0
def test_quantity_postprocessing():
    q1 = Quantity('q1', length*pressure**2*temperature/time)
    q2 = Quantity('q2', energy*pressure*temperature/(length**2*time))
    assert q1 + q2
    q = q1 + q2
    Dq = Dimension(Quantity.get_dimensional_expr(q))
    assert dimsys_default.get_dimensional_dependencies(Dq) == {
        'length': -1,
        'mass': 2,
        'temperature': 1,
        'time': -5,
    }
Пример #7
0
def test_quantity_abs():
    v_w1 = Quantity('v_w1', length/time, meter/second)
    v_w2 = Quantity('v_w2', length/time, meter/second)
    v_w3 = Quantity('v_w3', length/time, meter/second)
    expr = v_w3 - Abs(v_w1 - v_w2)

    Dq = Dimension(Quantity.get_dimensional_expr(expr))
    assert dimsys_default.get_dimensional_dependencies(Dq) == {
        'length': 1,
        'time': -1,
    }
    assert meter == sqrt(meter**2)
Пример #8
0
def _Quantity_constructor_postprocessor_Add(expr):
    # Construction postprocessor for the addition,
    # checks for dimension mismatches of the addends, thus preventing
    # expressions like `meter + second` to be created.

    deset = {
        tuple(sorted(dimsys_default.get_dimensional_dependencies(
            Dimension(Quantity.get_dimensional_expr(i) if not i.is_number else 1
        )).items()))
        for i in expr.args
        if i.free_symbols == set()  # do not raise if there are symbols
                    # (free symbols could contain the units corrections)
    }
    # If `deset` has more than one element, then some dimensions do not
    # match in the sum:
    if len(deset) > 1:
        raise ValueError("summation of quantities of incompatible dimensions")
    return expr
Пример #9
0
def test_quantity_abs():
    v_w1 = Quantity('v_w1')
    v_w2 = Quantity('v_w2')
    v_w3 = Quantity('v_w3')

    v_w1.set_dimension(length/time)
    v_w2.set_dimension(length/time)
    v_w3.set_dimension(length/time)

    v_w1.set_scale_factor(meter/second)
    v_w2.set_scale_factor(meter/second)
    v_w3.set_scale_factor(meter/second)

    expr = v_w3 - Abs(v_w1 - v_w2)

    Dq = Dimension(Quantity.get_dimensional_expr(expr))
    assert dimsys_default.get_dimensional_dependencies(Dq) == {
        'length': 1,
        'time': -1,
    }
    assert meter == sqrt(meter**2)
Пример #10
0
def _Quantity_constructor_postprocessor_Add(expr):
    # Construction postprocessor for the addition,
    # checks for dimension mismatches of the addends, thus preventing
    # expressions like `meter + second` to be created.

    deset = {
        tuple(
            sorted(
                dimsys_default.get_dimensional_dependencies(
                    Dimension(
                        Quantity.get_dimensional_expr(i)
                        if not i.is_number else 1)).items()))
        for i in expr.args
        if i.free_symbols == set()  # do not raise if there are symbols
        # (free symbols could contain the units corrections)
    }
    # If `deset` has more than one element, then some dimensions do not
    # match in the sum:
    if len(deset) > 1:
        raise ValueError("summation of quantities of incompatible dimensions")
    return expr
Пример #11
0
def test_Dimension_mul_div_exp():
    assert 2 * length == length * 2 == length / 2 == length
    assert 2 / length == 1 / length
    x = Symbol('x')
    m = x * length
    assert m == length * x and m.is_Mul and set(m.args) == {x, length}
    d = x / length
    assert d == x * length**-1 and d.is_Mul and set(d.args) == {x, 1 / length}
    d = length / x
    assert d == length * x**-1 and d.is_Mul and set(d.args) == {1 / x, length}

    velo = length / time

    assert (length * length) == length**2

    assert dimsys_default.get_dimensional_dependencies(length * length) == {
        "length": 2
    }
    assert dimsys_default.get_dimensional_dependencies(length**2) == {
        "length": 2
    }
    assert dimsys_default.get_dimensional_dependencies(length * time) == {
        "length": 1,
        "time": 1
    }
    assert dimsys_default.get_dimensional_dependencies(velo) == {
        "length": 1,
        "time": -1
    }
    assert dimsys_default.get_dimensional_dependencies(velo**2) == {
        "length": 2,
        "time": -2
    }

    assert dimsys_default.get_dimensional_dependencies(length / length) == {}
    assert dimsys_default.get_dimensional_dependencies(velo / length *
                                                       time) == {}
    assert dimsys_default.get_dimensional_dependencies(length**-1) == {
        "length": -1
    }
    assert dimsys_default.get_dimensional_dependencies(velo**-1.5) == {
        "length": -1.5,
        "time": 1.5
    }

    length_a = length**"a"
    assert dimsys_default.get_dimensional_dependencies(length_a) == {
        "length": Symbol("a")
    }

    assert length != 1
    assert length / length != 1

    length_0 = length**0
    assert dimsys_default.get_dimensional_dependencies(length_0) == {}
Пример #12
0
def test_Dimension_mul_div_exp():
    assert 2*length == length*2 == length/2 == length
    assert 2/length == 1/length
    x = Symbol('x')
    m = x*length
    assert m == length*x and m.is_Mul and set(m.args) == {x, length}
    d = x/length
    assert d == x*length**-1 and d.is_Mul and set(d.args) == {x, 1/length}
    d = length/x
    assert d == length*x**-1 and d.is_Mul and set(d.args) == {1/x, length}

    velo = length / time

    assert (length * length) == length ** 2

    assert dimsys_default.get_dimensional_dependencies(length * length) == {"length": 2}
    assert dimsys_default.get_dimensional_dependencies(length ** 2) == {"length": 2}
    assert dimsys_default.get_dimensional_dependencies(length * time) == { "length": 1, "time": 1}
    assert dimsys_default.get_dimensional_dependencies(velo) == { "length": 1, "time": -1}
    assert dimsys_default.get_dimensional_dependencies(velo ** 2) == {"length": 2, "time": -2}

    assert dimsys_default.get_dimensional_dependencies(length / length) == {}
    assert dimsys_default.get_dimensional_dependencies(velo / length * time) == {}
    assert dimsys_default.get_dimensional_dependencies(length ** -1) == {"length": -1}
    assert dimsys_default.get_dimensional_dependencies(velo ** -1.5) == {"length": -1.5, "time": 1.5}

    length_a = length**"a"
    assert dimsys_default.get_dimensional_dependencies(length_a) == {"length": Symbol("a")}

    assert length != 1
    assert length / length != 1

    length_0 = length ** 0
    assert dimsys_default.get_dimensional_dependencies(length_0) == {}
def test_Dimension_mul_div_exp():
    velo = length / time

    assert (length * length) == length**2

    assert dimsys_default.get_dimensional_dependencies(length * length) == {
        "length": 2
    }
    assert dimsys_default.get_dimensional_dependencies(length**2) == {
        "length": 2
    }
    assert dimsys_default.get_dimensional_dependencies(length * time) == {
        "length": 1,
        "time": 1
    }
    assert dimsys_default.get_dimensional_dependencies(velo) == {
        "length": 1,
        "time": -1
    }
    assert dimsys_default.get_dimensional_dependencies(velo**2) == {
        "length": 2,
        "time": -2
    }

    assert dimsys_default.get_dimensional_dependencies(length / length) == {}
    assert dimsys_default.get_dimensional_dependencies(velo / length *
                                                       time) == {}
    assert dimsys_default.get_dimensional_dependencies(length**-1) == {
        "length": -1
    }
    assert dimsys_default.get_dimensional_dependencies(velo**-1.5) == {
        "length": -1.5,
        "time": 1.5
    }

    length_a = length**"a"
    assert dimsys_default.get_dimensional_dependencies(length_a) == {
        "length": Symbol("a")
    }

    assert length != 1
    assert length / length != 1

    length_0 = length**0
    assert dimsys_default.get_dimensional_dependencies(length_0) == {}
Пример #14
0
def test_Dimension_mul_div_exp():
    velo = length / time

    assert (length * length) == length ** 2

    assert dimsys_default.get_dimensional_dependencies(length * length) == {"length": 2}
    assert dimsys_default.get_dimensional_dependencies(length ** 2) == {"length": 2}
    assert dimsys_default.get_dimensional_dependencies(length * time) == { "length": 1, "time": 1}
    assert dimsys_default.get_dimensional_dependencies(velo) == { "length": 1, "time": -1}
    assert dimsys_default.get_dimensional_dependencies(velo ** 2) == {"length": 2, "time": -2}

    assert dimsys_default.get_dimensional_dependencies(length / length) == {}
    assert dimsys_default.get_dimensional_dependencies(velo / length * time) == {}
    assert dimsys_default.get_dimensional_dependencies(length ** -1) == {"length": -1}
    assert dimsys_default.get_dimensional_dependencies(velo ** -1.5) == {"length": -1.5, "time": 1.5}

    length_a = length**"a"
    assert dimsys_default.get_dimensional_dependencies(length_a) == {"length": Symbol("a")}

    assert length != 1
    assert length / length != 1

    length_0 = length ** 0
    assert dimsys_default.get_dimensional_dependencies(length_0) == {}