def coefficients_from_source(self, avec, bvec, rscale, sac=None): from sumpy.kernel import DirectionalSourceDerivative kernel = self.kernel from sumpy.tools import mi_power, mi_factorial if not self.use_rscale: rscale = 1 if isinstance(kernel, DirectionalSourceDerivative): from sumpy.symbolic import make_sym_vector dir_vecs = [] tmp_kernel = kernel while isinstance(tmp_kernel, DirectionalSourceDerivative): dir_vecs.append( make_sym_vector(tmp_kernel.dir_vec_name, kernel.dim)) tmp_kernel = tmp_kernel.inner_kernel if kernel.get_base_kernel() is not tmp_kernel: raise NotImplementedError("Unknown kernel wrapper.") nderivs = len(dir_vecs) coeff_identifiers = self.get_full_coefficient_identifiers() result = [0] * len(coeff_identifiers) for i, mi in enumerate(coeff_identifiers): # One source derivative is the dot product of the gradient and # directional vector. # For multiple derivatives, gradient of gradients is taken. # For eg: in 3D, 2 source derivatives gives 9 terms and # cartesian_product below enumerates these 9 terms. for deriv_terms in cartesian_product(*[range(kernel.dim)] * nderivs): prod = 1 derivative_mi = list(mi) for nderivative, deriv_dim in enumerate(deriv_terms): prod *= -derivative_mi[deriv_dim] prod *= dir_vecs[nderivative][deriv_dim] derivative_mi[deriv_dim] -= 1 if any(v < 0 for v in derivative_mi): continue result[i] += mi_power(avec, derivative_mi) * prod for i, mi in enumerate(coeff_identifiers): result[i] /= (mi_factorial(mi) * rscale**sum(mi)) else: avec = [sym.UnevaluatedExpr(a * rscale**-1) for a in avec] result = [ mi_power(avec, mi) / mi_factorial(mi) for mi in self.get_full_coefficient_identifiers() ] return (self.expansion_terms_wrangler. get_stored_mpole_coefficients_from_full(result, rscale, sac=sac))
def test_cartesian_product(): from pytools import cartesian_product expected_outputs = [ (0, 2, 4), (0, 2, 5), (0, 3, 4), (0, 3, 5), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), ] for i, output in enumerate(cartesian_product([0, 1], [2, 3], [4, 5])): assert output == expected_outputs[i]
import rlcompleter # noqa: F401 HAVE_READLINE = True except ImportError: HAVE_READLINE = False from pytools import cartesian_product, Record class PlotStyle(Record): pass PLOT_STYLES = [ PlotStyle(dashes=dashes, color=color) for dashes, color in cartesian_product( [(), (12, 2), (4, 2), (2, 2), (2, 8)], ["blue", "green", "red", "magenta", "cyan"], ) ] class RunDB(object): def __init__(self, db, interactive): self.db = db self.interactive = interactive self.rank_agg_tables = set() def q(self, qry, *extra_args): return self.db.execute(self.mangle_sql(qry), extra_args) def mangle_sql(self, qry): return qry
HAVE_READLINE = True except ImportError: HAVE_READLINE = False from pytools import cartesian_product, Record class PlotStyle(Record): pass PLOT_STYLES = [ PlotStyle(dashes=dashes, color=color) for dashes, color in cartesian_product( [(), (12, 2), (4, 2), (2, 2), (2, 8)], ["blue", "green", "red", "magenta", "cyan"], )] class RunDB(object): def __init__(self, db, interactive): self.db = db self.interactive = interactive self.rank_agg_tables = set() def q(self, qry, *extra_args): return self.db.execute(self.mangle_sql(qry), extra_args) def mangle_sql(self, qry): return qry