def test_poly1d_prod(): ''' Checks the product of the polynomials of different degrees using the poly1d_product function and compares it to the analytically calculated product coefficients. ''' N = 3 N_a = 3 poly_a = af.range(N * N_a, dtype=af.Dtype.u32) poly_a = af.moddims(poly_a, d0=N, d1=N_a) N_b = 2 poly_b = af.range(N * N_b, dtype=af.Dtype.u32) poly_b = af.moddims(poly_b, d0=N, d1=N_b) ref_poly = af.np_to_af_array( np.array([[0., 0., 9., 18.], [1., 8., 23., 28.], [4., 20., 41., 40.]])) test_poly1d_prod = utils.poly1d_product(poly_a, poly_b) test_poly1d_prod_commutative = utils.poly1d_product(poly_b, poly_a) diff = af.abs(test_poly1d_prod - ref_poly) diff_commutative = af.abs(test_poly1d_prod_commutative - ref_poly) assert af.all_true(diff == 0.) and af.all_true(diff_commutative == 0.)
def test_Li_basis_value(): ''' This test compares the output of the lagrange basis value calculated by the function ``Li_basis_value`` to the analytical value of the lagrange polynomials created using the same LGL points. The analytical values were calculated in this sage worksheet_ .. _worksheet: https://goo.gl/ADyA3U ''' threshold = 1e-11 Li_value_ref = af.np_to_af_array(utils.csv_to_numpy( 'dg_maxwell/tests/lagrange/files/Li_value.csv')) N_LGL = 8 xi_LGL = lagrange.LGL_points(N_LGL) L_basis_poly1d, L_basis_af = lagrange.lagrange_polynomials(xi_LGL) L_basis_af = af.np_to_af_array(L_basis_af) Li_indexes = af.np_to_af_array(np.arange(3, dtype = np.int32)) xi = af.np_to_af_array(np.linspace(-1., 1, 10)) Li_value = lagrange.Li_basis_value(L_basis_af, Li_indexes, xi) assert af.all_true(af.abs(Li_value - Li_value_ref) < threshold)
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)
def test_dy_dxi(): ''' This test checks the derivative :math:`\\frac{\\partial y}{\\partial \\xi}` calculated using the function :math:`dg_maxwell.wave_equation_2d.dy_dxi` for the :math:`0^{th}` element of a mesh for a circular ring. You may download the file from this :download:`link <../dg_maxwell/tests/wave_equation_2d/files/circle.msh>`. ''' threshold = 1e-7 dy_dxi_reference = af.np_to_af_array( utils.csv_to_numpy( 'dg_maxwell/tests/wave_equation_2d/files/dy_dxi_data.csv')) nodes, elements = msh_parser.read_order_2_msh( 'dg_maxwell/tests/wave_equation_2d/files/circle.msh') N_LGL = 16 xi_LGL = lagrange.LGL_points(N_LGL) eta_LGL = lagrange.LGL_points(N_LGL) Xi = af.data.tile(af.array.transpose(xi_LGL), d0=N_LGL) Eta = af.data.tile(eta_LGL, d0=1, d1=N_LGL) dy_dxi = wave_equation_2d.dy_dxi(nodes[elements[0]][:, 1], Xi, Eta) check = af.abs(dy_dxi - dy_dxi_reference) < threshold assert af.all_true(check)
def test_integrate_1d(): ''' Tests the ``integrate_1d`` by comparing the integral agains the analytically calculated integral. The polynomials to be integrated are all the Lagrange polynomials obtained for the LGL points. The analytical integral is calculated in this `sage worksheet`_ .. _sage worksheet: https://goo.gl/1uYyNJ ''' threshold = 1e-12 N_LGL = 8 xi_LGL = lagrange.LGL_points(N_LGL) eta_LGL = lagrange.LGL_points(N_LGL) _, Li_xi = lagrange.lagrange_polynomials(xi_LGL) _, Lj_eta = lagrange.lagrange_polynomials(eta_LGL) Li_xi = af.np_to_af_array(Li_xi) Lp_xi = Li_xi.copy() Li_Lp = utils.poly1d_product(Li_xi, Lp_xi) test_integral_gauss = utils.integrate_1d(Li_Lp, order=9, scheme='gauss') test_integral_lobatto = utils.integrate_1d(Li_Lp, order=N_LGL + 1, scheme='lobatto') ref_integral = af.np_to_af_array( np.array([ 0.0333333333333, 0.196657278667, 0.318381179651, 0.384961541681, 0.384961541681, 0.318381179651, 0.196657278667, 0.0333333333333 ])) diff_gauss = af.abs(ref_integral - test_integral_gauss) diff_lobatto = af.abs(ref_integral - test_integral_lobatto) assert af.all_true(diff_gauss < threshold) and af.all_true( diff_lobatto < threshold)
def all(a: ndarray, axis: tp.Optional[int] = None, out: tp.Optional[ndarray] = None, keepdims: bool = False) \ -> tp.Union[bool, ndarray]: """ Returns True if all elements evaluate to True. """ if out: raise ValueError('out != None is not supported') return _wrap_af_array(af.all_true(a._af_array, dim=axis))
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)
def test_polyval_2d(): ''' Tests the ``utils.polyval_2d`` function by evaluating the polynomial .. math:: P_0(\\xi) P_1(\\eta) here, .. math:: P_0(\\xi) = 3 \, \\xi^{2} + 2 \, \\xi + 1 .. math:: P_1(\\eta) = 3 \, \\eta^{2} + 2 \, \\eta + 1 at corresponding ``linspace`` points in :math:`\\xi \\in [-1, 1]` and :math:`\\eta \\in [-1, 1]`. This value is then compared with the reference value calculated analytically. The reference values are calculated in `polynomial_product_two_variables.sagews`_ .. _polynomial_product_two_variables.sagews: https://goo.gl/KwG7k9 ''' threshold = 1e-12 poly_xi_degree = 4 poly_xi = af.flip(af.np_to_af_array(np.arange(1, poly_xi_degree))) poly_eta_degree = 4 poly_eta = af.flip(af.np_to_af_array(np.arange(1, poly_eta_degree))) poly_xi_eta = utils.polynomial_product_coeffs(poly_xi, poly_eta) xi = utils.linspace(-1, 1, 8) eta = utils.linspace(-1, 1, 8) polyval_xi_eta = af.transpose(utils.polyval_2d(poly_xi_eta, xi, eta)) polyval_xi_eta_ref = af.np_to_af_array( np.array([ 4.00000000000000, 1.21449396084962, 0.481466055810080, 0.601416076634741, 1.81424406497291, 5.79925031236988, 15.6751353602663, 36.0000000000000 ])) diff = af.abs(polyval_xi_eta - polyval_xi_eta_ref) assert af.all_true(diff < threshold)
def all(self, s, axis): return arrayfire.all_true(s, dim=axis)
def simple_algorithm(verbose = False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3, 3) 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.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) print(af.sum(a), af.product(a), af.min(a), af.max(a), af.count(a), af.any_true(a), af.all_true(a)) af.display(af.sum(a, 0)) af.display(af.sum(a, 1)) af.display(af.product(a, 0)) af.display(af.product(a, 1)) af.display(af.min(a, 0)) af.display(af.min(a, 1)) af.display(af.max(a, 0)) af.display(af.max(a, 1)) af.display(af.count(a, 0)) af.display(af.count(a, 1))
#!/usr/bin/python import arrayfire as af a = af.randu(3, 3) print(af.sum(a), af.product(a), af.min(a), af.max(a), af.count(a), af.any_true(a), af.all_true(a)) af.print_array(af.sum(a, 0)) af.print_array(af.sum(a, 1)) af.print_array(af.product(a, 0)) af.print_array(af.product(a, 1)) af.print_array(af.min(a, 0)) af.print_array(af.min(a, 1)) af.print_array(af.max(a, 0)) af.print_array(af.max(a, 1)) af.print_array(af.count(a, 0)) af.print_array(af.count(a, 1)) af.print_array(af.any_true(a, 0)) af.print_array(af.any_true(a, 1)) af.print_array(af.all_true(a, 0)) af.print_array(af.all_true(a, 1)) af.print_array(af.accum(a, 0)) af.print_array(af.accum(a, 1))
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)) rk = af.constant(1, 3, dtype=af.Dtype.u32) rk[2] = 0 af.eval(rk) display_func(af.sumByKey(rk, a, dim=0)) display_func(af.sumByKey(rk, a, dim=1)) display_func(af.productByKey(rk, a, dim=0)) display_func(af.productByKey(rk, a, dim=1)) display_func(af.minByKey(rk, a, dim=0)) display_func(af.minByKey(rk, a, dim=1)) display_func(af.maxByKey(rk, a, dim=0)) display_func(af.maxByKey(rk, a, dim=1)) display_func(af.anyTrueByKey(rk, a, dim=0)) display_func(af.anyTrueByKey(rk, a, dim=1)) display_func(af.allTrueByKey(rk, a, dim=0)) display_func(af.allTrueByKey(rk, a, dim=1)) display_func(af.countByKey(rk, a, dim=0)) display_func(af.countByKey(rk, a, dim=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))