Пример #1
0
 def test_eq(self):
     self.assertFalse(self.a == 5)
     self.assertNotEqual(self.a, 5)
     self.assertEqual(self.a, self.a)
     self.assertNotEqual(self.a, self.b)
     self.assertNotEqual(self.a, np.zeros((5, 5, 5)))
     self.assertEqual(self.arr1, ArrayUnit(self.arr1, Unit()))
     self.assertEqual(ArrayUnit(self.arr1, Unit()),
                      ArrayUnit(self.arr1, None))
Пример #2
0
 def test_div(self):
     self.a += 0.001  # avoid division by 0
     self.b += 0.001
     self.assertEqual(self.a / self.b,
                      ArrayUnit(self.arr1 / self.arr2, self.m / self.s))
     self.assertEqual(self.a // self.b,
                      ArrayUnit(self.arr1 // self.arr2, self.m / self.s))
     self.assertEqual(3 / self.a, ArrayUnit(3 / self.arr1, Unit('m', -1)))
     self.assertEqual(3 // self.a, ArrayUnit(3 // self.arr1, Unit('m', -1)))
     self.assertEqual(self.a, self.a / np.ones(self.a.shape))
Пример #3
0
    def setUp(self):
        ArrayUnit.is_strict = True

        self.arr1 = np.linspace(1, 9, 9).reshape(3, 3)
        self.arr2 = self.arr1 - 5
        self.m = Unit('m')
        self.s = Unit('s', -2)

        self.a = ArrayUnit(self.arr1, self.m)
        self.b = ArrayUnit(self.arr2, self.s)
Пример #4
0
def ex1():
    m = Unit('m')
    s = Unit('s', -2)
    arr = np.linspace(1,10,10, dtype=float)
    a = ArrayUnit(arr, m)
    b = ArrayUnit(arr**2, s)
    ArrayUnit.is_strict = True
    print(a, '\n+\n', 1, '\n=\n', a + 1)
    print('__________________________________________')
    print(a, '\n-\n', arr, '\n=\n', a - arr)
    print('__________________________________________')
    print(a, '\n*\n', b, '\n=\n', a * b)
    print('__________________________________________')
    print(b, '\n//\n', a, '\n=\n', b / a)
Пример #5
0
def ex2():
    newton = Unit({
        'kg': 1,
        'm': 1,
        's': -2
    })
    joule = newton * Unit('m')
    pascal = Unit({
        'kg': 1,
        'm': -1,
        's': -2
    })

    measure1 = ArrayUnit(np.random.random((3, 4)), joule)
    measure2 = ArrayUnit(np.random.random((3, 4)), pascal)
    print(measure1 / measure2)
    print(measure1 + 3)
Пример #6
0
    def test_mod(self):
        self.assertEqual(self.a % self.a,
                         ArrayUnit(np.zeros(self.a.shape), self.a.unit))
        self.assertEqual(self.a % self.arr1, self.a % self.a)

        ArrayUnit.is_strict = False
        with self.assertWarns(Warning):
            self.assertEqual((self.a % self.b).unit, self.a.unit)
        ArrayUnit.is_strict = True
        with self.assertRaises(ValueError):
            self.assertEqual((self.a % self.b).unit, self.a.unit)
Пример #7
0
    def test_sub(self):
        self.assertEqual(self.a - 1, ArrayUnit(self.arr1 - 1, self.m))
        self.assertEqual(self.b - self.b,
                         ArrayUnit(0 * self.arr2, Unit('s', -2)))

        ArrayUnit.is_strict = False
        # __sub__
        with self.assertWarns(Warning):
            self.assertEqual(
                self.a - self.b, self.a - self.arr2
            )  # a and b got different units, warnings, but a's unit is kept
        # __rsub__
        with self.assertWarns(Warning):
            self.assertEqual(self.b - self.a, self.b - self.arr1)
            self.assertNotEqual(self.b - self.a, self.arr2 -
                                self.a)  # unit will conflict (b vs a)

        ArrayUnit.is_strict = True
        with self.assertRaises(ValueError):
            self.a - self.b
Пример #8
0
    def test_add(self):
        self.assertEqual(self.a + 1, 1 + self.a)
        self.assertEqual(self.b + self.b,
                         ArrayUnit(2 * self.arr2, Unit('s', -2)))

        ArrayUnit.is_strict = False
        # __add__
        with self.assertWarns(Warning):
            self.assertEqual(
                self.a + self.b, self.a + self.arr2
            )  # a and b got different units, warnings, but a's unit is kept
        # __radd__
        with self.assertWarns(Warning):
            self.assertEqual(self.b + self.a, self.arr1 + self.b)

        ArrayUnit.is_strict = True
        with self.assertRaises(ValueError):
            self.a + self.b
Пример #9
0
 def test_pow(self):
     self.b += 0.1  # avoid division by 0
     self.assertEqual(self.a**4, self.a * self.a * self.a * self.a)
     self.assertEqual(self.a**0.5,
                      ArrayUnit(np.sqrt(self.arr1), Unit('m', 0.5)))
     self.assertEqual(self.b**-5, 1 / (self.b**5))
Пример #10
0
 def test_repr(self):
     self.assertTrue(str(ArrayUnit([])) == "[] ∅")