Beispiel #1
0
def test_operations(datashape):
    a = make_test_array(datashape)
    b = make_test_array(datashape)
    print('a:\n', a)
    print('b:\n', b)
    print('a + b:\n', a + b)
    print('a - b:\n', a - b)
    print('a * b:\n', a * b)
    print('a / b:\n', a / b)
    print('blaze.max(a):\n', blaze.max(a))
    print('blaze.min(a):\n', blaze.min(a))
    print('blaze.product(a):\n', blaze.product(a))
    print('blaze.sum(a):\n', blaze.sum(a))
 def test_min_zerosize(self):
     # Empty min operations should raise, because it has no
     # reduction identity
     self.assertRaises(ValueError, blaze.eval, blaze.min([]))
     self.assertRaises(ValueError, blaze.eval, blaze.min([], keepdims=True))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []]))
     self.assertRaises(ValueError, blaze.eval,
                       blaze.min([[], []], keepdims=True))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []], axis=-1))
     self.assertRaises(ValueError, blaze.eval,
                       blaze.min([[], []], axis=-1, keepdims=True))
     # However, if we're only reducing on a non-empty dimension, it's ok
     self.assertEqual(
         ddesc_as_py(blaze.eval(blaze.min([[], []], axis=0)).ddesc), [])
     self.assertEqual(
         ddesc_as_py(
             blaze.eval(blaze.min([[], []], axis=0, keepdims=True)).ddesc),
         [[]])
 def test_min_zerosize(self):
     # Empty min operations should raise, because it has no
     # reduction identity
     self.assertRaises(ValueError, blaze.eval, blaze.min([]))
     self.assertRaises(ValueError, blaze.eval, blaze.min([], keepdims=True))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []]))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []],
                                                         keepdims=True))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []], axis=-1))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []],
                                                         axis=-1,
                                                         keepdims=True))
     # However, if we're only reducing on a non-empty dimension, it's ok
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[], []],
                                                    axis=0)).ddesc),
                      [])
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[], []],
                                                    axis=0,
                                                    keepdims=True)).ddesc),
                      [[]])
 def test_min(self):
     # Min element of scalar case is the element itself
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min(10)).ddesc), 10)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min(-5.0)).ddesc), -5.0)
     # One-dimensional size one
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([10])).ddesc), 10)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([-5.0])).ddesc), -5.0)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([-5.0],
                                                    axis=0)).ddesc), -5.0)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([10],
                                                    keepdims=True)).ddesc),
                      [10])
     # One dimensional
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([1, 2])).ddesc), 1)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([2, 1])).ddesc), 1)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([0, 1, 0])).ddesc), 0)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([0, 1, 0])).ddesc), 0)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([1, 0, 2])).ddesc), 0)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([2, 1, 0])).ddesc), 0)
     # Two dimensional, test with minimum at all possible positions
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[1, 2, 3],
                                                     [4, 5, 6]])).ddesc), 1)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[2, 1, 3],
                                                     [4, 5, 6]])).ddesc), 1)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[3, 2, 1],
                                                     [4, 5, 6]])).ddesc), 1)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[3, 2, 5],
                                                     [4, 1, 6]])).ddesc), 1)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[3, 2, 5],
                                                     [4, 6, 1]])).ddesc), 1)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[3, 2, 5],
                                                     [1, 6, 4]])).ddesc), 1)
     # Two dimensional, with axis= argument both positive and negative
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[1, 5, 3],
                                                     [4, 2, 6]],
                                                    axis=0)).ddesc),
                      [1, 2, 3])
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[1, 5, 3],
                                                     [4, 2, 6]],
                                                    axis=-2)).ddesc),
                      [1, 2, 3])
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[1, 2, 3],
                                                     [4, 5, 6]],
                                                    axis=1)).ddesc),
                      [1, 4])
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[1, 2, 3],
                                                     [4, 5, 6]],
                                                    axis=-1)).ddesc),
                      [1, 4])
     # Two dimensional, with keepdims=True
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[1, 2, 3],
                                                     [4, 5, 6]],
                                                    keepdims=True)).ddesc),
                      [[1]])
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[1, 2, 3],
                                                     [5, 4, 6]],
                                                    axis=0,
                                                    keepdims=True)).ddesc),
                      [[1, 2, 3]])
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[1, 5, 3],
                                                     [4, 2, 6]],
                                                    axis=1,
                                                    keepdims=True)).ddesc),
                      [[1], [2]])
 def test_min(self):
     # Min element of scalar case is the element itself
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min(10)).ddesc), 10)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min(-5.0)).ddesc), -5.0)
     # One-dimensional size one
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([10])).ddesc), 10)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([-5.0])).ddesc),
                      -5.0)
     self.assertEqual(
         ddesc_as_py(blaze.eval(blaze.min([-5.0], axis=0)).ddesc), -5.0)
     self.assertEqual(
         ddesc_as_py(blaze.eval(blaze.min([10], keepdims=True)).ddesc),
         [10])
     # One dimensional
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([1, 2])).ddesc), 1)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([2, 1])).ddesc), 1)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([0, 1, 0])).ddesc),
                      0)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([0, 1, 0])).ddesc),
                      0)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([1, 0, 2])).ddesc),
                      0)
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([2, 1, 0])).ddesc),
                      0)
     # Two dimensional, test with minimum at all possible positions
     self.assertEqual(
         ddesc_as_py(blaze.eval(blaze.min([[1, 2, 3], [4, 5, 6]])).ddesc),
         1)
     self.assertEqual(
         ddesc_as_py(blaze.eval(blaze.min([[2, 1, 3], [4, 5, 6]])).ddesc),
         1)
     self.assertEqual(
         ddesc_as_py(blaze.eval(blaze.min([[3, 2, 1], [4, 5, 6]])).ddesc),
         1)
     self.assertEqual(
         ddesc_as_py(blaze.eval(blaze.min([[3, 2, 5], [4, 1, 6]])).ddesc),
         1)
     self.assertEqual(
         ddesc_as_py(blaze.eval(blaze.min([[3, 2, 5], [4, 6, 1]])).ddesc),
         1)
     self.assertEqual(
         ddesc_as_py(blaze.eval(blaze.min([[3, 2, 5], [1, 6, 4]])).ddesc),
         1)
     # Two dimensional, with axis= argument both positive and negative
     self.assertEqual(
         ddesc_as_py(
             blaze.eval(blaze.min([[1, 5, 3], [4, 2, 6]], axis=0)).ddesc),
         [1, 2, 3])
     self.assertEqual(
         ddesc_as_py(
             blaze.eval(blaze.min([[1, 5, 3], [4, 2, 6]], axis=-2)).ddesc),
         [1, 2, 3])
     self.assertEqual(
         ddesc_as_py(
             blaze.eval(blaze.min([[1, 2, 3], [4, 5, 6]], axis=1)).ddesc),
         [1, 4])
     self.assertEqual(
         ddesc_as_py(
             blaze.eval(blaze.min([[1, 2, 3], [4, 5, 6]], axis=-1)).ddesc),
         [1, 4])
     # Two dimensional, with keepdims=True
     self.assertEqual(
         ddesc_as_py(
             blaze.eval(blaze.min([[1, 2, 3], [4, 5, 6]],
                                  keepdims=True)).ddesc), [[1]])
     self.assertEqual(
         ddesc_as_py(
             blaze.eval(
                 blaze.min([[1, 2, 3], [5, 4, 6]], axis=0,
                           keepdims=True)).ddesc), [[1, 2, 3]])
     self.assertEqual(
         ddesc_as_py(
             blaze.eval(
                 blaze.min([[1, 5, 3], [4, 2, 6]], axis=1,
                           keepdims=True)).ddesc), [[1], [2]])