{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": "true" }, "source": [ "# Table of Contents\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Presentation\n", "\n", "I want to reproduce the symbolic (algebraic) computations done in §5.A of [L.S.'s PhD thesis](http://www.cmap.polytechnique.fr/~sacchelli/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I want to only use a free and open-source software, so I'm using [Python 3](https://docs.python.org/3) with the [Sympy](https://sympy.org) module." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPython 3.6.5\n", "IPython 6.4.0\n", "\n", "sympy 1.1.1\n", "\n", "compiler : GCC 7.3.0\n", "system : Linux\n", "release : 4.15.0-23-generic\n", "machine : x86_64\n", "processor : x86_64\n", "CPU cores : 4\n", "interpreter: 64bit\n", "Git hash : 3f62e1141f480e65f685cabe81f7a79b17bcd06d\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -m -p sympy -g" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from sympy import *" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "init_printing(use_latex='mathjax')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just a small introduction to SymPy: it works by using Python expressions on formal variables.\n", "First, we define variables, and then we can solve linear equations for example:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left ( x, \\quad y, \\quad z\\right )$$" ], "text/plain": [ "(x, y, z)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var(\"x y z\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left [ -21\\right ]$$" ], "text/plain": [ "[-21]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solve(x + 2 + 19)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left [ \\left \\{ x : - 2 y - 19\\right \\}\\right ]$$" ], "text/plain": [ "[{x: -2⋅y - 19}]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solve(x + 2*y + 19)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Computations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start by defining $b_1 > 0$ and the other variables." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "b1 = symbols(\"b1\", positive=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need a lot of variables." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "var(\"h1 h2 h3 h4 k131 k132 k141 k142 k231 k232 k241 k242 rest1 rest2 t s a b\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we can start to follow Ludovic's notebook and define the first expressions." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left[\\begin{matrix}0 & - b_{1}\\\\b_{1} & 0\\end{matrix}\\right]$$" ], "text/plain": [ "⎡0 -b₁⎤\n", "⎢ ⎥\n", "⎣b₁ 0 ⎦" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "J = Matrix([[0, -b1], [b1, 0]])\n", "J" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left[\\begin{matrix}h_{1}\\\\h_{2}\\end{matrix}\\right]$$" ], "text/plain": [ "⎡h₁⎤\n", "⎢ ⎥\n", "⎣h₂⎦" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h0 = Matrix([h1, h2])\n", "h0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\hat{h}$ is defined as $t \\mapsto \\exp(t J) . h_0$ and $\\hat{x}$ as $t \\mapsto \\int_{s=0}^{s=t} \\hat{h}(s) \\mathrm{d}s$." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left( t \\mapsto \\left[\\begin{matrix}h_{1} \\left(\\frac{1}{2} e^{i b_{1} t} + \\frac{1}{2} e^{- i b_{1} t}\\right) + h_{2} \\left(\\frac{i}{2} e^{i b_{1} t} - \\frac{i}{2} e^{- i b_{1} t}\\right)\\\\h_{1} \\left(- \\frac{i}{2} e^{i b_{1} t} + \\frac{i}{2} e^{- i b_{1} t}\\right) + h_{2} \\left(\\frac{1}{2} e^{i b_{1} t} + \\frac{1}{2} e^{- i b_{1} t}\\right)\\end{matrix}\\right] \\right)$$" ], "text/plain": [ " ⎡ ⎛ ⅈ⋅b₁⋅t -ⅈ⋅b₁⋅t⎞ ⎛ ⅈ⋅b₁⋅t -ⅈ⋅b₁⋅t⎞ ⎤\n", " ⎢ ⎜ℯ ℯ ⎟ ⎜ⅈ⋅ℯ ⅈ⋅ℯ ⎟ ⎥\n", " ⎢ h₁⋅⎜─────── + ────────⎟ + h₂⋅⎜───────── - ──────────⎟ ⎥\n", " ⎢ ⎝ 2 2 ⎠ ⎝ 2 2 ⎠ ⎥\n", "t ↦ ⎢ ⎥\n", " ⎢ ⎛ ⅈ⋅b₁⋅t -ⅈ⋅b₁⋅t⎞ ⎛ ⅈ⋅b₁⋅t -ⅈ⋅b₁⋅t⎞⎥\n", " ⎢ ⎜ ⅈ⋅ℯ ⅈ⋅ℯ ⎟ ⎜ℯ ℯ ⎟⎥\n", " ⎢h₁⋅⎜- ───────── + ──────────⎟ + h₂⋅⎜─────── + ────────⎟⎥\n", " ⎣ ⎝ 2 2 ⎠ ⎝ 2 2 ⎠⎦" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hhat = Lambda(t, (t * J).exp() * h0)\n", "hhat" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left( t \\mapsto \\left[\\begin{matrix}- \\frac{h_{2}}{b_{1}} + \\frac{1}{4 b_{1}^{2}} \\left(\\left(- 2 i b_{1} h_{1} + 2 b_{1} h_{2}\\right) e^{i b_{1} t} + \\left(2 i b_{1} h_{1} + 2 b_{1} h_{2}\\right) e^{- i b_{1} t}\\right)\\\\\\frac{h_{1}}{b_{1}} + \\frac{1}{4 b_{1}^{2}} \\left(\\left(- 2 b_{1} h_{1} - 2 i b_{1} h_{2}\\right) e^{i b_{1} t} + \\left(- 2 b_{1} h_{1} + 2 i b_{1} h_{2}\\right) e^{- i b_{1} t}\\right)\\end{matrix}\\right] \\right)$$" ], "text/plain": [ " ⎡ ⅈ⋅b₁⋅t -ⅈ⋅b₁⋅t⎤\n", " ⎢ h₂ (-2⋅ⅈ⋅b₁⋅h₁ + 2⋅b₁⋅h₂)⋅ℯ + (2⋅ⅈ⋅b₁⋅h₁ + 2⋅b₁⋅h₂)⋅ℯ ⎥\n", " ⎢- ── + ───────────────────────────────────────────────────────────────⎥\n", " ⎢ b₁ 2 ⎥\n", " ⎢ 4⋅b₁ ⎥\n", "t ↦ ⎢ ⎥\n", " ⎢ ⅈ⋅b₁⋅t -ⅈ⋅b₁⋅t ⎥\n", " ⎢h₁ (-2⋅b₁⋅h₁ - 2⋅ⅈ⋅b₁⋅h₂)⋅ℯ + (-2⋅b₁⋅h₁ + 2⋅ⅈ⋅b₁⋅h₂)⋅ℯ ⎥\n", " ⎢── + ──────────────────────────────────────────────────────────────── ⎥\n", " ⎢b₁ 2 ⎥\n", " ⎣ 4⋅b₁ ⎦" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xhat = Lambda(t, integrate(hhat(s), (s, 0, t)))\n", "xhat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\hat{z}$ is slightly more complex:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\frac{h_{1}^{2}}{4} e^{i b_{1} s} - \\frac{h_{1}^{2}}{2} + \\frac{h_{1}^{2}}{4} e^{- i b_{1} s} + \\frac{h_{2}^{2}}{4} e^{i b_{1} s} - \\frac{h_{2}^{2}}{2} + \\frac{h_{2}^{2}}{4} e^{- i b_{1} s}$$" ], "text/plain": [ " 2 ⅈ⋅b₁⋅s 2 2 -ⅈ⋅b₁⋅s 2 ⅈ⋅b₁⋅s 2 2 -ⅈ⋅b₁⋅s\n", "h₁ ⋅ℯ h₁ h₁ ⋅ℯ h₂ ⋅ℯ h₂ h₂ ⋅ℯ \n", "─────────── - ─── + ──────────── + ─────────── - ─── + ────────────\n", " 4 2 4 4 2 4 " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "integrand = simplify(b1 * (hhat(s)[0] * xhat(s)[1] - hhat(s)[1] * xhat(s)[0] )/2)\n", "integrand" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left( t \\mapsto t \\left(- \\frac{h_{1}^{2}}{2} - \\frac{h_{2}^{2}}{2}\\right) + \\frac{1}{16 b_{1}^{2}} \\left(\\left(- 4 i b_{1} h_{1}^{2} - 4 i b_{1} h_{2}^{2}\\right) e^{i b_{1} t} + \\left(4 i b_{1} h_{1}^{2} + 4 i b_{1} h_{2}^{2}\\right) e^{- i b_{1} t}\\right) \\right)$$" ], "text/plain": [ " ⎛ 2 2⎞ ⎛ 2 2⎞ ⅈ⋅b₁⋅t ⎛ 2 \n", " ⎜ h₁ h₂ ⎟ ⎝- 4⋅ⅈ⋅b₁⋅h₁ - 4⋅ⅈ⋅b₁⋅h₂ ⎠⋅ℯ + ⎝4⋅ⅈ⋅b₁⋅h₁ + 4⋅ⅈ⋅\n", "t ↦ t⋅⎜- ─── - ───⎟ + ────────────────────────────────────────────────────────\n", " ⎝ 2 2 ⎠ 2 \n", " 16⋅b₁ \n", "\n", " 2⎞ -ⅈ⋅b₁⋅t\n", "b₁⋅h₂ ⎠⋅ℯ \n", "────────────────\n", " \n", " " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zhat = Lambda(t, integrate(integrand, (s, 0, t)))\n", "zhat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we asked $b_1 > 0$, this won't get too complicated:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$- \\frac{e^{- i b_{1} t}}{4 b_{1}} \\left(h_{1}^{2} + h_{2}^{2}\\right) \\left(2 b_{1} t e^{i b_{1} t} + i e^{2 i b_{1} t} - i\\right)$$" ], "text/plain": [ " ⎛ 2 2⎞ ⎛ ⅈ⋅b₁⋅t 2⋅ⅈ⋅b₁⋅t ⎞ -ⅈ⋅b₁⋅t \n", "-⎝h₁ + h₂ ⎠⋅⎝2⋅b₁⋅t⋅ℯ + ⅈ⋅ℯ - ⅈ⎠⋅ℯ \n", "─────────────────────────────────────────────────────────\n", " 4⋅b₁ " ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factor(zhat(t))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two more expressions:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "exp21 = (3 * a * h1**2 + a * h2**2 + 2 * b * h1 * h2 + k131 * h1 * h3 + k141 * h1 * h4\n", " + k231 * h2 * h3 + k241 * h2 * h4 + rest1(h3, h4))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "exp22 = (b * h1**2 + 3 * b * h2**2 + 2 * a * h1 * h2 + k132 * h1 * h3 + k142 * h1 * h4\n", " + k232 * h2 * h3 + k242 * h2 * h4 + rest2(h3, h4))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Both terms `rest1` and `rest2` are not important, they only depend on $h_3$ and $h_4$ and will vanish as soon as we only differentiate with respect to $h_1$ and $h_2$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So far so good. Next cell:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left[\\begin{matrix}\\frac{2 \\pi}{b_{1}} h_{1}\\\\\\frac{2 \\pi}{b_{1}} h_{2}\\end{matrix}\\right]$$" ], "text/plain": [ "⎡2⋅π⋅h₁⎤\n", "⎢──────⎥\n", "⎢ b₁ ⎥\n", "⎢ ⎥\n", "⎢2⋅π⋅h₂⎥\n", "⎢──────⎥\n", "⎣ b₁ ⎦" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C10 = 2 * (pi / b1) * Matrix([h1, h2])\n", "C10" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left[\\begin{matrix}6 a h_{1} + 2 b h_{2} + h_{3} k_{131} + h_{4} k_{141} & 2 a h_{2} + 2 b h_{1} + h_{3} k_{231} + h_{4} k_{241}\\\\2 a h_{2} + 2 b h_{1} + h_{3} k_{132} + h_{4} k_{142} & 2 a h_{1} + 6 b h_{2} + h_{3} k_{232} + h_{4} k_{242}\\end{matrix}\\right]$$" ], "text/plain": [ "⎡6⋅a⋅h₁ + 2⋅b⋅h₂ + h₃⋅k₁₃₁ + h₄⋅k₁₄₁ 2⋅a⋅h₂ + 2⋅b⋅h₁ + h₃⋅k₂₃₁ + h₄⋅k₂₄₁⎤\n", "⎢ ⎥\n", "⎣2⋅a⋅h₂ + 2⋅b⋅h₁ + h₃⋅k₁₃₂ + h₄⋅k₁₄₂ 2⋅a⋅h₁ + 6⋅b⋅h₂ + h₃⋅k₂₃₂ + h₄⋅k₂₄₂⎦" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A0 = simplify(Matrix([\n", " [diff(exp21, h1), diff(exp21, h2)],\n", " [diff(exp22, h1), diff(exp22, h2)],\n", "]))\n", "A0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`rest1` and `rest2` already vanished." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left[\\begin{matrix}6 a h_{1} + 2 b h_{2} + h_{3} k_{131} + h_{4} k_{141} & 2 a h_{2} + 2 b h_{1} + h_{3} k_{231} + h_{4} k_{241} & \\frac{2 \\pi}{b_{1}} h_{1}\\\\2 a h_{2} + 2 b h_{1} + h_{3} k_{132} + h_{4} k_{142} & 2 a h_{1} + 6 b h_{2} + h_{3} k_{232} + h_{4} k_{242} & \\frac{2 \\pi}{b_{1}} h_{2}\\\\- \\frac{2 \\pi}{b_{1}} h_{1} & - \\frac{2 \\pi}{b_{1}} h_{2} & 0\\end{matrix}\\right]$$" ], "text/plain": [ "⎡ 2⋅π\n", "⎢6⋅a⋅h₁ + 2⋅b⋅h₂ + h₃⋅k₁₃₁ + h₄⋅k₁₄₁ 2⋅a⋅h₂ + 2⋅b⋅h₁ + h₃⋅k₂₃₁ + h₄⋅k₂₄₁ ───\n", "⎢ b\n", "⎢ \n", "⎢ 2⋅π\n", "⎢2⋅a⋅h₂ + 2⋅b⋅h₁ + h₃⋅k₁₃₂ + h₄⋅k₁₄₂ 2⋅a⋅h₁ + 6⋅b⋅h₂ + h₃⋅k₂₃₂ + h₄⋅k₂₄₂ ───\n", "⎢ b\n", "⎢ \n", "⎢ -2⋅π⋅h₁ -2⋅π⋅h₂ \n", "⎢ ──────── ──────── 0\n", "⎣ b₁ b₁ \n", "\n", "⋅h₁⎤\n", "───⎥\n", "₁ ⎥\n", " ⎥\n", "⋅h₂⎥\n", "───⎥\n", "₁ ⎥\n", " ⎥\n", " ⎥\n", " ⎥\n", " ⎦" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "jacobian = simplify(Matrix([\n", " [diff(exp21, h1), diff(exp21, h2), C10[0]],\n", " [diff(exp22, h1), diff(exp22, h2), C10[1]],\n", " [diff(zhat(2 * pi / b1), h1), diff(zhat(2 * pi / b1), h2), 0],\n", "]))\n", "jacobian" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need one more variable to solve an equation." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$dt$$" ], "text/plain": [ "dt" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var('dt')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "tc = factor(solve(Equality((jacobian + Matrix([[dt,0,0], [0,dt,0], [0,0,0]])).det(), 0), dt)[0])" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$- \\frac{1}{h_{1}^{2} + h_{2}^{2}} \\left(2 a h_{1}^{3} + 2 a h_{1} h_{2}^{2} + 2 b h_{1}^{2} h_{2} + 2 b h_{2}^{3} + h_{1}^{2} h_{3} k_{232} + h_{1}^{2} h_{4} k_{242} - h_{1} h_{2} h_{3} k_{132} - h_{1} h_{2} h_{3} k_{231} - h_{1} h_{2} h_{4} k_{142} - h_{1} h_{2} h_{4} k_{241} + h_{2}^{2} h_{3} k_{131} + h_{2}^{2} h_{4} k_{141}\\right)$$" ], "text/plain": [ " ⎛ 3 2 2 3 2 2 \n", "-⎝2⋅a⋅h₁ + 2⋅a⋅h₁⋅h₂ + 2⋅b⋅h₁ ⋅h₂ + 2⋅b⋅h₂ + h₁ ⋅h₃⋅k₂₃₂ + h₁ ⋅h₄⋅k₂₄₂ - h₁\n", "──────────────────────────────────────────────────────────────────────────────\n", " \n", " \n", "\n", " 2 \n", "⋅h₂⋅h₃⋅k₁₃₂ - h₁⋅h₂⋅h₃⋅k₂₃₁ - h₁⋅h₂⋅h₄⋅k₁₄₂ - h₁⋅h₂⋅h₄⋅k₂₄₁ + h₂ ⋅h₃⋅k₁₃₁ + h₂\n", "──────────────────────────────────────────────────────────────────────────────\n", " 2 2 \n", " h₁ + h₂ \n", "\n", "2 ⎞ \n", " ⋅h₄⋅k₁₄₁⎠ \n", "───────────\n", " \n", " " ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I can compare by copying the result from the document:" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "tc2 = (-1 / (h1**2 + h2**2)) * (\n", " 2 * a * h1**3\n", " + 6 * a * h1**2 * h2\n", " - 4 * b * h1**2 * h2\n", " + 2 * a * h1 * h2**2\n", " + 2 * b * h2**3\n", " + h2**2 * h3 * k131\n", " - h1 * h2 * h3 * k132\n", " + h2**2 * h4 * k141\n", " - h1 * h2 * h4 * k142\n", " - h1 * h2 * h3 * k231\n", " + h1**2 * h3 * k232\n", " - h1 * h2 * h4 * k241\n", " + h1**2 * h4 * k242 )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Drat, they are not equal! We might have a mistake somewhere, even though I just CAN'T find it!" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\frac{6 h_{1}^{2} h_{2} \\left(a - b\\right)}{h_{1}^{2} + h_{2}^{2}}$$" ], "text/plain": [ " 2 \n", "6⋅h₁ ⋅h₂⋅(a - b)\n", "────────────────\n", " 2 2 \n", " h₁ + h₂ " ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simplify(tc - tc2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's use the one from Ludovic's notebook." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "tc = tc2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next cell." ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "A12 = simplify(A0 + Matrix([[tc, 0], [0, tc]]))" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left ( u_{1}, \\quad u_{2}, \\quad u_{5}\\right )$$" ], "text/plain": [ "(u₁, u₂, u₅)" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var(\"u1 u2 u5\")" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "Psi = Lambda((u1, u2, u5), simplify(\n", " u5 * Matrix(A12.dot(Matrix([h1, h2]))).dot(Matrix([h2, -h1])) + 2 * pi / b1 * ( h1**2 + h2**2) * (h1 * u2 - h2 * u1)\n", "))" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$- \\frac{1}{b_{1}} \\left(- 2 a b_{1} h_{1}^{2} h_{2} u_{5} - 2 a b_{1} h_{2}^{3} u_{5} + 2 b b_{1} h_{1}^{3} u_{5} + 2 b b_{1} h_{1} h_{2}^{2} u_{5} + b_{1} h_{1}^{2} h_{3} k_{132} u_{5} + b_{1} h_{1}^{2} h_{4} k_{142} u_{5} - b_{1} h_{1} h_{2} h_{3} k_{131} u_{5} + b_{1} h_{1} h_{2} h_{3} k_{232} u_{5} - b_{1} h_{1} h_{2} h_{4} k_{141} u_{5} + b_{1} h_{1} h_{2} h_{4} k_{242} u_{5} - b_{1} h_{2}^{2} h_{3} k_{231} u_{5} - b_{1} h_{2}^{2} h_{4} k_{241} u_{5} - 2 \\pi h_{1}^{3} u_{2} + 2 \\pi h_{1}^{2} h_{2} u_{1} - 2 \\pi h_{1} h_{2}^{2} u_{2} + 2 \\pi h_{2}^{3} u_{1}\\right)$$" ], "text/plain": [ " ⎛ 2 3 3 2 \n", "-⎝- 2⋅a⋅b₁⋅h₁ ⋅h₂⋅u₅ - 2⋅a⋅b₁⋅h₂ ⋅u₅ + 2⋅b⋅b₁⋅h₁ ⋅u₅ + 2⋅b⋅b₁⋅h₁⋅h₂ ⋅u₅ + b₁⋅h\n", "──────────────────────────────────────────────────────────────────────────────\n", " \n", "\n", " 2 2 \n", "₁ ⋅h₃⋅k₁₃₂⋅u₅ + b₁⋅h₁ ⋅h₄⋅k₁₄₂⋅u₅ - b₁⋅h₁⋅h₂⋅h₃⋅k₁₃₁⋅u₅ + b₁⋅h₁⋅h₂⋅h₃⋅k₂₃₂⋅u₅ \n", "──────────────────────────────────────────────────────────────────────────────\n", " b₁ \n", "\n", " 2 2 \n", "- b₁⋅h₁⋅h₂⋅h₄⋅k₁₄₁⋅u₅ + b₁⋅h₁⋅h₂⋅h₄⋅k₂₄₂⋅u₅ - b₁⋅h₂ ⋅h₃⋅k₂₃₁⋅u₅ - b₁⋅h₂ ⋅h₄⋅k₂\n", "──────────────────────────────────────────────────────────────────────────────\n", " \n", "\n", " 3 2 2 3 ⎞ \n", "₄₁⋅u₅ - 2⋅π⋅h₁ ⋅u₂ + 2⋅π⋅h₁ ⋅h₂⋅u₁ - 2⋅π⋅h₁⋅h₂ ⋅u₂ + 2⋅π⋅h₂ ⋅u₁⎠ \n", "─────────────────────────────────────────────────────────────────\n", " " ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_psi = factor(simplify(Psi(u1, u2, u5)))\n", "my_psi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's compare with the value from Ludovic's notebook:" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "his_psi = (1 / b1) * (\n", " 2 * h1**3 * (pi * u2 - b * b1 * u5 ) -\n", " h1**2 * (2 * h2 * pi * u1 - 2 * a * b1 * h2 * u5 + b1 * h3 * k132 * u5 +\n", " b1 * h4 * k142 * u5 ) +\n", " h2**2 * ( -2 * h2 * pi * u1 + 2 * a * b1 * h2 * u5 + b1 * h3 * k231 * u5 +\n", " b1 * h4 * k241 * u5 ) +\n", " h1 * h2 * (b1 * (h3 * k131 + h4 * k141 - h3 * k232 - h4 * k242) * u5 +\n", " 2 * h2 * (pi * u2 - b * b1 * u5))\n", ")" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$0$$" ], "text/plain": [ "0" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simplify(my_psi - his_psi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ok, we have the same result so far! Great!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next cell." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\nu_{0}$$" ], "text/plain": [ "ν₀" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var(\"nu0\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here again, Sympy fails to solve the equation. It can solve on both lines separately, but cannot find a solution that satisfies both lines." ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "s1 = solve(Eq((Matrix(A12.dot(Matrix([-h2, h1]))) + nu0 * C10)[0]), nu0)[0]" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "s2 = solve(Eq((Matrix(A12.dot(Matrix([-h2, h1]))) + nu0 * C10)[1]), nu0)[0]" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left [ \\left \\{ a : b\\right \\}, \\quad \\left \\{ h_{1} : 0\\right \\}\\right ]$$" ], "text/plain": [ "[{a: b}, {h₁: 0}]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solve(Eq(s1, s2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is not possible to impose these constraints. And Sympy fails to solve both equation simultaneously:" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left [ \\right ]$$" ], "text/plain": [ "[]" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solve(Eq(Matrix(A12.dot(Matrix([-h2, h1]))) + nu0 * C10, Matrix([0,0])), nu0)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "nu = simplify(solve(Eq(Matrix(A12.dot(Matrix([-h2, h1]))) + nu0 * C10, Matrix([0,0])), nu0))\n", "if nu == []:\n", " nu = var(\"nu\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can continue by using a formal variable for $\\nu$ (`nu`)." ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "v = Matrix([nu, -h2, h1])" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$\\left[\\begin{matrix}\\nu\\\\- h_{2}\\\\h_{1}\\end{matrix}\\right]$$" ], "text/plain": [ "⎡ ν ⎤\n", "⎢ ⎥\n", "⎢-h₂⎥\n", "⎢ ⎥\n", "⎣h₁ ⎦" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's finish." ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "var(\"eta f F\");" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "d = [\n", " lambda F: -eta**2 * diff(F, eta) + eta * t * diff(F, t),\n", " lambda F: diff(F, h1),\n", " lambda F: diff(F, h2)\n", "]" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[