import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20%E8%87%AA%E5%8B%95%E5%BE%AE%E5%88%86%0A%0A%20%20%20%20%E6%95%B0%E5%BC%8F%E5%87%A6%E7%90%86%E3%81%A7%E3%82%82%E6%95%B0%E5%80%A4%E5%BE%AE%E5%88%86%E3%81%A7%E3%82%82%E3%81%AA%E3%81%84%E7%AC%AC%E4%B8%89%E3%81%AE%E6%96%B9%E6%B3%95%E3%81%A8%E3%81%97%E3%81%A6%E3%80%81%E8%A8%88%E7%AE%97%E3%82%B0%E3%83%A9%E3%83%95%E3%82%92%E3%81%9F%E3%81%A9%E3%82%8A%E3%81%AA%E3%81%8C%E3%82%89%0A%20%20%20%20%E9%80%A3%E9%8E%96%E5%BE%8B%E3%82%92%E9%81%A9%E7%94%A8%E3%81%99%E3%82%8B%E8%87%AA%E5%8B%95%E5%BE%AE%E5%88%86%E3%82%92%E6%89%B1%E3%81%86%E3%80%82%E5%89%8D%E5%8D%8A%E3%81%AF%20Ceres%20Solver%20%E3%81%AE%20%60Jet%60%EF%BC%88%E4%BA%8C%E9%87%8D%E6%95%B0%E3%82%92%E5%A4%9A%E5%A4%89%E6%95%B0%E3%81%B8%0A%20%20%20%20%E6%8B%A1%E5%BC%B5%E3%81%97%E3%81%9F%E3%82%82%E3%81%AE%EF%BC%89%E3%82%92%E8%87%AA%E5%88%86%E3%81%A7%E5%AE%9F%E8%A3%85%E3%81%97%E3%81%A6%E5%89%8D%E9%80%B2%E3%83%A2%E3%83%BC%E3%83%89%E3%81%AE%E4%BB%95%E7%B5%84%E3%81%BF%E3%82%92%E8%BF%BD%E3%81%84%E3%80%81%E5%BE%8C%E5%8D%8A%E3%81%AF%20JAX%20%E3%81%AE%0A%20%20%20%20%60grad%60%20%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%80%81%E3%81%9D%E3%81%AE%E7%B5%90%E6%9E%9C%E3%81%8C%E6%95%B0%E5%80%A4%E5%BE%AE%E5%88%86%E3%81%A8%E4%B8%80%E8%87%B4%E3%81%99%E3%82%8B%E3%81%93%E3%81%A8%E3%82%92%E7%A2%BA%E3%81%8B%E3%82%81%E3%82%8B%E3%80%82%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Automatic%20Derivative%20by%20myself%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%5BAutomatic%20Derivatives%20-%20Ceres%20Solver%5D(http%3A%2F%2Fceres-solver.org%2Fautomatic_derivatives.html)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20class%20Jet%3A%0A%0A%20%20%20%20%20%20%20%20def%20__init__(self%2C%20a%3A%20float%2C%20v)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.a%20%3D%20a%0A%20%20%20%20%20%20%20%20%20%20%20%20self.v%20%3D%20np.array(v)%0A%0A%20%20%20%20%20%20%20%20def%20__str__(self)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20f'%7Bself.a%7D%2B%7Bself.v%7D'%0A%0A%20%20%20%20%20%20%20%20def%20__add__(self%2C%20other)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20isinstance(other%2C%20(int%2C%20float))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20other%20%3D%20Jet(other%2C%20np.zeros(len(self.v)))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Jet(self.a%20%2B%20other.a%2C%20self.v%20%2B%20other.v)%0A%0A%20%20%20%20%20%20%20%20def%20__sub__(self%2C%20other)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20isinstance(other%2C%20(int%2C%20float))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20other%20%3D%20Jet(other%2C%20np.zeros(len(self.v)))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Jet(self.a%20-%20other.a%2C%20self.v%20-%20other.v)%0A%0A%20%20%20%20%20%20%20%20def%20__mul__(self%2C%20other)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20isinstance(other%2C%20(int%2C%20float))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20other%20%3D%20Jet(other%2C%20np.zeros(len(self.v)))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Jet(self.a%20*%20other.a%2C%20self.a%20*%20other.v%20%2B%20self.v%20*%20other.a)%0A%0A%20%20%20%20%20%20%20%20def%20__truediv__(self%2C%20other)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20isinstance(other%2C%20(int%2C%20float))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20other%20%3D%20Jet(other%2C%20np.zeros(len(self.v)))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Jet(self.a%20%2F%20other.a%2C%20self.v%20%2F%20other.a%20-%20self.a%20*%20other.v%20%2F%20other.a%20**%202)%0A%0A%20%20%20%20%20%20%20%20def%20__pow__(self%2C%20other)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20isinstance(other%2C%20(int%2C%20float))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20other%20%3D%20Jet(other%2C%20np.zeros(len(self.v)))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Jet(self.a%20**%20other.a%2C%20other.a%20*%20self.a%20**%20(other.a%20-%201)%20*%20self.v%20%2B%20self.a%20**%20other.a%20*%20np.log(self.a)%20*%20other.v)%0A%0A%20%20%20%20%20%20%20%20def%20__radd__(self%2C%20other)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20self.__add__(other)%0A%0A%20%20%20%20%20%20%20%20def%20__rsub__(self%2C%20other)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20self.__sub__(other)%0A%0A%20%20%20%20%20%20%20%20def%20__rmul__(self%2C%20other)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20self.__mul__(other)%0A%20%20%20%20_x%20%3D%20Jet(1%2C%20(2%2C%203))%0A%20%20%20%20_y%20%3D%20Jet(4%2C%20np.array((5%2C%206)))%0A%20%20%20%20print(_x%20%2B%20_y)%0A%20%20%20%20print(2%20*%20_x)%0A%20%20%20%20return%20Jet%2C%20np%0A%0A%0A%40app.cell%0Adef%20_(Jet%2C%20np)%3A%0A%20%20%20%20def%20exp(x)%3A%0A%20%20%20%20%20%20%20%20return%20Jet(np.exp(x.a)%2C%20np.exp(x.a)%20*%20x.v)%0A%20%20%20%20print(exp(Jet(2%2C%20(3%2C%204))))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%24%24%0A%20%20%20%20(%5Cnabla%20(x%5E2%2B2y%5E2%2B3xy))(x%3D2%2C%20y%3D1)%0A%20%20%20%20%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Jet)%3A%0A%20%20%20%20def%20target_func(x%2C%20y)%3A%0A%20%20%20%20%20%20%20%20return%20x%20**%202%20%2B%202%20*%20y%20**%202%20%2B%203%20*%20x%20*%20y%0A%20%20%20%20_x%20%3D%20Jet(2%2C%20(1%2C%200))%0A%20%20%20%20%23%20x%3D2%0A%20%20%20%20_y%20%3D%20Jet(1%2C%20(0%2C%201))%0A%20%20%20%20%23%20y%3D1%0A%20%20%20%20print(target_func(_x%2C%20_y))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Automatic%20Derivative%20by%20JAX%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20From%20%5Bthe%20quickstart%5D(https%3A%2F%2Fjax.readthedocs.io%2Fen%2Flatest%2Fnotebooks%2Fquickstart.html)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20jax.numpy%20as%20jnp%0A%20%20%20%20from%20jax%20import%20grad%0A%0A%20%20%20%20return%20grad%2C%20jnp%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20%5Cleft.%5Csum_i%20(1%2Be%5E%7B-x_i%7D)%5E%7B-1%7D%5Cright%7C_%7Bx_0%3D0%2C%20x_1%3D1%2C%20x_2%3D2%7D%0A%20%20%20%20%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(grad%2C%20jnp)%3A%0A%20%20%20%20def%20sum_logistic(x)%3A%0A%20%20%20%20%20%20%20%20return%20jnp.sum(1.0%20%2F%20(1.0%20%2B%20jnp.exp(-x)))%0A%20%20%20%20x_small%20%3D%20jnp.arange(3.0)%0A%20%20%20%20derivative_fn%20%3D%20grad(sum_logistic)%0A%20%20%20%20print(derivative_fn(x_small))%0A%20%20%20%20return%20sum_logistic%2C%20x_small%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20check%20numerical%20derivative%20solution%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(jnp%2C%20sum_logistic%2C%20x_small)%3A%0A%20%20%20%20def%20first_finite_differences(f%2C%20x)%3A%0A%20%20%20%20%20%20%20%20eps%20%3D%200.001%0A%20%20%20%20%20%20%20%20return%20jnp.array(%5B(f(x%20%2B%20eps%20*%20v)%20-%20f(x%20-%20eps%20*%20v))%20%2F%20(2%20*%20eps)%20for%20v%20in%20jnp.eye(len(x))%5D)%20%20%23%20v_i%20is%20the%20tiny%20shift%20vector%20w.r.t.%20i-th%20component%0A%20%20%20%20print(first_finite_differences(sum_logistic%2C%20x_small))%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
d8b5a0407ddf96e9085f2679ab35555d