Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimension system refactoring #13287

Merged
merged 4 commits into from Sep 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions doc/src/modules/physics/units/examples.rst
Expand Up @@ -32,15 +32,27 @@ should be :math:`L^3 M^{-1} T^{-2}`.
>>> from sympy import symbols
>>> from sympy.physics.units import length, mass, acceleration, force
>>> from sympy.physics.units import gravitational_constant as G
>>> from sympy.physics.units.dimensions import dimsys_SI
>>> F = mass*acceleration
>>> F
Dimension(acceleration*mass)
>>> F.get_dimensional_dependencies()
>>> dimsys_SI.get_dimensional_dependencies(F)
{'length': 1, 'mass': 1, 'time': -2}
>>> force.get_dimensional_dependencies()
>>> dimsys_SI.get_dimensional_dependencies(force)
{'length': 1, 'mass': 1, 'time': -2}

Dimensions cannot compared directly, even if in the SI convention they are
the same:

>>> F == force
False

Dimension system objects provide a way to test the equivalence of
dimensions:

>>> dimsys_SI.equivalent_dims(F, force)
True

>>> m1, m2, r = symbols("m1 m2 r")
>>> grav_eq = G * m1 * m2 / r**2
>>> F2 = grav_eq.subs({m1: mass, m2: mass, r: length, G: G.dimension})
Expand Down
6 changes: 6 additions & 0 deletions sympy/core/tests/test_args.py
Expand Up @@ -3236,6 +3236,12 @@ def test_sympy__physics__units__dimensions__Dimension():
assert _test_args(Dimension("length", "L"))


def test_sympy__physics__units__dimensions__DimensionSystem():
from sympy.physics.units.dimensions import DimensionSystem
from sympy.physics.units.dimensions import length, time, velocity
assert _test_args(DimensionSystem((length, time), (velocity,)))


def test_sympy__physics__units__quantities__Quantity():
from sympy.physics.units.quantities import Quantity
from sympy.physics.units import length
Expand Down
76 changes: 38 additions & 38 deletions sympy/physics/units/definitions.py
Expand Up @@ -19,32 +19,32 @@
# Angular units (dimensionless)

rad = radian = radians = Quantity("radian", 1, 1)
deg = degree = degrees = Quantity("degree", 1, pi/180, "deg")
sr = steradian = steradians = Quantity("steradian", 1, 1, "sr")
mil = angular_mil = angular_mils = Quantity("angular_mil", 1, 2*pi/6400, "mil")
deg = degree = degrees = Quantity("degree", 1, pi/180, abbrev="deg")
sr = steradian = steradians = Quantity("steradian", 1, 1, abbrev="sr")
mil = angular_mil = angular_mils = Quantity("angular_mil", 1, 2*pi/6400, abbrev="mil")

# Base units:

m = meter = meters = Quantity("meter", length, 1, abbrev="m")
kg = kilogram = kilograms = Quantity("kilogram", mass, kilo, abbrev="kg")
s = second = seconds = Quantity("second", time, 1, abbrev="s")
A = ampere = amperes = Quantity("ampere", current, 1, abbrev='A')
K = kelvin = kelvins = Quantity('kelvin', temperature, 1, 'K')
mol = mole = moles = Quantity("mole", amount_of_substance, 1, "mol")
cd = candela = candelas = Quantity("candela", luminous_intensity, 1, "cd")
K = kelvin = kelvins = Quantity('kelvin', temperature, 1, abbrev='K')
mol = mole = moles = Quantity("mole", amount_of_substance, 1, abbrev="mol")
cd = candela = candelas = Quantity("candela", luminous_intensity, 1, abbrev="cd")

# gram; used to define its prefixed units

g = gram = grams = Quantity("gram", mass, 1, "g")
mg = milligram = milligrams = Quantity("milligram", mass, milli*gram, "mg")
ug = microgram = micrograms = Quantity("microgram", mass, micro*gram, "ug")
g = gram = grams = Quantity("gram", mass, 1, abbrev="g")
mg = milligram = milligrams = Quantity("milligram", mass, milli*gram, abbrev="mg")
ug = microgram = micrograms = Quantity("microgram", mass, micro*gram, abbrev="ug")

# derived units
newton = newtons = N = Quantity("newton", force, kilogram*meter/second**2, "N")
joule = joules = J = Quantity("joule", energy, newton*meter, "J")
watt = watts = W = Quantity("watt", power, joule/second, "W")
pascal = pascals = Pa = pa = Quantity("pascal", pressure, newton/meter**2, "Pa")
hertz = hz = Hz = Quantity("hertz", frequency, 1, "Hz")
newton = newtons = N = Quantity("newton", force, kilogram*meter/second**2, abbrev="N")
joule = joules = J = Quantity("joule", energy, newton*meter, abbrev="J")
watt = watts = W = Quantity("watt", power, joule/second, abbrev="W")
pascal = pascals = Pa = pa = Quantity("pascal", pressure, newton/meter**2, abbrev="Pa")
hertz = hz = Hz = Quantity("hertz", frequency, 1, abbrev="Hz")

# MKSA extension to MKS: derived units

Expand All @@ -64,17 +64,17 @@

# Common length units

km = kilometer = kilometers = Quantity("kilometer", length, kilo*meter, "km")
dm = decimeter = decimeters = Quantity("decimeter", length, deci*meter, "dm")
cm = centimeter = centimeters = Quantity("centimeter", length, centi*meter, "cm")
mm = millimeter = millimeters = Quantity("millimeter", length, milli*meter, "mm")
um = micrometer = micrometers = micron = microns = Quantity("micrometer", length, micro*meter, "um")
nm = nanometer = nanometers = Quantity("nanometer", length, nano*meter, "nn")
pm = picometer = picometers = Quantity("picometer", length, pico*meter, "pm")
km = kilometer = kilometers = Quantity("kilometer", length, kilo*meter, abbrev="km")
dm = decimeter = decimeters = Quantity("decimeter", length, deci*meter, abbrev="dm")
cm = centimeter = centimeters = Quantity("centimeter", length, centi*meter, abbrev="cm")
mm = millimeter = millimeters = Quantity("millimeter", length, milli*meter, abbrev="mm")
um = micrometer = micrometers = micron = microns = Quantity("micrometer", length, micro*meter, abbrev="um")
nm = nanometer = nanometers = Quantity("nanometer", length, nano*meter, abbrev="nn")
pm = picometer = picometers = Quantity("picometer", length, pico*meter, abbrev="pm")

ft = foot = feet = Quantity("foot", length, Rational(3048, 10000)*meter, "ft")
ft = foot = feet = Quantity("foot", length, Rational(3048, 10000)*meter, abbrev="ft")
inch = inches = Quantity("inch", length, foot/12)
yd = yard = yards = Quantity("yard", length, 3*feet, "yd")
yd = yard = yards = Quantity("yard", length, 3*feet, abbrev="yd")
mi = mile = miles = Quantity("mile", length, 5280*feet)
nmi = nautical_mile = nautical_miles = Quantity("nautical_mile", length, 6076*feet)

Expand All @@ -87,10 +87,10 @@

# Common time units

ms = millisecond = milliseconds = Quantity("millisecond", time, milli*second, "ms")
us = microsecond = microseconds = Quantity("microsecond", time, micro*second, "us")
ns = nanosecond = nanoseconds = Quantity("nanosecond", time, nano*second, "ns")
ps = picosecond = picoseconds = Quantity("picosecond", time, pico*second, "ps")
ms = millisecond = milliseconds = Quantity("millisecond", time, milli*second, abbrev="ms")
us = microsecond = microseconds = Quantity("microsecond", time, micro*second, abbrev="us")
ns = nanosecond = nanoseconds = Quantity("nanosecond", time, nano*second, abbrev="ns")
ps = picosecond = picoseconds = Quantity("picosecond", time, pico*second, abbrev="ps")

minute = minutes = Quantity("minute", time, 60*second)
h = hour = hours = Quantity("hour", time, 60*minute)
Expand Down Expand Up @@ -139,20 +139,20 @@
# Von Klitzing constant
von_klitzing_constant = Quantity("von_klitzing_constant", voltage/current, 25812.8074555*ohm, abbrev="R_k")
# Acceleration due to gravity (on the Earth surface)
gee = gees = acceleration_due_to_gravity = Quantity("acceleration_due_to_gravity", acceleration, 9.80665*meter/second**2, "g")
gee = gees = acceleration_due_to_gravity = Quantity("acceleration_due_to_gravity", acceleration, 9.80665*meter/second**2, abbrev="g")
# magnetic constant:
u0 = magnetic_constant = Quantity("magnetic_constant", force/current**2, 4*pi/10**7 * newton/ampere**2)
# electric constat:
e0 = electric_constant = vacuum_permittivity = Quantity("vacuum_permittivity", capacitance/length, 1/(u0 * c**2))
# vacuum impedance:
Z0 = vacuum_impedance = Quantity("vacuum_impedance", impedance, u0 * c)
# Coulomb's constant:
coulomb_constant = electric_force_constant = Quantity("coulomb_constant", force*length**2/charge**2, 1/(4*pi*vacuum_permittivity), "k_e")
coulomb_constant = electric_force_constant = Quantity("coulomb_constant", force*length**2/charge**2, 1/(4*pi*vacuum_permittivity), abbrev="k_e")

atmosphere = atmospheres = atm = Quantity("atmosphere", pressure, 101325 * pascal, "atm")
atmosphere = atmospheres = atm = Quantity("atmosphere", pressure, 101325 * pascal, abbrev="atm")

kPa = kilopascal = Quantity("kilopascal", pressure, kilo*Pa, "kPa")
bar = bars = Quantity("bar", pressure, 100*kPa, "bar")
kPa = kilopascal = Quantity("kilopascal", pressure, kilo*Pa, abbrev="kPa")
bar = bars = Quantity("bar", pressure, 100*kPa, abbrev="bar")
pound = pounds = Quantity("pound", mass, 0.45359237 * kg) # exact
psi = Quantity("psi", pressure, pound * gee / inch ** 2)
dHg0 = 13.5951 # approx value at 0 C
Expand All @@ -162,14 +162,14 @@

# Other convenient units and magnitudes

ly = lightyear = lightyears = Quantity("lightyear", length, speed_of_light*julian_year, "ly")
au = astronomical_unit = astronomical_units = Quantity("astronomical_unit", length, 149597870691*meter, "AU")
ly = lightyear = lightyears = Quantity("lightyear", length, speed_of_light*julian_year, abbrev="ly")
au = astronomical_unit = astronomical_units = Quantity("astronomical_unit", length, 149597870691*meter, abbrev="AU")

# Planck units:
planck_mass = Quantity("planck_mass", mass, sqrt(hbar*speed_of_light/G), "m_P")
planck_time = Quantity("planck_time", time, sqrt(hbar*G/speed_of_light**5), "t_P")
planck_temperature = Quantity("planck_temperature", temperature, sqrt(hbar*speed_of_light**5/G/boltzmann**2), "T_P")
planck_length = Quantity("planck_length", length, sqrt(hbar*G/speed_of_light**3), "l_P")
planck_mass = Quantity("planck_mass", mass, sqrt(hbar*speed_of_light/G), abbrev="m_P")
planck_time = Quantity("planck_time", time, sqrt(hbar*G/speed_of_light**5), abbrev="t_P")
planck_temperature = Quantity("planck_temperature", temperature, sqrt(hbar*speed_of_light**5/G/boltzmann**2), abbrev="T_P")
planck_length = Quantity("planck_length", length, sqrt(hbar*G/speed_of_light**3), abbrev="l_P")
# TODO: add more from https://en.wikipedia.org/wiki/Planck_units

# Information theory units:
Expand Down