Skip to content

Commit

Permalink
Fixes and more autogenerated laws (#900)
Browse files Browse the repository at this point in the history
* Fixes and more autogenerated laws

* Update __init__

* Fix plot

* Fix law derivation
  • Loading branch information
blackyblack authored Sep 7, 2024
1 parent a29694e commit 00959ee
Show file tree
Hide file tree
Showing 63 changed files with 404 additions and 618 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sympy import Symbol, plot, sqrt
from sympy.physics.units import speed_of_light
from symplyphysics import Vector, vector_magnitude
from symplyphysics.core.symbols.quantities import evaluate_expression
from symplyphysics.laws.relativistic.vector import force_acceleration_relation

Datum = namedtuple("Datum", "label acceleration")
Expand Down Expand Up @@ -37,9 +38,9 @@
for datum_ in data_:
vector = force_acceleration_relation.force_law(datum_.acceleration, velocity)
expr = vector_magnitude(vector).subs(force_acceleration_relation.rest_mass, 1)

expr_value = evaluate_expression(expr)
sub_plot = plot(
expr,
expr_value,
(reduced_velocity, 0, 0.97),
label=datum_.label,
show=False,
Expand Down
19 changes: 19 additions & 0 deletions symplyphysics/core/symbols/quantities.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def __new__(cls,
display_latex: Optional[str] = None,
**assumptions: Any) -> Self:
name = next_name("QTY")
# Latex symbol is set in SymPy Quantity, not in DimensionSymbol, due to Latex printer
# specifics
display_symbol = name if display_symbol is None else display_symbol
display_latex = display_symbol if display_latex is None else display_latex
obj = SymQuantity.__new__(cls, name, None, display_latex, None, None, None, False,
**assumptions)
return obj
Expand Down Expand Up @@ -75,3 +79,18 @@ def evaluate_quantity(quantity: Expr, **kwargs: Any) -> Quantity:
scale_factor_ = quantity.scale_factor.evalf(**kwargs)
dimension = quantity.dimension
return Quantity(scale_factor_, dimension=dimension)


def evaluate_expression(expr: Expr, **kwargs: Any) -> Expr:
quantities = []
def _evaluate_inner(expr: Expr) -> None:
for arg in expr.args:
if isinstance(arg, SymQuantity):
quantities.append(arg)
continue
if isinstance(arg, Expr):
_evaluate_inner(arg)
_evaluate_inner(expr)
for q in quantities:
expr = expr.subs(q, q.scale_factor.evalf(**kwargs))
return expr
3 changes: 3 additions & 0 deletions symplyphysics/core/symbols/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ def __init__(cls,
display_name = str(cls.name) if display_symbol is None else display_symbol
super().__init__(display_name, dimension, display_latex=display_latex)

def __repr__(cls) -> str:
return str(cls.display_name)


# Symbol and Function have generated names, hence their display is not readable.
# Use custom implementation of the PrettyPrinter to convert real symbol names
Expand Down
4 changes: 1 addition & 3 deletions symplyphysics/definitions/admittance_is_inverse_impedance.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
r"""
:code:`Y = 1 / Z`
Latex:
.. math::
Y = \frac{1}{Z}
:laws:latex::
"""


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,48 @@
**Notation:**
#. :math:`\exp` is the exponential function.
#. :math:`k_\text{B}` (:code:`k_B`) is the Boltzmann constant.
"""

from sympy import Eq, exp
from symplyphysics import (
quantities,
units,
dimensionless,
Quantity,
Symbol,
validate_input,
validate_output,
symbols,
clone_symbol,
convert_to_float,
SymbolNew,
)

boltzmann_factor = Symbol("boltzmann_factor", dimensionless)
boltzmann_factor = SymbolNew("f", dimensionless)
"""
Boltzmann factor.
Symbol:
:code:`f`
"""

energy_of_state = Symbol("energy_of_state", units.energy)
energy_of_state = SymbolNew("E[i]", units.energy, display_latex="E_i")
"""
Energy of state :math:`i`.
Symbol:
:code:`E_i`
Latex:
:math:`E_i`
"""

equilibrium_temperature = clone_symbol(symbols.thermodynamics.temperature)
"""
Equilibrium :attr:`~symplyphysics.symbols.thermodynamics.temperature` of the system.
"""

boltzmann_constant = quantities.boltzmann_constant
"""
:attr:`~symplyphysics.quantities.boltzmann_constant`
"""

definition = Eq(boltzmann_factor,
exp(-1 * energy_of_state / (units.boltzmann_constant * equilibrium_temperature)))
r"""
:code:`f = exp(-1 * E_i / (k_B * T))`
exp(-1 * energy_of_state / (boltzmann_constant * equilibrium_temperature)))
"""
:laws:symbol::
Latex:
.. math::
f = \exp{\left( - \frac{E_i}{k_\text{B} T} \right)}
:laws:latex::
"""


Expand Down
25 changes: 7 additions & 18 deletions symplyphysics/definitions/capacitance_from_charge_and_voltage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,28 @@
"""

from sympy import Eq, solve
from symplyphysics import units, Quantity, Symbol, validate_input, validate_output
from symplyphysics import units, Quantity, SymbolNew, validate_input, validate_output

capacitance = Symbol("capacitance", units.capacitance)
capacitance = SymbolNew("C", units.capacitance)
"""
Capacitance of the object.
Symbol:
:code:`C`
"""

charge = Symbol("charge", units.charge)
charge = SymbolNew("q", units.charge)
"""
Charge accumulated in the object.
Symbol:
:code:`q`
"""

voltage = Symbol("voltage", units.voltage)
voltage = SymbolNew("V", units.voltage)
"""
Voltage across the object.
Symbol:
:code:`V`
"""

definition = Eq(capacitance, charge / voltage)
r"""
:code:`C = q / V`
"""
:laws:symbol::
Latex:
.. math::
C = \frac{q}{V}
:laws:latex::
"""


Expand Down
20 changes: 7 additions & 13 deletions symplyphysics/definitions/compliance_is_inverse_stiffness.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,28 @@
from symplyphysics import (
units,
Quantity,
Symbol,
SymbolNew,
validate_input,
validate_output,
)

compliance = Symbol("compliance", units.length / units.force)
compliance = SymbolNew("c", units.length / units.force)
"""
Compliance of the spring.
Symbol:
:code:`c`
"""

stiffness = Symbol("stiffness", units.force / units.length)
stiffness = SymbolNew("k", units.force / units.length)
"""
Stiffness of the spring.
Symbol:
:code:`k`
"""

# TODO: use autogenerated representation, but fix (1 / k) code representation before

definition = Eq(compliance, 1 / stiffness)
r"""
"""
:code:`c = 1 / k`
Latex:
.. math::
c = \frac{1}{k}
:laws:latex::
"""


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
ideal gas behaviour becomes more prominent the closer the gas is to a phase change, the lower
the temperature or the larger the pressure.
**Notation:**
#. :math:`R` is the molar gas constant.
**Notes:**
#. Can be equivalently defined as the ratio of the molar volume :math:`\frac{V}{n}` of the real gas to the
Expand All @@ -24,61 +20,53 @@

from sympy import Eq
from symplyphysics import (
quantities,
units,
dimensionless,
Quantity,
Symbol,
SymbolNew,
validate_input,
validate_output,
symbols,
convert_to_float,
)

compressibility_factor = Symbol("compressibility_factor", dimensionless)
compressibility_factor = SymbolNew("Z", dimensionless)
"""
Compressibility factor of the real gas.
Symbol:
:code:`Z`
"""

pressure = Symbol("pressure", units.pressure)
pressure = SymbolNew("p", units.pressure)
"""
Pressure of the gas.
Symbol:
:code:`p`
"""

volume = Symbol("volume", units.volume)
volume = SymbolNew("V", units.volume)
"""
Volume of the gas.
Symbol:
:code:`V`
"""

amount_of_substance = Symbol("amount_of_substance", units.amount_of_substance)
amount_of_substance = SymbolNew("n", units.amount_of_substance)
"""
Amount of gas substance.
Symbol:
:code:`n`
"""

temperature = symbols.thermodynamics.temperature
"""
Gas :attr:`~symplyphysics.symbols.thermodynamics.temperature`.
"""

molar_gas_constant = quantities.molar_gas_constant
"""
:attr:`~symplyphysics.quantities.molar_gas_constant`
"""

definition = Eq(compressibility_factor,
(pressure * volume) / (amount_of_substance * units.molar_gas_constant * temperature))
r"""
:code:`Z = (p * V) / (n * R * T)`
(pressure * volume) / (amount_of_substance * molar_gas_constant * temperature))
"""
:laws:symbol::
Latex:
.. math::
Z = \frac{p V}{n R T}
:laws:latex::
"""


Expand Down
25 changes: 7 additions & 18 deletions symplyphysics/definitions/current_is_charge_derivative.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,28 @@
"""

from sympy import (Eq, Derivative)
from symplyphysics import (units, Quantity, Function, Symbol, validate_input, validate_output)
from symplyphysics import (units, Quantity, FunctionNew, SymbolNew, validate_input, validate_output)

current = Function("current", units.current)
current = FunctionNew("I(t)", units.current, display_latex="I")
"""
Electric current as a function of time.
Symbol:
:code:`I(t)`
"""

charge = Function("charge", units.charge)
charge = FunctionNew("q(t)", units.charge, display_latex="q")
"""
Electric charge as a function of time.
Symbol:
:code:`q(t)`
"""

time = Symbol("time", units.time)
time = SymbolNew("t", units.time)
"""
Time.
Symbol:
:code:`t`
"""

definition = Eq(current(time), Derivative(charge(time), time))
r"""
:code:`I(t) = Derivative(q(t), t)`
"""
:laws:symbol::
Latex:
.. math::
I(t) = \frac{d q}{d t}
:laws:latex::
"""


Expand Down
Loading

0 comments on commit 00959ee

Please sign in to comment.