def _compile(self, expression, variables): import pymbolic.primitives as primi self._Expression = expression self._Variables = [primi.make_variable(v) for v in variables] ctx = self.context().copy() try: import numpy except ImportError: pass else: ctx["numpy"] = numpy from pymbolic.mapper.dependency import DependencyMapper used_variables = DependencyMapper( composite_leaves=False)(self._Expression) used_variables -= set(self._Variables) used_variables -= set(pymbolic.var(key) for key in list(ctx.keys())) used_variables = list(used_variables) used_variables.sort() all_variables = self._Variables + used_variables expr_s = CompileMapper()(self._Expression, PREC_NONE) func_s = "lambda %s: %s" % (",".join(str(v) for v in all_variables), expr_s) self._code = eval(func_s, ctx)
def _compile(self, expression, variables): import pymbolic.primitives as primi self._Expression = expression self._Variables = [primi.make_variable(v) for v in variables] ctx = self.context().copy() try: import numpy except ImportError: pass else: ctx["numpy"] = numpy from pymbolic.mapper.dependency import DependencyMapper used_variables = DependencyMapper(composite_leaves=False)( self._Expression) used_variables -= set(self._Variables) used_variables -= set(pymbolic.var(key) for key in list(ctx.keys())) used_variables = list(used_variables) used_variables.sort() all_variables = self._Variables + used_variables expr_s = CompileMapper()(self._Expression, PREC_NONE) func_s = "lambda %s: %s" % (",".join(str(v) for v in all_variables), expr_s) self._code = eval(func_s, ctx)
def differentiate(expression, variable, func_mapper=map_math_functions_by_name, allowed_nonsmoothness="none"): if not isinstance(variable, (primitives.Variable, primitives.Subscript)): variable = primitives.make_variable(variable) return DifferentiationMapper( variable, func_mapper, allowed_nonsmoothness=allowed_nonsmoothness)(expression)
def __init__(self, expression, variables=[]): """ :arg variables: The first arguments (as strings or :class:`pymbolic.primitives.Variable` instances) to be used for the compiled function. All variables used by the expression and not present here are added in alphabetical order. """ import pymbolic.primitives as primi self._Expression = expression self._Variables = [primi.make_variable(v) for v in variables] self._compile()
def diff(f, *x, allowed_nonsmoothness="discontinuous"): """ A differentiator which computes :math:`\\partial f / \\partial x` and understands :class:`Field`\\ s. If ``x`` is one of ``t``, ``x``, ``y``, or ``z`` and ``f`` is a :class:`DynamicField`, the corresponding derivative :class:`Field` is returned. Examples: .. doctest:: >>> f = ps.DynamicField("f") >>> print(ps.diff(f**3, f)) 3*f**2 >>> print(ps.diff(f**3, f, f)) 3*2*f >>> print(ps.diff(f**3, "t")) 3*f**2*dfdt >>> print(ps.diff(f**3, f, "t")) 3*2*f*dfdt >>> print(ps.diff(f + 2, "x")) dfdx[0] :arg f: A :mod:`pymbolic` expression to be differentiated. :arg x: A :class:`pymbolic.primitives.Expression` or a string to be parsed (or multiple thereof). If multiple positional arguments are provided, derivatives are taken with respect to each in order. (See the examples above.) """ if len(x) > 1: return diff(diff(f, x[0]), *x[1:]) else: differentiator = FieldDifferentiationMapper( pp.make_variable(x[0]), allowed_nonsmoothness=allowed_nonsmoothness) return differentiator(f)
def differentiate(expression, variable, func_mapper=map_math_functions_by_name): if not isinstance(variable, (primitives.Variable, primitives.Subscript)): variable = primitives.make_variable(variable) return DifferentiationMapper(variable, func_mapper)(expression)
def __init__(self, expression, variables = []): import pymbolic.primitives as primi self._Expression = expression self._Variables = [primi.make_variable(v) for v in variables] self._compile()
def __init__(self, expression, variables=[]): import pymbolic.primitives as primi self._Expression = expression self._Variables = [primi.make_variable(v) for v in variables] self._compile()