{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sympy import *\n", "init_printing(use_latex='mathjax')\n", "x, y, z = symbols('x,y,z')\n", "n, m = symbols('n,m', integer=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Numeric Evaluation\n", "\n", "In this section we'll learn how to use our symbolic equations to drive numeric computations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## .subs and .evalf\n", "\n", "The simplest (and slowest) ways to evaluate an expression numerically is with the .subs and .evalf methods " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sin(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sin(x).subs({x: 0})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "acos(x).subs({x: -1})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "acos(x).subs({x: -1}).evalf()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "acos(x).subs({x: -1}).evalf(n=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "In a previous section we computed the following symbolic integral\n", "\n", "$$\\int_y^z x^n dx$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = integrate(x**n, (x, y, z))\n", "result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use .subs and a dictionary with keys n, y, z to evaluate this result when \n", "\n", " n == 2\n", " y == 0\n", " z == 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Evaluate the resulting integral on the above values\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "This integral takes on a special form when $n = -1$. Use subs to find the expression when \n", "\n", " n == -1\n", " y == 5\n", " z == 100\n", " \n", "Then use .evalf to evaluate this subs-ed expression as a float." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Evaluate the resulting integral for the values {n: -1, y: 5, z: 100}\n", "# Then use evalf to get a numeric result\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## lambdify\n", "\n", "The .subs and .evalf methods are great for when you want to evaluate an expression at a single point. When you want to evaluate your expression on lots of points they quickly become slow. \n", "\n", "To resolve this problem SymPy can rewrite its expressions as normal Python functions using the math library, vectorized computations using the NumPy library, C or Fortran Code using code printers, or even more sophisticated systems.\n", "\n", "We'll talk about some of the more advanced topics later. For now, lambdify..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# function = lambdify(input, output)\n", "\n", "f = lambdify(x, x**2)\n", "f(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "f = lambdify(x, x**2, 'numpy') # Use numpy backend\n", "data = np.array([1, 2, 3, 4, 5])\n", "f(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Here is a radial wave function for the Carbon atom at $n=3$, $l=1$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sympy.physics.hydrogen import R_nl\n", "n = 3\n", "l = 1\n", "r = 6 # Carbon\n", "expr = R_nl(n, l, x, r)\n", "expr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a function, f, that evaluate this expression using the 'numpy' backend" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create Numpy function mapping x to expr with the numpy backend\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can plot your function from $x \\in [0, 5]$ with the following numpy/matplotlib code" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from matplotlib.pyplot import plot\n", "nx = np.linspace(0, 5, 1000)\n", "plot(nx, f(nx))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Create a numpy function that computes the derivative of our expression. Plot the result alongside the original." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Compute derivative of expr with respect to x\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create new fprime function using lambdify\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plot results alongside f(nx)\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }