diff --git a/symplyphysics/laws/quantities/__init__.py b/symplyphysics/laws/quantities/__init__.py index e69de29bb..2cab4f177 100644 --- a/symplyphysics/laws/quantities/__init__.py +++ b/symplyphysics/laws/quantities/__init__.py @@ -0,0 +1,14 @@ +""" +Quantities +========== + +Physical quantities can often be categorized as being either *extensive* or *intensive*, according to +how the property changes when the size or extent of the system changes. + +Intensive quantity + A quantity whose magnitude is independent of the size of the system. + +Extensive quantity + A quantity whose magnitude scales linearly with the size of the system, + i.e. it is additive with respect to its subsystems. +""" diff --git a/symplyphysics/laws/quantities/quantity_is_linear_density_times_length.py b/symplyphysics/laws/quantities/quantity_is_linear_density_times_length.py new file mode 100644 index 000000000..85032ae49 --- /dev/null +++ b/symplyphysics/laws/quantities/quantity_is_linear_density_times_length.py @@ -0,0 +1,72 @@ +""" +Extensive quantity is linear density times length +================================================= + +An extensive quantity of interest can be obtained by multiplying the corresponding linear +density by length. +""" + +from sympy import Eq, Symbol as SymSymbol +from symplyphysics import ( + units, + Quantity, + Symbol, + validate_input, +) +from symplyphysics.core.dimensions import assert_equivalent_dimension + +extensive_quantity = SymSymbol("extensive_quantity") +""" +Extensive quantity + +Symbol: + X +""" + +linear_density = SymSymbol("linear_density") +r""" +Intensive linear density + +Symbol: + lambda_X + +Latex: + :math:`\lambda_X` +""" + +length = Symbol("length", units.length) +""" +Length + +Symbol: + L +""" + +law = Eq(extensive_quantity, linear_density * length) +r""" +X = lambda_X * L + +Latex: + :math:`X = \lambda_X L` +""" + + +@validate_input(length_=length) +def calculate_extensive_quantity( + linear_density_: Quantity, + length_: Quantity, +) -> Quantity: + result_expr = law.rhs.subs({ + linear_density: linear_density_, + length: length_, + }) + result = Quantity(result_expr) + + assert_equivalent_dimension( + result, + "return", + "calculate_extensive_quantity", + linear_density_.dimension * units.length, + ) + + return result diff --git a/test/quantities/quantity_is_linear_density_times_length_test.py b/test/quantities/quantity_is_linear_density_times_length_test.py new file mode 100644 index 000000000..07ae25092 --- /dev/null +++ b/test/quantities/quantity_is_linear_density_times_length_test.py @@ -0,0 +1,37 @@ +from collections import namedtuple +from pytest import fixture, raises +from symplyphysics import ( + assert_equal, + units, + Quantity, + errors, +) +from symplyphysics.laws.quantities import quantity_is_linear_density_times_length as law + +Args = namedtuple("Args", "m q l") + + +@fixture(name="test_args") +def test_args_fixture() -> Args: + m = Quantity(4.4 * units.kilogram / units.meter) + q = Quantity(3.5 * units.coulomb / units.meter) + l = Quantity(0.1 * units.meter) + return Args(m=m, q=q, l=l) + + +def test_mass_law(test_args: Args) -> None: + result = law.calculate_extensive_quantity(test_args.m, test_args.l) + assert_equal(result, 0.44 * units.kilogram) + + +def test_charge_law(test_args: Args) -> None: + result = law.calculate_extensive_quantity(test_args.q, test_args.l) + assert_equal(result, 0.35 * units.coulomb) + + +def test_bad_length(test_args: Args) -> None: + lb = Quantity(units.second) + with raises(errors.UnitsError): + law.calculate_extensive_quantity(test_args.m, lb) + with raises(TypeError): + law.calculate_extensive_quantity(test_args.m, 100)