def test_binary_kerneltree_lifted(self):
        # Create some simple blaze funcs, using Numba
        def _add(a,b):
            return a + b

        def _mul(a,b):
            return a * b
        add = BlazeFunc('add',[('f8(f8,f8)', _add),
                               ('c16(c16,c16)', _add)])
        mul = BlazeFunc('mul', {(double,)*3: _mul,
                                (c128,)*3: _mul})
        # Array data and expression
        af = blaze.array([[1,2,3], [4,5,6]],dshape=double)
        bf = blaze.array([2,3,4],dshape=double)
        cf = add(af,bf)
        df = mul(cf,cf)
        lifted_kernel = df._data.kerneltree.fuse().kernel.lift(1, 'C')
        ubck = lifted_kernel.unbound_single_ckernel

        # Allocate the result, and run the kernel across the arguments
        result = blaze.zeros(df.dshape)
        args = [arg.arr._data for arg in df._data.args]
        ck = ubck.bind(result._data, args)
        execute_expr_single(result._data, args,
                        result._data.dshape.subarray(-2),
                        [a.dshape.subarray(-2) for a in args],
                        ck)
        self.assertEqual(dd_as_py(result._data),
                        [[(a+b) * (a+b) for a, b in izip(a1, b1)]
                                for a1, b1 in izip(
                                    [[1,2,3], [4,5,6]], [[2,3,4]]*2)])
 def test_scalar_to_one_d(self):
     # Set up our data buffers
     src0 = data_descriptor_from_ctypes(ctypes.c_double(1028), writable=False)
     src1 = data_descriptor_from_ctypes(ctypes.c_double(5), writable=False)
     src2 = data_descriptor_from_ctypes(ctypes.c_double(-123), writable=False)
     dst = data_descriptor_from_ctypes((ctypes.c_double * 3)(), writable=True)
     # Do the assignment
     execute_expr_single(dst, [src0, src1, src2],
                     datashape.float64, [datashape.float64]*3, self.muladd)
     self.assertEqual(dd_as_py(dst), [(1028.0 + 1) * 5 - 123] * 3)
    def test_binary_kerneltree(self):
        # Create some simple blaze funcs, using Numba
        def _add(a,b):
            return a + b

        def _mul(a,b):
            return a * b
        add = BlazeFunc('add',[('f8(f8,f8)', _add),
                               ('c16(c16,c16)', _add)])
        mul = BlazeFunc('mul', {(double,)*3: _mul,
                                (c128,)*3: _mul})
        # Array data and expression
        af = blaze.array([[1,2,3], [4,5,6]],dshape=double)
        bf = blaze.array([2,3,4],dshape=double)
        cf = add(af,bf)
        df = mul(cf,cf)
        ubck = df._data.kerneltree.unbound_single_ckernel

        # Allocate the result, and run the kernel across the arguments
        result = blaze.zeros(df.dshape)
        args = [arg.arr._data for arg in df._data.args]
        ck = ubck.bind(result._data, args)
        execute_expr_single(result._data, args,
                        df._data.kerneltree.kernel.dshapes[-1],
                        df._data.kerneltree.kernel.dshapes[:-1], ck)
        self.assertEqual(dd_as_py(result._data),
                        [[(a+b) * (a+b) for a, b in izip(a1, b1)]
                                for a1, b1 in izip(
                                    [[1,2,3], [4,5,6]], [[2,3,4]]*2)])

        # Use blaze.eval to evaluate cf and df into concrete arrays
        cf2 = blaze.eval(cf)
        self.assertEqual(dd_as_py(cf2._data),
                        [[(a+b) for a, b in izip(a1, b1)]
                                for a1, b1 in izip(
                                    [[1,2,3], [4,5,6]], [[2,3,4]]*2)])
        df2 = blaze.eval(df)
        self.assertEqual(dd_as_py(df2._data),
                        [[(a+b) * (a+b) for a, b in izip(a1, b1)]
                                for a1, b1 in izip(
                                    [[1,2,3], [4,5,6]], [[2,3,4]]*2)])
    def test_broadcast_to_two_d(self):
        # Set up our data buffers
        src0 = data_descriptor_from_ctypes(ctypes.c_double(12), writable=False)
        src0_broadcast = [[12] * 3] * 2
        src1_data = (ctypes.c_double * 3)()
        src1_list = [3, 9, 1]
        src1_broadcast = [src1_list] * 2
        for i, val in enumerate(src1_list):
            src1_data[i] = val
        src1 = data_descriptor_from_ctypes(src1_data, writable=False)
        src2_data = (ctypes.c_double * 3 * 2)()
        src2_list = [[5, 3, -2], [-1, 4, 9]]
        src2_broadcast = src2_list
        for j, val_j in enumerate(src2_list):
            for i, val in enumerate(val_j):
                src2_data[j][i] = val
        src2 = data_descriptor_from_ctypes(src2_data, writable=False)
        dst = data_descriptor_from_ctypes((ctypes.c_double * 3 * 2)(), writable=True)

        # Do assignments with the different permuations of the source arguments
        execute_expr_single(dst, [src0, src1, src2],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [[(x + 1) * y + z for x, y, z in izip(*tmp)]
                        for tmp in izip(src0_broadcast, src1_broadcast, src2_broadcast)])
        execute_expr_single(dst, [src0, src2, src1],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [[(x + 1) * y + z for x, z, y in izip(*tmp)]
                        for tmp in izip(src0_broadcast, src1_broadcast, src2_broadcast)])
        execute_expr_single(dst, [src1, src0, src2],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [[(x + 1) * y + z for y, x, z in izip(*tmp)]
                        for tmp in izip(src0_broadcast, src1_broadcast, src2_broadcast)])
        execute_expr_single(dst, [src1, src2, src0],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [[(x + 1) * y + z for z, x, y in izip(*tmp)]
                        for tmp in izip(src0_broadcast, src1_broadcast, src2_broadcast)])
        execute_expr_single(dst, [src2, src0, src1],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [[(x + 1) * y + z for y, z, x in izip(*tmp)]
                        for tmp in izip(src0_broadcast, src1_broadcast, src2_broadcast)])
        execute_expr_single(dst, [src2, src1, src0],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [[(x + 1) * y + z for z, y, x in izip(*tmp)]
                        for tmp in izip(src0_broadcast, src1_broadcast, src2_broadcast)])
    def test_broadcast_to_one_d(self):
        # Set up our data buffers
        src0 = data_descriptor_from_ctypes(ctypes.c_double(12), writable=False)
        src1_data = (ctypes.c_double * 1)()
        src1_data[0] = 3
        src1 = data_descriptor_from_ctypes(src1_data, writable=False)
        src2_data = (ctypes.c_double * 3)()
        src2_list = [5, 3, -2]
        for i, val in enumerate(src2_list):
            src2_data[i] = val
        src2 = data_descriptor_from_ctypes(src2_data, writable=False)
        dst = data_descriptor_from_ctypes((ctypes.c_double * 3)(), writable=True)

        # Do assignments with the different permuations of the source arguments
        execute_expr_single(dst, [src0, src1, src2],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [(12 + 1) * 3 + x for x in [5, 3, -2]])
        execute_expr_single(dst, [src0, src2, src1],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [(12 + 1) * x + 3 for x in [5, 3, -2]])
        execute_expr_single(dst, [src1, src0, src2],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [(3 + 1) * 12 + x for x in [5, 3, -2]])
        execute_expr_single(dst, [src1, src2, src0],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [(3 + 1) * x + 12 for x in [5, 3, -2]])
        execute_expr_single(dst, [src2, src0, src1],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [(x + 1) * 12 + 3 for x in [5, 3, -2]])
        execute_expr_single(dst, [src2, src1, src0],
                        datashape.float64, [datashape.float64]*3, self.muladd)
        self.assertEqual(dd_as_py(dst), [(x + 1) * 3 + 12 for x in [5, 3, -2]])