-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create law and test * Add definitions * Fix documentation
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
""" |
72 changes: 72 additions & 0 deletions
72
symplyphysics/laws/quantities/quantity_is_linear_density_times_length.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
37 changes: 37 additions & 0 deletions
37
test/quantities/quantity_is_linear_density_times_length_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |