def test_060_pow(cls): cls.assertEqual(cls.m**2, Dimension({"L": 2})) cls.assertEqual(cls.m**(1 / 2), Dimension({"L": 1 / 2})) cls.assertEqual(cls.m**1.2, Dimension({"L": 1.2})) cls.assertEqual(cls.m**Fraction(1 / 2), Dimension({"L": Fraction(1 / 2)}))
def setUpClass(cls): cls.m = Dimension("L") cls.none = Dimension(None) cls.dim_complexe = Dimension({"J": 1, "theta": -3}) cls.no_dimension_str = "no-dimension" cls.times = [] cls.ids = [] cls.amp = Dimension("I")
def test_latex_SI_unit(cls): cls.assertEqual(Dimension(None).latex_SI_unit(), "$1$") cls.assertEqual(Dimension({"L": 1}).latex_SI_unit(), "$m$") cls.assertEqual(Dimension({"L": 2}).latex_SI_unit(), "$m^{2}$") cls.assertEqual( Dimension({ "J": -1, "L": 1, "theta": 3 }).latex_SI_unit(), r"$\frac{K^{3} m}{cd}$")
def test_repr_latex(cls): cls.assertEqual(Dimension(None)._repr_latex_(), "$1$") cls.assertEqual(Dimension({"L": 1})._repr_latex_(), "$L$") cls.assertEqual(Dimension({"L": 2})._repr_latex_(), "$L^{2}$") cls.assertEqual( Dimension({ "J": -1, "L": 1, "theta": 3 })._repr_latex_(), r"$\frac{L \theta^{3}}{J}$")
def test_050_div(cls): cls.assertEqual(cls.m / cls.dim_complexe, Dimension({ "J": -1, "L": 1, "theta": 3 })) # Testing the inversion by dividing 1 cls.assertEqual(1 / cls.m, Dimension({"L": -1})) # Dividing by a number, not a Dimension object cls.assertRaises(TypeError, lambda: cls.m / 1.12) cls.assertRaises(TypeError, lambda: 1.12 / cls.m)
def __init__(self, dimension=None, all_units=False, **kwargs): super().__init__(**kwargs) self.dimension = dimensionify(dimension) self.units = units self.units["-"] = Quantity(1, Dimension(None), symbol="-") # list of available units if self.dimension == Dimension(None) or all_units: self.favunit_str_list = [ u_str for u_str, u_q in self.units.items() ] else: self.favunit_str_list = [ u_str for u_str, u_q in self.units.items() if self.dimension == u_q.dimension ] self.favunit_str_list.append("-") self.value = self.units["-"] # dropdown self.favunit_dd = ipyw.Dropdown( options=self.favunit_str_list, # set init value value=str(self.value.symbol), description='Favunit:', layout=Layout(width="30%", margin="5px 5px 5px 5px", border="solid black"), ) self.children = [self.favunit_dd] ### 3. Change favunit # selection of favunit def update_favunit_on_favunit_dd_change(change): # retrieve new favunit q self.value = self.units[change.new] self.favunit_dd.observe(update_favunit_on_favunit_dd_change, names="value") def update_dd_value(change): self.favunit_dd.value = str(change.new.symbol) self.observe(update_dd_value, names="value")
def test_100_expr_parsing(cls): cls.assertEqual(cls.m, Dimension("L")) cls.assertEqual(cls.m, Dimension("L**1")) cls.assertEqual(cls.m * cls.m, Dimension("L**2")) cls.assertEqual(cls.m * cls.dim_complexe, Dimension("L*J/theta**3")) cls.assertEqual(cls.m, Dimension("m")) cls.assertEqual(cls.m * cls.m, Dimension("m**2")) cls.assertEqual(cls.m * cls.dim_complexe, Dimension("m*cd/K**3")) # sympy was parsing "I" as complex number cls.assertEqual(cls.amp * cls.m, Dimension("L*I")) with cls.assertRaises(TypeError): # sympy parsing not good with ^ char Dimension("m^2")
def test_010_init(cls): metre_by_dict = Dimension({"L": 1}) cls.assertEqual(cls.m, metre_by_dict) none_dimenion_dict = cls.none.dim_dict dico_dimension_none = { 'L': 0, 'M': 0, 'T': 0, 'I': 0, 'theta': 0, 'N': 0, 'J': 0, 'RAD': 0, 'SR': 0 } cls.assertEqual(none_dimenion_dict, dico_dimension_none) cls.assertRaises(TypeError, lambda: Dimension({"m": 1}))
def test_100_expr_parsing(cls): cls.assertEqual(cls.m, Dimension("L")) cls.assertEqual(cls.m, Dimension("L**1")) cls.assertEqual(cls.m * cls.m, Dimension("L**2")) cls.assertEqual(cls.m * cls.dim_complexe, Dimension("L*J/theta**3")) cls.assertEqual(cls.m, Dimension("m")) cls.assertEqual(cls.m * cls.m, Dimension("m**2")) cls.assertEqual(cls.m * cls.dim_complexe, Dimension("m*cd/K**3")) with cls.assertRaises(AttributeError): # sympy parsing not good with ^ char cls.assertEqual(cls.m * cls.m, Dimension("m^2"))
def test_040_mul(cls): cls.assertEqual(cls.m * cls.dim_complexe, Dimension({ "J": 1, "L": 1, "theta": -3 })) # Multipliying by a number, not a Dimension object cls.assertRaises(TypeError, lambda: cls.m * 1.12) cls.assertRaises(TypeError, lambda: 1.12 * cls.m)
# # %% [markdown] # ## Creation # Basic creation of dimension-full arrays : # %% import matplotlib.pyplot as plt import numpy as np import physipy from physipy import m, s, Quantity, Dimension, rad # %% x_samples = np.array([1, 2, 3, 4]) * m y_samples = Quantity(np.array([1, 2, 3, 4]), Dimension("T")) print(x_samples) print(y_samples) print(m * np.array([1, 2, 3, 4]) == x_samples) # multiplication is commutativ # %% [markdown] # ## Operation # Basic array operation are handled the 'expected' way : note that the resulting dimension are consistent with the operation applied : # %% print(x_samples + 1 * m) print(x_samples * 2) print(x_samples**2) print(1 / x_samples) # %% [markdown]
import sys #print(sys.path) sys.path.insert( 0, r"/Users/mocquin/Documents/CLE/Optique/Python/JUPYTER/MYLIB10/MODULES/physipy" ) import matplotlib.pyplot as plt import numpy as np import physipy from physipy import s from physipy import Dimension from IPython.display import display, Latex # %% a = Dimension(None) b = Dimension("T") c = Dimension({"T": 3}) d = Dimension({"T": -4}) e = Dimension({"T": 2, "L": -3}) # %% print(a) print(b) print(c) print(d) print(e) print() # %% print(str(a))
def setup(self): self.length = Dimension("L") self.mass = Dimension("M") self.time = Dimension("T")