def test_asinh(self):
        # base elements
        elements = 30
        comparison = torch.linspace(
            -28, 30, elements, dtype=torch.float64, device=self.device.torch_device
        ).asinh()

        # asinh of float32
        float32_tensor = ht.linspace(-28, 30, elements, dtype=ht.float32)
        float32_asinh = ht.asinh(float32_tensor)
        self.assertIsInstance(float32_asinh, ht.DNDarray)
        self.assertEqual(float32_asinh.dtype, ht.float32)
        self.assertTrue(torch.allclose(float32_asinh.larray.double(), comparison))

        # asinh of float64
        float64_tensor = ht.linspace(-28, 30, elements, dtype=ht.float64)
        float64_asinh = ht.asinh(float64_tensor)
        self.assertIsInstance(float64_asinh, ht.DNDarray)
        self.assertEqual(float64_asinh.dtype, ht.float64)
        self.assertTrue(torch.allclose(float64_asinh.larray.double(), comparison))

        # asinh of ints, automatic conversion to intermediate floats
        int32_tensor = ht.linspace(-28, 30, elements, dtype=ht.int32)
        int32_asinh = ht.asinh(int32_tensor)
        self.assertIsInstance(int32_asinh, ht.DNDarray)
        self.assertEqual(int32_asinh.dtype, ht.float32)
        self.assertTrue(torch.allclose(float32_asinh.larray.double(), comparison))

        # asinh of longs, automatic conversion to intermediate floats
        int64_tensor = ht.linspace(-28, 30, elements, dtype=ht.int64)
        int64_asinh = ht.arcsinh(int64_tensor)
        self.assertIsInstance(int64_asinh, ht.DNDarray)
        self.assertEqual(int64_asinh.dtype, ht.float64)
        self.assertTrue(torch.allclose(int64_asinh.larray.double(), comparison))

        # check exceptions
        with self.assertRaises(TypeError):
            ht.asinh([1, 2, 3])
        with self.assertRaises(TypeError):
            ht.asinh("hello world")
Beispiel #2
0
    def test_linspace(self):
        # simple linear space
        ascending = ht.linspace(-3, 5, device=ht_device)
        self.assertIsInstance(ascending, ht.DNDarray)
        self.assertEqual(ascending.shape, (50,))
        self.assertLessEqual(ascending.lshape[0], 50)
        self.assertEqual(ascending.dtype, ht.float32)
        self.assertEqual(ascending._DNDarray__array.dtype, torch.float32)
        self.assertEqual(ascending.split, None)

        # simple inverse linear space
        descending = ht.linspace(-5, 3, num=100, device=ht_device)
        self.assertIsInstance(descending, ht.DNDarray)
        self.assertEqual(descending.shape, (100,))
        self.assertLessEqual(descending.lshape[0], 100)
        self.assertEqual(descending.dtype, ht.float32)
        self.assertEqual(descending._DNDarray__array.dtype, torch.float32)
        self.assertEqual(descending.split, None)

        # split linear space
        split = ht.linspace(-5, 3, num=70, split=0, device=ht_device)
        self.assertIsInstance(split, ht.DNDarray)
        self.assertEqual(split.shape, (70,))
        self.assertLessEqual(split.lshape[0], 70)
        self.assertEqual(split.dtype, ht.float32)
        self.assertEqual(split._DNDarray__array.dtype, torch.float32)
        self.assertEqual(split.split, 0)

        # with casted type
        casted = ht.linspace(-5, 3, num=70, dtype=ht.uint8, split=0, device=ht_device)
        self.assertIsInstance(casted, ht.DNDarray)
        self.assertEqual(casted.shape, (70,))
        self.assertLessEqual(casted.lshape[0], 70)
        self.assertEqual(casted.dtype, ht.uint8)
        self.assertEqual(casted._DNDarray__array.dtype, torch.uint8)
        self.assertEqual(casted.split, 0)

        # retstep test
        result = ht.linspace(-5, 3, num=70, retstep=True, dtype=ht.uint8, split=0, device=ht_device)
        self.assertIsInstance(result, tuple)
        self.assertEqual(len(result), 2)

        self.assertIsInstance(result[0], ht.DNDarray)
        self.assertEqual(result[0].shape, (70,))
        self.assertLessEqual(result[0].lshape[0], 70)
        self.assertEqual(result[0].dtype, ht.uint8)
        self.assertEqual(result[0]._DNDarray__array.dtype, torch.uint8)
        self.assertEqual(result[0].split, 0)

        self.assertIsInstance(result[1], float)
        self.assertEqual(result[1], 0.11594202898550725)

        # exceptions
        with self.assertRaises(ValueError):
            ht.linspace(-5, 3, split=1, device=ht_device)
        with self.assertRaises(ValueError):
            ht.linspace(-5, 3, num=-1, device=ht_device)
        with self.assertRaises(ValueError):
            ht.linspace(-5, 3, num=0, device=ht_device)
    def test_meshgrid(self):
        # arrays < 2
        self.assertEqual(ht.meshgrid(), [])
        self.assertEqual(ht.meshgrid(ht.array(1)), [ht.array(1)])

        # 2 arrays
        x = ht.arange(4)
        y = ht.arange(3)
        xx, yy = ht.meshgrid(x, y)
        res_xx = ht.array([[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]], dtype=ht.int32, split=None)
        res_yy = ht.array([[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2]], dtype=ht.int32, split=None)

        self.assertEqual(xx.shape, res_xx.shape)
        self.assertEqual(yy.shape, res_yy.shape)
        self.assertEqual(xx.device, res_xx.device)
        self.assertEqual(yy.device, res_yy.device)
        self.assertEqual(xx.dtype, x.dtype)
        self.assertEqual(yy.dtype, y.dtype)
        self.assertTrue(ht.equal(xx, res_xx))
        self.assertTrue(ht.equal(yy, res_yy))

        # different dtype + indexing parameter
        x = ht.linspace(0, 4, 3)
        y = ht.linspace(0, 4, 5)
        xx, yy = ht.meshgrid(x, y, indexing="ij")

        res_xx = ht.array(
            [[0.0, 0.0, 0.0, 0.0, 0.0], [2.0, 2.0, 2.0, 2.0, 2.0], [4.0, 4.0, 4.0, 4.0, 4.0]],
            dtype=ht.float32,
            split=None,
        )
        res_yy = ht.array(
            [[0.0, 1.0, 2.0, 3.0, 4.0], [0.0, 1.0, 2.0, 3.0, 4.0], [0.0, 1.0, 2.0, 3.0, 4.0]],
            dtype=ht.float32,
            split=None,
        )

        self.assertEqual(xx.shape, res_xx.shape)
        self.assertEqual(yy.shape, res_yy.shape)
        self.assertEqual(xx.device, res_xx.device)
        self.assertEqual(yy.device, res_yy.device)
        self.assertEqual(xx.dtype, x.dtype)
        self.assertEqual(yy.dtype, y.dtype)
        self.assertTrue(ht.equal(xx, res_xx))
        self.assertTrue(ht.equal(yy, res_yy))

        # 3 arrays
        z = ht.linspace(0, 1, 3, split=0)

        # split 0
        zz, xx, yy = ht.meshgrid(z, x, y)

        res_zz, res_xx, res_yy = np.meshgrid(z.numpy(), x.numpy(), y.numpy())

        self.assertEqual(xx.split, 0)
        self.assertEqual(yy.split, 0)
        self.assertEqual(zz.split, 0)
        self.assertTrue(ht.equal(xx, ht.array(res_xx)))
        self.assertTrue(ht.equal(yy, ht.array(res_yy)))
        self.assertTrue(ht.equal(zz, ht.array(res_zz)))

        # split 1
        xx, zz, yy = ht.meshgrid(x, z, y)

        res_xx, res_zz, res_yy = np.meshgrid(x.numpy(), z.numpy(), y.numpy())

        self.assertEqual(xx.split, 1)
        self.assertEqual(yy.split, 1)
        self.assertEqual(zz.split, 1)
        self.assertTrue(ht.equal(xx, ht.array(res_xx)))
        self.assertTrue(ht.equal(yy, ht.array(res_yy)))
        self.assertTrue(ht.equal(zz, ht.array(res_zz)))

        # split 2
        xx, yy, zz = ht.meshgrid(x, y, z)

        res_xx, res_yy, res_zz = np.meshgrid(x.numpy(), y.numpy(), z.numpy())

        self.assertEqual(xx.split, 2)
        self.assertEqual(yy.split, 2)
        self.assertEqual(zz.split, 2)
        self.assertTrue(ht.equal(xx, ht.array(res_xx)))
        self.assertTrue(ht.equal(yy, ht.array(res_yy)))
        self.assertTrue(ht.equal(zz, ht.array(res_zz)))

        # exceptions
        with self.assertRaises(ValueError):
            ht.meshgrid(ht.array(1), indexing="abc")

        with self.assertRaises(ValueError):
            ht.meshgrid(ht.array([0, 1], split=0), ht.array([1, 2], split=0))