def test_reductions(self):
     # Check that they compile OK.
     assert_equal(disassemble(
         numexpr("sum(x**2+2, axis=None)", [('x', float)])),
                  [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
                   ('add_fff', 't3', 't3', 'c2[2.0]'),
                   ('sum_ffn', 'r0', 't3', None)])
     assert_equal(disassemble(
         numexpr("sum(x**2+2, axis=1)", [('x', float)])),
                  [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
                   ('add_fff', 't3', 't3', 'c2[2.0]'),
                   ('sum_ffn', 'r0', 't3', 1)])
     assert_equal(disassemble(
         numexpr("prod(x**2+2, axis=2)", [('x', float)])),
                  [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
                   ('add_fff', 't3', 't3', 'c2[2.0]'),
                   ('prod_ffn', 'r0', 't3', 2)])
     # Check that full reductions work.
     x = arange(10.0)
     assert_equal(evaluate("sum(x**2+2,axis=0)"), sum(x**2+2,axis=0))
     assert_equal(evaluate("prod(x**2+2,axis=0)"), prod(x**2+2,axis=0))
     # Check that reductions along an axis work
     y = arange(9.0).reshape(3,3)
     assert_equal(evaluate("sum(y**2, axis=1)"), sum(y**2, axis=1))
     assert_equal(evaluate("sum(y**2, axis=0)"), sum(y**2, axis=0))
     assert_equal(evaluate("sum(y**2, axis=None)"), sum(y**2, axis=None))
     assert_equal(evaluate("prod(y**2, axis=1)"), prod(y**2, axis=1))
     assert_equal(evaluate("prod(y**2, axis=0)"), prod(y**2, axis=0))
     assert_equal(evaluate("prod(y**2, axis=None)"), prod(y**2, axis=None))
     # Check integers
     x = x.astype(int)
     assert_equal(evaluate("sum(x**2+2,axis=0)"), sum(x**2+2,axis=0))
     assert_equal(evaluate("prod(x**2+2,axis=0)"), prod(x**2+2,axis=0))
     # Check longs
     x = x.astype(long)
     assert_equal(evaluate("sum(x**2+2,axis=0)"), sum(x**2+2,axis=0))
     assert_equal(evaluate("prod(x**2+2,axis=0)"), prod(x**2+2,axis=0))
     # Check complex
     x = x + 5j
     assert_equal(evaluate("sum(x**2+2,axis=0)"), sum(x**2+2,axis=0))
     assert_equal(evaluate("prod(x**2+2,axis=0)"), prod(x**2+2,axis=0))
Exemple #2
0
 def test_reductions(self):
     # Check that they compile OK.
     assert_equal(
         disassemble(numexpr("sum(x**2+2, axis=None)", [('x', float)])),
         [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
          ('add_fff', 't3', 't3', 'c2[2.0]'),
          ('sum_ffn', 'r0', 't3', None)])
     assert_equal(
         disassemble(numexpr("sum(x**2+2, axis=1)", [('x', float)])),
         [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
          ('add_fff', 't3', 't3', 'c2[2.0]'), ('sum_ffn', 'r0', 't3', 1)])
     assert_equal(
         disassemble(numexpr("prod(x**2+2, axis=2)", [('x', float)])),
         [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
          ('add_fff', 't3', 't3', 'c2[2.0]'), ('prod_ffn', 'r0', 't3', 2)])
     # Check that full reductions work.
     x = arange(10.0)
     assert_equal(evaluate("sum(x**2+2,axis=0)"), sum(x**2 + 2, axis=0))
     assert_equal(evaluate("prod(x**2+2,axis=0)"), prod(x**2 + 2, axis=0))
     # Check that reductions along an axis work
     y = arange(9.0).reshape(3, 3)
     assert_equal(evaluate("sum(y**2, axis=1)"), sum(y**2, axis=1))
     assert_equal(evaluate("sum(y**2, axis=0)"), sum(y**2, axis=0))
     assert_equal(evaluate("sum(y**2, axis=None)"), sum(y**2, axis=None))
     assert_equal(evaluate("prod(y**2, axis=1)"), prod(y**2, axis=1))
     assert_equal(evaluate("prod(y**2, axis=0)"), prod(y**2, axis=0))
     assert_equal(evaluate("prod(y**2, axis=None)"), prod(y**2, axis=None))
     # Check integers
     x = x.astype(int)
     assert_equal(evaluate("sum(x**2+2,axis=0)"), sum(x**2 + 2, axis=0))
     assert_equal(evaluate("prod(x**2+2,axis=0)"), prod(x**2 + 2, axis=0))
     # Check longs
     x = x.astype(long)
     assert_equal(evaluate("sum(x**2+2,axis=0)"), sum(x**2 + 2, axis=0))
     assert_equal(evaluate("prod(x**2+2,axis=0)"), prod(x**2 + 2, axis=0))
     # Check complex
     x = x + 5j
     assert_equal(evaluate("sum(x**2+2,axis=0)"), sum(x**2 + 2, axis=0))
     assert_equal(evaluate("prod(x**2+2,axis=0)"), prod(x**2 + 2, axis=0))
 def test_r0_reuse(self):
     assert_equal(disassemble(numexpr("x**2+2", [('x', float)])),
                 [('mul_fff', 'r0', 'r1[x]', 'r1[x]'),
                  ('add_fff', 'r0', 'r0', 'c2[2.0]')])
Exemple #4
0
 def test_r0_reuse(self):
     assert_equal(disassemble(numexpr("x**2+2", [('x', float)])),
                  [('mul_fff', 'r0', 'r1[x]', 'r1[x]'),
                   ('add_fff', 'r0', 'r0', 'c2[2.0]')])