Пример #1
0
 def test_block(self):
     A = self.data
     B = numpy.array([((1, 0), 10)], dtype=sparse.dtype([4, 2]))
     C = E = numpy.array([((0, 2), 20)], dtype=sparse.dtype([1, 5]))
     D = F = numpy.array([((0, 1), 30)], dtype=sparse.dtype([1, 2]))
     for a, b, c, d, e, f in numpy.ndindex(2, 2, 2, 2, 2, 2):
         with self.subTest(A=a, B=b, C=c, D=d, E=e, F=f):
             datas = [[a and A, b and B], [c and C, d and D],
                      [e and E, f and F]]
             if (a or c or e) and (b or d or f) and (a or b) and (
                     c or d) and (e or f):
                 retval = sparse.block(datas)
                 self.assertEqual(sparse.shape(retval), (6, 7))
                 self.assertEqual(retval.tolist(),
                                  ([((2, 4), 10), ((3, 4), 20), ((2, 3), 1),
                                    ((1, 2), 30), ((0, 1), 40),
                                    ((1, 2), 50), ((2, 3), -1), ((3, 0), 0),
                                    ((2, 0), 60)] if a else []) +
                                  ([((1, 5), 10)] if b else []) +
                                  ([((4, 2), 20)] if c else []) +
                                  ([((4, 6), 30)] if d else []) +
                                  ([((5, 2), 20)] if e else []) +
                                  ([((5, 6), 30)] if f else []))
             else:
                 with self.assertRaises(Exception):
                     sparse.blocks(datas)
Пример #2
0
 def setUp(self):
     self.data = numpy.array([((2, 4), 10), ((3, 4), 20), ((2, 3), 1),
                              ((1, 2), 30), ((0, 1), 40), ((1, 2), 50),
                              ((2, 3), -1), ((3, 0), 0), ((2, 0), 60)],
                             dtype=sparse.dtype([4, 5], int))
     self.full = numpy.array([[0, 40, 0, 0, 0], [0, 0, 80, 0, 0],
                              [60, 0, 0, 0, 10], [0, 0, 0, 0, 20]])
Пример #3
0
 def setUp(self):
     super().setUp()
     self.data = numpy.array([((4, ), 10), ((4, ), 20), ((3, ), 1),
                              ((2, ), 30), ((1, ), 40), ((2, ), 50),
                              ((3, ), -1), ((0, ), 0), ((0, ), 60)],
                             dtype=sparse.dtype([6], int))
     self.full = numpy.array([60, 40, 80, 0, 30, 0])
Пример #4
0
 def test_add_float(self):
   other = numpy.array([
     ((1,), -40),
     ((2,),  .5)], dtype=sparse.dtype((5,), float))
   retval = sparse.add([self.data, other])
   self.assertEqual(retval.dtype, other.dtype)
   self.assertEqual(retval.tolist(),
     [((4,),10), ((4,),20), ((3,),1), ((2,),30), ((1,),40), ((2,),50), ((3,),-1), ((0,),0), ((0,),60), ((1,),-40), ((2,),.5)])
Пример #5
0
 def test_block(self):
   A = self.data
   B = C = numpy.array([
     ((1,), 10)], dtype=sparse.dtype([3]))
   for a, b, c in numpy.ndindex(2,2,2):
     with self.subTest(A=a, B=b, C=c):
       datas = [a and A, b and B, c and C]
       if a and b and c:
         retval = sparse.block(datas)
         self.assertEqual(sparse.shape(retval), (12,))
         self.assertEqual(retval.tolist(),
           [((4,),10), ((4,),20), ((3,),1), ((2,),30), ((1,),40), ((2,),50), ((3,),-1), ((0,),0), ((0,),60), ((7,),10), ((10,),10)])
       else:
         with self.assertRaises(Exception):
           sparse.blocks(datas)
Пример #6
0
 def test_65537_f2(self):
   dtype = sparse.dtype([65537], 'f2')
   self.assertTrue(sparse.issparsedtype(dtype))
   self.assertEqual(dtype.itemsize, 4+2)
Пример #7
0
 def test_65536(self):
   dtype = sparse.dtype([65536])
   self.assertTrue(sparse.issparsedtype(dtype))
   self.assertEqual(dtype.itemsize, 2+8)
Пример #8
0
 def test_256_257_f4(self):
   dtype = sparse.dtype([256, 257], 'f4')
   self.assertTrue(sparse.issparsedtype(dtype))
   self.assertEqual(dtype.itemsize, 3+4)
Пример #9
0
 def test_257_f8(self):
   dtype = sparse.dtype([257], 'f8')
   self.assertTrue(sparse.issparsedtype(dtype))
   self.assertEqual(dtype.itemsize, 2+8)
Пример #10
0
 def test_256(self):
   dtype = sparse.dtype([256])
   self.assertTrue(sparse.issparsedtype(dtype))
   self.assertEqual(dtype.itemsize, 1+8)