dfun.py 文件源码

python
阅读 35 收藏 0 点赞 0 评论 0

项目:BAG_framework 作者: ucb-art 项目源码 文件源码
def _fd(self, xi, idx, delta):
        """Calculate the derivative along the given index using central finite difference.

        Parameters
        ----------
        xi : array_like
            The coordinates to evaluate, with shape (..., ndim)
        idx : int
            The index to calculate the derivative on.
        delta : float
            The finite difference step size.

        Returns
        -------
        val : np.multiarray.ndarray
            The derivatives at the given coordinates.
        """
        if idx < 0 or idx >= self.ndim:
            raise ValueError('Invalid derivative index: %d' % idx)

        xi = np.asarray(xi, dtype=float)
        if xi.shape[-1] != self.ndim:
            raise ValueError("The requested sample points xi have dimension %d, "
                             "but this interpolator has dimension %d" % (xi.shape[-1], self.ndim))

        # use broadcasting to evaluate two points at once
        xtest = np.broadcast_to(xi, (2,) + xi.shape).copy()
        xtest[0, ..., idx] += delta / 2.0
        xtest[1, ..., idx] -= delta / 2.0
        val = self(xtest)
        ans = (val[0] - val[1]) / delta  # type: np.ndarray

        if ans.size == 1 and not np.isscalar(ans):
            return ans[0]
        return ans
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号