Пример #1
0
def test_as_quantity():
    from sympy.physics.unitsystems.quantities import Quantity

    u = Unit(length, factor=10)
    q = Quantity(10, Unit(length))

    assert u.as_quantity == q
Пример #2
0
def test_div():
    u = Unit(length, factor=10)

    assert u.rdiv(1) == u.pow(-1)
    assert u.div(1) == u

    assert u.div(Unit(time, factor=2)) == Unit(length.div(time), factor=5)
    assert u.div(Unit(length, factor=2)) == 5
Пример #3
0
def test_abbrev():
    u = Unit(length)
    assert u.abbrev == ""

    u = Unit(length, abbrev="m")
    assert u.abbrev == "m"

    u = Unit(length, abbrev="m", prefix=k)
    assert u.abbrev == "km"
Пример #4
0
def test_eq():
    u = Unit(length, factor=10, abbrev="dm")

    v = Unit(length, factor=10)
    assert (u == v) is True

    v = Unit(time, factor=10, abbrev="ds")
    assert (u == v) is False

    v = Unit(length, factor=1, abbrev="dm")
    assert (u == v) is False
Пример #5
0
def test_call():
    A = Unit(current)
    Js = Unit(action)
    mksa = UnitSystem((m, kg, s, A), (Js,))

    assert mksa(Js) == mksa.print_unit_base(Js)
    assert mksa(Js.dim) == mksa._system(Js.dim)

    q = Quantity(10, Js)

    assert mksa(q) == "%g %s" % (q.factor, mksa(Js))
Пример #6
0
def test_definition():
    u = Unit(length, factor=10, abbrev="dm")

    assert u.dim == length
    assert u._factor == 10
    assert u._abbrev == "dm"
    assert u.prefix is None

    km = Unit(length, prefix=k)
    assert km.prefix == k

    v = Unit(u, factor=5)
    assert v.dim == length
    assert v._factor == 5 * 10
Пример #7
0
def test_mul():
    u = Unit(length, factor=10)

    assert u.mul(1) == u

    assert u.mul(Unit(time, factor=2)) == Unit(length.mul(time), factor=20)
    assert u.mul(Unit(length.pow(-1), factor=2)) == 20
Пример #8
0
def prefix_unit(unit, prefixes):
    """
    Return a list of all units formed by unit and the given prefixes.

    You can use the predefined PREFIXES or BIN_PREFIXES, but you can also
    pass as argument a subdict of them if you don't want all prefixed units.

        >>> from sympy.physics.unitsystems.prefixes import (PREFIXES,
        ...                                                 prefix_unit)
        >>> from sympy.physics.unitsystems.systems import mks
        >>> m = mks["m"]
        >>> pref = {"m": PREFIXES["m"], "c": PREFIXES["c"], "d": PREFIXES["d"]}
        >>> prefix_unit(m, pref)  #doctest: +SKIP
        [cm, dm, mm]
    """

    from sympy.physics.unitsystems.units import Unit

    prefixed_units = []

    for prefix in prefixes:
        prefixed_units.append(
            Unit(unit, abbrev=unit.abbrev, prefix=prefixes[prefix]))

    return prefixed_units
Пример #9
0
def test_extend():
    ms = UnitSystem((m, s), (c,))
    Js = Unit(action)
    mks = ms.extend((kg,), (Js,))

    res = UnitSystem((m, s, kg), (c, Js))
    assert set(mks._base_units) == set(res._base_units)
    assert set(mks._units) == set(res._units)
Пример #10
0
def test_operations():
    q = Quantity(10, m)
    p = Quantity(5, s)

    assert -q == Quantity(-10, m)

    assert q.add(Quantity(20, m)) == Quantity(30, m)
    assert q.add(Quantity(20, Unit(m.dim, 10))) == Quantity(30, Unit(m.dim, 11))

    assert q.sub(Quantity(20, m)) == Quantity(-10, m)
    assert q.sub(Quantity(20, Unit(m.dim, 10))) == Quantity(-10, Unit(m.dim, -9))

    assert q.pow(2) == Quantity(10**2, m.pow(2))

    assert q.mul(p) == Quantity(5*10, m.mul(s))

    assert q.div(p) == Quantity(10/5, m.div(s))
Пример #11
0
def test_pow():
    u = Unit(length, factor=10)

    assert u.pow(0) == 1
    assert u.pow(1) == u
    assert u.pow(2) == Unit(length.pow(2), factor=100)
    assert u.pow(-1) == Unit(length.pow(-1), factor=0.1)
Пример #12
0
def test_get_unit():
    ms = UnitSystem((m, s), (c,))

    assert ms.get_unit("s") == s
    assert ms.get_unit(s) == s
    assert ms.get_unit(Unit(time)) == s

    assert ms["s"] == ms.get_unit("s")
    raises(KeyError, lambda: ms["g"])
Пример #13
0
def test_definition():
    # want to test if the system can have several units of the same dimension
    dm = Unit(m, factor=0.1)

    base = (m, s)
    base_dim = (m.dim, s.dim)
    ms = UnitSystem(base, (c, dm), "MS", "MS system")

    assert set(ms._base_units) == set(base)
    assert set(ms._units) == set((m, s, c, dm))
    #assert ms._units == DimensionSystem._sort_dims(base + (velocity,))
    assert ms.name == "MS"
    assert ms.descr == "MS system"

    assert ms._system._base_dims == DimensionSystem.sort_dims(base_dim)
    assert set(ms._system._dims) == set(base_dim + (velocity,))
Пример #14
0
def test_add_sub():
    u = Unit(length, factor=10)
    v = Unit(length, factor=5)
    w = Unit(time, factor=2)

    assert u.add(v) == Unit(length, factor=15)
    assert u.sub(v) == Unit(length, factor=5)

    raises(ValueError, lambda: u.add(w))
    raises(ValueError, lambda: u.sub(w))
    raises(TypeError, lambda: u.add(1))
    raises(TypeError, lambda: u.sub(1))
Пример #15
0
def test_repr():
    u = Unit(length, factor=10, abbrev="m")
    assert repr(u) == u.abbrev_dim
Пример #16
0
def test_str():
    u = Unit(length, factor=10)
    assert str(u) == u.abbrev_dim

    u = Unit(length, factor=10, abbrev="m")
    assert str(u) == "m"
Пример #17
0
def test_abbrev_dim():
    u = Unit(length, factor=10)
    assert u.abbrev_dim == "(10 L)"
Пример #18
0
def test_print_unit_base():
    A = Unit(current)
    Js = Unit(action)
    mksa = UnitSystem((m, kg, s, A), (Js,))

    assert mksa.print_unit_base(Js) == "0.001 m^2 kg s^-2"
Пример #19
0
def test_factor():
    u = Unit(length, factor=10, abbrev="dm")
    assert u.factor == 10

    u = Unit(length, factor=5, prefix=k)
    assert u.factor == 5000
Пример #20
0
def test_error_definition():
    raises(TypeError, lambda: Unit("m"))
Пример #21
0
def test_is_compatible():
    u = Unit(length, factor=10)

    assert u.is_compatible(Unit(length)) is True
    assert u.is_compatible(Unit(time)) is False
    assert u.is_compatible(2) is False
Пример #22
0
def test_convert_to():
    q = Quantity(5, Unit(m.dim, prefix=k))
    assert q.convert_to(m) == Quantity(5000, m)
Пример #23
0
action = Dimension(name="action", symbol="A", length=2, mass=1, time=-1)
energy = Dimension(name="energy", symbol="E", length=2, mass=1, time=-2)
velocity = Dimension(name="velocity", symbol="V", length=1, time=-1)

# derived dimensions
length = Dimension(name="length", symbol="L", length=1)
mass = Dimension(name="mass", symbol="M", mass=1)
time = Dimension(name="time", symbol="T", time=1)
acceleration = Dimension(name="acceleration", length=1, time=-2)
momentum = Dimension(name="momentum", mass=1, length=1, time=-1)
force = Dimension(name="force", symbol="F", mass=1, length=1, time=-2)
power = Dimension(name="power", length=2, mass=1, time=-3)
frequency = Dimension(name="frequency", symbol="f", time=-1)

dims = (length, mass, time, momentum, force, energy, power, frequency)

# dimension system
natural_dim = DimensionSystem(base=(action, energy, velocity),
                              dims=dims,
                              name="Natural system")

# base units
hbar = Constant(action, factor=1.05457266e-34, abbrev="hbar")
eV = Unit(energy, factor=1.60219e-19, abbrev="eV")
c = Constant(velocity, factor=299792458, abbrev="c")

units = prefix_unit(eV, PREFIXES)

# unit system
natural = UnitSystem(base=(hbar, eV, c), units=units, name="Natural system")
Пример #24
0
charge = Dimension(name='charge', symbol='Q', current=1, time=1)
magnetic_density = Dimension(name='charge',
                             symbol='B',
                             mass=1,
                             current=-1,
                             time=-2)
magnetic_flux = Dimension(name='charge', length=2, mass=1, current=-1, time=-2)

dims = (voltage, impedance, conductance, capacitance, inductance, charge,
        magnetic_density, magnetic_flux)

# dimension system
mksa_dim = mks_dim.extend(base=(current, ), dims=dims, name='MKSA')

# base units
A = Unit(current, abbrev='A')

# derived units
V = Unit(voltage, factor=10**3, abbrev='V')
ohm = Unit(impedance, factor=10**3, abbrev='ohm')
# siemens
S = Unit(conductance, factor=10**-3, abbrev='S')
# farad
F = Unit(capacitance, factor=10**-3, abbrev='F')
# henry
H = Unit(inductance, factor=10**3, abbrev='H')
# coulomb
C = Unit(charge, abbrev='C')
# tesla
T = Unit(magnetic_density, abbrev='T')
# weber
Пример #25
0
# -*- coding: utf-8 -*-

from sympy.physics.unitsystems.dimensions import Dimension, DimensionSystem
from sympy.physics.unitsystems.units import Unit, UnitSystem
from sympy.physics.unitsystems.quantities import Quantity
from sympy.utilities.pytest import raises

length = Dimension(name="length", symbol="L", length=1)
mass = Dimension(name="mass", symbol="M", mass=1)
time = Dimension(name="time", symbol="T", time=1)
current = Dimension(name="current", symbol="I", current=1)
velocity = Dimension(name="velocity", symbol="V", length=1, time=-1)
action = Dimension(name="action", symbol="A", length=2, mass=1, time=-2)

m = Unit(length, abbrev="m")
s = Unit(time, abbrev="s")
kg = Unit(mass, factor=10**3, abbrev="kg")
c = Unit(velocity, abbrev="c")


def test_definition():
    # want to test if the system can have several units of the same dimension
    dm = Unit(m, factor=0.1)

    base = (m, s)
    base_dim = (m.dim, s.dim)
    ms = UnitSystem(base, (c, dm), "MS", "MS system")

    assert set(ms._base_units) == set(base)
    assert set(ms._units) == set((m, s, c, dm))
    #assert ms._units == DimensionSystem._sort_dims(base + (velocity,))