Example #1
0
def test_where():
    a1 = afnumpy.array([1,2,3])
    b1 = numpy.array(a1)

    a2 = afnumpy.array([0,2,1])
    b2 = numpy.array(a2)
    
    # Test where with input as indices
    iassert(afnumpy.where(a2, a1, a2), numpy.where(b2, b1, b2))
    # Test where with input as indices
    iassert(afnumpy.where(a2), numpy.where(b2))
    # Test where with input as booleans
    iassert(afnumpy.where(a2 < 2, a1, a2), numpy.where(b2 < 2, b1, b2))
    # Test where with input as booleans
    iassert(afnumpy.where(a2 < 2), numpy.where(b2 < 2))

    # And now multidimensional
    a1 = afnumpy.array([[1,2,3],[4,5,6]])
    b1 = numpy.array(a1)

    a2 = afnumpy.array([[0,2,1],[1,0,1]])
    b2 = numpy.array(a2)

    # Test where with input as indices
    iassert(afnumpy.where(a2, a1, a2), numpy.where(b2, b1, b2))
    # Test where with input as indices
    iassert(afnumpy.where(a2), numpy.where(b2))

    # And now multidimensional
    b1 = numpy.random.random((3,3,3)) > 0.5
    a1 = afnumpy.array(b1)

    # Test where with input as indices
    iassert(afnumpy.where(a1), numpy.where(b1))
Example #2
0
def test_ndarray_abs():    
    b = numpy.random.random(3)+numpy.random.random(3)*1.0j
    a = afnumpy.array(b)
    fassert(abs(a), abs(b))
    b = numpy.random.random(3)
    a = afnumpy.array(b)
    fassert(abs(a), abs(b))
Example #3
0
def test_getitem_multi_array():
    # Multidimensional array indexing
    b = numpy.random.random((2,2))
    a = afnumpy.array(b)
    d = numpy.array([0,1])
    c = afnumpy.array(d)
    iassert(a[c,c], b[d,d])
Example #4
0
def test_ndarray_len():
    b = numpy.random.random(3)
    a = afnumpy.array(b)
    assert(len(a) == len(b))
    b = numpy.random.random((3,3))
    a = afnumpy.array(b)
    assert(len(a) == len(b))
Example #5
0
def test_ndarray_T():
    x = numpy.array([[1.,2.],[3.,4.]])
    y = afnumpy.array(x)
    fassert(y.T,x.T)
    x = numpy.array([1.,2.,3.,4.])
    y = afnumpy.array(x)
    fassert(y.T,x.T)
Example #6
0
def test_fftn():    
    b = numpy.random.random((3,3))
    a = afnumpy.array(b)
    fassert(afnumpy.fft.fftn(a), numpy.fft.fftn(b))

    b = numpy.random.random((3,2))
    a = afnumpy.array(b)
    fassert(afnumpy.fft.fftn(a), numpy.fft.fftn(b))

    b = numpy.random.random((5,3,2))
    a = afnumpy.array(b)
    fassert(afnumpy.fft.fftn(a), numpy.fft.fftn(b))

    b = numpy.random.random((5,3,2))+numpy.random.random((5,3,2))*1.0j
    a = afnumpy.array(b)
    fassert(afnumpy.fft.fftn(a), numpy.fft.fftn(b))

    # Test shape argument
    b = numpy.random.random((3,3))
    a = afnumpy.array(b)
    s = (3,3)
    fassert(afnumpy.fft.fftn(a, s), numpy.fft.fftn(b, s))
    s = (3,6)
    fassert(afnumpy.fft.fftn(a, s), numpy.fft.fftn(b, s))
    s = (3,2)
    fassert(afnumpy.fft.fftn(a, s), numpy.fft.fftn(b, s))
Example #7
0
def test_ifftn():    
    # Real to complex inverse fft not implemented in arrayfire
    # b = numpy.random.random((3,3))
    # a = afnumpy.array(b)
    # fassert(afnumpy.fft.ifftn(a), numpy.fft.ifftn(b))

    # b = numpy.random.random((3,2))
    # a = afnumpy.array(b)
    # fassert(afnumpy.fft.ifftn(a), numpy.fft.ifftn(b))

    # b = numpy.random.random((5,3,2))
    # a = afnumpy.array(b)
    # fassert(afnumpy.fft.ifftn(a), numpy.fft.ifftn(b))

    b = numpy.random.random((5,3,2))+numpy.random.random((5,3,2))*1.0j
#    b = numpy.ones((3,3))+numpy.zeros((3,3))*1.0j
    a = afnumpy.array(b)
    fassert(afnumpy.fft.ifftn(a), numpy.fft.ifftn(b))

    # Test shape argument
    b = numpy.random.random((3,3))
    a = afnumpy.array(b)
    s = (3,3)
    fassert(afnumpy.fft.ifftn(a, s), numpy.fft.ifftn(b, s))
    s = (3,6)
    fassert(afnumpy.fft.ifftn(a, s), numpy.fft.ifftn(b, s))
    s = (3,2)
    fassert(afnumpy.fft.ifftn(a, s), numpy.fft.ifftn(b, s))
Example #8
0
def test_floor():
    b = numpy.random.random((2,3))
    a = afnumpy.array(b)
    iassert(afnumpy.floor(a), numpy.floor(b))

    b = numpy.array([1,2,3])
    a = afnumpy.array(b)
    iassert(afnumpy.floor(a), numpy.floor(b))
Example #9
0
def test_ceil():
    b = numpy.random.random((2,3))
    a = afnumpy.array(b)
    iassert(afnumpy.ceil(a), numpy.ceil(b))

    b = numpy.array([1,2,3])
    a = afnumpy.array(b)
    iassert(afnumpy.ceil(a), numpy.ceil(b))
Example #10
0
def test_dot_1D():    
    b = numpy.random.random(3)+numpy.random.random(3)*1.0j
    a = afnumpy.array(b)
    fassert(afnumpy.dot(a,a), numpy.dot(b,b))

    a = numpy.random.random(3)+numpy.random.random(3)*1.0j
    b = numpy.random.random(3)
    fassert(afnumpy.dot(afnumpy.array(a),afnumpy.array(b)), numpy.dot(a,b))
Example #11
0
def test_meshgrid():
    nx, ny = (3, 2)
    x2 = numpy.linspace(0, 1, nx)
    y2 = numpy.linspace(0, 1, ny)
    x1 = afnumpy.array(x2)
    y1 = afnumpy.array(y2)

    iassert(afnumpy.meshgrid(x1, y1), numpy.meshgrid(x2, y2))
Example #12
0
def test_min():    
    b = numpy.random.random(3)+numpy.random.random(3)*1.0j
    a = afnumpy.array(b)
    # Arrayfire uses the magnitude for max while numpy uses
    # the real part as primary key followed by the imaginary part
    # fassert(a.min(), b.min())
    b = numpy.random.random(3)
    a = afnumpy.array(b)
    fassert(a.min(), b.min())
Example #13
0
def test_argsort():
    # Sort does not support 64bit int yet
    x = np.array([3, 1, 2], dtype=float)
    y = af.array(x)
    iassert(af.argsort(y), np.argsort(x))
    x = np.array([[0, 3], [2, 2]], dtype=float)    
    y = af.array(x)
    iassert(af.argsort(y), np.argsort(x))
    iassert(af.argsort(y, axis=1), np.argsort(x, axis=1))
Example #14
0
def test_ndarray_take():
    b = numpy.array([4, 3, 5, 7, 6, 8])
    a = afnumpy.array(b)
    indices = [0, 1, 4]
    iassert(a.take(indices), b.take(indices))
    b = numpy.random.random((2,3))
    a = afnumpy.array(b)
    iassert(a.take([0,1],axis=1), b.take([0,1],axis=1))
    iassert(a.take([0,1]), b.take([0,1]))
Example #15
0
def test_P1():
    # test symmetry operations
    print('\n\n')
    print('Testing solid_syms_Fourier:')
    a = np.arange(4**3).reshape((4,4,4)) + 0J

    sym_ops = crappy_crystals.phasing.symmetry_operations.P1(a.shape, a.shape, a.dtype)
    U = sym_ops.solid_syms_Fourier(a)

    sym_ops_gpu = crappy_crystals.gpu.phasing.symmetry_operations.P1(a.shape, a.shape, a.dtype)
    U_gpu = sym_ops_gpu.solid_syms_Fourier(afnumpy.array(a))

    obj = 0
    print('\n\n')
    print('solid unit:')
    print(a.real)
    print('U == U_gpu ?', np.allclose(np.array(U_gpu), U ))
    print('U')
    print(U.real)
    print('U_gpu')
    print(np.array(U_gpu).real)

    print('\n\n')
    print('Testing solid_syms_real')
    U     = sym_ops.solid_syms_real(a)
    U_gpu = sym_ops_gpu.solid_syms_real(afnumpy.array(a))
    print('U == U_gpu ?', np.allclose(np.array(U_gpu), U ))

    import time 

    shape = (128,128,128)
    a = np.random.random(shape) + 1J * np.random.random(shape)
    b = afnumpy.array(a)

    print('\n\n')
    print('Time test P1:', shape)

    sym_ops = crappy_crystals.phasing.symmetry_operations.P1(a.shape, a.shape, a.dtype)

    sym_ops_gpu = crappy_crystals.gpu.phasing.symmetry_operations.P1(a.shape, a.shape, a.dtype)

    d0 = time.time()
    for i in range(100):
        temp1 = sym_ops.solid_syms_Fourier(a)
    d1 = time.time()
    
    print('cpu : flip a        ', d1-d0, 's')

    d0 = time.time()
    for i in range(100):
        temp = sym_ops_gpu.solid_syms_Fourier(b)
    d1 = time.time()
    
    print('arrayfire : sym_ops ', d1-d0)

    print('arrayfire == np ?:', np.allclose(temp1, np.array(temp)))
Example #16
0
def test_vdot():    
    b = numpy.random.random(3)+numpy.random.random(3)*1.0j
    a = afnumpy.array(b)
    fassert(afnumpy.vdot(a,a), numpy.vdot(b,b))
    b = numpy.random.random((3,3))+numpy.random.random((3,3))*1.0j
    a = afnumpy.array(b)
    fassert(afnumpy.vdot(a,a), numpy.vdot(b,b))
    b = numpy.random.random((3,3,3))+numpy.random.random((3,3,3))*1.0j
    a = afnumpy.array(b)
    fassert(afnumpy.vdot(a,a), numpy.vdot(b,b))
Example #17
0
def test_ndarray_imag():
    x = np.sqrt([1+0j, 0+1j])
    y = af.array(x)
    fassert(y.imag, x.imag)
    y.imag[:] = 0
    x.imag[:] = 0
    fassert(y, x)
    x = np.sqrt([1.0, 0.0])
    y = af.array(x)
    fassert(y.imag, x.imag)
Example #18
0
def test_ndarray_all():    
    b = numpy.random.randint(0,2,3).astype('bool')
    a = afnumpy.array(b)
    iassert(a.all(), b.all())
    iassert(a.all(axis=0), b.all(axis=0))

    b = numpy.random.randint(0,2,(3,2)).astype('bool')
    a = afnumpy.array(b)
    iassert(a.all(), b.all())
    iassert(a.all(axis=0), b.all(axis=0))
    iassert(a.all(keepdims=True), b.all(keepdims=True))
Example #19
0
def test_floor_divide():
    a = afnumpy.random.random((2,3))
    b = numpy.array(a)
    fassert(afnumpy.floor_divide(a,a), numpy.floor_divide(b,b))
    a = afnumpy.array(2)
    b = numpy.array(a)
    ao = afnumpy.array(0)
    bo = numpy.array(0)
    fassert(afnumpy.floor_divide(a,a), numpy.floor_divide(b,b))
    fassert(afnumpy.floor_divide(a,a, out=ao), numpy.floor_divide(b,b, out = bo))
    fassert(ao, bo)
Example #20
0
def test_multiply():
    a = afnumpy.random.random((2,3))
    b = numpy.array(a)
    fassert(afnumpy.multiply(a,a), numpy.multiply(b,b))
    a = afnumpy.array(2)
    ao = afnumpy.array(0)
    b = numpy.array(a)
    bo = numpy.array(0)
    fassert(afnumpy.multiply(a,a), numpy.multiply(b,b))
    fassert(afnumpy.multiply(a,a, out=ao), numpy.multiply(b,b, out = bo))
    fassert(ao, bo)
Example #21
0
def test_sum():    
    b = numpy.random.random(3)
    a = afnumpy.array(b)
    fassert(afnumpy.sum(a), numpy.sum(b))
    fassert(afnumpy.sum(a,axis=0), numpy.sum(b,axis=0))
    fassert(afnumpy.sum(a,keepdims=True), numpy.sum(b,keepdims=True))

    b = numpy.random.random((2,3))
    a = afnumpy.array(b)
    fassert(afnumpy.sum(a), numpy.sum(b))
    fassert(afnumpy.sum(a,axis=0), numpy.sum(b,axis=0))
Example #22
0
def test_subtract():
    a = afnumpy.random.random((2,3))
    b = numpy.array(a)
    fassert(afnumpy.subtract(a,a), numpy.subtract(b,b))
    a = afnumpy.array(2)
    ao = afnumpy.array(0)
    b = numpy.array(a)
    bo = numpy.array(0)
    fassert(afnumpy.subtract(a,a), numpy.subtract(b,b))
    fassert(afnumpy.subtract(a,a, out=ao), numpy.subtract(b,b, out = bo))
    fassert(ao, bo)
Example #23
0
def test_broadcast_arrays():
    # Currently arrayfire is missing support for int64
    x2 = numpy.array([[1,2,3]], dtype=numpy.float32)
    y2 = numpy.array([[1],[2],[3]], dtype=numpy.float32)
    x1 = afnumpy.array(x2)
    y1 = afnumpy.array(y2)
    iassert(afnumpy.broadcast_arrays(x1, y1), numpy.broadcast_arrays(x2, y2))
    x1 = afnumpy.array([2])
    y1 = afnumpy.array(2)
    x2 = numpy.array([2])
    y2 = numpy.array(2)
    iassert(afnumpy.broadcast_arrays(x1, y1), numpy.broadcast_arrays(x2, y2))
Example #24
0
def test_dot_3D():    
    b = numpy.random.random((3,3,3))+numpy.random.random((3,3,3))*1.0j
    a = afnumpy.array(b)
    fassert(afnumpy.dot(a,a), numpy.dot(b,b))

    a = numpy.random.random((3,2,4))+numpy.random.random((3,2,4))*1.0j
    b = numpy.random.random((3,4,1))
    fassert(afnumpy.dot(afnumpy.array(a),afnumpy.array(b)), numpy.dot(a,b))

    b = numpy.random.random((3,3,3))
    a = numpy.random.random((3,3))
    fassert(afnumpy.dot(afnumpy.array(a),afnumpy.array(b)), numpy.dot(a,b))
Example #25
0
def test_transpose():
    b = numpy.random.random((2,3))
    a = afnumpy.array(b)
    iassert(a.transpose(), b.transpose())
    iassert(a.transpose((0,1)), b.transpose((0,1)))
    iassert(a.transpose((1,0)), b.transpose((1,0)))
    b = numpy.random.random((2))
    a = afnumpy.array(b)
    iassert(a.transpose(), b.transpose())
    b = numpy.random.random((2,3,4))
    a = afnumpy.array(b)
    iassert(a.transpose(), b.transpose())
    iassert(a.transpose((2,0,1)), b.transpose((2,0,1)))
Example #26
0
def test_reshape():
    b = numpy.random.random((2,3))
    a = afnumpy.array(b)
    iassert(a.reshape((3,2)), b.reshape((3,2)))
    iassert(a.reshape(6), b.reshape(6))
    iassert(a.reshape((-1,2)), b.reshape((-1,2)))
    assert a.d_array.device_ptr() == a.reshape((-1,2)).d_array.device_ptr()

    b = numpy.random.random((1))
    a = afnumpy.array(b)
    # Some empty shape reshape
    iassert(a.reshape(()), b.reshape(()))
    iassert(a.reshape([]), b.reshape([]))
Example #27
0
def test_ifftshift():
    b = numpy.random.random((3))
    a = afnumpy.array(b)
    fassert(afnumpy.fft.ifftshift(a), numpy.fft.ifftshift(b))
    b = numpy.random.random((3,3))
    a = afnumpy.array(b)
    fassert(afnumpy.fft.ifftshift(a), numpy.fft.ifftshift(b))
    b = numpy.random.random((3,3,3))
    a = afnumpy.array(b)
    fassert(afnumpy.fft.ifftshift(a), numpy.fft.ifftshift(b))
    fassert(afnumpy.fft.ifftshift(a,axes=0), numpy.fft.ifftshift(b,axes=0))
    fassert(afnumpy.fft.ifftshift(a,axes=1), numpy.fft.ifftshift(b,axes=1))
    fassert(afnumpy.fft.ifftshift(a,axes=2), numpy.fft.ifftshift(b,axes=2))
    fassert(afnumpy.fft.ifftshift(a,axes=(1,2)), numpy.fft.ifftshift(b,axes=(1,2)))
Example #28
0
def test_divide():
    a = afnumpy.random.random((2,3))
    b = numpy.array(a)
    fassert(afnumpy.divide(a,a), numpy.divide(b,b))
    a = afnumpy.array(2)
    b = numpy.array(a)
    if sys.version_info >= (3, 0):
        ao = afnumpy.array(0.)
        bo = numpy.array(0.)
    else:
        ao = afnumpy.array(0)
        bo = numpy.array(0)
    fassert(afnumpy.divide(a,a), numpy.divide(b,b))
    fassert(afnumpy.divide(a,a, out=ao), numpy.divide(b,b, out = bo))
    fassert(ao, bo)
Example #29
0
    def __setitem__(self, idx, value):
        try:
            idx, idx_shape, input_shape = indexing.__convert_dim__(self.shape, idx)
        except NotImplementedError:
            # Slow indexing method for not currently implemented fancy indexing
            a = numpy.array(self)
            a.__setitem__(idx, value)
            self[...] = afnumpy.array(a)
            return

        if numpy.prod(idx_shape) == 0:
            # We've selected an empty array
            # No need to do anything
            return
        if any(x is None for x in idx):
            # one of the indices is empty
            return
        idx = tuple(idx)
        if len(idx) == 0:
            idx = tuple([0])
        if(isinstance(value, numbers.Number)):
            pass
        elif(isinstance(value, ndarray)):
            if(value.dtype != self.dtype):
                value.astype(self.dtype)
            value = indexing.__expand_dim__(self.shape, value, idx).d_array
        else:
            raise NotImplementedError('values must be a afnumpy.ndarray')
        self.reshape(input_shape).d_array[idx] = value
        # This is a hack to be able to make it look like arrays with stride[0] > 1 can still be views
        # In practise right now it only applies to ndarray.real and ndarray.imag
        try:
            self._base[self._base_index] = self
        except AttributeError:
            pass
Example #30
0
    def __getitem__(self, args):
        if not isinstance(args, tuple):
            args = (args,)
        try:
            idx, new_shape, input_shape = indexing.__convert_dim__(self.shape, args)
        except NotImplementedError:
            # Slow indexing method for not currently implemented fancy indexing
            return afnumpy.array(numpy.array(self).__getitem__(args))
        if numpy.prod(new_shape) == 0:
            # We're gonna end up with an empty array
            # As we don't yet support empty arrays return an empty numpy array
            return numpy.empty(new_shape, dtype=self.dtype)

        if any(x is None for x in idx):
            # one of the indices is empty
            return ndarray(indexing.__index_shape__(self.shape, idx), dtype=self.dtype)
        idx = tuple(idx)
        if len(idx) == 0:
            idx = tuple([0])
        s = self.reshape(input_shape).d_array[idx]
        shape = pu.af_shape(s)
        array = ndarray(shape, dtype=self.dtype, af_array=s)
        if(shape != new_shape):
            array = array.reshape(new_shape)

        if new_shape == () and Ellipsis not in args:
            # Return the actual scalar
            return numpy.array(array)[()]

        return array