{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Le module sympy (calcul formel)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from sympy import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "x=symbols(\"x\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2*x**2/3 + 3*x - 5\n" ] }, { "data": { "text/latex": [ "$\\displaystyle \\frac{2 x^{2}}{3} + 3 x - 5$" ], "text/plain": [ "2*x**2/3 + 3*x - 5" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f=S(\"2/3*x**2+3*x-5\")\n", "print(f)\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calcul littéral" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 5 x$" ], "text/plain": [ "5*x" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*x+3*x" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left(x - 1\\right) \\left(x + 6\\right)$" ], "text/plain": [ "(x - 1)*(x + 6)" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factor(x**2+5*x-6)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{2 x + 1}{x \\left(x + 1\\right)}$" ], "text/plain": [ "(2*x + 1)/(x*(x + 1))" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simplify(1/x+1/(x+1))" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle x^{2} + x - 6$" ], "text/plain": [ "x**2 + x - 6" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "expand((x-2)*(x+3))" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle -4 + 5 \\sqrt{2}$" ], "text/plain": [ "-4 + 5*sqrt(2)" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f=x**2+5*x-6\n", "f.subs(x,sqrt(2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dérivation et intégration" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4*x/3 + 3\n" ] }, { "data": { "text/latex": [ "$\\displaystyle \\frac{4 x}{3} + 3$" ], "text/plain": [ "4*x/3 + 3" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g =diff(f,x)\n", "print(g)\n", "g" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{2 x^{2}}{3} + 3 x$" ], "text/plain": [ "2*x**2/3 + 3*x" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "integrate(g,x)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Les fractions" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{46}{63}$" ], "text/plain": [ "46/63" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Rational(\"2/7\")+Rational(\"4/9\")\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{46}{63}$" ], "text/plain": [ "46/63" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=S(\"2/7+4/9\")\n", "simplify(a)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Les matrices" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1 & 2\\\\4 & 2\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1, 2],\n", "[4, 2]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = Matrix([[1,2],[4,2]])\n", "A" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1 & 3\\\\2 & 1\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1, 3],\n", "[2, 1]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B=Matrix([[1,3],[2,1]])\n", "B" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}2 & 5\\\\6 & 3\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[2, 5],\n", "[6, 3]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A+B" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}5 & 5\\\\8 & 14\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[5, 5],\n", "[8, 14]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A*B\n" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}5 & 10\\\\20 & 10\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[ 5, 10],\n", "[20, 10]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5*A" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}- \\frac{1}{3} & \\frac{1}{3}\\\\\\frac{2}{3} & - \\frac{1}{6}\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[-1/3, 1/3],\n", "[ 2/3, -1/6]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A**(-1)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\left[\\begin{matrix}1 & 4\\\\2 & 2\\end{matrix}\\right]$" ], "text/plain": [ "Matrix([\n", "[1, 4],\n", "[2, 2]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.transpose()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 2 }