{ "metadata": { "name": "", "signature": "sha256:df1c115c86837c0f0c3e0207e7fab7b16aa871306b22ac8b0f0e3d8789926413" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "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", "collapsed": false, "input": [ "from sympy import *\n", "init_printing()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "var('x y z a')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the function solve to resolve equations (the right hand side is always 0)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "solve(x**2 - a, x)" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "x = Symbol('x')\n", "solve_univariate_inequality(x**2 > 4, x)" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "solve([x + 2*y + 1, x - 3*y - 2], x, y)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Non-linear systems are also supported." ] }, { "cell_type": "code", "collapsed": false, "input": [ "solve([x**2 + y**2 - 1, x**2 - y**2 - S(1)/2], x, y)" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "solve([x + 2*y + 1, -x - 2*y - 1], x, y)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's solve a linear system using matrices with symbolic variables." ] }, { "cell_type": "code", "collapsed": false, "input": [ "var('a b c d u v')" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "M = Matrix([[a, b, u], [c, d, v]]); M" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "solve_linear_system(M, x, y)" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "det(M[:2,:2])" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": {} } ] }