Created
October 31, 2018 18:31
-
-
Save nikibobi/f206637251fca0d79c5177c317c91f09 to your computer and use it in GitHub Desktop.
SymPy Example.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "SymPy Example.ipynb", | |
"version": "0.3.2", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/nikibobi/f206637251fca0d79c5177c317c91f09/sympy-example.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "RLHci_WpFM0W", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# [SymPy](https://www.sympy.org) Example Notebook\n", | |
"\n", | |
"Import SymPy used functions" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "aEi3iSs853jM", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"from sympy import init_printing, Eq\n", | |
"from sympy.abc import x, f, lamda\n", | |
"from sympy.solvers import solve" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "gRmyv2ZyE2fz", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"Setup colab LaTeX printing boilerplate" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "FKDewRvI8MtV", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"def custom_latex_printer(exp, **options):\n", | |
" from google.colab.output._publish import javascript\n", | |
" from sympy.printing import latex\n", | |
" url = \"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default\"\n", | |
" javascript(url=url)\n", | |
" return latex(exp, **options)\n", | |
"\n", | |
"init_printing(use_latex=\"mathjax\", latex_printer=custom_latex_printer)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "DUD7LhZFFop0", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"Define a quadratic equation for $x$ with parameter $\\lambda$ that can take specific values for $\\lambda\\in[0,20]$ or find a solution containing $\\lambda$ as a parameter (comment line `3` with `#`)" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "d7P4P_E66K5-", | |
"colab_type": "code", | |
"cellView": "both", | |
"outputId": "d2842de6-7924-4f5c-b811-34838c385e88", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 122 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"#@title Quadratic equation parameter { run: \"auto\" }\n", | |
"# comment the line bellow for parametric solution\n", | |
"lamda = 18 #@param {type:\"slider\", min:0, max:20, step:1}\n", | |
"\n", | |
"f = Eq(lamda * x ** 2 + x - lamda, -17)\n", | |
"display(f)\n", | |
"\n", | |
"solve(f, x)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/html": [ | |
"<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/latex": "$$18 x^{2} + x - 18 = -17$$", | |
"text/plain": [ | |
" 2 \n", | |
"18⋅x + x - 18 = -17" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/html": [ | |
"<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default'></script>" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
} | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/latex": "$$\\left [ - \\frac{1}{36} + \\frac{\\sqrt{73}}{36}, \\quad - \\frac{\\sqrt{73}}{36} - \\frac{1}{36}\\right ]$$", | |
"text/plain": [ | |
"⎡ 1 √73 √73 1 ⎤\n", | |
"⎢- ── + ───, - ─── - ──⎥\n", | |
"⎣ 36 36 36 36⎦" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 3 | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment