def potential_from_grid(self, grid, bypass_decorator=False): """ Calculate the potential at a given set of arc-second gridded coordinates. Parameters ---------- grid : grids.Grid The grid of (y,x) arc-second coordinates the deflection angles are computed on. return_in_2d : bool If *True*, the returned array is mapped to its unmasked 2D shape, if *False* it is the masked 1D shape. return_binned : bool If *True*, the returned array which is computed on a sub-grid is binned up to the grid dimensions \ by taking the mean of all sub-gridded values. If *False*, the array is returned on the dimensions of the \ sub-grid. """ potential_grid = quad_grid( self.potential_func, 0.0, 1.0, grid, args=(self.axis_ratio, self.slope, self.core_radius), )[0] return self.einstein_radius_rescaled * self.axis_ratio * potential_grid
def calculate_deflection_component(npow, index): deflection_grid = self.axis_ratio * grid[:, index] deflection_grid *= (self.kappa_s * quad_grid( self.deflection_func, 0.0, 1.0, grid, args=(npow, self.axis_ratio, self.scale_radius), )[0]) return deflection_grid
def benchmark_pyquad(): np.random.seed(101) grid = np.random.random((10000, 3)) for repeat in range(REPEATS): res = pyquad.quad_grid(test_func, 0.0, 2.0, grid, method=INTEGRAL_METHOD, num_threads=8)[0]
def test_grid_full_column(): grid = np.random.random((100, 3)) for method in INTEGRAL_METHODS: res = np.zeros(grid.shape[0]) for i in range(res.shape[0]): res[i] = scipy.integrate.quad( function1, 0, 1, (grid[i, 0], grid[i, 1], grid[i, 2]))[0] res2 = pyquad.quad_grid(function1, 0, 1, grid, method=method)[0] assert np.abs(np.sum(res) - np.sum(res2)) < 1e-5
def calculate_deflection_component(npow, index): einstein_radius_rescaled = self.einstein_radius_rescaled deflection_grid = self.axis_ratio * grid[:, index] deflection_grid *= (einstein_radius_rescaled * quad_grid( self.deflection_func, 0.0, 1.0, grid, args=(npow, self.axis_ratio, self.slope, self.core_radius), )[0]) return deflection_grid
def calculate_deflection_component(npow, index): deflection_grid = self.axis_ratio * grid[:, index] deflection_grid *= (self.intensity * self.mass_to_light_ratio * quad_grid( self.deflection_func, 0.0, 1.0, grid, args=(npow, self.axis_ratio, self.sigma / np.sqrt(self.axis_ratio)), )[0]) return deflection_grid
def test_grid_full_column(): np.random.seed(101) grid = np.random.random((100, 3)) res = np.zeros(grid.shape[0]) for i in range(res.shape[0]): res[i] = scipy.integrate.quad( function1, 0, 1, (grid[i, 0], grid[i, 1], grid[i, 2]) )[0] res2 = pyquad.quad_grid(function1, 0, 1, grid)[0] assert np.abs(np.sum(res) - np.sum(res2)) < 1e-5
def test_grid_two_column_with_args(): np.random.seed(101) grid = np.random.random((100, 2)) res = np.zeros(grid.shape[0]) for i in range(res.shape[0]): res[i] = scipy.integrate.quad( function2, 0, 1, (grid[i, 0], grid[i, 1], 1.0), )[0] res2 = pyquad.quad_grid(function2, 0, 1, grid, args=(1.0))[0] assert np.abs(np.sum(res) - np.sum(res2)) < 1e-5
def potential_from_grid(self, grid): """ Calculate the potential at a given set of arc-second gridded coordinates. Parameters ---------- grid : aa.Grid The grid of (y,x) arc-second coordinates the deflection angles are computed on. """ potential_grid = quad_grid(self.potential_func, 0.0, 1.0, grid, args=(self.axis_ratio))[0] return self.einstein_radius_rescaled * self.axis_ratio * potential_grid
def calculate_deflection_component(npow, index): deflection_grid = 2.0 * self.kappa_s * self.axis_ratio * grid[:, index] deflection_grid *= quad_grid( self.deflection_func, 0.0, 1.0, grid, args=( npow, self.axis_ratio, minimum_log_eta, maximum_log_eta, tabulate_bins, surface_density_integral, ), epsrel=EllipticalGeneralizedNFW.epsrel, )[0] return deflection_grid
def potential_from_grid(self, grid): """ Calculate the potential at a given set of arc-second gridded coordinates. Parameters ---------- grid : aa.Grid The grid of (y,x) arc-second coordinates the deflection angles are computed on. """ potential_grid = quad_grid( self.potential_func, 0.0, 1.0, grid, args=(self.axis_ratio, self.kappa_s, self.scale_radius), epsrel=1.49e-5, )[0] return potential_grid
def potential_2d_from_grid(self, grid): """ Calculate the potential on a grid of (y,x) arc-second coordinates. Parameters ---------- grid : aa.Grid2D The grid of (y,x) arc-second coordinates the deflection angles are computed on. """ potential_grid = quad_grid( self.potential_func, 0.0, 1.0, grid, args=(self.axis_ratio, self.slope, self.core_radius), )[0] return self.einstein_radius_rescaled * self.axis_ratio * potential_grid
def test_grid_two_column_with_args(): grid = np.random.random((100, 2)) for method in INTEGRAL_METHODS: res = np.zeros(grid.shape[0]) for i in range(res.shape[0]): res[i] = scipy.integrate.quad( function2, 0, 1, (grid[i, 0], grid[i, 1], 1.0), )[0] res2 = pyquad.quad_grid(function2, 0, 1, grid, args=(1.0), method=method)[0] assert np.abs(np.sum(res) - np.sum(res2)) < 1e-5
def calculate_deflection_component(npow, index): sersic_constant = self.sersic_constant deflection_grid = self.axis_ratio * grid[:, index] deflection_grid *= (self.intensity * self.mass_to_light_ratio * quad_grid( self.deflection_func, 0.0, 1.0, grid, args=( npow, self.axis_ratio, self.sersic_index, self.effective_radius, self.mass_to_light_gradient, sersic_constant, ), )[0]) return deflection_grid
def benchmark_pyquad(): for threads in NUM_THREADS: PYQUAD_TTS[threads] = [] for problem in PROBLEM_SIZES: np.random.seed(101) grid = np.random.random((problem, 3)) for threads in NUM_THREADS: t0 = time.time() for repeat in range(REPEATS): res, _ = pyquad.quad_grid(test_func, 0.0, 2.0, grid, method=INTEGRAL_METHOD, num_threads=threads) t1 = time.time() PYQUAD_TTS[threads].append((t1 - t0) / REPEATS)
import numpy as np import scipy.integrate import pyquad def test_integrand_func(x, alpha, beta, i, j, k, l): return x * alpha * beta + i * j * k grid = np.random.random((10000000, 2)) res = np.zeros(grid.shape[0]) t0 = time.time() for i in range(res.shape[0]): res[i] = scipy.integrate.quad( test_integrand_func, 0, 1, (grid[i, 0], grid[i, 1], 1.0, 1.0, 1.0, 1.0))[0] print(time.time() - t0) t0 = time.time() res = pyquad.quad_grid(test_integrand_func, 0, 1, grid, (1.0, 1.0, 1.0, 1.0)) print(time.time() - t0) t0 = time.time() res = pyquad.quad_grid(test_integrand_func, 0, 1, grid, (1.0, 1.0, 1.0, 1.0), parallel=True) print(time.time() - t0)