def assertBroadcast(self, s1, s2, expected_shape, broadcast_kwargs): # Assert that the operations are symmetric. r1 = BroadcastArray._compute_broadcast_kwargs(s1, s2) r2 = BroadcastArray._compute_broadcast_kwargs(s2, s1) self.assertEqual(r1[0], expected_shape) self.assertEqual(r1[0], r2[0]) self.assertEqual(r1[1], r2[2]) self.assertEqual(r1[2], r2[1]) self.assertEqual(dict(list(zip(["broadcast", "leading_shape"], broadcast_kwargs[0]))), r1[1]) self.assertEqual(dict(list(zip(["broadcast", "leading_shape"], broadcast_kwargs[1]))), r1[2]) actual = np.broadcast(np.empty(s1), np.empty(s2)).shape self.assertEqual(expected_shape, actual)
def test_have_enough_memory(self): # Using np.broadcast results in the actual data needing to be # realised. The code gets around this by using strides = 0. # Pick an array size which isn't realistic to realise in a # full array. a = ConstantArray([int(10 ** i) for i in range(4, 12)]) self.assertEqual( BroadcastArray._compute_broadcast_kwargs(a.shape, a.shape)[0], tuple(int(10 ** i) for i in range(4, 12)) )
def assertBroadcast(self, s1, s2, expected_shape, broadcast_kwargs): # Assert that the operations are symmetric. r1 = BroadcastArray._compute_broadcast_kwargs(s1, s2) r2 = BroadcastArray._compute_broadcast_kwargs(s2, s1) self.assertEqual(r1[0], expected_shape) self.assertEqual(r1[0], r2[0]) self.assertEqual(r1[1], r2[2]) self.assertEqual(r1[2], r2[1]) self.assertEqual(dict(list(zip(['broadcast', 'leading_shape'], broadcast_kwargs[0]))), r1[1]) self.assertEqual(dict(list(zip(['broadcast', 'leading_shape'], broadcast_kwargs[1]))), r1[2]) actual = np.broadcast(np.empty(s1), np.empty(s2)).shape self.assertEqual(expected_shape, actual)
def test_have_enough_memory(self): # Using np.broadcast results in the actual data needing to be # realised. The code gets around this by using strides = 0. # Pick an array size which isn't realistic to realise in a # full array. a = ConstantArray([int(10 ** i) for i in range(4, 12)]) self.assertEqual(BroadcastArray._compute_broadcast_kwargs(a.shape, a.shape)[0], tuple(int(10 ** i) for i in range(4, 12)), )
def test_rule3_value_error(self): msg = "operands could not be broadcast together with shapes " "\(2\,3\) \(1\,2\,4\)" with six.assertRaisesRegex(self, ValueError, msg): BroadcastArray._compute_broadcast_kwargs([2, 3], [1, 2, 4])
def test_rule3_value_error(self): msg = ('operands could not be broadcast together with shapes ' '\(2\,3\) \(1\,2\,4\)') with self.assertRaisesRegexp(ValueError, msg): BroadcastArray._compute_broadcast_kwargs([2, 3], [1, 2, 4])