{
	"cell_id": 10135449329227921550,
	"cells": [
		{
			"cell_id": 3989415718163890231,
			"cell_origin": "client",
			"cell_type": "latex",
			"cells": [
				{
					"cell_id": 3791214211892782773,
					"cell_origin": "client",
					"cell_type": "latex_view",
					"source": "\\package{cdb.numeric.integrate}{Functions for numeric integration and numeric solutions to differential equations}\n\nA collection of wrappers for some of the numeric functionality of the scipy library in order to accept cadabra Ex objects"
				}
			],
			"hidden": true,
			"source": "\\package{cdb.numeric.integrate}{Functions for numeric integration and numeric solutions to differential equations}\n\nA collection of wrappers for some of the numeric functionality of the scipy library in order to accept cadabra Ex objects"
		},
		{
			"cell_id": 8362589260562156834,
			"cell_origin": "client",
			"cell_type": "input",
			"source": "import numpy\nfrom scipy.integrate import odeint\nfrom cdb.numeric.evaluate import lambdify, _create_sublist"
		},
		{
			"cell_id": 6052509396776782195,
			"cell_origin": "client",
			"cell_type": "latex",
			"cells": [
				{
					"cell_id": 11317838269335231990,
					"cell_origin": "client",
					"cell_type": "latex_view",
					"source": "\\algorithm{integrate_ode(eqns: Ex|Iterable[Ex], variables: Ex|Iterable[Ex], initial_values: List[float], \nendpoint: float, delta: float, constants: Ex|dict) -> List}{Numerically integrate a system of ODEs over a range of values}\n\\begin{description}\n\\item \\verb|eqns| \\\\\n\tList of expressions representing first order derivatives with respect to a common variable. The expression\n\tshould not contain the derivative, so e.g. use $x - \\frac{1}{2}xy$ instead of $\\partial_{t}{x} = x - \\frac{1}{2}xy$.\n\tCan be a comma-separated in an Ex object, or any iterable of Ex objects.\n\\item \\verb|variables| \\\\\n\tList of variables, starting with the derivative variable and then each variable in the order that its derivative appears\n\tin \\verb|eqns|, so $(\\frac{dx}{dt}, \\frac{dy}{dt}, \\frac{dz}{dt})$ would have variables $(t, x, y, z)$\n\\item \\verb|initial_values| \\\\\n\tThe initial values of each variable in \\verb|variables| as a floating point number\n\\item \\verb|endpoint| \\\\\n\tThe value of the derivative variable at which to finish\n\\item \\verb|delta| \\\\\n\tThe amount to increase the derivative variable at each iteration\n\\item \\verb|constants| \\\\\n\tEx or dictionary object with a list of substitutions of constants to be performed before the numeric integration\n\\end{description}"
				}
			],
			"hidden": true,
			"source": "\\algorithm{integrate_ode(eqns: Ex|Iterable[Ex], variables: Ex|Iterable[Ex], initial_values: List[float], \nendpoint: float, delta: float, constants: Ex|dict) -> List}{Numerically integrate a system of ODEs over a range of values}\n\\begin{description}\n\\item \\verb|eqns| \\\\\n\tList of expressions representing first order derivatives with respect to a common variable. The expression\n\tshould not contain the derivative, so e.g. use $x - \\frac{1}{2}xy$ instead of $\\partial_{t}{x} = x - \\frac{1}{2}xy$.\n\tCan be a comma-separated in an Ex object, or any iterable of Ex objects.\n\\item \\verb|variables| \\\\\n\tList of variables, starting with the derivative variable and then each variable in the order that its derivative appears\n\tin \\verb|eqns|, so $(\\frac{dx}{dt}, \\frac{dy}{dt}, \\frac{dz}{dt})$ would have variables $(t, x, y, z)$\n\\item \\verb|initial_values| \\\\\n\tThe initial values of each variable in \\verb|variables| as a floating point number\n\\item \\verb|endpoint| \\\\\n\tThe value of the derivative variable at which to finish\n\\item \\verb|delta| \\\\\n\tThe amount to increase the derivative variable at each iteration\n\\item \\verb|constants| \\\\\n\tEx or dictionary object with a list of substitutions of constants to be performed before the numeric integration\n\\end{description}"
		},
		{
			"cell_id": 17270037226329397630,
			"cell_origin": "client",
			"cell_type": "input",
			"source": "def integrate_ode(eqns, variables, initial_values, endpoint, step=None, constants=None):\n\t# Collect list of equations and lambdify them\n\tif isinstance(eqns, Ex):\n\t\tif eqns.head() == r\"\\comma\":\n\t\t\tfuncs = [lambdify(eqn.ex(), variables, constants) for eqn in eqns.top().children()]\n\t\telse:\n\t\t\tfuncs = [lambdify(eqns, variables, constants)]\n\telse:\n\t\tfuncs = [lambdify(eqn, variables, constants) for eqn in eqns]\n\t\n\t# Master function\n\tf = lambda xs, t, params: [func(t, *xs) for func in funcs]\n\n\t# Integration range\n\tnpoints = 1000 if step is None else int((1 + endpoint - initial_values[0]) / step)\n\tpoints = numpy.linspace(initial_values[0], endpoint, npoints)\n\n\t# Rebroadcast so that each return list corresponds to a variable\n\tsol = odeint(f, initial_values[1:], points, ((),))\n\treturn [points] + [sol[:,i] for i in range(len(variables)-1)]"
		},
		{
			"cell_id": 12331971389017128553,
			"cell_origin": "client",
			"cell_type": "input",
			"ignore_on_import": true,
			"source": "{t}::Coordinate.\ndx := x-A*x*y.\ndy := -B*y + C*x*y.\n#sols = integrate_ode((dx, dy), $t, x, y$, (0, 1, 1), 10, 0.001, $A->1/2, B->3/4, C->1/4$)\nsols = integrate_ode($@(dx), @(dy)$, $t, x, y$, (0, 1, 1), 10, 0.001, {\"A\": 0.5, \"B\": 3/4, \"C\": 0.25})"
		},
		{
			"cell_id": 10058019511408460871,
			"cell_origin": "client",
			"cell_type": "input",
			"cells": [
				{
					"cell_id": 18371720257697375934,
					"cell_origin": "server",
					"cell_type": "image_png",
					"source": "[encoded data, 42176 characters]"
				}
			],
			"ignore_on_import": true,
			"source": "import matplotlib.pyplot as plt\nfig, axes = plt.subplots(2,1)\naxes[0].plot(sols[0], sols[1])\naxes[0].plot(sols[0], sols[2])\naxes[1].plot(sols[1], sols[2])\nfig;"
		},
		{
			"cell_id": 4262269971924348028,
			"cell_origin": "client",
			"cell_type": "input",
			"cells": [
				{
					"cell_id": 1287486217584996925,
					"cell_origin": "server",
					"cell_type": "latex_view",
					"cells": [
						{
							"cell_id": 6643470899229452613,
							"cell_origin": "server",
							"cell_type": "input_form",
							"source": "Raux"
						}
					],
					"source": "\\begin{dmath*}{}R_{aux}\\end{dmath*}"
				},
				{
					"cell_id": 17755464265255366914,
					"cell_origin": "server",
					"cell_type": "latex_view",
					"cells": [
						{
							"cell_id": 8501208413624250549,
							"cell_origin": "server",
							"cell_type": "input_form",
							"source": "-(4(M)**2 Raux + 2(r)**2 Raux + 2(laux R-3r Raux) M + ((\\omega)**2 (r)**3-laux r) R) (4(M)**2 r-4M (r)**2 + (r)**3)**(-1)"
						}
					],
					"source": "\\begin{dmath*}{}-\\left(4{M}^{2} R_{aux}+2{r}^{2} R_{aux}+2\\left(\\lambda_{aux} R-3r R_{aux}\\right) M+\\left({\\omega}^{2} {r}^{3}-\\lambda_{aux} r\\right) R\\right) {\\left(4{M}^{2} r-4M {r}^{2}+{r}^{3}\\right)}^{-1}\\end{dmath*}"
				}
			],
			"ignore_on_import": true,
			"source": "Raux::LaTeXForm(\"R_{aux}\")\nlaux::LaTeXForm(\"\\lambda_{aux}\")\ndR := Raux;\ndRaux := -(4* M**2* Raux + 2* r**2* Raux + 2*( laux * R - 3* r * Raux ) * M + (\n\t\\omega**2* r**3 - laux * r ) * R ) /(4* M**2* r - 4* M * r**2 + r**3);\nsols = integrate_ode((dR, dRaux), $r, R, Raux$, (0.3, 1, 0.5), 100, 0.01, $M->0.1, \\omega->0.2, k->2, laux->2$)"
		},
		{
			"cell_id": 15414656810465219608,
			"cell_origin": "client",
			"cell_type": "input",
			"cells": [
				{
					"cell_id": 13895594005555715725,
					"cell_origin": "server",
					"cell_type": "image_png",
					"source": "[encoded data, 29408 characters]"
				}
			],
			"source": "fig, axes = plt.subplots(2,1)\naxes[0].plot(sols[0], sols[1])\nfig;"
		},
		{
			"cell_id": 7751065038176589856,
			"cell_origin": "client",
			"cell_type": "input",
			"source": ""
		},
		{
			"cell_id": 14974314265954450945,
			"cell_origin": "client",
			"cell_type": "input",
			"source": ""
		}
	],
	"description": "Cadabra JSON notebook format",
	"version": 1.0
}
