def test_factor_and_dimension(): 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)
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)
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)
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 test_factor_and_dimension_with_Abs(): with warns_deprecated_sympy(): v_w1 = Quantity('v_w1', length / time, Rational(3, 2) * meter / second) v_w1.set_global_relative_scale_factor(Rational(3, 2), meter / second) expr = v_w1 - Abs(v_w1) with warns_deprecated_sympy(): assert (0, length / time) == Quantity._collect_factor_and_dimension(expr)
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
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(Rational(3, 2) * meter / second) v_w2.set_scale_factor(2 * meter / second) expr = Abs(v_w1 / 2 - v_w2) assert (Rational(5, 4), length/time) == \ Quantity._collect_factor_and_dimension(expr) expr = Rational(5, 2) * second / meter * v_w1 - 3000 assert (-(2996 + Rational(1, 4)), Dimension(1)) == \ Quantity._collect_factor_and_dimension(expr) expr = v_w1**(v_w2 / v_w1) assert ((Rational(3, 2))**Rational(4, 3), (length/time)**Rational(4, 3)) == \ Quantity._collect_factor_and_dimension(expr)
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
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 check_unit_consistency(expr): Quantity._collect_factor_and_dimension(expr)
def test_factor_and_dimension_with_Abs(): v_w1 = Quantity('v_w1', length/time, S(3)/2*meter/second) expr = v_w1 - Abs(v_w1) assert (0, lenth/time) == Quantity._collect_factor_and_dimension(expr)