{
	"cell_id": 55098764045313374,
	"cells": [
		{
			"cell_id": 11153443340774785722,
			"cell_origin": "client",
			"cell_type": "latex",
			"cells": [
				{
					"cell_id": 3683726891078164525,
					"cell_origin": "client",
					"cell_type": "latex_view",
					"source": "\\section*{Numerical evaluation of expressions}\n\nCadabra is primarily a symbolic computer algebra system, in the sense that \nit focuses on symbolic expressions, not the numerical value they take when\nall symbols in them are replaced with values. However, Cadabra does have\nfunctionality to evaluate expressions numerically as well, using either\na call through SymPy, or using its own internal expression \nevaluator. We will here focus on the latter, as it is by far the fastest.\n\nLet us start with a simple example to understand the basics. The following\ncode creates a Cadabra expression containing just $\\cos(x)$, and then\nnumerically evaluates that expression for 100 values of $x$ in the range\n$[0, 2\\pi]$. "
				}
			],
			"hidden": true,
			"source": "\\section*{Numerical evaluation of expressions}\n\nCadabra is primarily a symbolic computer algebra system, in the sense that \nit focuses on symbolic expressions, not the numerical value they take when\nall symbols in them are replaced with values. However, Cadabra does have\nfunctionality to evaluate expressions numerically as well, using either\na call through SymPy, or using its own internal expression \nevaluator. We will here focus on the latter, as it is by far the fastest.\n\nLet us start with a simple example to understand the basics. The following\ncode creates a Cadabra expression containing just $\\cos(x)$, and then\nnumerically evaluates that expression for 100 values of $x$ in the range\n$[0, 2\\pi]$. "
		},
		{
			"cell_id": 1786627167757323955,
			"cell_origin": "client",
			"cell_type": "input",
			"source": "import numpy as np\nimport matplotlib.pyplot as plt"
		},
		{
			"cell_id": 6954937305566758514,
			"cell_origin": "client",
			"cell_type": "input",
			"cells": [
				{
					"cell_id": 8577196663565344778,
					"cell_origin": "server",
					"cell_type": "latex_view",
					"cells": [
						{
							"cell_id": 9543274790289095588,
							"cell_origin": "server",
							"cell_type": "input_form",
							"source": "\\cos(x)"
						}
					],
					"source": "\\begin{dmath*}{}\\cos{x}\\end{dmath*}"
				},
				{
					"cell_id": 8321147478701128360,
					"cell_origin": "server",
					"cell_type": "verbatim",
					"source": "\\begin{verbatim}<cadabra2.NTensor object at 0xffffaf7f11b0>\\end{verbatim}"
				}
			],
			"source": "ex := \\cos(x);\nxv  = np.linspace(0, np.pi*2, 100)\nexv = nevaluate(ex, {$x$: xv} );"
		},
		{
			"cell_id": 17813844956869416932,
			"cell_origin": "client",
			"cell_type": "latex",
			"cells": [
				{
					"cell_id": 18054204918676316375,
					"cell_origin": "client",
					"cell_type": "latex_view",
					"source": "The \\algo{nevaluate} function returns an \\verb|NTensor|, which is Cadabra's\nobject to store numerical values of tensors. It can be converted to a numpy\narray by wrapping it in `np.array`, after which you can plot it:"
				}
			],
			"hidden": true,
			"source": "The \\algo{nevaluate} function returns an \\verb|NTensor|, which is Cadabra's\nobject to store numerical values of tensors. It can be converted to a numpy\narray by wrapping it in `np.array`, after which you can plot it:"
		},
		{
			"cell_id": 5634273073315875314,
			"cell_origin": "client",
			"cell_type": "input",
			"cells": [
				{
					"cell_id": 1616764054273130014,
					"cell_origin": "server",
					"cell_type": "image_png",
					"source": "[encoded data, 31632 characters]"
				},
				{
					"cell_id": 14563493594166874172,
					"cell_origin": "server",
					"cell_type": "latex_view",
					"source": "{}$\\big[$$\\big]$"
				}
			],
			"source": "plt.plot( xv, np.array(exv) );"
		},
		{
			"cell_id": 8775842585223173737,
			"cell_origin": "client",
			"cell_type": "latex",
			"cells": [
				{
					"cell_id": 10488429707702954358,
					"cell_origin": "client",
					"cell_type": "latex_view",
					"source": "To understand \\algo{nevaluate}, take a close look at the arguments. The first argument\nis the expression we want to evaluate. The second argument is a dictionary, in wich\nwe list the symbols appearing in the expression (here just $x$) and the values that\neach such symbol takes (here it's the values in the array \\verb|xv|)."
				}
			],
			"hidden": true,
			"source": "To understand \\algo{nevaluate}, take a close look at the arguments. The first argument\nis the expression we want to evaluate. The second argument is a dictionary, in wich\nwe list the symbols appearing in the expression (here just $x$) and the values that\neach such symbol takes (here it's the values in the array \\verb|xv|)."
		},
		{
			"cell_id": 9144309306588434351,
			"cell_origin": "client",
			"cell_type": "latex",
			"cells": [
				{
					"cell_id": 11117129739894336245,
					"cell_origin": "client",
					"cell_type": "latex_view",
					"source": "\\subsection*{More complicated examples}\n\nThe example above evaluated a function of a single variable over a range of values\nof that variable. We can also use this to evaluate functions of multiple variables.\nThe example below shows this."
				}
			],
			"hidden": true,
			"source": "\\subsection*{More complicated examples}\n\nThe example above evaluated a function of a single variable over a range of values\nof that variable. We can also use this to evaluate functions of multiple variables.\nThe example below shows this."
		},
		{
			"cell_id": 12217825775307860772,
			"cell_origin": "client",
			"cell_type": "input",
			"cells": [
				{
					"cell_id": 17372471785279723826,
					"cell_origin": "server",
					"cell_type": "latex_view",
					"cells": [
						{
							"cell_id": 11706414723386243608,
							"cell_origin": "server",
							"cell_type": "input_form",
							"source": "\\cos(x) \\sin(y)"
						}
					],
					"source": "\\begin{dmath*}{}\\cos{x} \\sin{y}\\end{dmath*}"
				}
			],
			"source": "ex:= \\cos(x) \\sin(y);\nxv = np.linspace(0, np.pi, 100)\nyv = np.linspace(0, np.pi, 100)\nz = np.array( nevaluate(ex, {$x$: xv, $y$: yv} ) )"
		},
		{
			"cell_id": 1982156798941009245,
			"cell_origin": "client",
			"cell_type": "latex",
			"cells": [
				{
					"cell_id": 1782089119232902477,
					"cell_origin": "client",
					"cell_type": "latex_view",
					"source": "The \\verb|z| variable is now a two-dimensional array, the first axis of which is the\n$x$-axis and the second the $y$-axis (more on this order below). We can plot such a\ndata set by creating a meshgrid from the $x$ and $y$ values, and then feeding the lot\ninto \\verb|plot_surface|:"
				}
			],
			"hidden": true,
			"source": "The \\verb|z| variable is now a two-dimensional array, the first axis of which is the\n$x$-axis and the second the $y$-axis (more on this order below). We can plot such a\ndata set by creating a meshgrid from the $x$ and $y$ values, and then feeding the lot\ninto \\verb|plot_surface|:"
		},
		{
			"cell_id": 16848152364733261862,
			"cell_origin": "client",
			"cell_type": "input",
			"cells": [
				{
					"cell_id": 16625852451007699030,
					"cell_origin": "server",
					"cell_type": "image_png",
					"source": "[encoded data, 302032 characters]"
				}
			],
			"source": "XV, YV=np.meshgrid(xv, yv)\nplt.figure(figsize=(20, 10))\nax = plt.axes(projection='3d')\nplt.figure().subplots_adjust(top=1, bottom=0, left=0, right=1, wspace=0)\nax.plot_surface(XV, YV, z, rstride=1, cstride=1, cmap='terrain', edgecolor=None);                   "
		},
		{
			"cell_id": 13200753290457032630,
			"cell_origin": "client",
			"cell_type": "latex",
			"cells": [
				{
					"cell_id": 5101273659635728255,
					"cell_origin": "client",
					"cell_type": "latex_view",
					"source": "The order in which the axis of the result of \\algo{nevaluate} should be interpreted\nis determined by the order in which you list them in the values dictionary. \nCompare the following:"
				}
			],
			"hidden": true,
			"source": "The order in which the axis of the result of \\algo{nevaluate} should be interpreted\nis determined by the order in which you list them in the values dictionary. \nCompare the following:"
		},
		{
			"cell_id": 17971765439671340505,
			"cell_origin": "client",
			"cell_type": "input",
			"source": "z1 = np.array( nevaluate(ex, {$x$: xv, $y$: yv} ))\nz2 = np.array( nevaluate(ex, {$y$: yv, $x$: xv} ))"
		},
		{
			"cell_id": 12456077622196445642,
			"cell_origin": "client",
			"cell_type": "input",
			"cells": [
				{
					"cell_id": 16614149837444711937,
					"cell_origin": "server",
					"cell_type": "verbatim",
					"source": "\\begin{verbatim}0.5633046988744114\\end{verbatim}"
				},
				{
					"cell_id": 6254526967367730991,
					"cell_origin": "server",
					"cell_type": "verbatim",
					"source": "\\begin{verbatim}0.2512712531759243\\end{verbatim}"
				}
			],
			"source": "z1[10,20];\nz2[10,20];"
		},
		{
			"cell_id": 730370848732475554,
			"cell_origin": "client",
			"cell_type": "latex",
			"cells": [
				{
					"cell_id": 2572738521568116665,
					"cell_origin": "client",
					"cell_type": "latex_view",
					"source": "\\subsection*{Supported elementary functions}\n\nAt present the \\verb|nevaluate| function supports expressions with the following building blocks:\nmultiplication/division, addition/subtraction, trigonometric functions, hyperbolic trigonometric functions,\nlogarithms, exponential, square root."
				}
			],
			"hidden": true,
			"source": "\\subsection*{Supported elementary functions}\n\nAt present the \\verb|nevaluate| function supports expressions with the following building blocks:\nmultiplication/division, addition/subtraction, trigonometric functions, hyperbolic trigonometric functions,\nlogarithms, exponential, square root."
		},
		{
			"cell_id": 11584173555601999890,
			"cell_origin": "client",
			"cell_type": "input",
			"source": ""
		}
	],
	"description": "Cadabra JSON notebook format",
	"version": 1.0
}
