Exemple #1
0
    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)}))
Exemple #2
0
 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")
Exemple #3
0
 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}$")
Exemple #4
0
 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}$")
Exemple #5
0
    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)
Exemple #6
0
    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")
Exemple #7
0
    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")
Exemple #8
0
    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"))
Exemple #9
0
    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}))
Exemple #10
0
    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)
Exemple #11
0
 def setup(self):
     self.length = Dimension("L")
     self.mass = Dimension("M")
     self.time = Dimension("T")
Exemple #12
0
    def test_070_eq_ne(cls):

        cls.assertTrue(cls.m == Dimension({"L": 1}))
        cls.assertTrue(cls.m != cls.none)
Exemple #13
0
 def decorated(x):
     x = quantify(x)
     if not x.dimension == Dimension(None):
         raise DimensionError(x.dimension, Dimension(None))
     return math_func(x.value)
Exemple #14
0
np.iterable(m)

# %%
type(m.value)

# %%
iter(m)

# %% [markdown] tags=[]
# ## Array repr with 0 value
# Pick best favunit take the smallest when 0 is in the array with positiv and negativ values

# %%
from physipy import m, Quantity, Dimension
import numpy as np
Quantity(np.array([0, -1.2, 1.2]), Dimension("L"))

# %% [markdown]
# # Inplace change using asqarray

# %%
from physipy.quantity.utils import asqarray
print(type(m.value))
arrq_9 = np.array([m.__copy__()], dtype=object)
out = asqarray(arrq_9)
# this changes the type of m value
print(type(m.value))

# %% [markdown]
# # Numpy trapz implementaion not called when only x or dx is a quantity
# %timeit parse_str_to_dic("M*L**2/T**3*I**-1")
# %timeit new_parse_str_to_dict("M*L**2/T**3*I**-1")

# %%
# %timeit Dimension({"M":1, "L":2, "T":-3, "I":-1})
# %timeit Dimension("M*L**2/T**3*I**-1")

# %%
# %prun -D prunsum_file -s nfl  new_parse_str_to_dict("M*L**2/T**3*I**-1")
# !snakeviz prunsum_file

# %%
from physipy import Dimension

# %%
d = Dimension("L")

# %%
# %prun -D prun d*d

# %%
# !snakeviz prun

# %% [markdown]
# We need operations on array-like objects.
# The solutions are :
#  - a dict
#  - list
#  - numpy array
#  - ordered dict
#  - counter
Exemple #16
0
    "perm": (perm, a),
    "pow": (math_pow, (a, 2)),
    "prod": (prod, [a, a]),
    "radians": (radians, a),
    "remainder": (remainder, (a, b)),
    "sin": (sin, a),
    "sinh": (sinh, a),
    "sqrt": (sqrt, a),
    "tan": (tan, a),
    "tanh": (tanh, a),
    "trunc": (trunc, a),
}

q_rad = 0.4 * rad
q = 2.123 * m
q_dimless = Quantity(1, Dimension(None))

physipy_math_params = {
    "acos": (physipy.math.acos, q_dimless),
    "acosh": (physipy.math.acosh, q_dimless),
    "asin": (physipy.math.asin, q_dimless),
    "asinh": (physipy.math.asinh, q_dimless),
    "atan": (physipy.math.atan, q_dimless),
    "atan2": (physipy.math.atan2, (q, q)),
    "atanh": (physipy.math.atanh, q_dimless),
    "ceil": (physipy.math.ceil, q),
    "coysign": (physipy.math.copysign, (q, q)),
    #"comb"     :(comb     , 10, 3),
    "cos": (physipy.math.cos, q),
    "cosh": (physipy.math.cosh, q),
    "degrees": (physipy.math.degrees, q),
Exemple #17
0
    return x + y + 1 * m


print(sum_length(1 * m, 1 * m))


@check_dimension((m, m), (m))
def sum_length(x, y):
    "This function could be used on any Quantity, but here restricted to lengths."
    return x + y


print(sum_length(1 * m, 1 * m))


@check_dimension((Dimension("L"), Dimension("L")), (Dimension("L")))
def sum_length(x, y):
    return x + y


print(sum_length(1 * m, 1 * m))

# %% [markdown]
# ## Favunit setting
# This decorator simply sets the favunit of the outputs

# %%
mm = units["mm"]


@set_favunit(mm)
Exemple #18
0
print(z / 2)
#print(2//z)
#print(z//2)

# %% [markdown]
# # Support with physipy

# %%
import physipy
from physipy import m, cd, s, Quantity, Dimension

# %% [markdown]
# ## Construction

# %%
x = Quantity(mcerp.Normal(1, 2), Dimension("L"))
x

# %%
# using multiplication
# x = mcerp.Normal(1, 2)*s : NotUpcast: <class 'physipy.quantity.quantity.Quantity'> cannot be converted to a number with uncertainty
x = m * mcerp.Normal(1, 2)
x

# %% [markdown]
# ## Basic Operation

# %%
# with scalar
scalar = 2 * m
print(x + scalar)
Exemple #19
0
 def test_080_pow_inverse(cls):
     m_inverse = 1 / cls.m
     cls.assertEqual(m_inverse, Dimension({"L": -1}))
Exemple #20
0
# %% [markdown]
# ## Construction from class

# %% [markdown]
# Several ways are available constructing a Quantity object

# %%
from physipy import Quantity, m, sr, Dimension

# %% [markdown]
# Scalar Quantity

# %%
# from int
print(Quantity(1, Dimension("L")))

# from float
print(Quantity(1.123, Dimension("L")))

# from fraction.Fraction
from fractions import Fraction
print(Quantity(Fraction(1, 2), Dimension("L")))


# %% [markdown]
# Array-like Quantity

# %%
# from list
print(Quantity([1, 2, 3, 4], Dimension("L")))
Exemple #21
0
    def test_110_siunit_dict(cls):
        cls.assertEqual(
            Dimension(None).siunit_dict(), {
                'm': 0,
                'kg': 0,
                's': 0,
                'A': 0,
                'K': 0,
                'mol': 0,
                'cd': 0,
                'rad': 0,
                'sr': 0
            })
        cls.assertEqual(
            Dimension({
                "L": 1
            }).siunit_dict(), {
                'm': 1,
                'kg': 0,
                's': 0,
                'A': 0,
                'K': 0,
                'mol': 0,
                'cd': 0,
                'rad': 0,
                'sr': 0
            })
        cls.assertEqual(
            Dimension({
                "L": 1.2
            }).siunit_dict(), {
                'm': 1.2,
                'kg': 0,
                's': 0,
                'A': 0,
                'K': 0,
                'mol': 0,
                'cd': 0,
                'rad': 0,
                'sr': 0
            })
        cls.assertEqual(
            Dimension({
                "L": 1 / 2
            }).siunit_dict(), {
                'm': 1 / 2,
                'kg': 0,
                's': 0,
                'A': 0,
                'K': 0,
                'mol': 0,
                'cd': 0,
                'rad': 0,
                'sr': 0
            })

        cls.assertEqual(
            Dimension({
                "J": -1,
                "L": 1,
                "theta": 3
            }).siunit_dict(), {
                'm': 1,
                'kg': 0,
                's': 0,
                'A': 0,
                'K': 3,
                'mol': 0,
                'cd': -1,
                'rad': 0,
                'sr': 0
            })
Exemple #22
0
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))
Exemple #23
0
 def setUp(cls):
     cls.m = Dimension("L")
     cls.none = Dimension(None)
     cls.dim_complexe = Dimension({"J": 1, "theta": -3})
     cls.no_dimension_str = "no-dimension"
Exemple #24
0
#

# %% [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]
Exemple #25
0
 def test_080_inverse(cls):
     m_inverse = cls.m.inverse()
     cls.assertEqual(m_inverse, Dimension({"L": -1}))