def initialization(self, Ic_image_up):
        '''
        
        initialization initializes the sample's transmittance function and the speckle field.
        
        '''

        pad = lambda x, pad_y, pad_x: af_pad(x, pad_y, pad_x, 0)
        F = lambda x: af.signal.fft2(x)
        iF = lambda x: af.signal.ifft2(x)

        # Initialization of object and pattern
        self.obj = af.constant(1, self.Nc, self.Mc, dtype=af.Dtype.c64)
        self.field_p_whole = af.constant(1,
                                         self.Npp,
                                         self.Mpp,
                                         dtype=af.Dtype.c64)

        for i in range(0, self.Nimg):
            I_temp = af.interop.np_to_af_array((Ic_image_up[0, i])**(1 / 2))
            field_p_shift_back = af.arith.maxof(0, af.arith.real(iF(F(pad(pad(I_temp, self.N_bound_pad, self.N_bound_pad),\
                                                                          self.yshift_max, self.xshift_max))* \
                                                                    af.arith.exp( -1j*2*np.pi*self.ps*\
                                                                                 (self.fxxp * self.xshift[0,i] +\
                                                                                  self.fyyp * self.yshift[0,i])))))
            self.field_p_whole = (self.field_p_whole +
                                  field_p_shift_back / self.Nimg).copy()
    def _train(self, X: af.Array, Y: af.Array, alpha: float,
               lambda_param: float, penalty: str, maxerr: float,
               maxiter: int) -> af.Array:
        # Add bias feature
        bias = af.constant(1, X.dims()[0], 1)
        X_biased = af.join(1, bias, X)

        # Initialize parameters to 0
        Weights = af.constant(0, X_biased.dims()[1], Y.dims()[1])

        for i in range(maxiter):
            # Get the cost and gradient
            J, dJ = self._cost(Weights, X_biased, Y, lambda_param, penalty)
            err = af.max(af.abs(J))
            if err < maxerr:
                Weights = Weights[1:]  # Remove bias weights
                return Weights

            # Update the weights via gradient descent
            Weights = Weights - alpha * dJ

        # Remove bias weights
        Weights = Weights[1:]

        return Weights
Beispiel #3
0
def calculate_p_center(p1_start, p2_start, p3_start,
                       N_p1, N_p2, N_p3,
                       dp1, dp2, dp3, 
                      ):
    """
    Initializes the cannonical variables p1, p2 and p3 using a centered
    formulation.
    """
    p1_center = af.constant(0, N_p1 * N_p2 * N_p3, len(p1_start), dtype = af.Dtype.f64)
    p2_center = af.constant(0, N_p1 * N_p2 * N_p3, len(p2_start), dtype = af.Dtype.f64)
    p3_center = af.constant(0, N_p1 * N_p2 * N_p3, len(p3_start), dtype = af.Dtype.f64)

    # Assigning for each species:
    for i in range(len(p1_start)):

        p1 = p1_start[i] + (0.5 + np.arange(N_p1)) * dp1[i]
        p2 = p2_start[i] + (0.5 + np.arange(N_p2)) * dp2[i]
        p3 = p3_start[i] + (0.5 + np.arange(N_p3)) * dp3[i]
        
        p2_center[:, i] = af.flat(af.to_array(np.meshgrid(p2, p1, p3)[0]))
        p1_center[:, i] = af.flat(af.to_array(np.meshgrid(p2, p1, p3)[1]))
        p3_center[:, i] = af.flat(af.to_array(np.meshgrid(p2, p1, p3)[2]))

    af.eval(p1_center, p2_center, p3_center)
    return (p1_center, p2_center, p3_center)
Beispiel #4
0
def monte_carlo_options(N,
                        K,
                        t,
                        vol,
                        r,
                        strike,
                        steps,
                        use_barrier=True,
                        B=None,
                        ty=af.Dtype.f32):
    payoff = af.constant(0, N, 1, dtype=ty)

    dt = t / float(steps - 1)
    s = af.constant(strike, N, 1, dtype=ty)

    randmat = af.randn(N, steps - 1, dtype=ty)
    randmat = af.exp((r - (vol * vol * 0.5)) * dt +
                     vol * math.sqrt(dt) * randmat)

    S = af.product(af.join(1, s, randmat), 1)

    if (use_barrier):
        S = S * af.all_true(S < B, 1)

    payoff = af.maxof(0, S - K)
    return af.mean(payoff) * math.exp(-r * t)
Beispiel #5
0
def test_surface_term():
    '''
    A test function to test the surface_term function in the wave_equation
    module using analytical Lax-Friedrichs flux.
    '''
    threshold = 1e-13
    params.c = 1

    params.N_LGL = 8
    params.N_quad = 10
    params.N_Elements = 10
    wave = 'gaussian'

    gv = global_variables.advection_variables(params.N_LGL, params.N_quad,\
                                          params.x_nodes, params.N_Elements,\
                                          params.c, params.total_time, wave,\
                                          params.c_x, params.c_y, params.courant,\
                                          params.mesh_file, params.total_time_2d)

    analytical_f_i = (gv.u_init[-1, :])
    analytical_f_i_minus1 = (af.shift(gv.u_init[-1, :], 0, 1))

    L_p_1 = af.constant(0, params.N_LGL, dtype=af.Dtype.f64)
    L_p_1[params.N_LGL - 1] = 1

    L_p_minus1 = af.constant(0, params.N_LGL, dtype=af.Dtype.f64)
    L_p_minus1[0] = 1

    analytical_surface_term = af.blas.matmul(L_p_1, analytical_f_i)\
        - af.blas.matmul(L_p_minus1, analytical_f_i_minus1)

    numerical_surface_term = (wave_equation.surface_term(gv.u_init[:, :], gv))
    assert af.max(af.abs(analytical_surface_term - numerical_surface_term)) \
        < threshold
    return analytical_surface_term
Beispiel #6
0
 def setUpdateParams(self, flag_update = None, pupil_step_size = None, update_method = None, global_update = False, \
                     measurement_num = None, \
                     pupil = None, pupil_support = None):
     """Modify update parameters for pupil"""
     if pupil is not None: self.setPupil(pupil=pupil)
     if pupil_support is not None: self.setPupilSupport(pupil_support)
     self.flag_update = flag_update if flag_update is not None else self.flag_update
     if self.flag_update:
         self.update_method = update_method if update_method is not None else self.update_method
         self.pupil_step_size = pupil_step_size if pupil_step_size is not None else self.pupil_step_size
         self.pupil_gradient_amp = af.constant(0.0,
                                               self.shape[0],
                                               self.shape[1],
                                               dtype=af_complex_datatype)
         self.pupil_gradient_phase = af.constant(0.0,
                                                 self.shape[0],
                                                 self.shape[1],
                                                 dtype=af_complex_datatype)
         self.measurement_num = measurement_num if global_update else 1
         self.measure_count = 0
         if self.update_method == "GaussNewton":
             self.approx_hessian = af.constant(0.0,
                                               self.shape[0],
                                               self.shape[1],
                                               dtype=af_complex_datatype)
Beispiel #7
0
 def _meanObject(self, obj, adjoint=False):
     """
     function to bin the object by factor of slice_binning_factor
     """
     if self.slice_binning_factor == 1:
         return obj
     assert self.shape[2] >= 1
     if adjoint:
         obj_out = af.constant(0.0,
                               self._shape_full[0],
                               self._shape_full[1],
                               self._shape_full[2],
                               dtype=af_complex_datatype)
         for idx in range((self.shape[2] - 1) * self.slice_binning_factor,
                          -1, -self.slice_binning_factor):
             idx_slice = slice(
                 idx,
                 np.min([obj_out.shape[2],
                         idx + self.slice_binning_factor]))
             obj_out[:, :, idx_slice] = af.broadcast(
                 self.assign_broadcast, obj_out[:, :, idx_slice],
                 obj[:, :, idx // self.slice_binning_factor])
     else:
         obj_out = af.constant(0.0,
                               self.shape[0],
                               self.shape[1],
                               self.shape[2],
                               dtype=af_complex_datatype)
         for idx in range(0, obj.shape[2], self.slice_binning_factor):
             idx_slice = slice(
                 idx,
                 np.min([obj.shape[2], idx + self.slice_binning_factor]))
             obj_out[:, :, idx // self.slice_binning_factor] = af.mean(
                 obj[:, :, idx_slice], dim=2)
     return obj_out
def af_pad(image, NN, MM, val):
    ''' 
    
    af_pad is a short-cut function to constant-pad a 2D array with arrayfire arrays
    
    Inputs:
        image     : a 2D array needed to be padded
        NN        : number of pixels padded in each side of dimension 0
        MM        : number of pixels padded in each side of dimension 1
        
    Outputs:
        image_pad : the padded 2D array
    
    '''

    N, M = image.shape
    Np = N + 2 * NN
    Mp = M + 2 * MM
    if image.dtype() == af.Dtype.f32 or image.dtype() == af.Dtype.f64:
        image_pad = af.constant(val, Np, Mp)
    else:
        image_pad = af.constant(val * (1 + 1j * 0), Np, Mp)
    image_pad[NN:NN + N, MM:MM + M] = image

    return image_pad
def test_surface_term():
    '''
    A test function to test the surface_term function in the wave_equation
    module using analytical Lax-Friedrichs flux.
    '''
    threshold = 1e-13
    params.c = 1

    change_parameters(8, 10, 8, 'gaussian')

    analytical_f_i = (params.u[-1, :, 0])
    analytical_f_i_minus1 = (af.shift(params.u[-1, :, 0], 0, 1))

    L_p_1 = af.constant(0, params.N_LGL, dtype=af.Dtype.f64)
    L_p_1[params.N_LGL - 1] = 1

    L_p_minus1 = af.constant(0, params.N_LGL, dtype=af.Dtype.f64)
    L_p_minus1[0] = 1

    analytical_surface_term = af.blas.matmul(L_p_1, analytical_f_i)\
        - af.blas.matmul(L_p_minus1, analytical_f_i_minus1)

    numerical_surface_term = (wave_equation.surface_term(params.u[:, :, 0]))
    assert af.max(af.abs(analytical_surface_term - numerical_surface_term)) \
        < threshold
    return analytical_surface_term
def test_dirichlet():

    obj = test('dirichlet', 'dirichlet')

    obj._A_q1, obj._A_q2 = af.Array([100]), af.Array([100])

    obj._A_q1 = af.tile(obj._A_q1, 1, 1, obj.q1_center.shape[2])
    obj._A_q2 = af.tile(obj._A_q2, 1, 1, obj.q1_center.shape[2])

    obj.dt = 0.001

    obj.f = af.constant(0,
                        obj.q1_center.shape[0],
                        obj.q1_center.shape[1],
                        obj.q1_center.shape[2],
                        dtype=af.Dtype.f64)

    apply_bcs_f(obj)

    expected = af.constant(0,
                           obj.q1_center.shape[0],
                           obj.q1_center.shape[1],
                           obj.q1_center.shape[2],
                           dtype=af.Dtype.f64)

    N_g = obj.N_ghost

    # Only ingoing characteristics should be affected:
    expected[:N_g] = af.select(obj.q1_center < obj.q1_start, 1, expected)[:N_g]
    expected[:, :N_g] = af.select(obj.q2_center < obj.q2_start, 2,
                                  expected)[:, :N_g]

    assert (af.max(af.abs(obj.f[:, N_g:-N_g] - expected[:, N_g:-N_g])) < 5e-14)
    assert (af.max(af.abs(obj.f[N_g:-N_g, :] - expected[N_g:-N_g, :])) < 5e-14)
Beispiel #11
0
def simple_data(verbose=False):
    display_func = _util.display_func(verbose)
    print_func   = _util.print_func(verbose)

    display_func(af.constant(100, 3,3, dtype=af.Dtype.f32))
    display_func(af.constant(25, 3,3, dtype=af.Dtype.c32))
    display_func(af.constant(2**50, 3,3, dtype=af.Dtype.s64))
    display_func(af.constant(2+3j, 3,3))
    display_func(af.constant(3+5j, 3,3, dtype=af.Dtype.c32))

    display_func(af.range(3, 3))
    display_func(af.iota(3, 3, tile_dims=(2,2)))

    display_func(af.identity(3, 3, 1, 2, af.Dtype.b8))
    display_func(af.identity(3, 3, dtype=af.Dtype.c32))

    a = af.randu(3, 4)
    b = af.diag(a, extract=True)
    c = af.diag(a, 1, extract=True)

    display_func(a)
    display_func(b)
    display_func(c)

    display_func(af.diag(b, extract = False))
    display_func(af.diag(c, 1, extract = False))

    display_func(af.join(0, a, a))
    display_func(af.join(1, a, a, a))

    display_func(af.tile(a, 2, 2))


    display_func(af.reorder(a, 1, 0))

    display_func(af.shift(a, -1, 1))

    display_func(af.moddims(a, 6, 2))

    display_func(af.flat(a))

    display_func(af.flip(a, 0))
    display_func(af.flip(a, 1))

    display_func(af.lower(a, False))
    display_func(af.lower(a, True))

    display_func(af.upper(a, False))
    display_func(af.upper(a, True))

    a = af.randu(5,5)
    display_func(af.transpose(a))
    af.transpose_inplace(a)
    display_func(a)

    display_func(af.select(a > 0.3, a, -0.3))

    af.replace(a, a > 0.3, -0.3)
    display_func(a)
Beispiel #12
0
def simple_data(verbose=False):
    display_func = _util.display_func(verbose)

    display_func(af.constant(100, 3, 3, dtype=af.Dtype.f32))
    display_func(af.constant(25, 3, 3, dtype=af.Dtype.c32))
    display_func(af.constant(2**50, 3, 3, dtype=af.Dtype.s64))
    display_func(af.constant(2+3j, 3, 3))
    display_func(af.constant(3+5j, 3, 3, dtype=af.Dtype.c32))

    display_func(af.range(3, 3))
    display_func(af.iota(3, 3, tile_dims=(2, 2)))

    display_func(af.identity(3, 3, 1, 2, af.Dtype.b8))
    display_func(af.identity(3, 3, dtype=af.Dtype.c32))

    a = af.randu(3, 4)
    b = af.diag(a, extract=True)
    c = af.diag(a, 1, extract=True)

    display_func(a)
    display_func(b)
    display_func(c)

    display_func(af.diag(b, extract=False))
    display_func(af.diag(c, 1, extract=False))

    display_func(af.join(0, a, a))
    display_func(af.join(1, a, a, a))

    display_func(af.tile(a, 2, 2))

    display_func(af.reorder(a, 1, 0))

    display_func(af.shift(a, -1, 1))

    display_func(af.moddims(a, 6, 2))

    display_func(af.flat(a))

    display_func(af.flip(a, 0))
    display_func(af.flip(a, 1))

    display_func(af.lower(a, False))
    display_func(af.lower(a, True))

    display_func(af.upper(a, False))
    display_func(af.upper(a, True))

    a = af.randu(5, 5)
    display_func(af.transpose(a))
    af.transpose_inplace(a)
    display_func(a)

    display_func(af.select(a > 0.3, a, -0.3))

    af.replace(a, a > 0.3, -0.3)
    display_func(a)

    display_func(af.pad(a, (1, 1, 0, 0), (2, 2, 0, 0)))
Beispiel #13
0
def simple_device(verbose=False):
    display_func = _util.display_func(verbose)
    print_func   = _util.print_func(verbose)
    print_func(af.device_info())
    print_func(af.get_device_count())
    print_func(af.is_dbl_supported())
    af.sync()

    curr_dev = af.get_device()
    print_func(curr_dev)
    for k in range(af.get_device_count()):
        af.set_device(k)
        dev = af.get_device()
        assert(k == dev)

        print_func(af.is_dbl_supported(k))

        af.device_gc()

        mem_info_old = af.device_mem_info()

        a = af.randu(100, 100)
        af.sync(dev)
        mem_info = af.device_mem_info()
        assert(mem_info['alloc']['buffers'] == 1 + mem_info_old['alloc']['buffers'])
        assert(mem_info[ 'lock']['buffers'] == 1 + mem_info_old[ 'lock']['buffers'])

    af.set_device(curr_dev)

    a = af.randu(10,10)
    display_func(a)
    dev_ptr = af.get_device_ptr(a)
    print_func(dev_ptr)
    b = af.Array(src=dev_ptr, dims=a.dims(), dtype=a.dtype(), is_device=True)
    display_func(b)

    c = af.randu(10,10)
    af.lock_array(c)
    af.unlock_array(c)

    a = af.constant(1, 3, 3)
    b = af.constant(2, 3, 3)
    af.eval(a)
    af.eval(b)
    print_func(a)
    print_func(b)
    c = a + b
    d = a - b
    af.eval(c, d)
    print_func(c)
    print_func(d)

    print_func(af.set_manual_eval_flag(True))
    assert(af.get_manual_eval_flag() == True)
    print_func(af.set_manual_eval_flag(False))
    assert(af.get_manual_eval_flag() == False)

    display_func(af.is_locked_array(a))
Beispiel #14
0
def relu(image):
    #TODO: Remove assumption of image.dims()[3] == 1
    if len(image.dims()) is 1:
        return af.maxof(image, af.constant(0, image.dims()[0]))
    elif len(image.dims()) is 3:
        d0, d1, d2 =  image.dims()
        return af.maxof(image, af.constant(0, d0, d1, d2))
    print "error, bad val num dims"
    return
Beispiel #15
0
def Get_Linear_Equation_Gpu(x,
                            weight,
                            bin_data_num,
                            bin_data_y,
                            r,
                            dtype='f4'):
    n, p = x.shape
    d = p - 1
    if dtype is 'f4':
        dtype = af.Dtype.f32
    elif dtype is 'f8':
        dtype = af.Dtype.f64
    xw = af.constant(0, n, p, dtype=dtype)
    for ii in af.ParallelRange(p):
        xw[:, ii] = x[:, ii] * weight
    s = af.constant(0, p, p, np.prod(bin_data_num.shape), dtype=dtype)
    t = af.constant(0, p, np.prod(bin_data_num.shape), dtype=dtype)
    ker_d = np.ones(4, dtype='int')
    ker_d[:d] = 2 * r + 1
    if d is 4:
        for i in range(p):
            for j in range(i, p):
                if i is 0:
                    kernel = af.moddims(xw[:, j], ker_d[0], ker_d[1], ker_d[2],
                                        ker_d[3])
                    t[j] = af.flat(
                        af.reorder(Convolve4(bin_data_y, kernel), 3, 2, 1, 0))
                else:
                    kernel = af.moddims(xw[:, i] * x[:, j], ker_d[0], ker_d[1],
                                        ker_d[2], ker_d[3])
                s[i, j] = af.flat(
                    af.reorder(Convolve4(bin_data_num, kernel), 3, 2, 1, 0))
                s[j, i] = s[i, j]
    elif d < 4:
        for i in range(p):
            for j in range(i, p):
                if i is 0:
                    kernel = af.moddims(xw[:, j], ker_d[0], ker_d[1], ker_d[2],
                                        ker_d[3])
                    if kernel.elements() is 1:
                        t[j] = af.flat((bin_data_y * kernel.to_list()[0]).T)
                    else:
                        t[j] = af.flat(af.fft_convolve(bin_data_y, kernel).T)
                else:
                    kernel = af.moddims(xw[:, i] * x[:, j], ker_d[0], ker_d[1],
                                        ker_d[2], ker_d[3])
                if kernel.elements() is 1:
                    s[i, j] = af.flat((bin_data_num * kernel.to_list()[0]).T)
                else:
                    s[i, j] = af.flat(af.fft_convolve(bin_data_num, kernel).T)
                s[j, i] = s[i, j]
    for i in range(1, p):
        s[i, i] += 1e-12
    return ([
        np.array(s).reshape(p**2, -1).T.reshape(-1, p, p),
        np.array(af.flat(t))
    ])
Beispiel #16
0
def arrayfire_perceptron_demo(dataset, num_classes=None):
    # Determine number of classes if not provided
    if num_classes is None:
        num_classes = np.amax(dataset[1] + 1)

    # Convert numpy array to af array (and convert labels/targets from ints to
    # one-hot encodings)
    train_feats = af.from_ndarray(dataset[0])
    train_targets = af.from_ndarray(ints_to_onehots(dataset[1], num_classes))
    test_feats = af.from_ndarray(dataset[2])
    test_targets = af.from_ndarray(ints_to_onehots(dataset[3], num_classes))

    num_train = train_feats.dims()[0]
    num_test = test_feats.dims()[0]

    # Add bias
    train_bias = af.constant(1, num_train, 1)
    test_bias = af.constant(1, num_test, 1)
    train_feats = af.join(1, train_bias, train_feats)
    test_feats = af.join(1, test_bias, test_feats)

    print('arrayfire perceptron classifier implementation')

    clf = AfPerceptron(alpha=0.1, maxerr=0.01, maxiter=1000, verbose=False)
    # Initial run to avoid overhead in training
    clf.train(train_feats, train_targets)
    clf.init_weights(train_feats, train_targets)

    # Benchmark training
    t0 = time.time()
    clf.train(train_feats, train_targets)
    clf.eval()
    t1 = time.time()
    dt_train = t1 - t0
    print('Training time: {0:4.4f} s'.format(dt_train))

    # Benchmark prediction
    iters = 100
    test_outputs = None
    t0 = time.time()
    for i in range(iters):
        test_outputs = clf.predict_proba(test_feats)
        af.eval(test_outputs)
        af.sync()
    t1 = time.time()
    dt = t1 - t0
    print('Prediction time: {0:4.4f} s'.format(dt / iters))

    print('Accuracy (test data): {0:2.2f}'.format(
        accuracy(test_outputs, test_targets)))

    # print('Accuracy on training data: {0:2.2f}'.format(accuracy(train_outputs, train_targets)))
    # print('Accuracy on testing data: {0:2.2f}'.format(accuracy(test_outputs, test_targets)))
    # print('Maximum error on testing data: {0:2.2f}'.format(abserr(test_outputs, test_targets)))

    return clf, dt_train
def forward(nn_np, data):
    """
    Given a dictionary representing a feed forward neural net and an input data matrix compute the network's output and store it within the dictionary
    :param nn: neural network dictionary
    :param data: a numpy n by m matrix where m in the number of input units in nn
    :return: the output layer activations
    """
    nn = nn_np
    nn['activations'] = []
    
    #prct = []
    #prct.append(data[0][0])
    #prct.append(data[1][0])
    
    #src = data.astype(float)
    
    nsrc = data#af.Array(prct)
    
    nn['activations'].append(nsrc)
    #(af.Array(src.ctypes.data, src.shape, src.dtype.char)) 
    
    nn['zs'] = []
    for w, s, b in map(None, nn['weights'], nn['nonlin'], nn['biases']):
	
	#:wq
	#print af.transpose(nn['activations'][-1])



	#print(type(nn['activations'][0]))
	#print(nn['activations'][0])
	
	
	
        z = af.matmul(w, nn['activations'][-1])
        
	
	
	#for i in range(w.shape[1]-1):
	#	newB = af.join(1,newB,b)
	
	


	p = z.T + b
	
	nn['zs'].append(p.T)
	if len(p.shape) == 2:
		maxVal = af.constant(1e-5,p.shape[0],p.shape[1]).T
	else:
		maxVal = af.constant(1e-5,p.shape[0]).T
	q = af.maxof(p.T,1e-5)#p(i) = 1e-5
	
        nn['activations'].append(q)
	
    return nn['activations'][-1]
Beispiel #18
0
def af_pad(image, NN, MM, val):
    N, M = image.shape
    Np = N + 2 * NN
    Mp = M + 2 * MM
    if image.dtype() == af.Dtype.f32 or image.dtype() == af.Dtype.f64:
        image_pad = af.constant(val, Np, Mp)
    else:
        image_pad = af.constant(val * (1 + 1j * 0), Np, Mp)
    image_pad[NN:NN + N, MM:MM + M] = image

    return image_pad
    def _dense_fit(self, X, strategy, missing_values, fill_value):
        """Fit the transformer on dense data."""
        mask = _get_mask(X, missing_values)
        X_np = X.to_ndarray()
        masked_X = ma.masked_array(X_np, mask=np.isnan(X_np))  # FIXME

        # Mean
        if strategy == "mean":
            mean_masked = ma.mean(masked_X, axis=0)
            # Avoid the warning "Warning: converting a masked element to nan."
            mean = ma.getdata(mean_masked)
            mean[ma.getmask(mean_masked)] = np.nan

            return mean

        # Median
        elif strategy == "median":
            median_masked = ma.median(masked_X, axis=0)
            # Avoid the warning "Warning: converting a masked element to nan."
            median = ma.getdata(median_masked)
            median[ma.getmaskarray(median_masked)] = np.nan

            return median

        # Most frequent
        elif strategy == "most_frequent":
            # Avoid use of scipy.stats.mstats.mode due to the required
            # additional overhead and slow benchmarking performance.
            # See Issue 14325 and PR 14399 for full discussion.

            # To be able access the elements by columns

            X = X.T
            mask = mask.T

            npdtype = typemap(X.dtype())
            if npdtype.kind == "O":
                most_frequent = af.constant(0, X.shape[0], dtype=object)
            else:
                most_frequent = af.constant(0, X.shape[0])

            for i, (row, row_mask) in enumerate(zip(X[:], mask[:])):
                row_mask = row_mask.logical_not()
                row = row[row_mask].to_ndarray()
                #most_frequent[i] = _most_frequent(row, np.nan, 0)
                most_frequent[i] = _most_frequent(row, np.nan, 0)

            return most_frequent

        # Constant
        elif strategy == "constant":
            # for constant strategy, self.statistcs_ is used to store
            # fill_value in each column
            return af.constant(fill_value, X.shape[1], dtype=X.dtype())
Beispiel #20
0
def conv(weights, biases, image, wx, wy, sx = 1, sy = 1, px = 0, py = 0, groups = 1):
    image = __pad(image, px, py)

    batch     = util.num_input(image)
    n_filters = util.num_filters(weights)

    n_channel = util.num_channels(weights)

    w_i = image.dims()[0]
    h_i = image.dims()[1]
    w_o = (w_i - wx) / sx + 1
    h_o = (h_i - wy) / sy + 1

    tiles     = af.unwrap(image, wx, wy, sx, sy)

    weights   = af.moddims(weights, wx*wy, n_channel, n_filters)
    out       = af.constant(0, batch, n_filters, w_o, h_o)

    if groups > 1:

        out = af.constant(0, w_o, h_o, n_filters, batch)

        split_in = util.num_channels(image) / groups
        s_i = split_in
        split_out = n_filters / groups
        s_o = split_out

        for i in xrange(groups):
            weights_slice = af.moddims(weights[:,:,i*s_o:(i+1)*s_o],
                                       wx, wy, n_channel, split_out)
            biases_slice = biases[i*s_o:(i+1)*s_o]
            image_slice  = image[:,:,i*s_i:(i+1)*s_i]
            out[:,:,i*s_o:(i+1)*s_o] = conv(weights_slice,
                                        biases_slice,
                                        image_slice,
                                        wx, wy, sx, sy, 0, 0, 1)
            # out[:,i*s_o:(i+1)*s_o] = conv(weights_slice,
            #                               biases_slice,
            #                               image_slice,
            #                               wx, wy, sx, sy, 0, 0, 1)
        return out

    #TODO: Speedup this section
    for f in xrange(n_filters):
        for d in xrange(n_channel):
            tile_d = af.reorder(tiles[:,:,d],1,0)
            weight_d = weights[:,d,f]
            out[0,f] += af.moddims(af.matmul(tile_d, weight_d), 1,1, w_o, h_o)
        out[0,f] += biases[f].to_list()[0]

    return af.reorder(out, 2, 3, 1, 0)
def sparse_matrix(n, Lambda, projection='false'):
    if projection == 'false':
        em1 = af.constant(-1, n**2)
        em1[n - 1:n**2:n] = 0

        ep1 = af.constant(-1, n**2)
        ep1[n:n**2:n] = 0

        sp_matrix = af.identity(n**2, n**2,
                                dtype=af.Dtype.f64) * (1 - 4 * Lambda)

        sp_matrix[n:, :-n] += Lambda * af.identity(n**2 - n, n**2 - n)
        sp_matrix[:-n, n:] += Lambda * af.identity(n**2 - n, n**2 - n)

        for i in range(n**2):
            for j in range(n**2):

                if (i == j + 1):
                    sp_matrix[i, j] -= Lambda * em1[j]

                if (i == j - 1):
                    sp_matrix[i, j] -= Lambda * ep1[j]

    if projection == 'true':
        sp_matrix = af.constant(0, n**2, n**2,
                                dtype=af.Dtype.f64) * (1 - 4 * Lambda)
        ep1 = af.constant(1, n**2, 1, dtype=af.Dtype.f64)
        ep1[n:n**2:n] = 0
        ep1[1:n**2:n] = 2

        epn = af.constant(1, n**2, 1, dtype=af.Dtype.f64)
        epn[0:2 * n] = 2

        em1 = af.constant(1, n**2, 1, dtype=af.Dtype.f64)
        em1[n - 1:n**2:n] = 0
        em1[n - 2:n**2:n] = 2

        emn = af.constant(1, n**2, 1, dtype=af.Dtype.f64)
        emn[n**2 - 2 * n:n**2] = 2

        d = af.constant(-4, n**2, 1, dtype=af.Dtype.f64)

        for i in range(n**2):
            for j in range(n**2):
                if (i == j):
                    sp_matrix[i, j] = d[i]
                if (i == j + 1):
                    sp_matrix[i, j] = em1[j]
                if (i == j + n):
                    sp_matrix[i, j] = emn[j]
                if (i == j - 1):
                    sp_matrix[i, j] = ep1[i + 1]
                if (i == j - n):
                    sp_matrix[i, j] = epn[i + n]
        sp_matrix *= Lambda

        sp_matrix[-1, :] = 0
        sp_matrix[-1, -1] = 1

    return sp_matrix
Beispiel #22
0
def Convolve4(signal, kernel):
    if kernel.elements() is 1:
        return(signal * kernel.to_list()[0])
    signal_d = signal.shape
    if len(kernel.shape) is 3:
        out = af.constant(0, signal_d[0], signal_d[1], signal_d[2], signal_d[3])
        for i in range(signal.shape[3]):
            out[:, :, :, i] = af.fft_convolve3(signal[:, :, :, i], kernel)
        return(out)
    extend_space = kernel.shape[3] - 1
    out = af.constant(0, signal_d[0], signal_d[1], signal_d[2], signal_d[3] + extend_space)
    for i in range(extend_space + 1):
        for j in range(signal.shape[3]):
            out[:, :, :, i + j] = out[:, :, :, i + j] + af.fft_convolve3(signal[:, :, :, j], kernel[:, :, :, i])
    return(out[:, :, :, (extend_space / 2):(-extend_space / 2)])
    def kernel_gen(self, lambda_f, NA_obj, NAs):  
        
        '''
        
        kernel_gen generates the required filter for the algorithm, including the OTF 
        and the Fourier support of the object, speckle and the OTF.
        
        '''
        
        # Compute transfer function
        Pupil_obj = np.zeros((self.Nc,self.Mc))
        frc = (self.fxx_c**2 + self.fyy_c**2)**(1/2)
        Pupil_obj[frc<NA_obj/lambda_f] = 1
        T_incoherent = abs(fft2(abs(ifft2(Pupil_obj))**2))
        self.T_incoherent = T_incoherent/np.max(T_incoherent)
        self.T_incoherent = af.interop.np_to_af_array(self.T_incoherent)
        
        # Compute support function
        self.Pattern_support = af.constant(0, self.Npp, self.Mpp, dtype=af.Dtype.f64)
        frp = (self.fxxp**2 + self.fyyp**2)**(1/2)
        self.Pattern_support[frp<2*NAs/lambda_f] = 1

        self.Object_support = np.zeros((self.Nc,self.Mc))
        self.Object_support[frc<2*(NA_obj+NAs)/lambda_f] = 1
        self.Object_support = af.interop.np_to_af_array(self.Object_support)

        self.OTF_support = np.zeros((self.Nc,self.Mc))
        self.OTF_support[frc<2*NA_obj/lambda_f] = 1
        self.OTF_support = af.interop.np_to_af_array(self.OTF_support)
def main():
    T = 1
    nT = 20 * T
    R_first = 1000
    R = 5000000

    x0 = 0 # initial log stock price
    v0 = 0.087**2 # initial volatility
    r = math.log(1.0319) # risk-free rate
    rho = -0.82 # instantaneous correlation between Brownian motions
    sigmaV = 0.14 # variance of volatility
    kappa = 3.46 # mean reversion speed
    vBar = 0.008 # mean variance
    k = math.log(0.95) # strike price

    # first run
    ( x, v ) = simulateHestonModel( T, nT, R_first, r, kappa, vBar, sigmaV, rho, x0, v0 )

    # Price plain vanilla call option
    tic = time.time()
    ( x, v ) = simulateHestonModel( T, nT, R, r, kappa, vBar, sigmaV, rho, x0, v0 )
    af.sync()
    toc = time.time() - tic
    K = math.exp(k)
    zeroConstant = af.constant(0, R, dtype=af.Dtype.f32)
    C_CPU = math.exp(-r * T) * af.mean(af.maxof(af.exp(x) - K, zeroConstant))
    print("Time elapsed = {} secs".format(toc))
    print("Call price = {}".format(C_CPU))
    print(af.mean(v))
Beispiel #25
0
def main():
    T = 1
    nT = 20 * T
    R_first = 1000
    R = 5000000

    x0 = 0  # initial log stock price
    v0 = 0.087**2  # initial volatility
    r = math.log(1.0319)  # risk-free rate
    rho = -0.82  # instantaneous correlation between Brownian motions
    sigmaV = 0.14  # variance of volatility
    kappa = 3.46  # mean reversion speed
    vBar = 0.008  # mean variance
    k = math.log(0.95)  # strike price

    # first run
    (x, v) = simulateHestonModel(T, nT, R_first, r, kappa, vBar, sigmaV, rho,
                                 x0, v0)

    # Price plain vanilla call option
    tic = time.time()
    (x, v) = simulateHestonModel(T, nT, R, r, kappa, vBar, sigmaV, rho, x0, v0)
    af.sync()
    toc = time.time() - tic
    K = math.exp(k)
    zeroConstant = af.constant(0, R, dtype=af.Dtype.f32)
    C_CPU = math.exp(-r * T) * af.mean(af.maxof(af.exp(x) - K, zeroConstant))
    print("Time elapsed = {} secs".format(toc))
    print("Call price = {}".format(C_CPU))
    print(af.mean(v))
Beispiel #26
0
    def __init__(self):

        self.q1_start = 0
        self.q2_start = 0

        self.q1_end = 1
        self.q2_end = 1

        self.N_q1 = 1024
        self.N_q2 = 1024

        self.dq1 = (self.q1_end - self.q1_start) / self.N_q1
        self.dq2 = (self.q2_end - self.q2_start) / self.N_q2

        self.N_ghost = self.N_g = np.random.randint(3, 5)

        self.q1_center, self.q2_center = \
            calculate_q_center(self.q1_start, self.q2_start,
                               self.N_q1, self.N_q2, self.N_ghost,
                               self.dq1, self.dq2
                              )

        self.params = type('obj', (object, ), {'eps':1})

        self.cell_centered_EM_fields = af.constant(0, 6, 1, 
                                                   self.q1_center.shape[2],
                                                   self.q1_center.shape[3],
                                                   dtype=af.Dtype.f64
                                                  )

        self._comm   = PETSc.COMM_WORLD.tompi4py()

        self.performance_test_flag = False
Beispiel #27
0
def nn_build(seed, layerlist, nonlin='sigmoid', eta=0.01, init='glorot'):
    """
    Returns a neural network of given architecture
    :param layerlist: a list of number of neurons in each layer starting with input and ending with output
    :param nonlin: nonlinearity which is either 'sigmoid', 'tanh', or 'ReLU'
    :return: a dictionary with neural network parameters
    """
    nn = {}
    nn['eta'] = eta
    nn['weights'] = []
    weights = []
    biases = []
    nn['biases'] = []
    nn['nonlin'] = []
    for i in range(len(layerlist) - 1):
        nn['weights'].append(
            weight_matrix(seed,
                          layerlist[i],
                          layerlist[i + 1],
                          layer=i,
                          type=init))
        nn['biases'].append(af.constant(1, 1, layerlist[i + 1]) * 0.1)
        nn['nonlin'].append(activate(nonlin))

    #nn['weights'] = weights
    #nn['biases'] = biases
    #addadadelta(nn)
    return nn
def monte_carlo_options(N, K, t, vol, r, strike, steps, use_barrier = True, B = None, ty = af.Dtype.f32):
    payoff = af.constant(0, N, 1, dtype = ty)

    dt = t / float(steps - 1)
    s = af.constant(strike, N, 1, dtype = ty)

    randmat = af.randn(N, steps - 1, dtype = ty)
    randmat = af.exp((r - (vol * vol * 0.5)) * dt + vol * math.sqrt(dt) * randmat);

    S = af.product(af.join(1, s, randmat), 1)

    if (use_barrier):
        S = S * af.all_true(S < B, 1)

    payoff = af.maxof(0, S - K)
    return af.mean(payoff) * math.exp(-r * t)
Beispiel #29
0
    def __init__(self):
        
        self.q1_start = np.random.randint(0, 5)
        self.q2_start = np.random.randint(0, 5)

        self.q1_end = np.random.randint(5, 10)
        self.q2_end = np.random.randint(5, 10)

        self.N_q1 = np.random.randint(16, 32)
        self.N_q2 = np.random.randint(16, 32)

        self.dq1 = (self.q1_end - self.q1_start) / self.N_q1
        self.dq2 = (self.q2_end - self.q2_start) / self.N_q2

        N_g = self.N_ghost = np.random.randint(1, 5)

        self.q1 = self.q1_start \
                  * (0.5 + np.arange(-self.N_ghost,
                                     self.N_q1 + self.N_ghost
                                    )
                    ) * self.dq1

        self.q2 = self.q2_start \
                  * (0.5 + np.arange(-self.N_ghost,
                                      self.N_q2 + self.N_ghost
                                    )
                    ) * self.dq2

        self.q2, self.q1 = np.meshgrid(self.q2, self.q1)
        self.q1, self.q2 = af.to_array(self.q1), af.to_array(self.q2)

        self.q1 = af.reorder(self.q1, 2, 0, 1)
        self.q2 = af.reorder(self.q2, 2, 0, 1)

        self.q1 = af.tile(self.q1, 6)
        self.q2 = af.tile(self.q2, 6)

        self._da_fields = PETSc.DMDA().create([self.N_q1, self.N_q2],
                                              dof=6,
                                              stencil_width=self.N_ghost,
                                              boundary_type=('periodic',
                                                             'periodic'),
                                              stencil_type=1,
                                             )

        self._glob_fields  = self._da_fields.createGlobalVec()
        self._local_fields = self._da_fields.createLocalVec()

        self._glob_fields_array  = self._glob_fields.getArray()
        self._local_fields_array = self._local_fields.getArray()

        self.cell_centered_EM_fields = af.constant(0, 6, self.q1.shape[1], 
                                                   self.q1.shape[2],
                                                   dtype=af.Dtype.f64
                                                  )

        self.cell_centered_EM_fields[:, N_g:-N_g, N_g:-N_g] = \
            af.sin(2 * np.pi * self.q1 + 4 * np.pi * self.q2)[:, N_g:-N_g,N_g:-N_g]
        
        self.performance_test_flag = False
def train(X,
          Y,
          alpha=0.1,
          lambda_param=1.0,
          maxerr=0.01,
          maxiter=1000,
          verbose=False):
    # Initialize parameters to 0
    Weights = af.constant(0, X.dims()[1], Y.dims()[1])

    for i in range(maxiter):
        # Get the cost and gradient
        J, dJ = cost(Weights, X, Y, lambda_param)

        err = af.max(af.abs(J))
        if err < maxerr:
            print('Iteration {0:4d} Err: {1:4f}'.format(i + 1, err))
            print('Training converged')
            return Weights

        if verbose and ((i + 1) % 10 == 0):
            print('Iteration {0:4d} Err: {1:4f}'.format(i + 1, err))

        # Update the parameters via gradient descent
        Weights = Weights - alpha * dJ

    if verbose:
        print('Training stopped after {0:d} iterations'.format(maxiter))

    return Weights
def cost(Weights, X, Y, lambda_param=1.0):
    # Number of samples
    m = Y.dims()[0]

    dim0 = Weights.dims()[0]
    dim1 = Weights.dims()[1] if len(Weights.dims()) > 1 else None
    dim2 = Weights.dims()[2] if len(Weights.dims()) > 2 else None
    dim3 = Weights.dims()[3] if len(Weights.dims()) > 3 else None
    # Make the lambda corresponding to Weights(0) == 0
    lambdat = af.constant(lambda_param, dim0, dim1, dim2, dim3)

    # No regularization for bias weights
    lambdat[0, :] = 0

    # Get the prediction
    H = predict_prob(X, Weights)

    # Cost of misprediction
    Jerr = -1 * af.sum(Y * af.log(H) + (1 - Y) * af.log(1 - H), dim=0)

    # Regularization cost
    Jreg = 0.5 * af.sum(lambdat * Weights * Weights, dim=0)

    # Total cost
    J = (Jerr + Jreg) / m

    # Find the gradient of cost
    D = (H - Y)
    dJ = (af.matmulTN(X, D) + lambdat * Weights) / m

    return J, dJ
Beispiel #32
0
def forward(nn_np, data):
    """
    Given a dictionary representing a feed forward neural net and an input data matrix compute the network's output and store it within the dictionary
    :param nn: neural network dictionary
    :param data: a numpy n by m matrix where m in the number of input units in nn
    :return: the output layer activations
    """
    nn = nn_np
    nn['activations'] = []

    #prct = []
    #prct.append(data[0][0])
    #prct.append(data[1][0])

    #src = data.astype(float)

    nsrc = data  #af.Array(prct)

    nn['activations'].append(nsrc)
    #(af.Array(src.ctypes.data, src.shape, src.dtype.char))

    nn['zs'] = []
    for w, s, b in map(None, nn['weights'], nn['nonlin'], nn['biases']):

        #:wq
        #print af.transpose(nn['activations'][-1])

        #print(type(nn['activations'][0]))
        #print(nn['activations'][0])

        z = af.matmul(w, nn['activations'][-1])

        #for i in range(w.shape[1]-1):
        #	newB = af.join(1,newB,b)

        p = z.T + b

        nn['zs'].append(p.T)
        if len(p.shape) == 2:
            maxVal = af.constant(1e-5, p.shape[0], p.shape[1]).T
        else:
            maxVal = af.constant(1e-5, p.shape[0]).T
        q = af.maxof(p.T, 1e-5)  #p(i) = 1e-5

        nn['activations'].append(q)

    return nn['activations'][-1]
Beispiel #33
0
 def adjoint(self, residual):
     if len(residual.shape) > 2:
         field_pad = af.constant(0.0,
                                 self.shape[0],
                                 self.shape[1],
                                 residual.shape[2],
                                 dtype=af_complex_datatype)
         for z_idx in range(field_pad.shape[2]):
             field_pad[self.row_crop, self.col_crop,
                       z_idx] = residual[:, :, z_idx] + 0.0j
     else:
         field_pad = af.constant(0.0,
                                 self.shape[0],
                                 self.shape[1],
                                 dtype=af_complex_datatype)
         field_pad[self.row_crop, self.col_crop] = residual + 0.0j
     return field_pad
    def __init__(self,
                 params,
                 learning_rate_init=0.001,
                 beta_1=0.9,
                 beta_2=0.999,
                 epsilon=1e-8):
        super().__init__(params, learning_rate_init)

        self.beta_1 = beta_1
        self.beta_2 = beta_2
        self.epsilon = epsilon
        self.t = 0

        dims_list = [paddims2(param.dims()) for param in params]

        self.ms = [af.constant(0, dim[0], dim[1]) for dim in dims_list]
        self.vs = [af.constant(0, dim[0], dim[1]) for dim in dims_list]
def simulateHestonModel( T, N, R, mu, kappa, vBar, sigmaV, rho, x0, v0 ) :

    deltaT = T / (float)(N - 1)

    x = [af.constant(x0, R, dtype=af.Dtype.f32), af.constant(0, R, dtype=af.Dtype.f32)]
    v = [af.constant(v0, R, dtype=af.Dtype.f32), af.constant(0, R, dtype=af.Dtype.f32)]

    sqrtDeltaT = math.sqrt(deltaT)
    sqrtOneMinusRhoSquare = math.sqrt(1-rho**2)

    m = af.constant(0, 2, dtype=af.Dtype.f32)
    m[0] = rho
    m[1] = sqrtOneMinusRhoSquare
    zeroArray = af.constant(0, R, 1, dtype=af.Dtype.f32)

    for t in range(1, N) :
        tPrevious = (t + 1) % 2
        tCurrent =  t % 2

        dBt = af.randn(R, 2, dtype=af.Dtype.f32) * sqrtDeltaT

        vLag = af.maxof(v[tPrevious], zeroArray)
        sqrtVLag = af.sqrt(vLag)

        x[tCurrent] = x[tPrevious] + (mu - 0.5 * vLag) * deltaT + sqrtVLag * dBt[:, 0]
        v[tCurrent] = vLag + kappa * (vBar - vLag) * deltaT + sigmaV * (sqrtVLag * af.matmul(dBt, m))

    return (x[tCurrent], af.maxof(v[tCurrent], zeroArray))
Beispiel #36
0
def calc_arrayfire(A, b, x0, maxiter=10):
    x = af.constant(0, b.dims()[0], dtype=af.Dtype.f32)
    r = b - af.matmul(A, x)
    p = r
    for i in range(maxiter):
        Ap = af.matmul(A, p)
        alpha_num = af.dot(r, r)
        alpha_den = af.dot(p, Ap)
        alpha = alpha_num/alpha_den
        r -= af.tile(alpha, Ap.dims()[0]) * Ap
        x += af.tile(alpha, Ap.dims()[0]) * p
        beta_num = af.dot(r, r)
        beta = beta_num/alpha_num
        p = r + af.tile(beta, p.dims()[0]) * p
    af.eval(x)
    res = x0 - x
    return x, af.dot(res, res)
Beispiel #37
0
def mandelbrot(data, it, maxval):
    C = data
    Z = data
    mag = af.constant(0, *C.dims())

    for ii in range(1, 1 + it):
        # Doing the calculation
        Z = Z * Z + C

        # Get indices where abs(Z) crosses maxval
        cond = ((af.abs(Z) > maxval)).as_type(af.Dtype.f32)
        mag = af.maxof(mag, cond * ii)

        C = C * (1 - cond)
        Z = Z * (1 - cond)

        af.eval(C)
        af.eval(Z)

    return mag / maxval
def nn_build(seed,layerlist, nonlin='sigmoid', eta=0.01, init='glorot'):
    """
    Returns a neural network of given architecture
    :param layerlist: a list of number of neurons in each layer starting with input and ending with output
    :param nonlin: nonlinearity which is either 'sigmoid', 'tanh', or 'ReLU'
    :return: a dictionary with neural network parameters
    """
    nn = {}
    nn['eta'] = eta
    nn['weights'] = []
    weights = []
    biases = []
    nn['biases'] = []
    nn['nonlin'] = []
    for i in range(len(layerlist) - 1):
        nn['weights'].append(weight_matrix(seed,layerlist[i], layerlist[i + 1],layer=i,type=init))
        nn['biases'].append(af.constant(1,1,layerlist[i + 1]) * 0.1)
        nn['nonlin'].append(activate(nonlin))
    
    #nn['weights'] = weights
    #nn['biases'] = biases
    #addadadelta(nn)
    return nn
Beispiel #39
0
def simple_arith(verbose = False):
    display_func = _util.display_func(verbose)
    print_func   = _util.print_func(verbose)

    a = af.randu(3,3,dtype=af.Dtype.u32)
    b = af.constant(4, 3, 3, dtype=af.Dtype.u32)
    display_func(a)
    display_func(b)

    c = a + b
    d = a
    d += b

    display_func(c)
    display_func(d)
    display_func(a + 2)
    display_func(3 + a)


    c = a - b
    d = a
    d -= b

    display_func(c)
    display_func(d)
    display_func(a - 2)
    display_func(3 - a)

    c = a * b
    d = a
    d *= b

    display_func(c * 2)
    display_func(3 * d)
    display_func(a * 2)
    display_func(3 * a)

    c = a / b
    d = a
    d /= b

    display_func(c / 2.0)
    display_func(3.0 / d)
    display_func(a / 2)
    display_func(3 / a)

    c = a % b
    d = a
    d %= b

    display_func(c % 2.0)
    display_func(3.0 % d)
    display_func(a % 2)
    display_func(3 % a)

    c = a ** b
    d = a
    d **= b

    display_func(c ** 2.0)
    display_func(3.0 ** d)
    display_func(a ** 2)
    display_func(3 ** a)

    display_func(a < b)
    display_func(a < 0.5)
    display_func(0.5 < a)

    display_func(a <= b)
    display_func(a <= 0.5)
    display_func(0.5 <= a)

    display_func(a > b)
    display_func(a > 0.5)
    display_func(0.5 > a)

    display_func(a >= b)
    display_func(a >= 0.5)
    display_func(0.5 >= a)

    display_func(a != b)
    display_func(a != 0.5)
    display_func(0.5 != a)

    display_func(a == b)
    display_func(a == 0.5)
    display_func(0.5 == a)

    display_func(a & b)
    display_func(a & 2)
    c = a
    c &= 2
    display_func(c)

    display_func(a | b)
    display_func(a | 2)
    c = a
    c |= 2
    display_func(c)

    display_func(a >> b)
    display_func(a >> 2)
    c = a
    c >>= 2
    display_func(c)

    display_func(a << b)
    display_func(a << 2)
    c = a
    c <<= 2
    display_func(c)

    display_func(-a)
    display_func(+a)
    display_func(~a)
    display_func(a)

    display_func(af.cast(a, af.Dtype.c32))
    display_func(af.maxof(a,b))
    display_func(af.minof(a,b))
    display_func(af.rem(a,b))

    a = af.randu(3,3) - 0.5
    b = af.randu(3,3) - 0.5

    display_func(af.abs(a))
    display_func(af.arg(a))
    display_func(af.sign(a))
    display_func(af.round(a))
    display_func(af.trunc(a))
    display_func(af.floor(a))
    display_func(af.ceil(a))
    display_func(af.hypot(a, b))
    display_func(af.sin(a))
    display_func(af.cos(a))
    display_func(af.tan(a))
    display_func(af.asin(a))
    display_func(af.acos(a))
    display_func(af.atan(a))
    display_func(af.atan2(a, b))

    c = af.cplx(a)
    d = af.cplx(a,b)
    display_func(c)
    display_func(d)
    display_func(af.real(d))
    display_func(af.imag(d))
    display_func(af.conjg(d))

    display_func(af.sinh(a))
    display_func(af.cosh(a))
    display_func(af.tanh(a))
    display_func(af.asinh(a))
    display_func(af.acosh(a))
    display_func(af.atanh(a))

    a = af.abs(a)
    b = af.abs(b)

    display_func(af.root(a, b))
    display_func(af.pow(a, b))
    display_func(af.pow2(a))
    display_func(af.exp(a))
    display_func(af.expm1(a))
    display_func(af.erf(a))
    display_func(af.erfc(a))
    display_func(af.log(a))
    display_func(af.log1p(a))
    display_func(af.log10(a))
    display_func(af.log2(a))
    display_func(af.sqrt(a))
    display_func(af.cbrt(a))

    a = af.round(5 * af.randu(3,3) - 1)
    b = af.round(5 * af.randu(3,3) - 1)

    display_func(af.factorial(a))
    display_func(af.tgamma(a))
    display_func(af.lgamma(a))
    display_func(af.iszero(a))
    display_func(af.isinf(a/b))
    display_func(af.isnan(a/a))

    a = af.randu(5, 1)
    b = af.randu(1, 5)
    c = af.broadcast(lambda x,y: x+y, a, b)
    display_func(a)
    display_func(b)
    display_func(c)

    @af.broadcast
    def test_add(aa, bb):
        return aa + bb

    display_func(test_add(a, b))
#!/usr/bin/python
#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

import arrayfire as af

af.display(af.constant(100, 3,3, dtype=af.f32))
af.display(af.constant(25, 3,3, dtype=af.c32))
af.display(af.constant(2**50, 3,3, dtype=af.s64))
af.display(af.constant(2+3j, 3,3))
af.display(af.constant(3+5j, 3,3, dtype=af.c32))

af.display(af.range(3, 3))
af.display(af.iota(3, 3, tile_dims=(2,2)))

af.display(af.randu(3, 3, 1, 2))
af.display(af.randu(3, 3, 1, 2, af.b8))
af.display(af.randu(3, 3, dtype=af.c32))

af.display(af.randn(3, 3, 1, 2))
af.display(af.randn(3, 3, dtype=af.c32))

af.set_seed(1024)
assert(af.get_seed() == 1024)
 def createValidityMap(self, push=True):  #vxy
     a = time.time()
     if push:
         self.volumes[self.indexVolume] = [copy.deepcopy(self.areas),
         copy.deepcopy(self.areaViewPoints), copy.deepcopy(self.drawingAreas),
                                       copy.copy(self.indexArea)]
     else:
         if self.volumes[self.indexVolume]:
             self.areas = self.volumes[self.indexVolume][0]
     oneAreaMode = len(self.areas) == 1 or (len(self.areas) == 2 and not self.areas[1])
     if self.colorMode == 'rgb':
         side = 256
     else:
         side = self.side
     if self.gpuMode and oneAreaMode:
         self.validityMap = af.constant(0, 256, side, side, dtype=af.Dtype.u8)
     else:
         self.validityMap = np.zeros(shape=(256, side, side), dtype=np.uint8)
     if not self.areas[0]:
         self.validityMap = False
         self.updateDynamic(self.validityMap)
         return
     if oneAreaMode:
         dict = self.numpyAreas2Dict(self.areas[0], concave=True)
         minx, maxx = dict['i'], dict['f']
         newbounds = []
         for x in xrange(minx, (maxx + 1)):
             try:
                 bounds = dict[x]
                 if len(bounds) == 2:
                     if bounds[0] != bounds[1]:
                         newbounds = bounds
                 else:
                     newbounds = [bounds[0]]
                     for i in xrange(1, len(bounds)):
                         if abs(bounds[i] - bounds[i - 1]) > 2:
                             newbounds.append(bounds[i])
                     if len(newbounds) % 2 == 1:
                         del newbounds[-1]
             except:
                 pass  # use previous values
             for i in xrange(0, len(newbounds), 2):
                 try:
                     self.validityMap[0:256, x, newbounds[i]:newbounds[i + 1]] = 1
                 except:
                     print 'ERROR! in colorspace.createValidityMap', len(newbounds), i, x, newbounds
                     quit()
         if push:
             b = time.time()
             print 'time to create simple validitymap', 1000*(b-a)
             self.updateDynamic(self.validityMap)
         return
     # complex drawings:
     ind = []  # indices in self.areas
     indvs = []  # and there corresponding v-values
     areaDicts = []  # mini, minv
     for i, area in enumerate(self.areas):  # preprocessing areas into dicts
         if area:
             ind.append(i)  # make orderedareas
             indvs.append(area[2])  # make orderedareas
             dict = self.numpyAreas2Dict(area)
             areaDicts.append(dict)
     orderedareas = [[i, v] for (v, i) in sorted(zip(indvs, ind))]
     [lowI, lowV] = orderedareas[0]
     for [highI, highV] in orderedareas[1::]:
         if highV == lowV:
             print 'an area was discounted for having the same intensity as the other'
             continue
         lowXi = areaDicts[lowI]['i']
         lowXf = areaDicts[lowI]['f']
         highXi = areaDicts[highI]['i']
         highXf = areaDicts[highI]['f']
         lowDX = lowXf - lowXi
         highDX = highXf - highXi
         for v in xrange(lowV, (highV + 1)):
             fracV = float(v - lowV) / (highV - lowV)
             ofracV = 1 - fracV
             avgXi = int(fracV * highXi + ofracV * lowXi)
             avgXf = int(fracV * highXf + ofracV * lowXf)
             avgDX = float(avgXf - avgXi)
             for avgX in xrange(avgXi, (avgXf + 1)):
                 avgFracX = (avgX - avgXi) / avgDX
                 highX = int(avgFracX * highDX) + highXi
                 lowX = int(avgFracX * lowDX) + lowXi
                 try:
                     [highYmin, highYmax] = areaDicts[highI][highX]  # get min and max
                 except:
                     pass  # use past values
                 try:
                     [lowYmin, lowYmax] = areaDicts[lowI][lowX]  # get min max
                 except:
                     pass  # use previous values
                 avgYmin = int(highYmin * fracV + lowYmin * ofracV)
                 avgYmax = int(highYmax * fracV + lowYmax * ofracV) + 1
                 self.validityMap[v, avgX, avgYmin:avgYmax] = 1
         [lowI, lowV] = [highI, highV]
     if self.gpuMode:
         self.validityMap = af.interop.np_to_af_array(self.validityMap)
     b = time.time()
     if push:
         self.updateDynamic(self.validityMap)
     print 'time to create validityMap ms:', (b-a) * 1000
Beispiel #42
0
def simple_index(verbose=False):
    display_func = _util.display_func(verbose)
    print_func   = _util.print_func(verbose)
    a = af.randu(5, 5)
    display_func(a)
    b = af.Array(a)
    display_func(b)

    c = a.copy()
    display_func(c)
    display_func(a[0,0])
    display_func(a[0])
    display_func(a[:])
    display_func(a[:,:])
    display_func(a[0:3,])
    display_func(a[-2:-1,-1])
    display_func(a[0:5])
    display_func(a[0:5:2])

    idx = af.Array(host.array('i', [0, 3, 2]))
    display_func(idx)
    aa = a[idx]
    display_func(aa)

    a[0] = 1
    display_func(a)
    a[0] = af.randu(1, 5)
    display_func(a)
    a[:] = af.randu(5,5)
    display_func(a)
    a[:,-1] = af.randu(5,1)
    display_func(a)
    a[0:5:2] = af.randu(3, 5)
    display_func(a)
    a[idx, idx] = af.randu(3,3)
    display_func(a)


    a = af.randu(5,1)
    b = af.randu(5,1)
    display_func(a)
    display_func(b)
    for ii in ParallelRange(1,3):
        a[ii] = b[ii]

    display_func(a)

    for ii in ParallelRange(2,5):
        b[ii] = 2
    display_func(b)

    a = af.randu(3,2)
    rows = af.constant(0, 1, dtype=af.Dtype.s32)
    b = a[:,rows]
    display_func(b)
    for r in rows:
        display_func(r)
        display_func(b[:,r])

    a = af.randu(3)
    c = af.randu(3)
    b = af.constant(1,3,dtype=af.Dtype.b8)
    display_func(a)
    a[b] = c
    display_func(a)
Beispiel #43
0
def simple_algorithm(verbose = False):
    display_func = _util.display_func(verbose)
    print_func   = _util.print_func(verbose)

    a = af.randu(3, 3)
    k = af.constant(1, 3, 3, dtype=af.Dtype.u32)
    af.eval(k)

    print_func(af.sum(a), af.product(a), af.min(a), af.max(a),
               af.count(a), af.any_true(a), af.all_true(a))

    display_func(af.sum(a, 0))
    display_func(af.sum(a, 1))

    display_func(af.product(a, 0))
    display_func(af.product(a, 1))

    display_func(af.min(a, 0))
    display_func(af.min(a, 1))

    display_func(af.max(a, 0))
    display_func(af.max(a, 1))

    display_func(af.count(a, 0))
    display_func(af.count(a, 1))

    display_func(af.any_true(a, 0))
    display_func(af.any_true(a, 1))

    display_func(af.all_true(a, 0))
    display_func(af.all_true(a, 1))

    display_func(af.accum(a, 0))
    display_func(af.accum(a, 1))

    display_func(af.scan(a, 0, af.BINARYOP.ADD))
    display_func(af.scan(a, 1, af.BINARYOP.MAX))

    display_func(af.scan_by_key(k, a, 0, af.BINARYOP.ADD))
    display_func(af.scan_by_key(k, a, 1, af.BINARYOP.MAX))

    display_func(af.sort(a, is_ascending=True))
    display_func(af.sort(a, is_ascending=False))

    b = (a > 0.1) * a
    c = (a > 0.4) * a
    d = b / c
    print_func(af.sum(d));
    print_func(af.sum(d, nan_val=0.0));
    display_func(af.sum(d, dim=0, nan_val=0.0));

    val,idx = af.sort_index(a, is_ascending=True)
    display_func(val)
    display_func(idx)
    val,idx = af.sort_index(a, is_ascending=False)
    display_func(val)
    display_func(idx)

    b = af.randu(3,3)
    keys,vals = af.sort_by_key(a, b, is_ascending=True)
    display_func(keys)
    display_func(vals)
    keys,vals = af.sort_by_key(a, b, is_ascending=False)
    display_func(keys)
    display_func(vals)

    c = af.randu(5,1)
    d = af.randu(5,1)
    cc = af.set_unique(c, is_sorted=False)
    dd = af.set_unique(af.sort(d), is_sorted=True)
    display_func(cc)
    display_func(dd)

    display_func(af.set_union(cc, dd, is_unique=True))
    display_func(af.set_union(cc, dd, is_unique=False))

    display_func(af.set_intersect(cc, cc, is_unique=True))
    display_func(af.set_intersect(cc, cc, is_unique=False))
#!/usr/bin/python
#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

import arrayfire as af

a = af.randu(3,3,dtype=af.u32)
b = af.constant(4, 3, 3, dtype=af.u32)
af.display(a)
af.display(b)

c = a + b
d = a
d += b

af.display(c)
af.display(d)
af.display(a + 2)
af.display(3 + a)


c = a - b
d = a
d -= b
    af.display(B)

    print("Negate the first three elements of second column\n")
    B[0:3, 1] = B[0:3, 1] * -1
    af.display(B)

    print("Fourier transform the result\n");
    C = af.fft(B);
    af.display(C);

    print("Grab last row\n");
    c = C[-1,:];
    af.display(c);

    print("Scan Test\n");
    r = af.constant(2, 16, 4, 1, 1);
    af.display(r);

    print("Scan\n");
    S = af.scan(r, 0, af.BINARYOP.MUL);
    af.display(S);

    print("Create 2-by-3 matrix from host data\n");
    d = [ 1, 2, 3, 4, 5, 6 ]
    D = af.Array(d, (2, 3))
    af.display(D)

    print("Copy last column onto first\n");
    D[:,0] = D[:, -1]
    af.display(D);
#!/usr/bin/python
import arrayfire as af

af.print_array(af.constant(100, 3,3, dtype=af.f32))
af.print_array(af.constant(25, 3,3, dtype=af.c32))
af.print_array(af.constant(2**50, 3,3, dtype=af.s64))
af.print_array(af.constant(2+3j, 3,3))
af.print_array(af.constant(3+5j, 3,3, dtype=af.c32))

af.print_array(af.range(3, 3))
af.print_array(af.iota(3, 3, tile_dims=(2,2)))

af.print_array(af.randu(3, 3, 1, 2))
af.print_array(af.randu(3, 3, 1, 2, af.b8))
af.print_array(af.randu(3, 3, dtype=af.c32))

af.print_array(af.randn(3, 3, 1, 2))
af.print_array(af.randn(3, 3, dtype=af.c32))

af.set_seed(1024)
assert(af.get_seed() == 1024)

af.print_array(af.identity(3, 3, 1, 2, af.b8))
af.print_array(af.identity(3, 3, dtype=af.c32))

a = af.randu(3, 4)
b = af.diag(a, extract=True)
c = af.diag(a, 1, extract=True)

af.print_array(a)
af.print_array(b)