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%3Ca%20href%3D%22https%3A%2F%2Fcolab.research.google.com%2Fgithub%2Fapplejxd%2Fcolaboratory%2Fblob%2Fmaster%2Falgorithm%2FAutomaticDerivative.ipynb%22%20target%3D%22_parent%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcolab.research.google.com%2Fassets%2Fcolab-badge.svg%22%20alt%3D%22Open%20In%20Colab%22%2F%3E%3C%2Fa%3E%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
90908a89961741e6e52561c0e7eb2ad3