def test_error_definition(): raises(ValueError, lambda: DimensionSystem((Dimension(time=1, name="time", symbol="T"), Dimension(length=1, symbol="L"), Dimension(mass=1, name="mass"), Dimension(current=1)))) raises(ValueError, lambda: DimensionSystem((length, time, velocity)))
def test_add_sub(): length = Dimension(length=1) assert length.add(length) == length assert length.sub(length) == length assert -length == length raises(TypeError, lambda: length.add(1)) raises(TypeError, lambda: length.sub(1)) raises(ValueError, lambda: length.add(Dimension(time=1))) raises(ValueError, lambda: length.sub(Dimension(time=1)))
def test_str_repr(): assert str(DimensionSystem((length, time), name="MS")) == "MS" dimsys = DimensionSystem((Dimension(length=1, symbol="L"), Dimension(time=1, symbol="T"))) assert str(dimsys) == "(L, T)" dimsys = DimensionSystem((Dimension(length=1, name="length", symbol="L"), Dimension(time=1, name="time"))) assert str(dimsys) == "(L, time)" assert (repr(DimensionSystem((length, time), name="MS")) == "<DimensionSystem: ({'length': 1}, {'time': 1})>")
def test_error_definition(): # tuple with more or less than two entries raises(ValueError, lambda: Dimension(("length", 1, 2))) raises(ValueError, lambda: Dimension(["length"])) # non-number power raises(TypeError, lambda: Dimension(length="a")) # non-dict/list/tuple as positional arg raises(TypeError, lambda: Dimension("length")) # non-number with named argument raises(TypeError, lambda: Dimension(length=(1, 2)))
def test_dict_properties(): dic = {"length": sympify(1), "time": sympify(2)} d = Dimension(dic) assert d["length"] == 1 assert set(d.items()) == set(dic.items()) assert len(d) == 2 assert d.get("length") == 1 assert d.get("mass") is None assert ("length" in d) is True assert ("mass" in d) is False
def test_definition(): base = (length, time) ms = DimensionSystem(base, (velocity,), "MS", "MS system") assert ms._base_dims == DimensionSystem.sort_dims(base) assert set(ms._dims) == set(base + (velocity,)) assert ms.name == "MS" assert ms.descr == "MS system" assert ms._can_transf_matrix is None # be sure that there is no duplicates in ms._dims dims = (Dimension(length=1), Dimension(length=1, symbol="l")) ms = DimensionSystem(base, dims) assert set(ms._dims) == set(base)
def test_get_dim(): ms = DimensionSystem((length, time), (velocity,)) assert ms.get_dim("L") == length assert ms.get_dim("length") == length assert ms.get_dim(length) == length assert ms.get_dim(Dimension(length=1)) == length assert ms["L"] == ms.get_dim("L") raises(KeyError, lambda: ms["M"])
def test_definition(): length = Dimension(name="length", symbol="L", length=1) assert length.get('length') == 1 assert length.get('time') is None assert length.name == "length" assert length.symbol == "L" halflength = Dimension(name="length", length=0.5) assert halflength.get('length') == sympify("1/2")
def test_properties(): assert Dimension(length=1).is_dimensionless is False assert Dimension().is_dimensionless is True assert Dimension(length=0).is_dimensionless is True assert Dimension(length=1).has_integer_powers is True assert Dimension(length=-1).has_integer_powers is True assert Dimension(length=1.5).has_integer_powers is False
def test_mul_div_exp(): length = Dimension(length=1) time = Dimension(time=1) velocity = length.div(time) assert length.pow(2) == Dimension(length=2) assert length.mul(length) == length.pow(2) assert length.mul(time) == Dimension(length=1, time=1) assert velocity == Dimension(length=1, time=-1) assert velocity.pow(2) == Dimension(length=2, time=-2) raises(TypeError, lambda: length.pow("a"))
def test_str(): assert str(Dimension(length=1)) == "{'length': 1}" assert str(Dimension(length=1, symbol="L")) == "L" assert str(Dimension(length=1, name="length")) == "length" assert str(Dimension(length=1, symbol="L", name="length")) == 'L'
""" MKS unit system. MKS stands for "meter, kilogram, second, ampere". """ from __future__ import division from sympy import pi from sympy.physics.unitsystems.dimensions import Dimension from sympy.physics.unitsystems.units import Unit, Constant from sympy.physics.unitsystems.prefixes import PREFIXES, prefix_unit from sympy.physics.unitsystems.systems.mks import mks_dim, mks # base dimensions current = Dimension(name='current', symbol='I', current=1) # derived dimensions voltage = Dimension(name='voltage', symbol='U', mass=1, length=2, current=-1, time=-3) impedance = Dimension(name='impedance', symbol='Z', mass=1, length=2, current=-2, time=-3) conductance = Dimension(name='conductance',
""" Naturalunit system. The natural system comes from "setting c = 1, hbar = 1". From the computer point of view it means that we use velocity and action instead of length and time. Moreover instead of mass we use energy. """ from __future__ import division from sympy.physics.unitsystems.dimensions import Dimension, DimensionSystem from sympy.physics.unitsystems.units import Unit, Constant, UnitSystem from sympy.physics.unitsystems.prefixes import PREFIXES, prefix_unit # base dimensions 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)
def test_call(): current = Dimension(name="current", symbol="I", current=1) mksa = DimensionSystem((length, time, mass, current), (action,)) assert mksa(action) == mksa.print_dim_base(action)
# -*- coding: utf-8 -*- from sympy import Matrix, eye from sympy.physics.unitsystems.dimensions import Dimension, DimensionSystem 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) velocity = Dimension(name="velocity", symbol="V", length=1, time=-1) action = Dimension(name="action", symbol="A", length=2, mass=1, time=-2) def test_definition(): base = (length, time) ms = DimensionSystem(base, (velocity,), "MS", "MS system") assert ms._base_dims == DimensionSystem.sort_dims(base) assert set(ms._dims) == set(base + (velocity,)) assert ms.name == "MS" assert ms.descr == "MS system" assert ms._can_transf_matrix is None # be sure that there is no duplicates in ms._dims dims = (Dimension(length=1), Dimension(length=1, symbol="l")) ms = DimensionSystem(base, dims) assert set(ms._dims) == set(base)