本文整理汇总了Python中sympy.physics.units.quantities.Quantity类的典型用法代码示例。如果您正苦于以下问题:Python Quantity类的具体用法?Python Quantity怎么用?Python Quantity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Quantity类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: test_mul_div
def test_mul_div():
u = Quantity("u", length, 10)
assert 1 / u == u**(-1)
assert u / 1 == u
v1 = u / Quantity("t", time, 2)
v2 = Quantity("v", length / time, 5)
# Pow only supports structural equality:
assert v1 != v2
assert v1 == v2.convert_to(v1)
# TODO: decide whether to allow such expression in the future
# (requires somehow manipulating the core).
#assert u / Quantity(length, 2) == 5
assert u * 1 == u
ut1 = u * Quantity("t", time, 2)
ut2 = Quantity("ut", length*time, 20)
# Mul only supports structural equality:
assert ut1 != ut2
assert ut1 == ut2.convert_to(ut1)
# Mul only supports structural equality:
assert u * Quantity("lp1", length**-1, 2) != 20
assert u**0 == 1
assert u**1 == u
# TODO: Pow only support structural equality:
assert u ** 2 != Quantity("u2", length ** 2, 100)
assert u ** -1 != Quantity("u3", length ** -1, 0.1)
assert u ** 2 == Quantity("u2", length ** 2, 100).convert_to(u)
assert u ** -1 == Quantity("u3", length ** -1, S.One/10).convert_to(u)
开发者ID:josephwillard,项目名称:sympy,代码行数:37,代码来源:test_quantities.py
示例17: 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).
"""
SymPyDeprecationWarning(
deprecated_since_version="1.2",
feature="dimensional simplification function",
issue=13336,
useinstead="don't use",
).warn()
_, expr = Quantity._collect_factor_and_dimension(expr)
return expr
开发者ID:bjodah,项目名称:sympy,代码行数:19,代码来源:util.py
示例18: check_dimensions
def check_dimensions(expr):
"""Return expr if there are not unitless values added to
dimensional quantities, else raise a ValueError."""
from sympy.solvers.solveset import _term_factors
# the case of adding a number to a dimensional quantity
# is ignored for the sake of SymPy core routines, so this
# function will raise an error now if such an addend is
# found.
# Also, when doing substitutions, multiplicative constants
# might be introduced, so remove those now
adds = expr.atoms(Add)
DIM_OF = dimsys_default.get_dimensional_dependencies
for a in adds:
deset = set()
for ai in a.args:
if ai.is_number:
deset.add(())
continue
dims = []
skip = False
for i in Mul.make_args(ai):
if i.has(Quantity):
i = Dimension(Quantity.get_dimensional_expr(i))
if i.has(Dimension):
dims.extend(DIM_OF(i).items())
elif i.free_symbols:
skip = True
break
if not skip:
deset.add(tuple(sorted(dims)))
if len(deset) > 1:
raise ValueError(
"addends have incompatible dimensions")
# clear multiplicative constants on Dimensions which may be
# left after substitution
reps = {}
for m in expr.atoms(Mul):
if any(isinstance(i, Dimension) for i in m.args):
reps[m] = m.func(*[
i for i in m.args if not i.is_number])
return expr.xreplace(reps)
开发者ID:bjodah,项目名称:sympy,代码行数:43,代码来源:util.py
示例19: test_get_dimensional_expr_with_function
def test_get_dimensional_expr_with_function():
v_w1 = Quantity('v_w1', length / time, meter / second)
assert Quantity.get_dimensional_expr(sin(v_w1)) == \
sin(Quantity.get_dimensional_expr(v_w1))
开发者ID:baoqchau,项目名称:sympy,代码行数:4,代码来源:test_quantities.py
示例20: test_get_dimensional_expr_with_function_1
def test_get_dimensional_expr_with_function_1():
v_w1 = Quantity('v_w1', length / time, meter / second)
v_w2 = Quantity('v_w2', length / time, meter / second)
assert Quantity.get_dimensional_expr(sin(v_w1/v_w2)) == 1
开发者ID:baoqchau,项目名称:sympy,代码行数:4,代码来源:test_quantities.py
注:本文中的sympy.physics.units.quantities.Quantity类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论