{ "cells": [ { "cell_type": "markdown", "id": "a83a0f93", "metadata": {}, "source": [ "# Laplacian, Hessian, and Jacobian\n", "\n", "All three operators are assembled from the same array of partial\n", "derivatives. For a scalar $f$ and vector map $F$,\n", "\n", "$$\n", "(\\operatorname{Hess}f)_{ij}=\\partial_i\\partial_j f,\n", "\\qquad\n", "\\Delta f=\\operatorname{tr}(\\operatorname{Hess}f),\n", "\\qquad\n", "J_F=(\\partial_jF_i)_{ij}.\n", "$$\n" ] }, { "cell_type": "markdown", "id": "b41b8969", "metadata": {}, "source": [ "## Coordinates and test fields\n", "\n", "We use $f=x^2+y^2+z^2$ and\n", "$F=(x^2,y^2,z^2)$. Both have diagonal derivative matrices, which\n", "makes the expected answers easy to inspect.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "3a52a9c1", "metadata": {}, "outputs": [], "source": [ "declare symbol x, y, z : MathValue\n", "\n", "def parameters : Vector MathValue := [| x, y, z |]\n", "def scalarField : MathValue := x^2 + y^2 + z^2\n", "def vectorField : Vector MathValue := [| x^2, y^2, z^2 |]\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "1b333b03", "metadata": {}, "outputs": [ { "data": { "text/html": [ "$\\begin{pmatrix} 2 x \\\\ 2 y \\\\ 2 z\\\\ \\end{pmatrix}$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "∂/∂ scalarField parameters\n" ] }, { "cell_type": "markdown", "id": "6ffe321e", "metadata": {}, "source": [ "## Hessian and Laplacian\n", "\n", "`generateTensor` supplies the two free matrix indices. Taking the\n", "trace of the resulting Hessian implements the Euclidean Laplacian.\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "b62505e2", "metadata": {}, "outputs": [], "source": [ "def hessian (f : MathValue) : Matrix MathValue :=\n", " generateTensor\n", " (\\[a, b] -> ∂/∂ (∂/∂ f parameters_a) parameters_b)\n", " [3, 3]\n", "\n", "def laplacian (f : MathValue) : MathValue := trace (hessian f)\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "7cdbfa34", "metadata": {}, "outputs": [ { "data": { "text/html": [ "$\\begin{pmatrix} 2 & 0 & 0 \\\\ 0 & 2 & 0 \\\\ 0 & 0 & 2 \\\\ \\end{pmatrix}$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "hessian scalarField\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "6a0c4f8d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "$6$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "laplacian scalarField\n" ] }, { "cell_type": "markdown", "id": "cb0ad2fa", "metadata": {}, "source": [ "## Jacobian matrix and determinant\n", "\n", "The same indexed construction differentiates each component of\n", "$F$. Its determinant measures the local volume scaling.\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "6fd7c37f", "metadata": {}, "outputs": [], "source": [ "def jacobian (v : Vector MathValue) : Matrix MathValue :=\n", " generateTensor\n", " (\\[a, b] -> ∂/∂ v_a parameters_b)\n", " [3, 3]\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "10abc531", "metadata": {}, "outputs": [ { "data": { "text/html": [ "$\\begin{pmatrix} 2 x & 0 & 0 \\\\ 0 & 2 y & 0 \\\\ 0 & 0 & 2 z \\\\ \\end{pmatrix}$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "jacobian vectorField\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "406a4283", "metadata": {}, "outputs": [ { "data": { "text/html": [ "$8 x y z$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "M.det (jacobian vectorField)\n" ] }, { "cell_type": "markdown", "id": "b90604d5", "metadata": {}, "source": [ "The outputs are $2I_3$, $6$, and $8xyz$, respectively. They show\n", "how tensor generation, contraction, and determinant calculation\n", "express three familiar multivariable operators in a uniform way.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Egison", "language": "egison", "name": "egison" }, "language_info": { "codemirror_mode": "egison", "file_extension": ".egi", "mimetype": "text/x-egison", "name": "egison" } }, "nbformat": 4, "nbformat_minor": 5 }