{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 15.2. Solving equations and inequalities" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sympy import *\n", "init_printing()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "var('x y z a')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the function solve to resolve equations (the right hand side is always 0)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "solve(x**2 - a, x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also solve inequations. You may need to specify the domain of your variables. Here, we tell SymPy that x is a real variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = Symbol('x')\n", "solve_univariate_inequality(x**2 > 4, x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Systems of equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function also accepts systems of equations (here a linear system)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "solve([x + 2*y + 1, x - 3*y - 2], x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Non-linear systems are also supported." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "solve([x**2 + y**2 - 1, x**2 - y**2 - S(1)/2], x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Singular linear systems can also be solved (here, there are infinitely many equations because the two equations are colinear)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "solve([x + 2*y + 1, -x - 2*y - 1], x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's solve a linear system using matrices with symbolic variables." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "var('a b c d u v')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We create the augmented matrix, which is the horizontal concatenation of the system's matrix with the linear coefficients, and the right-hand side vector." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "M = Matrix([[a, b, u], [c, d, v]]); M" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "solve_linear_system(M, x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This system needs to be non-singular to have a unique solution, which is equivalent to say that the determinant of the system's matrix needs to be non-zero (otherwise the denominators in the fractions above are equal to zero)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "det(M[:2,:2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "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.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }