Beispiel #1
0
    def test_reductions(self):
        # Check that they compile OK.
        assert_equal(
            disassemble(NumExpr("sum(x**2+2, axis=None)", [('x', double)])),
            [(b'mul_ddd', b't3', b'r1[x]', b'r1[x]'),
             (b'add_ddd', b't3', b't3', b'c2[2.0]'),
             (b'sum_ddn', b'r0', b't3', None)])
        assert_equal(
            disassemble(NumExpr("sum(x**2+2, axis=1)", [('x', double)])),
            [(b'mul_ddd', b't3', b'r1[x]', b'r1[x]'),
             (b'add_ddd', b't3', b't3', b'c2[2.0]'),
             (b'sum_ddn', b'r0', b't3', 1)])
        assert_equal(
            disassemble(NumExpr("prod(x**2+2, axis=2)", [('x', double)])),
            [(b'mul_ddd', b't3', b'r1[x]', b'r1[x]'),
             (b'add_ddd', b't3', b't3', b'c2[2.0]'),
             (b'prod_ddn', b'r0', b't3', 2)])
        # Check that full reductions work.
        x = zeros(1e5) + .01  # checks issue #41
        assert_allclose(evaluate("sum(x+2,axis=None)"), sum(x + 2, axis=None))
        assert_allclose(evaluate("sum(x+2,axis=0)"), sum(x + 2, axis=0))
        assert_allclose(evaluate("prod(x,axis=0)"), prod(x, axis=0))

        x = arange(10.0)
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2 + 2, axis=0))
        assert_allclose(evaluate("prod(x**2+2,axis=0)"), prod(x**2 + 2,
                                                              axis=0))

        x = arange(100.0)
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2 + 2, axis=0))
        assert_allclose(evaluate("prod(x-1,axis=0)"), prod(x - 1, axis=0))
        x = linspace(0.1, 1.0, 2000)
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2 + 2, axis=0))
        assert_allclose(evaluate("prod(x-1,axis=0)"), prod(x - 1, axis=0))

        # Check that reductions along an axis work
        y = arange(9.0).reshape(3, 3)
        assert_allclose(evaluate("sum(y**2, axis=1)"), sum(y**2, axis=1))
        assert_allclose(evaluate("sum(y**2, axis=0)"), sum(y**2, axis=0))
        assert_allclose(evaluate("sum(y**2, axis=None)"), sum(y**2, axis=None))
        assert_allclose(evaluate("prod(y**2, axis=1)"), prod(y**2, axis=1))
        assert_allclose(evaluate("prod(y**2, axis=0)"), prod(y**2, axis=0))
        assert_allclose(evaluate("prod(y**2, axis=None)"), prod(y**2,
                                                                axis=None))
        # Check integers
        x = arange(10.)
        x = x.astype(int)
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2 + 2, axis=0))
        assert_allclose(evaluate("prod(x**2+2,axis=0)"), prod(x**2 + 2,
                                                              axis=0))
        # Check longs
        x = x.astype(long)
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2 + 2, axis=0))
        assert_allclose(evaluate("prod(x**2+2,axis=0)"), prod(x**2 + 2,
                                                              axis=0))
        # Check complex
        x = x + .1j
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2 + 2, axis=0))
        assert_allclose(evaluate("prod(x-1,axis=0)"), prod(x - 1, axis=0))
 def test_rational_expr(self):
     func = NumExpr((E.a + 2.0 * E.b) / (1 + E.a + 4 * E.b * E.b))
     a = arange(1e6)
     b = arange(1e6) * 0.1
     x = (a + 2 * b) / (1 + a + 4 * b * b)
     y = func(a, b)
     assert_array_almost_equal(x, y)
 def test_broadcasting(self):
     a = arange(100).reshape(10, 10)[::2]
     c = arange(10)
     d = arange(5).reshape(5, 1)
     assert_array_equal(evaluate("a+c"), a + c)
     assert_array_equal(evaluate("a+d"), a + d)
     expr = NumExpr("2.0*a+3.0*c", [('a', double), ('c', double)])
     assert_array_equal(expr(a, c), 2.0 * a + 3.0 * c)
def issue53():
    import numexpr, numpy as np
    from numexpr import NumExpr, E
    func = NumExpr((E.a + 2.0 * E.b) / (1 + E.a + 4 * E.b * E.b))
    a = np.arange(10.0)
    b = np.arange(10.0) * 0.2
    x = (a + 2 * b) / (1 + a + 4 * b * b)
    y = func(a, b)
 def test_simple_expr(self):
     func = NumExpr(E.a)
     x = arange(1e6)
     y = func(x)
     assert_array_equal(x, y)
 def test_simple_expr_small_array(self):
     func = NumExpr(E.a)
     x = arange(100.0)
     y = func(x)
     assert_array_equal(x, y)
 def test_simple(self):
     ex = 2.0 * E.a + 3.0 * E.b * E.c
     sig = [('a', double), ('b', double), ('c', double)]
     func = NumExpr(ex, signature=sig)
     x = func(array([1., 2, 3]), array([4., 5, 6]), array([7., 8, 9]))
     assert_array_equal(x, array([86., 124., 168.]))
 def test_run(self):
     a = arange(100).reshape(10, 10)[::2]
     b = arange(10)
     expr = NumExpr("2*a+3*b", [('a', double), ('b', double)])
     assert_array_equal(expr(a, b), expr.run(a, b))
 def test_all_scalar(self):
     a = 3.
     b = 4.
     assert_allclose(evaluate("a+b"), a + b)
     expr = NumExpr("2*a+3*b", [('a', double), ('b', double)])
     assert_equal(expr(a, b), 2 * a + 3 * b)
 def test_r0_reuse(self):
     assert_equal(disassemble(NumExpr("x * x + 2", [('x', double)])),
                  [(b'mul_ddd', b'r0', b'r1[x]', b'r1[x]'),
                   (b'add_ddd', b'r0', b'r0', b'c2[2.0]')])