Instructions to use dl1wjd2/Logistics_AX with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- ultralytics
How to use dl1wjd2/Logistics_AX with ultralytics:
from huggingface_hub import hf_hub_download from ultralytics import YOLO # pick the weights file from this repo's "Files and versions" tab weights = hf_hub_download("dl1wjd2/Logistics_AX", "<weights>.pt") model = YOLO(weights) source = 'http://images.cocodataset.org/val2017/000000039769.jpg' model.predict(source=source, save=True) - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub (part 19)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +37 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/systems/si.py +377 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/__init__.py +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/__init__.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_dimensions.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_dimensionsystem.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_prefixes.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_quantities.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_unit_system_cgs_gauss.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_unitsystem.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_util.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_dimensions.py +150 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_dimensionsystem.py +95 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_prefixes.py +86 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_quantities.py +575 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_unit_system_cgs_gauss.py +55 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_unitsystem.py +86 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_util.py +178 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/unitsystem.py +204 -0
- hug/lib/python3.12/site-packages/sympy/physics/units/util.py +265 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/__init__.py +36 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/__init__.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/dyadic.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/fieldfunctions.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/frame.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/functions.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/point.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/printing.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/vector.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/dyadic.py +545 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/fieldfunctions.py +313 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/frame.py +1575 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/functions.py +650 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/point.py +635 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/printing.py +371 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__init__.py +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/__init__.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_dyadic.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_fieldfunctions.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_frame.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_functions.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_output.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_point.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_printing.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_vector.cpython-312.pyc +0 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/test_dyadic.py +123 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/test_fieldfunctions.py +133 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/test_frame.py +761 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/test_functions.py +509 -0
- hug/lib/python3.12/site-packages/sympy/physics/vector/tests/test_output.py +75 -0
.gitattributes
CHANGED
|
@@ -345,3 +345,40 @@ hug/lib/python3.12/site-packages/sympy/physics/control/tests/__pycache__/test_lt
|
|
| 345 |
hug/lib/python3.12/site-packages/sympy/physics/mechanics/tests/__pycache__/test_joint.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 346 |
hug/lib/python3.12/site-packages/sympy/physics/quantum/__pycache__/spin.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 347 |
hug/lib/python3.12/site-packages/sympy/physics/quantum/tests/__pycache__/test_spin.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
hug/lib/python3.12/site-packages/sympy/physics/mechanics/tests/__pycache__/test_joint.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 346 |
hug/lib/python3.12/site-packages/sympy/physics/quantum/__pycache__/spin.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 347 |
hug/lib/python3.12/site-packages/sympy/physics/quantum/tests/__pycache__/test_spin.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 348 |
+
hug/lib/python3.12/site-packages/sympy/plotting/__pycache__/series.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 349 |
+
hug/lib/python3.12/site-packages/sympy/plotting/tests/__pycache__/test_series.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 350 |
+
hug/lib/python3.12/site-packages/sympy/polys/__pycache__/compatibility.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 351 |
+
hug/lib/python3.12/site-packages/sympy/polys/__pycache__/polyclasses.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 352 |
+
hug/lib/python3.12/site-packages/sympy/polys/__pycache__/polyquinticconst.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 353 |
+
hug/lib/python3.12/site-packages/sympy/polys/__pycache__/polytools.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 354 |
+
hug/lib/python3.12/site-packages/sympy/polys/__pycache__/rings.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 355 |
+
hug/lib/python3.12/site-packages/sympy/polys/benchmarks/__pycache__/bench_solvers.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 356 |
+
hug/lib/python3.12/site-packages/sympy/polys/domains/tests/__pycache__/test_domains.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 357 |
+
hug/lib/python3.12/site-packages/sympy/polys/matrices/__pycache__/domainmatrix.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 358 |
+
hug/lib/python3.12/site-packages/sympy/polys/matrices/tests/__pycache__/test_domainmatrix.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 359 |
+
hug/lib/python3.12/site-packages/sympy/polys/numberfields/__pycache__/resolvent_lookup.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 360 |
+
hug/lib/python3.12/site-packages/sympy/polys/tests/__pycache__/test_polytools.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 361 |
+
hug/lib/python3.12/site-packages/sympy/polys/tests/__pycache__/test_rings.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 362 |
+
hug/lib/python3.12/site-packages/sympy/printing/__pycache__/latex.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 363 |
+
hug/lib/python3.12/site-packages/sympy/printing/__pycache__/mathml.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 364 |
+
hug/lib/python3.12/site-packages/sympy/printing/pretty/__pycache__/pretty.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 365 |
+
hug/lib/python3.12/site-packages/sympy/printing/pretty/tests/__pycache__/test_pretty.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 366 |
+
hug/lib/python3.12/site-packages/sympy/printing/tests/__pycache__/test_latex.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 367 |
+
hug/lib/python3.12/site-packages/sympy/printing/tests/__pycache__/test_mathml.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 368 |
+
hug/lib/python3.12/site-packages/sympy/series/tests/__pycache__/test_limits.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 369 |
+
hug/lib/python3.12/site-packages/sympy/sets/__pycache__/sets.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 370 |
+
hug/lib/python3.12/site-packages/sympy/sets/tests/__pycache__/test_fancysets.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 371 |
+
hug/lib/python3.12/site-packages/sympy/sets/tests/__pycache__/test_sets.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 372 |
+
hug/lib/python3.12/site-packages/sympy/simplify/__pycache__/hyperexpand.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 373 |
+
hug/lib/python3.12/site-packages/sympy/solvers/__pycache__/solvers.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 374 |
+
hug/lib/python3.12/site-packages/sympy/solvers/__pycache__/solveset.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 375 |
+
hug/lib/python3.12/site-packages/sympy/solvers/diophantine/__pycache__/diophantine.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 376 |
+
hug/lib/python3.12/site-packages/sympy/solvers/ode/__pycache__/ode.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 377 |
+
hug/lib/python3.12/site-packages/sympy/solvers/ode/__pycache__/single.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 378 |
+
hug/lib/python3.12/site-packages/sympy/solvers/ode/tests/__pycache__/test_ode.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 379 |
+
hug/lib/python3.12/site-packages/sympy/solvers/ode/tests/__pycache__/test_single.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 380 |
+
hug/lib/python3.12/site-packages/sympy/solvers/ode/tests/__pycache__/test_systems.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 381 |
+
hug/lib/python3.12/site-packages/sympy/solvers/tests/__pycache__/test_solvers.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 382 |
+
hug/lib/python3.12/site-packages/sympy/solvers/tests/__pycache__/test_solveset.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 383 |
+
hug/lib/python3.12/site-packages/sympy/stats/__pycache__/crv_types.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 384 |
+
hug/lib/python3.12/site-packages/sympy/stats/__pycache__/stochastic_process_types.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
hug/lib/python3.12/site-packages/sympy/physics/units/systems/si.py
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
SI unit system.
|
| 3 |
+
Based on MKSA, which stands for "meter, kilogram, second, ampere".
|
| 4 |
+
Added kelvin, candela and mole.
|
| 5 |
+
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
from sympy.physics.units import DimensionSystem, Dimension, dHg0
|
| 11 |
+
|
| 12 |
+
from sympy.physics.units.quantities import Quantity
|
| 13 |
+
|
| 14 |
+
from sympy.core.numbers import (Rational, pi)
|
| 15 |
+
from sympy.core.singleton import S
|
| 16 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 17 |
+
from sympy.physics.units.definitions.dimension_definitions import (
|
| 18 |
+
acceleration, action, current, impedance, length, mass, time, velocity,
|
| 19 |
+
amount_of_substance, temperature, information, frequency, force, pressure,
|
| 20 |
+
energy, power, charge, voltage, capacitance, conductance, magnetic_flux,
|
| 21 |
+
magnetic_density, inductance, luminous_intensity
|
| 22 |
+
)
|
| 23 |
+
from sympy.physics.units.definitions import (
|
| 24 |
+
kilogram, newton, second, meter, gram, cd, K, joule, watt, pascal, hertz,
|
| 25 |
+
coulomb, volt, ohm, siemens, farad, henry, tesla, weber, dioptre, lux,
|
| 26 |
+
katal, gray, becquerel, inch, liter, julian_year, gravitational_constant,
|
| 27 |
+
speed_of_light, elementary_charge, planck, hbar, electronvolt,
|
| 28 |
+
avogadro_number, avogadro_constant, boltzmann_constant, electron_rest_mass,
|
| 29 |
+
stefan_boltzmann_constant, Da, atomic_mass_constant, molar_gas_constant,
|
| 30 |
+
faraday_constant, josephson_constant, von_klitzing_constant,
|
| 31 |
+
acceleration_due_to_gravity, magnetic_constant, vacuum_permittivity,
|
| 32 |
+
vacuum_impedance, coulomb_constant, atmosphere, bar, pound, psi, mmHg,
|
| 33 |
+
milli_mass_unit, quart, lightyear, astronomical_unit, planck_mass,
|
| 34 |
+
planck_time, planck_temperature, planck_length, planck_charge, planck_area,
|
| 35 |
+
planck_volume, planck_momentum, planck_energy, planck_force, planck_power,
|
| 36 |
+
planck_density, planck_energy_density, planck_intensity,
|
| 37 |
+
planck_angular_frequency, planck_pressure, planck_current, planck_voltage,
|
| 38 |
+
planck_impedance, planck_acceleration, bit, byte, kibibyte, mebibyte,
|
| 39 |
+
gibibyte, tebibyte, pebibyte, exbibyte, curie, rutherford, radian, degree,
|
| 40 |
+
steradian, angular_mil, atomic_mass_unit, gee, kPa, ampere, u0, c, kelvin,
|
| 41 |
+
mol, mole, candela, m, kg, s, electric_constant, G, boltzmann
|
| 42 |
+
)
|
| 43 |
+
from sympy.physics.units.prefixes import PREFIXES, prefix_unit
|
| 44 |
+
from sympy.physics.units.systems.mksa import MKSA, dimsys_MKSA
|
| 45 |
+
|
| 46 |
+
derived_dims = (frequency, force, pressure, energy, power, charge, voltage,
|
| 47 |
+
capacitance, conductance, magnetic_flux,
|
| 48 |
+
magnetic_density, inductance, luminous_intensity)
|
| 49 |
+
base_dims = (amount_of_substance, luminous_intensity, temperature)
|
| 50 |
+
|
| 51 |
+
units = [mol, cd, K, lux, hertz, newton, pascal, joule, watt, coulomb, volt,
|
| 52 |
+
farad, ohm, siemens, weber, tesla, henry, candela, lux, becquerel,
|
| 53 |
+
gray, katal]
|
| 54 |
+
|
| 55 |
+
all_units: list[Quantity] = []
|
| 56 |
+
for u in units:
|
| 57 |
+
all_units.extend(prefix_unit(u, PREFIXES))
|
| 58 |
+
|
| 59 |
+
all_units.extend(units)
|
| 60 |
+
all_units.extend([mol, cd, K, lux])
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
dimsys_SI = dimsys_MKSA.extend(
|
| 64 |
+
[
|
| 65 |
+
# Dimensional dependencies for other base dimensions:
|
| 66 |
+
temperature,
|
| 67 |
+
amount_of_substance,
|
| 68 |
+
luminous_intensity,
|
| 69 |
+
])
|
| 70 |
+
|
| 71 |
+
dimsys_default = dimsys_SI.extend(
|
| 72 |
+
[information],
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
SI = MKSA.extend(base=(mol, cd, K), units=all_units, name='SI', dimension_system=dimsys_SI, derived_units={
|
| 76 |
+
power: watt,
|
| 77 |
+
magnetic_flux: weber,
|
| 78 |
+
time: second,
|
| 79 |
+
impedance: ohm,
|
| 80 |
+
pressure: pascal,
|
| 81 |
+
current: ampere,
|
| 82 |
+
voltage: volt,
|
| 83 |
+
length: meter,
|
| 84 |
+
frequency: hertz,
|
| 85 |
+
inductance: henry,
|
| 86 |
+
temperature: kelvin,
|
| 87 |
+
amount_of_substance: mole,
|
| 88 |
+
luminous_intensity: candela,
|
| 89 |
+
conductance: siemens,
|
| 90 |
+
mass: kilogram,
|
| 91 |
+
magnetic_density: tesla,
|
| 92 |
+
charge: coulomb,
|
| 93 |
+
force: newton,
|
| 94 |
+
capacitance: farad,
|
| 95 |
+
energy: joule,
|
| 96 |
+
velocity: meter/second,
|
| 97 |
+
})
|
| 98 |
+
|
| 99 |
+
One = S.One
|
| 100 |
+
|
| 101 |
+
SI.set_quantity_dimension(radian, One)
|
| 102 |
+
|
| 103 |
+
SI.set_quantity_scale_factor(ampere, One)
|
| 104 |
+
|
| 105 |
+
SI.set_quantity_scale_factor(kelvin, One)
|
| 106 |
+
|
| 107 |
+
SI.set_quantity_scale_factor(mole, One)
|
| 108 |
+
|
| 109 |
+
SI.set_quantity_scale_factor(candela, One)
|
| 110 |
+
|
| 111 |
+
# MKSA extension to MKS: derived units
|
| 112 |
+
|
| 113 |
+
SI.set_quantity_scale_factor(coulomb, One)
|
| 114 |
+
|
| 115 |
+
SI.set_quantity_scale_factor(volt, joule/coulomb)
|
| 116 |
+
|
| 117 |
+
SI.set_quantity_scale_factor(ohm, volt/ampere)
|
| 118 |
+
|
| 119 |
+
SI.set_quantity_scale_factor(siemens, ampere/volt)
|
| 120 |
+
|
| 121 |
+
SI.set_quantity_scale_factor(farad, coulomb/volt)
|
| 122 |
+
|
| 123 |
+
SI.set_quantity_scale_factor(henry, volt*second/ampere)
|
| 124 |
+
|
| 125 |
+
SI.set_quantity_scale_factor(tesla, volt*second/meter**2)
|
| 126 |
+
|
| 127 |
+
SI.set_quantity_scale_factor(weber, joule/ampere)
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
SI.set_quantity_dimension(lux, luminous_intensity / length ** 2)
|
| 131 |
+
SI.set_quantity_scale_factor(lux, steradian*candela/meter**2)
|
| 132 |
+
|
| 133 |
+
# katal is the SI unit of catalytic activity
|
| 134 |
+
|
| 135 |
+
SI.set_quantity_dimension(katal, amount_of_substance / time)
|
| 136 |
+
SI.set_quantity_scale_factor(katal, mol/second)
|
| 137 |
+
|
| 138 |
+
# gray is the SI unit of absorbed dose
|
| 139 |
+
|
| 140 |
+
SI.set_quantity_dimension(gray, energy / mass)
|
| 141 |
+
SI.set_quantity_scale_factor(gray, meter**2/second**2)
|
| 142 |
+
|
| 143 |
+
# becquerel is the SI unit of radioactivity
|
| 144 |
+
|
| 145 |
+
SI.set_quantity_dimension(becquerel, 1 / time)
|
| 146 |
+
SI.set_quantity_scale_factor(becquerel, 1/second)
|
| 147 |
+
|
| 148 |
+
#### CONSTANTS ####
|
| 149 |
+
|
| 150 |
+
# elementary charge
|
| 151 |
+
# REF: NIST SP 959 (June 2019)
|
| 152 |
+
|
| 153 |
+
SI.set_quantity_dimension(elementary_charge, charge)
|
| 154 |
+
SI.set_quantity_scale_factor(elementary_charge, 1.602176634e-19*coulomb)
|
| 155 |
+
|
| 156 |
+
# Electronvolt
|
| 157 |
+
# REF: NIST SP 959 (June 2019)
|
| 158 |
+
|
| 159 |
+
SI.set_quantity_dimension(electronvolt, energy)
|
| 160 |
+
SI.set_quantity_scale_factor(electronvolt, 1.602176634e-19*joule)
|
| 161 |
+
|
| 162 |
+
# Avogadro number
|
| 163 |
+
# REF: NIST SP 959 (June 2019)
|
| 164 |
+
|
| 165 |
+
SI.set_quantity_dimension(avogadro_number, One)
|
| 166 |
+
SI.set_quantity_scale_factor(avogadro_number, 6.02214076e23)
|
| 167 |
+
|
| 168 |
+
# Avogadro constant
|
| 169 |
+
|
| 170 |
+
SI.set_quantity_dimension(avogadro_constant, amount_of_substance ** -1)
|
| 171 |
+
SI.set_quantity_scale_factor(avogadro_constant, avogadro_number / mol)
|
| 172 |
+
|
| 173 |
+
# Boltzmann constant
|
| 174 |
+
# REF: NIST SP 959 (June 2019)
|
| 175 |
+
|
| 176 |
+
SI.set_quantity_dimension(boltzmann_constant, energy / temperature)
|
| 177 |
+
SI.set_quantity_scale_factor(boltzmann_constant, 1.380649e-23*joule/kelvin)
|
| 178 |
+
|
| 179 |
+
# Stefan-Boltzmann constant
|
| 180 |
+
# REF: NIST SP 959 (June 2019)
|
| 181 |
+
|
| 182 |
+
SI.set_quantity_dimension(stefan_boltzmann_constant, energy * time ** -1 * length ** -2 * temperature ** -4)
|
| 183 |
+
SI.set_quantity_scale_factor(stefan_boltzmann_constant, pi**2 * boltzmann_constant**4 / (60 * hbar**3 * speed_of_light ** 2))
|
| 184 |
+
|
| 185 |
+
# Atomic mass
|
| 186 |
+
# REF: NIST SP 959 (June 2019)
|
| 187 |
+
|
| 188 |
+
SI.set_quantity_dimension(atomic_mass_constant, mass)
|
| 189 |
+
SI.set_quantity_scale_factor(atomic_mass_constant, 1.66053906660e-24*gram)
|
| 190 |
+
|
| 191 |
+
# Molar gas constant
|
| 192 |
+
# REF: NIST SP 959 (June 2019)
|
| 193 |
+
|
| 194 |
+
SI.set_quantity_dimension(molar_gas_constant, energy / (temperature * amount_of_substance))
|
| 195 |
+
SI.set_quantity_scale_factor(molar_gas_constant, boltzmann_constant * avogadro_constant)
|
| 196 |
+
|
| 197 |
+
# Faraday constant
|
| 198 |
+
|
| 199 |
+
SI.set_quantity_dimension(faraday_constant, charge / amount_of_substance)
|
| 200 |
+
SI.set_quantity_scale_factor(faraday_constant, elementary_charge * avogadro_constant)
|
| 201 |
+
|
| 202 |
+
# Josephson constant
|
| 203 |
+
|
| 204 |
+
SI.set_quantity_dimension(josephson_constant, frequency / voltage)
|
| 205 |
+
SI.set_quantity_scale_factor(josephson_constant, 0.5 * planck / elementary_charge)
|
| 206 |
+
|
| 207 |
+
# Von Klitzing constant
|
| 208 |
+
|
| 209 |
+
SI.set_quantity_dimension(von_klitzing_constant, voltage / current)
|
| 210 |
+
SI.set_quantity_scale_factor(von_klitzing_constant, hbar / elementary_charge ** 2)
|
| 211 |
+
|
| 212 |
+
# Acceleration due to gravity (on the Earth surface)
|
| 213 |
+
|
| 214 |
+
SI.set_quantity_dimension(acceleration_due_to_gravity, acceleration)
|
| 215 |
+
SI.set_quantity_scale_factor(acceleration_due_to_gravity, 9.80665*meter/second**2)
|
| 216 |
+
|
| 217 |
+
# magnetic constant:
|
| 218 |
+
|
| 219 |
+
SI.set_quantity_dimension(magnetic_constant, force / current ** 2)
|
| 220 |
+
SI.set_quantity_scale_factor(magnetic_constant, 4*pi/10**7 * newton/ampere**2)
|
| 221 |
+
|
| 222 |
+
# electric constant:
|
| 223 |
+
|
| 224 |
+
SI.set_quantity_dimension(vacuum_permittivity, capacitance / length)
|
| 225 |
+
SI.set_quantity_scale_factor(vacuum_permittivity, 1/(u0 * c**2))
|
| 226 |
+
|
| 227 |
+
# vacuum impedance:
|
| 228 |
+
|
| 229 |
+
SI.set_quantity_dimension(vacuum_impedance, impedance)
|
| 230 |
+
SI.set_quantity_scale_factor(vacuum_impedance, u0 * c)
|
| 231 |
+
|
| 232 |
+
# Electron rest mass
|
| 233 |
+
SI.set_quantity_dimension(electron_rest_mass, mass)
|
| 234 |
+
SI.set_quantity_scale_factor(electron_rest_mass, 9.1093837015e-31*kilogram)
|
| 235 |
+
|
| 236 |
+
# Coulomb's constant:
|
| 237 |
+
SI.set_quantity_dimension(coulomb_constant, force * length ** 2 / charge ** 2)
|
| 238 |
+
SI.set_quantity_scale_factor(coulomb_constant, 1/(4*pi*vacuum_permittivity))
|
| 239 |
+
|
| 240 |
+
SI.set_quantity_dimension(psi, pressure)
|
| 241 |
+
SI.set_quantity_scale_factor(psi, pound * gee / inch ** 2)
|
| 242 |
+
|
| 243 |
+
SI.set_quantity_dimension(mmHg, pressure)
|
| 244 |
+
SI.set_quantity_scale_factor(mmHg, dHg0 * acceleration_due_to_gravity * kilogram / meter**2)
|
| 245 |
+
|
| 246 |
+
SI.set_quantity_dimension(milli_mass_unit, mass)
|
| 247 |
+
SI.set_quantity_scale_factor(milli_mass_unit, atomic_mass_unit/1000)
|
| 248 |
+
|
| 249 |
+
SI.set_quantity_dimension(quart, length ** 3)
|
| 250 |
+
SI.set_quantity_scale_factor(quart, Rational(231, 4) * inch**3)
|
| 251 |
+
|
| 252 |
+
# Other convenient units and magnitudes
|
| 253 |
+
|
| 254 |
+
SI.set_quantity_dimension(lightyear, length)
|
| 255 |
+
SI.set_quantity_scale_factor(lightyear, speed_of_light*julian_year)
|
| 256 |
+
|
| 257 |
+
SI.set_quantity_dimension(astronomical_unit, length)
|
| 258 |
+
SI.set_quantity_scale_factor(astronomical_unit, 149597870691*meter)
|
| 259 |
+
|
| 260 |
+
# Fundamental Planck units:
|
| 261 |
+
|
| 262 |
+
SI.set_quantity_dimension(planck_mass, mass)
|
| 263 |
+
SI.set_quantity_scale_factor(planck_mass, sqrt(hbar*speed_of_light/G))
|
| 264 |
+
|
| 265 |
+
SI.set_quantity_dimension(planck_time, time)
|
| 266 |
+
SI.set_quantity_scale_factor(planck_time, sqrt(hbar*G/speed_of_light**5))
|
| 267 |
+
|
| 268 |
+
SI.set_quantity_dimension(planck_temperature, temperature)
|
| 269 |
+
SI.set_quantity_scale_factor(planck_temperature, sqrt(hbar*speed_of_light**5/G/boltzmann**2))
|
| 270 |
+
|
| 271 |
+
SI.set_quantity_dimension(planck_length, length)
|
| 272 |
+
SI.set_quantity_scale_factor(planck_length, sqrt(hbar*G/speed_of_light**3))
|
| 273 |
+
|
| 274 |
+
SI.set_quantity_dimension(planck_charge, charge)
|
| 275 |
+
SI.set_quantity_scale_factor(planck_charge, sqrt(4*pi*electric_constant*hbar*speed_of_light))
|
| 276 |
+
|
| 277 |
+
# Derived Planck units:
|
| 278 |
+
|
| 279 |
+
SI.set_quantity_dimension(planck_area, length ** 2)
|
| 280 |
+
SI.set_quantity_scale_factor(planck_area, planck_length**2)
|
| 281 |
+
|
| 282 |
+
SI.set_quantity_dimension(planck_volume, length ** 3)
|
| 283 |
+
SI.set_quantity_scale_factor(planck_volume, planck_length**3)
|
| 284 |
+
|
| 285 |
+
SI.set_quantity_dimension(planck_momentum, mass * velocity)
|
| 286 |
+
SI.set_quantity_scale_factor(planck_momentum, planck_mass * speed_of_light)
|
| 287 |
+
|
| 288 |
+
SI.set_quantity_dimension(planck_energy, energy)
|
| 289 |
+
SI.set_quantity_scale_factor(planck_energy, planck_mass * speed_of_light**2)
|
| 290 |
+
|
| 291 |
+
SI.set_quantity_dimension(planck_force, force)
|
| 292 |
+
SI.set_quantity_scale_factor(planck_force, planck_energy / planck_length)
|
| 293 |
+
|
| 294 |
+
SI.set_quantity_dimension(planck_power, power)
|
| 295 |
+
SI.set_quantity_scale_factor(planck_power, planck_energy / planck_time)
|
| 296 |
+
|
| 297 |
+
SI.set_quantity_dimension(planck_density, mass / length ** 3)
|
| 298 |
+
SI.set_quantity_scale_factor(planck_density, planck_mass / planck_length**3)
|
| 299 |
+
|
| 300 |
+
SI.set_quantity_dimension(planck_energy_density, energy / length ** 3)
|
| 301 |
+
SI.set_quantity_scale_factor(planck_energy_density, planck_energy / planck_length**3)
|
| 302 |
+
|
| 303 |
+
SI.set_quantity_dimension(planck_intensity, mass * time ** (-3))
|
| 304 |
+
SI.set_quantity_scale_factor(planck_intensity, planck_energy_density * speed_of_light)
|
| 305 |
+
|
| 306 |
+
SI.set_quantity_dimension(planck_angular_frequency, 1 / time)
|
| 307 |
+
SI.set_quantity_scale_factor(planck_angular_frequency, 1 / planck_time)
|
| 308 |
+
|
| 309 |
+
SI.set_quantity_dimension(planck_pressure, pressure)
|
| 310 |
+
SI.set_quantity_scale_factor(planck_pressure, planck_force / planck_length**2)
|
| 311 |
+
|
| 312 |
+
SI.set_quantity_dimension(planck_current, current)
|
| 313 |
+
SI.set_quantity_scale_factor(planck_current, planck_charge / planck_time)
|
| 314 |
+
|
| 315 |
+
SI.set_quantity_dimension(planck_voltage, voltage)
|
| 316 |
+
SI.set_quantity_scale_factor(planck_voltage, planck_energy / planck_charge)
|
| 317 |
+
|
| 318 |
+
SI.set_quantity_dimension(planck_impedance, impedance)
|
| 319 |
+
SI.set_quantity_scale_factor(planck_impedance, planck_voltage / planck_current)
|
| 320 |
+
|
| 321 |
+
SI.set_quantity_dimension(planck_acceleration, acceleration)
|
| 322 |
+
SI.set_quantity_scale_factor(planck_acceleration, speed_of_light / planck_time)
|
| 323 |
+
|
| 324 |
+
# Older units for radioactivity
|
| 325 |
+
|
| 326 |
+
SI.set_quantity_dimension(curie, 1 / time)
|
| 327 |
+
SI.set_quantity_scale_factor(curie, 37000000000*becquerel)
|
| 328 |
+
|
| 329 |
+
SI.set_quantity_dimension(rutherford, 1 / time)
|
| 330 |
+
SI.set_quantity_scale_factor(rutherford, 1000000*becquerel)
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
# check that scale factors are the right SI dimensions:
|
| 334 |
+
for _scale_factor, _dimension in zip(
|
| 335 |
+
SI._quantity_scale_factors.values(),
|
| 336 |
+
SI._quantity_dimension_map.values()
|
| 337 |
+
):
|
| 338 |
+
dimex = SI.get_dimensional_expr(_scale_factor)
|
| 339 |
+
if dimex != 1:
|
| 340 |
+
# XXX: equivalent_dims is an instance method taking two arguments in
|
| 341 |
+
# addition to self so this can not work:
|
| 342 |
+
if not DimensionSystem.equivalent_dims(_dimension, Dimension(dimex)): # type: ignore
|
| 343 |
+
raise ValueError("quantity value and dimension mismatch")
|
| 344 |
+
del _scale_factor, _dimension
|
| 345 |
+
|
| 346 |
+
__all__ = [
|
| 347 |
+
'mmHg', 'atmosphere', 'inductance', 'newton', 'meter',
|
| 348 |
+
'vacuum_permittivity', 'pascal', 'magnetic_constant', 'voltage',
|
| 349 |
+
'angular_mil', 'luminous_intensity', 'all_units',
|
| 350 |
+
'julian_year', 'weber', 'exbibyte', 'liter',
|
| 351 |
+
'molar_gas_constant', 'faraday_constant', 'avogadro_constant',
|
| 352 |
+
'lightyear', 'planck_density', 'gee', 'mol', 'bit', 'gray',
|
| 353 |
+
'planck_momentum', 'bar', 'magnetic_density', 'prefix_unit', 'PREFIXES',
|
| 354 |
+
'planck_time', 'dimex', 'gram', 'candela', 'force', 'planck_intensity',
|
| 355 |
+
'energy', 'becquerel', 'planck_acceleration', 'speed_of_light',
|
| 356 |
+
'conductance', 'frequency', 'coulomb_constant', 'degree', 'lux', 'planck',
|
| 357 |
+
'current', 'planck_current', 'tebibyte', 'planck_power', 'MKSA', 'power',
|
| 358 |
+
'K', 'planck_volume', 'quart', 'pressure', 'amount_of_substance',
|
| 359 |
+
'joule', 'boltzmann_constant', 'Dimension', 'c', 'planck_force', 'length',
|
| 360 |
+
'watt', 'action', 'hbar', 'gibibyte', 'DimensionSystem', 'cd', 'volt',
|
| 361 |
+
'planck_charge', 'dioptre', 'vacuum_impedance', 'dimsys_default', 'farad',
|
| 362 |
+
'charge', 'gravitational_constant', 'temperature', 'u0', 'hertz',
|
| 363 |
+
'capacitance', 'tesla', 'steradian', 'planck_mass', 'josephson_constant',
|
| 364 |
+
'planck_area', 'stefan_boltzmann_constant', 'base_dims',
|
| 365 |
+
'astronomical_unit', 'radian', 'planck_voltage', 'impedance',
|
| 366 |
+
'planck_energy', 'Da', 'atomic_mass_constant', 'rutherford', 'second', 'inch',
|
| 367 |
+
'elementary_charge', 'SI', 'electronvolt', 'dimsys_SI', 'henry',
|
| 368 |
+
'planck_angular_frequency', 'ohm', 'pound', 'planck_pressure', 'G', 'psi',
|
| 369 |
+
'dHg0', 'von_klitzing_constant', 'planck_length', 'avogadro_number',
|
| 370 |
+
'mole', 'acceleration', 'information', 'planck_energy_density',
|
| 371 |
+
'mebibyte', 's', 'acceleration_due_to_gravity', 'electron_rest_mass',
|
| 372 |
+
'planck_temperature', 'units', 'mass', 'dimsys_MKSA', 'kelvin', 'kPa',
|
| 373 |
+
'boltzmann', 'milli_mass_unit', 'planck_impedance', 'electric_constant',
|
| 374 |
+
'derived_dims', 'kg', 'coulomb', 'siemens', 'byte', 'magnetic_flux',
|
| 375 |
+
'atomic_mass_unit', 'm', 'kibibyte', 'kilogram', 'One', 'curie', 'u',
|
| 376 |
+
'time', 'pebibyte', 'velocity', 'ampere', 'katal',
|
| 377 |
+
]
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/__init__.py
ADDED
|
File without changes
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (192 Bytes). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_dimensions.cpython-312.pyc
ADDED
|
Binary file (12.6 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_dimensionsystem.cpython-312.pyc
ADDED
|
Binary file (5.19 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_prefixes.cpython-312.pyc
ADDED
|
Binary file (4.57 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_quantities.cpython-312.pyc
ADDED
|
Binary file (33.6 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_unit_system_cgs_gauss.cpython-312.pyc
ADDED
|
Binary file (5.61 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_unitsystem.cpython-312.pyc
ADDED
|
Binary file (6.18 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/__pycache__/test_util.cpython-312.pyc
ADDED
|
Binary file (16.7 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_dimensions.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.physics.units.systems.si import dimsys_SI
|
| 2 |
+
|
| 3 |
+
from sympy.core.numbers import pi
|
| 4 |
+
from sympy.core.singleton import S
|
| 5 |
+
from sympy.core.symbol import Symbol
|
| 6 |
+
from sympy.functions.elementary.complexes import Abs
|
| 7 |
+
from sympy.functions.elementary.exponential import log
|
| 8 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 9 |
+
from sympy.functions.elementary.trigonometric import (acos, atan2, cos)
|
| 10 |
+
from sympy.physics.units.dimensions import Dimension
|
| 11 |
+
from sympy.physics.units.definitions.dimension_definitions import (
|
| 12 |
+
length, time, mass, force, pressure, angle
|
| 13 |
+
)
|
| 14 |
+
from sympy.physics.units import foot
|
| 15 |
+
from sympy.testing.pytest import raises
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_Dimension_definition():
|
| 19 |
+
assert dimsys_SI.get_dimensional_dependencies(length) == {length: 1}
|
| 20 |
+
assert length.name == Symbol("length")
|
| 21 |
+
assert length.symbol == Symbol("L")
|
| 22 |
+
|
| 23 |
+
halflength = sqrt(length)
|
| 24 |
+
assert dimsys_SI.get_dimensional_dependencies(halflength) == {length: S.Half}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_Dimension_error_definition():
|
| 28 |
+
# tuple with more or less than two entries
|
| 29 |
+
raises(TypeError, lambda: Dimension(("length", 1, 2)))
|
| 30 |
+
raises(TypeError, lambda: Dimension(["length"]))
|
| 31 |
+
|
| 32 |
+
# non-number power
|
| 33 |
+
raises(TypeError, lambda: Dimension({"length": "a"}))
|
| 34 |
+
|
| 35 |
+
# non-number with named argument
|
| 36 |
+
raises(TypeError, lambda: Dimension({"length": (1, 2)}))
|
| 37 |
+
|
| 38 |
+
# symbol should by Symbol or str
|
| 39 |
+
raises(AssertionError, lambda: Dimension("length", symbol=1))
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def test_str():
|
| 43 |
+
assert str(Dimension("length")) == "Dimension(length)"
|
| 44 |
+
assert str(Dimension("length", "L")) == "Dimension(length, L)"
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def test_Dimension_properties():
|
| 48 |
+
assert dimsys_SI.is_dimensionless(length) is False
|
| 49 |
+
assert dimsys_SI.is_dimensionless(length/length) is True
|
| 50 |
+
assert dimsys_SI.is_dimensionless(Dimension("undefined")) is False
|
| 51 |
+
|
| 52 |
+
assert length.has_integer_powers(dimsys_SI) is True
|
| 53 |
+
assert (length**(-1)).has_integer_powers(dimsys_SI) is True
|
| 54 |
+
assert (length**1.5).has_integer_powers(dimsys_SI) is False
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def test_Dimension_add_sub():
|
| 58 |
+
assert length + length == length
|
| 59 |
+
assert length - length == length
|
| 60 |
+
assert -length == length
|
| 61 |
+
|
| 62 |
+
raises(TypeError, lambda: length + foot)
|
| 63 |
+
raises(TypeError, lambda: foot + length)
|
| 64 |
+
raises(TypeError, lambda: length - foot)
|
| 65 |
+
raises(TypeError, lambda: foot - length)
|
| 66 |
+
|
| 67 |
+
# issue 14547 - only raise error for dimensional args; allow
|
| 68 |
+
# others to pass
|
| 69 |
+
x = Symbol('x')
|
| 70 |
+
e = length + x
|
| 71 |
+
assert e == x + length and e.is_Add and set(e.args) == {length, x}
|
| 72 |
+
e = length + 1
|
| 73 |
+
assert e == 1 + length == 1 - length and e.is_Add and set(e.args) == {length, 1}
|
| 74 |
+
|
| 75 |
+
assert dimsys_SI.get_dimensional_dependencies(mass * length / time**2 + force) == \
|
| 76 |
+
{length: 1, mass: 1, time: -2}
|
| 77 |
+
assert dimsys_SI.get_dimensional_dependencies(mass * length / time**2 + force -
|
| 78 |
+
pressure * length**2) == \
|
| 79 |
+
{length: 1, mass: 1, time: -2}
|
| 80 |
+
|
| 81 |
+
raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(mass * length / time**2 + pressure))
|
| 82 |
+
|
| 83 |
+
def test_Dimension_mul_div_exp():
|
| 84 |
+
assert 2*length == length*2 == length/2 == length
|
| 85 |
+
assert 2/length == 1/length
|
| 86 |
+
x = Symbol('x')
|
| 87 |
+
m = x*length
|
| 88 |
+
assert m == length*x and m.is_Mul and set(m.args) == {x, length}
|
| 89 |
+
d = x/length
|
| 90 |
+
assert d == x*length**-1 and d.is_Mul and set(d.args) == {x, 1/length}
|
| 91 |
+
d = length/x
|
| 92 |
+
assert d == length*x**-1 and d.is_Mul and set(d.args) == {1/x, length}
|
| 93 |
+
|
| 94 |
+
velo = length / time
|
| 95 |
+
|
| 96 |
+
assert (length * length) == length ** 2
|
| 97 |
+
|
| 98 |
+
assert dimsys_SI.get_dimensional_dependencies(length * length) == {length: 2}
|
| 99 |
+
assert dimsys_SI.get_dimensional_dependencies(length ** 2) == {length: 2}
|
| 100 |
+
assert dimsys_SI.get_dimensional_dependencies(length * time) == {length: 1, time: 1}
|
| 101 |
+
assert dimsys_SI.get_dimensional_dependencies(velo) == {length: 1, time: -1}
|
| 102 |
+
assert dimsys_SI.get_dimensional_dependencies(velo ** 2) == {length: 2, time: -2}
|
| 103 |
+
|
| 104 |
+
assert dimsys_SI.get_dimensional_dependencies(length / length) == {}
|
| 105 |
+
assert dimsys_SI.get_dimensional_dependencies(velo / length * time) == {}
|
| 106 |
+
assert dimsys_SI.get_dimensional_dependencies(length ** -1) == {length: -1}
|
| 107 |
+
assert dimsys_SI.get_dimensional_dependencies(velo ** -1.5) == {length: -1.5, time: 1.5}
|
| 108 |
+
|
| 109 |
+
length_a = length**"a"
|
| 110 |
+
assert dimsys_SI.get_dimensional_dependencies(length_a) == {length: Symbol("a")}
|
| 111 |
+
|
| 112 |
+
assert dimsys_SI.get_dimensional_dependencies(length**pi) == {length: pi}
|
| 113 |
+
assert dimsys_SI.get_dimensional_dependencies(length**(length/length)) == {length: Dimension(1)}
|
| 114 |
+
|
| 115 |
+
raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(length**length))
|
| 116 |
+
|
| 117 |
+
assert length != 1
|
| 118 |
+
assert length / length != 1
|
| 119 |
+
|
| 120 |
+
length_0 = length ** 0
|
| 121 |
+
assert dimsys_SI.get_dimensional_dependencies(length_0) == {}
|
| 122 |
+
|
| 123 |
+
# issue 18738
|
| 124 |
+
a = Symbol('a')
|
| 125 |
+
b = Symbol('b')
|
| 126 |
+
c = sqrt(a**2 + b**2)
|
| 127 |
+
c_dim = c.subs({a: length, b: length})
|
| 128 |
+
assert dimsys_SI.equivalent_dims(c_dim, length)
|
| 129 |
+
|
| 130 |
+
def test_Dimension_functions():
|
| 131 |
+
raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(cos(length)))
|
| 132 |
+
raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(acos(angle)))
|
| 133 |
+
raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(atan2(length, time)))
|
| 134 |
+
raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(log(length)))
|
| 135 |
+
raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(log(100, length)))
|
| 136 |
+
raises(TypeError, lambda: dimsys_SI.get_dimensional_dependencies(log(length, 10)))
|
| 137 |
+
|
| 138 |
+
assert dimsys_SI.get_dimensional_dependencies(pi) == {}
|
| 139 |
+
|
| 140 |
+
assert dimsys_SI.get_dimensional_dependencies(cos(1)) == {}
|
| 141 |
+
assert dimsys_SI.get_dimensional_dependencies(cos(angle)) == {}
|
| 142 |
+
|
| 143 |
+
assert dimsys_SI.get_dimensional_dependencies(atan2(length, length)) == {}
|
| 144 |
+
|
| 145 |
+
assert dimsys_SI.get_dimensional_dependencies(log(length / length, length / length)) == {}
|
| 146 |
+
|
| 147 |
+
assert dimsys_SI.get_dimensional_dependencies(Abs(length)) == {length: 1}
|
| 148 |
+
assert dimsys_SI.get_dimensional_dependencies(Abs(length / length)) == {}
|
| 149 |
+
|
| 150 |
+
assert dimsys_SI.get_dimensional_dependencies(sqrt(-1)) == {}
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_dimensionsystem.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.symbol import symbols
|
| 2 |
+
from sympy.matrices.dense import (Matrix, eye)
|
| 3 |
+
from sympy.physics.units.definitions.dimension_definitions import (
|
| 4 |
+
action, current, length, mass, time,
|
| 5 |
+
velocity)
|
| 6 |
+
from sympy.physics.units.dimensions import DimensionSystem
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def test_extend():
|
| 10 |
+
ms = DimensionSystem((length, time), (velocity,))
|
| 11 |
+
|
| 12 |
+
mks = ms.extend((mass,), (action,))
|
| 13 |
+
|
| 14 |
+
res = DimensionSystem((length, time, mass), (velocity, action))
|
| 15 |
+
assert mks.base_dims == res.base_dims
|
| 16 |
+
assert mks.derived_dims == res.derived_dims
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def test_list_dims():
|
| 20 |
+
dimsys = DimensionSystem((length, time, mass))
|
| 21 |
+
|
| 22 |
+
assert dimsys.list_can_dims == (length, mass, time)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_dim_can_vector():
|
| 26 |
+
dimsys = DimensionSystem(
|
| 27 |
+
[length, mass, time],
|
| 28 |
+
[velocity, action],
|
| 29 |
+
{
|
| 30 |
+
velocity: {length: 1, time: -1}
|
| 31 |
+
}
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
assert dimsys.dim_can_vector(length) == Matrix([1, 0, 0])
|
| 35 |
+
assert dimsys.dim_can_vector(velocity) == Matrix([1, 0, -1])
|
| 36 |
+
|
| 37 |
+
dimsys = DimensionSystem(
|
| 38 |
+
(length, velocity, action),
|
| 39 |
+
(mass, time),
|
| 40 |
+
{
|
| 41 |
+
time: {length: 1, velocity: -1}
|
| 42 |
+
}
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
assert dimsys.dim_can_vector(length) == Matrix([0, 1, 0])
|
| 46 |
+
assert dimsys.dim_can_vector(velocity) == Matrix([0, 0, 1])
|
| 47 |
+
assert dimsys.dim_can_vector(time) == Matrix([0, 1, -1])
|
| 48 |
+
|
| 49 |
+
dimsys = DimensionSystem(
|
| 50 |
+
(length, mass, time),
|
| 51 |
+
(velocity, action),
|
| 52 |
+
{velocity: {length: 1, time: -1},
|
| 53 |
+
action: {mass: 1, length: 2, time: -1}})
|
| 54 |
+
|
| 55 |
+
assert dimsys.dim_vector(length) == Matrix([1, 0, 0])
|
| 56 |
+
assert dimsys.dim_vector(velocity) == Matrix([1, 0, -1])
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_inv_can_transf_matrix():
|
| 60 |
+
dimsys = DimensionSystem((length, mass, time))
|
| 61 |
+
assert dimsys.inv_can_transf_matrix == eye(3)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def test_can_transf_matrix():
|
| 65 |
+
dimsys = DimensionSystem((length, mass, time))
|
| 66 |
+
assert dimsys.can_transf_matrix == eye(3)
|
| 67 |
+
|
| 68 |
+
dimsys = DimensionSystem((length, velocity, action))
|
| 69 |
+
assert dimsys.can_transf_matrix == eye(3)
|
| 70 |
+
|
| 71 |
+
dimsys = DimensionSystem((length, time), (velocity,), {velocity: {length: 1, time: -1}})
|
| 72 |
+
assert dimsys.can_transf_matrix == eye(2)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def test_is_consistent():
|
| 76 |
+
assert DimensionSystem((length, time)).is_consistent is True
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def test_print_dim_base():
|
| 80 |
+
mksa = DimensionSystem(
|
| 81 |
+
(length, time, mass, current),
|
| 82 |
+
(action,),
|
| 83 |
+
{action: {mass: 1, length: 2, time: -1}})
|
| 84 |
+
L, M, T = symbols("L M T")
|
| 85 |
+
assert mksa.print_dim_base(action) == L**2*M/T
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def test_dim():
|
| 89 |
+
dimsys = DimensionSystem(
|
| 90 |
+
(length, mass, time),
|
| 91 |
+
(velocity, action),
|
| 92 |
+
{velocity: {length: 1, time: -1},
|
| 93 |
+
action: {mass: 1, length: 2, time: -1}}
|
| 94 |
+
)
|
| 95 |
+
assert dimsys.dim == 3
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_prefixes.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.mul import Mul
|
| 2 |
+
from sympy.core.numbers import Rational
|
| 3 |
+
from sympy.core.singleton import S
|
| 4 |
+
from sympy.core.symbol import (Symbol, symbols)
|
| 5 |
+
from sympy.physics.units import Quantity, length, meter, W
|
| 6 |
+
from sympy.physics.units.prefixes import PREFIXES, Prefix, prefix_unit, kilo, \
|
| 7 |
+
kibi
|
| 8 |
+
from sympy.physics.units.systems import SI
|
| 9 |
+
|
| 10 |
+
x = Symbol('x')
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def test_prefix_operations():
|
| 14 |
+
m = PREFIXES['m']
|
| 15 |
+
k = PREFIXES['k']
|
| 16 |
+
M = PREFIXES['M']
|
| 17 |
+
|
| 18 |
+
dodeca = Prefix('dodeca', 'dd', 1, base=12)
|
| 19 |
+
|
| 20 |
+
assert m * k is S.One
|
| 21 |
+
assert m * W == W / 1000
|
| 22 |
+
assert k * k == M
|
| 23 |
+
assert 1 / m == k
|
| 24 |
+
assert k / m == M
|
| 25 |
+
|
| 26 |
+
assert dodeca * dodeca == 144
|
| 27 |
+
assert 1 / dodeca == S.One / 12
|
| 28 |
+
assert k / dodeca == S(1000) / 12
|
| 29 |
+
assert dodeca / dodeca is S.One
|
| 30 |
+
|
| 31 |
+
m = Quantity("fake_meter")
|
| 32 |
+
SI.set_quantity_dimension(m, S.One)
|
| 33 |
+
SI.set_quantity_scale_factor(m, S.One)
|
| 34 |
+
|
| 35 |
+
assert dodeca * m == 12 * m
|
| 36 |
+
assert dodeca / m == 12 / m
|
| 37 |
+
|
| 38 |
+
expr1 = kilo * 3
|
| 39 |
+
assert isinstance(expr1, Mul)
|
| 40 |
+
assert expr1.args == (3, kilo)
|
| 41 |
+
|
| 42 |
+
expr2 = kilo * x
|
| 43 |
+
assert isinstance(expr2, Mul)
|
| 44 |
+
assert expr2.args == (x, kilo)
|
| 45 |
+
|
| 46 |
+
expr3 = kilo / 3
|
| 47 |
+
assert isinstance(expr3, Mul)
|
| 48 |
+
assert expr3.args == (Rational(1, 3), kilo)
|
| 49 |
+
assert expr3.args == (S.One/3, kilo)
|
| 50 |
+
|
| 51 |
+
expr4 = kilo / x
|
| 52 |
+
assert isinstance(expr4, Mul)
|
| 53 |
+
assert expr4.args == (1/x, kilo)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def test_prefix_unit():
|
| 57 |
+
m = Quantity("fake_meter", abbrev="m")
|
| 58 |
+
m.set_global_relative_scale_factor(1, meter)
|
| 59 |
+
|
| 60 |
+
pref = {"m": PREFIXES["m"], "c": PREFIXES["c"], "d": PREFIXES["d"]}
|
| 61 |
+
|
| 62 |
+
q1 = Quantity("millifake_meter", abbrev="mm")
|
| 63 |
+
q2 = Quantity("centifake_meter", abbrev="cm")
|
| 64 |
+
q3 = Quantity("decifake_meter", abbrev="dm")
|
| 65 |
+
|
| 66 |
+
SI.set_quantity_dimension(q1, length)
|
| 67 |
+
|
| 68 |
+
SI.set_quantity_scale_factor(q1, PREFIXES["m"])
|
| 69 |
+
SI.set_quantity_scale_factor(q1, PREFIXES["c"])
|
| 70 |
+
SI.set_quantity_scale_factor(q1, PREFIXES["d"])
|
| 71 |
+
|
| 72 |
+
res = [q1, q2, q3]
|
| 73 |
+
|
| 74 |
+
prefs = prefix_unit(m, pref)
|
| 75 |
+
assert set(prefs) == set(res)
|
| 76 |
+
assert {v.abbrev for v in prefs} == set(symbols("mm,cm,dm"))
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def test_bases():
|
| 80 |
+
assert kilo.base == 10
|
| 81 |
+
assert kibi.base == 2
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def test_repr():
|
| 85 |
+
assert eval(repr(kilo)) == kilo
|
| 86 |
+
assert eval(repr(kibi)) == kibi
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_quantities.py
ADDED
|
@@ -0,0 +1,575 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
|
| 3 |
+
from sympy.core.add import Add
|
| 4 |
+
from sympy.core.function import (Function, diff)
|
| 5 |
+
from sympy.core.numbers import (Number, Rational)
|
| 6 |
+
from sympy.core.singleton import S
|
| 7 |
+
from sympy.core.symbol import (Symbol, symbols)
|
| 8 |
+
from sympy.functions.elementary.complexes import Abs
|
| 9 |
+
from sympy.functions.elementary.exponential import (exp, log)
|
| 10 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 11 |
+
from sympy.functions.elementary.trigonometric import sin
|
| 12 |
+
from sympy.integrals.integrals import integrate
|
| 13 |
+
from sympy.physics.units import (amount_of_substance, area, convert_to, find_unit,
|
| 14 |
+
volume, kilometer, joule, molar_gas_constant,
|
| 15 |
+
vacuum_permittivity, elementary_charge, volt,
|
| 16 |
+
ohm)
|
| 17 |
+
from sympy.physics.units.definitions import (amu, au, centimeter, coulomb,
|
| 18 |
+
day, foot, grams, hour, inch, kg, km, m, meter, millimeter,
|
| 19 |
+
minute, quart, s, second, speed_of_light, bit,
|
| 20 |
+
byte, kibibyte, mebibyte, gibibyte, tebibyte, pebibyte, exbibyte,
|
| 21 |
+
kilogram, gravitational_constant, electron_rest_mass)
|
| 22 |
+
|
| 23 |
+
from sympy.physics.units.definitions.dimension_definitions import (
|
| 24 |
+
Dimension, charge, length, time, temperature, pressure,
|
| 25 |
+
energy, mass
|
| 26 |
+
)
|
| 27 |
+
from sympy.physics.units.prefixes import PREFIXES, kilo
|
| 28 |
+
from sympy.physics.units.quantities import PhysicalConstant, Quantity
|
| 29 |
+
from sympy.physics.units.systems import SI
|
| 30 |
+
from sympy.testing.pytest import raises
|
| 31 |
+
|
| 32 |
+
k = PREFIXES["k"]
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def test_str_repr():
|
| 36 |
+
assert str(kg) == "kilogram"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def test_eq():
|
| 40 |
+
# simple test
|
| 41 |
+
assert 10*m == 10*m
|
| 42 |
+
assert 10*m != 10*s
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def test_convert_to():
|
| 46 |
+
q = Quantity("q1")
|
| 47 |
+
q.set_global_relative_scale_factor(S(5000), meter)
|
| 48 |
+
|
| 49 |
+
assert q.convert_to(m) == 5000*m
|
| 50 |
+
|
| 51 |
+
assert speed_of_light.convert_to(m / s) == 299792458 * m / s
|
| 52 |
+
assert day.convert_to(s) == 86400*s
|
| 53 |
+
|
| 54 |
+
# Wrong dimension to convert:
|
| 55 |
+
assert q.convert_to(s) == q
|
| 56 |
+
assert speed_of_light.convert_to(m) == speed_of_light
|
| 57 |
+
|
| 58 |
+
expr = joule*second
|
| 59 |
+
conv = convert_to(expr, joule)
|
| 60 |
+
assert conv == joule*second
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def test_Quantity_definition():
|
| 64 |
+
q = Quantity("s10", abbrev="sabbr")
|
| 65 |
+
q.set_global_relative_scale_factor(10, second)
|
| 66 |
+
u = Quantity("u", abbrev="dam")
|
| 67 |
+
u.set_global_relative_scale_factor(10, meter)
|
| 68 |
+
km = Quantity("km")
|
| 69 |
+
km.set_global_relative_scale_factor(kilo, meter)
|
| 70 |
+
v = Quantity("u")
|
| 71 |
+
v.set_global_relative_scale_factor(5*kilo, meter)
|
| 72 |
+
|
| 73 |
+
assert q.scale_factor == 10
|
| 74 |
+
assert q.dimension == time
|
| 75 |
+
assert q.abbrev == Symbol("sabbr")
|
| 76 |
+
|
| 77 |
+
assert u.dimension == length
|
| 78 |
+
assert u.scale_factor == 10
|
| 79 |
+
assert u.abbrev == Symbol("dam")
|
| 80 |
+
|
| 81 |
+
assert km.scale_factor == 1000
|
| 82 |
+
assert km.func(*km.args) == km
|
| 83 |
+
assert km.func(*km.args).args == km.args
|
| 84 |
+
|
| 85 |
+
assert v.dimension == length
|
| 86 |
+
assert v.scale_factor == 5000
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def test_abbrev():
|
| 90 |
+
u = Quantity("u")
|
| 91 |
+
u.set_global_relative_scale_factor(S.One, meter)
|
| 92 |
+
|
| 93 |
+
assert u.name == Symbol("u")
|
| 94 |
+
assert u.abbrev == Symbol("u")
|
| 95 |
+
|
| 96 |
+
u = Quantity("u", abbrev="om")
|
| 97 |
+
u.set_global_relative_scale_factor(S(2), meter)
|
| 98 |
+
|
| 99 |
+
assert u.name == Symbol("u")
|
| 100 |
+
assert u.abbrev == Symbol("om")
|
| 101 |
+
assert u.scale_factor == 2
|
| 102 |
+
assert isinstance(u.scale_factor, Number)
|
| 103 |
+
|
| 104 |
+
u = Quantity("u", abbrev="ikm")
|
| 105 |
+
u.set_global_relative_scale_factor(3*kilo, meter)
|
| 106 |
+
|
| 107 |
+
assert u.abbrev == Symbol("ikm")
|
| 108 |
+
assert u.scale_factor == 3000
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def test_print():
|
| 112 |
+
u = Quantity("unitname", abbrev="dam")
|
| 113 |
+
assert repr(u) == "unitname"
|
| 114 |
+
assert str(u) == "unitname"
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def test_Quantity_eq():
|
| 118 |
+
u = Quantity("u", abbrev="dam")
|
| 119 |
+
v = Quantity("v1")
|
| 120 |
+
assert u != v
|
| 121 |
+
v = Quantity("v2", abbrev="ds")
|
| 122 |
+
assert u != v
|
| 123 |
+
v = Quantity("v3", abbrev="dm")
|
| 124 |
+
assert u != v
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def test_add_sub():
|
| 128 |
+
u = Quantity("u")
|
| 129 |
+
v = Quantity("v")
|
| 130 |
+
w = Quantity("w")
|
| 131 |
+
|
| 132 |
+
u.set_global_relative_scale_factor(S(10), meter)
|
| 133 |
+
v.set_global_relative_scale_factor(S(5), meter)
|
| 134 |
+
w.set_global_relative_scale_factor(S(2), second)
|
| 135 |
+
|
| 136 |
+
assert isinstance(u + v, Add)
|
| 137 |
+
assert (u + v.convert_to(u)) == (1 + S.Half)*u
|
| 138 |
+
assert isinstance(u - v, Add)
|
| 139 |
+
assert (u - v.convert_to(u)) == S.Half*u
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def test_quantity_abs():
|
| 143 |
+
v_w1 = Quantity('v_w1')
|
| 144 |
+
v_w2 = Quantity('v_w2')
|
| 145 |
+
v_w3 = Quantity('v_w3')
|
| 146 |
+
|
| 147 |
+
v_w1.set_global_relative_scale_factor(1, meter/second)
|
| 148 |
+
v_w2.set_global_relative_scale_factor(1, meter/second)
|
| 149 |
+
v_w3.set_global_relative_scale_factor(1, meter/second)
|
| 150 |
+
|
| 151 |
+
expr = v_w3 - Abs(v_w1 - v_w2)
|
| 152 |
+
|
| 153 |
+
assert SI.get_dimensional_expr(v_w1) == (length/time).name
|
| 154 |
+
|
| 155 |
+
Dq = Dimension(SI.get_dimensional_expr(expr))
|
| 156 |
+
|
| 157 |
+
assert SI.get_dimension_system().get_dimensional_dependencies(Dq) == {
|
| 158 |
+
length: 1,
|
| 159 |
+
time: -1,
|
| 160 |
+
}
|
| 161 |
+
assert meter == sqrt(meter**2)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def test_check_unit_consistency():
|
| 165 |
+
u = Quantity("u")
|
| 166 |
+
v = Quantity("v")
|
| 167 |
+
w = Quantity("w")
|
| 168 |
+
|
| 169 |
+
u.set_global_relative_scale_factor(S(10), meter)
|
| 170 |
+
v.set_global_relative_scale_factor(S(5), meter)
|
| 171 |
+
w.set_global_relative_scale_factor(S(2), second)
|
| 172 |
+
|
| 173 |
+
def check_unit_consistency(expr):
|
| 174 |
+
SI._collect_factor_and_dimension(expr)
|
| 175 |
+
|
| 176 |
+
raises(ValueError, lambda: check_unit_consistency(u + w))
|
| 177 |
+
raises(ValueError, lambda: check_unit_consistency(u - w))
|
| 178 |
+
raises(ValueError, lambda: check_unit_consistency(u + 1))
|
| 179 |
+
raises(ValueError, lambda: check_unit_consistency(u - 1))
|
| 180 |
+
raises(ValueError, lambda: check_unit_consistency(1 - exp(u / w)))
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def test_mul_div():
|
| 184 |
+
u = Quantity("u")
|
| 185 |
+
v = Quantity("v")
|
| 186 |
+
t = Quantity("t")
|
| 187 |
+
ut = Quantity("ut")
|
| 188 |
+
v2 = Quantity("v")
|
| 189 |
+
|
| 190 |
+
u.set_global_relative_scale_factor(S(10), meter)
|
| 191 |
+
v.set_global_relative_scale_factor(S(5), meter)
|
| 192 |
+
t.set_global_relative_scale_factor(S(2), second)
|
| 193 |
+
ut.set_global_relative_scale_factor(S(20), meter*second)
|
| 194 |
+
v2.set_global_relative_scale_factor(S(5), meter/second)
|
| 195 |
+
|
| 196 |
+
assert 1 / u == u**(-1)
|
| 197 |
+
assert u / 1 == u
|
| 198 |
+
|
| 199 |
+
v1 = u / t
|
| 200 |
+
v2 = v
|
| 201 |
+
|
| 202 |
+
# Pow only supports structural equality:
|
| 203 |
+
assert v1 != v2
|
| 204 |
+
assert v1 == v2.convert_to(v1)
|
| 205 |
+
|
| 206 |
+
# TODO: decide whether to allow such expression in the future
|
| 207 |
+
# (requires somehow manipulating the core).
|
| 208 |
+
# assert u / Quantity('l2', dimension=length, scale_factor=2) == 5
|
| 209 |
+
|
| 210 |
+
assert u * 1 == u
|
| 211 |
+
|
| 212 |
+
ut1 = u * t
|
| 213 |
+
ut2 = ut
|
| 214 |
+
|
| 215 |
+
# Mul only supports structural equality:
|
| 216 |
+
assert ut1 != ut2
|
| 217 |
+
assert ut1 == ut2.convert_to(ut1)
|
| 218 |
+
|
| 219 |
+
# Mul only supports structural equality:
|
| 220 |
+
lp1 = Quantity("lp1")
|
| 221 |
+
lp1.set_global_relative_scale_factor(S(2), 1/meter)
|
| 222 |
+
assert u * lp1 != 20
|
| 223 |
+
|
| 224 |
+
assert u**0 == 1
|
| 225 |
+
assert u**1 == u
|
| 226 |
+
|
| 227 |
+
# TODO: Pow only support structural equality:
|
| 228 |
+
u2 = Quantity("u2")
|
| 229 |
+
u3 = Quantity("u3")
|
| 230 |
+
u2.set_global_relative_scale_factor(S(100), meter**2)
|
| 231 |
+
u3.set_global_relative_scale_factor(Rational(1, 10), 1/meter)
|
| 232 |
+
|
| 233 |
+
assert u ** 2 != u2
|
| 234 |
+
assert u ** -1 != u3
|
| 235 |
+
|
| 236 |
+
assert u ** 2 == u2.convert_to(u)
|
| 237 |
+
assert u ** -1 == u3.convert_to(u)
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
def test_units():
|
| 241 |
+
assert convert_to((5*m/s * day) / km, 1) == 432
|
| 242 |
+
assert convert_to(foot / meter, meter) == Rational(3048, 10000)
|
| 243 |
+
# amu is a pure mass so mass/mass gives a number, not an amount (mol)
|
| 244 |
+
# TODO: need better simplification routine:
|
| 245 |
+
assert str(convert_to(grams/amu, grams).n(2)) == '6.0e+23'
|
| 246 |
+
|
| 247 |
+
# Light from the sun needs about 8.3 minutes to reach earth
|
| 248 |
+
t = (1*au / speed_of_light) / minute
|
| 249 |
+
# TODO: need a better way to simplify expressions containing units:
|
| 250 |
+
t = convert_to(convert_to(t, meter / minute), meter)
|
| 251 |
+
assert t.simplify() == Rational(49865956897, 5995849160)
|
| 252 |
+
|
| 253 |
+
# TODO: fix this, it should give `m` without `Abs`
|
| 254 |
+
assert sqrt(m**2) == m
|
| 255 |
+
assert (sqrt(m))**2 == m
|
| 256 |
+
|
| 257 |
+
t = Symbol('t')
|
| 258 |
+
assert integrate(t*m/s, (t, 1*s, 5*s)) == 12*m*s
|
| 259 |
+
assert (t * m/s).integrate((t, 1*s, 5*s)) == 12*m*s
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def test_issue_quart():
|
| 263 |
+
assert convert_to(4 * quart / inch ** 3, meter) == 231
|
| 264 |
+
assert convert_to(4 * quart / inch ** 3, millimeter) == 231
|
| 265 |
+
|
| 266 |
+
def test_electron_rest_mass():
|
| 267 |
+
assert convert_to(electron_rest_mass, kilogram) == 9.1093837015e-31*kilogram
|
| 268 |
+
assert convert_to(electron_rest_mass, grams) == 9.1093837015e-28*grams
|
| 269 |
+
|
| 270 |
+
def test_issue_5565():
|
| 271 |
+
assert (m < s).is_Relational
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def test_find_unit():
|
| 275 |
+
assert find_unit('coulomb') == ['coulomb', 'coulombs', 'coulomb_constant']
|
| 276 |
+
assert find_unit(coulomb) == ['C', 'coulomb', 'coulombs', 'planck_charge', 'elementary_charge']
|
| 277 |
+
assert find_unit(charge) == ['C', 'coulomb', 'coulombs', 'planck_charge', 'elementary_charge']
|
| 278 |
+
assert find_unit(inch) == [
|
| 279 |
+
'm', 'au', 'cm', 'dm', 'ft', 'km', 'ly', 'mi', 'mm', 'nm', 'pm', 'um', 'yd',
|
| 280 |
+
'nmi', 'feet', 'foot', 'inch', 'mile', 'yard', 'meter', 'miles', 'yards',
|
| 281 |
+
'inches', 'meters', 'micron', 'microns', 'angstrom', 'angstroms', 'decimeter',
|
| 282 |
+
'kilometer', 'lightyear', 'nanometer', 'picometer', 'centimeter', 'decimeters',
|
| 283 |
+
'kilometers', 'lightyears', 'micrometer', 'millimeter', 'nanometers', 'picometers',
|
| 284 |
+
'centimeters', 'micrometers', 'millimeters', 'nautical_mile', 'planck_length',
|
| 285 |
+
'nautical_miles', 'astronomical_unit', 'astronomical_units']
|
| 286 |
+
assert find_unit(inch**-1) == ['D', 'dioptre', 'optical_power']
|
| 287 |
+
assert find_unit(length**-1) == ['D', 'dioptre', 'optical_power']
|
| 288 |
+
assert find_unit(inch ** 2) == ['ha', 'hectare', 'planck_area']
|
| 289 |
+
assert find_unit(inch ** 3) == [
|
| 290 |
+
'L', 'l', 'cL', 'cl', 'dL', 'dl', 'mL', 'ml', 'liter', 'quart', 'liters', 'quarts',
|
| 291 |
+
'deciliter', 'centiliter', 'deciliters', 'milliliter',
|
| 292 |
+
'centiliters', 'milliliters', 'planck_volume']
|
| 293 |
+
assert find_unit('voltage') == ['V', 'v', 'volt', 'volts', 'planck_voltage']
|
| 294 |
+
assert find_unit(grams) == ['g', 't', 'Da', 'kg', 'me', 'mg', 'ug', 'amu', 'mmu', 'amus',
|
| 295 |
+
'gram', 'mmus', 'grams', 'pound', 'tonne', 'dalton', 'pounds',
|
| 296 |
+
'kilogram', 'kilograms', 'microgram', 'milligram', 'metric_ton',
|
| 297 |
+
'micrograms', 'milligrams', 'planck_mass', 'milli_mass_unit', 'atomic_mass_unit',
|
| 298 |
+
'electron_rest_mass', 'atomic_mass_constant']
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
def test_Quantity_derivative():
|
| 302 |
+
x = symbols("x")
|
| 303 |
+
assert diff(x*meter, x) == meter
|
| 304 |
+
assert diff(x**3*meter**2, x) == 3*x**2*meter**2
|
| 305 |
+
assert diff(meter, meter) == 1
|
| 306 |
+
assert diff(meter**2, meter) == 2*meter
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
def test_quantity_postprocessing():
|
| 310 |
+
q1 = Quantity('q1')
|
| 311 |
+
q2 = Quantity('q2')
|
| 312 |
+
|
| 313 |
+
SI.set_quantity_dimension(q1, length*pressure**2*temperature/time)
|
| 314 |
+
SI.set_quantity_dimension(q2, energy*pressure*temperature/(length**2*time))
|
| 315 |
+
|
| 316 |
+
assert q1 + q2
|
| 317 |
+
q = q1 + q2
|
| 318 |
+
Dq = Dimension(SI.get_dimensional_expr(q))
|
| 319 |
+
assert SI.get_dimension_system().get_dimensional_dependencies(Dq) == {
|
| 320 |
+
length: -1,
|
| 321 |
+
mass: 2,
|
| 322 |
+
temperature: 1,
|
| 323 |
+
time: -5,
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def test_factor_and_dimension():
|
| 328 |
+
assert (3000, Dimension(1)) == SI._collect_factor_and_dimension(3000)
|
| 329 |
+
assert (1001, length) == SI._collect_factor_and_dimension(meter + km)
|
| 330 |
+
assert (2, length/time) == SI._collect_factor_and_dimension(
|
| 331 |
+
meter/second + 36*km/(10*hour))
|
| 332 |
+
|
| 333 |
+
x, y = symbols('x y')
|
| 334 |
+
assert (x + y/100, length) == SI._collect_factor_and_dimension(
|
| 335 |
+
x*m + y*centimeter)
|
| 336 |
+
|
| 337 |
+
cH = Quantity('cH')
|
| 338 |
+
SI.set_quantity_dimension(cH, amount_of_substance/volume)
|
| 339 |
+
|
| 340 |
+
pH = -log(cH)
|
| 341 |
+
|
| 342 |
+
assert (1, volume/amount_of_substance) == SI._collect_factor_and_dimension(
|
| 343 |
+
exp(pH))
|
| 344 |
+
|
| 345 |
+
v_w1 = Quantity('v_w1')
|
| 346 |
+
v_w2 = Quantity('v_w2')
|
| 347 |
+
|
| 348 |
+
v_w1.set_global_relative_scale_factor(Rational(3, 2), meter/second)
|
| 349 |
+
v_w2.set_global_relative_scale_factor(2, meter/second)
|
| 350 |
+
|
| 351 |
+
expr = Abs(v_w1/2 - v_w2)
|
| 352 |
+
assert (Rational(5, 4), length/time) == \
|
| 353 |
+
SI._collect_factor_and_dimension(expr)
|
| 354 |
+
|
| 355 |
+
expr = Rational(5, 2)*second/meter*v_w1 - 3000
|
| 356 |
+
assert (-(2996 + Rational(1, 4)), Dimension(1)) == \
|
| 357 |
+
SI._collect_factor_and_dimension(expr)
|
| 358 |
+
|
| 359 |
+
expr = v_w1**(v_w2/v_w1)
|
| 360 |
+
assert ((Rational(3, 2))**Rational(4, 3), (length/time)**Rational(4, 3)) == \
|
| 361 |
+
SI._collect_factor_and_dimension(expr)
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def test_dimensional_expr_of_derivative():
|
| 365 |
+
l = Quantity('l')
|
| 366 |
+
t = Quantity('t')
|
| 367 |
+
t1 = Quantity('t1')
|
| 368 |
+
l.set_global_relative_scale_factor(36, km)
|
| 369 |
+
t.set_global_relative_scale_factor(1, hour)
|
| 370 |
+
t1.set_global_relative_scale_factor(1, second)
|
| 371 |
+
x = Symbol('x')
|
| 372 |
+
y = Symbol('y')
|
| 373 |
+
f = Function('f')
|
| 374 |
+
dfdx = f(x, y).diff(x, y)
|
| 375 |
+
dl_dt = dfdx.subs({f(x, y): l, x: t, y: t1})
|
| 376 |
+
assert SI.get_dimensional_expr(dl_dt) ==\
|
| 377 |
+
SI.get_dimensional_expr(l / t / t1) ==\
|
| 378 |
+
Symbol("length")/Symbol("time")**2
|
| 379 |
+
assert SI._collect_factor_and_dimension(dl_dt) ==\
|
| 380 |
+
SI._collect_factor_and_dimension(l / t / t1) ==\
|
| 381 |
+
(10, length/time**2)
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
def test_get_dimensional_expr_with_function():
|
| 385 |
+
v_w1 = Quantity('v_w1')
|
| 386 |
+
v_w2 = Quantity('v_w2')
|
| 387 |
+
v_w1.set_global_relative_scale_factor(1, meter/second)
|
| 388 |
+
v_w2.set_global_relative_scale_factor(1, meter/second)
|
| 389 |
+
|
| 390 |
+
assert SI.get_dimensional_expr(sin(v_w1)) == \
|
| 391 |
+
sin(SI.get_dimensional_expr(v_w1))
|
| 392 |
+
assert SI.get_dimensional_expr(sin(v_w1/v_w2)) == 1
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def test_binary_information():
|
| 396 |
+
assert convert_to(kibibyte, byte) == 1024*byte
|
| 397 |
+
assert convert_to(mebibyte, byte) == 1024**2*byte
|
| 398 |
+
assert convert_to(gibibyte, byte) == 1024**3*byte
|
| 399 |
+
assert convert_to(tebibyte, byte) == 1024**4*byte
|
| 400 |
+
assert convert_to(pebibyte, byte) == 1024**5*byte
|
| 401 |
+
assert convert_to(exbibyte, byte) == 1024**6*byte
|
| 402 |
+
|
| 403 |
+
assert kibibyte.convert_to(bit) == 8*1024*bit
|
| 404 |
+
assert byte.convert_to(bit) == 8*bit
|
| 405 |
+
|
| 406 |
+
a = 10*kibibyte*hour
|
| 407 |
+
|
| 408 |
+
assert convert_to(a, byte) == 10240*byte*hour
|
| 409 |
+
assert convert_to(a, minute) == 600*kibibyte*minute
|
| 410 |
+
assert convert_to(a, [byte, minute]) == 614400*byte*minute
|
| 411 |
+
|
| 412 |
+
|
| 413 |
+
def test_conversion_with_2_nonstandard_dimensions():
|
| 414 |
+
good_grade = Quantity("good_grade")
|
| 415 |
+
kilo_good_grade = Quantity("kilo_good_grade")
|
| 416 |
+
centi_good_grade = Quantity("centi_good_grade")
|
| 417 |
+
|
| 418 |
+
kilo_good_grade.set_global_relative_scale_factor(1000, good_grade)
|
| 419 |
+
centi_good_grade.set_global_relative_scale_factor(S.One/10**5, kilo_good_grade)
|
| 420 |
+
|
| 421 |
+
charity_points = Quantity("charity_points")
|
| 422 |
+
milli_charity_points = Quantity("milli_charity_points")
|
| 423 |
+
missions = Quantity("missions")
|
| 424 |
+
|
| 425 |
+
milli_charity_points.set_global_relative_scale_factor(S.One/1000, charity_points)
|
| 426 |
+
missions.set_global_relative_scale_factor(251, charity_points)
|
| 427 |
+
|
| 428 |
+
assert convert_to(
|
| 429 |
+
kilo_good_grade*milli_charity_points*millimeter,
|
| 430 |
+
[centi_good_grade, missions, centimeter]
|
| 431 |
+
) == S.One * 10**5 / (251*1000) / 10 * centi_good_grade*missions*centimeter
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
def test_eval_subs():
|
| 435 |
+
energy, mass, force = symbols('energy mass force')
|
| 436 |
+
expr1 = energy/mass
|
| 437 |
+
units = {energy: kilogram*meter**2/second**2, mass: kilogram}
|
| 438 |
+
assert expr1.subs(units) == meter**2/second**2
|
| 439 |
+
expr2 = force/mass
|
| 440 |
+
units = {force:gravitational_constant*kilogram**2/meter**2, mass:kilogram}
|
| 441 |
+
assert expr2.subs(units) == gravitational_constant*kilogram/meter**2
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
def test_issue_14932():
|
| 445 |
+
assert (log(inch) - log(2)).simplify() == log(inch/2)
|
| 446 |
+
assert (log(inch) - log(foot)).simplify() == -log(12)
|
| 447 |
+
p = symbols('p', positive=True)
|
| 448 |
+
assert (log(inch) - log(p)).simplify() == log(inch/p)
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
def test_issue_14547():
|
| 452 |
+
# the root issue is that an argument with dimensions should
|
| 453 |
+
# not raise an error when the `arg - 1` calculation is
|
| 454 |
+
# performed in the assumptions system
|
| 455 |
+
from sympy.physics.units import foot, inch
|
| 456 |
+
from sympy.core.relational import Eq
|
| 457 |
+
assert log(foot).is_zero is None
|
| 458 |
+
assert log(foot).is_positive is None
|
| 459 |
+
assert log(foot).is_nonnegative is None
|
| 460 |
+
assert log(foot).is_negative is None
|
| 461 |
+
assert log(foot).is_algebraic is None
|
| 462 |
+
assert log(foot).is_rational is None
|
| 463 |
+
# doesn't raise error
|
| 464 |
+
assert Eq(log(foot), log(inch)) is not None # might be False or unevaluated
|
| 465 |
+
|
| 466 |
+
x = Symbol('x')
|
| 467 |
+
e = foot + x
|
| 468 |
+
assert e.is_Add and set(e.args) == {foot, x}
|
| 469 |
+
e = foot + 1
|
| 470 |
+
assert e.is_Add and set(e.args) == {foot, 1}
|
| 471 |
+
|
| 472 |
+
|
| 473 |
+
def test_issue_22164():
|
| 474 |
+
warnings.simplefilter("error")
|
| 475 |
+
dm = Quantity("dm")
|
| 476 |
+
SI.set_quantity_dimension(dm, length)
|
| 477 |
+
SI.set_quantity_scale_factor(dm, 1)
|
| 478 |
+
|
| 479 |
+
bad_exp = Quantity("bad_exp")
|
| 480 |
+
SI.set_quantity_dimension(bad_exp, length)
|
| 481 |
+
SI.set_quantity_scale_factor(bad_exp, 1)
|
| 482 |
+
|
| 483 |
+
expr = dm ** bad_exp
|
| 484 |
+
|
| 485 |
+
# deprecation warning is not expected here
|
| 486 |
+
SI._collect_factor_and_dimension(expr)
|
| 487 |
+
|
| 488 |
+
|
| 489 |
+
def test_issue_22819():
|
| 490 |
+
from sympy.physics.units import tonne, gram, Da
|
| 491 |
+
from sympy.physics.units.systems.si import dimsys_SI
|
| 492 |
+
assert tonne.convert_to(gram) == 1000000*gram
|
| 493 |
+
assert dimsys_SI.get_dimensional_dependencies(area) == {length: 2}
|
| 494 |
+
assert Da.scale_factor == 1.66053906660000e-24
|
| 495 |
+
|
| 496 |
+
|
| 497 |
+
def test_issue_20288():
|
| 498 |
+
from sympy.core.numbers import E
|
| 499 |
+
from sympy.physics.units import energy
|
| 500 |
+
u = Quantity('u')
|
| 501 |
+
v = Quantity('v')
|
| 502 |
+
SI.set_quantity_dimension(u, energy)
|
| 503 |
+
SI.set_quantity_dimension(v, energy)
|
| 504 |
+
u.set_global_relative_scale_factor(1, joule)
|
| 505 |
+
v.set_global_relative_scale_factor(1, joule)
|
| 506 |
+
expr = 1 + exp(u**2/v**2)
|
| 507 |
+
assert SI._collect_factor_and_dimension(expr) == (1 + E, Dimension(1))
|
| 508 |
+
|
| 509 |
+
|
| 510 |
+
def test_issue_24062():
|
| 511 |
+
from sympy.core.numbers import E
|
| 512 |
+
from sympy.physics.units import impedance, capacitance, time, ohm, farad, second
|
| 513 |
+
|
| 514 |
+
R = Quantity('R')
|
| 515 |
+
C = Quantity('C')
|
| 516 |
+
T = Quantity('T')
|
| 517 |
+
SI.set_quantity_dimension(R, impedance)
|
| 518 |
+
SI.set_quantity_dimension(C, capacitance)
|
| 519 |
+
SI.set_quantity_dimension(T, time)
|
| 520 |
+
R.set_global_relative_scale_factor(1, ohm)
|
| 521 |
+
C.set_global_relative_scale_factor(1, farad)
|
| 522 |
+
T.set_global_relative_scale_factor(1, second)
|
| 523 |
+
expr = T / (R * C)
|
| 524 |
+
dim = SI._collect_factor_and_dimension(expr)[1]
|
| 525 |
+
assert SI.get_dimension_system().is_dimensionless(dim)
|
| 526 |
+
|
| 527 |
+
exp_expr = 1 + exp(expr)
|
| 528 |
+
assert SI._collect_factor_and_dimension(exp_expr) == (1 + E, Dimension(1))
|
| 529 |
+
|
| 530 |
+
def test_issue_24211():
|
| 531 |
+
from sympy.physics.units import time, velocity, acceleration, second, meter
|
| 532 |
+
V1 = Quantity('V1')
|
| 533 |
+
SI.set_quantity_dimension(V1, velocity)
|
| 534 |
+
SI.set_quantity_scale_factor(V1, 1 * meter / second)
|
| 535 |
+
A1 = Quantity('A1')
|
| 536 |
+
SI.set_quantity_dimension(A1, acceleration)
|
| 537 |
+
SI.set_quantity_scale_factor(A1, 1 * meter / second**2)
|
| 538 |
+
T1 = Quantity('T1')
|
| 539 |
+
SI.set_quantity_dimension(T1, time)
|
| 540 |
+
SI.set_quantity_scale_factor(T1, 1 * second)
|
| 541 |
+
|
| 542 |
+
expr = A1*T1 + V1
|
| 543 |
+
# should not throw ValueError here
|
| 544 |
+
SI._collect_factor_and_dimension(expr)
|
| 545 |
+
|
| 546 |
+
|
| 547 |
+
def test_prefixed_property():
|
| 548 |
+
assert not meter.is_prefixed
|
| 549 |
+
assert not joule.is_prefixed
|
| 550 |
+
assert not day.is_prefixed
|
| 551 |
+
assert not second.is_prefixed
|
| 552 |
+
assert not volt.is_prefixed
|
| 553 |
+
assert not ohm.is_prefixed
|
| 554 |
+
assert centimeter.is_prefixed
|
| 555 |
+
assert kilometer.is_prefixed
|
| 556 |
+
assert kilogram.is_prefixed
|
| 557 |
+
assert pebibyte.is_prefixed
|
| 558 |
+
|
| 559 |
+
def test_physics_constant():
|
| 560 |
+
from sympy.physics.units import definitions
|
| 561 |
+
|
| 562 |
+
for name in dir(definitions):
|
| 563 |
+
quantity = getattr(definitions, name)
|
| 564 |
+
if not isinstance(quantity, Quantity):
|
| 565 |
+
continue
|
| 566 |
+
if name.endswith('_constant'):
|
| 567 |
+
assert isinstance(quantity, PhysicalConstant), f"{quantity} must be PhysicalConstant, but is {type(quantity)}"
|
| 568 |
+
assert quantity.is_physical_constant, f"{name} is not marked as physics constant when it should be"
|
| 569 |
+
|
| 570 |
+
for const in [gravitational_constant, molar_gas_constant, vacuum_permittivity, speed_of_light, elementary_charge]:
|
| 571 |
+
assert isinstance(const, PhysicalConstant), f"{const} must be PhysicalConstant, but is {type(const)}"
|
| 572 |
+
assert const.is_physical_constant, f"{const} is not marked as physics constant when it should be"
|
| 573 |
+
|
| 574 |
+
assert not meter.is_physical_constant
|
| 575 |
+
assert not joule.is_physical_constant
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_unit_system_cgs_gauss.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.concrete.tests.test_sums_products import NS
|
| 2 |
+
|
| 3 |
+
from sympy.core.singleton import S
|
| 4 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 5 |
+
from sympy.physics.units import convert_to, coulomb_constant, elementary_charge, gravitational_constant, planck
|
| 6 |
+
from sympy.physics.units.definitions.unit_definitions import angstrom, statcoulomb, coulomb, second, gram, centimeter, erg, \
|
| 7 |
+
newton, joule, dyne, speed_of_light, meter, farad, henry, statvolt, volt, ohm
|
| 8 |
+
from sympy.physics.units.systems import SI
|
| 9 |
+
from sympy.physics.units.systems.cgs import cgs_gauss
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_conversion_to_from_si():
|
| 13 |
+
assert convert_to(statcoulomb, coulomb, cgs_gauss) == coulomb/2997924580
|
| 14 |
+
assert convert_to(coulomb, statcoulomb, cgs_gauss) == 2997924580*statcoulomb
|
| 15 |
+
assert convert_to(statcoulomb, sqrt(gram*centimeter**3)/second, cgs_gauss) == centimeter**(S(3)/2)*sqrt(gram)/second
|
| 16 |
+
assert convert_to(coulomb, sqrt(gram*centimeter**3)/second, cgs_gauss) == 2997924580*centimeter**(S(3)/2)*sqrt(gram)/second
|
| 17 |
+
|
| 18 |
+
# SI units have an additional base unit, no conversion in case of electromagnetism:
|
| 19 |
+
assert convert_to(coulomb, statcoulomb, SI) == coulomb
|
| 20 |
+
assert convert_to(statcoulomb, coulomb, SI) == statcoulomb
|
| 21 |
+
|
| 22 |
+
# SI without electromagnetism:
|
| 23 |
+
assert convert_to(erg, joule, SI) == joule/10**7
|
| 24 |
+
assert convert_to(erg, joule, cgs_gauss) == joule/10**7
|
| 25 |
+
assert convert_to(joule, erg, SI) == 10**7*erg
|
| 26 |
+
assert convert_to(joule, erg, cgs_gauss) == 10**7*erg
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
assert convert_to(dyne, newton, SI) == newton/10**5
|
| 30 |
+
assert convert_to(dyne, newton, cgs_gauss) == newton/10**5
|
| 31 |
+
assert convert_to(newton, dyne, SI) == 10**5*dyne
|
| 32 |
+
assert convert_to(newton, dyne, cgs_gauss) == 10**5*dyne
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def test_cgs_gauss_convert_constants():
|
| 36 |
+
|
| 37 |
+
assert convert_to(speed_of_light, centimeter/second, cgs_gauss) == 29979245800*centimeter/second
|
| 38 |
+
|
| 39 |
+
assert convert_to(coulomb_constant, 1, cgs_gauss) == 1
|
| 40 |
+
assert convert_to(coulomb_constant, newton*meter**2/coulomb**2, cgs_gauss) == 22468879468420441*meter**2*newton/(2500000*coulomb**2)
|
| 41 |
+
assert convert_to(coulomb_constant, newton*meter**2/coulomb**2, SI) == 22468879468420441*meter**2*newton/(2500000*coulomb**2)
|
| 42 |
+
assert convert_to(coulomb_constant, dyne*centimeter**2/statcoulomb**2, cgs_gauss) == centimeter**2*dyne/statcoulomb**2
|
| 43 |
+
assert convert_to(coulomb_constant, 1, SI) == coulomb_constant
|
| 44 |
+
assert NS(convert_to(coulomb_constant, newton*meter**2/coulomb**2, SI)) == '8987551787.36818*meter**2*newton/coulomb**2'
|
| 45 |
+
|
| 46 |
+
assert convert_to(elementary_charge, statcoulomb, cgs_gauss)
|
| 47 |
+
assert convert_to(angstrom, centimeter, cgs_gauss) == 1*centimeter/10**8
|
| 48 |
+
assert convert_to(gravitational_constant, dyne*centimeter**2/gram**2, cgs_gauss)
|
| 49 |
+
assert NS(convert_to(planck, erg*second, cgs_gauss)) == '6.62607015e-27*erg*second'
|
| 50 |
+
|
| 51 |
+
spc = 25000*second/(22468879468420441*centimeter)
|
| 52 |
+
assert convert_to(ohm, second/centimeter, cgs_gauss) == spc
|
| 53 |
+
assert convert_to(henry, second**2/centimeter, cgs_gauss) == spc*second
|
| 54 |
+
assert convert_to(volt, statvolt, cgs_gauss) == 10**6*statvolt/299792458
|
| 55 |
+
assert convert_to(farad, centimeter, cgs_gauss) == 299792458**2*centimeter/10**5
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_unitsystem.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.physics.units import DimensionSystem, joule, second, ampere
|
| 2 |
+
|
| 3 |
+
from sympy.core.numbers import Rational
|
| 4 |
+
from sympy.core.singleton import S
|
| 5 |
+
from sympy.physics.units.definitions import c, kg, m, s
|
| 6 |
+
from sympy.physics.units.definitions.dimension_definitions import length, time
|
| 7 |
+
from sympy.physics.units.quantities import Quantity
|
| 8 |
+
from sympy.physics.units.unitsystem import UnitSystem
|
| 9 |
+
from sympy.physics.units.util import convert_to
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_definition():
|
| 13 |
+
# want to test if the system can have several units of the same dimension
|
| 14 |
+
dm = Quantity("dm")
|
| 15 |
+
base = (m, s)
|
| 16 |
+
# base_dim = (m.dimension, s.dimension)
|
| 17 |
+
ms = UnitSystem(base, (c, dm), "MS", "MS system")
|
| 18 |
+
ms.set_quantity_dimension(dm, length)
|
| 19 |
+
ms.set_quantity_scale_factor(dm, Rational(1, 10))
|
| 20 |
+
|
| 21 |
+
assert set(ms._base_units) == set(base)
|
| 22 |
+
assert set(ms._units) == {m, s, c, dm}
|
| 23 |
+
# assert ms._units == DimensionSystem._sort_dims(base + (velocity,))
|
| 24 |
+
assert ms.name == "MS"
|
| 25 |
+
assert ms.descr == "MS system"
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test_str_repr():
|
| 29 |
+
assert str(UnitSystem((m, s), name="MS")) == "MS"
|
| 30 |
+
assert str(UnitSystem((m, s))) == "UnitSystem((meter, second))"
|
| 31 |
+
|
| 32 |
+
assert repr(UnitSystem((m, s))) == "<UnitSystem: (%s, %s)>" % (m, s)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def test_convert_to():
|
| 36 |
+
A = Quantity("A")
|
| 37 |
+
A.set_global_relative_scale_factor(S.One, ampere)
|
| 38 |
+
|
| 39 |
+
Js = Quantity("Js")
|
| 40 |
+
Js.set_global_relative_scale_factor(S.One, joule*second)
|
| 41 |
+
|
| 42 |
+
mksa = UnitSystem((m, kg, s, A), (Js,))
|
| 43 |
+
assert convert_to(Js, mksa._base_units) == m**2*kg*s**-1/1000
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_extend():
|
| 47 |
+
ms = UnitSystem((m, s), (c,))
|
| 48 |
+
Js = Quantity("Js")
|
| 49 |
+
Js.set_global_relative_scale_factor(1, joule*second)
|
| 50 |
+
mks = ms.extend((kg,), (Js,))
|
| 51 |
+
|
| 52 |
+
res = UnitSystem((m, s, kg), (c, Js))
|
| 53 |
+
assert set(mks._base_units) == set(res._base_units)
|
| 54 |
+
assert set(mks._units) == set(res._units)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def test_dim():
|
| 58 |
+
dimsys = UnitSystem((m, kg, s), (c,))
|
| 59 |
+
assert dimsys.dim == 3
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def test_is_consistent():
|
| 63 |
+
dimension_system = DimensionSystem([length, time])
|
| 64 |
+
us = UnitSystem([m, s], dimension_system=dimension_system)
|
| 65 |
+
assert us.is_consistent == True
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_get_units_non_prefixed():
|
| 69 |
+
from sympy.physics.units import volt, ohm
|
| 70 |
+
unit_system = UnitSystem.get_unit_system("SI")
|
| 71 |
+
units = unit_system.get_units_non_prefixed()
|
| 72 |
+
for prefix in ["giga", "tera", "peta", "exa", "zetta", "yotta", "kilo", "hecto", "deca", "deci", "centi", "milli", "micro", "nano", "pico", "femto", "atto", "zepto", "yocto"]:
|
| 73 |
+
for unit in units:
|
| 74 |
+
assert isinstance(unit, Quantity), f"{unit} must be a Quantity, not {type(unit)}"
|
| 75 |
+
assert not unit.is_prefixed, f"{unit} is marked as prefixed"
|
| 76 |
+
assert not unit.is_physical_constant, f"{unit} is marked as physics constant"
|
| 77 |
+
assert not unit.name.name.startswith(prefix), f"Unit {unit.name} has prefix {prefix}"
|
| 78 |
+
assert volt in units
|
| 79 |
+
assert ohm in units
|
| 80 |
+
|
| 81 |
+
def test_derived_units_must_exist_in_unit_system():
|
| 82 |
+
for unit_system in UnitSystem._unit_systems.values():
|
| 83 |
+
for preferred_unit in unit_system.derived_units.values():
|
| 84 |
+
units = preferred_unit.atoms(Quantity)
|
| 85 |
+
for unit in units:
|
| 86 |
+
assert unit in unit_system._units, f"Unit {unit} is not in unit system {unit_system}"
|
hug/lib/python3.12/site-packages/sympy/physics/units/tests/test_util.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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import Pow
|
| 4 |
+
from sympy.core.symbol import symbols
|
| 5 |
+
from sympy.core.sympify import sympify
|
| 6 |
+
from sympy.printing.str import sstr
|
| 7 |
+
from sympy.physics.units import (
|
| 8 |
+
G, centimeter, coulomb, day, degree, gram, hbar, hour, inch, joule, kelvin,
|
| 9 |
+
kilogram, kilometer, length, meter, mile, minute, newton, planck,
|
| 10 |
+
planck_length, planck_mass, planck_temperature, planck_time, radians,
|
| 11 |
+
second, speed_of_light, steradian, time, km)
|
| 12 |
+
from sympy.physics.units.util import convert_to, check_dimensions
|
| 13 |
+
from sympy.testing.pytest import raises
|
| 14 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def NS(e, n=15, **options):
|
| 18 |
+
return sstr(sympify(e).evalf(n, **options), full_prec=True)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
L = length
|
| 22 |
+
T = time
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_dim_simplify_add():
|
| 26 |
+
# assert Add(L, L) == L
|
| 27 |
+
assert L + L == L
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_dim_simplify_mul():
|
| 31 |
+
# assert Mul(L, T) == L*T
|
| 32 |
+
assert L*T == L*T
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def test_dim_simplify_pow():
|
| 36 |
+
assert Pow(L, 2) == L**2
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def test_dim_simplify_rec():
|
| 40 |
+
# assert Mul(Add(L, L), T) == L*T
|
| 41 |
+
assert (L + L) * T == L*T
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_convert_to_quantities():
|
| 45 |
+
assert convert_to(3, meter) == 3
|
| 46 |
+
|
| 47 |
+
assert convert_to(mile, kilometer) == 25146*kilometer/15625
|
| 48 |
+
assert convert_to(meter/second, speed_of_light) == speed_of_light/299792458
|
| 49 |
+
assert convert_to(299792458*meter/second, speed_of_light) == speed_of_light
|
| 50 |
+
assert convert_to(2*299792458*meter/second, speed_of_light) == 2*speed_of_light
|
| 51 |
+
assert convert_to(speed_of_light, meter/second) == 299792458*meter/second
|
| 52 |
+
assert convert_to(2*speed_of_light, meter/second) == 599584916*meter/second
|
| 53 |
+
assert convert_to(day, second) == 86400*second
|
| 54 |
+
assert convert_to(2*hour, minute) == 120*minute
|
| 55 |
+
assert convert_to(mile, meter) == 201168*meter/125
|
| 56 |
+
assert convert_to(mile/hour, kilometer/hour) == 25146*kilometer/(15625*hour)
|
| 57 |
+
assert convert_to(3*newton, meter/second) == 3*newton
|
| 58 |
+
assert convert_to(3*newton, kilogram*meter/second**2) == 3*meter*kilogram/second**2
|
| 59 |
+
assert convert_to(kilometer + mile, meter) == 326168*meter/125
|
| 60 |
+
assert convert_to(2*kilometer + 3*mile, meter) == 853504*meter/125
|
| 61 |
+
assert convert_to(inch**2, meter**2) == 16129*meter**2/25000000
|
| 62 |
+
assert convert_to(3*inch**2, meter) == 48387*meter**2/25000000
|
| 63 |
+
assert convert_to(2*kilometer/hour + 3*mile/hour, meter/second) == 53344*meter/(28125*second)
|
| 64 |
+
assert convert_to(2*kilometer/hour + 3*mile/hour, centimeter/second) == 213376*centimeter/(1125*second)
|
| 65 |
+
assert convert_to(kilometer * (mile + kilometer), meter) == 2609344 * meter ** 2
|
| 66 |
+
|
| 67 |
+
assert convert_to(steradian, coulomb) == steradian
|
| 68 |
+
assert convert_to(radians, degree) == 180*degree/pi
|
| 69 |
+
assert convert_to(radians, [meter, degree]) == 180*degree/pi
|
| 70 |
+
assert convert_to(pi*radians, degree) == 180*degree
|
| 71 |
+
assert convert_to(pi, degree) == 180*degree
|
| 72 |
+
|
| 73 |
+
# https://github.com/sympy/sympy/issues/26263
|
| 74 |
+
assert convert_to(sqrt(meter**2 + meter**2.0), meter) == sqrt(meter**2 + meter**2.0)
|
| 75 |
+
assert convert_to((meter**2 + meter**2.0)**2, meter) == (meter**2 + meter**2.0)**2
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def test_convert_to_tuples_of_quantities():
|
| 79 |
+
from sympy.core.symbol import symbols
|
| 80 |
+
|
| 81 |
+
alpha, beta = symbols('alpha beta')
|
| 82 |
+
|
| 83 |
+
assert convert_to(speed_of_light, [meter, second]) == 299792458 * meter / second
|
| 84 |
+
assert convert_to(speed_of_light, (meter, second)) == 299792458 * meter / second
|
| 85 |
+
assert convert_to(speed_of_light, Tuple(meter, second)) == 299792458 * meter / second
|
| 86 |
+
assert convert_to(joule, [meter, kilogram, second]) == kilogram*meter**2/second**2
|
| 87 |
+
assert convert_to(joule, [centimeter, gram, second]) == 10000000*centimeter**2*gram/second**2
|
| 88 |
+
assert convert_to(299792458*meter/second, [speed_of_light]) == speed_of_light
|
| 89 |
+
assert convert_to(speed_of_light / 2, [meter, second, kilogram]) == meter/second*299792458 / 2
|
| 90 |
+
# This doesn't make physically sense, but let's keep it as a conversion test:
|
| 91 |
+
assert convert_to(2 * speed_of_light, [meter, second, kilogram]) == 2 * 299792458 * meter / second
|
| 92 |
+
assert convert_to(G, [G, speed_of_light, planck]) == 1.0*G
|
| 93 |
+
|
| 94 |
+
assert NS(convert_to(meter, [G, speed_of_light, hbar]), n=7) == '6.187142e+34*gravitational_constant**0.5000000*hbar**0.5000000/speed_of_light**1.500000'
|
| 95 |
+
assert NS(convert_to(planck_mass, kilogram), n=7) == '2.176434e-8*kilogram'
|
| 96 |
+
assert NS(convert_to(planck_length, meter), n=7) == '1.616255e-35*meter'
|
| 97 |
+
assert NS(convert_to(planck_time, second), n=6) == '5.39125e-44*second'
|
| 98 |
+
assert NS(convert_to(planck_temperature, kelvin), n=7) == '1.416784e+32*kelvin'
|
| 99 |
+
assert NS(convert_to(convert_to(meter, [G, speed_of_light, planck]), meter), n=10) == '1.000000000*meter'
|
| 100 |
+
|
| 101 |
+
# similar to https://github.com/sympy/sympy/issues/26263
|
| 102 |
+
assert convert_to(sqrt(meter**2 + second**2.0), [meter, second]) == sqrt(meter**2 + second**2.0)
|
| 103 |
+
assert convert_to((meter**2 + second**2.0)**2, [meter, second]) == (meter**2 + second**2.0)**2
|
| 104 |
+
|
| 105 |
+
# similar to https://github.com/sympy/sympy/issues/21463
|
| 106 |
+
assert convert_to(1/(beta*meter + meter), 1/meter) == 1/(beta*meter + meter)
|
| 107 |
+
assert convert_to(1/(beta*meter + alpha*meter), 1/kilometer) == (1/(kilometer*beta/1000 + alpha*kilometer/1000))
|
| 108 |
+
|
| 109 |
+
def test_eval_simplify():
|
| 110 |
+
from sympy.physics.units import cm, mm, km, m, K, kilo
|
| 111 |
+
from sympy.core.symbol import symbols
|
| 112 |
+
|
| 113 |
+
x, y = symbols('x y')
|
| 114 |
+
|
| 115 |
+
assert (cm/mm).simplify() == 10
|
| 116 |
+
assert (km/m).simplify() == 1000
|
| 117 |
+
assert (km/cm).simplify() == 100000
|
| 118 |
+
assert (10*x*K*km**2/m/cm).simplify() == 1000000000*x*kelvin
|
| 119 |
+
assert (cm/km/m).simplify() == 1/(10000000*centimeter)
|
| 120 |
+
|
| 121 |
+
assert (3*kilo*meter).simplify() == 3000*meter
|
| 122 |
+
assert (4*kilo*meter/(2*kilometer)).simplify() == 2
|
| 123 |
+
assert (4*kilometer**2/(kilo*meter)**2).simplify() == 4
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def test_quantity_simplify():
|
| 127 |
+
from sympy.physics.units.util import quantity_simplify
|
| 128 |
+
from sympy.physics.units import kilo, foot
|
| 129 |
+
from sympy.core.symbol import symbols
|
| 130 |
+
|
| 131 |
+
x, y = symbols('x y')
|
| 132 |
+
|
| 133 |
+
assert quantity_simplify(x*(8*kilo*newton*meter + y)) == x*(8000*meter*newton + y)
|
| 134 |
+
assert quantity_simplify(foot*inch*(foot + inch)) == foot**2*(foot + foot/12)/12
|
| 135 |
+
assert quantity_simplify(foot*inch*(foot*foot + inch*(foot + inch))) == foot**2*(foot**2 + foot/12*(foot + foot/12))/12
|
| 136 |
+
assert quantity_simplify(2**(foot/inch*kilo/1000)*inch) == 4096*foot/12
|
| 137 |
+
assert quantity_simplify(foot**2*inch + inch**2*foot) == 13*foot**3/144
|
| 138 |
+
|
| 139 |
+
def test_quantity_simplify_across_dimensions():
|
| 140 |
+
from sympy.physics.units.util import quantity_simplify
|
| 141 |
+
from sympy.physics.units import ampere, ohm, volt, joule, pascal, farad, second, watt, siemens, henry, tesla, weber, hour, newton
|
| 142 |
+
|
| 143 |
+
assert quantity_simplify(ampere*ohm, across_dimensions=True, unit_system="SI") == volt
|
| 144 |
+
assert quantity_simplify(6*ampere*ohm, across_dimensions=True, unit_system="SI") == 6*volt
|
| 145 |
+
assert quantity_simplify(volt/ampere, across_dimensions=True, unit_system="SI") == ohm
|
| 146 |
+
assert quantity_simplify(volt/ohm, across_dimensions=True, unit_system="SI") == ampere
|
| 147 |
+
assert quantity_simplify(joule/meter**3, across_dimensions=True, unit_system="SI") == pascal
|
| 148 |
+
assert quantity_simplify(farad*ohm, across_dimensions=True, unit_system="SI") == second
|
| 149 |
+
assert quantity_simplify(joule/second, across_dimensions=True, unit_system="SI") == watt
|
| 150 |
+
assert quantity_simplify(meter**3/second, across_dimensions=True, unit_system="SI") == meter**3/second
|
| 151 |
+
assert quantity_simplify(joule/second, across_dimensions=True, unit_system="SI") == watt
|
| 152 |
+
|
| 153 |
+
assert quantity_simplify(joule/coulomb, across_dimensions=True, unit_system="SI") == volt
|
| 154 |
+
assert quantity_simplify(volt/ampere, across_dimensions=True, unit_system="SI") == ohm
|
| 155 |
+
assert quantity_simplify(ampere/volt, across_dimensions=True, unit_system="SI") == siemens
|
| 156 |
+
assert quantity_simplify(coulomb/volt, across_dimensions=True, unit_system="SI") == farad
|
| 157 |
+
assert quantity_simplify(volt*second/ampere, across_dimensions=True, unit_system="SI") == henry
|
| 158 |
+
assert quantity_simplify(volt*second/meter**2, across_dimensions=True, unit_system="SI") == tesla
|
| 159 |
+
assert quantity_simplify(joule/ampere, across_dimensions=True, unit_system="SI") == weber
|
| 160 |
+
|
| 161 |
+
assert quantity_simplify(5*kilometer/hour, across_dimensions=True, unit_system="SI") == 25*meter/(18*second)
|
| 162 |
+
assert quantity_simplify(5*kilogram*meter/second**2, across_dimensions=True, unit_system="SI") == 5*newton
|
| 163 |
+
|
| 164 |
+
def test_check_dimensions():
|
| 165 |
+
x = symbols('x')
|
| 166 |
+
assert check_dimensions(inch + x) == inch + x
|
| 167 |
+
assert check_dimensions(length + x) == length + x
|
| 168 |
+
# after subs we get 2*length; check will clear the constant
|
| 169 |
+
assert check_dimensions((length + x).subs(x, length)) == length
|
| 170 |
+
assert check_dimensions(newton*meter + joule) == joule + meter*newton
|
| 171 |
+
raises(ValueError, lambda: check_dimensions(inch + 1))
|
| 172 |
+
raises(ValueError, lambda: check_dimensions(length + 1))
|
| 173 |
+
raises(ValueError, lambda: check_dimensions(length + time))
|
| 174 |
+
raises(ValueError, lambda: check_dimensions(meter + second))
|
| 175 |
+
raises(ValueError, lambda: check_dimensions(2 * meter + second))
|
| 176 |
+
raises(ValueError, lambda: check_dimensions(2 * meter + 3 * second))
|
| 177 |
+
raises(ValueError, lambda: check_dimensions(1 / second + 1 / meter))
|
| 178 |
+
raises(ValueError, lambda: check_dimensions(2 * meter*(mile + centimeter) + km))
|
hug/lib/python3.12/site-packages/sympy/physics/units/unitsystem.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Unit system for physical quantities; include definition of constants.
|
| 3 |
+
"""
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
from sympy.core.add import Add
|
| 7 |
+
from sympy.core.function import (Derivative, Function)
|
| 8 |
+
from sympy.core.mul import Mul
|
| 9 |
+
from sympy.core.power import Pow
|
| 10 |
+
from sympy.core.singleton import S
|
| 11 |
+
from sympy.physics.units.dimensions import _QuantityMapper
|
| 12 |
+
from sympy.physics.units.quantities import Quantity
|
| 13 |
+
|
| 14 |
+
from .dimensions import Dimension
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class UnitSystem(_QuantityMapper):
|
| 18 |
+
"""
|
| 19 |
+
UnitSystem represents a coherent set of units.
|
| 20 |
+
|
| 21 |
+
A unit system is basically a dimension system with notions of scales. Many
|
| 22 |
+
of the methods are defined in the same way.
|
| 23 |
+
|
| 24 |
+
It is much better if all base units have a symbol.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
_unit_systems: dict[str, UnitSystem] = {}
|
| 28 |
+
|
| 29 |
+
def __init__(self, base_units, units=(), name="", descr="", dimension_system=None, derived_units: dict[Dimension, Quantity]={}):
|
| 30 |
+
|
| 31 |
+
UnitSystem._unit_systems[name] = self
|
| 32 |
+
|
| 33 |
+
self.name = name
|
| 34 |
+
self.descr = descr
|
| 35 |
+
|
| 36 |
+
self._base_units = base_units
|
| 37 |
+
self._dimension_system = dimension_system
|
| 38 |
+
self._units = tuple(set(base_units) | set(units))
|
| 39 |
+
self._base_units = tuple(base_units)
|
| 40 |
+
self._derived_units = derived_units
|
| 41 |
+
|
| 42 |
+
super().__init__()
|
| 43 |
+
|
| 44 |
+
def __str__(self):
|
| 45 |
+
"""
|
| 46 |
+
Return the name of the system.
|
| 47 |
+
|
| 48 |
+
If it does not exist, then it makes a list of symbols (or names) of
|
| 49 |
+
the base dimensions.
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
if self.name != "":
|
| 53 |
+
return self.name
|
| 54 |
+
else:
|
| 55 |
+
return "UnitSystem((%s))" % ", ".join(
|
| 56 |
+
str(d) for d in self._base_units)
|
| 57 |
+
|
| 58 |
+
def __repr__(self):
|
| 59 |
+
return '<UnitSystem: %s>' % repr(self._base_units)
|
| 60 |
+
|
| 61 |
+
def extend(self, base, units=(), name="", description="", dimension_system=None, derived_units: dict[Dimension, Quantity]={}):
|
| 62 |
+
"""Extend the current system into a new one.
|
| 63 |
+
|
| 64 |
+
Take the base and normal units of the current system to merge
|
| 65 |
+
them to the base and normal units given in argument.
|
| 66 |
+
If not provided, name and description are overridden by empty strings.
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
base = self._base_units + tuple(base)
|
| 70 |
+
units = self._units + tuple(units)
|
| 71 |
+
|
| 72 |
+
return UnitSystem(base, units, name, description, dimension_system, {**self._derived_units, **derived_units})
|
| 73 |
+
|
| 74 |
+
def get_dimension_system(self):
|
| 75 |
+
return self._dimension_system
|
| 76 |
+
|
| 77 |
+
def get_quantity_dimension(self, unit):
|
| 78 |
+
qdm = self.get_dimension_system()._quantity_dimension_map
|
| 79 |
+
if unit in qdm:
|
| 80 |
+
return qdm[unit]
|
| 81 |
+
return super().get_quantity_dimension(unit)
|
| 82 |
+
|
| 83 |
+
def get_quantity_scale_factor(self, unit):
|
| 84 |
+
qsfm = self.get_dimension_system()._quantity_scale_factors
|
| 85 |
+
if unit in qsfm:
|
| 86 |
+
return qsfm[unit]
|
| 87 |
+
return super().get_quantity_scale_factor(unit)
|
| 88 |
+
|
| 89 |
+
@staticmethod
|
| 90 |
+
def get_unit_system(unit_system):
|
| 91 |
+
if isinstance(unit_system, UnitSystem):
|
| 92 |
+
return unit_system
|
| 93 |
+
|
| 94 |
+
if unit_system not in UnitSystem._unit_systems:
|
| 95 |
+
raise ValueError(
|
| 96 |
+
"Unit system is not supported. Currently"
|
| 97 |
+
"supported unit systems are {}".format(
|
| 98 |
+
", ".join(sorted(UnitSystem._unit_systems))
|
| 99 |
+
)
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
return UnitSystem._unit_systems[unit_system]
|
| 103 |
+
|
| 104 |
+
@staticmethod
|
| 105 |
+
def get_default_unit_system():
|
| 106 |
+
return UnitSystem._unit_systems["SI"]
|
| 107 |
+
|
| 108 |
+
@property
|
| 109 |
+
def dim(self):
|
| 110 |
+
"""
|
| 111 |
+
Give the dimension of the system.
|
| 112 |
+
|
| 113 |
+
That is return the number of units forming the basis.
|
| 114 |
+
"""
|
| 115 |
+
return len(self._base_units)
|
| 116 |
+
|
| 117 |
+
@property
|
| 118 |
+
def is_consistent(self):
|
| 119 |
+
"""
|
| 120 |
+
Check if the underlying dimension system is consistent.
|
| 121 |
+
"""
|
| 122 |
+
# test is performed in DimensionSystem
|
| 123 |
+
return self.get_dimension_system().is_consistent
|
| 124 |
+
|
| 125 |
+
@property
|
| 126 |
+
def derived_units(self) -> dict[Dimension, Quantity]:
|
| 127 |
+
return self._derived_units
|
| 128 |
+
|
| 129 |
+
def get_dimensional_expr(self, expr):
|
| 130 |
+
from sympy.physics.units import Quantity
|
| 131 |
+
if isinstance(expr, Mul):
|
| 132 |
+
return Mul(*[self.get_dimensional_expr(i) for i in expr.args])
|
| 133 |
+
elif isinstance(expr, Pow):
|
| 134 |
+
return self.get_dimensional_expr(expr.base) ** expr.exp
|
| 135 |
+
elif isinstance(expr, Add):
|
| 136 |
+
return self.get_dimensional_expr(expr.args[0])
|
| 137 |
+
elif isinstance(expr, Derivative):
|
| 138 |
+
dim = self.get_dimensional_expr(expr.expr)
|
| 139 |
+
for independent, count in expr.variable_count:
|
| 140 |
+
dim /= self.get_dimensional_expr(independent)**count
|
| 141 |
+
return dim
|
| 142 |
+
elif isinstance(expr, Function):
|
| 143 |
+
args = [self.get_dimensional_expr(arg) for arg in expr.args]
|
| 144 |
+
if all(i == 1 for i in args):
|
| 145 |
+
return S.One
|
| 146 |
+
return expr.func(*args)
|
| 147 |
+
elif isinstance(expr, Quantity):
|
| 148 |
+
return self.get_quantity_dimension(expr).name
|
| 149 |
+
return S.One
|
| 150 |
+
|
| 151 |
+
def _collect_factor_and_dimension(self, expr):
|
| 152 |
+
"""
|
| 153 |
+
Return tuple with scale factor expression and dimension expression.
|
| 154 |
+
"""
|
| 155 |
+
from sympy.physics.units import Quantity
|
| 156 |
+
if isinstance(expr, Quantity):
|
| 157 |
+
return expr.scale_factor, expr.dimension
|
| 158 |
+
elif isinstance(expr, Mul):
|
| 159 |
+
factor = 1
|
| 160 |
+
dimension = Dimension(1)
|
| 161 |
+
for arg in expr.args:
|
| 162 |
+
arg_factor, arg_dim = self._collect_factor_and_dimension(arg)
|
| 163 |
+
factor *= arg_factor
|
| 164 |
+
dimension *= arg_dim
|
| 165 |
+
return factor, dimension
|
| 166 |
+
elif isinstance(expr, Pow):
|
| 167 |
+
factor, dim = self._collect_factor_and_dimension(expr.base)
|
| 168 |
+
exp_factor, exp_dim = self._collect_factor_and_dimension(expr.exp)
|
| 169 |
+
if self.get_dimension_system().is_dimensionless(exp_dim):
|
| 170 |
+
exp_dim = 1
|
| 171 |
+
return factor ** exp_factor, dim ** (exp_factor * exp_dim)
|
| 172 |
+
elif isinstance(expr, Add):
|
| 173 |
+
factor, dim = self._collect_factor_and_dimension(expr.args[0])
|
| 174 |
+
for addend in expr.args[1:]:
|
| 175 |
+
addend_factor, addend_dim = \
|
| 176 |
+
self._collect_factor_and_dimension(addend)
|
| 177 |
+
if not self.get_dimension_system().equivalent_dims(dim, addend_dim):
|
| 178 |
+
raise ValueError(
|
| 179 |
+
'Dimension of "{}" is {}, '
|
| 180 |
+
'but it should be {}'.format(
|
| 181 |
+
addend, addend_dim, dim))
|
| 182 |
+
factor += addend_factor
|
| 183 |
+
return factor, dim
|
| 184 |
+
elif isinstance(expr, Derivative):
|
| 185 |
+
factor, dim = self._collect_factor_and_dimension(expr.args[0])
|
| 186 |
+
for independent, count in expr.variable_count:
|
| 187 |
+
ifactor, idim = self._collect_factor_and_dimension(independent)
|
| 188 |
+
factor /= ifactor**count
|
| 189 |
+
dim /= idim**count
|
| 190 |
+
return factor, dim
|
| 191 |
+
elif isinstance(expr, Function):
|
| 192 |
+
fds = [self._collect_factor_and_dimension(arg) for arg in expr.args]
|
| 193 |
+
dims = [Dimension(1) if self.get_dimension_system().is_dimensionless(d[1]) else d[1] for d in fds]
|
| 194 |
+
return (expr.func(*(f[0] for f in fds)), *dims)
|
| 195 |
+
elif isinstance(expr, Dimension):
|
| 196 |
+
return S.One, expr
|
| 197 |
+
else:
|
| 198 |
+
return expr, Dimension(1)
|
| 199 |
+
|
| 200 |
+
def get_units_non_prefixed(self) -> set[Quantity]:
|
| 201 |
+
"""
|
| 202 |
+
Return the units of the system that do not have a prefix.
|
| 203 |
+
"""
|
| 204 |
+
return set(filter(lambda u: not u.is_prefixed and not u.is_physical_constant, self._units))
|
hug/lib/python3.12/site-packages/sympy/physics/units/util.py
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
+
from typing import Optional
|
| 7 |
+
|
| 8 |
+
from sympy import default_sort_key
|
| 9 |
+
from sympy.core.add import Add
|
| 10 |
+
from sympy.core.containers import Tuple
|
| 11 |
+
from sympy.core.mul import Mul
|
| 12 |
+
from sympy.core.power import Pow
|
| 13 |
+
from sympy.core.sorting import ordered
|
| 14 |
+
from sympy.core.sympify import sympify
|
| 15 |
+
from sympy.core.function import Function
|
| 16 |
+
from sympy.matrices.exceptions import NonInvertibleMatrixError
|
| 17 |
+
from sympy.physics.units.dimensions import Dimension, DimensionSystem
|
| 18 |
+
from sympy.physics.units.prefixes import Prefix
|
| 19 |
+
from sympy.physics.units.quantities import Quantity
|
| 20 |
+
from sympy.physics.units.unitsystem import UnitSystem
|
| 21 |
+
from sympy.utilities.iterables import sift
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _get_conversion_matrix_for_expr(expr, target_units, unit_system):
|
| 25 |
+
from sympy.matrices.dense import Matrix
|
| 26 |
+
|
| 27 |
+
dimension_system = unit_system.get_dimension_system()
|
| 28 |
+
|
| 29 |
+
expr_dim = Dimension(unit_system.get_dimensional_expr(expr))
|
| 30 |
+
dim_dependencies = dimension_system.get_dimensional_dependencies(expr_dim, mark_dimensionless=True)
|
| 31 |
+
target_dims = [Dimension(unit_system.get_dimensional_expr(x)) for x in target_units]
|
| 32 |
+
canon_dim_units = [i for x in target_dims for i in dimension_system.get_dimensional_dependencies(x, mark_dimensionless=True)]
|
| 33 |
+
canon_expr_units = set(dim_dependencies)
|
| 34 |
+
|
| 35 |
+
if not canon_expr_units.issubset(set(canon_dim_units)):
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
seen = set()
|
| 39 |
+
canon_dim_units = [i for i in canon_dim_units if not (i in seen or seen.add(i))]
|
| 40 |
+
|
| 41 |
+
camat = Matrix([[dimension_system.get_dimensional_dependencies(i, mark_dimensionless=True).get(j, 0) for i in target_dims] for j in canon_dim_units])
|
| 42 |
+
exprmat = Matrix([dim_dependencies.get(k, 0) for k in canon_dim_units])
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
res_exponents = camat.solve(exprmat)
|
| 46 |
+
except NonInvertibleMatrixError:
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
return res_exponents
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def convert_to(expr, target_units, unit_system="SI"):
|
| 53 |
+
"""
|
| 54 |
+
Convert ``expr`` to the same expression with all of its units and quantities
|
| 55 |
+
represented as factors of ``target_units``, whenever the dimension is compatible.
|
| 56 |
+
|
| 57 |
+
``target_units`` may be a single unit/quantity, or a collection of
|
| 58 |
+
units/quantities.
|
| 59 |
+
|
| 60 |
+
Examples
|
| 61 |
+
========
|
| 62 |
+
|
| 63 |
+
>>> from sympy.physics.units import speed_of_light, meter, gram, second, day
|
| 64 |
+
>>> from sympy.physics.units import mile, newton, kilogram, atomic_mass_constant
|
| 65 |
+
>>> from sympy.physics.units import kilometer, centimeter
|
| 66 |
+
>>> from sympy.physics.units import gravitational_constant, hbar
|
| 67 |
+
>>> from sympy.physics.units import convert_to
|
| 68 |
+
>>> convert_to(mile, kilometer)
|
| 69 |
+
25146*kilometer/15625
|
| 70 |
+
>>> convert_to(mile, kilometer).n()
|
| 71 |
+
1.609344*kilometer
|
| 72 |
+
>>> convert_to(speed_of_light, meter/second)
|
| 73 |
+
299792458*meter/second
|
| 74 |
+
>>> convert_to(day, second)
|
| 75 |
+
86400*second
|
| 76 |
+
>>> 3*newton
|
| 77 |
+
3*newton
|
| 78 |
+
>>> convert_to(3*newton, kilogram*meter/second**2)
|
| 79 |
+
3*kilogram*meter/second**2
|
| 80 |
+
>>> convert_to(atomic_mass_constant, gram)
|
| 81 |
+
1.660539060e-24*gram
|
| 82 |
+
|
| 83 |
+
Conversion to multiple units:
|
| 84 |
+
|
| 85 |
+
>>> convert_to(speed_of_light, [meter, second])
|
| 86 |
+
299792458*meter/second
|
| 87 |
+
>>> convert_to(3*newton, [centimeter, gram, second])
|
| 88 |
+
300000*centimeter*gram/second**2
|
| 89 |
+
|
| 90 |
+
Conversion to Planck units:
|
| 91 |
+
|
| 92 |
+
>>> convert_to(atomic_mass_constant, [gravitational_constant, speed_of_light, hbar]).n()
|
| 93 |
+
7.62963087839509e-20*hbar**0.5*speed_of_light**0.5/gravitational_constant**0.5
|
| 94 |
+
|
| 95 |
+
"""
|
| 96 |
+
from sympy.physics.units import UnitSystem
|
| 97 |
+
unit_system = UnitSystem.get_unit_system(unit_system)
|
| 98 |
+
|
| 99 |
+
if not isinstance(target_units, (Iterable, Tuple)):
|
| 100 |
+
target_units = [target_units]
|
| 101 |
+
|
| 102 |
+
def handle_Adds(expr):
|
| 103 |
+
return Add.fromiter(convert_to(i, target_units, unit_system)
|
| 104 |
+
for i in expr.args)
|
| 105 |
+
|
| 106 |
+
if isinstance(expr, Add):
|
| 107 |
+
return handle_Adds(expr)
|
| 108 |
+
elif isinstance(expr, Pow) and isinstance(expr.base, Add):
|
| 109 |
+
return handle_Adds(expr.base) ** expr.exp
|
| 110 |
+
|
| 111 |
+
expr = sympify(expr)
|
| 112 |
+
target_units = sympify(target_units)
|
| 113 |
+
|
| 114 |
+
if isinstance(expr, Function):
|
| 115 |
+
expr = expr.together()
|
| 116 |
+
|
| 117 |
+
if not isinstance(expr, Quantity) and expr.has(Quantity):
|
| 118 |
+
expr = expr.replace(lambda x: isinstance(x, Quantity),
|
| 119 |
+
lambda x: x.convert_to(target_units, unit_system))
|
| 120 |
+
|
| 121 |
+
def get_total_scale_factor(expr):
|
| 122 |
+
if isinstance(expr, Mul):
|
| 123 |
+
return reduce(lambda x, y: x * y,
|
| 124 |
+
[get_total_scale_factor(i) for i in expr.args])
|
| 125 |
+
elif isinstance(expr, Pow):
|
| 126 |
+
return get_total_scale_factor(expr.base) ** expr.exp
|
| 127 |
+
elif isinstance(expr, Quantity):
|
| 128 |
+
return unit_system.get_quantity_scale_factor(expr)
|
| 129 |
+
return expr
|
| 130 |
+
|
| 131 |
+
depmat = _get_conversion_matrix_for_expr(expr, target_units, unit_system)
|
| 132 |
+
if depmat is None:
|
| 133 |
+
return expr
|
| 134 |
+
|
| 135 |
+
expr_scale_factor = get_total_scale_factor(expr)
|
| 136 |
+
return expr_scale_factor * Mul.fromiter(
|
| 137 |
+
(1/get_total_scale_factor(u)*u)**p for u, p in
|
| 138 |
+
zip(target_units, depmat))
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def quantity_simplify(expr, across_dimensions: bool=False, unit_system=None):
|
| 142 |
+
"""Return an equivalent expression in which prefixes are replaced
|
| 143 |
+
with numerical values and all units of a given dimension are the
|
| 144 |
+
unified in a canonical manner by default. `across_dimensions` allows
|
| 145 |
+
for units of different dimensions to be simplified together.
|
| 146 |
+
|
| 147 |
+
`unit_system` must be specified if `across_dimensions` is True.
|
| 148 |
+
|
| 149 |
+
Examples
|
| 150 |
+
========
|
| 151 |
+
|
| 152 |
+
>>> from sympy.physics.units.util import quantity_simplify
|
| 153 |
+
>>> from sympy.physics.units.prefixes import kilo
|
| 154 |
+
>>> from sympy.physics.units import foot, inch, joule, coulomb
|
| 155 |
+
>>> quantity_simplify(kilo*foot*inch)
|
| 156 |
+
250*foot**2/3
|
| 157 |
+
>>> quantity_simplify(foot - 6*inch)
|
| 158 |
+
foot/2
|
| 159 |
+
>>> quantity_simplify(5*joule/coulomb, across_dimensions=True, unit_system="SI")
|
| 160 |
+
5*volt
|
| 161 |
+
"""
|
| 162 |
+
|
| 163 |
+
if expr.is_Atom or not expr.has(Prefix, Quantity):
|
| 164 |
+
return expr
|
| 165 |
+
|
| 166 |
+
# replace all prefixes with numerical values
|
| 167 |
+
p = expr.atoms(Prefix)
|
| 168 |
+
expr = expr.xreplace({p: p.scale_factor for p in p})
|
| 169 |
+
|
| 170 |
+
# replace all quantities of given dimension with a canonical
|
| 171 |
+
# quantity, chosen from those in the expression
|
| 172 |
+
d = sift(expr.atoms(Quantity), lambda i: i.dimension)
|
| 173 |
+
for k in d:
|
| 174 |
+
if len(d[k]) == 1:
|
| 175 |
+
continue
|
| 176 |
+
v = list(ordered(d[k]))
|
| 177 |
+
ref = v[0]/v[0].scale_factor
|
| 178 |
+
expr = expr.xreplace({vi: ref*vi.scale_factor for vi in v[1:]})
|
| 179 |
+
|
| 180 |
+
if across_dimensions:
|
| 181 |
+
# combine quantities of different dimensions into a single
|
| 182 |
+
# quantity that is equivalent to the original expression
|
| 183 |
+
|
| 184 |
+
if unit_system is None:
|
| 185 |
+
raise ValueError("unit_system must be specified if across_dimensions is True")
|
| 186 |
+
|
| 187 |
+
unit_system = UnitSystem.get_unit_system(unit_system)
|
| 188 |
+
dimension_system: DimensionSystem = unit_system.get_dimension_system()
|
| 189 |
+
dim_expr = unit_system.get_dimensional_expr(expr)
|
| 190 |
+
dim_deps = dimension_system.get_dimensional_dependencies(dim_expr, mark_dimensionless=True)
|
| 191 |
+
|
| 192 |
+
target_dimension: Optional[Dimension] = None
|
| 193 |
+
for ds_dim, ds_dim_deps in dimension_system.dimensional_dependencies.items():
|
| 194 |
+
if ds_dim_deps == dim_deps:
|
| 195 |
+
target_dimension = ds_dim
|
| 196 |
+
break
|
| 197 |
+
|
| 198 |
+
if target_dimension is None:
|
| 199 |
+
# if we can't find a target dimension, we can't do anything. unsure how to handle this case.
|
| 200 |
+
return expr
|
| 201 |
+
|
| 202 |
+
target_unit = unit_system.derived_units.get(target_dimension)
|
| 203 |
+
if target_unit:
|
| 204 |
+
expr = convert_to(expr, target_unit, unit_system)
|
| 205 |
+
|
| 206 |
+
return expr
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def check_dimensions(expr, unit_system="SI"):
|
| 210 |
+
"""Return expr if units in addends have the same
|
| 211 |
+
base dimensions, else raise a ValueError."""
|
| 212 |
+
# the case of adding a number to a dimensional quantity
|
| 213 |
+
# is ignored for the sake of SymPy core routines, so this
|
| 214 |
+
# function will raise an error now if such an addend is
|
| 215 |
+
# found.
|
| 216 |
+
# Also, when doing substitutions, multiplicative constants
|
| 217 |
+
# might be introduced, so remove those now
|
| 218 |
+
|
| 219 |
+
from sympy.physics.units import UnitSystem
|
| 220 |
+
unit_system = UnitSystem.get_unit_system(unit_system)
|
| 221 |
+
|
| 222 |
+
def addDict(dict1, dict2):
|
| 223 |
+
"""Merge dictionaries by adding values of common keys and
|
| 224 |
+
removing keys with value of 0."""
|
| 225 |
+
dict3 = {**dict1, **dict2}
|
| 226 |
+
for key, value in dict3.items():
|
| 227 |
+
if key in dict1 and key in dict2:
|
| 228 |
+
dict3[key] = value + dict1[key]
|
| 229 |
+
return {key:val for key, val in dict3.items() if val != 0}
|
| 230 |
+
|
| 231 |
+
adds = expr.atoms(Add)
|
| 232 |
+
DIM_OF = unit_system.get_dimension_system().get_dimensional_dependencies
|
| 233 |
+
for a in adds:
|
| 234 |
+
deset = set()
|
| 235 |
+
for ai in a.args:
|
| 236 |
+
if ai.is_number:
|
| 237 |
+
deset.add(())
|
| 238 |
+
continue
|
| 239 |
+
dims = []
|
| 240 |
+
skip = False
|
| 241 |
+
dimdict = {}
|
| 242 |
+
for i in Mul.make_args(ai):
|
| 243 |
+
if i.has(Quantity):
|
| 244 |
+
i = Dimension(unit_system.get_dimensional_expr(i))
|
| 245 |
+
if i.has(Dimension):
|
| 246 |
+
dimdict = addDict(dimdict, DIM_OF(i))
|
| 247 |
+
elif i.free_symbols:
|
| 248 |
+
skip = True
|
| 249 |
+
break
|
| 250 |
+
dims.extend(dimdict.items())
|
| 251 |
+
if not skip:
|
| 252 |
+
deset.add(tuple(sorted(dims, key=default_sort_key)))
|
| 253 |
+
if len(deset) > 1:
|
| 254 |
+
raise ValueError(
|
| 255 |
+
"addends have incompatible dimensions: {}".format(deset))
|
| 256 |
+
|
| 257 |
+
# clear multiplicative constants on Dimensions which may be
|
| 258 |
+
# left after substitution
|
| 259 |
+
reps = {}
|
| 260 |
+
for m in expr.atoms(Mul):
|
| 261 |
+
if any(isinstance(i, Dimension) for i in m.args):
|
| 262 |
+
reps[m] = m.func(*[
|
| 263 |
+
i for i in m.args if not i.is_number])
|
| 264 |
+
|
| 265 |
+
return expr.xreplace(reps)
|
hug/lib/python3.12/site-packages/sympy/physics/vector/__init__.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__all__ = [
|
| 2 |
+
'CoordinateSym', 'ReferenceFrame',
|
| 3 |
+
|
| 4 |
+
'Dyadic',
|
| 5 |
+
|
| 6 |
+
'Vector',
|
| 7 |
+
|
| 8 |
+
'Point',
|
| 9 |
+
|
| 10 |
+
'cross', 'dot', 'express', 'time_derivative', 'outer',
|
| 11 |
+
'kinematic_equations', 'get_motion_params', 'partial_velocity',
|
| 12 |
+
'dynamicsymbols',
|
| 13 |
+
|
| 14 |
+
'vprint', 'vsstrrepr', 'vsprint', 'vpprint', 'vlatex', 'init_vprinting',
|
| 15 |
+
|
| 16 |
+
'curl', 'divergence', 'gradient', 'is_conservative', 'is_solenoidal',
|
| 17 |
+
'scalar_potential', 'scalar_potential_difference',
|
| 18 |
+
|
| 19 |
+
]
|
| 20 |
+
from .frame import CoordinateSym, ReferenceFrame
|
| 21 |
+
|
| 22 |
+
from .dyadic import Dyadic
|
| 23 |
+
|
| 24 |
+
from .vector import Vector
|
| 25 |
+
|
| 26 |
+
from .point import Point
|
| 27 |
+
|
| 28 |
+
from .functions import (cross, dot, express, time_derivative, outer,
|
| 29 |
+
kinematic_equations, get_motion_params, partial_velocity,
|
| 30 |
+
dynamicsymbols)
|
| 31 |
+
|
| 32 |
+
from .printing import (vprint, vsstrrepr, vsprint, vpprint, vlatex,
|
| 33 |
+
init_vprinting)
|
| 34 |
+
|
| 35 |
+
from .fieldfunctions import (curl, divergence, gradient, is_conservative,
|
| 36 |
+
is_solenoidal, scalar_potential, scalar_potential_difference)
|
hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (1.13 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/dyadic.cpython-312.pyc
ADDED
|
Binary file (22.1 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/fieldfunctions.cpython-312.pyc
ADDED
|
Binary file (10.1 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/frame.cpython-312.pyc
ADDED
|
Binary file (63.5 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/functions.cpython-312.pyc
ADDED
|
Binary file (27.7 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/point.cpython-312.pyc
ADDED
|
Binary file (23.5 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/printing.cpython-312.pyc
ADDED
|
Binary file (14.6 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/__pycache__/vector.cpython-312.pyc
ADDED
|
Binary file (32.2 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/dyadic.py
ADDED
|
@@ -0,0 +1,545 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 4 |
+
|
| 5 |
+
from mpmath.libmp.libmpf import prec_to_dps
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
__all__ = ['Dyadic']
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class Dyadic(Printable, EvalfMixin):
|
| 12 |
+
"""A Dyadic object.
|
| 13 |
+
|
| 14 |
+
See:
|
| 15 |
+
https://en.wikipedia.org/wiki/Dyadic_tensor
|
| 16 |
+
Kane, T., Levinson, D. Dynamics Theory and Applications. 1985 McGraw-Hill
|
| 17 |
+
|
| 18 |
+
A more powerful way to represent a rigid body's inertia. While it is more
|
| 19 |
+
complex, by choosing Dyadic components to be in body fixed basis vectors,
|
| 20 |
+
the resulting matrix is equivalent to the inertia tensor.
|
| 21 |
+
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
is_number = False
|
| 25 |
+
|
| 26 |
+
def __init__(self, inlist):
|
| 27 |
+
"""
|
| 28 |
+
Just like Vector's init, you should not call this unless creating a
|
| 29 |
+
zero dyadic.
|
| 30 |
+
|
| 31 |
+
zd = Dyadic(0)
|
| 32 |
+
|
| 33 |
+
Stores a Dyadic as a list of lists; the inner list has the measure
|
| 34 |
+
number and the two unit vectors; the outerlist holds each unique
|
| 35 |
+
unit vector pair.
|
| 36 |
+
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
self.args = []
|
| 40 |
+
if inlist == 0:
|
| 41 |
+
inlist = []
|
| 42 |
+
while len(inlist) != 0:
|
| 43 |
+
added = 0
|
| 44 |
+
for i, v in enumerate(self.args):
|
| 45 |
+
if ((str(inlist[0][1]) == str(self.args[i][1])) and
|
| 46 |
+
(str(inlist[0][2]) == str(self.args[i][2]))):
|
| 47 |
+
self.args[i] = (self.args[i][0] + inlist[0][0],
|
| 48 |
+
inlist[0][1], inlist[0][2])
|
| 49 |
+
inlist.remove(inlist[0])
|
| 50 |
+
added = 1
|
| 51 |
+
break
|
| 52 |
+
if added != 1:
|
| 53 |
+
self.args.append(inlist[0])
|
| 54 |
+
inlist.remove(inlist[0])
|
| 55 |
+
i = 0
|
| 56 |
+
# This code is to remove empty parts from the list
|
| 57 |
+
while i < len(self.args):
|
| 58 |
+
if ((self.args[i][0] == 0) | (self.args[i][1] == 0) |
|
| 59 |
+
(self.args[i][2] == 0)):
|
| 60 |
+
self.args.remove(self.args[i])
|
| 61 |
+
i -= 1
|
| 62 |
+
i += 1
|
| 63 |
+
|
| 64 |
+
@property
|
| 65 |
+
def func(self):
|
| 66 |
+
"""Returns the class Dyadic. """
|
| 67 |
+
return Dyadic
|
| 68 |
+
|
| 69 |
+
def __add__(self, other):
|
| 70 |
+
"""The add operator for Dyadic. """
|
| 71 |
+
other = _check_dyadic(other)
|
| 72 |
+
return Dyadic(self.args + other.args)
|
| 73 |
+
|
| 74 |
+
__radd__ = __add__
|
| 75 |
+
|
| 76 |
+
def __mul__(self, other):
|
| 77 |
+
"""Multiplies the Dyadic by a sympifyable expression.
|
| 78 |
+
|
| 79 |
+
Parameters
|
| 80 |
+
==========
|
| 81 |
+
|
| 82 |
+
other : Sympafiable
|
| 83 |
+
The scalar to multiply this Dyadic with
|
| 84 |
+
|
| 85 |
+
Examples
|
| 86 |
+
========
|
| 87 |
+
|
| 88 |
+
>>> from sympy.physics.vector import ReferenceFrame, outer
|
| 89 |
+
>>> N = ReferenceFrame('N')
|
| 90 |
+
>>> d = outer(N.x, N.x)
|
| 91 |
+
>>> 5 * d
|
| 92 |
+
5*(N.x|N.x)
|
| 93 |
+
|
| 94 |
+
"""
|
| 95 |
+
newlist = list(self.args)
|
| 96 |
+
other = sympify(other)
|
| 97 |
+
for i in range(len(newlist)):
|
| 98 |
+
newlist[i] = (other * newlist[i][0], newlist[i][1],
|
| 99 |
+
newlist[i][2])
|
| 100 |
+
return Dyadic(newlist)
|
| 101 |
+
|
| 102 |
+
__rmul__ = __mul__
|
| 103 |
+
|
| 104 |
+
def dot(self, other):
|
| 105 |
+
"""The inner product operator for a Dyadic and a Dyadic or Vector.
|
| 106 |
+
|
| 107 |
+
Parameters
|
| 108 |
+
==========
|
| 109 |
+
|
| 110 |
+
other : Dyadic or Vector
|
| 111 |
+
The other Dyadic or Vector to take the inner product with
|
| 112 |
+
|
| 113 |
+
Examples
|
| 114 |
+
========
|
| 115 |
+
|
| 116 |
+
>>> from sympy.physics.vector import ReferenceFrame, outer
|
| 117 |
+
>>> N = ReferenceFrame('N')
|
| 118 |
+
>>> D1 = outer(N.x, N.y)
|
| 119 |
+
>>> D2 = outer(N.y, N.y)
|
| 120 |
+
>>> D1.dot(D2)
|
| 121 |
+
(N.x|N.y)
|
| 122 |
+
>>> D1.dot(N.y)
|
| 123 |
+
N.x
|
| 124 |
+
|
| 125 |
+
"""
|
| 126 |
+
from sympy.physics.vector.vector import Vector, _check_vector
|
| 127 |
+
if isinstance(other, Dyadic):
|
| 128 |
+
other = _check_dyadic(other)
|
| 129 |
+
ol = Dyadic(0)
|
| 130 |
+
for v in self.args:
|
| 131 |
+
for v2 in other.args:
|
| 132 |
+
ol += v[0] * v2[0] * (v[2].dot(v2[1])) * (v[1].outer(v2[2]))
|
| 133 |
+
else:
|
| 134 |
+
other = _check_vector(other)
|
| 135 |
+
ol = Vector(0)
|
| 136 |
+
for v in self.args:
|
| 137 |
+
ol += v[0] * v[1] * (v[2].dot(other))
|
| 138 |
+
return ol
|
| 139 |
+
|
| 140 |
+
# NOTE : supports non-advertised Dyadic & Dyadic, Dyadic & Vector notation
|
| 141 |
+
__and__ = dot
|
| 142 |
+
|
| 143 |
+
def __truediv__(self, other):
|
| 144 |
+
"""Divides the Dyadic by a sympifyable expression. """
|
| 145 |
+
return self.__mul__(1 / other)
|
| 146 |
+
|
| 147 |
+
def __eq__(self, other):
|
| 148 |
+
"""Tests for equality.
|
| 149 |
+
|
| 150 |
+
Is currently weak; needs stronger comparison testing
|
| 151 |
+
|
| 152 |
+
"""
|
| 153 |
+
|
| 154 |
+
if other == 0:
|
| 155 |
+
other = Dyadic(0)
|
| 156 |
+
other = _check_dyadic(other)
|
| 157 |
+
if (self.args == []) and (other.args == []):
|
| 158 |
+
return True
|
| 159 |
+
elif (self.args == []) or (other.args == []):
|
| 160 |
+
return False
|
| 161 |
+
return set(self.args) == set(other.args)
|
| 162 |
+
|
| 163 |
+
def __ne__(self, other):
|
| 164 |
+
return not self == other
|
| 165 |
+
|
| 166 |
+
def __neg__(self):
|
| 167 |
+
return self * -1
|
| 168 |
+
|
| 169 |
+
def _latex(self, printer):
|
| 170 |
+
ar = self.args # just to shorten things
|
| 171 |
+
if len(ar) == 0:
|
| 172 |
+
return str(0)
|
| 173 |
+
ol = [] # output list, to be concatenated to a string
|
| 174 |
+
for v in ar:
|
| 175 |
+
# if the coef of the dyadic is 1, we skip the 1
|
| 176 |
+
if v[0] == 1:
|
| 177 |
+
ol.append(' + ' + printer._print(v[1]) + r"\otimes " +
|
| 178 |
+
printer._print(v[2]))
|
| 179 |
+
# if the coef of the dyadic is -1, we skip the 1
|
| 180 |
+
elif v[0] == -1:
|
| 181 |
+
ol.append(' - ' +
|
| 182 |
+
printer._print(v[1]) +
|
| 183 |
+
r"\otimes " +
|
| 184 |
+
printer._print(v[2]))
|
| 185 |
+
# If the coefficient of the dyadic is not 1 or -1,
|
| 186 |
+
# we might wrap it in parentheses, for readability.
|
| 187 |
+
elif v[0] != 0:
|
| 188 |
+
arg_str = printer._print(v[0])
|
| 189 |
+
if isinstance(v[0], Add):
|
| 190 |
+
arg_str = '(%s)' % arg_str
|
| 191 |
+
if arg_str.startswith('-'):
|
| 192 |
+
arg_str = arg_str[1:]
|
| 193 |
+
str_start = ' - '
|
| 194 |
+
else:
|
| 195 |
+
str_start = ' + '
|
| 196 |
+
ol.append(str_start + arg_str + printer._print(v[1]) +
|
| 197 |
+
r"\otimes " + printer._print(v[2]))
|
| 198 |
+
outstr = ''.join(ol)
|
| 199 |
+
if outstr.startswith(' + '):
|
| 200 |
+
outstr = outstr[3:]
|
| 201 |
+
elif outstr.startswith(' '):
|
| 202 |
+
outstr = outstr[1:]
|
| 203 |
+
return outstr
|
| 204 |
+
|
| 205 |
+
def _pretty(self, printer):
|
| 206 |
+
e = self
|
| 207 |
+
|
| 208 |
+
class Fake:
|
| 209 |
+
baseline = 0
|
| 210 |
+
|
| 211 |
+
def render(self, *args, **kwargs):
|
| 212 |
+
ar = e.args # just to shorten things
|
| 213 |
+
mpp = printer
|
| 214 |
+
if len(ar) == 0:
|
| 215 |
+
return str(0)
|
| 216 |
+
bar = "\N{CIRCLED TIMES}" if printer._use_unicode else "|"
|
| 217 |
+
ol = [] # output list, to be concatenated to a string
|
| 218 |
+
for v in ar:
|
| 219 |
+
# if the coef of the dyadic is 1, we skip the 1
|
| 220 |
+
if v[0] == 1:
|
| 221 |
+
ol.extend([" + ",
|
| 222 |
+
mpp.doprint(v[1]),
|
| 223 |
+
bar,
|
| 224 |
+
mpp.doprint(v[2])])
|
| 225 |
+
|
| 226 |
+
# if the coef of the dyadic is -1, we skip the 1
|
| 227 |
+
elif v[0] == -1:
|
| 228 |
+
ol.extend([" - ",
|
| 229 |
+
mpp.doprint(v[1]),
|
| 230 |
+
bar,
|
| 231 |
+
mpp.doprint(v[2])])
|
| 232 |
+
|
| 233 |
+
# If the coefficient of the dyadic is not 1 or -1,
|
| 234 |
+
# we might wrap it in parentheses, for readability.
|
| 235 |
+
elif v[0] != 0:
|
| 236 |
+
if isinstance(v[0], Add):
|
| 237 |
+
arg_str = mpp._print(
|
| 238 |
+
v[0]).parens()[0]
|
| 239 |
+
else:
|
| 240 |
+
arg_str = mpp.doprint(v[0])
|
| 241 |
+
if arg_str.startswith("-"):
|
| 242 |
+
arg_str = arg_str[1:]
|
| 243 |
+
str_start = " - "
|
| 244 |
+
else:
|
| 245 |
+
str_start = " + "
|
| 246 |
+
ol.extend([str_start, arg_str, " ",
|
| 247 |
+
mpp.doprint(v[1]),
|
| 248 |
+
bar,
|
| 249 |
+
mpp.doprint(v[2])])
|
| 250 |
+
|
| 251 |
+
outstr = "".join(ol)
|
| 252 |
+
if outstr.startswith(" + "):
|
| 253 |
+
outstr = outstr[3:]
|
| 254 |
+
elif outstr.startswith(" "):
|
| 255 |
+
outstr = outstr[1:]
|
| 256 |
+
return outstr
|
| 257 |
+
return Fake()
|
| 258 |
+
|
| 259 |
+
def __rsub__(self, other):
|
| 260 |
+
return (-1 * self) + other
|
| 261 |
+
|
| 262 |
+
def _sympystr(self, printer):
|
| 263 |
+
"""Printing method. """
|
| 264 |
+
ar = self.args # just to shorten things
|
| 265 |
+
if len(ar) == 0:
|
| 266 |
+
return printer._print(0)
|
| 267 |
+
ol = [] # output list, to be concatenated to a string
|
| 268 |
+
for v in ar:
|
| 269 |
+
# if the coef of the dyadic is 1, we skip the 1
|
| 270 |
+
if v[0] == 1:
|
| 271 |
+
ol.append(' + (' + printer._print(v[1]) + '|' +
|
| 272 |
+
printer._print(v[2]) + ')')
|
| 273 |
+
# if the coef of the dyadic is -1, we skip the 1
|
| 274 |
+
elif v[0] == -1:
|
| 275 |
+
ol.append(' - (' + printer._print(v[1]) + '|' +
|
| 276 |
+
printer._print(v[2]) + ')')
|
| 277 |
+
# If the coefficient of the dyadic is not 1 or -1,
|
| 278 |
+
# we might wrap it in parentheses, for readability.
|
| 279 |
+
elif v[0] != 0:
|
| 280 |
+
arg_str = printer._print(v[0])
|
| 281 |
+
if isinstance(v[0], Add):
|
| 282 |
+
arg_str = "(%s)" % arg_str
|
| 283 |
+
if arg_str[0] == '-':
|
| 284 |
+
arg_str = arg_str[1:]
|
| 285 |
+
str_start = ' - '
|
| 286 |
+
else:
|
| 287 |
+
str_start = ' + '
|
| 288 |
+
ol.append(str_start + arg_str + '*(' +
|
| 289 |
+
printer._print(v[1]) +
|
| 290 |
+
'|' + printer._print(v[2]) + ')')
|
| 291 |
+
outstr = ''.join(ol)
|
| 292 |
+
if outstr.startswith(' + '):
|
| 293 |
+
outstr = outstr[3:]
|
| 294 |
+
elif outstr.startswith(' '):
|
| 295 |
+
outstr = outstr[1:]
|
| 296 |
+
return outstr
|
| 297 |
+
|
| 298 |
+
def __sub__(self, other):
|
| 299 |
+
"""The subtraction operator. """
|
| 300 |
+
return self.__add__(other * -1)
|
| 301 |
+
|
| 302 |
+
def cross(self, other):
|
| 303 |
+
"""Returns the dyadic resulting from the dyadic vector cross product:
|
| 304 |
+
Dyadic x Vector.
|
| 305 |
+
|
| 306 |
+
Parameters
|
| 307 |
+
==========
|
| 308 |
+
other : Vector
|
| 309 |
+
Vector to cross with.
|
| 310 |
+
|
| 311 |
+
Examples
|
| 312 |
+
========
|
| 313 |
+
>>> from sympy.physics.vector import ReferenceFrame, outer, cross
|
| 314 |
+
>>> N = ReferenceFrame('N')
|
| 315 |
+
>>> d = outer(N.x, N.x)
|
| 316 |
+
>>> cross(d, N.y)
|
| 317 |
+
(N.x|N.z)
|
| 318 |
+
|
| 319 |
+
"""
|
| 320 |
+
from sympy.physics.vector.vector import _check_vector
|
| 321 |
+
other = _check_vector(other)
|
| 322 |
+
ol = Dyadic(0)
|
| 323 |
+
for v in self.args:
|
| 324 |
+
ol += v[0] * (v[1].outer((v[2].cross(other))))
|
| 325 |
+
return ol
|
| 326 |
+
|
| 327 |
+
# NOTE : supports non-advertised Dyadic ^ Vector notation
|
| 328 |
+
__xor__ = cross
|
| 329 |
+
|
| 330 |
+
def express(self, frame1, frame2=None):
|
| 331 |
+
"""Expresses this Dyadic in alternate frame(s)
|
| 332 |
+
|
| 333 |
+
The first frame is the list side expression, the second frame is the
|
| 334 |
+
right side; if Dyadic is in form A.x|B.y, you can express it in two
|
| 335 |
+
different frames. If no second frame is given, the Dyadic is
|
| 336 |
+
expressed in only one frame.
|
| 337 |
+
|
| 338 |
+
Calls the global express function
|
| 339 |
+
|
| 340 |
+
Parameters
|
| 341 |
+
==========
|
| 342 |
+
|
| 343 |
+
frame1 : ReferenceFrame
|
| 344 |
+
The frame to express the left side of the Dyadic in
|
| 345 |
+
frame2 : ReferenceFrame
|
| 346 |
+
If provided, the frame to express the right side of the Dyadic in
|
| 347 |
+
|
| 348 |
+
Examples
|
| 349 |
+
========
|
| 350 |
+
|
| 351 |
+
>>> from sympy.physics.vector import ReferenceFrame, outer, dynamicsymbols
|
| 352 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 353 |
+
>>> init_vprinting(pretty_print=False)
|
| 354 |
+
>>> N = ReferenceFrame('N')
|
| 355 |
+
>>> q = dynamicsymbols('q')
|
| 356 |
+
>>> B = N.orientnew('B', 'Axis', [q, N.z])
|
| 357 |
+
>>> d = outer(N.x, N.x)
|
| 358 |
+
>>> d.express(B, N)
|
| 359 |
+
cos(q)*(B.x|N.x) - sin(q)*(B.y|N.x)
|
| 360 |
+
|
| 361 |
+
"""
|
| 362 |
+
from sympy.physics.vector.functions import express
|
| 363 |
+
return express(self, frame1, frame2)
|
| 364 |
+
|
| 365 |
+
def to_matrix(self, reference_frame, second_reference_frame=None):
|
| 366 |
+
"""Returns the matrix form of the dyadic with respect to one or two
|
| 367 |
+
reference frames.
|
| 368 |
+
|
| 369 |
+
Parameters
|
| 370 |
+
----------
|
| 371 |
+
reference_frame : ReferenceFrame
|
| 372 |
+
The reference frame that the rows and columns of the matrix
|
| 373 |
+
correspond to. If a second reference frame is provided, this
|
| 374 |
+
only corresponds to the rows of the matrix.
|
| 375 |
+
second_reference_frame : ReferenceFrame, optional, default=None
|
| 376 |
+
The reference frame that the columns of the matrix correspond
|
| 377 |
+
to.
|
| 378 |
+
|
| 379 |
+
Returns
|
| 380 |
+
-------
|
| 381 |
+
matrix : ImmutableMatrix, shape(3,3)
|
| 382 |
+
The matrix that gives the 2D tensor form.
|
| 383 |
+
|
| 384 |
+
Examples
|
| 385 |
+
========
|
| 386 |
+
|
| 387 |
+
>>> from sympy import symbols, trigsimp
|
| 388 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 389 |
+
>>> from sympy.physics.mechanics import inertia
|
| 390 |
+
>>> Ixx, Iyy, Izz, Ixy, Iyz, Ixz = symbols('Ixx, Iyy, Izz, Ixy, Iyz, Ixz')
|
| 391 |
+
>>> N = ReferenceFrame('N')
|
| 392 |
+
>>> inertia_dyadic = inertia(N, Ixx, Iyy, Izz, Ixy, Iyz, Ixz)
|
| 393 |
+
>>> inertia_dyadic.to_matrix(N)
|
| 394 |
+
Matrix([
|
| 395 |
+
[Ixx, Ixy, Ixz],
|
| 396 |
+
[Ixy, Iyy, Iyz],
|
| 397 |
+
[Ixz, Iyz, Izz]])
|
| 398 |
+
>>> beta = symbols('beta')
|
| 399 |
+
>>> A = N.orientnew('A', 'Axis', (beta, N.x))
|
| 400 |
+
>>> trigsimp(inertia_dyadic.to_matrix(A))
|
| 401 |
+
Matrix([
|
| 402 |
+
[ Ixx, Ixy*cos(beta) + Ixz*sin(beta), -Ixy*sin(beta) + Ixz*cos(beta)],
|
| 403 |
+
[ Ixy*cos(beta) + Ixz*sin(beta), Iyy*cos(2*beta)/2 + Iyy/2 + Iyz*sin(2*beta) - Izz*cos(2*beta)/2 + Izz/2, -Iyy*sin(2*beta)/2 + Iyz*cos(2*beta) + Izz*sin(2*beta)/2],
|
| 404 |
+
[-Ixy*sin(beta) + Ixz*cos(beta), -Iyy*sin(2*beta)/2 + Iyz*cos(2*beta) + Izz*sin(2*beta)/2, -Iyy*cos(2*beta)/2 + Iyy/2 - Iyz*sin(2*beta) + Izz*cos(2*beta)/2 + Izz/2]])
|
| 405 |
+
|
| 406 |
+
"""
|
| 407 |
+
|
| 408 |
+
if second_reference_frame is None:
|
| 409 |
+
second_reference_frame = reference_frame
|
| 410 |
+
|
| 411 |
+
return Matrix([i.dot(self).dot(j) for i in reference_frame for j in
|
| 412 |
+
second_reference_frame]).reshape(3, 3)
|
| 413 |
+
|
| 414 |
+
def doit(self, **hints):
|
| 415 |
+
"""Calls .doit() on each term in the Dyadic"""
|
| 416 |
+
return sum([Dyadic([(v[0].doit(**hints), v[1], v[2])])
|
| 417 |
+
for v in self.args], Dyadic(0))
|
| 418 |
+
|
| 419 |
+
def dt(self, frame):
|
| 420 |
+
"""Take the time derivative of this Dyadic in a frame.
|
| 421 |
+
|
| 422 |
+
This function calls the global time_derivative method
|
| 423 |
+
|
| 424 |
+
Parameters
|
| 425 |
+
==========
|
| 426 |
+
|
| 427 |
+
frame : ReferenceFrame
|
| 428 |
+
The frame to take the time derivative in
|
| 429 |
+
|
| 430 |
+
Examples
|
| 431 |
+
========
|
| 432 |
+
|
| 433 |
+
>>> from sympy.physics.vector import ReferenceFrame, outer, dynamicsymbols
|
| 434 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 435 |
+
>>> init_vprinting(pretty_print=False)
|
| 436 |
+
>>> N = ReferenceFrame('N')
|
| 437 |
+
>>> q = dynamicsymbols('q')
|
| 438 |
+
>>> B = N.orientnew('B', 'Axis', [q, N.z])
|
| 439 |
+
>>> d = outer(N.x, N.x)
|
| 440 |
+
>>> d.dt(B)
|
| 441 |
+
- q'*(N.y|N.x) - q'*(N.x|N.y)
|
| 442 |
+
|
| 443 |
+
"""
|
| 444 |
+
from sympy.physics.vector.functions import time_derivative
|
| 445 |
+
return time_derivative(self, frame)
|
| 446 |
+
|
| 447 |
+
def simplify(self):
|
| 448 |
+
"""Returns a simplified Dyadic."""
|
| 449 |
+
out = Dyadic(0)
|
| 450 |
+
for v in self.args:
|
| 451 |
+
out += Dyadic([(v[0].simplify(), v[1], v[2])])
|
| 452 |
+
return out
|
| 453 |
+
|
| 454 |
+
def subs(self, *args, **kwargs):
|
| 455 |
+
"""Substitution on the Dyadic.
|
| 456 |
+
|
| 457 |
+
Examples
|
| 458 |
+
========
|
| 459 |
+
|
| 460 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 461 |
+
>>> from sympy import Symbol
|
| 462 |
+
>>> N = ReferenceFrame('N')
|
| 463 |
+
>>> s = Symbol('s')
|
| 464 |
+
>>> a = s*(N.x|N.x)
|
| 465 |
+
>>> a.subs({s: 2})
|
| 466 |
+
2*(N.x|N.x)
|
| 467 |
+
|
| 468 |
+
"""
|
| 469 |
+
|
| 470 |
+
return sum([Dyadic([(v[0].subs(*args, **kwargs), v[1], v[2])])
|
| 471 |
+
for v in self.args], Dyadic(0))
|
| 472 |
+
|
| 473 |
+
def applyfunc(self, f):
|
| 474 |
+
"""Apply a function to each component of a Dyadic."""
|
| 475 |
+
if not callable(f):
|
| 476 |
+
raise TypeError("`f` must be callable.")
|
| 477 |
+
|
| 478 |
+
out = Dyadic(0)
|
| 479 |
+
for a, b, c in self.args:
|
| 480 |
+
out += f(a) * (b.outer(c))
|
| 481 |
+
return out
|
| 482 |
+
|
| 483 |
+
def _eval_evalf(self, prec):
|
| 484 |
+
if not self.args:
|
| 485 |
+
return self
|
| 486 |
+
new_args = []
|
| 487 |
+
dps = prec_to_dps(prec)
|
| 488 |
+
for inlist in self.args:
|
| 489 |
+
new_inlist = list(inlist)
|
| 490 |
+
new_inlist[0] = inlist[0].evalf(n=dps)
|
| 491 |
+
new_args.append(tuple(new_inlist))
|
| 492 |
+
return Dyadic(new_args)
|
| 493 |
+
|
| 494 |
+
def xreplace(self, rule):
|
| 495 |
+
"""
|
| 496 |
+
Replace occurrences of objects within the measure numbers of the
|
| 497 |
+
Dyadic.
|
| 498 |
+
|
| 499 |
+
Parameters
|
| 500 |
+
==========
|
| 501 |
+
|
| 502 |
+
rule : dict-like
|
| 503 |
+
Expresses a replacement rule.
|
| 504 |
+
|
| 505 |
+
Returns
|
| 506 |
+
=======
|
| 507 |
+
|
| 508 |
+
Dyadic
|
| 509 |
+
Result of the replacement.
|
| 510 |
+
|
| 511 |
+
Examples
|
| 512 |
+
========
|
| 513 |
+
|
| 514 |
+
>>> from sympy import symbols, pi
|
| 515 |
+
>>> from sympy.physics.vector import ReferenceFrame, outer
|
| 516 |
+
>>> N = ReferenceFrame('N')
|
| 517 |
+
>>> D = outer(N.x, N.x)
|
| 518 |
+
>>> x, y, z = symbols('x y z')
|
| 519 |
+
>>> ((1 + x*y) * D).xreplace({x: pi})
|
| 520 |
+
(pi*y + 1)*(N.x|N.x)
|
| 521 |
+
>>> ((1 + x*y) * D).xreplace({x: pi, y: 2})
|
| 522 |
+
(1 + 2*pi)*(N.x|N.x)
|
| 523 |
+
|
| 524 |
+
Replacements occur only if an entire node in the expression tree is
|
| 525 |
+
matched:
|
| 526 |
+
|
| 527 |
+
>>> ((x*y + z) * D).xreplace({x*y: pi})
|
| 528 |
+
(z + pi)*(N.x|N.x)
|
| 529 |
+
>>> ((x*y*z) * D).xreplace({x*y: pi})
|
| 530 |
+
x*y*z*(N.x|N.x)
|
| 531 |
+
|
| 532 |
+
"""
|
| 533 |
+
|
| 534 |
+
new_args = []
|
| 535 |
+
for inlist in self.args:
|
| 536 |
+
new_inlist = list(inlist)
|
| 537 |
+
new_inlist[0] = new_inlist[0].xreplace(rule)
|
| 538 |
+
new_args.append(tuple(new_inlist))
|
| 539 |
+
return Dyadic(new_args)
|
| 540 |
+
|
| 541 |
+
|
| 542 |
+
def _check_dyadic(other):
|
| 543 |
+
if not isinstance(other, Dyadic):
|
| 544 |
+
raise TypeError('A Dyadic must be supplied')
|
| 545 |
+
return other
|
hug/lib/python3.12/site-packages/sympy/physics/vector/fieldfunctions.py
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.function import diff
|
| 2 |
+
from sympy.core.singleton import S
|
| 3 |
+
from sympy.integrals.integrals import integrate
|
| 4 |
+
from sympy.physics.vector import Vector, express
|
| 5 |
+
from sympy.physics.vector.frame import _check_frame
|
| 6 |
+
from sympy.physics.vector.vector import _check_vector
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
__all__ = ['curl', 'divergence', 'gradient', 'is_conservative',
|
| 10 |
+
'is_solenoidal', 'scalar_potential',
|
| 11 |
+
'scalar_potential_difference']
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def curl(vect, frame):
|
| 15 |
+
"""
|
| 16 |
+
Returns the curl of a vector field computed wrt the coordinate
|
| 17 |
+
symbols of the given frame.
|
| 18 |
+
|
| 19 |
+
Parameters
|
| 20 |
+
==========
|
| 21 |
+
|
| 22 |
+
vect : Vector
|
| 23 |
+
The vector operand
|
| 24 |
+
|
| 25 |
+
frame : ReferenceFrame
|
| 26 |
+
The reference frame to calculate the curl in
|
| 27 |
+
|
| 28 |
+
Examples
|
| 29 |
+
========
|
| 30 |
+
|
| 31 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 32 |
+
>>> from sympy.physics.vector import curl
|
| 33 |
+
>>> R = ReferenceFrame('R')
|
| 34 |
+
>>> v1 = R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z
|
| 35 |
+
>>> curl(v1, R)
|
| 36 |
+
0
|
| 37 |
+
>>> v2 = R[0]*R[1]*R[2]*R.x
|
| 38 |
+
>>> curl(v2, R)
|
| 39 |
+
R_x*R_y*R.y - R_x*R_z*R.z
|
| 40 |
+
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
_check_vector(vect)
|
| 44 |
+
if vect == 0:
|
| 45 |
+
return Vector(0)
|
| 46 |
+
vect = express(vect, frame, variables=True)
|
| 47 |
+
# A mechanical approach to avoid looping overheads
|
| 48 |
+
vectx = vect.dot(frame.x)
|
| 49 |
+
vecty = vect.dot(frame.y)
|
| 50 |
+
vectz = vect.dot(frame.z)
|
| 51 |
+
outvec = Vector(0)
|
| 52 |
+
outvec += (diff(vectz, frame[1]) - diff(vecty, frame[2])) * frame.x
|
| 53 |
+
outvec += (diff(vectx, frame[2]) - diff(vectz, frame[0])) * frame.y
|
| 54 |
+
outvec += (diff(vecty, frame[0]) - diff(vectx, frame[1])) * frame.z
|
| 55 |
+
return outvec
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def divergence(vect, frame):
|
| 59 |
+
"""
|
| 60 |
+
Returns the divergence of a vector field computed wrt the coordinate
|
| 61 |
+
symbols of the given frame.
|
| 62 |
+
|
| 63 |
+
Parameters
|
| 64 |
+
==========
|
| 65 |
+
|
| 66 |
+
vect : Vector
|
| 67 |
+
The vector operand
|
| 68 |
+
|
| 69 |
+
frame : ReferenceFrame
|
| 70 |
+
The reference frame to calculate the divergence in
|
| 71 |
+
|
| 72 |
+
Examples
|
| 73 |
+
========
|
| 74 |
+
|
| 75 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 76 |
+
>>> from sympy.physics.vector import divergence
|
| 77 |
+
>>> R = ReferenceFrame('R')
|
| 78 |
+
>>> v1 = R[0]*R[1]*R[2] * (R.x+R.y+R.z)
|
| 79 |
+
>>> divergence(v1, R)
|
| 80 |
+
R_x*R_y + R_x*R_z + R_y*R_z
|
| 81 |
+
>>> v2 = 2*R[1]*R[2]*R.y
|
| 82 |
+
>>> divergence(v2, R)
|
| 83 |
+
2*R_z
|
| 84 |
+
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
_check_vector(vect)
|
| 88 |
+
if vect == 0:
|
| 89 |
+
return S.Zero
|
| 90 |
+
vect = express(vect, frame, variables=True)
|
| 91 |
+
vectx = vect.dot(frame.x)
|
| 92 |
+
vecty = vect.dot(frame.y)
|
| 93 |
+
vectz = vect.dot(frame.z)
|
| 94 |
+
out = S.Zero
|
| 95 |
+
out += diff(vectx, frame[0])
|
| 96 |
+
out += diff(vecty, frame[1])
|
| 97 |
+
out += diff(vectz, frame[2])
|
| 98 |
+
return out
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def gradient(scalar, frame):
|
| 102 |
+
"""
|
| 103 |
+
Returns the vector gradient of a scalar field computed wrt the
|
| 104 |
+
coordinate symbols of the given frame.
|
| 105 |
+
|
| 106 |
+
Parameters
|
| 107 |
+
==========
|
| 108 |
+
|
| 109 |
+
scalar : sympifiable
|
| 110 |
+
The scalar field to take the gradient of
|
| 111 |
+
|
| 112 |
+
frame : ReferenceFrame
|
| 113 |
+
The frame to calculate the gradient in
|
| 114 |
+
|
| 115 |
+
Examples
|
| 116 |
+
========
|
| 117 |
+
|
| 118 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 119 |
+
>>> from sympy.physics.vector import gradient
|
| 120 |
+
>>> R = ReferenceFrame('R')
|
| 121 |
+
>>> s1 = R[0]*R[1]*R[2]
|
| 122 |
+
>>> gradient(s1, R)
|
| 123 |
+
R_y*R_z*R.x + R_x*R_z*R.y + R_x*R_y*R.z
|
| 124 |
+
>>> s2 = 5*R[0]**2*R[2]
|
| 125 |
+
>>> gradient(s2, R)
|
| 126 |
+
10*R_x*R_z*R.x + 5*R_x**2*R.z
|
| 127 |
+
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
+
_check_frame(frame)
|
| 131 |
+
outvec = Vector(0)
|
| 132 |
+
scalar = express(scalar, frame, variables=True)
|
| 133 |
+
for i, x in enumerate(frame):
|
| 134 |
+
outvec += diff(scalar, frame[i]) * x # noqa: PLR1736
|
| 135 |
+
return outvec
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def is_conservative(field):
|
| 139 |
+
"""
|
| 140 |
+
Checks if a field is conservative.
|
| 141 |
+
|
| 142 |
+
Parameters
|
| 143 |
+
==========
|
| 144 |
+
|
| 145 |
+
field : Vector
|
| 146 |
+
The field to check for conservative property
|
| 147 |
+
|
| 148 |
+
Examples
|
| 149 |
+
========
|
| 150 |
+
|
| 151 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 152 |
+
>>> from sympy.physics.vector import is_conservative
|
| 153 |
+
>>> R = ReferenceFrame('R')
|
| 154 |
+
>>> is_conservative(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z)
|
| 155 |
+
True
|
| 156 |
+
>>> is_conservative(R[2] * R.y)
|
| 157 |
+
False
|
| 158 |
+
|
| 159 |
+
"""
|
| 160 |
+
|
| 161 |
+
# Field is conservative irrespective of frame
|
| 162 |
+
# Take the first frame in the result of the separate method of Vector
|
| 163 |
+
if field == Vector(0):
|
| 164 |
+
return True
|
| 165 |
+
frame = list(field.separate())[0]
|
| 166 |
+
return curl(field, frame).simplify() == Vector(0)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def is_solenoidal(field):
|
| 170 |
+
"""
|
| 171 |
+
Checks if a field is solenoidal.
|
| 172 |
+
|
| 173 |
+
Parameters
|
| 174 |
+
==========
|
| 175 |
+
|
| 176 |
+
field : Vector
|
| 177 |
+
The field to check for solenoidal property
|
| 178 |
+
|
| 179 |
+
Examples
|
| 180 |
+
========
|
| 181 |
+
|
| 182 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 183 |
+
>>> from sympy.physics.vector import is_solenoidal
|
| 184 |
+
>>> R = ReferenceFrame('R')
|
| 185 |
+
>>> is_solenoidal(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z)
|
| 186 |
+
True
|
| 187 |
+
>>> is_solenoidal(R[1] * R.y)
|
| 188 |
+
False
|
| 189 |
+
|
| 190 |
+
"""
|
| 191 |
+
|
| 192 |
+
# Field is solenoidal irrespective of frame
|
| 193 |
+
# Take the first frame in the result of the separate method in Vector
|
| 194 |
+
if field == Vector(0):
|
| 195 |
+
return True
|
| 196 |
+
frame = list(field.separate())[0]
|
| 197 |
+
return divergence(field, frame).simplify() is S.Zero
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
def scalar_potential(field, frame):
|
| 201 |
+
"""
|
| 202 |
+
Returns the scalar potential function of a field in a given frame
|
| 203 |
+
(without the added integration constant).
|
| 204 |
+
|
| 205 |
+
Parameters
|
| 206 |
+
==========
|
| 207 |
+
|
| 208 |
+
field : Vector
|
| 209 |
+
The vector field whose scalar potential function is to be
|
| 210 |
+
calculated
|
| 211 |
+
|
| 212 |
+
frame : ReferenceFrame
|
| 213 |
+
The frame to do the calculation in
|
| 214 |
+
|
| 215 |
+
Examples
|
| 216 |
+
========
|
| 217 |
+
|
| 218 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 219 |
+
>>> from sympy.physics.vector import scalar_potential, gradient
|
| 220 |
+
>>> R = ReferenceFrame('R')
|
| 221 |
+
>>> scalar_potential(R.z, R) == R[2]
|
| 222 |
+
True
|
| 223 |
+
>>> scalar_field = 2*R[0]**2*R[1]*R[2]
|
| 224 |
+
>>> grad_field = gradient(scalar_field, R)
|
| 225 |
+
>>> scalar_potential(grad_field, R)
|
| 226 |
+
2*R_x**2*R_y*R_z
|
| 227 |
+
|
| 228 |
+
"""
|
| 229 |
+
|
| 230 |
+
# Check whether field is conservative
|
| 231 |
+
if not is_conservative(field):
|
| 232 |
+
raise ValueError("Field is not conservative")
|
| 233 |
+
if field == Vector(0):
|
| 234 |
+
return S.Zero
|
| 235 |
+
# Express the field exntirely in frame
|
| 236 |
+
# Substitute coordinate variables also
|
| 237 |
+
_check_frame(frame)
|
| 238 |
+
field = express(field, frame, variables=True)
|
| 239 |
+
# Make a list of dimensions of the frame
|
| 240 |
+
dimensions = list(frame)
|
| 241 |
+
# Calculate scalar potential function
|
| 242 |
+
temp_function = integrate(field.dot(dimensions[0]), frame[0])
|
| 243 |
+
for i, dim in enumerate(dimensions[1:]):
|
| 244 |
+
partial_diff = diff(temp_function, frame[i + 1])
|
| 245 |
+
partial_diff = field.dot(dim) - partial_diff
|
| 246 |
+
temp_function += integrate(partial_diff, frame[i + 1])
|
| 247 |
+
return temp_function
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def scalar_potential_difference(field, frame, point1, point2, origin):
|
| 251 |
+
"""
|
| 252 |
+
Returns the scalar potential difference between two points in a
|
| 253 |
+
certain frame, wrt a given field.
|
| 254 |
+
|
| 255 |
+
If a scalar field is provided, its values at the two points are
|
| 256 |
+
considered. If a conservative vector field is provided, the values
|
| 257 |
+
of its scalar potential function at the two points are used.
|
| 258 |
+
|
| 259 |
+
Returns (potential at position 2) - (potential at position 1)
|
| 260 |
+
|
| 261 |
+
Parameters
|
| 262 |
+
==========
|
| 263 |
+
|
| 264 |
+
field : Vector/sympyfiable
|
| 265 |
+
The field to calculate wrt
|
| 266 |
+
|
| 267 |
+
frame : ReferenceFrame
|
| 268 |
+
The frame to do the calculations in
|
| 269 |
+
|
| 270 |
+
point1 : Point
|
| 271 |
+
The initial Point in given frame
|
| 272 |
+
|
| 273 |
+
position2 : Point
|
| 274 |
+
The second Point in the given frame
|
| 275 |
+
|
| 276 |
+
origin : Point
|
| 277 |
+
The Point to use as reference point for position vector
|
| 278 |
+
calculation
|
| 279 |
+
|
| 280 |
+
Examples
|
| 281 |
+
========
|
| 282 |
+
|
| 283 |
+
>>> from sympy.physics.vector import ReferenceFrame, Point
|
| 284 |
+
>>> from sympy.physics.vector import scalar_potential_difference
|
| 285 |
+
>>> R = ReferenceFrame('R')
|
| 286 |
+
>>> O = Point('O')
|
| 287 |
+
>>> P = O.locatenew('P', R[0]*R.x + R[1]*R.y + R[2]*R.z)
|
| 288 |
+
>>> vectfield = 4*R[0]*R[1]*R.x + 2*R[0]**2*R.y
|
| 289 |
+
>>> scalar_potential_difference(vectfield, R, O, P, O)
|
| 290 |
+
2*R_x**2*R_y
|
| 291 |
+
>>> Q = O.locatenew('O', 3*R.x + R.y + 2*R.z)
|
| 292 |
+
>>> scalar_potential_difference(vectfield, R, P, Q, O)
|
| 293 |
+
-2*R_x**2*R_y + 18
|
| 294 |
+
|
| 295 |
+
"""
|
| 296 |
+
|
| 297 |
+
_check_frame(frame)
|
| 298 |
+
if isinstance(field, Vector):
|
| 299 |
+
# Get the scalar potential function
|
| 300 |
+
scalar_fn = scalar_potential(field, frame)
|
| 301 |
+
else:
|
| 302 |
+
# Field is a scalar
|
| 303 |
+
scalar_fn = field
|
| 304 |
+
# Express positions in required frame
|
| 305 |
+
position1 = express(point1.pos_from(origin), frame, variables=True)
|
| 306 |
+
position2 = express(point2.pos_from(origin), frame, variables=True)
|
| 307 |
+
# Get the two positions as substitution dicts for coordinate variables
|
| 308 |
+
subs_dict1 = {}
|
| 309 |
+
subs_dict2 = {}
|
| 310 |
+
for i, x in enumerate(frame):
|
| 311 |
+
subs_dict1[frame[i]] = x.dot(position1)
|
| 312 |
+
subs_dict2[frame[i]] = x.dot(position2)
|
| 313 |
+
return scalar_fn.subs(subs_dict2) - scalar_fn.subs(subs_dict1)
|
hug/lib/python3.12/site-packages/sympy/physics/vector/frame.py
ADDED
|
@@ -0,0 +1,1575 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy import (diff, expand, sin, cos, sympify, eye, zeros,
|
| 2 |
+
ImmutableMatrix as Matrix, MatrixBase)
|
| 3 |
+
from sympy.core.symbol import Symbol
|
| 4 |
+
from sympy.simplify.trigsimp import trigsimp
|
| 5 |
+
from sympy.physics.vector.vector import Vector, _check_vector
|
| 6 |
+
from sympy.utilities.misc import translate
|
| 7 |
+
|
| 8 |
+
from warnings import warn
|
| 9 |
+
|
| 10 |
+
__all__ = ['CoordinateSym', 'ReferenceFrame']
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class CoordinateSym(Symbol):
|
| 14 |
+
"""
|
| 15 |
+
A coordinate symbol/base scalar associated wrt a Reference Frame.
|
| 16 |
+
|
| 17 |
+
Ideally, users should not instantiate this class. Instances of
|
| 18 |
+
this class must only be accessed through the corresponding frame
|
| 19 |
+
as 'frame[index]'.
|
| 20 |
+
|
| 21 |
+
CoordinateSyms having the same frame and index parameters are equal
|
| 22 |
+
(even though they may be instantiated separately).
|
| 23 |
+
|
| 24 |
+
Parameters
|
| 25 |
+
==========
|
| 26 |
+
|
| 27 |
+
name : string
|
| 28 |
+
The display name of the CoordinateSym
|
| 29 |
+
|
| 30 |
+
frame : ReferenceFrame
|
| 31 |
+
The reference frame this base scalar belongs to
|
| 32 |
+
|
| 33 |
+
index : 0, 1 or 2
|
| 34 |
+
The index of the dimension denoted by this coordinate variable
|
| 35 |
+
|
| 36 |
+
Examples
|
| 37 |
+
========
|
| 38 |
+
|
| 39 |
+
>>> from sympy.physics.vector import ReferenceFrame, CoordinateSym
|
| 40 |
+
>>> A = ReferenceFrame('A')
|
| 41 |
+
>>> A[1]
|
| 42 |
+
A_y
|
| 43 |
+
>>> type(A[0])
|
| 44 |
+
<class 'sympy.physics.vector.frame.CoordinateSym'>
|
| 45 |
+
>>> a_y = CoordinateSym('a_y', A, 1)
|
| 46 |
+
>>> a_y == A[1]
|
| 47 |
+
True
|
| 48 |
+
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
def __new__(cls, name, frame, index):
|
| 52 |
+
# We can't use the cached Symbol.__new__ because this class depends on
|
| 53 |
+
# frame and index, which are not passed to Symbol.__xnew__.
|
| 54 |
+
assumptions = {}
|
| 55 |
+
super()._sanitize(assumptions, cls)
|
| 56 |
+
obj = super().__xnew__(cls, name, **assumptions)
|
| 57 |
+
_check_frame(frame)
|
| 58 |
+
if index not in range(0, 3):
|
| 59 |
+
raise ValueError("Invalid index specified")
|
| 60 |
+
obj._id = (frame, index)
|
| 61 |
+
return obj
|
| 62 |
+
|
| 63 |
+
def __getnewargs_ex__(self):
|
| 64 |
+
return (self.name, *self._id), {}
|
| 65 |
+
|
| 66 |
+
@property
|
| 67 |
+
def frame(self):
|
| 68 |
+
return self._id[0]
|
| 69 |
+
|
| 70 |
+
def __eq__(self, other):
|
| 71 |
+
# Check if the other object is a CoordinateSym of the same frame and
|
| 72 |
+
# same index
|
| 73 |
+
if isinstance(other, CoordinateSym):
|
| 74 |
+
if other._id == self._id:
|
| 75 |
+
return True
|
| 76 |
+
return False
|
| 77 |
+
|
| 78 |
+
def __ne__(self, other):
|
| 79 |
+
return not self == other
|
| 80 |
+
|
| 81 |
+
def __hash__(self):
|
| 82 |
+
return (self._id[0].__hash__(), self._id[1]).__hash__()
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
class ReferenceFrame:
|
| 86 |
+
"""A reference frame in classical mechanics.
|
| 87 |
+
|
| 88 |
+
ReferenceFrame is a class used to represent a reference frame in classical
|
| 89 |
+
mechanics. It has a standard basis of three unit vectors in the frame's
|
| 90 |
+
x, y, and z directions.
|
| 91 |
+
|
| 92 |
+
It also can have a rotation relative to a parent frame; this rotation is
|
| 93 |
+
defined by a direction cosine matrix relating this frame's basis vectors to
|
| 94 |
+
the parent frame's basis vectors. It can also have an angular velocity
|
| 95 |
+
vector, defined in another frame.
|
| 96 |
+
|
| 97 |
+
"""
|
| 98 |
+
_count = 0
|
| 99 |
+
|
| 100 |
+
def __init__(self, name, indices=None, latexs=None, variables=None):
|
| 101 |
+
"""ReferenceFrame initialization method.
|
| 102 |
+
|
| 103 |
+
A ReferenceFrame has a set of orthonormal basis vectors, along with
|
| 104 |
+
orientations relative to other ReferenceFrames and angular velocities
|
| 105 |
+
relative to other ReferenceFrames.
|
| 106 |
+
|
| 107 |
+
Parameters
|
| 108 |
+
==========
|
| 109 |
+
|
| 110 |
+
indices : tuple of str
|
| 111 |
+
Enables the reference frame's basis unit vectors to be accessed by
|
| 112 |
+
Python's square bracket indexing notation using the provided three
|
| 113 |
+
indice strings and alters the printing of the unit vectors to
|
| 114 |
+
reflect this choice.
|
| 115 |
+
latexs : tuple of str
|
| 116 |
+
Alters the LaTeX printing of the reference frame's basis unit
|
| 117 |
+
vectors to the provided three valid LaTeX strings.
|
| 118 |
+
|
| 119 |
+
Examples
|
| 120 |
+
========
|
| 121 |
+
|
| 122 |
+
>>> from sympy.physics.vector import ReferenceFrame, vlatex
|
| 123 |
+
>>> N = ReferenceFrame('N')
|
| 124 |
+
>>> N.x
|
| 125 |
+
N.x
|
| 126 |
+
>>> O = ReferenceFrame('O', indices=('1', '2', '3'))
|
| 127 |
+
>>> O.x
|
| 128 |
+
O['1']
|
| 129 |
+
>>> O['1']
|
| 130 |
+
O['1']
|
| 131 |
+
>>> P = ReferenceFrame('P', latexs=('A1', 'A2', 'A3'))
|
| 132 |
+
>>> vlatex(P.x)
|
| 133 |
+
'A1'
|
| 134 |
+
|
| 135 |
+
``symbols()`` can be used to create multiple Reference Frames in one
|
| 136 |
+
step, for example:
|
| 137 |
+
|
| 138 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 139 |
+
>>> from sympy import symbols
|
| 140 |
+
>>> A, B, C = symbols('A B C', cls=ReferenceFrame)
|
| 141 |
+
>>> D, E = symbols('D E', cls=ReferenceFrame, indices=('1', '2', '3'))
|
| 142 |
+
>>> A[0]
|
| 143 |
+
A_x
|
| 144 |
+
>>> D.x
|
| 145 |
+
D['1']
|
| 146 |
+
>>> E.y
|
| 147 |
+
E['2']
|
| 148 |
+
>>> type(A) == type(D)
|
| 149 |
+
True
|
| 150 |
+
|
| 151 |
+
Unit dyads for the ReferenceFrame can be accessed through the attributes ``xx``, ``xy``, etc. For example:
|
| 152 |
+
|
| 153 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 154 |
+
>>> N = ReferenceFrame('N')
|
| 155 |
+
>>> N.yz
|
| 156 |
+
(N.y|N.z)
|
| 157 |
+
>>> N.zx
|
| 158 |
+
(N.z|N.x)
|
| 159 |
+
>>> P = ReferenceFrame('P', indices=['1', '2', '3'])
|
| 160 |
+
>>> P.xx
|
| 161 |
+
(P['1']|P['1'])
|
| 162 |
+
>>> P.zy
|
| 163 |
+
(P['3']|P['2'])
|
| 164 |
+
|
| 165 |
+
Unit dyadic is also accessible via the ``u`` attribute:
|
| 166 |
+
|
| 167 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 168 |
+
>>> N = ReferenceFrame('N')
|
| 169 |
+
>>> N.u
|
| 170 |
+
(N.x|N.x) + (N.y|N.y) + (N.z|N.z)
|
| 171 |
+
>>> P = ReferenceFrame('P', indices=['1', '2', '3'])
|
| 172 |
+
>>> P.u
|
| 173 |
+
(P['1']|P['1']) + (P['2']|P['2']) + (P['3']|P['3'])
|
| 174 |
+
|
| 175 |
+
"""
|
| 176 |
+
|
| 177 |
+
if not isinstance(name, str):
|
| 178 |
+
raise TypeError('Need to supply a valid name')
|
| 179 |
+
# The if statements below are for custom printing of basis-vectors for
|
| 180 |
+
# each frame.
|
| 181 |
+
# First case, when custom indices are supplied
|
| 182 |
+
if indices is not None:
|
| 183 |
+
if not isinstance(indices, (tuple, list)):
|
| 184 |
+
raise TypeError('Supply the indices as a list')
|
| 185 |
+
if len(indices) != 3:
|
| 186 |
+
raise ValueError('Supply 3 indices')
|
| 187 |
+
for i in indices:
|
| 188 |
+
if not isinstance(i, str):
|
| 189 |
+
raise TypeError('Indices must be strings')
|
| 190 |
+
self.str_vecs = [(name + '[\'' + indices[0] + '\']'),
|
| 191 |
+
(name + '[\'' + indices[1] + '\']'),
|
| 192 |
+
(name + '[\'' + indices[2] + '\']')]
|
| 193 |
+
self.pretty_vecs = [(name.lower() + "_" + indices[0]),
|
| 194 |
+
(name.lower() + "_" + indices[1]),
|
| 195 |
+
(name.lower() + "_" + indices[2])]
|
| 196 |
+
self.latex_vecs = [(r"\mathbf{\hat{%s}_{%s}}" % (name.lower(),
|
| 197 |
+
indices[0])),
|
| 198 |
+
(r"\mathbf{\hat{%s}_{%s}}" % (name.lower(),
|
| 199 |
+
indices[1])),
|
| 200 |
+
(r"\mathbf{\hat{%s}_{%s}}" % (name.lower(),
|
| 201 |
+
indices[2]))]
|
| 202 |
+
self.indices = indices
|
| 203 |
+
# Second case, when no custom indices are supplied
|
| 204 |
+
else:
|
| 205 |
+
self.str_vecs = [(name + '.x'), (name + '.y'), (name + '.z')]
|
| 206 |
+
self.pretty_vecs = [name.lower() + "_x",
|
| 207 |
+
name.lower() + "_y",
|
| 208 |
+
name.lower() + "_z"]
|
| 209 |
+
self.latex_vecs = [(r"\mathbf{\hat{%s}_x}" % name.lower()),
|
| 210 |
+
(r"\mathbf{\hat{%s}_y}" % name.lower()),
|
| 211 |
+
(r"\mathbf{\hat{%s}_z}" % name.lower())]
|
| 212 |
+
self.indices = ['x', 'y', 'z']
|
| 213 |
+
# Different step, for custom latex basis vectors
|
| 214 |
+
if latexs is not None:
|
| 215 |
+
if not isinstance(latexs, (tuple, list)):
|
| 216 |
+
raise TypeError('Supply the indices as a list')
|
| 217 |
+
if len(latexs) != 3:
|
| 218 |
+
raise ValueError('Supply 3 indices')
|
| 219 |
+
for i in latexs:
|
| 220 |
+
if not isinstance(i, str):
|
| 221 |
+
raise TypeError('Latex entries must be strings')
|
| 222 |
+
self.latex_vecs = latexs
|
| 223 |
+
self.name = name
|
| 224 |
+
self._var_dict = {}
|
| 225 |
+
# The _dcm_dict dictionary will only store the dcms of adjacent
|
| 226 |
+
# parent-child relationships. The _dcm_cache dictionary will store
|
| 227 |
+
# calculated dcm along with all content of _dcm_dict for faster
|
| 228 |
+
# retrieval of dcms.
|
| 229 |
+
self._dcm_dict = {}
|
| 230 |
+
self._dcm_cache = {}
|
| 231 |
+
self._ang_vel_dict = {}
|
| 232 |
+
self._ang_acc_dict = {}
|
| 233 |
+
self._dlist = [self._dcm_dict, self._ang_vel_dict, self._ang_acc_dict]
|
| 234 |
+
self._cur = 0
|
| 235 |
+
self._x = Vector([(Matrix([1, 0, 0]), self)])
|
| 236 |
+
self._y = Vector([(Matrix([0, 1, 0]), self)])
|
| 237 |
+
self._z = Vector([(Matrix([0, 0, 1]), self)])
|
| 238 |
+
# Associate coordinate symbols wrt this frame
|
| 239 |
+
if variables is not None:
|
| 240 |
+
if not isinstance(variables, (tuple, list)):
|
| 241 |
+
raise TypeError('Supply the variable names as a list/tuple')
|
| 242 |
+
if len(variables) != 3:
|
| 243 |
+
raise ValueError('Supply 3 variable names')
|
| 244 |
+
for i in variables:
|
| 245 |
+
if not isinstance(i, str):
|
| 246 |
+
raise TypeError('Variable names must be strings')
|
| 247 |
+
else:
|
| 248 |
+
variables = [name + '_x', name + '_y', name + '_z']
|
| 249 |
+
self.varlist = (CoordinateSym(variables[0], self, 0),
|
| 250 |
+
CoordinateSym(variables[1], self, 1),
|
| 251 |
+
CoordinateSym(variables[2], self, 2))
|
| 252 |
+
ReferenceFrame._count += 1
|
| 253 |
+
self.index = ReferenceFrame._count
|
| 254 |
+
|
| 255 |
+
def __getitem__(self, ind):
|
| 256 |
+
"""
|
| 257 |
+
Returns basis vector for the provided index, if the index is a string.
|
| 258 |
+
|
| 259 |
+
If the index is a number, returns the coordinate variable correspon-
|
| 260 |
+
-ding to that index.
|
| 261 |
+
"""
|
| 262 |
+
if not isinstance(ind, str):
|
| 263 |
+
if ind < 3:
|
| 264 |
+
return self.varlist[ind]
|
| 265 |
+
else:
|
| 266 |
+
raise ValueError("Invalid index provided")
|
| 267 |
+
if self.indices[0] == ind:
|
| 268 |
+
return self.x
|
| 269 |
+
if self.indices[1] == ind:
|
| 270 |
+
return self.y
|
| 271 |
+
if self.indices[2] == ind:
|
| 272 |
+
return self.z
|
| 273 |
+
else:
|
| 274 |
+
raise ValueError('Not a defined index')
|
| 275 |
+
|
| 276 |
+
def __iter__(self):
|
| 277 |
+
return iter([self.x, self.y, self.z])
|
| 278 |
+
|
| 279 |
+
def __str__(self):
|
| 280 |
+
"""Returns the name of the frame. """
|
| 281 |
+
return self.name
|
| 282 |
+
|
| 283 |
+
__repr__ = __str__
|
| 284 |
+
|
| 285 |
+
def _dict_list(self, other, num):
|
| 286 |
+
"""Returns an inclusive list of reference frames that connect this
|
| 287 |
+
reference frame to the provided reference frame.
|
| 288 |
+
|
| 289 |
+
Parameters
|
| 290 |
+
==========
|
| 291 |
+
other : ReferenceFrame
|
| 292 |
+
The other reference frame to look for a connecting relationship to.
|
| 293 |
+
num : integer
|
| 294 |
+
``0``, ``1``, and ``2`` will look for orientation, angular
|
| 295 |
+
velocity, and angular acceleration relationships between the two
|
| 296 |
+
frames, respectively.
|
| 297 |
+
|
| 298 |
+
Returns
|
| 299 |
+
=======
|
| 300 |
+
list
|
| 301 |
+
Inclusive list of reference frames that connect this reference
|
| 302 |
+
frame to the other reference frame.
|
| 303 |
+
|
| 304 |
+
Examples
|
| 305 |
+
========
|
| 306 |
+
|
| 307 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 308 |
+
>>> A = ReferenceFrame('A')
|
| 309 |
+
>>> B = ReferenceFrame('B')
|
| 310 |
+
>>> C = ReferenceFrame('C')
|
| 311 |
+
>>> D = ReferenceFrame('D')
|
| 312 |
+
>>> B.orient_axis(A, A.x, 1.0)
|
| 313 |
+
>>> C.orient_axis(B, B.x, 1.0)
|
| 314 |
+
>>> D.orient_axis(C, C.x, 1.0)
|
| 315 |
+
>>> D._dict_list(A, 0)
|
| 316 |
+
[D, C, B, A]
|
| 317 |
+
|
| 318 |
+
Raises
|
| 319 |
+
======
|
| 320 |
+
|
| 321 |
+
ValueError
|
| 322 |
+
When no path is found between the two reference frames or ``num``
|
| 323 |
+
is an incorrect value.
|
| 324 |
+
|
| 325 |
+
"""
|
| 326 |
+
|
| 327 |
+
connect_type = {0: 'orientation',
|
| 328 |
+
1: 'angular velocity',
|
| 329 |
+
2: 'angular acceleration'}
|
| 330 |
+
|
| 331 |
+
if num not in connect_type.keys():
|
| 332 |
+
raise ValueError('Valid values for num are 0, 1, or 2.')
|
| 333 |
+
|
| 334 |
+
possible_connecting_paths = [[self]]
|
| 335 |
+
oldlist = [[]]
|
| 336 |
+
while possible_connecting_paths != oldlist:
|
| 337 |
+
oldlist = possible_connecting_paths.copy()
|
| 338 |
+
for frame_list in possible_connecting_paths:
|
| 339 |
+
frames_adjacent_to_last = frame_list[-1]._dlist[num].keys()
|
| 340 |
+
for adjacent_frame in frames_adjacent_to_last:
|
| 341 |
+
if adjacent_frame not in frame_list:
|
| 342 |
+
connecting_path = frame_list + [adjacent_frame]
|
| 343 |
+
if connecting_path not in possible_connecting_paths:
|
| 344 |
+
possible_connecting_paths.append(connecting_path)
|
| 345 |
+
|
| 346 |
+
for connecting_path in oldlist:
|
| 347 |
+
if connecting_path[-1] != other:
|
| 348 |
+
possible_connecting_paths.remove(connecting_path)
|
| 349 |
+
possible_connecting_paths.sort(key=len)
|
| 350 |
+
|
| 351 |
+
if len(possible_connecting_paths) != 0:
|
| 352 |
+
return possible_connecting_paths[0] # selects the shortest path
|
| 353 |
+
|
| 354 |
+
msg = 'No connecting {} path found between {} and {}.'
|
| 355 |
+
raise ValueError(msg.format(connect_type[num], self.name, other.name))
|
| 356 |
+
|
| 357 |
+
def _w_diff_dcm(self, otherframe):
|
| 358 |
+
"""Angular velocity from time differentiating the DCM. """
|
| 359 |
+
from sympy.physics.vector.functions import dynamicsymbols
|
| 360 |
+
dcm2diff = otherframe.dcm(self)
|
| 361 |
+
diffed = dcm2diff.diff(dynamicsymbols._t)
|
| 362 |
+
angvelmat = diffed * dcm2diff.T
|
| 363 |
+
w1 = trigsimp(expand(angvelmat[7]), recursive=True)
|
| 364 |
+
w2 = trigsimp(expand(angvelmat[2]), recursive=True)
|
| 365 |
+
w3 = trigsimp(expand(angvelmat[3]), recursive=True)
|
| 366 |
+
return Vector([(Matrix([w1, w2, w3]), otherframe)])
|
| 367 |
+
|
| 368 |
+
def variable_map(self, otherframe):
|
| 369 |
+
"""
|
| 370 |
+
Returns a dictionary which expresses the coordinate variables
|
| 371 |
+
of this frame in terms of the variables of otherframe.
|
| 372 |
+
|
| 373 |
+
If Vector.simp is True, returns a simplified version of the mapped
|
| 374 |
+
values. Else, returns them without simplification.
|
| 375 |
+
|
| 376 |
+
Simplification of the expressions may take time.
|
| 377 |
+
|
| 378 |
+
Parameters
|
| 379 |
+
==========
|
| 380 |
+
|
| 381 |
+
otherframe : ReferenceFrame
|
| 382 |
+
The other frame to map the variables to
|
| 383 |
+
|
| 384 |
+
Examples
|
| 385 |
+
========
|
| 386 |
+
|
| 387 |
+
>>> from sympy.physics.vector import ReferenceFrame, dynamicsymbols
|
| 388 |
+
>>> A = ReferenceFrame('A')
|
| 389 |
+
>>> q = dynamicsymbols('q')
|
| 390 |
+
>>> B = A.orientnew('B', 'Axis', [q, A.z])
|
| 391 |
+
>>> A.variable_map(B)
|
| 392 |
+
{A_x: B_x*cos(q(t)) - B_y*sin(q(t)), A_y: B_x*sin(q(t)) + B_y*cos(q(t)), A_z: B_z}
|
| 393 |
+
|
| 394 |
+
"""
|
| 395 |
+
|
| 396 |
+
_check_frame(otherframe)
|
| 397 |
+
if (otherframe, Vector.simp) in self._var_dict:
|
| 398 |
+
return self._var_dict[(otherframe, Vector.simp)]
|
| 399 |
+
else:
|
| 400 |
+
vars_matrix = self.dcm(otherframe) * Matrix(otherframe.varlist)
|
| 401 |
+
mapping = {}
|
| 402 |
+
for i, x in enumerate(self):
|
| 403 |
+
if Vector.simp:
|
| 404 |
+
mapping[self.varlist[i]] = trigsimp(vars_matrix[i],
|
| 405 |
+
method='fu')
|
| 406 |
+
else:
|
| 407 |
+
mapping[self.varlist[i]] = vars_matrix[i]
|
| 408 |
+
self._var_dict[(otherframe, Vector.simp)] = mapping
|
| 409 |
+
return mapping
|
| 410 |
+
|
| 411 |
+
def ang_acc_in(self, otherframe):
|
| 412 |
+
"""Returns the angular acceleration Vector of the ReferenceFrame.
|
| 413 |
+
|
| 414 |
+
Effectively returns the Vector:
|
| 415 |
+
|
| 416 |
+
``N_alpha_B``
|
| 417 |
+
|
| 418 |
+
which represent the angular acceleration of B in N, where B is self,
|
| 419 |
+
and N is otherframe.
|
| 420 |
+
|
| 421 |
+
Parameters
|
| 422 |
+
==========
|
| 423 |
+
|
| 424 |
+
otherframe : ReferenceFrame
|
| 425 |
+
The ReferenceFrame which the angular acceleration is returned in.
|
| 426 |
+
|
| 427 |
+
Examples
|
| 428 |
+
========
|
| 429 |
+
|
| 430 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 431 |
+
>>> N = ReferenceFrame('N')
|
| 432 |
+
>>> A = ReferenceFrame('A')
|
| 433 |
+
>>> V = 10 * N.x
|
| 434 |
+
>>> A.set_ang_acc(N, V)
|
| 435 |
+
>>> A.ang_acc_in(N)
|
| 436 |
+
10*N.x
|
| 437 |
+
|
| 438 |
+
"""
|
| 439 |
+
|
| 440 |
+
_check_frame(otherframe)
|
| 441 |
+
if otherframe in self._ang_acc_dict:
|
| 442 |
+
return self._ang_acc_dict[otherframe]
|
| 443 |
+
else:
|
| 444 |
+
return self.ang_vel_in(otherframe).dt(otherframe)
|
| 445 |
+
|
| 446 |
+
def ang_vel_in(self, otherframe):
|
| 447 |
+
"""Returns the angular velocity Vector of the ReferenceFrame.
|
| 448 |
+
|
| 449 |
+
Effectively returns the Vector:
|
| 450 |
+
|
| 451 |
+
^N omega ^B
|
| 452 |
+
|
| 453 |
+
which represent the angular velocity of B in N, where B is self, and
|
| 454 |
+
N is otherframe.
|
| 455 |
+
|
| 456 |
+
Parameters
|
| 457 |
+
==========
|
| 458 |
+
|
| 459 |
+
otherframe : ReferenceFrame
|
| 460 |
+
The ReferenceFrame which the angular velocity is returned in.
|
| 461 |
+
|
| 462 |
+
Examples
|
| 463 |
+
========
|
| 464 |
+
|
| 465 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 466 |
+
>>> N = ReferenceFrame('N')
|
| 467 |
+
>>> A = ReferenceFrame('A')
|
| 468 |
+
>>> V = 10 * N.x
|
| 469 |
+
>>> A.set_ang_vel(N, V)
|
| 470 |
+
>>> A.ang_vel_in(N)
|
| 471 |
+
10*N.x
|
| 472 |
+
|
| 473 |
+
"""
|
| 474 |
+
|
| 475 |
+
_check_frame(otherframe)
|
| 476 |
+
flist = self._dict_list(otherframe, 1)
|
| 477 |
+
outvec = Vector(0)
|
| 478 |
+
for i in range(len(flist) - 1):
|
| 479 |
+
outvec += flist[i]._ang_vel_dict[flist[i + 1]]
|
| 480 |
+
return outvec
|
| 481 |
+
|
| 482 |
+
def dcm(self, otherframe):
|
| 483 |
+
r"""Returns the direction cosine matrix of this reference frame
|
| 484 |
+
relative to the provided reference frame.
|
| 485 |
+
|
| 486 |
+
The returned matrix can be used to express the orthogonal unit vectors
|
| 487 |
+
of this frame in terms of the orthogonal unit vectors of
|
| 488 |
+
``otherframe``.
|
| 489 |
+
|
| 490 |
+
Parameters
|
| 491 |
+
==========
|
| 492 |
+
|
| 493 |
+
otherframe : ReferenceFrame
|
| 494 |
+
The reference frame which the direction cosine matrix of this frame
|
| 495 |
+
is formed relative to.
|
| 496 |
+
|
| 497 |
+
Examples
|
| 498 |
+
========
|
| 499 |
+
|
| 500 |
+
The following example rotates the reference frame A relative to N by a
|
| 501 |
+
simple rotation and then calculates the direction cosine matrix of N
|
| 502 |
+
relative to A.
|
| 503 |
+
|
| 504 |
+
>>> from sympy import symbols, sin, cos
|
| 505 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 506 |
+
>>> q1 = symbols('q1')
|
| 507 |
+
>>> N = ReferenceFrame('N')
|
| 508 |
+
>>> A = ReferenceFrame('A')
|
| 509 |
+
>>> A.orient_axis(N, q1, N.x)
|
| 510 |
+
>>> N.dcm(A)
|
| 511 |
+
Matrix([
|
| 512 |
+
[1, 0, 0],
|
| 513 |
+
[0, cos(q1), -sin(q1)],
|
| 514 |
+
[0, sin(q1), cos(q1)]])
|
| 515 |
+
|
| 516 |
+
The second row of the above direction cosine matrix represents the
|
| 517 |
+
``N.y`` unit vector in N expressed in A. Like so:
|
| 518 |
+
|
| 519 |
+
>>> Ny = 0*A.x + cos(q1)*A.y - sin(q1)*A.z
|
| 520 |
+
|
| 521 |
+
Thus, expressing ``N.y`` in A should return the same result:
|
| 522 |
+
|
| 523 |
+
>>> N.y.express(A)
|
| 524 |
+
cos(q1)*A.y - sin(q1)*A.z
|
| 525 |
+
|
| 526 |
+
Notes
|
| 527 |
+
=====
|
| 528 |
+
|
| 529 |
+
It is important to know what form of the direction cosine matrix is
|
| 530 |
+
returned. If ``B.dcm(A)`` is called, it means the "direction cosine
|
| 531 |
+
matrix of B rotated relative to A". This is the matrix
|
| 532 |
+
:math:`{}^B\mathbf{C}^A` shown in the following relationship:
|
| 533 |
+
|
| 534 |
+
.. math::
|
| 535 |
+
|
| 536 |
+
\begin{bmatrix}
|
| 537 |
+
\hat{\mathbf{b}}_1 \\
|
| 538 |
+
\hat{\mathbf{b}}_2 \\
|
| 539 |
+
\hat{\mathbf{b}}_3
|
| 540 |
+
\end{bmatrix}
|
| 541 |
+
=
|
| 542 |
+
{}^B\mathbf{C}^A
|
| 543 |
+
\begin{bmatrix}
|
| 544 |
+
\hat{\mathbf{a}}_1 \\
|
| 545 |
+
\hat{\mathbf{a}}_2 \\
|
| 546 |
+
\hat{\mathbf{a}}_3
|
| 547 |
+
\end{bmatrix}.
|
| 548 |
+
|
| 549 |
+
:math:`{}^B\mathbf{C}^A` is the matrix that expresses the B unit
|
| 550 |
+
vectors in terms of the A unit vectors.
|
| 551 |
+
|
| 552 |
+
"""
|
| 553 |
+
|
| 554 |
+
_check_frame(otherframe)
|
| 555 |
+
# Check if the dcm wrt that frame has already been calculated
|
| 556 |
+
if otherframe in self._dcm_cache:
|
| 557 |
+
return self._dcm_cache[otherframe]
|
| 558 |
+
flist = self._dict_list(otherframe, 0)
|
| 559 |
+
outdcm = eye(3)
|
| 560 |
+
for i in range(len(flist) - 1):
|
| 561 |
+
outdcm = outdcm * flist[i]._dcm_dict[flist[i + 1]]
|
| 562 |
+
# After calculation, store the dcm in dcm cache for faster future
|
| 563 |
+
# retrieval
|
| 564 |
+
self._dcm_cache[otherframe] = outdcm
|
| 565 |
+
otherframe._dcm_cache[self] = outdcm.T
|
| 566 |
+
return outdcm
|
| 567 |
+
|
| 568 |
+
def _dcm(self, parent, parent_orient):
|
| 569 |
+
# If parent.oreint(self) is already defined,then
|
| 570 |
+
# update the _dcm_dict of parent while over write
|
| 571 |
+
# all content of self._dcm_dict and self._dcm_cache
|
| 572 |
+
# with new dcm relation.
|
| 573 |
+
# Else update _dcm_cache and _dcm_dict of both
|
| 574 |
+
# self and parent.
|
| 575 |
+
frames = self._dcm_cache.keys()
|
| 576 |
+
dcm_dict_del = []
|
| 577 |
+
dcm_cache_del = []
|
| 578 |
+
if parent in frames:
|
| 579 |
+
for frame in frames:
|
| 580 |
+
if frame in self._dcm_dict:
|
| 581 |
+
dcm_dict_del += [frame]
|
| 582 |
+
dcm_cache_del += [frame]
|
| 583 |
+
# Reset the _dcm_cache of this frame, and remove it from the
|
| 584 |
+
# _dcm_caches of the frames it is linked to. Also remove it from
|
| 585 |
+
# the _dcm_dict of its parent
|
| 586 |
+
for frame in dcm_dict_del:
|
| 587 |
+
del frame._dcm_dict[self]
|
| 588 |
+
for frame in dcm_cache_del:
|
| 589 |
+
del frame._dcm_cache[self]
|
| 590 |
+
# Reset the _dcm_dict
|
| 591 |
+
self._dcm_dict = self._dlist[0] = {}
|
| 592 |
+
# Reset the _dcm_cache
|
| 593 |
+
self._dcm_cache = {}
|
| 594 |
+
|
| 595 |
+
else:
|
| 596 |
+
# Check for loops and raise warning accordingly.
|
| 597 |
+
visited = []
|
| 598 |
+
queue = list(frames)
|
| 599 |
+
cont = True # Flag to control queue loop.
|
| 600 |
+
while queue and cont:
|
| 601 |
+
node = queue.pop(0)
|
| 602 |
+
if node not in visited:
|
| 603 |
+
visited.append(node)
|
| 604 |
+
neighbors = node._dcm_dict.keys()
|
| 605 |
+
for neighbor in neighbors:
|
| 606 |
+
if neighbor == parent:
|
| 607 |
+
warn('Loops are defined among the orientation of '
|
| 608 |
+
'frames. This is likely not desired and may '
|
| 609 |
+
'cause errors in your calculations.')
|
| 610 |
+
cont = False
|
| 611 |
+
break
|
| 612 |
+
queue.append(neighbor)
|
| 613 |
+
|
| 614 |
+
# Add the dcm relationship to _dcm_dict
|
| 615 |
+
self._dcm_dict.update({parent: parent_orient.T})
|
| 616 |
+
parent._dcm_dict.update({self: parent_orient})
|
| 617 |
+
# Update the dcm cache
|
| 618 |
+
self._dcm_cache.update({parent: parent_orient.T})
|
| 619 |
+
parent._dcm_cache.update({self: parent_orient})
|
| 620 |
+
|
| 621 |
+
def orient_axis(self, parent, axis, angle):
|
| 622 |
+
"""Sets the orientation of this reference frame with respect to a
|
| 623 |
+
parent reference frame by rotating through an angle about an axis fixed
|
| 624 |
+
in the parent reference frame.
|
| 625 |
+
|
| 626 |
+
Parameters
|
| 627 |
+
==========
|
| 628 |
+
|
| 629 |
+
parent : ReferenceFrame
|
| 630 |
+
Reference frame that this reference frame will be rotated relative
|
| 631 |
+
to.
|
| 632 |
+
axis : Vector
|
| 633 |
+
Vector fixed in the parent frame about about which this frame is
|
| 634 |
+
rotated. It need not be a unit vector and the rotation follows the
|
| 635 |
+
right hand rule.
|
| 636 |
+
angle : sympifiable
|
| 637 |
+
Angle in radians by which it the frame is to be rotated.
|
| 638 |
+
|
| 639 |
+
Warns
|
| 640 |
+
======
|
| 641 |
+
|
| 642 |
+
UserWarning
|
| 643 |
+
If the orientation creates a kinematic loop.
|
| 644 |
+
|
| 645 |
+
Examples
|
| 646 |
+
========
|
| 647 |
+
|
| 648 |
+
Setup variables for the examples:
|
| 649 |
+
|
| 650 |
+
>>> from sympy import symbols
|
| 651 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 652 |
+
>>> q1 = symbols('q1')
|
| 653 |
+
>>> N = ReferenceFrame('N')
|
| 654 |
+
>>> B = ReferenceFrame('B')
|
| 655 |
+
>>> B.orient_axis(N, N.x, q1)
|
| 656 |
+
|
| 657 |
+
The ``orient_axis()`` method generates a direction cosine matrix and
|
| 658 |
+
its transpose which defines the orientation of B relative to N and vice
|
| 659 |
+
versa. Once orient is called, ``dcm()`` outputs the appropriate
|
| 660 |
+
direction cosine matrix:
|
| 661 |
+
|
| 662 |
+
>>> B.dcm(N)
|
| 663 |
+
Matrix([
|
| 664 |
+
[1, 0, 0],
|
| 665 |
+
[0, cos(q1), sin(q1)],
|
| 666 |
+
[0, -sin(q1), cos(q1)]])
|
| 667 |
+
>>> N.dcm(B)
|
| 668 |
+
Matrix([
|
| 669 |
+
[1, 0, 0],
|
| 670 |
+
[0, cos(q1), -sin(q1)],
|
| 671 |
+
[0, sin(q1), cos(q1)]])
|
| 672 |
+
|
| 673 |
+
The following two lines show that the sense of the rotation can be
|
| 674 |
+
defined by negating the vector direction or the angle. Both lines
|
| 675 |
+
produce the same result.
|
| 676 |
+
|
| 677 |
+
>>> B.orient_axis(N, -N.x, q1)
|
| 678 |
+
>>> B.orient_axis(N, N.x, -q1)
|
| 679 |
+
|
| 680 |
+
"""
|
| 681 |
+
|
| 682 |
+
from sympy.physics.vector.functions import dynamicsymbols
|
| 683 |
+
_check_frame(parent)
|
| 684 |
+
|
| 685 |
+
if not isinstance(axis, Vector) and isinstance(angle, Vector):
|
| 686 |
+
axis, angle = angle, axis
|
| 687 |
+
|
| 688 |
+
axis = _check_vector(axis)
|
| 689 |
+
theta = sympify(angle)
|
| 690 |
+
|
| 691 |
+
if not axis.dt(parent) == 0:
|
| 692 |
+
raise ValueError('Axis cannot be time-varying.')
|
| 693 |
+
unit_axis = axis.express(parent).normalize()
|
| 694 |
+
unit_col = unit_axis.args[0][0]
|
| 695 |
+
parent_orient_axis = (
|
| 696 |
+
(eye(3) - unit_col * unit_col.T) * cos(theta) +
|
| 697 |
+
Matrix([[0, -unit_col[2], unit_col[1]],
|
| 698 |
+
[unit_col[2], 0, -unit_col[0]],
|
| 699 |
+
[-unit_col[1], unit_col[0], 0]]) *
|
| 700 |
+
sin(theta) + unit_col * unit_col.T)
|
| 701 |
+
|
| 702 |
+
self._dcm(parent, parent_orient_axis)
|
| 703 |
+
|
| 704 |
+
thetad = (theta).diff(dynamicsymbols._t)
|
| 705 |
+
wvec = thetad*axis.express(parent).normalize()
|
| 706 |
+
self._ang_vel_dict.update({parent: wvec})
|
| 707 |
+
parent._ang_vel_dict.update({self: -wvec})
|
| 708 |
+
self._var_dict = {}
|
| 709 |
+
|
| 710 |
+
def orient_explicit(self, parent, dcm):
|
| 711 |
+
"""Sets the orientation of this reference frame relative to another (parent) reference frame
|
| 712 |
+
using a direction cosine matrix that describes the rotation from the parent to the child.
|
| 713 |
+
|
| 714 |
+
Parameters
|
| 715 |
+
==========
|
| 716 |
+
|
| 717 |
+
parent : ReferenceFrame
|
| 718 |
+
Reference frame that this reference frame will be rotated relative
|
| 719 |
+
to.
|
| 720 |
+
dcm : Matrix, shape(3, 3)
|
| 721 |
+
Direction cosine matrix that specifies the relative rotation
|
| 722 |
+
between the two reference frames.
|
| 723 |
+
|
| 724 |
+
Warns
|
| 725 |
+
======
|
| 726 |
+
|
| 727 |
+
UserWarning
|
| 728 |
+
If the orientation creates a kinematic loop.
|
| 729 |
+
|
| 730 |
+
Examples
|
| 731 |
+
========
|
| 732 |
+
|
| 733 |
+
Setup variables for the examples:
|
| 734 |
+
|
| 735 |
+
>>> from sympy import symbols, Matrix, sin, cos
|
| 736 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 737 |
+
>>> q1 = symbols('q1')
|
| 738 |
+
>>> A = ReferenceFrame('A')
|
| 739 |
+
>>> B = ReferenceFrame('B')
|
| 740 |
+
>>> N = ReferenceFrame('N')
|
| 741 |
+
|
| 742 |
+
A simple rotation of ``A`` relative to ``N`` about ``N.x`` is defined
|
| 743 |
+
by the following direction cosine matrix:
|
| 744 |
+
|
| 745 |
+
>>> dcm = Matrix([[1, 0, 0],
|
| 746 |
+
... [0, cos(q1), -sin(q1)],
|
| 747 |
+
... [0, sin(q1), cos(q1)]])
|
| 748 |
+
>>> A.orient_explicit(N, dcm)
|
| 749 |
+
>>> A.dcm(N)
|
| 750 |
+
Matrix([
|
| 751 |
+
[1, 0, 0],
|
| 752 |
+
[0, cos(q1), sin(q1)],
|
| 753 |
+
[0, -sin(q1), cos(q1)]])
|
| 754 |
+
|
| 755 |
+
This is equivalent to using ``orient_axis()``:
|
| 756 |
+
|
| 757 |
+
>>> B.orient_axis(N, N.x, q1)
|
| 758 |
+
>>> B.dcm(N)
|
| 759 |
+
Matrix([
|
| 760 |
+
[1, 0, 0],
|
| 761 |
+
[0, cos(q1), sin(q1)],
|
| 762 |
+
[0, -sin(q1), cos(q1)]])
|
| 763 |
+
|
| 764 |
+
**Note carefully that** ``N.dcm(B)`` **(the transpose) would be passed
|
| 765 |
+
into** ``orient_explicit()`` **for** ``A.dcm(N)`` **to match**
|
| 766 |
+
``B.dcm(N)``:
|
| 767 |
+
|
| 768 |
+
>>> A.orient_explicit(N, N.dcm(B))
|
| 769 |
+
>>> A.dcm(N)
|
| 770 |
+
Matrix([
|
| 771 |
+
[1, 0, 0],
|
| 772 |
+
[0, cos(q1), sin(q1)],
|
| 773 |
+
[0, -sin(q1), cos(q1)]])
|
| 774 |
+
|
| 775 |
+
"""
|
| 776 |
+
_check_frame(parent)
|
| 777 |
+
# amounts must be a Matrix type object
|
| 778 |
+
# (e.g. sympy.matrices.dense.MutableDenseMatrix).
|
| 779 |
+
if not isinstance(dcm, MatrixBase):
|
| 780 |
+
raise TypeError("Amounts must be a SymPy Matrix type object.")
|
| 781 |
+
|
| 782 |
+
self.orient_dcm(parent, dcm.T)
|
| 783 |
+
|
| 784 |
+
def orient_dcm(self, parent, dcm):
|
| 785 |
+
"""Sets the orientation of this reference frame relative to another (parent) reference frame
|
| 786 |
+
using a direction cosine matrix that describes the rotation from the child to the parent.
|
| 787 |
+
|
| 788 |
+
Parameters
|
| 789 |
+
==========
|
| 790 |
+
|
| 791 |
+
parent : ReferenceFrame
|
| 792 |
+
Reference frame that this reference frame will be rotated relative
|
| 793 |
+
to.
|
| 794 |
+
dcm : Matrix, shape(3, 3)
|
| 795 |
+
Direction cosine matrix that specifies the relative rotation
|
| 796 |
+
between the two reference frames.
|
| 797 |
+
|
| 798 |
+
Warns
|
| 799 |
+
======
|
| 800 |
+
|
| 801 |
+
UserWarning
|
| 802 |
+
If the orientation creates a kinematic loop.
|
| 803 |
+
|
| 804 |
+
Examples
|
| 805 |
+
========
|
| 806 |
+
|
| 807 |
+
Setup variables for the examples:
|
| 808 |
+
|
| 809 |
+
>>> from sympy import symbols, Matrix, sin, cos
|
| 810 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 811 |
+
>>> q1 = symbols('q1')
|
| 812 |
+
>>> A = ReferenceFrame('A')
|
| 813 |
+
>>> B = ReferenceFrame('B')
|
| 814 |
+
>>> N = ReferenceFrame('N')
|
| 815 |
+
|
| 816 |
+
A simple rotation of ``A`` relative to ``N`` about ``N.x`` is defined
|
| 817 |
+
by the following direction cosine matrix:
|
| 818 |
+
|
| 819 |
+
>>> dcm = Matrix([[1, 0, 0],
|
| 820 |
+
... [0, cos(q1), sin(q1)],
|
| 821 |
+
... [0, -sin(q1), cos(q1)]])
|
| 822 |
+
>>> A.orient_dcm(N, dcm)
|
| 823 |
+
>>> A.dcm(N)
|
| 824 |
+
Matrix([
|
| 825 |
+
[1, 0, 0],
|
| 826 |
+
[0, cos(q1), sin(q1)],
|
| 827 |
+
[0, -sin(q1), cos(q1)]])
|
| 828 |
+
|
| 829 |
+
This is equivalent to using ``orient_axis()``:
|
| 830 |
+
|
| 831 |
+
>>> B.orient_axis(N, N.x, q1)
|
| 832 |
+
>>> B.dcm(N)
|
| 833 |
+
Matrix([
|
| 834 |
+
[1, 0, 0],
|
| 835 |
+
[0, cos(q1), sin(q1)],
|
| 836 |
+
[0, -sin(q1), cos(q1)]])
|
| 837 |
+
|
| 838 |
+
"""
|
| 839 |
+
|
| 840 |
+
_check_frame(parent)
|
| 841 |
+
# amounts must be a Matrix type object
|
| 842 |
+
# (e.g. sympy.matrices.dense.MutableDenseMatrix).
|
| 843 |
+
if not isinstance(dcm, MatrixBase):
|
| 844 |
+
raise TypeError("Amounts must be a SymPy Matrix type object.")
|
| 845 |
+
|
| 846 |
+
self._dcm(parent, dcm.T)
|
| 847 |
+
|
| 848 |
+
wvec = self._w_diff_dcm(parent)
|
| 849 |
+
self._ang_vel_dict.update({parent: wvec})
|
| 850 |
+
parent._ang_vel_dict.update({self: -wvec})
|
| 851 |
+
self._var_dict = {}
|
| 852 |
+
|
| 853 |
+
def _rot(self, axis, angle):
|
| 854 |
+
"""DCM for simple axis 1,2,or 3 rotations."""
|
| 855 |
+
if axis == 1:
|
| 856 |
+
return Matrix([[1, 0, 0],
|
| 857 |
+
[0, cos(angle), -sin(angle)],
|
| 858 |
+
[0, sin(angle), cos(angle)]])
|
| 859 |
+
elif axis == 2:
|
| 860 |
+
return Matrix([[cos(angle), 0, sin(angle)],
|
| 861 |
+
[0, 1, 0],
|
| 862 |
+
[-sin(angle), 0, cos(angle)]])
|
| 863 |
+
elif axis == 3:
|
| 864 |
+
return Matrix([[cos(angle), -sin(angle), 0],
|
| 865 |
+
[sin(angle), cos(angle), 0],
|
| 866 |
+
[0, 0, 1]])
|
| 867 |
+
|
| 868 |
+
def _parse_consecutive_rotations(self, angles, rotation_order):
|
| 869 |
+
"""Helper for orient_body_fixed and orient_space_fixed.
|
| 870 |
+
|
| 871 |
+
Parameters
|
| 872 |
+
==========
|
| 873 |
+
angles : 3-tuple of sympifiable
|
| 874 |
+
Three angles in radians used for the successive rotations.
|
| 875 |
+
rotation_order : 3 character string or 3 digit integer
|
| 876 |
+
Order of the rotations. The order can be specified by the strings
|
| 877 |
+
``'XZX'``, ``'131'``, or the integer ``131``. There are 12 unique
|
| 878 |
+
valid rotation orders.
|
| 879 |
+
|
| 880 |
+
Returns
|
| 881 |
+
=======
|
| 882 |
+
|
| 883 |
+
amounts : list
|
| 884 |
+
List of sympifiables corresponding to the rotation angles.
|
| 885 |
+
rot_order : list
|
| 886 |
+
List of integers corresponding to the axis of rotation.
|
| 887 |
+
rot_matrices : list
|
| 888 |
+
List of DCM around the given axis with corresponding magnitude.
|
| 889 |
+
|
| 890 |
+
"""
|
| 891 |
+
amounts = list(angles)
|
| 892 |
+
for i, v in enumerate(amounts):
|
| 893 |
+
if not isinstance(v, Vector):
|
| 894 |
+
amounts[i] = sympify(v)
|
| 895 |
+
|
| 896 |
+
approved_orders = ('123', '231', '312', '132', '213', '321', '121',
|
| 897 |
+
'131', '212', '232', '313', '323', '')
|
| 898 |
+
# make sure XYZ => 123
|
| 899 |
+
rot_order = translate(str(rotation_order), 'XYZxyz', '123123')
|
| 900 |
+
if rot_order not in approved_orders:
|
| 901 |
+
raise TypeError('The rotation order is not a valid order.')
|
| 902 |
+
|
| 903 |
+
rot_order = [int(r) for r in rot_order]
|
| 904 |
+
if not (len(amounts) == 3 & len(rot_order) == 3):
|
| 905 |
+
raise TypeError('Body orientation takes 3 values & 3 orders')
|
| 906 |
+
rot_matrices = [self._rot(order, amount)
|
| 907 |
+
for (order, amount) in zip(rot_order, amounts)]
|
| 908 |
+
return amounts, rot_order, rot_matrices
|
| 909 |
+
|
| 910 |
+
def orient_body_fixed(self, parent, angles, rotation_order):
|
| 911 |
+
"""Rotates this reference frame relative to the parent reference frame
|
| 912 |
+
by right hand rotating through three successive body fixed simple axis
|
| 913 |
+
rotations. Each subsequent axis of rotation is about the "body fixed"
|
| 914 |
+
unit vectors of a new intermediate reference frame. This type of
|
| 915 |
+
rotation is also referred to rotating through the `Euler and Tait-Bryan
|
| 916 |
+
Angles`_.
|
| 917 |
+
|
| 918 |
+
.. _Euler and Tait-Bryan Angles: https://en.wikipedia.org/wiki/Euler_angles
|
| 919 |
+
|
| 920 |
+
The computed angular velocity in this method is by default expressed in
|
| 921 |
+
the child's frame, so it is most preferable to use ``u1 * child.x + u2 *
|
| 922 |
+
child.y + u3 * child.z`` as generalized speeds.
|
| 923 |
+
|
| 924 |
+
Parameters
|
| 925 |
+
==========
|
| 926 |
+
|
| 927 |
+
parent : ReferenceFrame
|
| 928 |
+
Reference frame that this reference frame will be rotated relative
|
| 929 |
+
to.
|
| 930 |
+
angles : 3-tuple of sympifiable
|
| 931 |
+
Three angles in radians used for the successive rotations.
|
| 932 |
+
rotation_order : 3 character string or 3 digit integer
|
| 933 |
+
Order of the rotations about each intermediate reference frames'
|
| 934 |
+
unit vectors. The Euler rotation about the X, Z', X'' axes can be
|
| 935 |
+
specified by the strings ``'XZX'``, ``'131'``, or the integer
|
| 936 |
+
``131``. There are 12 unique valid rotation orders (6 Euler and 6
|
| 937 |
+
Tait-Bryan): zxz, xyx, yzy, zyz, xzx, yxy, xyz, yzx, zxy, xzy, zyx,
|
| 938 |
+
and yxz.
|
| 939 |
+
|
| 940 |
+
Warns
|
| 941 |
+
======
|
| 942 |
+
|
| 943 |
+
UserWarning
|
| 944 |
+
If the orientation creates a kinematic loop.
|
| 945 |
+
|
| 946 |
+
Examples
|
| 947 |
+
========
|
| 948 |
+
|
| 949 |
+
Setup variables for the examples:
|
| 950 |
+
|
| 951 |
+
>>> from sympy import symbols
|
| 952 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 953 |
+
>>> q1, q2, q3 = symbols('q1, q2, q3')
|
| 954 |
+
>>> N = ReferenceFrame('N')
|
| 955 |
+
>>> B = ReferenceFrame('B')
|
| 956 |
+
>>> B1 = ReferenceFrame('B1')
|
| 957 |
+
>>> B2 = ReferenceFrame('B2')
|
| 958 |
+
>>> B3 = ReferenceFrame('B3')
|
| 959 |
+
|
| 960 |
+
For example, a classic Euler Angle rotation can be done by:
|
| 961 |
+
|
| 962 |
+
>>> B.orient_body_fixed(N, (q1, q2, q3), 'XYX')
|
| 963 |
+
>>> B.dcm(N)
|
| 964 |
+
Matrix([
|
| 965 |
+
[ cos(q2), sin(q1)*sin(q2), -sin(q2)*cos(q1)],
|
| 966 |
+
[sin(q2)*sin(q3), -sin(q1)*sin(q3)*cos(q2) + cos(q1)*cos(q3), sin(q1)*cos(q3) + sin(q3)*cos(q1)*cos(q2)],
|
| 967 |
+
[sin(q2)*cos(q3), -sin(q1)*cos(q2)*cos(q3) - sin(q3)*cos(q1), -sin(q1)*sin(q3) + cos(q1)*cos(q2)*cos(q3)]])
|
| 968 |
+
|
| 969 |
+
This rotates reference frame B relative to reference frame N through
|
| 970 |
+
``q1`` about ``N.x``, then rotates B again through ``q2`` about
|
| 971 |
+
``B.y``, and finally through ``q3`` about ``B.x``. It is equivalent to
|
| 972 |
+
three successive ``orient_axis()`` calls:
|
| 973 |
+
|
| 974 |
+
>>> B1.orient_axis(N, N.x, q1)
|
| 975 |
+
>>> B2.orient_axis(B1, B1.y, q2)
|
| 976 |
+
>>> B3.orient_axis(B2, B2.x, q3)
|
| 977 |
+
>>> B3.dcm(N)
|
| 978 |
+
Matrix([
|
| 979 |
+
[ cos(q2), sin(q1)*sin(q2), -sin(q2)*cos(q1)],
|
| 980 |
+
[sin(q2)*sin(q3), -sin(q1)*sin(q3)*cos(q2) + cos(q1)*cos(q3), sin(q1)*cos(q3) + sin(q3)*cos(q1)*cos(q2)],
|
| 981 |
+
[sin(q2)*cos(q3), -sin(q1)*cos(q2)*cos(q3) - sin(q3)*cos(q1), -sin(q1)*sin(q3) + cos(q1)*cos(q2)*cos(q3)]])
|
| 982 |
+
|
| 983 |
+
Acceptable rotation orders are of length 3, expressed in as a string
|
| 984 |
+
``'XYZ'`` or ``'123'`` or integer ``123``. Rotations about an axis
|
| 985 |
+
twice in a row are prohibited.
|
| 986 |
+
|
| 987 |
+
>>> B.orient_body_fixed(N, (q1, q2, 0), 'ZXZ')
|
| 988 |
+
>>> B.orient_body_fixed(N, (q1, q2, 0), '121')
|
| 989 |
+
>>> B.orient_body_fixed(N, (q1, q2, q3), 123)
|
| 990 |
+
|
| 991 |
+
"""
|
| 992 |
+
from sympy.physics.vector.functions import dynamicsymbols
|
| 993 |
+
|
| 994 |
+
_check_frame(parent)
|
| 995 |
+
|
| 996 |
+
amounts, rot_order, rot_matrices = self._parse_consecutive_rotations(
|
| 997 |
+
angles, rotation_order)
|
| 998 |
+
self._dcm(parent, rot_matrices[0] * rot_matrices[1] * rot_matrices[2])
|
| 999 |
+
|
| 1000 |
+
rot_vecs = [zeros(3, 1) for _ in range(3)]
|
| 1001 |
+
for i, order in enumerate(rot_order):
|
| 1002 |
+
rot_vecs[i][order - 1] = amounts[i].diff(dynamicsymbols._t)
|
| 1003 |
+
u1, u2, u3 = rot_vecs[2] + rot_matrices[2].T * (
|
| 1004 |
+
rot_vecs[1] + rot_matrices[1].T * rot_vecs[0])
|
| 1005 |
+
wvec = u1 * self.x + u2 * self.y + u3 * self.z # There is a double -
|
| 1006 |
+
self._ang_vel_dict.update({parent: wvec})
|
| 1007 |
+
parent._ang_vel_dict.update({self: -wvec})
|
| 1008 |
+
self._var_dict = {}
|
| 1009 |
+
|
| 1010 |
+
def orient_space_fixed(self, parent, angles, rotation_order):
|
| 1011 |
+
"""Rotates this reference frame relative to the parent reference frame
|
| 1012 |
+
by right hand rotating through three successive space fixed simple axis
|
| 1013 |
+
rotations. Each subsequent axis of rotation is about the "space fixed"
|
| 1014 |
+
unit vectors of the parent reference frame.
|
| 1015 |
+
|
| 1016 |
+
The computed angular velocity in this method is by default expressed in
|
| 1017 |
+
the child's frame, so it is most preferable to use ``u1 * child.x + u2 *
|
| 1018 |
+
child.y + u3 * child.z`` as generalized speeds.
|
| 1019 |
+
|
| 1020 |
+
Parameters
|
| 1021 |
+
==========
|
| 1022 |
+
parent : ReferenceFrame
|
| 1023 |
+
Reference frame that this reference frame will be rotated relative
|
| 1024 |
+
to.
|
| 1025 |
+
angles : 3-tuple of sympifiable
|
| 1026 |
+
Three angles in radians used for the successive rotations.
|
| 1027 |
+
rotation_order : 3 character string or 3 digit integer
|
| 1028 |
+
Order of the rotations about the parent reference frame's unit
|
| 1029 |
+
vectors. The order can be specified by the strings ``'XZX'``,
|
| 1030 |
+
``'131'``, or the integer ``131``. There are 12 unique valid
|
| 1031 |
+
rotation orders.
|
| 1032 |
+
|
| 1033 |
+
Warns
|
| 1034 |
+
======
|
| 1035 |
+
|
| 1036 |
+
UserWarning
|
| 1037 |
+
If the orientation creates a kinematic loop.
|
| 1038 |
+
|
| 1039 |
+
Examples
|
| 1040 |
+
========
|
| 1041 |
+
|
| 1042 |
+
Setup variables for the examples:
|
| 1043 |
+
|
| 1044 |
+
>>> from sympy import symbols
|
| 1045 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 1046 |
+
>>> q1, q2, q3 = symbols('q1, q2, q3')
|
| 1047 |
+
>>> N = ReferenceFrame('N')
|
| 1048 |
+
>>> B = ReferenceFrame('B')
|
| 1049 |
+
>>> B1 = ReferenceFrame('B1')
|
| 1050 |
+
>>> B2 = ReferenceFrame('B2')
|
| 1051 |
+
>>> B3 = ReferenceFrame('B3')
|
| 1052 |
+
|
| 1053 |
+
>>> B.orient_space_fixed(N, (q1, q2, q3), '312')
|
| 1054 |
+
>>> B.dcm(N)
|
| 1055 |
+
Matrix([
|
| 1056 |
+
[ sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3), sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1)],
|
| 1057 |
+
[-sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1), cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3)],
|
| 1058 |
+
[ sin(q3)*cos(q2), -sin(q2), cos(q2)*cos(q3)]])
|
| 1059 |
+
|
| 1060 |
+
is equivalent to:
|
| 1061 |
+
|
| 1062 |
+
>>> B1.orient_axis(N, N.z, q1)
|
| 1063 |
+
>>> B2.orient_axis(B1, N.x, q2)
|
| 1064 |
+
>>> B3.orient_axis(B2, N.y, q3)
|
| 1065 |
+
>>> B3.dcm(N).simplify()
|
| 1066 |
+
Matrix([
|
| 1067 |
+
[ sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3), sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1)],
|
| 1068 |
+
[-sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1), cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3)],
|
| 1069 |
+
[ sin(q3)*cos(q2), -sin(q2), cos(q2)*cos(q3)]])
|
| 1070 |
+
|
| 1071 |
+
It is worth noting that space-fixed and body-fixed rotations are
|
| 1072 |
+
related by the order of the rotations, i.e. the reverse order of body
|
| 1073 |
+
fixed will give space fixed and vice versa.
|
| 1074 |
+
|
| 1075 |
+
>>> B.orient_space_fixed(N, (q1, q2, q3), '231')
|
| 1076 |
+
>>> B.dcm(N)
|
| 1077 |
+
Matrix([
|
| 1078 |
+
[cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3), -sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1)],
|
| 1079 |
+
[ -sin(q2), cos(q2)*cos(q3), sin(q3)*cos(q2)],
|
| 1080 |
+
[sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1), sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3)]])
|
| 1081 |
+
|
| 1082 |
+
>>> B.orient_body_fixed(N, (q3, q2, q1), '132')
|
| 1083 |
+
>>> B.dcm(N)
|
| 1084 |
+
Matrix([
|
| 1085 |
+
[cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3), -sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1)],
|
| 1086 |
+
[ -sin(q2), cos(q2)*cos(q3), sin(q3)*cos(q2)],
|
| 1087 |
+
[sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1), sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3)]])
|
| 1088 |
+
|
| 1089 |
+
"""
|
| 1090 |
+
from sympy.physics.vector.functions import dynamicsymbols
|
| 1091 |
+
|
| 1092 |
+
_check_frame(parent)
|
| 1093 |
+
|
| 1094 |
+
amounts, rot_order, rot_matrices = self._parse_consecutive_rotations(
|
| 1095 |
+
angles, rotation_order)
|
| 1096 |
+
self._dcm(parent, rot_matrices[2] * rot_matrices[1] * rot_matrices[0])
|
| 1097 |
+
|
| 1098 |
+
rot_vecs = [zeros(3, 1) for _ in range(3)]
|
| 1099 |
+
for i, order in enumerate(rot_order):
|
| 1100 |
+
rot_vecs[i][order - 1] = amounts[i].diff(dynamicsymbols._t)
|
| 1101 |
+
u1, u2, u3 = rot_vecs[0] + rot_matrices[0].T * (
|
| 1102 |
+
rot_vecs[1] + rot_matrices[1].T * rot_vecs[2])
|
| 1103 |
+
wvec = u1 * self.x + u2 * self.y + u3 * self.z # There is a double -
|
| 1104 |
+
self._ang_vel_dict.update({parent: wvec})
|
| 1105 |
+
parent._ang_vel_dict.update({self: -wvec})
|
| 1106 |
+
self._var_dict = {}
|
| 1107 |
+
|
| 1108 |
+
def orient_quaternion(self, parent, numbers):
|
| 1109 |
+
"""Sets the orientation of this reference frame relative to a parent
|
| 1110 |
+
reference frame via an orientation quaternion. An orientation
|
| 1111 |
+
quaternion is defined as a finite rotation a unit vector, ``(lambda_x,
|
| 1112 |
+
lambda_y, lambda_z)``, by an angle ``theta``. The orientation
|
| 1113 |
+
quaternion is described by four parameters:
|
| 1114 |
+
|
| 1115 |
+
- ``q0 = cos(theta/2)``
|
| 1116 |
+
- ``q1 = lambda_x*sin(theta/2)``
|
| 1117 |
+
- ``q2 = lambda_y*sin(theta/2)``
|
| 1118 |
+
- ``q3 = lambda_z*sin(theta/2)``
|
| 1119 |
+
|
| 1120 |
+
See `Quaternions and Spatial Rotation
|
| 1121 |
+
<https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation>`_ on
|
| 1122 |
+
Wikipedia for more information.
|
| 1123 |
+
|
| 1124 |
+
Parameters
|
| 1125 |
+
==========
|
| 1126 |
+
parent : ReferenceFrame
|
| 1127 |
+
Reference frame that this reference frame will be rotated relative
|
| 1128 |
+
to.
|
| 1129 |
+
numbers : 4-tuple of sympifiable
|
| 1130 |
+
The four quaternion scalar numbers as defined above: ``q0``,
|
| 1131 |
+
``q1``, ``q2``, ``q3``.
|
| 1132 |
+
|
| 1133 |
+
Warns
|
| 1134 |
+
======
|
| 1135 |
+
|
| 1136 |
+
UserWarning
|
| 1137 |
+
If the orientation creates a kinematic loop.
|
| 1138 |
+
|
| 1139 |
+
Examples
|
| 1140 |
+
========
|
| 1141 |
+
|
| 1142 |
+
Setup variables for the examples:
|
| 1143 |
+
|
| 1144 |
+
>>> from sympy import symbols
|
| 1145 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 1146 |
+
>>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
|
| 1147 |
+
>>> N = ReferenceFrame('N')
|
| 1148 |
+
>>> B = ReferenceFrame('B')
|
| 1149 |
+
|
| 1150 |
+
Set the orientation:
|
| 1151 |
+
|
| 1152 |
+
>>> B.orient_quaternion(N, (q0, q1, q2, q3))
|
| 1153 |
+
>>> B.dcm(N)
|
| 1154 |
+
Matrix([
|
| 1155 |
+
[q0**2 + q1**2 - q2**2 - q3**2, 2*q0*q3 + 2*q1*q2, -2*q0*q2 + 2*q1*q3],
|
| 1156 |
+
[ -2*q0*q3 + 2*q1*q2, q0**2 - q1**2 + q2**2 - q3**2, 2*q0*q1 + 2*q2*q3],
|
| 1157 |
+
[ 2*q0*q2 + 2*q1*q3, -2*q0*q1 + 2*q2*q3, q0**2 - q1**2 - q2**2 + q3**2]])
|
| 1158 |
+
|
| 1159 |
+
"""
|
| 1160 |
+
|
| 1161 |
+
from sympy.physics.vector.functions import dynamicsymbols
|
| 1162 |
+
_check_frame(parent)
|
| 1163 |
+
|
| 1164 |
+
numbers = list(numbers)
|
| 1165 |
+
for i, v in enumerate(numbers):
|
| 1166 |
+
if not isinstance(v, Vector):
|
| 1167 |
+
numbers[i] = sympify(v)
|
| 1168 |
+
|
| 1169 |
+
if not (isinstance(numbers, (list, tuple)) & (len(numbers) == 4)):
|
| 1170 |
+
raise TypeError('Amounts are a list or tuple of length 4')
|
| 1171 |
+
q0, q1, q2, q3 = numbers
|
| 1172 |
+
parent_orient_quaternion = (
|
| 1173 |
+
Matrix([[q0**2 + q1**2 - q2**2 - q3**2,
|
| 1174 |
+
2 * (q1 * q2 - q0 * q3),
|
| 1175 |
+
2 * (q0 * q2 + q1 * q3)],
|
| 1176 |
+
[2 * (q1 * q2 + q0 * q3),
|
| 1177 |
+
q0**2 - q1**2 + q2**2 - q3**2,
|
| 1178 |
+
2 * (q2 * q3 - q0 * q1)],
|
| 1179 |
+
[2 * (q1 * q3 - q0 * q2),
|
| 1180 |
+
2 * (q0 * q1 + q2 * q3),
|
| 1181 |
+
q0**2 - q1**2 - q2**2 + q3**2]]))
|
| 1182 |
+
|
| 1183 |
+
self._dcm(parent, parent_orient_quaternion)
|
| 1184 |
+
|
| 1185 |
+
t = dynamicsymbols._t
|
| 1186 |
+
q0, q1, q2, q3 = numbers
|
| 1187 |
+
q0d = diff(q0, t)
|
| 1188 |
+
q1d = diff(q1, t)
|
| 1189 |
+
q2d = diff(q2, t)
|
| 1190 |
+
q3d = diff(q3, t)
|
| 1191 |
+
w1 = 2 * (q1d * q0 + q2d * q3 - q3d * q2 - q0d * q1)
|
| 1192 |
+
w2 = 2 * (q2d * q0 + q3d * q1 - q1d * q3 - q0d * q2)
|
| 1193 |
+
w3 = 2 * (q3d * q0 + q1d * q2 - q2d * q1 - q0d * q3)
|
| 1194 |
+
wvec = Vector([(Matrix([w1, w2, w3]), self)])
|
| 1195 |
+
|
| 1196 |
+
self._ang_vel_dict.update({parent: wvec})
|
| 1197 |
+
parent._ang_vel_dict.update({self: -wvec})
|
| 1198 |
+
self._var_dict = {}
|
| 1199 |
+
|
| 1200 |
+
def orient(self, parent, rot_type, amounts, rot_order=''):
|
| 1201 |
+
"""Sets the orientation of this reference frame relative to another
|
| 1202 |
+
(parent) reference frame.
|
| 1203 |
+
|
| 1204 |
+
.. note:: It is now recommended to use the ``.orient_axis,
|
| 1205 |
+
.orient_body_fixed, .orient_space_fixed, .orient_quaternion``
|
| 1206 |
+
methods for the different rotation types.
|
| 1207 |
+
|
| 1208 |
+
Parameters
|
| 1209 |
+
==========
|
| 1210 |
+
|
| 1211 |
+
parent : ReferenceFrame
|
| 1212 |
+
Reference frame that this reference frame will be rotated relative
|
| 1213 |
+
to.
|
| 1214 |
+
rot_type : str
|
| 1215 |
+
The method used to generate the direction cosine matrix. Supported
|
| 1216 |
+
methods are:
|
| 1217 |
+
|
| 1218 |
+
- ``'Axis'``: simple rotations about a single common axis
|
| 1219 |
+
- ``'DCM'``: for setting the direction cosine matrix directly
|
| 1220 |
+
- ``'Body'``: three successive rotations about new intermediate
|
| 1221 |
+
axes, also called "Euler and Tait-Bryan angles"
|
| 1222 |
+
- ``'Space'``: three successive rotations about the parent
|
| 1223 |
+
frames' unit vectors
|
| 1224 |
+
- ``'Quaternion'``: rotations defined by four parameters which
|
| 1225 |
+
result in a singularity free direction cosine matrix
|
| 1226 |
+
|
| 1227 |
+
amounts :
|
| 1228 |
+
Expressions defining the rotation angles or direction cosine
|
| 1229 |
+
matrix. These must match the ``rot_type``. See examples below for
|
| 1230 |
+
details. The input types are:
|
| 1231 |
+
|
| 1232 |
+
- ``'Axis'``: 2-tuple (expr/sym/func, Vector)
|
| 1233 |
+
- ``'DCM'``: Matrix, shape(3,3)
|
| 1234 |
+
- ``'Body'``: 3-tuple of expressions, symbols, or functions
|
| 1235 |
+
- ``'Space'``: 3-tuple of expressions, symbols, or functions
|
| 1236 |
+
- ``'Quaternion'``: 4-tuple of expressions, symbols, or
|
| 1237 |
+
functions
|
| 1238 |
+
|
| 1239 |
+
rot_order : str or int, optional
|
| 1240 |
+
If applicable, the order of the successive of rotations. The string
|
| 1241 |
+
``'123'`` and integer ``123`` are equivalent, for example. Required
|
| 1242 |
+
for ``'Body'`` and ``'Space'``.
|
| 1243 |
+
|
| 1244 |
+
Warns
|
| 1245 |
+
======
|
| 1246 |
+
|
| 1247 |
+
UserWarning
|
| 1248 |
+
If the orientation creates a kinematic loop.
|
| 1249 |
+
|
| 1250 |
+
"""
|
| 1251 |
+
|
| 1252 |
+
_check_frame(parent)
|
| 1253 |
+
|
| 1254 |
+
approved_orders = ('123', '231', '312', '132', '213', '321', '121',
|
| 1255 |
+
'131', '212', '232', '313', '323', '')
|
| 1256 |
+
rot_order = translate(str(rot_order), 'XYZxyz', '123123')
|
| 1257 |
+
rot_type = rot_type.upper()
|
| 1258 |
+
|
| 1259 |
+
if rot_order not in approved_orders:
|
| 1260 |
+
raise TypeError('The supplied order is not an approved type')
|
| 1261 |
+
|
| 1262 |
+
if rot_type == 'AXIS':
|
| 1263 |
+
self.orient_axis(parent, amounts[1], amounts[0])
|
| 1264 |
+
|
| 1265 |
+
elif rot_type == 'DCM':
|
| 1266 |
+
self.orient_explicit(parent, amounts)
|
| 1267 |
+
|
| 1268 |
+
elif rot_type == 'BODY':
|
| 1269 |
+
self.orient_body_fixed(parent, amounts, rot_order)
|
| 1270 |
+
|
| 1271 |
+
elif rot_type == 'SPACE':
|
| 1272 |
+
self.orient_space_fixed(parent, amounts, rot_order)
|
| 1273 |
+
|
| 1274 |
+
elif rot_type == 'QUATERNION':
|
| 1275 |
+
self.orient_quaternion(parent, amounts)
|
| 1276 |
+
|
| 1277 |
+
else:
|
| 1278 |
+
raise NotImplementedError('That is not an implemented rotation')
|
| 1279 |
+
|
| 1280 |
+
def orientnew(self, newname, rot_type, amounts, rot_order='',
|
| 1281 |
+
variables=None, indices=None, latexs=None):
|
| 1282 |
+
r"""Returns a new reference frame oriented with respect to this
|
| 1283 |
+
reference frame.
|
| 1284 |
+
|
| 1285 |
+
See ``ReferenceFrame.orient()`` for detailed examples of how to orient
|
| 1286 |
+
reference frames.
|
| 1287 |
+
|
| 1288 |
+
Parameters
|
| 1289 |
+
==========
|
| 1290 |
+
|
| 1291 |
+
newname : str
|
| 1292 |
+
Name for the new reference frame.
|
| 1293 |
+
rot_type : str
|
| 1294 |
+
The method used to generate the direction cosine matrix. Supported
|
| 1295 |
+
methods are:
|
| 1296 |
+
|
| 1297 |
+
- ``'Axis'``: simple rotations about a single common axis
|
| 1298 |
+
- ``'DCM'``: for setting the direction cosine matrix directly
|
| 1299 |
+
- ``'Body'``: three successive rotations about new intermediate
|
| 1300 |
+
axes, also called "Euler and Tait-Bryan angles"
|
| 1301 |
+
- ``'Space'``: three successive rotations about the parent
|
| 1302 |
+
frames' unit vectors
|
| 1303 |
+
- ``'Quaternion'``: rotations defined by four parameters which
|
| 1304 |
+
result in a singularity free direction cosine matrix
|
| 1305 |
+
|
| 1306 |
+
amounts :
|
| 1307 |
+
Expressions defining the rotation angles or direction cosine
|
| 1308 |
+
matrix. These must match the ``rot_type``. See examples below for
|
| 1309 |
+
details. The input types are:
|
| 1310 |
+
|
| 1311 |
+
- ``'Axis'``: 2-tuple (expr/sym/func, Vector)
|
| 1312 |
+
- ``'DCM'``: Matrix, shape(3,3)
|
| 1313 |
+
- ``'Body'``: 3-tuple of expressions, symbols, or functions
|
| 1314 |
+
- ``'Space'``: 3-tuple of expressions, symbols, or functions
|
| 1315 |
+
- ``'Quaternion'``: 4-tuple of expressions, symbols, or
|
| 1316 |
+
functions
|
| 1317 |
+
|
| 1318 |
+
rot_order : str or int, optional
|
| 1319 |
+
If applicable, the order of the successive of rotations. The string
|
| 1320 |
+
``'123'`` and integer ``123`` are equivalent, for example. Required
|
| 1321 |
+
for ``'Body'`` and ``'Space'``.
|
| 1322 |
+
indices : tuple of str
|
| 1323 |
+
Enables the reference frame's basis unit vectors to be accessed by
|
| 1324 |
+
Python's square bracket indexing notation using the provided three
|
| 1325 |
+
indice strings and alters the printing of the unit vectors to
|
| 1326 |
+
reflect this choice.
|
| 1327 |
+
latexs : tuple of str
|
| 1328 |
+
Alters the LaTeX printing of the reference frame's basis unit
|
| 1329 |
+
vectors to the provided three valid LaTeX strings.
|
| 1330 |
+
|
| 1331 |
+
Examples
|
| 1332 |
+
========
|
| 1333 |
+
|
| 1334 |
+
>>> from sympy import symbols
|
| 1335 |
+
>>> from sympy.physics.vector import ReferenceFrame, vlatex
|
| 1336 |
+
>>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
|
| 1337 |
+
>>> N = ReferenceFrame('N')
|
| 1338 |
+
|
| 1339 |
+
Create a new reference frame A rotated relative to N through a simple
|
| 1340 |
+
rotation.
|
| 1341 |
+
|
| 1342 |
+
>>> A = N.orientnew('A', 'Axis', (q0, N.x))
|
| 1343 |
+
|
| 1344 |
+
Create a new reference frame B rotated relative to N through body-fixed
|
| 1345 |
+
rotations.
|
| 1346 |
+
|
| 1347 |
+
>>> B = N.orientnew('B', 'Body', (q1, q2, q3), '123')
|
| 1348 |
+
|
| 1349 |
+
Create a new reference frame C rotated relative to N through a simple
|
| 1350 |
+
rotation with unique indices and LaTeX printing.
|
| 1351 |
+
|
| 1352 |
+
>>> C = N.orientnew('C', 'Axis', (q0, N.x), indices=('1', '2', '3'),
|
| 1353 |
+
... latexs=(r'\hat{\mathbf{c}}_1',r'\hat{\mathbf{c}}_2',
|
| 1354 |
+
... r'\hat{\mathbf{c}}_3'))
|
| 1355 |
+
>>> C['1']
|
| 1356 |
+
C['1']
|
| 1357 |
+
>>> print(vlatex(C['1']))
|
| 1358 |
+
\hat{\mathbf{c}}_1
|
| 1359 |
+
|
| 1360 |
+
"""
|
| 1361 |
+
|
| 1362 |
+
newframe = self.__class__(newname, variables=variables,
|
| 1363 |
+
indices=indices, latexs=latexs)
|
| 1364 |
+
|
| 1365 |
+
approved_orders = ('123', '231', '312', '132', '213', '321', '121',
|
| 1366 |
+
'131', '212', '232', '313', '323', '')
|
| 1367 |
+
rot_order = translate(str(rot_order), 'XYZxyz', '123123')
|
| 1368 |
+
rot_type = rot_type.upper()
|
| 1369 |
+
|
| 1370 |
+
if rot_order not in approved_orders:
|
| 1371 |
+
raise TypeError('The supplied order is not an approved type')
|
| 1372 |
+
|
| 1373 |
+
if rot_type == 'AXIS':
|
| 1374 |
+
newframe.orient_axis(self, amounts[1], amounts[0])
|
| 1375 |
+
|
| 1376 |
+
elif rot_type == 'DCM':
|
| 1377 |
+
newframe.orient_explicit(self, amounts)
|
| 1378 |
+
|
| 1379 |
+
elif rot_type == 'BODY':
|
| 1380 |
+
newframe.orient_body_fixed(self, amounts, rot_order)
|
| 1381 |
+
|
| 1382 |
+
elif rot_type == 'SPACE':
|
| 1383 |
+
newframe.orient_space_fixed(self, amounts, rot_order)
|
| 1384 |
+
|
| 1385 |
+
elif rot_type == 'QUATERNION':
|
| 1386 |
+
newframe.orient_quaternion(self, amounts)
|
| 1387 |
+
|
| 1388 |
+
else:
|
| 1389 |
+
raise NotImplementedError('That is not an implemented rotation')
|
| 1390 |
+
return newframe
|
| 1391 |
+
|
| 1392 |
+
def set_ang_acc(self, otherframe, value):
|
| 1393 |
+
"""Define the angular acceleration Vector in a ReferenceFrame.
|
| 1394 |
+
|
| 1395 |
+
Defines the angular acceleration of this ReferenceFrame, in another.
|
| 1396 |
+
Angular acceleration can be defined with respect to multiple different
|
| 1397 |
+
ReferenceFrames. Care must be taken to not create loops which are
|
| 1398 |
+
inconsistent.
|
| 1399 |
+
|
| 1400 |
+
Parameters
|
| 1401 |
+
==========
|
| 1402 |
+
|
| 1403 |
+
otherframe : ReferenceFrame
|
| 1404 |
+
A ReferenceFrame to define the angular acceleration in
|
| 1405 |
+
value : Vector
|
| 1406 |
+
The Vector representing angular acceleration
|
| 1407 |
+
|
| 1408 |
+
Examples
|
| 1409 |
+
========
|
| 1410 |
+
|
| 1411 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 1412 |
+
>>> N = ReferenceFrame('N')
|
| 1413 |
+
>>> A = ReferenceFrame('A')
|
| 1414 |
+
>>> V = 10 * N.x
|
| 1415 |
+
>>> A.set_ang_acc(N, V)
|
| 1416 |
+
>>> A.ang_acc_in(N)
|
| 1417 |
+
10*N.x
|
| 1418 |
+
|
| 1419 |
+
"""
|
| 1420 |
+
|
| 1421 |
+
if value == 0:
|
| 1422 |
+
value = Vector(0)
|
| 1423 |
+
value = _check_vector(value)
|
| 1424 |
+
_check_frame(otherframe)
|
| 1425 |
+
self._ang_acc_dict.update({otherframe: value})
|
| 1426 |
+
otherframe._ang_acc_dict.update({self: -value})
|
| 1427 |
+
|
| 1428 |
+
def set_ang_vel(self, otherframe, value):
|
| 1429 |
+
"""Define the angular velocity vector in a ReferenceFrame.
|
| 1430 |
+
|
| 1431 |
+
Defines the angular velocity of this ReferenceFrame, in another.
|
| 1432 |
+
Angular velocity can be defined with respect to multiple different
|
| 1433 |
+
ReferenceFrames. Care must be taken to not create loops which are
|
| 1434 |
+
inconsistent.
|
| 1435 |
+
|
| 1436 |
+
Parameters
|
| 1437 |
+
==========
|
| 1438 |
+
|
| 1439 |
+
otherframe : ReferenceFrame
|
| 1440 |
+
A ReferenceFrame to define the angular velocity in
|
| 1441 |
+
value : Vector
|
| 1442 |
+
The Vector representing angular velocity
|
| 1443 |
+
|
| 1444 |
+
Examples
|
| 1445 |
+
========
|
| 1446 |
+
|
| 1447 |
+
>>> from sympy.physics.vector import ReferenceFrame
|
| 1448 |
+
>>> N = ReferenceFrame('N')
|
| 1449 |
+
>>> A = ReferenceFrame('A')
|
| 1450 |
+
>>> V = 10 * N.x
|
| 1451 |
+
>>> A.set_ang_vel(N, V)
|
| 1452 |
+
>>> A.ang_vel_in(N)
|
| 1453 |
+
10*N.x
|
| 1454 |
+
|
| 1455 |
+
"""
|
| 1456 |
+
|
| 1457 |
+
if value == 0:
|
| 1458 |
+
value = Vector(0)
|
| 1459 |
+
value = _check_vector(value)
|
| 1460 |
+
_check_frame(otherframe)
|
| 1461 |
+
self._ang_vel_dict.update({otherframe: value})
|
| 1462 |
+
otherframe._ang_vel_dict.update({self: -value})
|
| 1463 |
+
|
| 1464 |
+
@property
|
| 1465 |
+
def x(self):
|
| 1466 |
+
"""The basis Vector for the ReferenceFrame, in the x direction. """
|
| 1467 |
+
return self._x
|
| 1468 |
+
|
| 1469 |
+
@property
|
| 1470 |
+
def y(self):
|
| 1471 |
+
"""The basis Vector for the ReferenceFrame, in the y direction. """
|
| 1472 |
+
return self._y
|
| 1473 |
+
|
| 1474 |
+
@property
|
| 1475 |
+
def z(self):
|
| 1476 |
+
"""The basis Vector for the ReferenceFrame, in the z direction. """
|
| 1477 |
+
return self._z
|
| 1478 |
+
|
| 1479 |
+
@property
|
| 1480 |
+
def xx(self):
|
| 1481 |
+
"""Unit dyad of basis Vectors x and x for the ReferenceFrame."""
|
| 1482 |
+
return Vector.outer(self.x, self.x)
|
| 1483 |
+
|
| 1484 |
+
@property
|
| 1485 |
+
def xy(self):
|
| 1486 |
+
"""Unit dyad of basis Vectors x and y for the ReferenceFrame."""
|
| 1487 |
+
return Vector.outer(self.x, self.y)
|
| 1488 |
+
|
| 1489 |
+
@property
|
| 1490 |
+
def xz(self):
|
| 1491 |
+
"""Unit dyad of basis Vectors x and z for the ReferenceFrame."""
|
| 1492 |
+
return Vector.outer(self.x, self.z)
|
| 1493 |
+
|
| 1494 |
+
@property
|
| 1495 |
+
def yx(self):
|
| 1496 |
+
"""Unit dyad of basis Vectors y and x for the ReferenceFrame."""
|
| 1497 |
+
return Vector.outer(self.y, self.x)
|
| 1498 |
+
|
| 1499 |
+
@property
|
| 1500 |
+
def yy(self):
|
| 1501 |
+
"""Unit dyad of basis Vectors y and y for the ReferenceFrame."""
|
| 1502 |
+
return Vector.outer(self.y, self.y)
|
| 1503 |
+
|
| 1504 |
+
@property
|
| 1505 |
+
def yz(self):
|
| 1506 |
+
"""Unit dyad of basis Vectors y and z for the ReferenceFrame."""
|
| 1507 |
+
return Vector.outer(self.y, self.z)
|
| 1508 |
+
|
| 1509 |
+
@property
|
| 1510 |
+
def zx(self):
|
| 1511 |
+
"""Unit dyad of basis Vectors z and x for the ReferenceFrame."""
|
| 1512 |
+
return Vector.outer(self.z, self.x)
|
| 1513 |
+
|
| 1514 |
+
@property
|
| 1515 |
+
def zy(self):
|
| 1516 |
+
"""Unit dyad of basis Vectors z and y for the ReferenceFrame."""
|
| 1517 |
+
return Vector.outer(self.z, self.y)
|
| 1518 |
+
|
| 1519 |
+
@property
|
| 1520 |
+
def zz(self):
|
| 1521 |
+
"""Unit dyad of basis Vectors z and z for the ReferenceFrame."""
|
| 1522 |
+
return Vector.outer(self.z, self.z)
|
| 1523 |
+
|
| 1524 |
+
@property
|
| 1525 |
+
def u(self):
|
| 1526 |
+
"""Unit dyadic for the ReferenceFrame."""
|
| 1527 |
+
return self.xx + self.yy + self.zz
|
| 1528 |
+
|
| 1529 |
+
def partial_velocity(self, frame, *gen_speeds):
|
| 1530 |
+
"""Returns the partial angular velocities of this frame in the given
|
| 1531 |
+
frame with respect to one or more provided generalized speeds.
|
| 1532 |
+
|
| 1533 |
+
Parameters
|
| 1534 |
+
==========
|
| 1535 |
+
frame : ReferenceFrame
|
| 1536 |
+
The frame with which the angular velocity is defined in.
|
| 1537 |
+
gen_speeds : functions of time
|
| 1538 |
+
The generalized speeds.
|
| 1539 |
+
|
| 1540 |
+
Returns
|
| 1541 |
+
=======
|
| 1542 |
+
partial_velocities : tuple of Vector
|
| 1543 |
+
The partial angular velocity vectors corresponding to the provided
|
| 1544 |
+
generalized speeds.
|
| 1545 |
+
|
| 1546 |
+
Examples
|
| 1547 |
+
========
|
| 1548 |
+
|
| 1549 |
+
>>> from sympy.physics.vector import ReferenceFrame, dynamicsymbols
|
| 1550 |
+
>>> N = ReferenceFrame('N')
|
| 1551 |
+
>>> A = ReferenceFrame('A')
|
| 1552 |
+
>>> u1, u2 = dynamicsymbols('u1, u2')
|
| 1553 |
+
>>> A.set_ang_vel(N, u1 * A.x + u2 * N.y)
|
| 1554 |
+
>>> A.partial_velocity(N, u1)
|
| 1555 |
+
A.x
|
| 1556 |
+
>>> A.partial_velocity(N, u1, u2)
|
| 1557 |
+
(A.x, N.y)
|
| 1558 |
+
|
| 1559 |
+
"""
|
| 1560 |
+
|
| 1561 |
+
from sympy.physics.vector.functions import partial_velocity
|
| 1562 |
+
|
| 1563 |
+
vel = self.ang_vel_in(frame)
|
| 1564 |
+
partials = partial_velocity([vel], gen_speeds, frame)[0]
|
| 1565 |
+
|
| 1566 |
+
if len(partials) == 1:
|
| 1567 |
+
return partials[0]
|
| 1568 |
+
else:
|
| 1569 |
+
return tuple(partials)
|
| 1570 |
+
|
| 1571 |
+
|
| 1572 |
+
def _check_frame(other):
|
| 1573 |
+
from .vector import VectorTypeError
|
| 1574 |
+
if not isinstance(other, ReferenceFrame):
|
| 1575 |
+
raise VectorTypeError(other, ReferenceFrame('A'))
|
hug/lib/python3.12/site-packages/sympy/physics/vector/functions.py
ADDED
|
@@ -0,0 +1,650 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from functools import reduce
|
| 2 |
+
|
| 3 |
+
from sympy import (sympify, diff, sin, cos, Matrix, symbols,
|
| 4 |
+
Function, S, Symbol, linear_eq_to_matrix)
|
| 5 |
+
from sympy.integrals.integrals import integrate
|
| 6 |
+
from sympy.simplify.trigsimp import trigsimp
|
| 7 |
+
from .vector import Vector, _check_vector
|
| 8 |
+
from .frame import CoordinateSym, _check_frame
|
| 9 |
+
from .dyadic import Dyadic
|
| 10 |
+
from .printing import vprint, vsprint, vpprint, vlatex, init_vprinting
|
| 11 |
+
from sympy.utilities.iterables import iterable
|
| 12 |
+
from sympy.utilities.misc import translate
|
| 13 |
+
|
| 14 |
+
__all__ = ['cross', 'dot', 'express', 'time_derivative', 'outer',
|
| 15 |
+
'kinematic_equations', 'get_motion_params', 'partial_velocity',
|
| 16 |
+
'dynamicsymbols', 'vprint', 'vsprint', 'vpprint', 'vlatex',
|
| 17 |
+
'init_vprinting']
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def cross(vec1, vec2):
|
| 21 |
+
"""Cross product convenience wrapper for Vector.cross(): \n"""
|
| 22 |
+
if not isinstance(vec1, (Vector, Dyadic)):
|
| 23 |
+
raise TypeError('Cross product is between two vectors')
|
| 24 |
+
return vec1 ^ vec2
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
cross.__doc__ += Vector.cross.__doc__ # type: ignore
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def dot(vec1, vec2):
|
| 31 |
+
"""Dot product convenience wrapper for Vector.dot(): \n"""
|
| 32 |
+
if not isinstance(vec1, (Vector, Dyadic)):
|
| 33 |
+
raise TypeError('Dot product is between two vectors')
|
| 34 |
+
return vec1 & vec2
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
dot.__doc__ += Vector.dot.__doc__ # type: ignore
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def express(expr, frame, frame2=None, variables=False):
|
| 41 |
+
"""
|
| 42 |
+
Global function for 'express' functionality.
|
| 43 |
+
|
| 44 |
+
Re-expresses a Vector, scalar(sympyfiable) or Dyadic in given frame.
|
| 45 |
+
|
| 46 |
+
Refer to the local methods of Vector and Dyadic for details.
|
| 47 |
+
If 'variables' is True, then the coordinate variables (CoordinateSym
|
| 48 |
+
instances) of other frames present in the vector/scalar field or
|
| 49 |
+
dyadic expression are also substituted in terms of the base scalars of
|
| 50 |
+
this frame.
|
| 51 |
+
|
| 52 |
+
Parameters
|
| 53 |
+
==========
|
| 54 |
+
|
| 55 |
+
expr : Vector/Dyadic/scalar(sympyfiable)
|
| 56 |
+
The expression to re-express in ReferenceFrame 'frame'
|
| 57 |
+
|
| 58 |
+
frame: ReferenceFrame
|
| 59 |
+
The reference frame to express expr in
|
| 60 |
+
|
| 61 |
+
frame2 : ReferenceFrame
|
| 62 |
+
The other frame required for re-expression(only for Dyadic expr)
|
| 63 |
+
|
| 64 |
+
variables : boolean
|
| 65 |
+
Specifies whether to substitute the coordinate variables present
|
| 66 |
+
in expr, in terms of those of frame
|
| 67 |
+
|
| 68 |
+
Examples
|
| 69 |
+
========
|
| 70 |
+
|
| 71 |
+
>>> from sympy.physics.vector import ReferenceFrame, outer, dynamicsymbols
|
| 72 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 73 |
+
>>> init_vprinting(pretty_print=False)
|
| 74 |
+
>>> N = ReferenceFrame('N')
|
| 75 |
+
>>> q = dynamicsymbols('q')
|
| 76 |
+
>>> B = N.orientnew('B', 'Axis', [q, N.z])
|
| 77 |
+
>>> d = outer(N.x, N.x)
|
| 78 |
+
>>> from sympy.physics.vector import express
|
| 79 |
+
>>> express(d, B, N)
|
| 80 |
+
cos(q)*(B.x|N.x) - sin(q)*(B.y|N.x)
|
| 81 |
+
>>> express(B.x, N)
|
| 82 |
+
cos(q)*N.x + sin(q)*N.y
|
| 83 |
+
>>> express(N[0], B, variables=True)
|
| 84 |
+
B_x*cos(q) - B_y*sin(q)
|
| 85 |
+
|
| 86 |
+
"""
|
| 87 |
+
|
| 88 |
+
_check_frame(frame)
|
| 89 |
+
|
| 90 |
+
if expr == 0:
|
| 91 |
+
return expr
|
| 92 |
+
|
| 93 |
+
if isinstance(expr, Vector):
|
| 94 |
+
# Given expr is a Vector
|
| 95 |
+
if variables:
|
| 96 |
+
# If variables attribute is True, substitute the coordinate
|
| 97 |
+
# variables in the Vector
|
| 98 |
+
frame_list = [x[-1] for x in expr.args]
|
| 99 |
+
subs_dict = {}
|
| 100 |
+
for f in frame_list:
|
| 101 |
+
subs_dict.update(f.variable_map(frame))
|
| 102 |
+
expr = expr.subs(subs_dict)
|
| 103 |
+
# Re-express in this frame
|
| 104 |
+
outvec = Vector([])
|
| 105 |
+
for v in expr.args:
|
| 106 |
+
if v[1] != frame:
|
| 107 |
+
temp = frame.dcm(v[1]) * v[0]
|
| 108 |
+
if Vector.simp:
|
| 109 |
+
temp = temp.applyfunc(lambda x:
|
| 110 |
+
trigsimp(x, method='fu'))
|
| 111 |
+
outvec += Vector([(temp, frame)])
|
| 112 |
+
else:
|
| 113 |
+
outvec += Vector([v])
|
| 114 |
+
return outvec
|
| 115 |
+
|
| 116 |
+
if isinstance(expr, Dyadic):
|
| 117 |
+
if frame2 is None:
|
| 118 |
+
frame2 = frame
|
| 119 |
+
_check_frame(frame2)
|
| 120 |
+
ol = Dyadic(0)
|
| 121 |
+
for v in expr.args:
|
| 122 |
+
ol += express(v[0], frame, variables=variables) * \
|
| 123 |
+
(express(v[1], frame, variables=variables) |
|
| 124 |
+
express(v[2], frame2, variables=variables))
|
| 125 |
+
return ol
|
| 126 |
+
|
| 127 |
+
else:
|
| 128 |
+
if variables:
|
| 129 |
+
# Given expr is a scalar field
|
| 130 |
+
frame_set = set()
|
| 131 |
+
expr = sympify(expr)
|
| 132 |
+
# Substitute all the coordinate variables
|
| 133 |
+
for x in expr.free_symbols:
|
| 134 |
+
if isinstance(x, CoordinateSym) and x.frame != frame:
|
| 135 |
+
frame_set.add(x.frame)
|
| 136 |
+
subs_dict = {}
|
| 137 |
+
for f in frame_set:
|
| 138 |
+
subs_dict.update(f.variable_map(frame))
|
| 139 |
+
return expr.subs(subs_dict)
|
| 140 |
+
return expr
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def time_derivative(expr, frame, order=1):
|
| 144 |
+
"""
|
| 145 |
+
Calculate the time derivative of a vector/scalar field function
|
| 146 |
+
or dyadic expression in given frame.
|
| 147 |
+
|
| 148 |
+
References
|
| 149 |
+
==========
|
| 150 |
+
|
| 151 |
+
https://en.wikipedia.org/wiki/Rotating_reference_frame#Time_derivatives_in_the_two_frames
|
| 152 |
+
|
| 153 |
+
Parameters
|
| 154 |
+
==========
|
| 155 |
+
|
| 156 |
+
expr : Vector/Dyadic/sympifyable
|
| 157 |
+
The expression whose time derivative is to be calculated
|
| 158 |
+
|
| 159 |
+
frame : ReferenceFrame
|
| 160 |
+
The reference frame to calculate the time derivative in
|
| 161 |
+
|
| 162 |
+
order : integer
|
| 163 |
+
The order of the derivative to be calculated
|
| 164 |
+
|
| 165 |
+
Examples
|
| 166 |
+
========
|
| 167 |
+
|
| 168 |
+
>>> from sympy.physics.vector import ReferenceFrame, dynamicsymbols
|
| 169 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 170 |
+
>>> init_vprinting(pretty_print=False)
|
| 171 |
+
>>> from sympy import Symbol
|
| 172 |
+
>>> q1 = Symbol('q1')
|
| 173 |
+
>>> u1 = dynamicsymbols('u1')
|
| 174 |
+
>>> N = ReferenceFrame('N')
|
| 175 |
+
>>> A = N.orientnew('A', 'Axis', [q1, N.x])
|
| 176 |
+
>>> v = u1 * N.x
|
| 177 |
+
>>> A.set_ang_vel(N, 10*A.x)
|
| 178 |
+
>>> from sympy.physics.vector import time_derivative
|
| 179 |
+
>>> time_derivative(v, N)
|
| 180 |
+
u1'*N.x
|
| 181 |
+
>>> time_derivative(u1*A[0], N)
|
| 182 |
+
N_x*u1'
|
| 183 |
+
>>> B = N.orientnew('B', 'Axis', [u1, N.z])
|
| 184 |
+
>>> from sympy.physics.vector import outer
|
| 185 |
+
>>> d = outer(N.x, N.x)
|
| 186 |
+
>>> time_derivative(d, B)
|
| 187 |
+
- u1'*(N.y|N.x) - u1'*(N.x|N.y)
|
| 188 |
+
|
| 189 |
+
"""
|
| 190 |
+
|
| 191 |
+
t = dynamicsymbols._t
|
| 192 |
+
_check_frame(frame)
|
| 193 |
+
|
| 194 |
+
if order == 0:
|
| 195 |
+
return expr
|
| 196 |
+
if order % 1 != 0 or order < 0:
|
| 197 |
+
raise ValueError("Unsupported value of order entered")
|
| 198 |
+
|
| 199 |
+
if isinstance(expr, Vector):
|
| 200 |
+
outlist = []
|
| 201 |
+
for v in expr.args:
|
| 202 |
+
if v[1] == frame:
|
| 203 |
+
outlist += [(express(v[0], frame, variables=True).diff(t),
|
| 204 |
+
frame)]
|
| 205 |
+
else:
|
| 206 |
+
outlist += (time_derivative(Vector([v]), v[1]) +
|
| 207 |
+
(v[1].ang_vel_in(frame) ^ Vector([v]))).args
|
| 208 |
+
outvec = Vector(outlist)
|
| 209 |
+
return time_derivative(outvec, frame, order - 1)
|
| 210 |
+
|
| 211 |
+
if isinstance(expr, Dyadic):
|
| 212 |
+
ol = Dyadic(0)
|
| 213 |
+
for v in expr.args:
|
| 214 |
+
ol += (v[0].diff(t) * (v[1] | v[2]))
|
| 215 |
+
ol += (v[0] * (time_derivative(v[1], frame) | v[2]))
|
| 216 |
+
ol += (v[0] * (v[1] | time_derivative(v[2], frame)))
|
| 217 |
+
return time_derivative(ol, frame, order - 1)
|
| 218 |
+
|
| 219 |
+
else:
|
| 220 |
+
return diff(express(expr, frame, variables=True), t, order)
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def outer(vec1, vec2):
|
| 224 |
+
"""Outer product convenience wrapper for Vector.outer():\n"""
|
| 225 |
+
if not isinstance(vec1, Vector):
|
| 226 |
+
raise TypeError('Outer product is between two Vectors')
|
| 227 |
+
return vec1.outer(vec2)
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
outer.__doc__ += Vector.outer.__doc__ # type: ignore
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
def kinematic_equations(speeds, coords, rot_type, rot_order=''):
|
| 234 |
+
"""Gives equations relating the qdot's to u's for a rotation type.
|
| 235 |
+
|
| 236 |
+
Supply rotation type and order as in orient. Speeds are assumed to be
|
| 237 |
+
body-fixed; if we are defining the orientation of B in A using by rot_type,
|
| 238 |
+
the angular velocity of B in A is assumed to be in the form: speed[0]*B.x +
|
| 239 |
+
speed[1]*B.y + speed[2]*B.z
|
| 240 |
+
|
| 241 |
+
Parameters
|
| 242 |
+
==========
|
| 243 |
+
|
| 244 |
+
speeds : list of length 3
|
| 245 |
+
The body fixed angular velocity measure numbers.
|
| 246 |
+
coords : list of length 3 or 4
|
| 247 |
+
The coordinates used to define the orientation of the two frames.
|
| 248 |
+
rot_type : str
|
| 249 |
+
The type of rotation used to create the equations. Body, Space, or
|
| 250 |
+
Quaternion only
|
| 251 |
+
rot_order : str or int
|
| 252 |
+
If applicable, the order of a series of rotations.
|
| 253 |
+
|
| 254 |
+
Examples
|
| 255 |
+
========
|
| 256 |
+
|
| 257 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 258 |
+
>>> from sympy.physics.vector import kinematic_equations, vprint
|
| 259 |
+
>>> u1, u2, u3 = dynamicsymbols('u1 u2 u3')
|
| 260 |
+
>>> q1, q2, q3 = dynamicsymbols('q1 q2 q3')
|
| 261 |
+
>>> vprint(kinematic_equations([u1,u2,u3], [q1,q2,q3], 'body', '313'),
|
| 262 |
+
... order=None)
|
| 263 |
+
[-(u1*sin(q3) + u2*cos(q3))/sin(q2) + q1', -u1*cos(q3) + u2*sin(q3) + q2', (u1*sin(q3) + u2*cos(q3))*cos(q2)/sin(q2) - u3 + q3']
|
| 264 |
+
|
| 265 |
+
"""
|
| 266 |
+
|
| 267 |
+
# Code below is checking and sanitizing input
|
| 268 |
+
approved_orders = ('123', '231', '312', '132', '213', '321', '121', '131',
|
| 269 |
+
'212', '232', '313', '323', '1', '2', '3', '')
|
| 270 |
+
# make sure XYZ => 123 and rot_type is in lower case
|
| 271 |
+
rot_order = translate(str(rot_order), 'XYZxyz', '123123')
|
| 272 |
+
rot_type = rot_type.lower()
|
| 273 |
+
|
| 274 |
+
if not isinstance(speeds, (list, tuple)):
|
| 275 |
+
raise TypeError('Need to supply speeds in a list')
|
| 276 |
+
if len(speeds) != 3:
|
| 277 |
+
raise TypeError('Need to supply 3 body-fixed speeds')
|
| 278 |
+
if not isinstance(coords, (list, tuple)):
|
| 279 |
+
raise TypeError('Need to supply coordinates in a list')
|
| 280 |
+
if rot_type in ['body', 'space']:
|
| 281 |
+
if rot_order not in approved_orders:
|
| 282 |
+
raise ValueError('Not an acceptable rotation order')
|
| 283 |
+
if len(coords) != 3:
|
| 284 |
+
raise ValueError('Need 3 coordinates for body or space')
|
| 285 |
+
# Actual hard-coded kinematic differential equations
|
| 286 |
+
w1, w2, w3 = speeds
|
| 287 |
+
if w1 == w2 == w3 == 0:
|
| 288 |
+
return [S.Zero]*3
|
| 289 |
+
q1, q2, q3 = coords
|
| 290 |
+
q1d, q2d, q3d = [diff(i, dynamicsymbols._t) for i in coords]
|
| 291 |
+
s1, s2, s3 = [sin(q1), sin(q2), sin(q3)]
|
| 292 |
+
c1, c2, c3 = [cos(q1), cos(q2), cos(q3)]
|
| 293 |
+
if rot_type == 'body':
|
| 294 |
+
if rot_order == '123':
|
| 295 |
+
return [q1d - (w1 * c3 - w2 * s3) / c2, q2d - w1 * s3 - w2 *
|
| 296 |
+
c3, q3d - (-w1 * c3 + w2 * s3) * s2 / c2 - w3]
|
| 297 |
+
if rot_order == '231':
|
| 298 |
+
return [q1d - (w2 * c3 - w3 * s3) / c2, q2d - w2 * s3 - w3 *
|
| 299 |
+
c3, q3d - w1 - (- w2 * c3 + w3 * s3) * s2 / c2]
|
| 300 |
+
if rot_order == '312':
|
| 301 |
+
return [q1d - (-w1 * s3 + w3 * c3) / c2, q2d - w1 * c3 - w3 *
|
| 302 |
+
s3, q3d - (w1 * s3 - w3 * c3) * s2 / c2 - w2]
|
| 303 |
+
if rot_order == '132':
|
| 304 |
+
return [q1d - (w1 * c3 + w3 * s3) / c2, q2d + w1 * s3 - w3 *
|
| 305 |
+
c3, q3d - (w1 * c3 + w3 * s3) * s2 / c2 - w2]
|
| 306 |
+
if rot_order == '213':
|
| 307 |
+
return [q1d - (w1 * s3 + w2 * c3) / c2, q2d - w1 * c3 + w2 *
|
| 308 |
+
s3, q3d - (w1 * s3 + w2 * c3) * s2 / c2 - w3]
|
| 309 |
+
if rot_order == '321':
|
| 310 |
+
return [q1d - (w2 * s3 + w3 * c3) / c2, q2d - w2 * c3 + w3 *
|
| 311 |
+
s3, q3d - w1 - (w2 * s3 + w3 * c3) * s2 / c2]
|
| 312 |
+
if rot_order == '121':
|
| 313 |
+
return [q1d - (w2 * s3 + w3 * c3) / s2, q2d - w2 * c3 + w3 *
|
| 314 |
+
s3, q3d - w1 + (w2 * s3 + w3 * c3) * c2 / s2]
|
| 315 |
+
if rot_order == '131':
|
| 316 |
+
return [q1d - (-w2 * c3 + w3 * s3) / s2, q2d - w2 * s3 - w3 *
|
| 317 |
+
c3, q3d - w1 - (w2 * c3 - w3 * s3) * c2 / s2]
|
| 318 |
+
if rot_order == '212':
|
| 319 |
+
return [q1d - (w1 * s3 - w3 * c3) / s2, q2d - w1 * c3 - w3 *
|
| 320 |
+
s3, q3d - (-w1 * s3 + w3 * c3) * c2 / s2 - w2]
|
| 321 |
+
if rot_order == '232':
|
| 322 |
+
return [q1d - (w1 * c3 + w3 * s3) / s2, q2d + w1 * s3 - w3 *
|
| 323 |
+
c3, q3d + (w1 * c3 + w3 * s3) * c2 / s2 - w2]
|
| 324 |
+
if rot_order == '313':
|
| 325 |
+
return [q1d - (w1 * s3 + w2 * c3) / s2, q2d - w1 * c3 + w2 *
|
| 326 |
+
s3, q3d + (w1 * s3 + w2 * c3) * c2 / s2 - w3]
|
| 327 |
+
if rot_order == '323':
|
| 328 |
+
return [q1d - (-w1 * c3 + w2 * s3) / s2, q2d - w1 * s3 - w2 *
|
| 329 |
+
c3, q3d - (w1 * c3 - w2 * s3) * c2 / s2 - w3]
|
| 330 |
+
if rot_type == 'space':
|
| 331 |
+
if rot_order == '123':
|
| 332 |
+
return [q1d - w1 - (w2 * s1 + w3 * c1) * s2 / c2, q2d - w2 *
|
| 333 |
+
c1 + w3 * s1, q3d - (w2 * s1 + w3 * c1) / c2]
|
| 334 |
+
if rot_order == '231':
|
| 335 |
+
return [q1d - (w1 * c1 + w3 * s1) * s2 / c2 - w2, q2d + w1 *
|
| 336 |
+
s1 - w3 * c1, q3d - (w1 * c1 + w3 * s1) / c2]
|
| 337 |
+
if rot_order == '312':
|
| 338 |
+
return [q1d - (w1 * s1 + w2 * c1) * s2 / c2 - w3, q2d - w1 *
|
| 339 |
+
c1 + w2 * s1, q3d - (w1 * s1 + w2 * c1) / c2]
|
| 340 |
+
if rot_order == '132':
|
| 341 |
+
return [q1d - w1 - (-w2 * c1 + w3 * s1) * s2 / c2, q2d - w2 *
|
| 342 |
+
s1 - w3 * c1, q3d - (w2 * c1 - w3 * s1) / c2]
|
| 343 |
+
if rot_order == '213':
|
| 344 |
+
return [q1d - (w1 * s1 - w3 * c1) * s2 / c2 - w2, q2d - w1 *
|
| 345 |
+
c1 - w3 * s1, q3d - (-w1 * s1 + w3 * c1) / c2]
|
| 346 |
+
if rot_order == '321':
|
| 347 |
+
return [q1d - (-w1 * c1 + w2 * s1) * s2 / c2 - w3, q2d - w1 *
|
| 348 |
+
s1 - w2 * c1, q3d - (w1 * c1 - w2 * s1) / c2]
|
| 349 |
+
if rot_order == '121':
|
| 350 |
+
return [q1d - w1 + (w2 * s1 + w3 * c1) * c2 / s2, q2d - w2 *
|
| 351 |
+
c1 + w3 * s1, q3d - (w2 * s1 + w3 * c1) / s2]
|
| 352 |
+
if rot_order == '131':
|
| 353 |
+
return [q1d - w1 - (w2 * c1 - w3 * s1) * c2 / s2, q2d - w2 *
|
| 354 |
+
s1 - w3 * c1, q3d - (-w2 * c1 + w3 * s1) / s2]
|
| 355 |
+
if rot_order == '212':
|
| 356 |
+
return [q1d - (-w1 * s1 + w3 * c1) * c2 / s2 - w2, q2d - w1 *
|
| 357 |
+
c1 - w3 * s1, q3d - (w1 * s1 - w3 * c1) / s2]
|
| 358 |
+
if rot_order == '232':
|
| 359 |
+
return [q1d + (w1 * c1 + w3 * s1) * c2 / s2 - w2, q2d + w1 *
|
| 360 |
+
s1 - w3 * c1, q3d - (w1 * c1 + w3 * s1) / s2]
|
| 361 |
+
if rot_order == '313':
|
| 362 |
+
return [q1d + (w1 * s1 + w2 * c1) * c2 / s2 - w3, q2d - w1 *
|
| 363 |
+
c1 + w2 * s1, q3d - (w1 * s1 + w2 * c1) / s2]
|
| 364 |
+
if rot_order == '323':
|
| 365 |
+
return [q1d - (w1 * c1 - w2 * s1) * c2 / s2 - w3, q2d - w1 *
|
| 366 |
+
s1 - w2 * c1, q3d - (-w1 * c1 + w2 * s1) / s2]
|
| 367 |
+
elif rot_type == 'quaternion':
|
| 368 |
+
if rot_order != '':
|
| 369 |
+
raise ValueError('Cannot have rotation order for quaternion')
|
| 370 |
+
if len(coords) != 4:
|
| 371 |
+
raise ValueError('Need 4 coordinates for quaternion')
|
| 372 |
+
# Actual hard-coded kinematic differential equations
|
| 373 |
+
e0, e1, e2, e3 = coords
|
| 374 |
+
w = Matrix(speeds + [0])
|
| 375 |
+
E = Matrix([[e0, -e3, e2, e1],
|
| 376 |
+
[e3, e0, -e1, e2],
|
| 377 |
+
[-e2, e1, e0, e3],
|
| 378 |
+
[-e1, -e2, -e3, e0]])
|
| 379 |
+
edots = Matrix([diff(i, dynamicsymbols._t) for i in [e1, e2, e3, e0]])
|
| 380 |
+
return list(edots.T - 0.5 * w.T * E.T)
|
| 381 |
+
else:
|
| 382 |
+
raise ValueError('Not an approved rotation type for this function')
|
| 383 |
+
|
| 384 |
+
|
| 385 |
+
def get_motion_params(frame, **kwargs):
|
| 386 |
+
"""
|
| 387 |
+
Returns the three motion parameters - (acceleration, velocity, and
|
| 388 |
+
position) as vectorial functions of time in the given frame.
|
| 389 |
+
|
| 390 |
+
If a higher order differential function is provided, the lower order
|
| 391 |
+
functions are used as boundary conditions. For example, given the
|
| 392 |
+
acceleration, the velocity and position parameters are taken as
|
| 393 |
+
boundary conditions.
|
| 394 |
+
|
| 395 |
+
The values of time at which the boundary conditions are specified
|
| 396 |
+
are taken from timevalue1(for position boundary condition) and
|
| 397 |
+
timevalue2(for velocity boundary condition).
|
| 398 |
+
|
| 399 |
+
If any of the boundary conditions are not provided, they are taken
|
| 400 |
+
to be zero by default (zero vectors, in case of vectorial inputs). If
|
| 401 |
+
the boundary conditions are also functions of time, they are converted
|
| 402 |
+
to constants by substituting the time values in the dynamicsymbols._t
|
| 403 |
+
time Symbol.
|
| 404 |
+
|
| 405 |
+
This function can also be used for calculating rotational motion
|
| 406 |
+
parameters. Have a look at the Parameters and Examples for more clarity.
|
| 407 |
+
|
| 408 |
+
Parameters
|
| 409 |
+
==========
|
| 410 |
+
|
| 411 |
+
frame : ReferenceFrame
|
| 412 |
+
The frame to express the motion parameters in
|
| 413 |
+
|
| 414 |
+
acceleration : Vector
|
| 415 |
+
Acceleration of the object/frame as a function of time
|
| 416 |
+
|
| 417 |
+
velocity : Vector
|
| 418 |
+
Velocity as function of time or as boundary condition
|
| 419 |
+
of velocity at time = timevalue1
|
| 420 |
+
|
| 421 |
+
position : Vector
|
| 422 |
+
Velocity as function of time or as boundary condition
|
| 423 |
+
of velocity at time = timevalue1
|
| 424 |
+
|
| 425 |
+
timevalue1 : sympyfiable
|
| 426 |
+
Value of time for position boundary condition
|
| 427 |
+
|
| 428 |
+
timevalue2 : sympyfiable
|
| 429 |
+
Value of time for velocity boundary condition
|
| 430 |
+
|
| 431 |
+
Examples
|
| 432 |
+
========
|
| 433 |
+
|
| 434 |
+
>>> from sympy.physics.vector import ReferenceFrame, get_motion_params, dynamicsymbols
|
| 435 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 436 |
+
>>> init_vprinting(pretty_print=False)
|
| 437 |
+
>>> from sympy import symbols
|
| 438 |
+
>>> R = ReferenceFrame('R')
|
| 439 |
+
>>> v1, v2, v3 = dynamicsymbols('v1 v2 v3')
|
| 440 |
+
>>> v = v1*R.x + v2*R.y + v3*R.z
|
| 441 |
+
>>> get_motion_params(R, position = v)
|
| 442 |
+
(v1''*R.x + v2''*R.y + v3''*R.z, v1'*R.x + v2'*R.y + v3'*R.z, v1*R.x + v2*R.y + v3*R.z)
|
| 443 |
+
>>> a, b, c = symbols('a b c')
|
| 444 |
+
>>> v = a*R.x + b*R.y + c*R.z
|
| 445 |
+
>>> get_motion_params(R, velocity = v)
|
| 446 |
+
(0, a*R.x + b*R.y + c*R.z, a*t*R.x + b*t*R.y + c*t*R.z)
|
| 447 |
+
>>> parameters = get_motion_params(R, acceleration = v)
|
| 448 |
+
>>> parameters[1]
|
| 449 |
+
a*t*R.x + b*t*R.y + c*t*R.z
|
| 450 |
+
>>> parameters[2]
|
| 451 |
+
a*t**2/2*R.x + b*t**2/2*R.y + c*t**2/2*R.z
|
| 452 |
+
|
| 453 |
+
"""
|
| 454 |
+
|
| 455 |
+
def _process_vector_differential(vectdiff, condition, variable, ordinate,
|
| 456 |
+
frame):
|
| 457 |
+
"""
|
| 458 |
+
Helper function for get_motion methods. Finds derivative of vectdiff
|
| 459 |
+
wrt variable, and its integral using the specified boundary condition
|
| 460 |
+
at value of variable = ordinate.
|
| 461 |
+
Returns a tuple of - (derivative, function and integral) wrt vectdiff
|
| 462 |
+
|
| 463 |
+
"""
|
| 464 |
+
|
| 465 |
+
# Make sure boundary condition is independent of 'variable'
|
| 466 |
+
if condition != 0:
|
| 467 |
+
condition = express(condition, frame, variables=True)
|
| 468 |
+
# Special case of vectdiff == 0
|
| 469 |
+
if vectdiff == Vector(0):
|
| 470 |
+
return (0, 0, condition)
|
| 471 |
+
# Express vectdiff completely in condition's frame to give vectdiff1
|
| 472 |
+
vectdiff1 = express(vectdiff, frame)
|
| 473 |
+
# Find derivative of vectdiff
|
| 474 |
+
vectdiff2 = time_derivative(vectdiff, frame)
|
| 475 |
+
# Integrate and use boundary condition
|
| 476 |
+
vectdiff0 = Vector(0)
|
| 477 |
+
lims = (variable, ordinate, variable)
|
| 478 |
+
for dim in frame:
|
| 479 |
+
function1 = vectdiff1.dot(dim)
|
| 480 |
+
abscissa = dim.dot(condition).subs({variable: ordinate})
|
| 481 |
+
# Indefinite integral of 'function1' wrt 'variable', using
|
| 482 |
+
# the given initial condition (ordinate, abscissa).
|
| 483 |
+
vectdiff0 += (integrate(function1, lims) + abscissa) * dim
|
| 484 |
+
# Return tuple
|
| 485 |
+
return (vectdiff2, vectdiff, vectdiff0)
|
| 486 |
+
|
| 487 |
+
_check_frame(frame)
|
| 488 |
+
# Decide mode of operation based on user's input
|
| 489 |
+
if 'acceleration' in kwargs:
|
| 490 |
+
mode = 2
|
| 491 |
+
elif 'velocity' in kwargs:
|
| 492 |
+
mode = 1
|
| 493 |
+
else:
|
| 494 |
+
mode = 0
|
| 495 |
+
# All the possible parameters in kwargs
|
| 496 |
+
# Not all are required for every case
|
| 497 |
+
# If not specified, set to default values(may or may not be used in
|
| 498 |
+
# calculations)
|
| 499 |
+
conditions = ['acceleration', 'velocity', 'position',
|
| 500 |
+
'timevalue', 'timevalue1', 'timevalue2']
|
| 501 |
+
for i, x in enumerate(conditions):
|
| 502 |
+
if x not in kwargs:
|
| 503 |
+
if i < 3:
|
| 504 |
+
kwargs[x] = Vector(0)
|
| 505 |
+
else:
|
| 506 |
+
kwargs[x] = S.Zero
|
| 507 |
+
elif i < 3:
|
| 508 |
+
_check_vector(kwargs[x])
|
| 509 |
+
else:
|
| 510 |
+
kwargs[x] = sympify(kwargs[x])
|
| 511 |
+
if mode == 2:
|
| 512 |
+
vel = _process_vector_differential(kwargs['acceleration'],
|
| 513 |
+
kwargs['velocity'],
|
| 514 |
+
dynamicsymbols._t,
|
| 515 |
+
kwargs['timevalue2'], frame)[2]
|
| 516 |
+
pos = _process_vector_differential(vel, kwargs['position'],
|
| 517 |
+
dynamicsymbols._t,
|
| 518 |
+
kwargs['timevalue1'], frame)[2]
|
| 519 |
+
return (kwargs['acceleration'], vel, pos)
|
| 520 |
+
elif mode == 1:
|
| 521 |
+
return _process_vector_differential(kwargs['velocity'],
|
| 522 |
+
kwargs['position'],
|
| 523 |
+
dynamicsymbols._t,
|
| 524 |
+
kwargs['timevalue1'], frame)
|
| 525 |
+
else:
|
| 526 |
+
vel = time_derivative(kwargs['position'], frame)
|
| 527 |
+
acc = time_derivative(vel, frame)
|
| 528 |
+
return (acc, vel, kwargs['position'])
|
| 529 |
+
|
| 530 |
+
|
| 531 |
+
def partial_velocity(vel_vecs, gen_speeds, frame):
|
| 532 |
+
"""Returns a list of partial velocities with respect to the provided
|
| 533 |
+
generalized speeds in the given reference frame for each of the supplied
|
| 534 |
+
velocity vectors.
|
| 535 |
+
|
| 536 |
+
The output is a list of lists. The outer list has a number of elements
|
| 537 |
+
equal to the number of supplied velocity vectors. The inner lists are, for
|
| 538 |
+
each velocity vector, the partial derivatives of that velocity vector with
|
| 539 |
+
respect to the generalized speeds supplied.
|
| 540 |
+
|
| 541 |
+
Parameters
|
| 542 |
+
==========
|
| 543 |
+
|
| 544 |
+
vel_vecs : iterable
|
| 545 |
+
An iterable of velocity vectors (angular or linear).
|
| 546 |
+
gen_speeds : iterable
|
| 547 |
+
An iterable of generalized speeds.
|
| 548 |
+
frame : ReferenceFrame
|
| 549 |
+
The reference frame that the partial derivatives are going to be taken
|
| 550 |
+
in.
|
| 551 |
+
|
| 552 |
+
Examples
|
| 553 |
+
========
|
| 554 |
+
|
| 555 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame
|
| 556 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 557 |
+
>>> from sympy.physics.vector import partial_velocity
|
| 558 |
+
>>> u = dynamicsymbols('u')
|
| 559 |
+
>>> N = ReferenceFrame('N')
|
| 560 |
+
>>> P = Point('P')
|
| 561 |
+
>>> P.set_vel(N, u * N.x)
|
| 562 |
+
>>> vel_vecs = [P.vel(N)]
|
| 563 |
+
>>> gen_speeds = [u]
|
| 564 |
+
>>> partial_velocity(vel_vecs, gen_speeds, N)
|
| 565 |
+
[[N.x]]
|
| 566 |
+
|
| 567 |
+
"""
|
| 568 |
+
|
| 569 |
+
if not iterable(vel_vecs):
|
| 570 |
+
raise TypeError('Velocity vectors must be contained in an iterable.')
|
| 571 |
+
|
| 572 |
+
if not iterable(gen_speeds):
|
| 573 |
+
raise TypeError('Generalized speeds must be contained in an iterable')
|
| 574 |
+
|
| 575 |
+
vec_partials = []
|
| 576 |
+
gen_speeds = list(gen_speeds)
|
| 577 |
+
for vel in vel_vecs:
|
| 578 |
+
partials = [Vector(0) for _ in gen_speeds]
|
| 579 |
+
for components, ref in vel.args:
|
| 580 |
+
mat, _ = linear_eq_to_matrix(components, gen_speeds)
|
| 581 |
+
for i in range(len(gen_speeds)):
|
| 582 |
+
for dim, direction in enumerate(ref):
|
| 583 |
+
if mat[dim, i] != 0:
|
| 584 |
+
partials[i] += direction * mat[dim, i]
|
| 585 |
+
|
| 586 |
+
vec_partials.append(partials)
|
| 587 |
+
|
| 588 |
+
return vec_partials
|
| 589 |
+
|
| 590 |
+
|
| 591 |
+
def dynamicsymbols(names, level=0, **assumptions):
|
| 592 |
+
"""Uses symbols and Function for functions of time.
|
| 593 |
+
|
| 594 |
+
Creates a SymPy UndefinedFunction, which is then initialized as a function
|
| 595 |
+
of a variable, the default being Symbol('t').
|
| 596 |
+
|
| 597 |
+
Parameters
|
| 598 |
+
==========
|
| 599 |
+
|
| 600 |
+
names : str
|
| 601 |
+
Names of the dynamic symbols you want to create; works the same way as
|
| 602 |
+
inputs to symbols
|
| 603 |
+
level : int
|
| 604 |
+
Level of differentiation of the returned function; d/dt once of t,
|
| 605 |
+
twice of t, etc.
|
| 606 |
+
assumptions :
|
| 607 |
+
- real(bool) : This is used to set the dynamicsymbol as real,
|
| 608 |
+
by default is False.
|
| 609 |
+
- positive(bool) : This is used to set the dynamicsymbol as positive,
|
| 610 |
+
by default is False.
|
| 611 |
+
- commutative(bool) : This is used to set the commutative property of
|
| 612 |
+
a dynamicsymbol, by default is True.
|
| 613 |
+
- integer(bool) : This is used to set the dynamicsymbol as integer,
|
| 614 |
+
by default is False.
|
| 615 |
+
|
| 616 |
+
Examples
|
| 617 |
+
========
|
| 618 |
+
|
| 619 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 620 |
+
>>> from sympy import diff, Symbol
|
| 621 |
+
>>> q1 = dynamicsymbols('q1')
|
| 622 |
+
>>> q1
|
| 623 |
+
q1(t)
|
| 624 |
+
>>> q2 = dynamicsymbols('q2', real=True)
|
| 625 |
+
>>> q2.is_real
|
| 626 |
+
True
|
| 627 |
+
>>> q3 = dynamicsymbols('q3', positive=True)
|
| 628 |
+
>>> q3.is_positive
|
| 629 |
+
True
|
| 630 |
+
>>> q4, q5 = dynamicsymbols('q4,q5', commutative=False)
|
| 631 |
+
>>> bool(q4*q5 != q5*q4)
|
| 632 |
+
True
|
| 633 |
+
>>> q6 = dynamicsymbols('q6', integer=True)
|
| 634 |
+
>>> q6.is_integer
|
| 635 |
+
True
|
| 636 |
+
>>> diff(q1, Symbol('t'))
|
| 637 |
+
Derivative(q1(t), t)
|
| 638 |
+
|
| 639 |
+
"""
|
| 640 |
+
esses = symbols(names, cls=Function, **assumptions)
|
| 641 |
+
t = dynamicsymbols._t
|
| 642 |
+
if iterable(esses):
|
| 643 |
+
esses = [reduce(diff, [t] * level, e(t)) for e in esses]
|
| 644 |
+
return esses
|
| 645 |
+
else:
|
| 646 |
+
return reduce(diff, [t] * level, esses(t))
|
| 647 |
+
|
| 648 |
+
|
| 649 |
+
dynamicsymbols._t = Symbol('t') # type: ignore
|
| 650 |
+
dynamicsymbols._str = '\'' # type: ignore
|
hug/lib/python3.12/site-packages/sympy/physics/vector/point.py
ADDED
|
@@ -0,0 +1,635 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .vector import Vector, _check_vector
|
| 2 |
+
from .frame import _check_frame
|
| 3 |
+
from warnings import warn
|
| 4 |
+
from sympy.utilities.misc import filldedent
|
| 5 |
+
|
| 6 |
+
__all__ = ['Point']
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class Point:
|
| 10 |
+
"""This object represents a point in a dynamic system.
|
| 11 |
+
|
| 12 |
+
It stores the: position, velocity, and acceleration of a point.
|
| 13 |
+
The position is a vector defined as the vector distance from a parent
|
| 14 |
+
point to this point.
|
| 15 |
+
|
| 16 |
+
Parameters
|
| 17 |
+
==========
|
| 18 |
+
|
| 19 |
+
name : string
|
| 20 |
+
The display name of the Point
|
| 21 |
+
|
| 22 |
+
Examples
|
| 23 |
+
========
|
| 24 |
+
|
| 25 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols
|
| 26 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 27 |
+
>>> init_vprinting(pretty_print=False)
|
| 28 |
+
>>> N = ReferenceFrame('N')
|
| 29 |
+
>>> O = Point('O')
|
| 30 |
+
>>> P = Point('P')
|
| 31 |
+
>>> u1, u2, u3 = dynamicsymbols('u1 u2 u3')
|
| 32 |
+
>>> O.set_vel(N, u1 * N.x + u2 * N.y + u3 * N.z)
|
| 33 |
+
>>> O.acc(N)
|
| 34 |
+
u1'*N.x + u2'*N.y + u3'*N.z
|
| 35 |
+
|
| 36 |
+
``symbols()`` can be used to create multiple Points in a single step, for
|
| 37 |
+
example:
|
| 38 |
+
|
| 39 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols
|
| 40 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 41 |
+
>>> init_vprinting(pretty_print=False)
|
| 42 |
+
>>> from sympy import symbols
|
| 43 |
+
>>> N = ReferenceFrame('N')
|
| 44 |
+
>>> u1, u2 = dynamicsymbols('u1 u2')
|
| 45 |
+
>>> A, B = symbols('A B', cls=Point)
|
| 46 |
+
>>> type(A)
|
| 47 |
+
<class 'sympy.physics.vector.point.Point'>
|
| 48 |
+
>>> A.set_vel(N, u1 * N.x + u2 * N.y)
|
| 49 |
+
>>> B.set_vel(N, u2 * N.x + u1 * N.y)
|
| 50 |
+
>>> A.acc(N) - B.acc(N)
|
| 51 |
+
(u1' - u2')*N.x + (-u1' + u2')*N.y
|
| 52 |
+
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
def __init__(self, name):
|
| 56 |
+
"""Initialization of a Point object. """
|
| 57 |
+
self.name = name
|
| 58 |
+
self._pos_dict = {}
|
| 59 |
+
self._vel_dict = {}
|
| 60 |
+
self._acc_dict = {}
|
| 61 |
+
self._pdlist = [self._pos_dict, self._vel_dict, self._acc_dict]
|
| 62 |
+
|
| 63 |
+
def __str__(self):
|
| 64 |
+
return self.name
|
| 65 |
+
|
| 66 |
+
__repr__ = __str__
|
| 67 |
+
|
| 68 |
+
def _check_point(self, other):
|
| 69 |
+
if not isinstance(other, Point):
|
| 70 |
+
raise TypeError('A Point must be supplied')
|
| 71 |
+
|
| 72 |
+
def _pdict_list(self, other, num):
|
| 73 |
+
"""Returns a list of points that gives the shortest path with respect
|
| 74 |
+
to position, velocity, or acceleration from this point to the provided
|
| 75 |
+
point.
|
| 76 |
+
|
| 77 |
+
Parameters
|
| 78 |
+
==========
|
| 79 |
+
other : Point
|
| 80 |
+
A point that may be related to this point by position, velocity, or
|
| 81 |
+
acceleration.
|
| 82 |
+
num : integer
|
| 83 |
+
0 for searching the position tree, 1 for searching the velocity
|
| 84 |
+
tree, and 2 for searching the acceleration tree.
|
| 85 |
+
|
| 86 |
+
Returns
|
| 87 |
+
=======
|
| 88 |
+
list of Points
|
| 89 |
+
A sequence of points from self to other.
|
| 90 |
+
|
| 91 |
+
Notes
|
| 92 |
+
=====
|
| 93 |
+
|
| 94 |
+
It is not clear if num = 1 or num = 2 actually works because the keys
|
| 95 |
+
to ``_vel_dict`` and ``_acc_dict`` are :class:`ReferenceFrame` objects
|
| 96 |
+
which do not have the ``_pdlist`` attribute.
|
| 97 |
+
|
| 98 |
+
"""
|
| 99 |
+
outlist = [[self]]
|
| 100 |
+
oldlist = [[]]
|
| 101 |
+
while outlist != oldlist:
|
| 102 |
+
oldlist = outlist.copy()
|
| 103 |
+
for v in outlist:
|
| 104 |
+
templist = v[-1]._pdlist[num].keys()
|
| 105 |
+
for v2 in templist:
|
| 106 |
+
if not v.__contains__(v2):
|
| 107 |
+
littletemplist = v + [v2]
|
| 108 |
+
if not outlist.__contains__(littletemplist):
|
| 109 |
+
outlist.append(littletemplist)
|
| 110 |
+
for v in oldlist:
|
| 111 |
+
if v[-1] != other:
|
| 112 |
+
outlist.remove(v)
|
| 113 |
+
outlist.sort(key=len)
|
| 114 |
+
if len(outlist) != 0:
|
| 115 |
+
return outlist[0]
|
| 116 |
+
raise ValueError('No Connecting Path found between ' + other.name +
|
| 117 |
+
' and ' + self.name)
|
| 118 |
+
|
| 119 |
+
def a1pt_theory(self, otherpoint, outframe, interframe):
|
| 120 |
+
"""Sets the acceleration of this point with the 1-point theory.
|
| 121 |
+
|
| 122 |
+
The 1-point theory for point acceleration looks like this:
|
| 123 |
+
|
| 124 |
+
^N a^P = ^B a^P + ^N a^O + ^N alpha^B x r^OP + ^N omega^B x (^N omega^B
|
| 125 |
+
x r^OP) + 2 ^N omega^B x ^B v^P
|
| 126 |
+
|
| 127 |
+
where O is a point fixed in B, P is a point moving in B, and B is
|
| 128 |
+
rotating in frame N.
|
| 129 |
+
|
| 130 |
+
Parameters
|
| 131 |
+
==========
|
| 132 |
+
|
| 133 |
+
otherpoint : Point
|
| 134 |
+
The first point of the 1-point theory (O)
|
| 135 |
+
outframe : ReferenceFrame
|
| 136 |
+
The frame we want this point's acceleration defined in (N)
|
| 137 |
+
fixedframe : ReferenceFrame
|
| 138 |
+
The intermediate frame in this calculation (B)
|
| 139 |
+
|
| 140 |
+
Examples
|
| 141 |
+
========
|
| 142 |
+
|
| 143 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame
|
| 144 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 145 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 146 |
+
>>> init_vprinting(pretty_print=False)
|
| 147 |
+
>>> q = dynamicsymbols('q')
|
| 148 |
+
>>> q2 = dynamicsymbols('q2')
|
| 149 |
+
>>> qd = dynamicsymbols('q', 1)
|
| 150 |
+
>>> q2d = dynamicsymbols('q2', 1)
|
| 151 |
+
>>> N = ReferenceFrame('N')
|
| 152 |
+
>>> B = ReferenceFrame('B')
|
| 153 |
+
>>> B.set_ang_vel(N, 5 * B.y)
|
| 154 |
+
>>> O = Point('O')
|
| 155 |
+
>>> P = O.locatenew('P', q * B.x + q2 * B.y)
|
| 156 |
+
>>> P.set_vel(B, qd * B.x + q2d * B.y)
|
| 157 |
+
>>> O.set_vel(N, 0)
|
| 158 |
+
>>> P.a1pt_theory(O, N, B)
|
| 159 |
+
(-25*q + q'')*B.x + q2''*B.y - 10*q'*B.z
|
| 160 |
+
|
| 161 |
+
"""
|
| 162 |
+
|
| 163 |
+
_check_frame(outframe)
|
| 164 |
+
_check_frame(interframe)
|
| 165 |
+
self._check_point(otherpoint)
|
| 166 |
+
dist = self.pos_from(otherpoint)
|
| 167 |
+
v = self.vel(interframe)
|
| 168 |
+
a1 = otherpoint.acc(outframe)
|
| 169 |
+
a2 = self.acc(interframe)
|
| 170 |
+
omega = interframe.ang_vel_in(outframe)
|
| 171 |
+
alpha = interframe.ang_acc_in(outframe)
|
| 172 |
+
self.set_acc(outframe, a2 + 2 * (omega.cross(v)) + a1 +
|
| 173 |
+
(alpha.cross(dist)) + (omega.cross(omega.cross(dist))))
|
| 174 |
+
return self.acc(outframe)
|
| 175 |
+
|
| 176 |
+
def a2pt_theory(self, otherpoint, outframe, fixedframe):
|
| 177 |
+
"""Sets the acceleration of this point with the 2-point theory.
|
| 178 |
+
|
| 179 |
+
The 2-point theory for point acceleration looks like this:
|
| 180 |
+
|
| 181 |
+
^N a^P = ^N a^O + ^N alpha^B x r^OP + ^N omega^B x (^N omega^B x r^OP)
|
| 182 |
+
|
| 183 |
+
where O and P are both points fixed in frame B, which is rotating in
|
| 184 |
+
frame N.
|
| 185 |
+
|
| 186 |
+
Parameters
|
| 187 |
+
==========
|
| 188 |
+
|
| 189 |
+
otherpoint : Point
|
| 190 |
+
The first point of the 2-point theory (O)
|
| 191 |
+
outframe : ReferenceFrame
|
| 192 |
+
The frame we want this point's acceleration defined in (N)
|
| 193 |
+
fixedframe : ReferenceFrame
|
| 194 |
+
The frame in which both points are fixed (B)
|
| 195 |
+
|
| 196 |
+
Examples
|
| 197 |
+
========
|
| 198 |
+
|
| 199 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols
|
| 200 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 201 |
+
>>> init_vprinting(pretty_print=False)
|
| 202 |
+
>>> q = dynamicsymbols('q')
|
| 203 |
+
>>> qd = dynamicsymbols('q', 1)
|
| 204 |
+
>>> N = ReferenceFrame('N')
|
| 205 |
+
>>> B = N.orientnew('B', 'Axis', [q, N.z])
|
| 206 |
+
>>> O = Point('O')
|
| 207 |
+
>>> P = O.locatenew('P', 10 * B.x)
|
| 208 |
+
>>> O.set_vel(N, 5 * N.x)
|
| 209 |
+
>>> P.a2pt_theory(O, N, B)
|
| 210 |
+
- 10*q'**2*B.x + 10*q''*B.y
|
| 211 |
+
|
| 212 |
+
"""
|
| 213 |
+
|
| 214 |
+
_check_frame(outframe)
|
| 215 |
+
_check_frame(fixedframe)
|
| 216 |
+
self._check_point(otherpoint)
|
| 217 |
+
dist = self.pos_from(otherpoint)
|
| 218 |
+
a = otherpoint.acc(outframe)
|
| 219 |
+
omega = fixedframe.ang_vel_in(outframe)
|
| 220 |
+
alpha = fixedframe.ang_acc_in(outframe)
|
| 221 |
+
self.set_acc(outframe, a + (alpha.cross(dist)) +
|
| 222 |
+
(omega.cross(omega.cross(dist))))
|
| 223 |
+
return self.acc(outframe)
|
| 224 |
+
|
| 225 |
+
def acc(self, frame):
|
| 226 |
+
"""The acceleration Vector of this Point in a ReferenceFrame.
|
| 227 |
+
|
| 228 |
+
Parameters
|
| 229 |
+
==========
|
| 230 |
+
|
| 231 |
+
frame : ReferenceFrame
|
| 232 |
+
The frame in which the returned acceleration vector will be defined
|
| 233 |
+
in.
|
| 234 |
+
|
| 235 |
+
Examples
|
| 236 |
+
========
|
| 237 |
+
|
| 238 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame
|
| 239 |
+
>>> N = ReferenceFrame('N')
|
| 240 |
+
>>> p1 = Point('p1')
|
| 241 |
+
>>> p1.set_acc(N, 10 * N.x)
|
| 242 |
+
>>> p1.acc(N)
|
| 243 |
+
10*N.x
|
| 244 |
+
|
| 245 |
+
"""
|
| 246 |
+
|
| 247 |
+
_check_frame(frame)
|
| 248 |
+
if not (frame in self._acc_dict):
|
| 249 |
+
if self.vel(frame) != 0:
|
| 250 |
+
return (self._vel_dict[frame]).dt(frame)
|
| 251 |
+
else:
|
| 252 |
+
return Vector(0)
|
| 253 |
+
return self._acc_dict[frame]
|
| 254 |
+
|
| 255 |
+
def locatenew(self, name, value):
|
| 256 |
+
"""Creates a new point with a position defined from this point.
|
| 257 |
+
|
| 258 |
+
Parameters
|
| 259 |
+
==========
|
| 260 |
+
|
| 261 |
+
name : str
|
| 262 |
+
The name for the new point
|
| 263 |
+
value : Vector
|
| 264 |
+
The position of the new point relative to this point
|
| 265 |
+
|
| 266 |
+
Examples
|
| 267 |
+
========
|
| 268 |
+
|
| 269 |
+
>>> from sympy.physics.vector import ReferenceFrame, Point
|
| 270 |
+
>>> N = ReferenceFrame('N')
|
| 271 |
+
>>> P1 = Point('P1')
|
| 272 |
+
>>> P2 = P1.locatenew('P2', 10 * N.x)
|
| 273 |
+
|
| 274 |
+
"""
|
| 275 |
+
|
| 276 |
+
if not isinstance(name, str):
|
| 277 |
+
raise TypeError('Must supply a valid name')
|
| 278 |
+
if value == 0:
|
| 279 |
+
value = Vector(0)
|
| 280 |
+
value = _check_vector(value)
|
| 281 |
+
p = Point(name)
|
| 282 |
+
p.set_pos(self, value)
|
| 283 |
+
self.set_pos(p, -value)
|
| 284 |
+
return p
|
| 285 |
+
|
| 286 |
+
def pos_from(self, otherpoint):
|
| 287 |
+
"""Returns a Vector distance between this Point and the other Point.
|
| 288 |
+
|
| 289 |
+
Parameters
|
| 290 |
+
==========
|
| 291 |
+
|
| 292 |
+
otherpoint : Point
|
| 293 |
+
The otherpoint we are locating this one relative to
|
| 294 |
+
|
| 295 |
+
Examples
|
| 296 |
+
========
|
| 297 |
+
|
| 298 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame
|
| 299 |
+
>>> N = ReferenceFrame('N')
|
| 300 |
+
>>> p1 = Point('p1')
|
| 301 |
+
>>> p2 = Point('p2')
|
| 302 |
+
>>> p1.set_pos(p2, 10 * N.x)
|
| 303 |
+
>>> p1.pos_from(p2)
|
| 304 |
+
10*N.x
|
| 305 |
+
|
| 306 |
+
"""
|
| 307 |
+
|
| 308 |
+
outvec = Vector(0)
|
| 309 |
+
plist = self._pdict_list(otherpoint, 0)
|
| 310 |
+
for i in range(len(plist) - 1):
|
| 311 |
+
outvec += plist[i]._pos_dict[plist[i + 1]]
|
| 312 |
+
return outvec
|
| 313 |
+
|
| 314 |
+
def set_acc(self, frame, value):
|
| 315 |
+
"""Used to set the acceleration of this Point in a ReferenceFrame.
|
| 316 |
+
|
| 317 |
+
Parameters
|
| 318 |
+
==========
|
| 319 |
+
|
| 320 |
+
frame : ReferenceFrame
|
| 321 |
+
The frame in which this point's acceleration is defined
|
| 322 |
+
value : Vector
|
| 323 |
+
The vector value of this point's acceleration in the frame
|
| 324 |
+
|
| 325 |
+
Examples
|
| 326 |
+
========
|
| 327 |
+
|
| 328 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame
|
| 329 |
+
>>> N = ReferenceFrame('N')
|
| 330 |
+
>>> p1 = Point('p1')
|
| 331 |
+
>>> p1.set_acc(N, 10 * N.x)
|
| 332 |
+
>>> p1.acc(N)
|
| 333 |
+
10*N.x
|
| 334 |
+
|
| 335 |
+
"""
|
| 336 |
+
|
| 337 |
+
if value == 0:
|
| 338 |
+
value = Vector(0)
|
| 339 |
+
value = _check_vector(value)
|
| 340 |
+
_check_frame(frame)
|
| 341 |
+
self._acc_dict.update({frame: value})
|
| 342 |
+
|
| 343 |
+
def set_pos(self, otherpoint, value):
|
| 344 |
+
"""Used to set the position of this point w.r.t. another point.
|
| 345 |
+
|
| 346 |
+
Parameters
|
| 347 |
+
==========
|
| 348 |
+
|
| 349 |
+
otherpoint : Point
|
| 350 |
+
The other point which this point's location is defined relative to
|
| 351 |
+
value : Vector
|
| 352 |
+
The vector which defines the location of this point
|
| 353 |
+
|
| 354 |
+
Examples
|
| 355 |
+
========
|
| 356 |
+
|
| 357 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame
|
| 358 |
+
>>> N = ReferenceFrame('N')
|
| 359 |
+
>>> p1 = Point('p1')
|
| 360 |
+
>>> p2 = Point('p2')
|
| 361 |
+
>>> p1.set_pos(p2, 10 * N.x)
|
| 362 |
+
>>> p1.pos_from(p2)
|
| 363 |
+
10*N.x
|
| 364 |
+
|
| 365 |
+
"""
|
| 366 |
+
|
| 367 |
+
if value == 0:
|
| 368 |
+
value = Vector(0)
|
| 369 |
+
value = _check_vector(value)
|
| 370 |
+
self._check_point(otherpoint)
|
| 371 |
+
self._pos_dict.update({otherpoint: value})
|
| 372 |
+
otherpoint._pos_dict.update({self: -value})
|
| 373 |
+
|
| 374 |
+
def set_vel(self, frame, value):
|
| 375 |
+
"""Sets the velocity Vector of this Point in a ReferenceFrame.
|
| 376 |
+
|
| 377 |
+
Parameters
|
| 378 |
+
==========
|
| 379 |
+
|
| 380 |
+
frame : ReferenceFrame
|
| 381 |
+
The frame in which this point's velocity is defined
|
| 382 |
+
value : Vector
|
| 383 |
+
The vector value of this point's velocity in the frame
|
| 384 |
+
|
| 385 |
+
Examples
|
| 386 |
+
========
|
| 387 |
+
|
| 388 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame
|
| 389 |
+
>>> N = ReferenceFrame('N')
|
| 390 |
+
>>> p1 = Point('p1')
|
| 391 |
+
>>> p1.set_vel(N, 10 * N.x)
|
| 392 |
+
>>> p1.vel(N)
|
| 393 |
+
10*N.x
|
| 394 |
+
|
| 395 |
+
"""
|
| 396 |
+
|
| 397 |
+
if value == 0:
|
| 398 |
+
value = Vector(0)
|
| 399 |
+
value = _check_vector(value)
|
| 400 |
+
_check_frame(frame)
|
| 401 |
+
self._vel_dict.update({frame: value})
|
| 402 |
+
|
| 403 |
+
def v1pt_theory(self, otherpoint, outframe, interframe):
|
| 404 |
+
"""Sets the velocity of this point with the 1-point theory.
|
| 405 |
+
|
| 406 |
+
The 1-point theory for point velocity looks like this:
|
| 407 |
+
|
| 408 |
+
^N v^P = ^B v^P + ^N v^O + ^N omega^B x r^OP
|
| 409 |
+
|
| 410 |
+
where O is a point fixed in B, P is a point moving in B, and B is
|
| 411 |
+
rotating in frame N.
|
| 412 |
+
|
| 413 |
+
Parameters
|
| 414 |
+
==========
|
| 415 |
+
|
| 416 |
+
otherpoint : Point
|
| 417 |
+
The first point of the 1-point theory (O)
|
| 418 |
+
outframe : ReferenceFrame
|
| 419 |
+
The frame we want this point's velocity defined in (N)
|
| 420 |
+
interframe : ReferenceFrame
|
| 421 |
+
The intermediate frame in this calculation (B)
|
| 422 |
+
|
| 423 |
+
Examples
|
| 424 |
+
========
|
| 425 |
+
|
| 426 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame
|
| 427 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 428 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 429 |
+
>>> init_vprinting(pretty_print=False)
|
| 430 |
+
>>> q = dynamicsymbols('q')
|
| 431 |
+
>>> q2 = dynamicsymbols('q2')
|
| 432 |
+
>>> qd = dynamicsymbols('q', 1)
|
| 433 |
+
>>> q2d = dynamicsymbols('q2', 1)
|
| 434 |
+
>>> N = ReferenceFrame('N')
|
| 435 |
+
>>> B = ReferenceFrame('B')
|
| 436 |
+
>>> B.set_ang_vel(N, 5 * B.y)
|
| 437 |
+
>>> O = Point('O')
|
| 438 |
+
>>> P = O.locatenew('P', q * B.x + q2 * B.y)
|
| 439 |
+
>>> P.set_vel(B, qd * B.x + q2d * B.y)
|
| 440 |
+
>>> O.set_vel(N, 0)
|
| 441 |
+
>>> P.v1pt_theory(O, N, B)
|
| 442 |
+
q'*B.x + q2'*B.y - 5*q*B.z
|
| 443 |
+
|
| 444 |
+
"""
|
| 445 |
+
|
| 446 |
+
_check_frame(outframe)
|
| 447 |
+
_check_frame(interframe)
|
| 448 |
+
self._check_point(otherpoint)
|
| 449 |
+
dist = self.pos_from(otherpoint)
|
| 450 |
+
v1 = self.vel(interframe)
|
| 451 |
+
v2 = otherpoint.vel(outframe)
|
| 452 |
+
omega = interframe.ang_vel_in(outframe)
|
| 453 |
+
self.set_vel(outframe, v1 + v2 + (omega.cross(dist)))
|
| 454 |
+
return self.vel(outframe)
|
| 455 |
+
|
| 456 |
+
def v2pt_theory(self, otherpoint, outframe, fixedframe):
|
| 457 |
+
"""Sets the velocity of this point with the 2-point theory.
|
| 458 |
+
|
| 459 |
+
The 2-point theory for point velocity looks like this:
|
| 460 |
+
|
| 461 |
+
^N v^P = ^N v^O + ^N omega^B x r^OP
|
| 462 |
+
|
| 463 |
+
where O and P are both points fixed in frame B, which is rotating in
|
| 464 |
+
frame N.
|
| 465 |
+
|
| 466 |
+
Parameters
|
| 467 |
+
==========
|
| 468 |
+
|
| 469 |
+
otherpoint : Point
|
| 470 |
+
The first point of the 2-point theory (O)
|
| 471 |
+
outframe : ReferenceFrame
|
| 472 |
+
The frame we want this point's velocity defined in (N)
|
| 473 |
+
fixedframe : ReferenceFrame
|
| 474 |
+
The frame in which both points are fixed (B)
|
| 475 |
+
|
| 476 |
+
Examples
|
| 477 |
+
========
|
| 478 |
+
|
| 479 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols
|
| 480 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 481 |
+
>>> init_vprinting(pretty_print=False)
|
| 482 |
+
>>> q = dynamicsymbols('q')
|
| 483 |
+
>>> qd = dynamicsymbols('q', 1)
|
| 484 |
+
>>> N = ReferenceFrame('N')
|
| 485 |
+
>>> B = N.orientnew('B', 'Axis', [q, N.z])
|
| 486 |
+
>>> O = Point('O')
|
| 487 |
+
>>> P = O.locatenew('P', 10 * B.x)
|
| 488 |
+
>>> O.set_vel(N, 5 * N.x)
|
| 489 |
+
>>> P.v2pt_theory(O, N, B)
|
| 490 |
+
5*N.x + 10*q'*B.y
|
| 491 |
+
|
| 492 |
+
"""
|
| 493 |
+
|
| 494 |
+
_check_frame(outframe)
|
| 495 |
+
_check_frame(fixedframe)
|
| 496 |
+
self._check_point(otherpoint)
|
| 497 |
+
dist = self.pos_from(otherpoint)
|
| 498 |
+
v = otherpoint.vel(outframe)
|
| 499 |
+
omega = fixedframe.ang_vel_in(outframe)
|
| 500 |
+
self.set_vel(outframe, v + (omega.cross(dist)))
|
| 501 |
+
return self.vel(outframe)
|
| 502 |
+
|
| 503 |
+
def vel(self, frame):
|
| 504 |
+
"""The velocity Vector of this Point in the ReferenceFrame.
|
| 505 |
+
|
| 506 |
+
Parameters
|
| 507 |
+
==========
|
| 508 |
+
|
| 509 |
+
frame : ReferenceFrame
|
| 510 |
+
The frame in which the returned velocity vector will be defined in
|
| 511 |
+
|
| 512 |
+
Examples
|
| 513 |
+
========
|
| 514 |
+
|
| 515 |
+
>>> from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols
|
| 516 |
+
>>> N = ReferenceFrame('N')
|
| 517 |
+
>>> p1 = Point('p1')
|
| 518 |
+
>>> p1.set_vel(N, 10 * N.x)
|
| 519 |
+
>>> p1.vel(N)
|
| 520 |
+
10*N.x
|
| 521 |
+
|
| 522 |
+
Velocities will be automatically calculated if possible, otherwise a
|
| 523 |
+
``ValueError`` will be returned. If it is possible to calculate
|
| 524 |
+
multiple different velocities from the relative points, the points
|
| 525 |
+
defined most directly relative to this point will be used. In the case
|
| 526 |
+
of inconsistent relative positions of points, incorrect velocities may
|
| 527 |
+
be returned. It is up to the user to define prior relative positions
|
| 528 |
+
and velocities of points in a self-consistent way.
|
| 529 |
+
|
| 530 |
+
>>> p = Point('p')
|
| 531 |
+
>>> q = dynamicsymbols('q')
|
| 532 |
+
>>> p.set_vel(N, 10 * N.x)
|
| 533 |
+
>>> p2 = Point('p2')
|
| 534 |
+
>>> p2.set_pos(p, q*N.x)
|
| 535 |
+
>>> p2.vel(N)
|
| 536 |
+
(Derivative(q(t), t) + 10)*N.x
|
| 537 |
+
|
| 538 |
+
"""
|
| 539 |
+
|
| 540 |
+
_check_frame(frame)
|
| 541 |
+
if not (frame in self._vel_dict):
|
| 542 |
+
valid_neighbor_found = False
|
| 543 |
+
is_cyclic = False
|
| 544 |
+
visited = []
|
| 545 |
+
queue = [self]
|
| 546 |
+
candidate_neighbor = []
|
| 547 |
+
while queue: # BFS to find nearest point
|
| 548 |
+
node = queue.pop(0)
|
| 549 |
+
if node not in visited:
|
| 550 |
+
visited.append(node)
|
| 551 |
+
for neighbor, neighbor_pos in node._pos_dict.items():
|
| 552 |
+
if neighbor in visited:
|
| 553 |
+
continue
|
| 554 |
+
try:
|
| 555 |
+
# Checks if pos vector is valid
|
| 556 |
+
neighbor_pos.express(frame)
|
| 557 |
+
except ValueError:
|
| 558 |
+
continue
|
| 559 |
+
if neighbor in queue:
|
| 560 |
+
is_cyclic = True
|
| 561 |
+
try:
|
| 562 |
+
# Checks if point has its vel defined in req frame
|
| 563 |
+
neighbor_velocity = neighbor._vel_dict[frame]
|
| 564 |
+
except KeyError:
|
| 565 |
+
queue.append(neighbor)
|
| 566 |
+
continue
|
| 567 |
+
candidate_neighbor.append(neighbor)
|
| 568 |
+
if not valid_neighbor_found:
|
| 569 |
+
self.set_vel(frame, self.pos_from(neighbor).dt(frame) + neighbor_velocity)
|
| 570 |
+
valid_neighbor_found = True
|
| 571 |
+
if is_cyclic:
|
| 572 |
+
warn(filldedent("""
|
| 573 |
+
Kinematic loops are defined among the positions of points. This
|
| 574 |
+
is likely not desired and may cause errors in your calculations.
|
| 575 |
+
"""))
|
| 576 |
+
if len(candidate_neighbor) > 1:
|
| 577 |
+
warn(filldedent(f"""
|
| 578 |
+
Velocity of {self.name} automatically calculated based on point
|
| 579 |
+
{candidate_neighbor[0].name} but it is also possible from
|
| 580 |
+
points(s): {str(candidate_neighbor[1:])}. Velocities from these
|
| 581 |
+
points are not necessarily the same. This may cause errors in
|
| 582 |
+
your calculations."""))
|
| 583 |
+
if valid_neighbor_found:
|
| 584 |
+
return self._vel_dict[frame]
|
| 585 |
+
else:
|
| 586 |
+
raise ValueError(filldedent(f"""
|
| 587 |
+
Velocity of point {self.name} has not been defined in
|
| 588 |
+
ReferenceFrame {frame.name}."""))
|
| 589 |
+
|
| 590 |
+
return self._vel_dict[frame]
|
| 591 |
+
|
| 592 |
+
def partial_velocity(self, frame, *gen_speeds):
|
| 593 |
+
"""Returns the partial velocities of the linear velocity vector of this
|
| 594 |
+
point in the given frame with respect to one or more provided
|
| 595 |
+
generalized speeds.
|
| 596 |
+
|
| 597 |
+
Parameters
|
| 598 |
+
==========
|
| 599 |
+
frame : ReferenceFrame
|
| 600 |
+
The frame with which the velocity is defined in.
|
| 601 |
+
gen_speeds : functions of time
|
| 602 |
+
The generalized speeds.
|
| 603 |
+
|
| 604 |
+
Returns
|
| 605 |
+
=======
|
| 606 |
+
partial_velocities : tuple of Vector
|
| 607 |
+
The partial velocity vectors corresponding to the provided
|
| 608 |
+
generalized speeds.
|
| 609 |
+
|
| 610 |
+
Examples
|
| 611 |
+
========
|
| 612 |
+
|
| 613 |
+
>>> from sympy.physics.vector import ReferenceFrame, Point
|
| 614 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 615 |
+
>>> N = ReferenceFrame('N')
|
| 616 |
+
>>> A = ReferenceFrame('A')
|
| 617 |
+
>>> p = Point('p')
|
| 618 |
+
>>> u1, u2 = dynamicsymbols('u1, u2')
|
| 619 |
+
>>> p.set_vel(N, u1 * N.x + u2 * A.y)
|
| 620 |
+
>>> p.partial_velocity(N, u1)
|
| 621 |
+
N.x
|
| 622 |
+
>>> p.partial_velocity(N, u1, u2)
|
| 623 |
+
(N.x, A.y)
|
| 624 |
+
|
| 625 |
+
"""
|
| 626 |
+
|
| 627 |
+
from sympy.physics.vector.functions import partial_velocity
|
| 628 |
+
|
| 629 |
+
vel = self.vel(frame)
|
| 630 |
+
partials = partial_velocity([vel], gen_speeds, frame)[0]
|
| 631 |
+
|
| 632 |
+
if len(partials) == 1:
|
| 633 |
+
return partials[0]
|
| 634 |
+
else:
|
| 635 |
+
return tuple(partials)
|
hug/lib/python3.12/site-packages/sympy/physics/vector/printing.py
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.function import Derivative
|
| 2 |
+
from sympy.core.function import UndefinedFunction, AppliedUndef
|
| 3 |
+
from sympy.core.symbol import Symbol
|
| 4 |
+
from sympy.interactive.printing import init_printing
|
| 5 |
+
from sympy.printing.latex import LatexPrinter
|
| 6 |
+
from sympy.printing.pretty.pretty import PrettyPrinter
|
| 7 |
+
from sympy.printing.pretty.pretty_symbology import center_accent
|
| 8 |
+
from sympy.printing.str import StrPrinter
|
| 9 |
+
from sympy.printing.precedence import PRECEDENCE
|
| 10 |
+
|
| 11 |
+
__all__ = ['vprint', 'vsstrrepr', 'vsprint', 'vpprint', 'vlatex',
|
| 12 |
+
'init_vprinting']
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class VectorStrPrinter(StrPrinter):
|
| 16 |
+
"""String Printer for vector expressions. """
|
| 17 |
+
|
| 18 |
+
def _print_Derivative(self, e):
|
| 19 |
+
from sympy.physics.vector.functions import dynamicsymbols
|
| 20 |
+
t = dynamicsymbols._t
|
| 21 |
+
if (bool(sum(i == t for i in e.variables)) &
|
| 22 |
+
isinstance(type(e.args[0]), UndefinedFunction)):
|
| 23 |
+
ol = str(e.args[0].func)
|
| 24 |
+
for i, v in enumerate(e.variables):
|
| 25 |
+
ol += dynamicsymbols._str
|
| 26 |
+
return ol
|
| 27 |
+
else:
|
| 28 |
+
return StrPrinter().doprint(e)
|
| 29 |
+
|
| 30 |
+
def _print_Function(self, e):
|
| 31 |
+
from sympy.physics.vector.functions import dynamicsymbols
|
| 32 |
+
t = dynamicsymbols._t
|
| 33 |
+
if isinstance(type(e), UndefinedFunction):
|
| 34 |
+
return StrPrinter().doprint(e).replace("(%s)" % t, '')
|
| 35 |
+
return e.func.__name__ + "(%s)" % self.stringify(e.args, ", ")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class VectorStrReprPrinter(VectorStrPrinter):
|
| 39 |
+
"""String repr printer for vector expressions."""
|
| 40 |
+
def _print_str(self, s):
|
| 41 |
+
return repr(s)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class VectorLatexPrinter(LatexPrinter):
|
| 45 |
+
"""Latex Printer for vector expressions. """
|
| 46 |
+
|
| 47 |
+
def _print_Function(self, expr, exp=None):
|
| 48 |
+
from sympy.physics.vector.functions import dynamicsymbols
|
| 49 |
+
func = expr.func.__name__
|
| 50 |
+
t = dynamicsymbols._t
|
| 51 |
+
|
| 52 |
+
if (hasattr(self, '_print_' + func) and not
|
| 53 |
+
isinstance(type(expr), UndefinedFunction)):
|
| 54 |
+
return getattr(self, '_print_' + func)(expr, exp)
|
| 55 |
+
elif isinstance(type(expr), UndefinedFunction) and (expr.args == (t,)):
|
| 56 |
+
# treat this function like a symbol
|
| 57 |
+
expr = Symbol(func)
|
| 58 |
+
if exp is not None:
|
| 59 |
+
# copied from LatexPrinter._helper_print_standard_power, which
|
| 60 |
+
# we can't call because we only have exp as a string.
|
| 61 |
+
base = self.parenthesize(expr, PRECEDENCE['Pow'])
|
| 62 |
+
base = self.parenthesize_super(base)
|
| 63 |
+
return r"%s^{%s}" % (base, exp)
|
| 64 |
+
else:
|
| 65 |
+
return super()._print(expr)
|
| 66 |
+
else:
|
| 67 |
+
return super()._print_Function(expr, exp)
|
| 68 |
+
|
| 69 |
+
def _print_Derivative(self, der_expr):
|
| 70 |
+
from sympy.physics.vector.functions import dynamicsymbols
|
| 71 |
+
# make sure it is in the right form
|
| 72 |
+
der_expr = der_expr.doit()
|
| 73 |
+
if not isinstance(der_expr, Derivative):
|
| 74 |
+
return r"\left(%s\right)" % self.doprint(der_expr)
|
| 75 |
+
|
| 76 |
+
# check if expr is a dynamicsymbol
|
| 77 |
+
t = dynamicsymbols._t
|
| 78 |
+
expr = der_expr.expr
|
| 79 |
+
red = expr.atoms(AppliedUndef)
|
| 80 |
+
syms = der_expr.variables
|
| 81 |
+
test1 = not all(True for i in red if i.free_symbols == {t})
|
| 82 |
+
test2 = not all(t == i for i in syms)
|
| 83 |
+
if test1 or test2:
|
| 84 |
+
return super()._print_Derivative(der_expr)
|
| 85 |
+
|
| 86 |
+
# done checking
|
| 87 |
+
dots = len(syms)
|
| 88 |
+
base = self._print_Function(expr)
|
| 89 |
+
base_split = base.split('_', 1)
|
| 90 |
+
base = base_split[0]
|
| 91 |
+
if dots == 1:
|
| 92 |
+
base = r"\dot{%s}" % base
|
| 93 |
+
elif dots == 2:
|
| 94 |
+
base = r"\ddot{%s}" % base
|
| 95 |
+
elif dots == 3:
|
| 96 |
+
base = r"\dddot{%s}" % base
|
| 97 |
+
elif dots == 4:
|
| 98 |
+
base = r"\ddddot{%s}" % base
|
| 99 |
+
else: # Fallback to standard printing
|
| 100 |
+
return super()._print_Derivative(der_expr)
|
| 101 |
+
if len(base_split) != 1:
|
| 102 |
+
base += '_' + base_split[1]
|
| 103 |
+
return base
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
class VectorPrettyPrinter(PrettyPrinter):
|
| 107 |
+
"""Pretty Printer for vectorialexpressions. """
|
| 108 |
+
|
| 109 |
+
def _print_Derivative(self, deriv):
|
| 110 |
+
from sympy.physics.vector.functions import dynamicsymbols
|
| 111 |
+
# XXX use U('PARTIAL DIFFERENTIAL') here ?
|
| 112 |
+
t = dynamicsymbols._t
|
| 113 |
+
dot_i = 0
|
| 114 |
+
syms = list(reversed(deriv.variables))
|
| 115 |
+
|
| 116 |
+
while len(syms) > 0:
|
| 117 |
+
if syms[-1] == t:
|
| 118 |
+
syms.pop()
|
| 119 |
+
dot_i += 1
|
| 120 |
+
else:
|
| 121 |
+
return super()._print_Derivative(deriv)
|
| 122 |
+
|
| 123 |
+
if not (isinstance(type(deriv.expr), UndefinedFunction) and
|
| 124 |
+
(deriv.expr.args == (t,))):
|
| 125 |
+
return super()._print_Derivative(deriv)
|
| 126 |
+
else:
|
| 127 |
+
pform = self._print_Function(deriv.expr)
|
| 128 |
+
|
| 129 |
+
# the following condition would happen with some sort of non-standard
|
| 130 |
+
# dynamic symbol I guess, so we'll just print the SymPy way
|
| 131 |
+
if len(pform.picture) > 1:
|
| 132 |
+
return super()._print_Derivative(deriv)
|
| 133 |
+
|
| 134 |
+
# There are only special symbols up to fourth-order derivatives
|
| 135 |
+
if dot_i >= 5:
|
| 136 |
+
return super()._print_Derivative(deriv)
|
| 137 |
+
|
| 138 |
+
# Deal with special symbols
|
| 139 |
+
dots = {0: "",
|
| 140 |
+
1: "\N{COMBINING DOT ABOVE}",
|
| 141 |
+
2: "\N{COMBINING DIAERESIS}",
|
| 142 |
+
3: "\N{COMBINING THREE DOTS ABOVE}",
|
| 143 |
+
4: "\N{COMBINING FOUR DOTS ABOVE}"}
|
| 144 |
+
|
| 145 |
+
d = pform.__dict__
|
| 146 |
+
# if unicode is false then calculate number of apostrophes needed and
|
| 147 |
+
# add to output
|
| 148 |
+
if not self._use_unicode:
|
| 149 |
+
apostrophes = ""
|
| 150 |
+
for i in range(0, dot_i):
|
| 151 |
+
apostrophes += "'"
|
| 152 |
+
d['picture'][0] += apostrophes + "(t)"
|
| 153 |
+
else:
|
| 154 |
+
d['picture'] = [center_accent(d['picture'][0], dots[dot_i])]
|
| 155 |
+
return pform
|
| 156 |
+
|
| 157 |
+
def _print_Function(self, e):
|
| 158 |
+
from sympy.physics.vector.functions import dynamicsymbols
|
| 159 |
+
t = dynamicsymbols._t
|
| 160 |
+
# XXX works only for applied functions
|
| 161 |
+
func = e.func
|
| 162 |
+
args = e.args
|
| 163 |
+
func_name = func.__name__
|
| 164 |
+
pform = self._print_Symbol(Symbol(func_name))
|
| 165 |
+
# If this function is an Undefined function of t, it is probably a
|
| 166 |
+
# dynamic symbol, so we'll skip the (t). The rest of the code is
|
| 167 |
+
# identical to the normal PrettyPrinter code
|
| 168 |
+
if not (isinstance(func, UndefinedFunction) and (args == (t,))):
|
| 169 |
+
return super()._print_Function(e)
|
| 170 |
+
return pform
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def vprint(expr, **settings):
|
| 174 |
+
r"""Function for printing of expressions generated in the
|
| 175 |
+
sympy.physics vector package.
|
| 176 |
+
|
| 177 |
+
Extends SymPy's StrPrinter, takes the same setting accepted by SymPy's
|
| 178 |
+
:func:`~.sstr`, and is equivalent to ``print(sstr(foo))``.
|
| 179 |
+
|
| 180 |
+
Parameters
|
| 181 |
+
==========
|
| 182 |
+
|
| 183 |
+
expr : valid SymPy object
|
| 184 |
+
SymPy expression to print.
|
| 185 |
+
settings : args
|
| 186 |
+
Same as the settings accepted by SymPy's sstr().
|
| 187 |
+
|
| 188 |
+
Examples
|
| 189 |
+
========
|
| 190 |
+
|
| 191 |
+
>>> from sympy.physics.vector import vprint, dynamicsymbols
|
| 192 |
+
>>> u1 = dynamicsymbols('u1')
|
| 193 |
+
>>> print(u1)
|
| 194 |
+
u1(t)
|
| 195 |
+
>>> vprint(u1)
|
| 196 |
+
u1
|
| 197 |
+
|
| 198 |
+
"""
|
| 199 |
+
|
| 200 |
+
outstr = vsprint(expr, **settings)
|
| 201 |
+
|
| 202 |
+
import builtins
|
| 203 |
+
if (outstr != 'None'):
|
| 204 |
+
builtins._ = outstr
|
| 205 |
+
print(outstr)
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def vsstrrepr(expr, **settings):
|
| 209 |
+
"""Function for displaying expression representation's with vector
|
| 210 |
+
printing enabled.
|
| 211 |
+
|
| 212 |
+
Parameters
|
| 213 |
+
==========
|
| 214 |
+
|
| 215 |
+
expr : valid SymPy object
|
| 216 |
+
SymPy expression to print.
|
| 217 |
+
settings : args
|
| 218 |
+
Same as the settings accepted by SymPy's sstrrepr().
|
| 219 |
+
|
| 220 |
+
"""
|
| 221 |
+
p = VectorStrReprPrinter(settings)
|
| 222 |
+
return p.doprint(expr)
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def vsprint(expr, **settings):
|
| 226 |
+
r"""Function for displaying expressions generated in the
|
| 227 |
+
sympy.physics vector package.
|
| 228 |
+
|
| 229 |
+
Returns the output of vprint() as a string.
|
| 230 |
+
|
| 231 |
+
Parameters
|
| 232 |
+
==========
|
| 233 |
+
|
| 234 |
+
expr : valid SymPy object
|
| 235 |
+
SymPy expression to print
|
| 236 |
+
settings : args
|
| 237 |
+
Same as the settings accepted by SymPy's sstr().
|
| 238 |
+
|
| 239 |
+
Examples
|
| 240 |
+
========
|
| 241 |
+
|
| 242 |
+
>>> from sympy.physics.vector import vsprint, dynamicsymbols
|
| 243 |
+
>>> u1, u2 = dynamicsymbols('u1 u2')
|
| 244 |
+
>>> u2d = dynamicsymbols('u2', level=1)
|
| 245 |
+
>>> print("%s = %s" % (u1, u2 + u2d))
|
| 246 |
+
u1(t) = u2(t) + Derivative(u2(t), t)
|
| 247 |
+
>>> print("%s = %s" % (vsprint(u1), vsprint(u2 + u2d)))
|
| 248 |
+
u1 = u2 + u2'
|
| 249 |
+
|
| 250 |
+
"""
|
| 251 |
+
|
| 252 |
+
string_printer = VectorStrPrinter(settings)
|
| 253 |
+
return string_printer.doprint(expr)
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
def vpprint(expr, **settings):
|
| 257 |
+
r"""Function for pretty printing of expressions generated in the
|
| 258 |
+
sympy.physics vector package.
|
| 259 |
+
|
| 260 |
+
Mainly used for expressions not inside a vector; the output of running
|
| 261 |
+
scripts and generating equations of motion. Takes the same options as
|
| 262 |
+
SymPy's :func:`~.pretty_print`; see that function for more information.
|
| 263 |
+
|
| 264 |
+
Parameters
|
| 265 |
+
==========
|
| 266 |
+
|
| 267 |
+
expr : valid SymPy object
|
| 268 |
+
SymPy expression to pretty print
|
| 269 |
+
settings : args
|
| 270 |
+
Same as those accepted by SymPy's pretty_print.
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
"""
|
| 274 |
+
|
| 275 |
+
pp = VectorPrettyPrinter(settings)
|
| 276 |
+
|
| 277 |
+
# Note that this is copied from sympy.printing.pretty.pretty_print:
|
| 278 |
+
|
| 279 |
+
# XXX: this is an ugly hack, but at least it works
|
| 280 |
+
use_unicode = pp._settings['use_unicode']
|
| 281 |
+
from sympy.printing.pretty.pretty_symbology import pretty_use_unicode
|
| 282 |
+
uflag = pretty_use_unicode(use_unicode)
|
| 283 |
+
|
| 284 |
+
try:
|
| 285 |
+
return pp.doprint(expr)
|
| 286 |
+
finally:
|
| 287 |
+
pretty_use_unicode(uflag)
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
def vlatex(expr, **settings):
|
| 291 |
+
r"""Function for printing latex representation of sympy.physics.vector
|
| 292 |
+
objects.
|
| 293 |
+
|
| 294 |
+
For latex representation of Vectors, Dyadics, and dynamicsymbols. Takes the
|
| 295 |
+
same options as SymPy's :func:`~.latex`; see that function for more
|
| 296 |
+
information;
|
| 297 |
+
|
| 298 |
+
Parameters
|
| 299 |
+
==========
|
| 300 |
+
|
| 301 |
+
expr : valid SymPy object
|
| 302 |
+
SymPy expression to represent in LaTeX form
|
| 303 |
+
settings : args
|
| 304 |
+
Same as latex()
|
| 305 |
+
|
| 306 |
+
Examples
|
| 307 |
+
========
|
| 308 |
+
|
| 309 |
+
>>> from sympy.physics.vector import vlatex, ReferenceFrame, dynamicsymbols
|
| 310 |
+
>>> N = ReferenceFrame('N')
|
| 311 |
+
>>> q1, q2 = dynamicsymbols('q1 q2')
|
| 312 |
+
>>> q1d, q2d = dynamicsymbols('q1 q2', 1)
|
| 313 |
+
>>> q1dd, q2dd = dynamicsymbols('q1 q2', 2)
|
| 314 |
+
>>> vlatex(N.x + N.y)
|
| 315 |
+
'\\mathbf{\\hat{n}_x} + \\mathbf{\\hat{n}_y}'
|
| 316 |
+
>>> vlatex(q1 + q2)
|
| 317 |
+
'q_{1} + q_{2}'
|
| 318 |
+
>>> vlatex(q1d)
|
| 319 |
+
'\\dot{q}_{1}'
|
| 320 |
+
>>> vlatex(q1 * q2d)
|
| 321 |
+
'q_{1} \\dot{q}_{2}'
|
| 322 |
+
>>> vlatex(q1dd * q1 / q1d)
|
| 323 |
+
'\\frac{q_{1} \\ddot{q}_{1}}{\\dot{q}_{1}}'
|
| 324 |
+
|
| 325 |
+
"""
|
| 326 |
+
latex_printer = VectorLatexPrinter(settings)
|
| 327 |
+
|
| 328 |
+
return latex_printer.doprint(expr)
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
def init_vprinting(**kwargs):
|
| 332 |
+
"""Initializes time derivative printing for all SymPy objects, i.e. any
|
| 333 |
+
functions of time will be displayed in a more compact notation. The main
|
| 334 |
+
benefit of this is for printing of time derivatives; instead of
|
| 335 |
+
displaying as ``Derivative(f(t),t)``, it will display ``f'``. This is
|
| 336 |
+
only actually needed for when derivatives are present and are not in a
|
| 337 |
+
physics.vector.Vector or physics.vector.Dyadic object. This function is a
|
| 338 |
+
light wrapper to :func:`~.init_printing`. Any keyword
|
| 339 |
+
arguments for it are valid here.
|
| 340 |
+
|
| 341 |
+
{0}
|
| 342 |
+
|
| 343 |
+
Examples
|
| 344 |
+
========
|
| 345 |
+
|
| 346 |
+
>>> from sympy import Function, symbols
|
| 347 |
+
>>> t, x = symbols('t, x')
|
| 348 |
+
>>> omega = Function('omega')
|
| 349 |
+
>>> omega(x).diff()
|
| 350 |
+
Derivative(omega(x), x)
|
| 351 |
+
>>> omega(t).diff()
|
| 352 |
+
Derivative(omega(t), t)
|
| 353 |
+
|
| 354 |
+
Now use the string printer:
|
| 355 |
+
|
| 356 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 357 |
+
>>> init_vprinting(pretty_print=False)
|
| 358 |
+
>>> omega(x).diff()
|
| 359 |
+
Derivative(omega(x), x)
|
| 360 |
+
>>> omega(t).diff()
|
| 361 |
+
omega'
|
| 362 |
+
|
| 363 |
+
"""
|
| 364 |
+
kwargs['str_printer'] = vsstrrepr
|
| 365 |
+
kwargs['pretty_printer'] = vpprint
|
| 366 |
+
kwargs['latex_printer'] = vlatex
|
| 367 |
+
init_printing(**kwargs)
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
params = init_printing.__doc__.split('Examples\n ========')[0] # type: ignore
|
| 371 |
+
init_vprinting.__doc__ = init_vprinting.__doc__.format(params) # type: ignore
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__init__.py
ADDED
|
File without changes
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (193 Bytes). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_dyadic.cpython-312.pyc
ADDED
|
Binary file (11.6 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_fieldfunctions.cpython-312.pyc
ADDED
|
Binary file (16.5 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_frame.cpython-312.pyc
ADDED
|
Binary file (68.1 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_functions.cpython-312.pyc
ADDED
|
Binary file (62.9 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_output.cpython-312.pyc
ADDED
|
Binary file (8.57 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_point.cpython-312.pyc
ADDED
|
Binary file (30.3 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_printing.cpython-312.pyc
ADDED
|
Binary file (17.3 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/__pycache__/test_vector.cpython-312.pyc
ADDED
|
Binary file (26.7 kB). View file
|
|
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/test_dyadic.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.numbers import (Float, pi)
|
| 2 |
+
from sympy.core.symbol import symbols
|
| 3 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 4 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
|
| 5 |
+
from sympy.physics.vector import ReferenceFrame, dynamicsymbols, outer
|
| 6 |
+
from sympy.physics.vector.dyadic import _check_dyadic
|
| 7 |
+
from sympy.testing.pytest import raises
|
| 8 |
+
|
| 9 |
+
A = ReferenceFrame('A')
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_dyadic():
|
| 13 |
+
d1 = A.x | A.x
|
| 14 |
+
d2 = A.y | A.y
|
| 15 |
+
d3 = A.x | A.y
|
| 16 |
+
assert d1 * 0 == 0
|
| 17 |
+
assert d1 != 0
|
| 18 |
+
assert d1 * 2 == 2 * A.x | A.x
|
| 19 |
+
assert d1 / 2. == 0.5 * d1
|
| 20 |
+
assert d1 & (0 * d1) == 0
|
| 21 |
+
assert d1 & d2 == 0
|
| 22 |
+
assert d1 & A.x == A.x
|
| 23 |
+
assert d1 ^ A.x == 0
|
| 24 |
+
assert d1 ^ A.y == A.x | A.z
|
| 25 |
+
assert d1 ^ A.z == - A.x | A.y
|
| 26 |
+
assert d2 ^ A.x == - A.y | A.z
|
| 27 |
+
assert A.x ^ d1 == 0
|
| 28 |
+
assert A.y ^ d1 == - A.z | A.x
|
| 29 |
+
assert A.z ^ d1 == A.y | A.x
|
| 30 |
+
assert A.x & d1 == A.x
|
| 31 |
+
assert A.y & d1 == 0
|
| 32 |
+
assert A.y & d2 == A.y
|
| 33 |
+
assert d1 & d3 == A.x | A.y
|
| 34 |
+
assert d3 & d1 == 0
|
| 35 |
+
assert d1.dt(A) == 0
|
| 36 |
+
q = dynamicsymbols('q')
|
| 37 |
+
qd = dynamicsymbols('q', 1)
|
| 38 |
+
B = A.orientnew('B', 'Axis', [q, A.z])
|
| 39 |
+
assert d1.express(B) == d1.express(B, B)
|
| 40 |
+
assert d1.express(B) == ((cos(q)**2) * (B.x | B.x) + (-sin(q) * cos(q)) *
|
| 41 |
+
(B.x | B.y) + (-sin(q) * cos(q)) * (B.y | B.x) + (sin(q)**2) *
|
| 42 |
+
(B.y | B.y))
|
| 43 |
+
assert d1.express(B, A) == (cos(q)) * (B.x | A.x) + (-sin(q)) * (B.y | A.x)
|
| 44 |
+
assert d1.express(A, B) == (cos(q)) * (A.x | B.x) + (-sin(q)) * (A.x | B.y)
|
| 45 |
+
assert d1.dt(B) == (-qd) * (A.y | A.x) + (-qd) * (A.x | A.y)
|
| 46 |
+
|
| 47 |
+
assert d1.to_matrix(A) == Matrix([[1, 0, 0], [0, 0, 0], [0, 0, 0]])
|
| 48 |
+
assert d1.to_matrix(A, B) == Matrix([[cos(q), -sin(q), 0],
|
| 49 |
+
[0, 0, 0],
|
| 50 |
+
[0, 0, 0]])
|
| 51 |
+
assert d3.to_matrix(A) == Matrix([[0, 1, 0], [0, 0, 0], [0, 0, 0]])
|
| 52 |
+
a, b, c, d, e, f = symbols('a, b, c, d, e, f')
|
| 53 |
+
v1 = a * A.x + b * A.y + c * A.z
|
| 54 |
+
v2 = d * A.x + e * A.y + f * A.z
|
| 55 |
+
d4 = v1.outer(v2)
|
| 56 |
+
assert d4.to_matrix(A) == Matrix([[a * d, a * e, a * f],
|
| 57 |
+
[b * d, b * e, b * f],
|
| 58 |
+
[c * d, c * e, c * f]])
|
| 59 |
+
d5 = v1.outer(v1)
|
| 60 |
+
C = A.orientnew('C', 'Axis', [q, A.x])
|
| 61 |
+
for expected, actual in zip(C.dcm(A) * d5.to_matrix(A) * C.dcm(A).T,
|
| 62 |
+
d5.to_matrix(C)):
|
| 63 |
+
assert (expected - actual).simplify() == 0
|
| 64 |
+
|
| 65 |
+
raises(TypeError, lambda: d1.applyfunc(0))
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_dyadic_simplify():
|
| 69 |
+
x, y, z, k, n, m, w, f, s, A = symbols('x, y, z, k, n, m, w, f, s, A')
|
| 70 |
+
N = ReferenceFrame('N')
|
| 71 |
+
|
| 72 |
+
dy = N.x | N.x
|
| 73 |
+
test1 = (1 / x + 1 / y) * dy
|
| 74 |
+
assert (N.x & test1 & N.x) != (x + y) / (x * y)
|
| 75 |
+
test1 = test1.simplify()
|
| 76 |
+
assert (N.x & test1 & N.x) == (x + y) / (x * y)
|
| 77 |
+
|
| 78 |
+
test2 = (A**2 * s**4 / (4 * pi * k * m**3)) * dy
|
| 79 |
+
test2 = test2.simplify()
|
| 80 |
+
assert (N.x & test2 & N.x) == (A**2 * s**4 / (4 * pi * k * m**3))
|
| 81 |
+
|
| 82 |
+
test3 = ((4 + 4 * x - 2 * (2 + 2 * x)) / (2 + 2 * x)) * dy
|
| 83 |
+
test3 = test3.simplify()
|
| 84 |
+
assert (N.x & test3 & N.x) == 0
|
| 85 |
+
|
| 86 |
+
test4 = ((-4 * x * y**2 - 2 * y**3 - 2 * x**2 * y) / (x + y)**2) * dy
|
| 87 |
+
test4 = test4.simplify()
|
| 88 |
+
assert (N.x & test4 & N.x) == -2 * y
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def test_dyadic_subs():
|
| 92 |
+
N = ReferenceFrame('N')
|
| 93 |
+
s = symbols('s')
|
| 94 |
+
a = s*(N.x | N.x)
|
| 95 |
+
assert a.subs({s: 2}) == 2*(N.x | N.x)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def test_check_dyadic():
|
| 99 |
+
raises(TypeError, lambda: _check_dyadic(0))
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def test_dyadic_evalf():
|
| 103 |
+
N = ReferenceFrame('N')
|
| 104 |
+
a = pi * (N.x | N.x)
|
| 105 |
+
assert a.evalf(3) == Float('3.1416', 3) * (N.x | N.x)
|
| 106 |
+
s = symbols('s')
|
| 107 |
+
a = 5 * s * pi* (N.x | N.x)
|
| 108 |
+
assert a.evalf(2) == Float('5', 2) * Float('3.1416', 2) * s * (N.x | N.x)
|
| 109 |
+
assert a.evalf(9, subs={s: 5.124}) == Float('80.48760378', 9) * (N.x | N.x)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def test_dyadic_xreplace():
|
| 113 |
+
x, y, z = symbols('x y z')
|
| 114 |
+
N = ReferenceFrame('N')
|
| 115 |
+
D = outer(N.x, N.x)
|
| 116 |
+
v = x*y * D
|
| 117 |
+
assert v.xreplace({x : cos(x)}) == cos(x)*y * D
|
| 118 |
+
assert v.xreplace({x*y : pi}) == pi * D
|
| 119 |
+
v = (x*y)**z * D
|
| 120 |
+
assert v.xreplace({(x*y)**z : 1}) == D
|
| 121 |
+
assert v.xreplace({x:1, z:0}) == D
|
| 122 |
+
raises(TypeError, lambda: v.xreplace())
|
| 123 |
+
raises(TypeError, lambda: v.xreplace([x, y]))
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/test_fieldfunctions.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.singleton import S
|
| 2 |
+
from sympy.core.symbol import Symbol
|
| 3 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 4 |
+
from sympy.physics.vector import ReferenceFrame, Vector, Point, \
|
| 5 |
+
dynamicsymbols
|
| 6 |
+
from sympy.physics.vector.fieldfunctions import divergence, \
|
| 7 |
+
gradient, curl, is_conservative, is_solenoidal, \
|
| 8 |
+
scalar_potential, scalar_potential_difference
|
| 9 |
+
from sympy.testing.pytest import raises
|
| 10 |
+
|
| 11 |
+
R = ReferenceFrame('R')
|
| 12 |
+
q = dynamicsymbols('q')
|
| 13 |
+
P = R.orientnew('P', 'Axis', [q, R.z])
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_curl():
|
| 17 |
+
assert curl(Vector(0), R) == Vector(0)
|
| 18 |
+
assert curl(R.x, R) == Vector(0)
|
| 19 |
+
assert curl(2*R[1]**2*R.y, R) == Vector(0)
|
| 20 |
+
assert curl(R[0]*R[1]*R.z, R) == R[0]*R.x - R[1]*R.y
|
| 21 |
+
assert curl(R[0]*R[1]*R[2] * (R.x+R.y+R.z), R) == \
|
| 22 |
+
(-R[0]*R[1] + R[0]*R[2])*R.x + (R[0]*R[1] - R[1]*R[2])*R.y + \
|
| 23 |
+
(-R[0]*R[2] + R[1]*R[2])*R.z
|
| 24 |
+
assert curl(2*R[0]**2*R.y, R) == 4*R[0]*R.z
|
| 25 |
+
assert curl(P[0]**2*R.x + P.y, R) == \
|
| 26 |
+
- 2*(R[0]*cos(q) + R[1]*sin(q))*sin(q)*R.z
|
| 27 |
+
assert curl(P[0]*R.y, P) == cos(q)*P.z
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_divergence():
|
| 31 |
+
assert divergence(Vector(0), R) is S.Zero
|
| 32 |
+
assert divergence(R.x, R) is S.Zero
|
| 33 |
+
assert divergence(R[0]**2*R.x, R) == 2*R[0]
|
| 34 |
+
assert divergence(R[0]*R[1]*R[2] * (R.x+R.y+R.z), R) == \
|
| 35 |
+
R[0]*R[1] + R[0]*R[2] + R[1]*R[2]
|
| 36 |
+
assert divergence((1/(R[0]*R[1]*R[2])) * (R.x+R.y+R.z), R) == \
|
| 37 |
+
-1/(R[0]*R[1]*R[2]**2) - 1/(R[0]*R[1]**2*R[2]) - \
|
| 38 |
+
1/(R[0]**2*R[1]*R[2])
|
| 39 |
+
v = P[0]*P.x + P[1]*P.y + P[2]*P.z
|
| 40 |
+
assert divergence(v, P) == 3
|
| 41 |
+
assert divergence(v, R).simplify() == 3
|
| 42 |
+
assert divergence(P[0]*R.x + R[0]*P.x, R) == 2*cos(q)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def test_gradient():
|
| 46 |
+
a = Symbol('a')
|
| 47 |
+
assert gradient(0, R) == Vector(0)
|
| 48 |
+
assert gradient(R[0], R) == R.x
|
| 49 |
+
assert gradient(R[0]*R[1]*R[2], R) == \
|
| 50 |
+
R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z
|
| 51 |
+
assert gradient(2*R[0]**2, R) == 4*R[0]*R.x
|
| 52 |
+
assert gradient(a*sin(R[1])/R[0], R) == \
|
| 53 |
+
- a*sin(R[1])/R[0]**2*R.x + a*cos(R[1])/R[0]*R.y
|
| 54 |
+
assert gradient(P[0]*P[1], R) == \
|
| 55 |
+
((-R[0]*sin(q) + R[1]*cos(q))*cos(q) - (R[0]*cos(q) + R[1]*sin(q))*sin(q))*R.x + \
|
| 56 |
+
((-R[0]*sin(q) + R[1]*cos(q))*sin(q) + (R[0]*cos(q) + R[1]*sin(q))*cos(q))*R.y
|
| 57 |
+
assert gradient(P[0]*R[2], P) == P[2]*P.x + P[0]*P.z
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
scalar_field = 2*R[0]**2*R[1]*R[2]
|
| 61 |
+
grad_field = gradient(scalar_field, R)
|
| 62 |
+
vector_field = R[1]**2*R.x + 3*R[0]*R.y + 5*R[1]*R[2]*R.z
|
| 63 |
+
curl_field = curl(vector_field, R)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def test_conservative():
|
| 67 |
+
assert is_conservative(0) is True
|
| 68 |
+
assert is_conservative(R.x) is True
|
| 69 |
+
assert is_conservative(2 * R.x + 3 * R.y + 4 * R.z) is True
|
| 70 |
+
assert is_conservative(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z) is \
|
| 71 |
+
True
|
| 72 |
+
assert is_conservative(R[0] * R.y) is False
|
| 73 |
+
assert is_conservative(grad_field) is True
|
| 74 |
+
assert is_conservative(curl_field) is False
|
| 75 |
+
assert is_conservative(4*R[0]*R[1]*R[2]*R.x + 2*R[0]**2*R[2]*R.y) is \
|
| 76 |
+
False
|
| 77 |
+
assert is_conservative(R[2]*P.x + P[0]*R.z) is True
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def test_solenoidal():
|
| 81 |
+
assert is_solenoidal(0) is True
|
| 82 |
+
assert is_solenoidal(R.x) is True
|
| 83 |
+
assert is_solenoidal(2 * R.x + 3 * R.y + 4 * R.z) is True
|
| 84 |
+
assert is_solenoidal(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z) is \
|
| 85 |
+
True
|
| 86 |
+
assert is_solenoidal(R[1] * R.y) is False
|
| 87 |
+
assert is_solenoidal(grad_field) is False
|
| 88 |
+
assert is_solenoidal(curl_field) is True
|
| 89 |
+
assert is_solenoidal((-2*R[1] + 3)*R.z) is True
|
| 90 |
+
assert is_solenoidal(cos(q)*R.x + sin(q)*R.y + cos(q)*P.z) is True
|
| 91 |
+
assert is_solenoidal(R[2]*P.x + P[0]*R.z) is True
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def test_scalar_potential():
|
| 95 |
+
assert scalar_potential(0, R) == 0
|
| 96 |
+
assert scalar_potential(R.x, R) == R[0]
|
| 97 |
+
assert scalar_potential(R.y, R) == R[1]
|
| 98 |
+
assert scalar_potential(R.z, R) == R[2]
|
| 99 |
+
assert scalar_potential(R[1]*R[2]*R.x + R[0]*R[2]*R.y + \
|
| 100 |
+
R[0]*R[1]*R.z, R) == R[0]*R[1]*R[2]
|
| 101 |
+
assert scalar_potential(grad_field, R) == scalar_field
|
| 102 |
+
assert scalar_potential(R[2]*P.x + P[0]*R.z, R) == \
|
| 103 |
+
R[0]*R[2]*cos(q) + R[1]*R[2]*sin(q)
|
| 104 |
+
assert scalar_potential(R[2]*P.x + P[0]*R.z, P) == P[0]*P[2]
|
| 105 |
+
raises(ValueError, lambda: scalar_potential(R[0] * R.y, R))
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def test_scalar_potential_difference():
|
| 109 |
+
origin = Point('O')
|
| 110 |
+
point1 = origin.locatenew('P1', 1*R.x + 2*R.y + 3*R.z)
|
| 111 |
+
point2 = origin.locatenew('P2', 4*R.x + 5*R.y + 6*R.z)
|
| 112 |
+
genericpointR = origin.locatenew('RP', R[0]*R.x + R[1]*R.y + R[2]*R.z)
|
| 113 |
+
genericpointP = origin.locatenew('PP', P[0]*P.x + P[1]*P.y + P[2]*P.z)
|
| 114 |
+
assert scalar_potential_difference(S.Zero, R, point1, point2, \
|
| 115 |
+
origin) == 0
|
| 116 |
+
assert scalar_potential_difference(scalar_field, R, origin, \
|
| 117 |
+
genericpointR, origin) == \
|
| 118 |
+
scalar_field
|
| 119 |
+
assert scalar_potential_difference(grad_field, R, origin, \
|
| 120 |
+
genericpointR, origin) == \
|
| 121 |
+
scalar_field
|
| 122 |
+
assert scalar_potential_difference(grad_field, R, point1, point2,
|
| 123 |
+
origin) == 948
|
| 124 |
+
assert scalar_potential_difference(R[1]*R[2]*R.x + R[0]*R[2]*R.y + \
|
| 125 |
+
R[0]*R[1]*R.z, R, point1,
|
| 126 |
+
genericpointR, origin) == \
|
| 127 |
+
R[0]*R[1]*R[2] - 6
|
| 128 |
+
potential_diff_P = 2*P[2]*(P[0]*sin(q) + P[1]*cos(q))*\
|
| 129 |
+
(P[0]*cos(q) - P[1]*sin(q))**2
|
| 130 |
+
assert scalar_potential_difference(grad_field, P, origin, \
|
| 131 |
+
genericpointP, \
|
| 132 |
+
origin).simplify() == \
|
| 133 |
+
potential_diff_P
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/test_frame.py
ADDED
|
@@ -0,0 +1,761 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.numbers import pi
|
| 2 |
+
from sympy.core.symbol import symbols
|
| 3 |
+
from sympy.simplify import trigsimp
|
| 4 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 5 |
+
from sympy.matrices.dense import (eye, zeros)
|
| 6 |
+
from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
|
| 7 |
+
from sympy.simplify.simplify import simplify
|
| 8 |
+
from sympy.physics.vector import (ReferenceFrame, Vector, CoordinateSym,
|
| 9 |
+
dynamicsymbols, time_derivative, express,
|
| 10 |
+
dot)
|
| 11 |
+
from sympy.physics.vector.frame import _check_frame
|
| 12 |
+
from sympy.physics.vector.vector import VectorTypeError
|
| 13 |
+
from sympy.testing.pytest import raises
|
| 14 |
+
import warnings
|
| 15 |
+
import pickle
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_dict_list():
|
| 19 |
+
|
| 20 |
+
A = ReferenceFrame('A')
|
| 21 |
+
B = ReferenceFrame('B')
|
| 22 |
+
C = ReferenceFrame('C')
|
| 23 |
+
D = ReferenceFrame('D')
|
| 24 |
+
E = ReferenceFrame('E')
|
| 25 |
+
F = ReferenceFrame('F')
|
| 26 |
+
|
| 27 |
+
B.orient_axis(A, A.x, 1.0)
|
| 28 |
+
C.orient_axis(B, B.x, 1.0)
|
| 29 |
+
D.orient_axis(C, C.x, 1.0)
|
| 30 |
+
|
| 31 |
+
assert D._dict_list(A, 0) == [D, C, B, A]
|
| 32 |
+
|
| 33 |
+
E.orient_axis(D, D.x, 1.0)
|
| 34 |
+
|
| 35 |
+
assert C._dict_list(A, 0) == [C, B, A]
|
| 36 |
+
assert C._dict_list(E, 0) == [C, D, E]
|
| 37 |
+
|
| 38 |
+
# only 0, 1, 2 permitted for second argument
|
| 39 |
+
raises(ValueError, lambda: C._dict_list(E, 5))
|
| 40 |
+
# no connecting path
|
| 41 |
+
raises(ValueError, lambda: F._dict_list(A, 0))
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_coordinate_vars():
|
| 45 |
+
"""Tests the coordinate variables functionality"""
|
| 46 |
+
A = ReferenceFrame('A')
|
| 47 |
+
assert CoordinateSym('Ax', A, 0) == A[0]
|
| 48 |
+
assert CoordinateSym('Ax', A, 1) == A[1]
|
| 49 |
+
assert CoordinateSym('Ax', A, 2) == A[2]
|
| 50 |
+
raises(ValueError, lambda: CoordinateSym('Ax', A, 3))
|
| 51 |
+
q = dynamicsymbols('q')
|
| 52 |
+
qd = dynamicsymbols('q', 1)
|
| 53 |
+
assert isinstance(A[0], CoordinateSym) and \
|
| 54 |
+
isinstance(A[0], CoordinateSym) and \
|
| 55 |
+
isinstance(A[0], CoordinateSym)
|
| 56 |
+
assert A.variable_map(A) == {A[0]:A[0], A[1]:A[1], A[2]:A[2]}
|
| 57 |
+
assert A[0].frame == A
|
| 58 |
+
B = A.orientnew('B', 'Axis', [q, A.z])
|
| 59 |
+
assert B.variable_map(A) == {B[2]: A[2], B[1]: -A[0]*sin(q) + A[1]*cos(q),
|
| 60 |
+
B[0]: A[0]*cos(q) + A[1]*sin(q)}
|
| 61 |
+
assert A.variable_map(B) == {A[0]: B[0]*cos(q) - B[1]*sin(q),
|
| 62 |
+
A[1]: B[0]*sin(q) + B[1]*cos(q), A[2]: B[2]}
|
| 63 |
+
assert time_derivative(B[0], A) == -A[0]*sin(q)*qd + A[1]*cos(q)*qd
|
| 64 |
+
assert time_derivative(B[1], A) == -A[0]*cos(q)*qd - A[1]*sin(q)*qd
|
| 65 |
+
assert time_derivative(B[2], A) == 0
|
| 66 |
+
assert express(B[0], A, variables=True) == A[0]*cos(q) + A[1]*sin(q)
|
| 67 |
+
assert express(B[1], A, variables=True) == -A[0]*sin(q) + A[1]*cos(q)
|
| 68 |
+
assert express(B[2], A, variables=True) == A[2]
|
| 69 |
+
assert time_derivative(A[0]*A.x + A[1]*A.y + A[2]*A.z, B) == A[1]*qd*A.x - A[0]*qd*A.y
|
| 70 |
+
assert time_derivative(B[0]*B.x + B[1]*B.y + B[2]*B.z, A) == - B[1]*qd*B.x + B[0]*qd*B.y
|
| 71 |
+
assert express(B[0]*B[1]*B[2], A, variables=True) == \
|
| 72 |
+
A[2]*(-A[0]*sin(q) + A[1]*cos(q))*(A[0]*cos(q) + A[1]*sin(q))
|
| 73 |
+
assert (time_derivative(B[0]*B[1]*B[2], A) -
|
| 74 |
+
(A[2]*(-A[0]**2*cos(2*q) -
|
| 75 |
+
2*A[0]*A[1]*sin(2*q) +
|
| 76 |
+
A[1]**2*cos(2*q))*qd)).trigsimp() == 0
|
| 77 |
+
assert express(B[0]*B.x + B[1]*B.y + B[2]*B.z, A) == \
|
| 78 |
+
(B[0]*cos(q) - B[1]*sin(q))*A.x + (B[0]*sin(q) + \
|
| 79 |
+
B[1]*cos(q))*A.y + B[2]*A.z
|
| 80 |
+
assert express(B[0]*B.x + B[1]*B.y + B[2]*B.z, A,
|
| 81 |
+
variables=True).simplify() == A[0]*A.x + A[1]*A.y + A[2]*A.z
|
| 82 |
+
assert express(A[0]*A.x + A[1]*A.y + A[2]*A.z, B) == \
|
| 83 |
+
(A[0]*cos(q) + A[1]*sin(q))*B.x + \
|
| 84 |
+
(-A[0]*sin(q) + A[1]*cos(q))*B.y + A[2]*B.z
|
| 85 |
+
assert express(A[0]*A.x + A[1]*A.y + A[2]*A.z, B,
|
| 86 |
+
variables=True).simplify() == B[0]*B.x + B[1]*B.y + B[2]*B.z
|
| 87 |
+
N = B.orientnew('N', 'Axis', [-q, B.z])
|
| 88 |
+
assert ({k: v.simplify() for k, v in N.variable_map(A).items()} ==
|
| 89 |
+
{N[0]: A[0], N[2]: A[2], N[1]: A[1]})
|
| 90 |
+
C = A.orientnew('C', 'Axis', [q, A.x + A.y + A.z])
|
| 91 |
+
mapping = A.variable_map(C)
|
| 92 |
+
assert trigsimp(mapping[A[0]]) == (2*C[0]*cos(q)/3 + C[0]/3 -
|
| 93 |
+
2*C[1]*sin(q + pi/6)/3 +
|
| 94 |
+
C[1]/3 - 2*C[2]*cos(q + pi/3)/3 +
|
| 95 |
+
C[2]/3)
|
| 96 |
+
assert trigsimp(mapping[A[1]]) == -2*C[0]*cos(q + pi/3)/3 + \
|
| 97 |
+
C[0]/3 + 2*C[1]*cos(q)/3 + C[1]/3 - 2*C[2]*sin(q + pi/6)/3 + C[2]/3
|
| 98 |
+
assert trigsimp(mapping[A[2]]) == -2*C[0]*sin(q + pi/6)/3 + C[0]/3 - \
|
| 99 |
+
2*C[1]*cos(q + pi/3)/3 + C[1]/3 + 2*C[2]*cos(q)/3 + C[2]/3
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def test_ang_vel():
|
| 103 |
+
q1, q2, q3, q4 = dynamicsymbols('q1 q2 q3 q4')
|
| 104 |
+
q1d, q2d, q3d, q4d = dynamicsymbols('q1 q2 q3 q4', 1)
|
| 105 |
+
N = ReferenceFrame('N')
|
| 106 |
+
A = N.orientnew('A', 'Axis', [q1, N.z])
|
| 107 |
+
B = A.orientnew('B', 'Axis', [q2, A.x])
|
| 108 |
+
C = B.orientnew('C', 'Axis', [q3, B.y])
|
| 109 |
+
D = N.orientnew('D', 'Axis', [q4, N.y])
|
| 110 |
+
u1, u2, u3 = dynamicsymbols('u1 u2 u3')
|
| 111 |
+
assert A.ang_vel_in(N) == (q1d)*A.z
|
| 112 |
+
assert B.ang_vel_in(N) == (q2d)*B.x + (q1d)*A.z
|
| 113 |
+
assert C.ang_vel_in(N) == (q3d)*C.y + (q2d)*B.x + (q1d)*A.z
|
| 114 |
+
|
| 115 |
+
A2 = N.orientnew('A2', 'Axis', [q4, N.y])
|
| 116 |
+
assert N.ang_vel_in(N) == 0
|
| 117 |
+
assert N.ang_vel_in(A) == -q1d*N.z
|
| 118 |
+
assert N.ang_vel_in(B) == -q1d*A.z - q2d*B.x
|
| 119 |
+
assert N.ang_vel_in(C) == -q1d*A.z - q2d*B.x - q3d*B.y
|
| 120 |
+
assert N.ang_vel_in(A2) == -q4d*N.y
|
| 121 |
+
|
| 122 |
+
assert A.ang_vel_in(N) == q1d*N.z
|
| 123 |
+
assert A.ang_vel_in(A) == 0
|
| 124 |
+
assert A.ang_vel_in(B) == - q2d*B.x
|
| 125 |
+
assert A.ang_vel_in(C) == - q2d*B.x - q3d*B.y
|
| 126 |
+
assert A.ang_vel_in(A2) == q1d*N.z - q4d*N.y
|
| 127 |
+
|
| 128 |
+
assert B.ang_vel_in(N) == q1d*A.z + q2d*A.x
|
| 129 |
+
assert B.ang_vel_in(A) == q2d*A.x
|
| 130 |
+
assert B.ang_vel_in(B) == 0
|
| 131 |
+
assert B.ang_vel_in(C) == -q3d*B.y
|
| 132 |
+
assert B.ang_vel_in(A2) == q1d*A.z + q2d*A.x - q4d*N.y
|
| 133 |
+
|
| 134 |
+
assert C.ang_vel_in(N) == q1d*A.z + q2d*A.x + q3d*B.y
|
| 135 |
+
assert C.ang_vel_in(A) == q2d*A.x + q3d*C.y
|
| 136 |
+
assert C.ang_vel_in(B) == q3d*B.y
|
| 137 |
+
assert C.ang_vel_in(C) == 0
|
| 138 |
+
assert C.ang_vel_in(A2) == q1d*A.z + q2d*A.x + q3d*B.y - q4d*N.y
|
| 139 |
+
|
| 140 |
+
assert A2.ang_vel_in(N) == q4d*A2.y
|
| 141 |
+
assert A2.ang_vel_in(A) == q4d*A2.y - q1d*N.z
|
| 142 |
+
assert A2.ang_vel_in(B) == q4d*N.y - q1d*A.z - q2d*A.x
|
| 143 |
+
assert A2.ang_vel_in(C) == q4d*N.y - q1d*A.z - q2d*A.x - q3d*B.y
|
| 144 |
+
assert A2.ang_vel_in(A2) == 0
|
| 145 |
+
|
| 146 |
+
C.set_ang_vel(N, u1*C.x + u2*C.y + u3*C.z)
|
| 147 |
+
assert C.ang_vel_in(N) == (u1)*C.x + (u2)*C.y + (u3)*C.z
|
| 148 |
+
assert N.ang_vel_in(C) == (-u1)*C.x + (-u2)*C.y + (-u3)*C.z
|
| 149 |
+
assert C.ang_vel_in(D) == (u1)*C.x + (u2)*C.y + (u3)*C.z + (-q4d)*D.y
|
| 150 |
+
assert D.ang_vel_in(C) == (-u1)*C.x + (-u2)*C.y + (-u3)*C.z + (q4d)*D.y
|
| 151 |
+
|
| 152 |
+
q0 = dynamicsymbols('q0')
|
| 153 |
+
q0d = dynamicsymbols('q0', 1)
|
| 154 |
+
E = N.orientnew('E', 'Quaternion', (q0, q1, q2, q3))
|
| 155 |
+
assert E.ang_vel_in(N) == (
|
| 156 |
+
2 * (q1d * q0 + q2d * q3 - q3d * q2 - q0d * q1) * E.x +
|
| 157 |
+
2 * (q2d * q0 + q3d * q1 - q1d * q3 - q0d * q2) * E.y +
|
| 158 |
+
2 * (q3d * q0 + q1d * q2 - q2d * q1 - q0d * q3) * E.z)
|
| 159 |
+
|
| 160 |
+
F = N.orientnew('F', 'Body', (q1, q2, q3), 313)
|
| 161 |
+
assert F.ang_vel_in(N) == ((sin(q2)*sin(q3)*q1d + cos(q3)*q2d)*F.x +
|
| 162 |
+
(sin(q2)*cos(q3)*q1d - sin(q3)*q2d)*F.y + (cos(q2)*q1d + q3d)*F.z)
|
| 163 |
+
G = N.orientnew('G', 'Axis', (q1, N.x + N.y))
|
| 164 |
+
assert G.ang_vel_in(N) == q1d * (N.x + N.y).normalize()
|
| 165 |
+
assert N.ang_vel_in(G) == -q1d * (N.x + N.y).normalize()
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def test_dcm():
|
| 169 |
+
q1, q2, q3, q4 = dynamicsymbols('q1 q2 q3 q4')
|
| 170 |
+
N = ReferenceFrame('N')
|
| 171 |
+
A = N.orientnew('A', 'Axis', [q1, N.z])
|
| 172 |
+
B = A.orientnew('B', 'Axis', [q2, A.x])
|
| 173 |
+
C = B.orientnew('C', 'Axis', [q3, B.y])
|
| 174 |
+
D = N.orientnew('D', 'Axis', [q4, N.y])
|
| 175 |
+
E = N.orientnew('E', 'Space', [q1, q2, q3], '123')
|
| 176 |
+
assert N.dcm(C) == Matrix([
|
| 177 |
+
[- sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), - sin(q1) *
|
| 178 |
+
cos(q2), sin(q1) * sin(q2) * cos(q3) + sin(q3) * cos(q1)], [sin(q1) *
|
| 179 |
+
cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) * cos(q2), sin(q1) *
|
| 180 |
+
sin(q3) - sin(q2) * cos(q1) * cos(q3)], [- sin(q3) * cos(q2), sin(q2),
|
| 181 |
+
cos(q2) * cos(q3)]])
|
| 182 |
+
# This is a little touchy. Is it ok to use simplify in assert?
|
| 183 |
+
test_mat = D.dcm(C) - Matrix(
|
| 184 |
+
[[cos(q1) * cos(q3) * cos(q4) - sin(q3) * (- sin(q4) * cos(q2) +
|
| 185 |
+
sin(q1) * sin(q2) * cos(q4)), - sin(q2) * sin(q4) - sin(q1) *
|
| 186 |
+
cos(q2) * cos(q4), sin(q3) * cos(q1) * cos(q4) + cos(q3) * (- sin(q4) *
|
| 187 |
+
cos(q2) + sin(q1) * sin(q2) * cos(q4))], [sin(q1) * cos(q3) +
|
| 188 |
+
sin(q2) * sin(q3) * cos(q1), cos(q1) * cos(q2), sin(q1) * sin(q3) -
|
| 189 |
+
sin(q2) * cos(q1) * cos(q3)], [sin(q4) * cos(q1) * cos(q3) -
|
| 190 |
+
sin(q3) * (cos(q2) * cos(q4) + sin(q1) * sin(q2) * sin(q4)), sin(q2) *
|
| 191 |
+
cos(q4) - sin(q1) * sin(q4) * cos(q2), sin(q3) * sin(q4) * cos(q1) +
|
| 192 |
+
cos(q3) * (cos(q2) * cos(q4) + sin(q1) * sin(q2) * sin(q4))]])
|
| 193 |
+
assert test_mat.expand() == zeros(3, 3)
|
| 194 |
+
assert E.dcm(N) == Matrix(
|
| 195 |
+
[[cos(q2)*cos(q3), sin(q3)*cos(q2), -sin(q2)],
|
| 196 |
+
[sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1), sin(q1)*sin(q2)*sin(q3) +
|
| 197 |
+
cos(q1)*cos(q3), sin(q1)*cos(q2)], [sin(q1)*sin(q3) +
|
| 198 |
+
sin(q2)*cos(q1)*cos(q3), - sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1),
|
| 199 |
+
cos(q1)*cos(q2)]])
|
| 200 |
+
|
| 201 |
+
def test_w_diff_dcm1():
|
| 202 |
+
# Ref:
|
| 203 |
+
# Dynamics Theory and Applications, Kane 1985
|
| 204 |
+
# Sec. 2.1 ANGULAR VELOCITY
|
| 205 |
+
A = ReferenceFrame('A')
|
| 206 |
+
B = ReferenceFrame('B')
|
| 207 |
+
|
| 208 |
+
c11, c12, c13 = dynamicsymbols('C11 C12 C13')
|
| 209 |
+
c21, c22, c23 = dynamicsymbols('C21 C22 C23')
|
| 210 |
+
c31, c32, c33 = dynamicsymbols('C31 C32 C33')
|
| 211 |
+
|
| 212 |
+
c11d, c12d, c13d = dynamicsymbols('C11 C12 C13', level=1)
|
| 213 |
+
c21d, c22d, c23d = dynamicsymbols('C21 C22 C23', level=1)
|
| 214 |
+
c31d, c32d, c33d = dynamicsymbols('C31 C32 C33', level=1)
|
| 215 |
+
|
| 216 |
+
DCM = Matrix([
|
| 217 |
+
[c11, c12, c13],
|
| 218 |
+
[c21, c22, c23],
|
| 219 |
+
[c31, c32, c33]
|
| 220 |
+
])
|
| 221 |
+
|
| 222 |
+
B.orient(A, 'DCM', DCM)
|
| 223 |
+
b1a = (B.x).express(A)
|
| 224 |
+
b2a = (B.y).express(A)
|
| 225 |
+
b3a = (B.z).express(A)
|
| 226 |
+
|
| 227 |
+
# Equation (2.1.1)
|
| 228 |
+
B.set_ang_vel(A, B.x*(dot((b3a).dt(A), B.y))
|
| 229 |
+
+ B.y*(dot((b1a).dt(A), B.z))
|
| 230 |
+
+ B.z*(dot((b2a).dt(A), B.x)))
|
| 231 |
+
|
| 232 |
+
# Equation (2.1.21)
|
| 233 |
+
expr = ( (c12*c13d + c22*c23d + c32*c33d)*B.x
|
| 234 |
+
+ (c13*c11d + c23*c21d + c33*c31d)*B.y
|
| 235 |
+
+ (c11*c12d + c21*c22d + c31*c32d)*B.z)
|
| 236 |
+
assert B.ang_vel_in(A) - expr == 0
|
| 237 |
+
|
| 238 |
+
def test_w_diff_dcm2():
|
| 239 |
+
q1, q2, q3 = dynamicsymbols('q1:4')
|
| 240 |
+
N = ReferenceFrame('N')
|
| 241 |
+
A = N.orientnew('A', 'axis', [q1, N.x])
|
| 242 |
+
B = A.orientnew('B', 'axis', [q2, A.y])
|
| 243 |
+
C = B.orientnew('C', 'axis', [q3, B.z])
|
| 244 |
+
|
| 245 |
+
DCM = C.dcm(N).T
|
| 246 |
+
D = N.orientnew('D', 'DCM', DCM)
|
| 247 |
+
|
| 248 |
+
# Frames D and C are the same ReferenceFrame,
|
| 249 |
+
# since they have equal DCM respect to frame N.
|
| 250 |
+
# Therefore, D and C should have same angle velocity in N.
|
| 251 |
+
assert D.dcm(N) == C.dcm(N) == Matrix([
|
| 252 |
+
[cos(q2)*cos(q3), sin(q1)*sin(q2)*cos(q3) +
|
| 253 |
+
sin(q3)*cos(q1), sin(q1)*sin(q3) -
|
| 254 |
+
sin(q2)*cos(q1)*cos(q3)], [-sin(q3)*cos(q2),
|
| 255 |
+
-sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3),
|
| 256 |
+
sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1)],
|
| 257 |
+
[sin(q2), -sin(q1)*cos(q2), cos(q1)*cos(q2)]])
|
| 258 |
+
assert (D.ang_vel_in(N) - C.ang_vel_in(N)).express(N).simplify() == 0
|
| 259 |
+
|
| 260 |
+
def test_orientnew_respects_parent_class():
|
| 261 |
+
class MyReferenceFrame(ReferenceFrame):
|
| 262 |
+
pass
|
| 263 |
+
B = MyReferenceFrame('B')
|
| 264 |
+
C = B.orientnew('C', 'Axis', [0, B.x])
|
| 265 |
+
assert isinstance(C, MyReferenceFrame)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def test_orientnew_respects_input_indices():
|
| 269 |
+
N = ReferenceFrame('N')
|
| 270 |
+
q1 = dynamicsymbols('q1')
|
| 271 |
+
A = N.orientnew('a', 'Axis', [q1, N.z])
|
| 272 |
+
#modify default indices:
|
| 273 |
+
minds = [x+'1' for x in N.indices]
|
| 274 |
+
B = N.orientnew('b', 'Axis', [q1, N.z], indices=minds)
|
| 275 |
+
|
| 276 |
+
assert N.indices == A.indices
|
| 277 |
+
assert B.indices == minds
|
| 278 |
+
|
| 279 |
+
def test_orientnew_respects_input_latexs():
|
| 280 |
+
N = ReferenceFrame('N')
|
| 281 |
+
q1 = dynamicsymbols('q1')
|
| 282 |
+
A = N.orientnew('a', 'Axis', [q1, N.z])
|
| 283 |
+
|
| 284 |
+
#build default and alternate latex_vecs:
|
| 285 |
+
def_latex_vecs = [(r"\mathbf{\hat{%s}_%s}" % (A.name.lower(),
|
| 286 |
+
A.indices[0])), (r"\mathbf{\hat{%s}_%s}" %
|
| 287 |
+
(A.name.lower(), A.indices[1])),
|
| 288 |
+
(r"\mathbf{\hat{%s}_%s}" % (A.name.lower(),
|
| 289 |
+
A.indices[2]))]
|
| 290 |
+
|
| 291 |
+
name = 'b'
|
| 292 |
+
indices = [x+'1' for x in N.indices]
|
| 293 |
+
new_latex_vecs = [(r"\mathbf{\hat{%s}_{%s}}" % (name.lower(),
|
| 294 |
+
indices[0])), (r"\mathbf{\hat{%s}_{%s}}" %
|
| 295 |
+
(name.lower(), indices[1])),
|
| 296 |
+
(r"\mathbf{\hat{%s}_{%s}}" % (name.lower(),
|
| 297 |
+
indices[2]))]
|
| 298 |
+
|
| 299 |
+
B = N.orientnew(name, 'Axis', [q1, N.z], latexs=new_latex_vecs)
|
| 300 |
+
|
| 301 |
+
assert A.latex_vecs == def_latex_vecs
|
| 302 |
+
assert B.latex_vecs == new_latex_vecs
|
| 303 |
+
assert B.indices != indices
|
| 304 |
+
|
| 305 |
+
def test_orientnew_respects_input_variables():
|
| 306 |
+
N = ReferenceFrame('N')
|
| 307 |
+
q1 = dynamicsymbols('q1')
|
| 308 |
+
A = N.orientnew('a', 'Axis', [q1, N.z])
|
| 309 |
+
|
| 310 |
+
#build non-standard variable names
|
| 311 |
+
name = 'b'
|
| 312 |
+
new_variables = ['notb_'+x+'1' for x in N.indices]
|
| 313 |
+
B = N.orientnew(name, 'Axis', [q1, N.z], variables=new_variables)
|
| 314 |
+
|
| 315 |
+
for j,var in enumerate(A.varlist):
|
| 316 |
+
assert var.name == A.name + '_' + A.indices[j]
|
| 317 |
+
|
| 318 |
+
for j,var in enumerate(B.varlist):
|
| 319 |
+
assert var.name == new_variables[j]
|
| 320 |
+
|
| 321 |
+
def test_issue_10348():
|
| 322 |
+
u = dynamicsymbols('u:3')
|
| 323 |
+
I = ReferenceFrame('I')
|
| 324 |
+
I.orientnew('A', 'space', u, 'XYZ')
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def test_issue_11503():
|
| 328 |
+
A = ReferenceFrame("A")
|
| 329 |
+
A.orientnew("B", "Axis", [35, A.y])
|
| 330 |
+
C = ReferenceFrame("C")
|
| 331 |
+
A.orient(C, "Axis", [70, C.z])
|
| 332 |
+
|
| 333 |
+
|
| 334 |
+
def test_partial_velocity():
|
| 335 |
+
|
| 336 |
+
N = ReferenceFrame('N')
|
| 337 |
+
A = ReferenceFrame('A')
|
| 338 |
+
|
| 339 |
+
u1, u2 = dynamicsymbols('u1, u2')
|
| 340 |
+
|
| 341 |
+
A.set_ang_vel(N, u1 * A.x + u2 * N.y)
|
| 342 |
+
|
| 343 |
+
assert N.partial_velocity(A, u1) == -A.x
|
| 344 |
+
assert N.partial_velocity(A, u1, u2) == (-A.x, -N.y)
|
| 345 |
+
|
| 346 |
+
assert A.partial_velocity(N, u1) == A.x
|
| 347 |
+
assert A.partial_velocity(N, u1, u2) == (A.x, N.y)
|
| 348 |
+
|
| 349 |
+
assert N.partial_velocity(N, u1) == 0
|
| 350 |
+
assert A.partial_velocity(A, u1) == 0
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
def test_issue_11498():
|
| 354 |
+
A = ReferenceFrame('A')
|
| 355 |
+
B = ReferenceFrame('B')
|
| 356 |
+
|
| 357 |
+
# Identity transformation
|
| 358 |
+
A.orient(B, 'DCM', eye(3))
|
| 359 |
+
assert A.dcm(B) == Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
|
| 360 |
+
assert B.dcm(A) == Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
|
| 361 |
+
|
| 362 |
+
# x -> y
|
| 363 |
+
# y -> -z
|
| 364 |
+
# z -> -x
|
| 365 |
+
A.orient(B, 'DCM', Matrix([[0, 1, 0], [0, 0, -1], [-1, 0, 0]]))
|
| 366 |
+
assert B.dcm(A) == Matrix([[0, 1, 0], [0, 0, -1], [-1, 0, 0]])
|
| 367 |
+
assert A.dcm(B) == Matrix([[0, 0, -1], [1, 0, 0], [0, -1, 0]])
|
| 368 |
+
assert B.dcm(A).T == A.dcm(B)
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
def test_reference_frame():
|
| 372 |
+
raises(TypeError, lambda: ReferenceFrame(0))
|
| 373 |
+
raises(TypeError, lambda: ReferenceFrame('N', 0))
|
| 374 |
+
raises(ValueError, lambda: ReferenceFrame('N', [0, 1]))
|
| 375 |
+
raises(TypeError, lambda: ReferenceFrame('N', [0, 1, 2]))
|
| 376 |
+
raises(TypeError, lambda: ReferenceFrame('N', ['a', 'b', 'c'], 0))
|
| 377 |
+
raises(ValueError, lambda: ReferenceFrame('N', ['a', 'b', 'c'], [0, 1]))
|
| 378 |
+
raises(TypeError, lambda: ReferenceFrame('N', ['a', 'b', 'c'], [0, 1, 2]))
|
| 379 |
+
raises(TypeError, lambda: ReferenceFrame('N', ['a', 'b', 'c'],
|
| 380 |
+
['a', 'b', 'c'], 0))
|
| 381 |
+
raises(ValueError, lambda: ReferenceFrame('N', ['a', 'b', 'c'],
|
| 382 |
+
['a', 'b', 'c'], [0, 1]))
|
| 383 |
+
raises(TypeError, lambda: ReferenceFrame('N', ['a', 'b', 'c'],
|
| 384 |
+
['a', 'b', 'c'], [0, 1, 2]))
|
| 385 |
+
N = ReferenceFrame('N')
|
| 386 |
+
assert N[0] == CoordinateSym('N_x', N, 0)
|
| 387 |
+
assert N[1] == CoordinateSym('N_y', N, 1)
|
| 388 |
+
assert N[2] == CoordinateSym('N_z', N, 2)
|
| 389 |
+
raises(ValueError, lambda: N[3])
|
| 390 |
+
N = ReferenceFrame('N', ['a', 'b', 'c'])
|
| 391 |
+
assert N['a'] == N.x
|
| 392 |
+
assert N['b'] == N.y
|
| 393 |
+
assert N['c'] == N.z
|
| 394 |
+
raises(ValueError, lambda: N['d'])
|
| 395 |
+
assert str(N) == 'N'
|
| 396 |
+
|
| 397 |
+
A = ReferenceFrame('A')
|
| 398 |
+
B = ReferenceFrame('B')
|
| 399 |
+
q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
|
| 400 |
+
raises(TypeError, lambda: A.orient(B, 'DCM', 0))
|
| 401 |
+
raises(TypeError, lambda: B.orient(N, 'Space', [q1, q2, q3], '222'))
|
| 402 |
+
raises(TypeError, lambda: B.orient(N, 'Axis', [q1, N.x + 2 * N.y], '222'))
|
| 403 |
+
raises(TypeError, lambda: B.orient(N, 'Axis', q1))
|
| 404 |
+
raises(IndexError, lambda: B.orient(N, 'Axis', [q1]))
|
| 405 |
+
raises(TypeError, lambda: B.orient(N, 'Quaternion', [q0, q1, q2, q3], '222'))
|
| 406 |
+
raises(TypeError, lambda: B.orient(N, 'Quaternion', q0))
|
| 407 |
+
raises(TypeError, lambda: B.orient(N, 'Quaternion', [q0, q1, q2]))
|
| 408 |
+
raises(NotImplementedError, lambda: B.orient(N, 'Foo', [q0, q1, q2]))
|
| 409 |
+
raises(TypeError, lambda: B.orient(N, 'Body', [q1, q2], '232'))
|
| 410 |
+
raises(TypeError, lambda: B.orient(N, 'Space', [q1, q2], '232'))
|
| 411 |
+
|
| 412 |
+
N.set_ang_acc(B, 0)
|
| 413 |
+
assert N.ang_acc_in(B) == Vector(0)
|
| 414 |
+
N.set_ang_vel(B, 0)
|
| 415 |
+
assert N.ang_vel_in(B) == Vector(0)
|
| 416 |
+
|
| 417 |
+
|
| 418 |
+
def test_check_frame():
|
| 419 |
+
raises(VectorTypeError, lambda: _check_frame(0))
|
| 420 |
+
|
| 421 |
+
|
| 422 |
+
def test_dcm_diff_16824():
|
| 423 |
+
# NOTE : This is a regression test for the bug introduced in PR 14758,
|
| 424 |
+
# identified in 16824, and solved by PR 16828.
|
| 425 |
+
|
| 426 |
+
# This is the solution to Problem 2.2 on page 264 in Kane & Lenvinson's
|
| 427 |
+
# 1985 book.
|
| 428 |
+
|
| 429 |
+
q1, q2, q3 = dynamicsymbols('q1:4')
|
| 430 |
+
|
| 431 |
+
s1 = sin(q1)
|
| 432 |
+
c1 = cos(q1)
|
| 433 |
+
s2 = sin(q2)
|
| 434 |
+
c2 = cos(q2)
|
| 435 |
+
s3 = sin(q3)
|
| 436 |
+
c3 = cos(q3)
|
| 437 |
+
|
| 438 |
+
dcm = Matrix([[c2*c3, s1*s2*c3 - s3*c1, c1*s2*c3 + s3*s1],
|
| 439 |
+
[c2*s3, s1*s2*s3 + c3*c1, c1*s2*s3 - c3*s1],
|
| 440 |
+
[-s2, s1*c2, c1*c2]])
|
| 441 |
+
|
| 442 |
+
A = ReferenceFrame('A')
|
| 443 |
+
B = ReferenceFrame('B')
|
| 444 |
+
B.orient(A, 'DCM', dcm)
|
| 445 |
+
|
| 446 |
+
AwB = B.ang_vel_in(A)
|
| 447 |
+
|
| 448 |
+
alpha2 = s3*c2*q1.diff() + c3*q2.diff()
|
| 449 |
+
beta2 = s1*c2*q3.diff() + c1*q2.diff()
|
| 450 |
+
|
| 451 |
+
assert simplify(AwB.dot(A.y) - alpha2) == 0
|
| 452 |
+
assert simplify(AwB.dot(B.y) - beta2) == 0
|
| 453 |
+
|
| 454 |
+
def test_orient_explicit():
|
| 455 |
+
cxx, cyy, czz = dynamicsymbols('c_{xx}, c_{yy}, c_{zz}')
|
| 456 |
+
cxy, cxz, cyx = dynamicsymbols('c_{xy}, c_{xz}, c_{yx}')
|
| 457 |
+
cyz, czx, czy = dynamicsymbols('c_{yz}, c_{zx}, c_{zy}')
|
| 458 |
+
dcxx, dcyy, dczz = dynamicsymbols('c_{xx}, c_{yy}, c_{zz}', 1)
|
| 459 |
+
dcxy, dcxz, dcyx = dynamicsymbols('c_{xy}, c_{xz}, c_{yx}', 1)
|
| 460 |
+
dcyz, dczx, dczy = dynamicsymbols('c_{yz}, c_{zx}, c_{zy}', 1)
|
| 461 |
+
A = ReferenceFrame('A')
|
| 462 |
+
B = ReferenceFrame('B')
|
| 463 |
+
B_C_A = Matrix([[cxx, cxy, cxz],
|
| 464 |
+
[cyx, cyy, cyz],
|
| 465 |
+
[czx, czy, czz]])
|
| 466 |
+
B_w_A = ((cyx*dczx + cyy*dczy + cyz*dczz)*B.x +
|
| 467 |
+
(czx*dcxx + czy*dcxy + czz*dcxz)*B.y +
|
| 468 |
+
(cxx*dcyx + cxy*dcyy + cxz*dcyz)*B.z)
|
| 469 |
+
A.orient_explicit(B, B_C_A)
|
| 470 |
+
assert B.dcm(A) == B_C_A
|
| 471 |
+
assert A.ang_vel_in(B) == B_w_A
|
| 472 |
+
assert B.ang_vel_in(A) == -B_w_A
|
| 473 |
+
|
| 474 |
+
def test_orient_dcm():
|
| 475 |
+
cxx, cyy, czz = dynamicsymbols('c_{xx}, c_{yy}, c_{zz}')
|
| 476 |
+
cxy, cxz, cyx = dynamicsymbols('c_{xy}, c_{xz}, c_{yx}')
|
| 477 |
+
cyz, czx, czy = dynamicsymbols('c_{yz}, c_{zx}, c_{zy}')
|
| 478 |
+
B_C_A = Matrix([[cxx, cxy, cxz],
|
| 479 |
+
[cyx, cyy, cyz],
|
| 480 |
+
[czx, czy, czz]])
|
| 481 |
+
A = ReferenceFrame('A')
|
| 482 |
+
B = ReferenceFrame('B')
|
| 483 |
+
B.orient_dcm(A, B_C_A)
|
| 484 |
+
assert B.dcm(A) == Matrix([[cxx, cxy, cxz],
|
| 485 |
+
[cyx, cyy, cyz],
|
| 486 |
+
[czx, czy, czz]])
|
| 487 |
+
|
| 488 |
+
def test_orient_axis():
|
| 489 |
+
A = ReferenceFrame('A')
|
| 490 |
+
B = ReferenceFrame('B')
|
| 491 |
+
A.orient_axis(B,-B.x, 1)
|
| 492 |
+
A1 = A.dcm(B)
|
| 493 |
+
A.orient_axis(B, B.x, -1)
|
| 494 |
+
A2 = A.dcm(B)
|
| 495 |
+
A.orient_axis(B, 1, -B.x)
|
| 496 |
+
A3 = A.dcm(B)
|
| 497 |
+
assert A1 == A2
|
| 498 |
+
assert A2 == A3
|
| 499 |
+
raises(TypeError, lambda: A.orient_axis(B, 1, 1))
|
| 500 |
+
|
| 501 |
+
def test_orient_body():
|
| 502 |
+
A = ReferenceFrame('A')
|
| 503 |
+
B = ReferenceFrame('B')
|
| 504 |
+
B.orient_body_fixed(A, (1,1,0), 'XYX')
|
| 505 |
+
assert B.dcm(A) == Matrix([[cos(1), sin(1)**2, -sin(1)*cos(1)], [0, cos(1), sin(1)], [sin(1), -sin(1)*cos(1), cos(1)**2]])
|
| 506 |
+
|
| 507 |
+
|
| 508 |
+
def test_orient_body_advanced():
|
| 509 |
+
q1, q2, q3 = dynamicsymbols('q1:4')
|
| 510 |
+
c1, c2, c3 = symbols('c1:4')
|
| 511 |
+
u1, u2, u3 = dynamicsymbols('q1:4', 1)
|
| 512 |
+
|
| 513 |
+
# Test with everything as dynamicsymbols
|
| 514 |
+
A, B = ReferenceFrame('A'), ReferenceFrame('B')
|
| 515 |
+
B.orient_body_fixed(A, (q1, q2, q3), 'zxy')
|
| 516 |
+
assert A.dcm(B) == Matrix([
|
| 517 |
+
[-sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), -sin(q1) * cos(q2),
|
| 518 |
+
sin(q1) * sin(q2) * cos(q3) + sin(q3) * cos(q1)],
|
| 519 |
+
[sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) * cos(q2),
|
| 520 |
+
sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3)],
|
| 521 |
+
[-sin(q3) * cos(q2), sin(q2), cos(q2) * cos(q3)]])
|
| 522 |
+
assert B.ang_vel_in(A).to_matrix(B) == Matrix([
|
| 523 |
+
[-sin(q3) * cos(q2) * u1 + cos(q3) * u2],
|
| 524 |
+
[sin(q2) * u1 + u3],
|
| 525 |
+
[sin(q3) * u2 + cos(q2) * cos(q3) * u1]])
|
| 526 |
+
|
| 527 |
+
# Test with constant symbol
|
| 528 |
+
A, B = ReferenceFrame('A'), ReferenceFrame('B')
|
| 529 |
+
B.orient_body_fixed(A, (q1, c2, q3), 131)
|
| 530 |
+
assert A.dcm(B) == Matrix([
|
| 531 |
+
[cos(c2), -sin(c2) * cos(q3), sin(c2) * sin(q3)],
|
| 532 |
+
[sin(c2) * cos(q1), -sin(q1) * sin(q3) + cos(c2) * cos(q1) * cos(q3),
|
| 533 |
+
-sin(q1) * cos(q3) - sin(q3) * cos(c2) * cos(q1)],
|
| 534 |
+
[sin(c2) * sin(q1), sin(q1) * cos(c2) * cos(q3) + sin(q3) * cos(q1),
|
| 535 |
+
-sin(q1) * sin(q3) * cos(c2) + cos(q1) * cos(q3)]])
|
| 536 |
+
assert B.ang_vel_in(A).to_matrix(B) == Matrix([
|
| 537 |
+
[cos(c2) * u1 + u3],
|
| 538 |
+
[-sin(c2) * cos(q3) * u1],
|
| 539 |
+
[sin(c2) * sin(q3) * u1]])
|
| 540 |
+
|
| 541 |
+
# Test all symbols not time dependent
|
| 542 |
+
A, B = ReferenceFrame('A'), ReferenceFrame('B')
|
| 543 |
+
B.orient_body_fixed(A, (c1, c2, c3), 123)
|
| 544 |
+
assert B.ang_vel_in(A) == Vector(0)
|
| 545 |
+
|
| 546 |
+
|
| 547 |
+
def test_orient_space_advanced():
|
| 548 |
+
# space fixed is in the end like body fixed only in opposite order
|
| 549 |
+
q1, q2, q3 = dynamicsymbols('q1:4')
|
| 550 |
+
c1, c2, c3 = symbols('c1:4')
|
| 551 |
+
u1, u2, u3 = dynamicsymbols('q1:4', 1)
|
| 552 |
+
|
| 553 |
+
# Test with everything as dynamicsymbols
|
| 554 |
+
A, B = ReferenceFrame('A'), ReferenceFrame('B')
|
| 555 |
+
B.orient_space_fixed(A, (q3, q2, q1), 'yxz')
|
| 556 |
+
assert A.dcm(B) == Matrix([
|
| 557 |
+
[-sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), -sin(q1) * cos(q2),
|
| 558 |
+
sin(q1) * sin(q2) * cos(q3) + sin(q3) * cos(q1)],
|
| 559 |
+
[sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) * cos(q2),
|
| 560 |
+
sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3)],
|
| 561 |
+
[-sin(q3) * cos(q2), sin(q2), cos(q2) * cos(q3)]])
|
| 562 |
+
assert B.ang_vel_in(A).to_matrix(B) == Matrix([
|
| 563 |
+
[-sin(q3) * cos(q2) * u1 + cos(q3) * u2],
|
| 564 |
+
[sin(q2) * u1 + u3],
|
| 565 |
+
[sin(q3) * u2 + cos(q2) * cos(q3) * u1]])
|
| 566 |
+
|
| 567 |
+
# Test with constant symbol
|
| 568 |
+
A, B = ReferenceFrame('A'), ReferenceFrame('B')
|
| 569 |
+
B.orient_space_fixed(A, (q3, c2, q1), 131)
|
| 570 |
+
assert A.dcm(B) == Matrix([
|
| 571 |
+
[cos(c2), -sin(c2) * cos(q3), sin(c2) * sin(q3)],
|
| 572 |
+
[sin(c2) * cos(q1), -sin(q1) * sin(q3) + cos(c2) * cos(q1) * cos(q3),
|
| 573 |
+
-sin(q1) * cos(q3) - sin(q3) * cos(c2) * cos(q1)],
|
| 574 |
+
[sin(c2) * sin(q1), sin(q1) * cos(c2) * cos(q3) + sin(q3) * cos(q1),
|
| 575 |
+
-sin(q1) * sin(q3) * cos(c2) + cos(q1) * cos(q3)]])
|
| 576 |
+
assert B.ang_vel_in(A).to_matrix(B) == Matrix([
|
| 577 |
+
[cos(c2) * u1 + u3],
|
| 578 |
+
[-sin(c2) * cos(q3) * u1],
|
| 579 |
+
[sin(c2) * sin(q3) * u1]])
|
| 580 |
+
|
| 581 |
+
# Test all symbols not time dependent
|
| 582 |
+
A, B = ReferenceFrame('A'), ReferenceFrame('B')
|
| 583 |
+
B.orient_space_fixed(A, (c1, c2, c3), 123)
|
| 584 |
+
assert B.ang_vel_in(A) == Vector(0)
|
| 585 |
+
|
| 586 |
+
|
| 587 |
+
def test_orient_body_simple_ang_vel():
|
| 588 |
+
"""This test ensures that the simplest form of that linear system solution
|
| 589 |
+
is returned, thus the == for the expression comparison."""
|
| 590 |
+
|
| 591 |
+
psi, theta, phi = dynamicsymbols('psi, theta, varphi')
|
| 592 |
+
t = dynamicsymbols._t
|
| 593 |
+
A = ReferenceFrame('A')
|
| 594 |
+
B = ReferenceFrame('B')
|
| 595 |
+
B.orient_body_fixed(A, (psi, theta, phi), 'ZXZ')
|
| 596 |
+
A_w_B = B.ang_vel_in(A)
|
| 597 |
+
assert A_w_B.args[0][1] == B
|
| 598 |
+
assert A_w_B.args[0][0][0] == (sin(theta)*sin(phi)*psi.diff(t) +
|
| 599 |
+
cos(phi)*theta.diff(t))
|
| 600 |
+
assert A_w_B.args[0][0][1] == (sin(theta)*cos(phi)*psi.diff(t) -
|
| 601 |
+
sin(phi)*theta.diff(t))
|
| 602 |
+
assert A_w_B.args[0][0][2] == cos(theta)*psi.diff(t) + phi.diff(t)
|
| 603 |
+
|
| 604 |
+
|
| 605 |
+
def test_orient_space():
|
| 606 |
+
A = ReferenceFrame('A')
|
| 607 |
+
B = ReferenceFrame('B')
|
| 608 |
+
B.orient_space_fixed(A, (0,0,0), '123')
|
| 609 |
+
assert B.dcm(A) == Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
|
| 610 |
+
|
| 611 |
+
def test_orient_quaternion():
|
| 612 |
+
A = ReferenceFrame('A')
|
| 613 |
+
B = ReferenceFrame('B')
|
| 614 |
+
B.orient_quaternion(A, (0,0,0,0))
|
| 615 |
+
assert B.dcm(A) == Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
|
| 616 |
+
|
| 617 |
+
def test_looped_frame_warning():
|
| 618 |
+
A = ReferenceFrame('A')
|
| 619 |
+
B = ReferenceFrame('B')
|
| 620 |
+
C = ReferenceFrame('C')
|
| 621 |
+
|
| 622 |
+
a, b, c = symbols('a b c')
|
| 623 |
+
B.orient_axis(A, A.x, a)
|
| 624 |
+
C.orient_axis(B, B.x, b)
|
| 625 |
+
|
| 626 |
+
with warnings.catch_warnings(record = True) as w:
|
| 627 |
+
warnings.simplefilter("always")
|
| 628 |
+
A.orient_axis(C, C.x, c)
|
| 629 |
+
assert issubclass(w[-1].category, UserWarning)
|
| 630 |
+
assert 'Loops are defined among the orientation of frames. ' + \
|
| 631 |
+
'This is likely not desired and may cause errors in your calculations.' in str(w[-1].message)
|
| 632 |
+
|
| 633 |
+
def test_frame_dict():
|
| 634 |
+
A = ReferenceFrame('A')
|
| 635 |
+
B = ReferenceFrame('B')
|
| 636 |
+
C = ReferenceFrame('C')
|
| 637 |
+
|
| 638 |
+
a, b, c = symbols('a b c')
|
| 639 |
+
|
| 640 |
+
B.orient_axis(A, A.x, a)
|
| 641 |
+
assert A._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(a), -sin(a)],[0, sin(a), cos(a)]])}
|
| 642 |
+
assert B._dcm_dict == {A: Matrix([[1, 0, 0],[0, cos(a), sin(a)],[0, -sin(a), cos(a)]])}
|
| 643 |
+
assert C._dcm_dict == {}
|
| 644 |
+
|
| 645 |
+
B.orient_axis(C, C.x, b)
|
| 646 |
+
# Previous relation is not wiped
|
| 647 |
+
assert A._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(a), -sin(a)],[0, sin(a), cos(a)]])}
|
| 648 |
+
assert B._dcm_dict == {A: Matrix([[1, 0, 0],[0, cos(a), sin(a)],[0, -sin(a), cos(a)]]), \
|
| 649 |
+
C: Matrix([[1, 0, 0],[0, cos(b), sin(b)],[0, -sin(b), cos(b)]])}
|
| 650 |
+
assert C._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(b), -sin(b)],[0, sin(b), cos(b)]])}
|
| 651 |
+
|
| 652 |
+
A.orient_axis(B, B.x, c)
|
| 653 |
+
# Previous relation is updated
|
| 654 |
+
assert B._dcm_dict == {C: Matrix([[1, 0, 0],[0, cos(b), sin(b)],[0, -sin(b), cos(b)]]),\
|
| 655 |
+
A: Matrix([[1, 0, 0],[0, cos(c), -sin(c)],[0, sin(c), cos(c)]])}
|
| 656 |
+
assert A._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(c), sin(c)],[0, -sin(c), cos(c)]])}
|
| 657 |
+
assert C._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(b), -sin(b)],[0, sin(b), cos(b)]])}
|
| 658 |
+
|
| 659 |
+
def test_dcm_cache_dict():
|
| 660 |
+
A = ReferenceFrame('A')
|
| 661 |
+
B = ReferenceFrame('B')
|
| 662 |
+
C = ReferenceFrame('C')
|
| 663 |
+
D = ReferenceFrame('D')
|
| 664 |
+
|
| 665 |
+
a, b, c = symbols('a b c')
|
| 666 |
+
|
| 667 |
+
B.orient_axis(A, A.x, a)
|
| 668 |
+
C.orient_axis(B, B.x, b)
|
| 669 |
+
D.orient_axis(C, C.x, c)
|
| 670 |
+
|
| 671 |
+
assert D._dcm_dict == {C: Matrix([[1, 0, 0],[0, cos(c), sin(c)],[0, -sin(c), cos(c)]])}
|
| 672 |
+
assert C._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(b), sin(b)],[0, -sin(b), cos(b)]]), \
|
| 673 |
+
D: Matrix([[1, 0, 0],[0, cos(c), -sin(c)],[0, sin(c), cos(c)]])}
|
| 674 |
+
assert B._dcm_dict == {A: Matrix([[1, 0, 0],[0, cos(a), sin(a)],[0, -sin(a), cos(a)]]), \
|
| 675 |
+
C: Matrix([[1, 0, 0],[0, cos(b), -sin(b)],[0, sin(b), cos(b)]])}
|
| 676 |
+
assert A._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(a), -sin(a)],[0, sin(a), cos(a)]])}
|
| 677 |
+
|
| 678 |
+
assert D._dcm_dict == D._dcm_cache
|
| 679 |
+
|
| 680 |
+
D.dcm(A) # Check calculated dcm relation is stored in _dcm_cache and not in _dcm_dict
|
| 681 |
+
assert list(A._dcm_cache.keys()) == [A, B, D]
|
| 682 |
+
assert list(D._dcm_cache.keys()) == [C, A]
|
| 683 |
+
assert list(A._dcm_dict.keys()) == [B]
|
| 684 |
+
assert list(D._dcm_dict.keys()) == [C]
|
| 685 |
+
assert A._dcm_dict != A._dcm_cache
|
| 686 |
+
|
| 687 |
+
A.orient_axis(B, B.x, b) # _dcm_cache of A is wiped out and new relation is stored.
|
| 688 |
+
assert A._dcm_dict == {B: Matrix([[1, 0, 0],[0, cos(b), sin(b)],[0, -sin(b), cos(b)]])}
|
| 689 |
+
assert A._dcm_dict == A._dcm_cache
|
| 690 |
+
assert B._dcm_dict == {C: Matrix([[1, 0, 0],[0, cos(b), -sin(b)],[0, sin(b), cos(b)]]), \
|
| 691 |
+
A: Matrix([[1, 0, 0],[0, cos(b), -sin(b)],[0, sin(b), cos(b)]])}
|
| 692 |
+
|
| 693 |
+
def test_xx_dyad():
|
| 694 |
+
N = ReferenceFrame('N')
|
| 695 |
+
F = ReferenceFrame('F', indices=['1', '2', '3'])
|
| 696 |
+
assert N.xx == Vector.outer(N.x, N.x)
|
| 697 |
+
assert F.xx == Vector.outer(F.x, F.x)
|
| 698 |
+
|
| 699 |
+
def test_xy_dyad():
|
| 700 |
+
N = ReferenceFrame('N')
|
| 701 |
+
F = ReferenceFrame('F', indices=['1', '2', '3'])
|
| 702 |
+
assert N.xy == Vector.outer(N.x, N.y)
|
| 703 |
+
assert F.xy == Vector.outer(F.x, F.y)
|
| 704 |
+
|
| 705 |
+
def test_xz_dyad():
|
| 706 |
+
N = ReferenceFrame('N')
|
| 707 |
+
F = ReferenceFrame('F', indices=['1', '2', '3'])
|
| 708 |
+
assert N.xz == Vector.outer(N.x, N.z)
|
| 709 |
+
assert F.xz == Vector.outer(F.x, F.z)
|
| 710 |
+
|
| 711 |
+
def test_yx_dyad():
|
| 712 |
+
N = ReferenceFrame('N')
|
| 713 |
+
F = ReferenceFrame('F', indices=['1', '2', '3'])
|
| 714 |
+
assert N.yx == Vector.outer(N.y, N.x)
|
| 715 |
+
assert F.yx == Vector.outer(F.y, F.x)
|
| 716 |
+
|
| 717 |
+
def test_yy_dyad():
|
| 718 |
+
N = ReferenceFrame('N')
|
| 719 |
+
F = ReferenceFrame('F', indices=['1', '2', '3'])
|
| 720 |
+
assert N.yy == Vector.outer(N.y, N.y)
|
| 721 |
+
assert F.yy == Vector.outer(F.y, F.y)
|
| 722 |
+
|
| 723 |
+
def test_yz_dyad():
|
| 724 |
+
N = ReferenceFrame('N')
|
| 725 |
+
F = ReferenceFrame('F', indices=['1', '2', '3'])
|
| 726 |
+
assert N.yz == Vector.outer(N.y, N.z)
|
| 727 |
+
assert F.yz == Vector.outer(F.y, F.z)
|
| 728 |
+
|
| 729 |
+
def test_zx_dyad():
|
| 730 |
+
N = ReferenceFrame('N')
|
| 731 |
+
F = ReferenceFrame('F', indices=['1', '2', '3'])
|
| 732 |
+
assert N.zx == Vector.outer(N.z, N.x)
|
| 733 |
+
assert F.zx == Vector.outer(F.z, F.x)
|
| 734 |
+
|
| 735 |
+
def test_zy_dyad():
|
| 736 |
+
N = ReferenceFrame('N')
|
| 737 |
+
F = ReferenceFrame('F', indices=['1', '2', '3'])
|
| 738 |
+
assert N.zy == Vector.outer(N.z, N.y)
|
| 739 |
+
assert F.zy == Vector.outer(F.z, F.y)
|
| 740 |
+
|
| 741 |
+
def test_zz_dyad():
|
| 742 |
+
N = ReferenceFrame('N')
|
| 743 |
+
F = ReferenceFrame('F', indices=['1', '2', '3'])
|
| 744 |
+
assert N.zz == Vector.outer(N.z, N.z)
|
| 745 |
+
assert F.zz == Vector.outer(F.z, F.z)
|
| 746 |
+
|
| 747 |
+
def test_unit_dyadic():
|
| 748 |
+
N = ReferenceFrame('N')
|
| 749 |
+
F = ReferenceFrame('F', indices=['1', '2', '3'])
|
| 750 |
+
assert N.u == N.xx + N.yy + N.zz
|
| 751 |
+
assert F.u == F.xx + F.yy + F.zz
|
| 752 |
+
|
| 753 |
+
|
| 754 |
+
def test_pickle_frame():
|
| 755 |
+
N = ReferenceFrame('N')
|
| 756 |
+
A = ReferenceFrame('A')
|
| 757 |
+
A.orient_axis(N, N.x, 1)
|
| 758 |
+
A_C_N = A.dcm(N)
|
| 759 |
+
N1 = pickle.loads(pickle.dumps(N))
|
| 760 |
+
A1 = tuple(N1._dcm_dict.keys())[0]
|
| 761 |
+
assert A1.dcm(N1) == A_C_N
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/test_functions.py
ADDED
|
@@ -0,0 +1,509 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.numbers import pi
|
| 2 |
+
from sympy.core.singleton import S
|
| 3 |
+
from sympy.core.symbol import symbols
|
| 4 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 5 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 6 |
+
from sympy.integrals.integrals import Integral
|
| 7 |
+
from sympy.physics.vector import Dyadic, Point, ReferenceFrame, Vector
|
| 8 |
+
from sympy.physics.vector.functions import (cross, dot, express,
|
| 9 |
+
time_derivative,
|
| 10 |
+
kinematic_equations, outer,
|
| 11 |
+
partial_velocity,
|
| 12 |
+
get_motion_params, dynamicsymbols)
|
| 13 |
+
from sympy.simplify import trigsimp
|
| 14 |
+
from sympy.testing.pytest import raises
|
| 15 |
+
|
| 16 |
+
q1, q2, q3, q4, q5 = symbols('q1 q2 q3 q4 q5')
|
| 17 |
+
N = ReferenceFrame('N')
|
| 18 |
+
A = N.orientnew('A', 'Axis', [q1, N.z])
|
| 19 |
+
B = A.orientnew('B', 'Axis', [q2, A.x])
|
| 20 |
+
C = B.orientnew('C', 'Axis', [q3, B.y])
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_dot():
|
| 24 |
+
assert dot(A.x, A.x) == 1
|
| 25 |
+
assert dot(A.x, A.y) == 0
|
| 26 |
+
assert dot(A.x, A.z) == 0
|
| 27 |
+
|
| 28 |
+
assert dot(A.y, A.x) == 0
|
| 29 |
+
assert dot(A.y, A.y) == 1
|
| 30 |
+
assert dot(A.y, A.z) == 0
|
| 31 |
+
|
| 32 |
+
assert dot(A.z, A.x) == 0
|
| 33 |
+
assert dot(A.z, A.y) == 0
|
| 34 |
+
assert dot(A.z, A.z) == 1
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def test_dot_different_frames():
|
| 38 |
+
assert dot(N.x, A.x) == cos(q1)
|
| 39 |
+
assert dot(N.x, A.y) == -sin(q1)
|
| 40 |
+
assert dot(N.x, A.z) == 0
|
| 41 |
+
assert dot(N.y, A.x) == sin(q1)
|
| 42 |
+
assert dot(N.y, A.y) == cos(q1)
|
| 43 |
+
assert dot(N.y, A.z) == 0
|
| 44 |
+
assert dot(N.z, A.x) == 0
|
| 45 |
+
assert dot(N.z, A.y) == 0
|
| 46 |
+
assert dot(N.z, A.z) == 1
|
| 47 |
+
|
| 48 |
+
assert trigsimp(dot(N.x, A.x + A.y)) == sqrt(2)*cos(q1 + pi/4)
|
| 49 |
+
assert trigsimp(dot(N.x, A.x + A.y)) == trigsimp(dot(A.x + A.y, N.x))
|
| 50 |
+
|
| 51 |
+
assert dot(A.x, C.x) == cos(q3)
|
| 52 |
+
assert dot(A.x, C.y) == 0
|
| 53 |
+
assert dot(A.x, C.z) == sin(q3)
|
| 54 |
+
assert dot(A.y, C.x) == sin(q2)*sin(q3)
|
| 55 |
+
assert dot(A.y, C.y) == cos(q2)
|
| 56 |
+
assert dot(A.y, C.z) == -sin(q2)*cos(q3)
|
| 57 |
+
assert dot(A.z, C.x) == -cos(q2)*sin(q3)
|
| 58 |
+
assert dot(A.z, C.y) == sin(q2)
|
| 59 |
+
assert dot(A.z, C.z) == cos(q2)*cos(q3)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def test_cross():
|
| 63 |
+
assert cross(A.x, A.x) == 0
|
| 64 |
+
assert cross(A.x, A.y) == A.z
|
| 65 |
+
assert cross(A.x, A.z) == -A.y
|
| 66 |
+
|
| 67 |
+
assert cross(A.y, A.x) == -A.z
|
| 68 |
+
assert cross(A.y, A.y) == 0
|
| 69 |
+
assert cross(A.y, A.z) == A.x
|
| 70 |
+
|
| 71 |
+
assert cross(A.z, A.x) == A.y
|
| 72 |
+
assert cross(A.z, A.y) == -A.x
|
| 73 |
+
assert cross(A.z, A.z) == 0
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def test_cross_different_frames():
|
| 77 |
+
assert cross(N.x, A.x) == sin(q1)*A.z
|
| 78 |
+
assert cross(N.x, A.y) == cos(q1)*A.z
|
| 79 |
+
assert cross(N.x, A.z) == -sin(q1)*A.x - cos(q1)*A.y
|
| 80 |
+
assert cross(N.y, A.x) == -cos(q1)*A.z
|
| 81 |
+
assert cross(N.y, A.y) == sin(q1)*A.z
|
| 82 |
+
assert cross(N.y, A.z) == cos(q1)*A.x - sin(q1)*A.y
|
| 83 |
+
assert cross(N.z, A.x) == A.y
|
| 84 |
+
assert cross(N.z, A.y) == -A.x
|
| 85 |
+
assert cross(N.z, A.z) == 0
|
| 86 |
+
|
| 87 |
+
assert cross(N.x, A.x) == sin(q1)*A.z
|
| 88 |
+
assert cross(N.x, A.y) == cos(q1)*A.z
|
| 89 |
+
assert cross(N.x, A.x + A.y) == sin(q1)*A.z + cos(q1)*A.z
|
| 90 |
+
assert cross(A.x + A.y, N.x) == -sin(q1)*A.z - cos(q1)*A.z
|
| 91 |
+
|
| 92 |
+
assert cross(A.x, C.x) == sin(q3)*C.y
|
| 93 |
+
assert cross(A.x, C.y) == -sin(q3)*C.x + cos(q3)*C.z
|
| 94 |
+
assert cross(A.x, C.z) == -cos(q3)*C.y
|
| 95 |
+
assert cross(C.x, A.x) == -sin(q3)*C.y
|
| 96 |
+
assert cross(C.y, A.x).express(C).simplify() == sin(q3)*C.x - cos(q3)*C.z
|
| 97 |
+
assert cross(C.z, A.x) == cos(q3)*C.y
|
| 98 |
+
|
| 99 |
+
def test_operator_match():
|
| 100 |
+
"""Test that the output of dot, cross, outer functions match
|
| 101 |
+
operator behavior.
|
| 102 |
+
"""
|
| 103 |
+
A = ReferenceFrame('A')
|
| 104 |
+
v = A.x + A.y
|
| 105 |
+
d = v | v
|
| 106 |
+
zerov = Vector(0)
|
| 107 |
+
zerod = Dyadic(0)
|
| 108 |
+
|
| 109 |
+
# dot products
|
| 110 |
+
assert d & d == dot(d, d)
|
| 111 |
+
assert d & zerod == dot(d, zerod)
|
| 112 |
+
assert zerod & d == dot(zerod, d)
|
| 113 |
+
assert d & v == dot(d, v)
|
| 114 |
+
assert v & d == dot(v, d)
|
| 115 |
+
assert d & zerov == dot(d, zerov)
|
| 116 |
+
assert zerov & d == dot(zerov, d)
|
| 117 |
+
raises(TypeError, lambda: dot(d, S.Zero))
|
| 118 |
+
raises(TypeError, lambda: dot(S.Zero, d))
|
| 119 |
+
raises(TypeError, lambda: dot(d, 0))
|
| 120 |
+
raises(TypeError, lambda: dot(0, d))
|
| 121 |
+
assert v & v == dot(v, v)
|
| 122 |
+
assert v & zerov == dot(v, zerov)
|
| 123 |
+
assert zerov & v == dot(zerov, v)
|
| 124 |
+
raises(TypeError, lambda: dot(v, S.Zero))
|
| 125 |
+
raises(TypeError, lambda: dot(S.Zero, v))
|
| 126 |
+
raises(TypeError, lambda: dot(v, 0))
|
| 127 |
+
raises(TypeError, lambda: dot(0, v))
|
| 128 |
+
|
| 129 |
+
# cross products
|
| 130 |
+
raises(TypeError, lambda: cross(d, d))
|
| 131 |
+
raises(TypeError, lambda: cross(d, zerod))
|
| 132 |
+
raises(TypeError, lambda: cross(zerod, d))
|
| 133 |
+
assert d ^ v == cross(d, v)
|
| 134 |
+
assert v ^ d == cross(v, d)
|
| 135 |
+
assert d ^ zerov == cross(d, zerov)
|
| 136 |
+
assert zerov ^ d == cross(zerov, d)
|
| 137 |
+
assert zerov ^ d == cross(zerov, d)
|
| 138 |
+
raises(TypeError, lambda: cross(d, S.Zero))
|
| 139 |
+
raises(TypeError, lambda: cross(S.Zero, d))
|
| 140 |
+
raises(TypeError, lambda: cross(d, 0))
|
| 141 |
+
raises(TypeError, lambda: cross(0, d))
|
| 142 |
+
assert v ^ v == cross(v, v)
|
| 143 |
+
assert v ^ zerov == cross(v, zerov)
|
| 144 |
+
assert zerov ^ v == cross(zerov, v)
|
| 145 |
+
raises(TypeError, lambda: cross(v, S.Zero))
|
| 146 |
+
raises(TypeError, lambda: cross(S.Zero, v))
|
| 147 |
+
raises(TypeError, lambda: cross(v, 0))
|
| 148 |
+
raises(TypeError, lambda: cross(0, v))
|
| 149 |
+
|
| 150 |
+
# outer products
|
| 151 |
+
raises(TypeError, lambda: outer(d, d))
|
| 152 |
+
raises(TypeError, lambda: outer(d, zerod))
|
| 153 |
+
raises(TypeError, lambda: outer(zerod, d))
|
| 154 |
+
raises(TypeError, lambda: outer(d, v))
|
| 155 |
+
raises(TypeError, lambda: outer(v, d))
|
| 156 |
+
raises(TypeError, lambda: outer(d, zerov))
|
| 157 |
+
raises(TypeError, lambda: outer(zerov, d))
|
| 158 |
+
raises(TypeError, lambda: outer(zerov, d))
|
| 159 |
+
raises(TypeError, lambda: outer(d, S.Zero))
|
| 160 |
+
raises(TypeError, lambda: outer(S.Zero, d))
|
| 161 |
+
raises(TypeError, lambda: outer(d, 0))
|
| 162 |
+
raises(TypeError, lambda: outer(0, d))
|
| 163 |
+
assert v | v == outer(v, v)
|
| 164 |
+
assert v | zerov == outer(v, zerov)
|
| 165 |
+
assert zerov | v == outer(zerov, v)
|
| 166 |
+
raises(TypeError, lambda: outer(v, S.Zero))
|
| 167 |
+
raises(TypeError, lambda: outer(S.Zero, v))
|
| 168 |
+
raises(TypeError, lambda: outer(v, 0))
|
| 169 |
+
raises(TypeError, lambda: outer(0, v))
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def test_express():
|
| 173 |
+
assert express(Vector(0), N) == Vector(0)
|
| 174 |
+
assert express(S.Zero, N) is S.Zero
|
| 175 |
+
assert express(A.x, C) == cos(q3)*C.x + sin(q3)*C.z
|
| 176 |
+
assert express(A.y, C) == sin(q2)*sin(q3)*C.x + cos(q2)*C.y - \
|
| 177 |
+
sin(q2)*cos(q3)*C.z
|
| 178 |
+
assert express(A.z, C) == -sin(q3)*cos(q2)*C.x + sin(q2)*C.y + \
|
| 179 |
+
cos(q2)*cos(q3)*C.z
|
| 180 |
+
assert express(A.x, N) == cos(q1)*N.x + sin(q1)*N.y
|
| 181 |
+
assert express(A.y, N) == -sin(q1)*N.x + cos(q1)*N.y
|
| 182 |
+
assert express(A.z, N) == N.z
|
| 183 |
+
assert express(A.x, A) == A.x
|
| 184 |
+
assert express(A.y, A) == A.y
|
| 185 |
+
assert express(A.z, A) == A.z
|
| 186 |
+
assert express(A.x, B) == B.x
|
| 187 |
+
assert express(A.y, B) == cos(q2)*B.y - sin(q2)*B.z
|
| 188 |
+
assert express(A.z, B) == sin(q2)*B.y + cos(q2)*B.z
|
| 189 |
+
assert express(A.x, C) == cos(q3)*C.x + sin(q3)*C.z
|
| 190 |
+
assert express(A.y, C) == sin(q2)*sin(q3)*C.x + cos(q2)*C.y - \
|
| 191 |
+
sin(q2)*cos(q3)*C.z
|
| 192 |
+
assert express(A.z, C) == -sin(q3)*cos(q2)*C.x + sin(q2)*C.y + \
|
| 193 |
+
cos(q2)*cos(q3)*C.z
|
| 194 |
+
# Check to make sure UnitVectors get converted properly
|
| 195 |
+
assert express(N.x, N) == N.x
|
| 196 |
+
assert express(N.y, N) == N.y
|
| 197 |
+
assert express(N.z, N) == N.z
|
| 198 |
+
assert express(N.x, A) == (cos(q1)*A.x - sin(q1)*A.y)
|
| 199 |
+
assert express(N.y, A) == (sin(q1)*A.x + cos(q1)*A.y)
|
| 200 |
+
assert express(N.z, A) == A.z
|
| 201 |
+
assert express(N.x, B) == (cos(q1)*B.x - sin(q1)*cos(q2)*B.y +
|
| 202 |
+
sin(q1)*sin(q2)*B.z)
|
| 203 |
+
assert express(N.y, B) == (sin(q1)*B.x + cos(q1)*cos(q2)*B.y -
|
| 204 |
+
sin(q2)*cos(q1)*B.z)
|
| 205 |
+
assert express(N.z, B) == (sin(q2)*B.y + cos(q2)*B.z)
|
| 206 |
+
assert express(N.x, C) == (
|
| 207 |
+
(cos(q1)*cos(q3) - sin(q1)*sin(q2)*sin(q3))*C.x -
|
| 208 |
+
sin(q1)*cos(q2)*C.y +
|
| 209 |
+
(sin(q3)*cos(q1) + sin(q1)*sin(q2)*cos(q3))*C.z)
|
| 210 |
+
assert express(N.y, C) == (
|
| 211 |
+
(sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*C.x +
|
| 212 |
+
cos(q1)*cos(q2)*C.y +
|
| 213 |
+
(sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*C.z)
|
| 214 |
+
assert express(N.z, C) == (-sin(q3)*cos(q2)*C.x + sin(q2)*C.y +
|
| 215 |
+
cos(q2)*cos(q3)*C.z)
|
| 216 |
+
|
| 217 |
+
assert express(A.x, N) == (cos(q1)*N.x + sin(q1)*N.y)
|
| 218 |
+
assert express(A.y, N) == (-sin(q1)*N.x + cos(q1)*N.y)
|
| 219 |
+
assert express(A.z, N) == N.z
|
| 220 |
+
assert express(A.x, A) == A.x
|
| 221 |
+
assert express(A.y, A) == A.y
|
| 222 |
+
assert express(A.z, A) == A.z
|
| 223 |
+
assert express(A.x, B) == B.x
|
| 224 |
+
assert express(A.y, B) == (cos(q2)*B.y - sin(q2)*B.z)
|
| 225 |
+
assert express(A.z, B) == (sin(q2)*B.y + cos(q2)*B.z)
|
| 226 |
+
assert express(A.x, C) == (cos(q3)*C.x + sin(q3)*C.z)
|
| 227 |
+
assert express(A.y, C) == (sin(q2)*sin(q3)*C.x + cos(q2)*C.y -
|
| 228 |
+
sin(q2)*cos(q3)*C.z)
|
| 229 |
+
assert express(A.z, C) == (-sin(q3)*cos(q2)*C.x + sin(q2)*C.y +
|
| 230 |
+
cos(q2)*cos(q3)*C.z)
|
| 231 |
+
|
| 232 |
+
assert express(B.x, N) == (cos(q1)*N.x + sin(q1)*N.y)
|
| 233 |
+
assert express(B.y, N) == (-sin(q1)*cos(q2)*N.x +
|
| 234 |
+
cos(q1)*cos(q2)*N.y + sin(q2)*N.z)
|
| 235 |
+
assert express(B.z, N) == (sin(q1)*sin(q2)*N.x -
|
| 236 |
+
sin(q2)*cos(q1)*N.y + cos(q2)*N.z)
|
| 237 |
+
assert express(B.x, A) == A.x
|
| 238 |
+
assert express(B.y, A) == (cos(q2)*A.y + sin(q2)*A.z)
|
| 239 |
+
assert express(B.z, A) == (-sin(q2)*A.y + cos(q2)*A.z)
|
| 240 |
+
assert express(B.x, B) == B.x
|
| 241 |
+
assert express(B.y, B) == B.y
|
| 242 |
+
assert express(B.z, B) == B.z
|
| 243 |
+
assert express(B.x, C) == (cos(q3)*C.x + sin(q3)*C.z)
|
| 244 |
+
assert express(B.y, C) == C.y
|
| 245 |
+
assert express(B.z, C) == (-sin(q3)*C.x + cos(q3)*C.z)
|
| 246 |
+
|
| 247 |
+
assert express(C.x, N) == (
|
| 248 |
+
(cos(q1)*cos(q3) - sin(q1)*sin(q2)*sin(q3))*N.x +
|
| 249 |
+
(sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*N.y -
|
| 250 |
+
sin(q3)*cos(q2)*N.z)
|
| 251 |
+
assert express(C.y, N) == (
|
| 252 |
+
-sin(q1)*cos(q2)*N.x + cos(q1)*cos(q2)*N.y + sin(q2)*N.z)
|
| 253 |
+
assert express(C.z, N) == (
|
| 254 |
+
(sin(q3)*cos(q1) + sin(q1)*sin(q2)*cos(q3))*N.x +
|
| 255 |
+
(sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*N.y +
|
| 256 |
+
cos(q2)*cos(q3)*N.z)
|
| 257 |
+
assert express(C.x, A) == (cos(q3)*A.x + sin(q2)*sin(q3)*A.y -
|
| 258 |
+
sin(q3)*cos(q2)*A.z)
|
| 259 |
+
assert express(C.y, A) == (cos(q2)*A.y + sin(q2)*A.z)
|
| 260 |
+
assert express(C.z, A) == (sin(q3)*A.x - sin(q2)*cos(q3)*A.y +
|
| 261 |
+
cos(q2)*cos(q3)*A.z)
|
| 262 |
+
assert express(C.x, B) == (cos(q3)*B.x - sin(q3)*B.z)
|
| 263 |
+
assert express(C.y, B) == B.y
|
| 264 |
+
assert express(C.z, B) == (sin(q3)*B.x + cos(q3)*B.z)
|
| 265 |
+
assert express(C.x, C) == C.x
|
| 266 |
+
assert express(C.y, C) == C.y
|
| 267 |
+
assert express(C.z, C) == C.z == (C.z)
|
| 268 |
+
|
| 269 |
+
# Check to make sure Vectors get converted back to UnitVectors
|
| 270 |
+
assert N.x == express((cos(q1)*A.x - sin(q1)*A.y), N).simplify()
|
| 271 |
+
assert N.y == express((sin(q1)*A.x + cos(q1)*A.y), N).simplify()
|
| 272 |
+
assert N.x == express((cos(q1)*B.x - sin(q1)*cos(q2)*B.y +
|
| 273 |
+
sin(q1)*sin(q2)*B.z), N).simplify()
|
| 274 |
+
assert N.y == express((sin(q1)*B.x + cos(q1)*cos(q2)*B.y -
|
| 275 |
+
sin(q2)*cos(q1)*B.z), N).simplify()
|
| 276 |
+
assert N.z == express((sin(q2)*B.y + cos(q2)*B.z), N).simplify()
|
| 277 |
+
|
| 278 |
+
"""
|
| 279 |
+
These don't really test our code, they instead test the auto simplification
|
| 280 |
+
(or lack thereof) of SymPy.
|
| 281 |
+
assert N.x == express((
|
| 282 |
+
(cos(q1)*cos(q3)-sin(q1)*sin(q2)*sin(q3))*C.x -
|
| 283 |
+
sin(q1)*cos(q2)*C.y +
|
| 284 |
+
(sin(q3)*cos(q1)+sin(q1)*sin(q2)*cos(q3))*C.z), N)
|
| 285 |
+
assert N.y == express((
|
| 286 |
+
(sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*C.x +
|
| 287 |
+
cos(q1)*cos(q2)*C.y +
|
| 288 |
+
(sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*C.z), N)
|
| 289 |
+
assert N.z == express((-sin(q3)*cos(q2)*C.x + sin(q2)*C.y +
|
| 290 |
+
cos(q2)*cos(q3)*C.z), N)
|
| 291 |
+
"""
|
| 292 |
+
|
| 293 |
+
assert A.x == express((cos(q1)*N.x + sin(q1)*N.y), A).simplify()
|
| 294 |
+
assert A.y == express((-sin(q1)*N.x + cos(q1)*N.y), A).simplify()
|
| 295 |
+
|
| 296 |
+
assert A.y == express((cos(q2)*B.y - sin(q2)*B.z), A).simplify()
|
| 297 |
+
assert A.z == express((sin(q2)*B.y + cos(q2)*B.z), A).simplify()
|
| 298 |
+
|
| 299 |
+
assert A.x == express((cos(q3)*C.x + sin(q3)*C.z), A).simplify()
|
| 300 |
+
|
| 301 |
+
# Tripsimp messes up here too.
|
| 302 |
+
#print express((sin(q2)*sin(q3)*C.x + cos(q2)*C.y -
|
| 303 |
+
# sin(q2)*cos(q3)*C.z), A)
|
| 304 |
+
assert A.y == express((sin(q2)*sin(q3)*C.x + cos(q2)*C.y -
|
| 305 |
+
sin(q2)*cos(q3)*C.z), A).simplify()
|
| 306 |
+
|
| 307 |
+
assert A.z == express((-sin(q3)*cos(q2)*C.x + sin(q2)*C.y +
|
| 308 |
+
cos(q2)*cos(q3)*C.z), A).simplify()
|
| 309 |
+
assert B.x == express((cos(q1)*N.x + sin(q1)*N.y), B).simplify()
|
| 310 |
+
assert B.y == express((-sin(q1)*cos(q2)*N.x +
|
| 311 |
+
cos(q1)*cos(q2)*N.y + sin(q2)*N.z), B).simplify()
|
| 312 |
+
|
| 313 |
+
assert B.z == express((sin(q1)*sin(q2)*N.x -
|
| 314 |
+
sin(q2)*cos(q1)*N.y + cos(q2)*N.z), B).simplify()
|
| 315 |
+
|
| 316 |
+
assert B.y == express((cos(q2)*A.y + sin(q2)*A.z), B).simplify()
|
| 317 |
+
assert B.z == express((-sin(q2)*A.y + cos(q2)*A.z), B).simplify()
|
| 318 |
+
assert B.x == express((cos(q3)*C.x + sin(q3)*C.z), B).simplify()
|
| 319 |
+
assert B.z == express((-sin(q3)*C.x + cos(q3)*C.z), B).simplify()
|
| 320 |
+
|
| 321 |
+
"""
|
| 322 |
+
assert C.x == express((
|
| 323 |
+
(cos(q1)*cos(q3)-sin(q1)*sin(q2)*sin(q3))*N.x +
|
| 324 |
+
(sin(q1)*cos(q3)+sin(q2)*sin(q3)*cos(q1))*N.y -
|
| 325 |
+
sin(q3)*cos(q2)*N.z), C)
|
| 326 |
+
assert C.y == express((
|
| 327 |
+
-sin(q1)*cos(q2)*N.x + cos(q1)*cos(q2)*N.y + sin(q2)*N.z), C)
|
| 328 |
+
assert C.z == express((
|
| 329 |
+
(sin(q3)*cos(q1)+sin(q1)*sin(q2)*cos(q3))*N.x +
|
| 330 |
+
(sin(q1)*sin(q3)-sin(q2)*cos(q1)*cos(q3))*N.y +
|
| 331 |
+
cos(q2)*cos(q3)*N.z), C)
|
| 332 |
+
"""
|
| 333 |
+
assert C.x == express((cos(q3)*A.x + sin(q2)*sin(q3)*A.y -
|
| 334 |
+
sin(q3)*cos(q2)*A.z), C).simplify()
|
| 335 |
+
assert C.y == express((cos(q2)*A.y + sin(q2)*A.z), C).simplify()
|
| 336 |
+
assert C.z == express((sin(q3)*A.x - sin(q2)*cos(q3)*A.y +
|
| 337 |
+
cos(q2)*cos(q3)*A.z), C).simplify()
|
| 338 |
+
assert C.x == express((cos(q3)*B.x - sin(q3)*B.z), C).simplify()
|
| 339 |
+
assert C.z == express((sin(q3)*B.x + cos(q3)*B.z), C).simplify()
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
def test_time_derivative():
|
| 343 |
+
#The use of time_derivative for calculations pertaining to scalar
|
| 344 |
+
#fields has been tested in test_coordinate_vars in test_essential.py
|
| 345 |
+
A = ReferenceFrame('A')
|
| 346 |
+
q = dynamicsymbols('q')
|
| 347 |
+
qd = dynamicsymbols('q', 1)
|
| 348 |
+
B = A.orientnew('B', 'Axis', [q, A.z])
|
| 349 |
+
d = A.x | A.x
|
| 350 |
+
assert time_derivative(d, B) == (-qd) * (A.y | A.x) + \
|
| 351 |
+
(-qd) * (A.x | A.y)
|
| 352 |
+
d1 = A.x | B.y
|
| 353 |
+
assert time_derivative(d1, A) == - qd*(A.x|B.x)
|
| 354 |
+
assert time_derivative(d1, B) == - qd*(A.y|B.y)
|
| 355 |
+
d2 = A.x | B.x
|
| 356 |
+
assert time_derivative(d2, A) == qd*(A.x|B.y)
|
| 357 |
+
assert time_derivative(d2, B) == - qd*(A.y|B.x)
|
| 358 |
+
d3 = A.x | B.z
|
| 359 |
+
assert time_derivative(d3, A) == 0
|
| 360 |
+
assert time_derivative(d3, B) == - qd*(A.y|B.z)
|
| 361 |
+
q1, q2, q3, q4 = dynamicsymbols('q1 q2 q3 q4')
|
| 362 |
+
q1d, q2d, q3d, q4d = dynamicsymbols('q1 q2 q3 q4', 1)
|
| 363 |
+
q1dd, q2dd, q3dd, q4dd = dynamicsymbols('q1 q2 q3 q4', 2)
|
| 364 |
+
C = B.orientnew('C', 'Axis', [q4, B.x])
|
| 365 |
+
v1 = q1 * A.z
|
| 366 |
+
v2 = q2*A.x + q3*B.y
|
| 367 |
+
v3 = q1*A.x + q2*A.y + q3*A.z
|
| 368 |
+
assert time_derivative(B.x, C) == 0
|
| 369 |
+
assert time_derivative(B.y, C) == - q4d*B.z
|
| 370 |
+
assert time_derivative(B.z, C) == q4d*B.y
|
| 371 |
+
assert time_derivative(v1, B) == q1d*A.z
|
| 372 |
+
assert time_derivative(v1, C) == - q1*sin(q)*q4d*A.x + \
|
| 373 |
+
q1*cos(q)*q4d*A.y + q1d*A.z
|
| 374 |
+
assert time_derivative(v2, A) == q2d*A.x - q3*qd*B.x + q3d*B.y
|
| 375 |
+
assert time_derivative(v2, C) == q2d*A.x - q2*qd*A.y + \
|
| 376 |
+
q2*sin(q)*q4d*A.z + q3d*B.y - q3*q4d*B.z
|
| 377 |
+
assert time_derivative(v3, B) == (q2*qd + q1d)*A.x + \
|
| 378 |
+
(-q1*qd + q2d)*A.y + q3d*A.z
|
| 379 |
+
assert time_derivative(d, C) == - qd*(A.y|A.x) + \
|
| 380 |
+
sin(q)*q4d*(A.z|A.x) - qd*(A.x|A.y) + sin(q)*q4d*(A.x|A.z)
|
| 381 |
+
raises(ValueError, lambda: time_derivative(B.x, C, order=0.5))
|
| 382 |
+
raises(ValueError, lambda: time_derivative(B.x, C, order=-1))
|
| 383 |
+
|
| 384 |
+
|
| 385 |
+
def test_get_motion_methods():
|
| 386 |
+
#Initialization
|
| 387 |
+
t = dynamicsymbols._t
|
| 388 |
+
s1, s2, s3 = symbols('s1 s2 s3')
|
| 389 |
+
S1, S2, S3 = symbols('S1 S2 S3')
|
| 390 |
+
S4, S5, S6 = symbols('S4 S5 S6')
|
| 391 |
+
t1, t2 = symbols('t1 t2')
|
| 392 |
+
a, b, c = dynamicsymbols('a b c')
|
| 393 |
+
ad, bd, cd = dynamicsymbols('a b c', 1)
|
| 394 |
+
a2d, b2d, c2d = dynamicsymbols('a b c', 2)
|
| 395 |
+
v0 = S1*N.x + S2*N.y + S3*N.z
|
| 396 |
+
v01 = S4*N.x + S5*N.y + S6*N.z
|
| 397 |
+
v1 = s1*N.x + s2*N.y + s3*N.z
|
| 398 |
+
v2 = a*N.x + b*N.y + c*N.z
|
| 399 |
+
v2d = ad*N.x + bd*N.y + cd*N.z
|
| 400 |
+
v2dd = a2d*N.x + b2d*N.y + c2d*N.z
|
| 401 |
+
#Test position parameter
|
| 402 |
+
assert get_motion_params(frame = N) == (0, 0, 0)
|
| 403 |
+
assert get_motion_params(N, position=v1) == (0, 0, v1)
|
| 404 |
+
assert get_motion_params(N, position=v2) == (v2dd, v2d, v2)
|
| 405 |
+
#Test velocity parameter
|
| 406 |
+
assert get_motion_params(N, velocity=v1) == (0, v1, v1 * t)
|
| 407 |
+
assert get_motion_params(N, velocity=v1, position=v0, timevalue1=t1) == \
|
| 408 |
+
(0, v1, v0 + v1*(t - t1))
|
| 409 |
+
answer = get_motion_params(N, velocity=v1, position=v2, timevalue1=t1)
|
| 410 |
+
answer_expected = (0, v1, v1*t - v1*t1 + v2.subs(t, t1))
|
| 411 |
+
assert answer == answer_expected
|
| 412 |
+
|
| 413 |
+
answer = get_motion_params(N, velocity=v2, position=v0, timevalue1=t1)
|
| 414 |
+
integral_vector = Integral(a, (t, t1, t))*N.x + Integral(b, (t, t1, t))*N.y \
|
| 415 |
+
+ Integral(c, (t, t1, t))*N.z
|
| 416 |
+
answer_expected = (v2d, v2, v0 + integral_vector)
|
| 417 |
+
assert answer == answer_expected
|
| 418 |
+
|
| 419 |
+
#Test acceleration parameter
|
| 420 |
+
assert get_motion_params(N, acceleration=v1) == \
|
| 421 |
+
(v1, v1 * t, v1 * t**2/2)
|
| 422 |
+
assert get_motion_params(N, acceleration=v1, velocity=v0,
|
| 423 |
+
position=v2, timevalue1=t1, timevalue2=t2) == \
|
| 424 |
+
(v1, (v0 + v1*t - v1*t2),
|
| 425 |
+
-v0*t1 + v1*t**2/2 + v1*t2*t1 - \
|
| 426 |
+
v1*t1**2/2 + t*(v0 - v1*t2) + \
|
| 427 |
+
v2.subs(t, t1))
|
| 428 |
+
assert get_motion_params(N, acceleration=v1, velocity=v0,
|
| 429 |
+
position=v01, timevalue1=t1, timevalue2=t2) == \
|
| 430 |
+
(v1, v0 + v1*t - v1*t2,
|
| 431 |
+
-v0*t1 + v01 + v1*t**2/2 + \
|
| 432 |
+
v1*t2*t1 - v1*t1**2/2 + \
|
| 433 |
+
t*(v0 - v1*t2))
|
| 434 |
+
answer = get_motion_params(N, acceleration=a*N.x, velocity=S1*N.x,
|
| 435 |
+
position=S2*N.x, timevalue1=t1, timevalue2=t2)
|
| 436 |
+
i1 = Integral(a, (t, t2, t))
|
| 437 |
+
answer_expected = (a*N.x, (S1 + i1)*N.x, \
|
| 438 |
+
(S2 + Integral(S1 + i1, (t, t1, t)))*N.x)
|
| 439 |
+
assert answer == answer_expected
|
| 440 |
+
|
| 441 |
+
|
| 442 |
+
def test_kin_eqs():
|
| 443 |
+
q0, q1, q2, q3 = dynamicsymbols('q0 q1 q2 q3')
|
| 444 |
+
q0d, q1d, q2d, q3d = dynamicsymbols('q0 q1 q2 q3', 1)
|
| 445 |
+
u1, u2, u3 = dynamicsymbols('u1 u2 u3')
|
| 446 |
+
ke = kinematic_equations([u1,u2,u3], [q1,q2,q3], 'body', 313)
|
| 447 |
+
assert ke == kinematic_equations([u1,u2,u3], [q1,q2,q3], 'body', '313')
|
| 448 |
+
kds = kinematic_equations([u1, u2, u3], [q0, q1, q2, q3], 'quaternion')
|
| 449 |
+
assert kds == [-0.5 * q0 * u1 - 0.5 * q2 * u3 + 0.5 * q3 * u2 + q1d,
|
| 450 |
+
-0.5 * q0 * u2 + 0.5 * q1 * u3 - 0.5 * q3 * u1 + q2d,
|
| 451 |
+
-0.5 * q0 * u3 - 0.5 * q1 * u2 + 0.5 * q2 * u1 + q3d,
|
| 452 |
+
0.5 * q1 * u1 + 0.5 * q2 * u2 + 0.5 * q3 * u3 + q0d]
|
| 453 |
+
raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2], 'quaternion'))
|
| 454 |
+
raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2, q3], 'quaternion', '123'))
|
| 455 |
+
raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2, q3], 'foo'))
|
| 456 |
+
raises(TypeError, lambda: kinematic_equations(u1, [q0, q1, q2, q3], 'quaternion'))
|
| 457 |
+
raises(TypeError, lambda: kinematic_equations([u1], [q0, q1, q2, q3], 'quaternion'))
|
| 458 |
+
raises(TypeError, lambda: kinematic_equations([u1, u2, u3], q0, 'quaternion'))
|
| 459 |
+
raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2, q3], 'body'))
|
| 460 |
+
raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2, q3], 'space'))
|
| 461 |
+
raises(ValueError, lambda: kinematic_equations([u1, u2, u3], [q0, q1, q2], 'body', '222'))
|
| 462 |
+
assert kinematic_equations([0, 0, 0], [q0, q1, q2], 'space') == [S.Zero, S.Zero, S.Zero]
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
def test_partial_velocity():
|
| 466 |
+
q1, q2, q3, u1, u2, u3 = dynamicsymbols('q1 q2 q3 u1 u2 u3')
|
| 467 |
+
u4, u5 = dynamicsymbols('u4, u5')
|
| 468 |
+
r = symbols('r')
|
| 469 |
+
|
| 470 |
+
N = ReferenceFrame('N')
|
| 471 |
+
Y = N.orientnew('Y', 'Axis', [q1, N.z])
|
| 472 |
+
L = Y.orientnew('L', 'Axis', [q2, Y.x])
|
| 473 |
+
R = L.orientnew('R', 'Axis', [q3, L.y])
|
| 474 |
+
R.set_ang_vel(N, u1 * L.x + u2 * L.y + u3 * L.z)
|
| 475 |
+
|
| 476 |
+
C = Point('C')
|
| 477 |
+
C.set_vel(N, u4 * L.x + u5 * (Y.z ^ L.x))
|
| 478 |
+
Dmc = C.locatenew('Dmc', r * L.z)
|
| 479 |
+
Dmc.v2pt_theory(C, N, R)
|
| 480 |
+
|
| 481 |
+
vel_list = [Dmc.vel(N), C.vel(N), R.ang_vel_in(N)]
|
| 482 |
+
u_list = [u1, u2, u3, u4, u5]
|
| 483 |
+
assert (partial_velocity(vel_list, u_list, N) ==
|
| 484 |
+
[[- r*L.y, r*L.x, 0, L.x, cos(q2)*L.y - sin(q2)*L.z],
|
| 485 |
+
[0, 0, 0, L.x, cos(q2)*L.y - sin(q2)*L.z],
|
| 486 |
+
[L.x, L.y, L.z, 0, 0]])
|
| 487 |
+
|
| 488 |
+
# Make sure that partial velocities can be computed regardless if the
|
| 489 |
+
# orientation between frames is defined or not.
|
| 490 |
+
A = ReferenceFrame('A')
|
| 491 |
+
B = ReferenceFrame('B')
|
| 492 |
+
v = u4 * A.x + u5 * B.y
|
| 493 |
+
assert partial_velocity((v, ), (u4, u5), A) == [[A.x, B.y]]
|
| 494 |
+
|
| 495 |
+
raises(TypeError, lambda: partial_velocity(Dmc.vel(N), u_list, N))
|
| 496 |
+
raises(TypeError, lambda: partial_velocity(vel_list, u1, N))
|
| 497 |
+
|
| 498 |
+
def test_dynamicsymbols():
|
| 499 |
+
#Tests to check the assumptions applied to dynamicsymbols
|
| 500 |
+
f1 = dynamicsymbols('f1')
|
| 501 |
+
f2 = dynamicsymbols('f2', real=True)
|
| 502 |
+
f3 = dynamicsymbols('f3', positive=True)
|
| 503 |
+
f4, f5 = dynamicsymbols('f4,f5', commutative=False)
|
| 504 |
+
f6 = dynamicsymbols('f6', integer=True)
|
| 505 |
+
assert f1.is_real is None
|
| 506 |
+
assert f2.is_real
|
| 507 |
+
assert f3.is_positive
|
| 508 |
+
assert f4*f5 != f5*f4
|
| 509 |
+
assert f6.is_integer
|
hug/lib/python3.12/site-packages/sympy/physics/vector/tests/test_output.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.singleton import S
|
| 2 |
+
from sympy.physics.vector import Vector, ReferenceFrame, Dyadic
|
| 3 |
+
from sympy.testing.pytest import raises
|
| 4 |
+
|
| 5 |
+
A = ReferenceFrame('A')
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_output_type():
|
| 9 |
+
A = ReferenceFrame('A')
|
| 10 |
+
v = A.x + A.y
|
| 11 |
+
d = v | v
|
| 12 |
+
zerov = Vector(0)
|
| 13 |
+
zerod = Dyadic(0)
|
| 14 |
+
|
| 15 |
+
# dot products
|
| 16 |
+
assert isinstance(d & d, Dyadic)
|
| 17 |
+
assert isinstance(d & zerod, Dyadic)
|
| 18 |
+
assert isinstance(zerod & d, Dyadic)
|
| 19 |
+
assert isinstance(d & v, Vector)
|
| 20 |
+
assert isinstance(v & d, Vector)
|
| 21 |
+
assert isinstance(d & zerov, Vector)
|
| 22 |
+
assert isinstance(zerov & d, Vector)
|
| 23 |
+
raises(TypeError, lambda: d & S.Zero)
|
| 24 |
+
raises(TypeError, lambda: S.Zero & d)
|
| 25 |
+
raises(TypeError, lambda: d & 0)
|
| 26 |
+
raises(TypeError, lambda: 0 & d)
|
| 27 |
+
assert not isinstance(v & v, (Vector, Dyadic))
|
| 28 |
+
assert not isinstance(v & zerov, (Vector, Dyadic))
|
| 29 |
+
assert not isinstance(zerov & v, (Vector, Dyadic))
|
| 30 |
+
raises(TypeError, lambda: v & S.Zero)
|
| 31 |
+
raises(TypeError, lambda: S.Zero & v)
|
| 32 |
+
raises(TypeError, lambda: v & 0)
|
| 33 |
+
raises(TypeError, lambda: 0 & v)
|
| 34 |
+
|
| 35 |
+
# cross products
|
| 36 |
+
raises(TypeError, lambda: d ^ d)
|
| 37 |
+
raises(TypeError, lambda: d ^ zerod)
|
| 38 |
+
raises(TypeError, lambda: zerod ^ d)
|
| 39 |
+
assert isinstance(d ^ v, Dyadic)
|
| 40 |
+
assert isinstance(v ^ d, Dyadic)
|
| 41 |
+
assert isinstance(d ^ zerov, Dyadic)
|
| 42 |
+
assert isinstance(zerov ^ d, Dyadic)
|
| 43 |
+
assert isinstance(zerov ^ d, Dyadic)
|
| 44 |
+
raises(TypeError, lambda: d ^ S.Zero)
|
| 45 |
+
raises(TypeError, lambda: S.Zero ^ d)
|
| 46 |
+
raises(TypeError, lambda: d ^ 0)
|
| 47 |
+
raises(TypeError, lambda: 0 ^ d)
|
| 48 |
+
assert isinstance(v ^ v, Vector)
|
| 49 |
+
assert isinstance(v ^ zerov, Vector)
|
| 50 |
+
assert isinstance(zerov ^ v, Vector)
|
| 51 |
+
raises(TypeError, lambda: v ^ S.Zero)
|
| 52 |
+
raises(TypeError, lambda: S.Zero ^ v)
|
| 53 |
+
raises(TypeError, lambda: v ^ 0)
|
| 54 |
+
raises(TypeError, lambda: 0 ^ v)
|
| 55 |
+
|
| 56 |
+
# outer products
|
| 57 |
+
raises(TypeError, lambda: d | d)
|
| 58 |
+
raises(TypeError, lambda: d | zerod)
|
| 59 |
+
raises(TypeError, lambda: zerod | d)
|
| 60 |
+
raises(TypeError, lambda: d | v)
|
| 61 |
+
raises(TypeError, lambda: v | d)
|
| 62 |
+
raises(TypeError, lambda: d | zerov)
|
| 63 |
+
raises(TypeError, lambda: zerov | d)
|
| 64 |
+
raises(TypeError, lambda: zerov | d)
|
| 65 |
+
raises(TypeError, lambda: d | S.Zero)
|
| 66 |
+
raises(TypeError, lambda: S.Zero | d)
|
| 67 |
+
raises(TypeError, lambda: d | 0)
|
| 68 |
+
raises(TypeError, lambda: 0 | d)
|
| 69 |
+
assert isinstance(v | v, Dyadic)
|
| 70 |
+
assert isinstance(v | zerov, Dyadic)
|
| 71 |
+
assert isinstance(zerov | v, Dyadic)
|
| 72 |
+
raises(TypeError, lambda: v | S.Zero)
|
| 73 |
+
raises(TypeError, lambda: S.Zero | v)
|
| 74 |
+
raises(TypeError, lambda: v | 0)
|
| 75 |
+
raises(TypeError, lambda: 0 | v)
|