def test_factor_and_dimension(): assert (3000, Dimension(1)) == SI._collect_factor_and_dimension(3000) assert (1001, length) == SI._collect_factor_and_dimension(meter + km) assert (2, length / time) == SI._collect_factor_and_dimension(meter / second + 36 * km / (10 * hour)) x, y = symbols("x y") assert (x + y / 100, length) == SI._collect_factor_and_dimension(x * m + y * centimeter) cH = Quantity("cH") SI.set_quantity_dimension(cH, amount_of_substance / volume) pH = -log(cH) assert (1, volume / amount_of_substance) == SI._collect_factor_and_dimension(exp(pH)) v_w1 = Quantity("v_w1") v_w2 = Quantity("v_w2") v_w1.set_global_relative_scale_factor(Rational(3, 2), meter / second) v_w2.set_global_relative_scale_factor(2, meter / second) expr = Abs(v_w1 / 2 - v_w2) assert (Rational(5, 4), length / time) == SI._collect_factor_and_dimension(expr) expr = Rational(5, 2) * second / meter * v_w1 - 3000 assert (-(2996 + Rational(1, 4)), Dimension(1)) == SI._collect_factor_and_dimension(expr) expr = v_w1**(v_w2 / v_w1) assert ( (Rational(3, 2))**Rational(4, 3), (length / time)**Rational(4, 3), ) == SI._collect_factor_and_dimension(expr) with warns_deprecated_sympy(): assert (3000, Dimension(1)) == Quantity._collect_factor_and_dimension(3000)
def test_issue_20288(): from sympy.core.numbers import E from sympy.physics.units import energy u = Quantity('u') v = Quantity('v') SI.set_quantity_dimension(u, energy) SI.set_quantity_dimension(v, energy) u.set_global_relative_scale_factor(1, joule) v.set_global_relative_scale_factor(1, joule) expr = 1 + exp(u**2 / v**2) assert SI._collect_factor_and_dimension(expr) == (1 + E, Dimension(1))
def test_conversion_with_2_nonstandard_dimensions(): smartness = Dimension("smartness") generousness = Dimension("generousness") good_grade = Quantity("good_grade") kilo_good_grade = Quantity("kilo_good_grade") centi_good_grade = Quantity("centi_good_grade") kilo_good_grade.set_global_relative_scale_factor(1000, good_grade) centi_good_grade.set_global_relative_scale_factor(S.One/10**5, kilo_good_grade) charity_points = Quantity("charity_points") milli_charity_points = Quantity("milli_charity_points") missions = Quantity("missions") milli_charity_points.set_global_relative_scale_factor(S.One/1000, charity_points) missions.set_global_relative_scale_factor(251, charity_points) assert convert_to( kilo_good_grade*milli_charity_points*millimeter, [centi_good_grade, missions, centimeter] ) == S.One * 10**5 / (251*1000) / 10 * centi_good_grade*missions*centimeter
def test_quantity_abs(): v_w1 = Quantity('v_w1') v_w2 = Quantity('v_w2') v_w3 = Quantity('v_w3') v_w1.set_global_relative_scale_factor(1, meter / second) v_w2.set_global_relative_scale_factor(1, meter / second) v_w3.set_global_relative_scale_factor(1, meter / second) expr = v_w3 - Abs(v_w1 - v_w2) assert SI.get_dimensional_expr(v_w1) == (length / time).name Dq = Dimension(SI.get_dimensional_expr(expr)) with warns_deprecated_sympy(): Dq1 = Dimension(Quantity.get_dimensional_expr(expr)) assert Dq == Dq1 assert SI.get_dimension_system().get_dimensional_dependencies(Dq) == { 'length': 1, 'time': -1, } assert meter == sqrt(meter**2)
def test_quantity_postprocessing(): q1 = Quantity('q1') q2 = Quantity('q2') SI.set_quantity_dimension(q1, length*pressure**2*temperature/time) SI.set_quantity_dimension(q2, energy*pressure*temperature/(length**2*time)) assert q1 + q2 q = q1 + q2 Dq = Dimension(SI.get_dimensional_expr(q)) assert SI.get_dimension_system().get_dimensional_dependencies(Dq) == { 'length': -1, 'mass': 2, 'temperature': 1, 'time': -5, }
def test_quantity_postprocessing(): q1 = Quantity("q1") q2 = Quantity("q2") SI.set_quantity_dimension(q1, length * pressure**2 * temperature / time) SI.set_quantity_dimension( q2, energy * pressure * temperature / (length**2 * time)) assert q1 + q2 q = q1 + q2 Dq = Dimension(SI.get_dimensional_expr(q)) assert SI.get_dimension_system().get_dimensional_dependencies(Dq) == { "length": -1, "mass": 2, "temperature": 1, "time": -5, }