{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "source": [ "$$\n", "\\def\\CC{\\bf C}\n", "\\def\\QQ{\\bf Q}\n", "\\def\\RR{\\bf R}\n", "\\def\\ZZ{\\bf Z}\n", "\\def\\NN{\\bf N}\n", "$$\n", "# Calcul symbolique" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from __future__ import division, print_function # Python 3" ], "outputs": [], "metadata": {} }, { "source": [ "En Python, la fonction sqrt retourne un r\u00e9sultat approxim\u00e9 sur les nombres flottants:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from math import sqrt\n", "sqrt(3)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "1.73205080757" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "La racine de 3 ne peut pas \u00eatre \u00e9crite exactement avec un nombre fini de d\u00e9cimales, car c'est un nombre irrationel. La fa\u00e7on la plus exacte d'exprimer la racine (positive) du nombre trois est de l'\u00e9crire tel quel. En SymPy, la racine carr\u00e9e de 3 est exprim\u00e9e *symboliquement*:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import sqrt\n", "sqrt(3)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "sqrt(3)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Quand c'est possible, SymPy proc\u00e8de \u00e0 des simplifications:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "sqrt(4)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "sqrt(8)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "2*sqrt(2)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Variable symbolique\n", "\n", "La fonction Symbol de SymPy permet de cr\u00e9er une variable symbolique:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import Symbol\n", "a = Symbol(\"a\") # le symbole a est stock\u00e9 dans la variable a\n", "a" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "a" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Cette variable peut \u00eatre additionn\u00e9e, soustraite, multipli\u00e9e et divis\u00e9e:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "a + a + a + a * a + 1/a - a" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "a**2 + 2*a + 1/a" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour plus de commodit\u00e9, on peut importer les variables les plus souvent utilis\u00e9es du sous-module abc :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import a, epsilon\n", "a + a * a + epsilon" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "a**2 + a + epsilon" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## D\u00e9finir les variables symboliques x\\_1, x\\_2, ..., x\\_n\n", "\n", "En SymPy, on peut cr\u00e9er plusieurs variables $x_i$ indic\u00e9es pour $i=a,...,b-1$ \u00e0 l'aide de la fonction symbols(\"xa:b\") o\u00f9 a et b sont remplac\u00e9s par des nombres entiers:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import symbols\n", "x4,x5,x6,x7,x8 = symbols(\"x4:9\")\n", "x4 + x5 + x6 + x7 + x8" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "x4 + x5 + x6 + x7 + x8" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Affichage automatique des r\u00e9sultats en LaTeX\n", "\n", "Pour que les r\u00e9sultats soient affich\u00e9s en LaTeX automatiquement, il suffit d'utiliser la commande suivante une seule fois pour le fichier:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import init_printing\n", "init_printing(use_latex='mathjax')" ], "outputs": [], "metadata": {} }, { "source": [ "Dans le notebook Jupyter, cette commande utilise la librairie MathJax.\n", "\n", "\n", "\n", "Dans ces notes, on utilisera l'option init_printing(use_latex='mathjax', use_unicode=False) lorsque cela aide la lecture des formules. Pour un exemple de la section pr\u00e9c\u00e9dente, on obtient:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import a, epsilon\n", "a + a * a + epsilon" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2\n", "a + a + \u03b5" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Expressions symboliques\n", "\n", "On peut faire des calculs impliquant plus d'une variable:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import a,b\n", "(a + b)**2" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "(a + b)**2" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "ou impliquant les fonctions de SymPy:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import sin,cos\n", "sin(a)**2 + cos(a)**2 + b" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "b + sin(a)**2 + cos(a)**2" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Les expressions symboliques peuvent combiner des rationels, des fonctions et des constantes de toutes sortes:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import Rational,pi,exp,I\n", "from sympy.abc import x,y\n", "Rational(3,2)*pi + exp(I*x) / (x**2 + y)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "3*pi/2 + exp(I*x)/(x**2 + y)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Repr\u00e9sentation interne\n", "\n", "Pour voir comment une expression symbolique est repr\u00e9sent\u00e9e dans la machine, on peut utiliser la fonction srepr :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import srepr\n", "expr = x - y\n", "srepr(x - y)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "\"Add(Symbol('x'), Mul(Integer(-1), Symbol('y')))\"" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "L'expression symbolique est repr\u00e9sent\u00e9e par un arbre d'op\u00e9rations.\n", "\n", "\n", "\n", "Pour information, l'image a \u00e9t\u00e9 cr\u00e9\u00e9e avec Graphviz avec le resultat de la fonction dotprint :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.printing.dot import dotprint\n", "s = dotprint(expr)" ], "outputs": [], "metadata": {} }, { "source": [ "## Substitutions\n", "\n", "Pour substituer certaines variables dans une expression, on utilise la m\u00e9thode subs qui s'\u00e9crit **apr\u00e8s** l'expressions sous la forme expressions.subs(). Par exemple:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import a,b,c\n", "expression = a + 2*b + 3*c\n", "expression.subs(a,9)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "2*b + 3*c + 9" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour faire plus d'une substitutions, on peut les indiquer dans un dictionaire ({}) comme ci-bas:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "expression.subs({a:9, b:4})" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "3*c + 17" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "expression.subs({a:9, b:4, c:100})" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "317" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "On peut aussi substituer une variable symbolique par une expression symbolique:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import log\n", "from sympy.abc import x,y,z\n", "expression.subs({a:x**2, b:log(y), c:z})" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "x**2 + 3*z + 2*log(y)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Constantes symboliques\n", "\n", "Contrairement au module math de Python o\u00f9 le nombre pi est repr\u00e9sent\u00e9 par une approximation d\u00e9cimale, dans SymPy, le nombre pi est repr\u00e9sent\u00e9 symboliquement. C'est une **constante symbolique**:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import pi\n", "pi" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "pi" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Cela permet de faire des calculs exacts. Par exemple, le sinus d'un angle de $\\pi/3$ est \u00e9gal \u00e0 la racine de trois sur deux:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import sin, pi\n", "sin(pi/3)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " ___\n", "\\/ 3\n", "-----\n", " 2" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "La fonction inverse du sinus aussi appel\u00e9e arc sinus et repr\u00e9sent\u00e9e par la fonction asin dans SymPy peut retourner des expressions symboliques impliquant des constantes symboliques telles que le nombre $\\pi$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import asin, Rational\n", "asin(1)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "pi\n", "--\n", "2" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "asin(Rational(1,2))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "pi\n", "--\n", "6" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "SymPy sait que les fonctions sinus et arc sinus sont inverses une de l'autre:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import x\n", "sin(asin(x))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "x" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "**ATTENTION**\n", "\n", "La fonction sin du module math de Python ne peut pas \u00eatre appel\u00e9e sur des expressions symboliques, car elle assume que l'argument est un nombre r\u00e9el (type float):" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import \n", "from math import sin\n", "sin(x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "Traceback (most recent call last):\n", "...\n", "TypeError: can't convert expression to float" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Simplifier une expression\n", "\n", "Les expressions ne sont pas toujours simplifi\u00e9es:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import sin,cos\n", "from sympy.abc import a\n", "r = sin(a)**2 + cos(a)**2\n", "r" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "sin(a)**2 + cos(a)**2" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour simplifier une expression, on utilise la commande simplify :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import simplify\n", "simplify(r)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "1" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Voici un autre exemple:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "x - 1" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "La fonction simplify performe une s\u00e9rie de simplifications dans un ordre bien choisi. Les simplifications sp\u00e9cifiques incluent besselsimp, combsimp, exptrigsimp, hypersimp, nsimplify, powsimp, radsimp, ratsimp, ratsimpmodprime, signsimp, simplify, simplify_logic, trigsimp et d'autres encore. Il suffit de consulter le code simplify?? pour voir ce qui se passe et dans quel ordre.\n", "\n", "Lorsque l'on sait exactement ce qu'on veut faire sur l'expression symbolique (factoriser, mettre sur un dominateur commun, etc.), on peut utiliser directement la bonne fonction. Cela peut retourner un r\u00e9sultat plus rapidement. Les plus importantes fonctions de modification d'expressions symboliques sont d\u00e9crites dans les sections qui suivent. On trouvera plus d'informations sur les fa\u00e7ons de simplifier une expression dans le tutoriel de SymPy: \n", "\n", "## D\u00e9velopper une expression\n", "\n", "Pour d\u00e9velopper une expression, on utilise la fonction expand :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import expand\n", "from sympy.abc import a,b\n", "(a + b)**2" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "(a + b)**2" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "expand((a + b)**2)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "a**2 + 2*a*b + b**2" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Cela peut mener \u00e0 des simplifications:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "(a + b)**2 - (a - b)**2" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "-(a - b)**2 + (a + b)**2" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "expand(_)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "4*a*b" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "**Note**: En IPython et Jupyter, la barre de soulignement (_) est une variable qui contient le dernier r\u00e9sultat calcul\u00e9. Aussi, la double barre de soulignement (__) est une variable qui contient l'avant-dernier r\u00e9sultat calcul\u00e9. Finalement, la triple barre de soulignement (___) est une variable qui contient l'avant-avant-dernier r\u00e9sultat calcul\u00e9. La quadruple barre de soulignement ne contient rien.\n", "\n", "## Annuler les facteurs communs d'une fraction\n", "\n", "Pour annuler les facteurs communs dans une fonction rationnelle, on utilise cancel :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "expr = (x**2 + x*y) / x\n", "expr" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "(x**2 + x*y)/x" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import cancel\n", "cancel(expr)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "x + y" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Factoriser un polyn\u00f4me\n", "\n", "La fonction factor de SymPy permet de factoriser un polyn\u00f4me en un produit de facteurs irr\u00e9ductibles sur l'anneau des nombres rationnels:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import factor\n", "factor(x**3 - x**2 + x - 1)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "(x - 1)*(x**2 + 1)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour factoriser le polyn\u00f4me sur les nombres de Gauss (nombres complexes \u00e0 parties imaginaire et r\u00e9elle enti\u00e8res), on ajoute l'option gaussian=True :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "factor(x**3 - x**2 + x - 1, gaussian=True)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "(x - 1)*(x - I)*(x + I)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour faire la factorisation sur une extension alg\u00e9brique des nombres rationnels, il suffit de sp\u00e9ficifier un ou des nombres alg\u00e9briques qui engendrent l'extension:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "factor(x**2 - 5)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "x**2 - 5" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "factor(x**2 - 5, extension=sqrt(5))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "(x - sqrt(5))*(x + sqrt(5))" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Consulter la documentation factor? pour obtenir de l'aide sur la factorisation de polyn\u00f4mes sur d'autres domaines ou sur des extensions de corps.\n", "\n", "## Rassembler les termes d'une expression\n", "\n", "La fonction collect rassemble les puissances communes d'un terme dans une expression. Par exemple:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "expr = x*z + x**2 + x + x*y + x**2 * w + 5 - x**3\n", "expr" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "w*x**2 - x**3 + x**2 + x*y + x*z + x + 5" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "On rassemble les termes selon les puissances de x :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import collect\n", "collect(expr, x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "-x**3 + x**2*(w + 1) + x*(y + z + 1) + 5" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## R\u00e9duire au m\u00eame d\u00e9nominateur\n", "\n", "Une somme de fonctions rationnelles reste sous forme de somme:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import x,y,z\n", "1/(x+1) + 1/y + 1/z" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 1 1 1\n", "----- + - + -\n", "x + 1 z y" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour la mettre au m\u00eame d\u00e9nominateur, on utilise ratsimp :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import ratsimp\n", "ratsimp(1/(x+1) + 1/y + 1/z)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "x*y + x*z + y*z + y + z\n", "-----------------------\n", " x*y*z + y*z" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Alternativement, on peut aussi utiliser la fonction together. \u00c0 la diff\u00e9rence de ratsimp la fonction together pr\u00e9serve le plus possible les termes sous la forme initiale:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import together\n", "together(1/(x+1) + 1/y + 1/z)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "y*z + y*(x + 1) + z*(x + 1)\n", "---------------------------\n", " y*z*(x + 1)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## D\u00e9composition en fractions partielles\n", "\n", "Soit un fraction rationnelle:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "expr = (3*x**2 + 52*x - 265) / ((x - 7)*(x - 1)*(x + 34))\n", "expr" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2\n", " 3*x + 52*x - 265\n", "------------------------\n", "(x - 7)*(x - 1)*(x + 34)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "On peut la d\u00e9composer en somme de fractions rationnelles \u00e0 l'aide de la fonction apart de SymPy:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import apart\n", "apart(expr)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 1 1 1\n", "------ + ----- + -----\n", "x + 34 x - 1 x - 7" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Rationalisation du d\u00e9nominateur d'une expression\n", "\n", "Pour rationaliser le d\u00e9nominateur d'un expression, on utilise la fonction radsimp de SymPy:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "A = 1 / (1+sqrt(5))\n", "A" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 1 \n", "---------\n", " ___\n", "1 + \\/ 5 " ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import radsimp\n", "radsimp(A)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " ___\n", "-1 + \\/ 5 \n", "----------\n", " 4 " ] }, "metadata": {} } ], "metadata": {} } ], "metadata": { "kernelspec": { "display_name": "python2", "name": "python2" } } }