예제 #1
0
파일: iterate.py 프로젝트: zivzone/cupy
    def __getitem__(self, ind):
        if ind is Ellipsis:
            return self[:]

        if isinstance(ind, tuple):
            raise IndexError('unsupported iterator index')

        if isinstance(ind, bool):
            raise IndexError('unsupported iterator index')

        if numpy.isscalar(ind):
            ind = int(ind)
            base = self._base
            size = base.size
            indices = []
            for s in base.shape:
                size = size // s
                indices.append(ind // size)
                ind %= size
            return base[tuple(indices)].copy()

        if isinstance(ind, slice):
            base = self._base
            s = internal.complete_slice(ind, base.size)
            s_start = s.start
            s_step = s.step
            size = s.stop - s.start
            if s_step > 0:
                size = (size - 1) // s_step + 1
            else:
                size = (size + 1) // s_step + 1
            return _flatiter_getitem_slice(base, s_start, s_step, size=size)

        raise IndexError('unsupported iterator index')
예제 #2
0
파일: iterate.py 프로젝트: zivzone/cupy
    def __setitem__(self, ind, value):
        if ind is Ellipsis:
            self[:] = value
            return

        if isinstance(ind, tuple):
            raise IndexError('unsupported iterator index')

        if isinstance(ind, bool):
            raise IndexError('unsupported iterator index')

        if numpy.isscalar(ind):
            ind = int(ind)
            base = self._base
            size = base.size
            indices = []
            for s in base.shape:
                size = size // s
                indices.append(ind // size)
                ind %= size
            base[tuple(indices)] = value
            return

        if isinstance(ind, slice):
            base = self._base
            s = internal.complete_slice(ind, base.size)
            s_start = s.start
            s_step = s.step
            size = s.stop - s.start
            if s_step > 0:
                size = (size - 1) // s_step + 1
            else:
                size = (size + 1) // s_step + 1
            value = cupy.asarray(value, dtype=base.dtype)
            _flatiter_setitem_slice(value, s_start, s_step, base, size=size)
            return

        raise IndexError('unsupported iterator index')
예제 #3
0
 def test_invalid_stop_type(self):
     with self.assertRaises(TypeError):
         internal.complete_slice(slice((1, 2), 1, 1), 1)
     with self.assertRaises(TypeError):
         internal.complete_slice(slice((1, 2), 1, -1), 1)
예제 #4
0
 def test_invalid_step_value(self):
     with self.assertRaises(ValueError):
         internal.complete_slice(slice(1, 1, 0), 1)
예제 #5
0
 def test_complete_slice(self):
     assert internal.complete_slice(slice(*self.slice),
                                    10) == slice(*self.expect)
예제 #6
0
 def test_complete_slice(self):
     self.assertEqual(
         internal.complete_slice(slice(*self.slice), 10),
         slice(*self.expect))
예제 #7
0
 def test_invalid_stop_type(self):
     with self.assertRaises(IndexError):
         internal.complete_slice(slice((1, 2), 1, 1), 1)
     with self.assertRaises(IndexError):
         internal.complete_slice(slice((1, 2), 1, -1), 1)
예제 #8
0
 def test_invalid_step_type(self):
     with self.assertRaises(IndexError):
         internal.complete_slice(slice(1, 1, (1, 2)), 1)
예제 #9
0
 def test_invalid_step_value(self):
     with self.assertRaises(ValueError):
         internal.complete_slice(slice(1, 1, 0), 1)
예제 #10
0
 def test_complete_slice(self):
     self.assertEqual(
         internal.complete_slice(slice(*self.slice), 10),
         slice(*self.expect))
예제 #11
0
 def test_invalid_step_type(self):
     with self.assertRaises(TypeError):
         internal.complete_slice(slice(1, 1, (1, 2)), 1)