dfun.py 文件源码

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

项目:BAG_framework 作者: ucb-art 项目源码 文件源码
def _fd_jacobian(self, xi, delta_list):
        """Calculate the Jacobian matrix using central finite difference.

        Parameters
        ----------
        xi : array_like
            The coordinates to evaluate, with shape (..., ndim)
        delta_list : List[float]
            list of finite difference step sizes for each input.

        Returns
        -------
        val : np.multiarray.ndarray
            The Jacobian matrices at the given coordinates.
        """
        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 all points at once
        xtest = np.broadcast_to(xi, (2 * self.ndim,) + xi.shape).copy()
        for idx, delta in enumerate(delta_list):
            xtest[2 * idx, ..., idx] += delta / 2.0
            xtest[2 * idx + 1, ..., idx] -= delta / 2.0

        val = self(xtest)
        ans = np.empty(xi.shape)
        for idx, delta in enumerate(delta_list):
            ans[..., idx] = (val[2 * idx, ...] - val[2 * idx + 1, ...]) / delta
        return ans
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号