def _eval_fd(self, expr): expr = getattr(expr, 'evaluate', expr) if self.side in [left, right] and self.deriv_order == 1: res = first_derivative(expr, self.dims[0], self.fd_order, side=self.side, matvec=self.transpose, x0=self.x0) elif len(self.dims) > 1: res = cross_derivative(expr, self.dims, self.fd_order, self.deriv_order, matvec=self.transpose, x0=self.x0) else: res = generic_derivative(expr, *self.dims, self.fd_order, self.deriv_order, matvec=self.transpose, x0=self.x0) for e in self._subs: res = res.xreplace(e) return res
def _eval_fd(self, expr): """ Evaluate finite difference approximation of the Derivative. Evaluation is carried out via the following four steps: - 1: Evaluate derivatives within the expression. For example given `f.dx * g`, `f.dx` will be evaluated first. - 2: Evaluate the finite difference for the (new) expression. - 3: Evaluate remaining terms (as `g` may need to be evaluated at a different point). - 4: Apply substitutions. """ # Step 1: Evaluate derivatives within expression expr = getattr(expr, '_eval_deriv', expr) # Step 2: Evaluate FD of the new expression if self.side is not None and self.deriv_order == 1: res = first_derivative(expr, self.dims[0], self.fd_order, side=self.side, matvec=self.transpose, x0=self.x0) elif len(self.dims) > 1: res = cross_derivative(expr, self.dims, self.fd_order, self.deriv_order, matvec=self.transpose, x0=self.x0) else: res = generic_derivative(expr, *self.dims, self.fd_order, self.deriv_order, matvec=self.transpose, x0=self.x0) # Step 3: Evaluate remaining part of expression res = res.evaluate # Step 4: Apply substitution for e in self._subs: res = res.xreplace(e) return res
def _eval_fd(self, expr): """ Evaluate finite difference approximation of the Derivative. Evaluation is carried out via the following four steps: - 1: Evaluate derivatives within the expression. For example given `f.dx * g`, `f.dx` will be evaluated first. - 2: Evaluate the finite difference for the (new) expression. - 3: Evaluate remaining terms (as `g` may need to be evaluated at a different point). - 4: Apply substitutions. - 5: Cast to an object of type `EvalDiffDerivative` so that we know the argument stems from a `Derivative. This may be useful for later compilation passes. """ # Step 1: Evaluate derivatives within expression expr = getattr(expr, '_eval_deriv', expr) # Step 2: Evaluate FD of the new expression if self.side is not None and self.deriv_order == 1: res = first_derivative(expr, self.dims[0], self.fd_order, side=self.side, matvec=self.transpose, x0=self.x0) elif len(self.dims) > 1: res = cross_derivative(expr, self.dims, self.fd_order, self.deriv_order, matvec=self.transpose, x0=self.x0) else: res = generic_derivative(expr, *self.dims, self.fd_order, self.deriv_order, matvec=self.transpose, x0=self.x0) # Step 3: Evaluate remaining part of expression res = res.evaluate # Step 4: Apply substitution for e in self._subs: res = res.xreplace(e) # Step 5: Cast to EvaluatedDerivative assert res.is_Add res = EvalDiffDerivative(*res.args, evaluate=False) return res
def evaluate(self): expr = getattr(self.expr, 'evaluate', self.expr) if self.side in [left, right] and self.deriv_order == 1: res = first_derivative(expr, self.dims[0], self.fd_order, side=self.side, matvec=self.transpose) elif len(self.dims) > 1: res = cross_derivative(expr, self.dims, self.fd_order, self.deriv_order, matvec=self.transpose, stagger=self.stagger) else: res = generic_derivative(expr, *self.dims, self.fd_order, self.deriv_order, stagger=self.stagger[0], matvec=self.transpose) for e in self._eval_at: res = res.xreplace(e) return res
def _eval_fd(self, expr): """ Evaluate the finite-difference approximation of the Derivative. Evaluation is carried out via the following three steps: - 1: Evaluate derivatives within the expression. For example given `f.dx * g`, `f.dx` will be evaluated first. - 2: Evaluate the finite difference for the (new) expression. This in turn is a two-step procedure, for Functions that may may need to be evaluated at a different point due to e.g. a shited derivative. - 3: Apply substitutions. """ # Step 1: Evaluate derivatives within expression try: expr = expr._eval_deriv except AttributeError: pass # Step 2: Evaluate FD of the new expression if self.side is not None and self.deriv_order == 1: res = first_derivative(expr, self.dims[0], self.fd_order, side=self.side, matvec=self.transpose, x0=self.x0) elif len(self.dims) > 1: res = cross_derivative(expr, self.dims, self.fd_order, self.deriv_order, matvec=self.transpose, x0=self.x0) else: res = generic_derivative(expr, *self.dims, self.fd_order, self.deriv_order, matvec=self.transpose, x0=self.x0) # Step 3: Apply substitutions for e in self._ppsubs: res = res.xreplace(e) return res