当前位置: 首页>>代码示例>>Python>>正文


Python quantities.Quantity类代码示例

本文整理汇总了Python中sympy.physics.units.quantities.Quantity的典型用法代码示例。如果您正苦于以下问题:Python Quantity类的具体用法?Python Quantity怎么用?Python Quantity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Quantity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: prefix_unit

def prefix_unit(unit, prefixes):
    """
    Return a list of all units formed by unit and the given prefixes.

    You can use the predefined PREFIXES or BIN_PREFIXES, but you can also
    pass as argument a subdict of them if you don't want all prefixed units.

        >>> from sympy.physics.units.prefixes import (PREFIXES,
        ...                                                 prefix_unit)
        >>> from sympy.physics.units.systems import MKS
        >>> from sympy.physics.units import m
        >>> pref = {"m": PREFIXES["m"], "c": PREFIXES["c"], "d": PREFIXES["d"]}
        >>> prefix_unit(m, pref)  #doctest: +SKIP
        [cm, dm, mm]
    """

    from sympy.physics.units.quantities import Quantity

    prefixed_units = []

    for prefix_abbr, prefix in prefixes.items():
        quantity = Quantity(
                "%s%s" % (prefix.name, unit.name),
                abbrev=("%s%s" % (prefix.abbrev, unit.abbrev))
           )
        quantity.set_dimension(unit.dimension)
        quantity.set_scale_factor(unit.scale_factor*prefix)
        prefixed_units.append(quantity)

    return prefixed_units
开发者ID:KonstantinTogoi,项目名称:sympy,代码行数:30,代码来源:prefixes.py

示例2: test_extend

def test_extend():
    ms = UnitSystem((m, s), (c,))
    Js = Quantity("Js")
    Js.set_dimension(action)
    Js.set_scale_factor(1)
    mks = ms.extend((kg,), (Js,))

    res = UnitSystem((m, s, kg), (c, Js))
    assert set(mks._base_units) == set(res._base_units)
    assert set(mks._units) == set(res._units)
开发者ID:KonstantinTogoi,项目名称:sympy,代码行数:10,代码来源:test_unitsystem.py

示例3: test_add_sub

def test_add_sub():
    u = Quantity("u", length, 10)
    v = Quantity("v", length, 5)
    w = Quantity("w", time, 2)

    assert isinstance(u + v, Add)
    assert (u + v.convert_to(u)) == (1 + S.Half)*u
    # TODO: eventually add this:
    # assert (u + v).convert_to(u) == (1 + S.Half)*u
    assert isinstance(u - v, Add)
    assert (u - v.convert_to(u)) == S.Half*u
开发者ID:josephwillard,项目名称:sympy,代码行数:11,代码来源:test_quantities.py

示例4: test_convert_to

def test_convert_to():
    q = Quantity("q1", length, 5000)
    assert q.convert_to(m) == 5000*m

    assert speed_of_light.convert_to(m / s) == 299792458 * m / s
    # TODO: eventually support this kind of conversion:
    # assert (2*speed_of_light).convert_to(m / s) == 2 * 299792458 * m / s
    assert day.convert_to(s) == 86400*s

    # Wrong dimension to convert:
    assert q.convert_to(s) == q
    assert speed_of_light.convert_to(m) == speed_of_light
开发者ID:josephwillard,项目名称:sympy,代码行数:12,代码来源:test_quantities.py

示例5: test_print_unit_base

def test_print_unit_base():
    A = Quantity("A")
    A.set_dimension(current)
    A.set_scale_factor(S.One)

    Js = Quantity("Js")
    Js.set_dimension(action)
    Js.set_scale_factor(S.One)

    mksa = UnitSystem((m, kg, s, A), (Js,))
    with warns_deprecated_sympy():
        assert mksa.print_unit_base(Js) == m**2*kg*s**-1
开发者ID:asmeurer,项目名称:sympy,代码行数:12,代码来源:test_unitsystem.py

示例6: test_print_unit_base

def test_print_unit_base():
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)

        A = Quantity("A")
        A.set_dimension(current)
        A.set_scale_factor(S.One)

        Js = Quantity("Js")
        Js.set_dimension(action)
        Js.set_scale_factor(S.One)

        mksa = UnitSystem((m, kg, s, A), (Js,))
        assert mksa.print_unit_base(Js) == m**2*kg*s**-1/1000
开发者ID:KonstantinTogoi,项目名称:sympy,代码行数:14,代码来源:test_unitsystem.py

示例7: test_factor_and_dimension

def test_factor_and_dimension():
    assert (3000, Dimension(1)) == Quantity._collect_factor_and_dimension(3000)
    assert (1001, length) == Quantity._collect_factor_and_dimension(meter + km)
    assert (2, length/time) == Quantity._collect_factor_and_dimension(
        meter/second + 36*km/(10*hour))

    x, y = symbols('x y')
    assert (x + y/100, length) == Quantity._collect_factor_and_dimension(
        x*m + y*centimeter)

    cH = Quantity('cH', amount_of_substance/volume)
    pH = -log(cH)

    assert (1, volume/amount_of_substance) == Quantity._collect_factor_and_dimension(
        exp(pH))

    v_w1 = Quantity('v_w1', length/time, S(3)/2*meter/second)
    v_w2 = Quantity('v_w2', length/time, 2*meter/second)
    expr = Abs(v_w1/2 - v_w2)
    assert (S(5)/4, length/time) == \
        Quantity._collect_factor_and_dimension(expr)

    expr = S(5)/2*second/meter*v_w1 - 3000
    assert (-(2996 + S(1)/4), Dimension(1)) == \
        Quantity._collect_factor_and_dimension(expr)

    expr = v_w1**(v_w2/v_w1)
    assert ((S(3)/2)**(S(4)/3), (length/time)**(S(4)/3)) == \
        Quantity._collect_factor_and_dimension(expr)
开发者ID:josephwillard,项目名称:sympy,代码行数:29,代码来源:test_quantities.py

示例8: test_dimensional_expr_of_derivative

def test_dimensional_expr_of_derivative():
    l = Quantity('l', length, 36 * km)
    t = Quantity('t', time, hour)
    t1 = Quantity('t1', time, second)
    x = Symbol('x')
    y = Symbol('y')
    f = Function('f')
    dfdx = f(x, y).diff(x, y)
    dl_dt = dfdx.subs({f(x, y): l, x: t, y: t1})
    assert Quantity.get_dimensional_expr(dl_dt) ==\
        Quantity.get_dimensional_expr(l / t / t1) ==\
        Symbol("length")/Symbol("time")**2
    assert Quantity._collect_factor_and_dimension(dl_dt) ==\
        Quantity._collect_factor_and_dimension(l / t / t1) ==\
        (10, length/time**2)
开发者ID:josephwillard,项目名称:sympy,代码行数:15,代码来源:test_quantities.py

示例9: _get_conversion_matrix_for_expr

def _get_conversion_matrix_for_expr(expr, target_units):
    from sympy import Matrix

    expr_dim = Dimension(Quantity.get_dimensional_expr(expr))
    dim_dependencies = expr_dim.get_dimensional_dependencies(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 x.get_dimensional_dependencies(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([[i.get_dimensional_dependencies(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
开发者ID:sixpearls,项目名称:sympy,代码行数:19,代码来源:util.py

示例10: __add__

 def __add__(self, other):
     from sympy.physics.units.quantities import Quantity
     other = sympify(other)
     if isinstance(other, Basic):
         if other.has(Quantity):
             other = Dimension(Quantity.get_dimensional_expr(other))
         if isinstance(other, Dimension) and self == other:
             return self
         return super(Dimension, self).__add__(other)
     return self
开发者ID:bjodah,项目名称:sympy,代码行数:10,代码来源:dimensions.py

示例11: test_definition

def test_definition():
    # want to test if the system can have several units of the same dimension
    dm = Quantity("dm")
    dm.set_dimension(length)

    dm.set_scale_factor(Rational(1, 10))

    base = (m, s)
    base_dim = (m.dimension, s.dimension)
    ms = UnitSystem(base, (c, dm), "MS", "MS system")

    assert set(ms._base_units) == set(base)
    assert set(ms._units) == set((m, s, c, dm))
    #assert ms._units == DimensionSystem._sort_dims(base + (velocity,))
    assert ms.name == "MS"
    assert ms.descr == "MS system"

    assert ms._system.base_dims == base_dim
    assert ms._system.derived_dims == (velocity,)
开发者ID:KonstantinTogoi,项目名称:sympy,代码行数:19,代码来源:test_unitsystem.py

示例12: __mul__

 def __mul__(self, other):
     from sympy.physics.units.quantities import Quantity
     if isinstance(other, Basic):
         if other.has(Quantity):
             other = Dimension(Quantity.get_dimensional_expr(other))
         if isinstance(other, Dimension):
             return Dimension(self.name*other.name)
         if not other.free_symbols: # other.is_number cannot be used
             return self
         return super(Dimension, self).__mul__(other)
     return self
开发者ID:bjodah,项目名称:sympy,代码行数:11,代码来源:dimensions.py

示例13: test_quantity_postprocessing

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,
    }
开发者ID:baoqchau,项目名称:sympy,代码行数:12,代码来源:test_quantities.py

示例14: test_quantity_abs

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)
开发者ID:baoqchau,项目名称:sympy,代码行数:12,代码来源:test_quantities.py

示例15: dim_simplify

def dim_simplify(expr):
    """
    NOTE: this function could be deprecated in the future.

    Simplify expression by recursively evaluating the dimension arguments.

    This function proceeds to a very rough dimensional analysis. It tries to
    simplify expression with dimensions, and it deletes all what multiplies a
    dimension without being a dimension. This is necessary to avoid strange
    behavior when Add(L, L) be transformed into Mul(2, L).
    """
    _, expr = Quantity._collect_factor_and_dimension(expr)
    return expr
开发者ID:sixpearls,项目名称:sympy,代码行数:13,代码来源:util.py


注:本文中的sympy.physics.units.quantities.Quantity类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。