def test_deprecated_quantity_methods(): step = Quantity("step") with warns_deprecated_sympy(): step.set_dimension(length) step.set_scale_factor(2*meter) assert convert_to(step, centimeter) == 200*centimeter assert convert_to(1000*step/second, kilometer/second) == 2*kilometer/second
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
def test_factor_and_dimension_with_Abs(): with warns_deprecated_sympy(): v_w1 = Quantity('v_w1', length/time, S(3)/2*meter/second) v_w1.set_dimension(length/time) v_w1.set_scale_factor(S(3)/2*meter/second) expr = v_w1 - Abs(v_w1) assert (0, length/time) == Quantity._collect_factor_and_dimension(expr)
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
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)
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)
def test_get_dimensional_expr_with_function(): v_w1 = Quantity('v_w1') v_w2 = Quantity('v_w2') v_w1.set_dimension(length/time) v_w2.set_dimension(length/time) v_w1.set_scale_factor(meter/second) v_w2.set_scale_factor(meter/second) assert Quantity.get_dimensional_expr(sin(v_w1)) == \ sin(Quantity.get_dimensional_expr(v_w1)) assert Quantity.get_dimensional_expr(sin(v_w1/v_w2)) == 1
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
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
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
def test_convert_to(): q = Quantity("q1") q.set_dimension(length) q.set_scale_factor(S(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
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,)
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, )
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') cH.set_dimension(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') v_w2 = Quantity('v_w2') v_w1.set_dimension(length / time) v_w2.set_dimension(length / time) v_w1.set_scale_factor(S(3) / 2 * meter / second) v_w2.set_scale_factor(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)
def test_Quantity_definition(): q = Quantity("s10", abbrev="sabbr") q.set_dimension(time) q.set_scale_factor(10) u = Quantity("u", abbrev="dam") u.set_dimension(length) u.set_scale_factor(10) km = Quantity("km") km.set_dimension(length) km.set_scale_factor(kilo) v = Quantity("u") v.set_dimension(length) v.set_scale_factor(5*kilo) assert q.scale_factor == 10 assert q.dimension == time assert q.abbrev == Symbol("sabbr") assert u.dimension == length assert u.scale_factor == 10 assert u.abbrev == Symbol("dam") assert km.scale_factor == 1000 assert km.func(*km.args) == km assert km.func(*km.args).args == km.args assert v.dimension == length assert v.scale_factor == 5000 with warns_deprecated_sympy(): Quantity('invalid', 'dimension', 1) with warns_deprecated_sympy(): Quantity('mismatch', dimension=length, scale_factor=kg)
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') cH.set_dimension(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') v_w2 = Quantity('v_w2') v_w1.set_dimension(length/time) v_w2.set_dimension(length/time) v_w1.set_scale_factor(S(3)/2*meter/second) v_w2.set_scale_factor(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)
def test_add_sub(): u = Quantity("u") v = Quantity("v") w = Quantity("w") u.set_dimension(length) v.set_dimension(length) w.set_dimension(time) u.set_scale_factor(S(10)) v.set_scale_factor(S(5)) w.set_scale_factor(S(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
def test_check_unit_consistency(): u = Quantity("u") v = Quantity("v") w = Quantity("w") u.set_dimension(length) v.set_dimension(length) w.set_dimension(time) u.set_scale_factor(S(10)) v.set_scale_factor(S(5)) w.set_scale_factor(S(2)) def check_unit_consistency(expr): Quantity._collect_factor_and_dimension(expr) raises(ValueError, lambda: check_unit_consistency(u + w)) raises(ValueError, lambda: check_unit_consistency(u - w)) raises(ValueError, lambda: check_unit_consistency(u + 1)) raises(ValueError, lambda: check_unit_consistency(u - 1))
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)
def test_dimensional_expr_of_derivative(): l = Quantity('l') t = Quantity('t') t1 = Quantity('t1') l.set_dimension(length) t.set_dimension(time) t1.set_dimension(time) l.set_scale_factor(36*km) t.set_scale_factor(hour) t1.set_scale_factor(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)
def test_abbrev(): u = Quantity("u") u.set_dimension(length) u.set_scale_factor(S.One) assert u.name == Symbol("u") assert u.abbrev == Symbol("u") u = Quantity("u", abbrev="om") u.set_dimension(length) u.set_scale_factor(S(2)) assert u.name == Symbol("u") assert u.abbrev == Symbol("om") assert u.scale_factor == 2 assert isinstance(u.scale_factor, Number) u = Quantity("u", abbrev="ikm") u.set_dimension(length) u.set_scale_factor(3*kilo) assert u.abbrev == Symbol("ikm") assert u.scale_factor == 3000
def test_mul_div(): u = Quantity("u") v = Quantity("v") t = Quantity("t") ut = Quantity("ut") v2 = Quantity("v") u.set_dimension(length) v.set_dimension(length) t.set_dimension(time) ut.set_dimension(length*time) v2.set_dimension(length/time) u.set_scale_factor(S(10)) v.set_scale_factor(S(5)) t.set_scale_factor(S(2)) ut.set_scale_factor(S(20)) v2.set_scale_factor(S(5)) assert 1 / u == u**(-1) assert u / 1 == u v1 = u / t v2 = v # 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('l2', dimension=length, scale_factor=2) == 5 assert u * 1 == u ut1 = u * t ut2 = ut # Mul only supports structural equality: assert ut1 != ut2 assert ut1 == ut2.convert_to(ut1) # Mul only supports structural equality: lp1 = Quantity("lp1") lp1.set_dimension(length**-1) lp1.set_scale_factor(S(2)) assert u * lp1 != 20 assert u**0 == 1 assert u**1 == u # TODO: Pow only support structural equality: u2 = Quantity("u2") u3 = Quantity("u3") u2.set_dimension(length**2) u3.set_dimension(length**-1) u2.set_scale_factor(S(100)) u3.set_scale_factor(S(1)/10) assert u ** 2 != u2 assert u ** -1 != u3 assert u ** 2 == u2.convert_to(u) assert u ** -1 == u3.convert_to(u)
nano, pico, kibi, mebi, gibi, tebi, pebi, exbi) One = S.One #### UNITS #### # Dimensionless: percent = percents = Quantity("percent") percent.set_dimension(One) percent.set_scale_factor(Rational(1, 100)) permille = Quantity("permille") permille.set_dimension(One) permille.set_scale_factor(Rational(1, 1000)) # Angular units (dimensionless) rad = radian = radians = Quantity("radian") radian.set_dimension(One) radian.set_scale_factor(One) deg = degree = degrees = Quantity("degree", abbrev="deg") degree.set_dimension(One) degree.set_scale_factor(pi / 180) sr = steradian = steradians = Quantity("steradian", abbrev="sr") steradian.set_dimension(One) steradian.set_scale_factor(One)
centi, deci, kilo, micro, milli, nano, pico, kibi, mebi, gibi, tebi, pebi, exbi) One = S.One #### UNITS #### # Dimensionless: percent = percents = Quantity("percent", latex_repr=r"\%") percent.set_dimension(One) percent.set_scale_factor(Rational(1, 100)) permille = Quantity("permille") permille.set_dimension(One) permille.set_scale_factor(Rational(1, 1000)) # Angular units (dimensionless) rad = radian = radians = Quantity("radian", abbrev="rad") radian.set_dimension(One) radian.set_scale_factor(One) deg = degree = degrees = Quantity("degree", abbrev="deg", latex_repr=r"^\circ") degree.set_dimension(One) degree.set_scale_factor(pi/180) sr = steradian = steradians = Quantity("steradian", abbrev="sr") steradian.set_dimension(One) steradian.set_scale_factor(One)
centi, deci, kilo, micro, milli, nano, pico, kibi, mebi, gibi, tebi, pebi, exbi) One = S.One #### UNITS #### # Dimensionless: percent = percents = Quantity("percent") percent.set_dimension(One) percent.set_scale_factor(Rational(1, 100)) permille = Quantity("permille") permille.set_dimension(One) permille.set_scale_factor(Rational(1, 1000)) # Angular units (dimensionless) rad = radian = radians = Quantity("radian") radian.set_dimension(One) radian.set_scale_factor(One) deg = degree = degrees = Quantity("degree", abbrev="deg") degree.set_dimension(One) degree.set_scale_factor(pi/180) sr = steradian = steradians = Quantity("steradian", abbrev="sr") steradian.set_dimension(One) steradian.set_scale_factor(One)