{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Play around area" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "from sympy import *\n", "x, y, z = symbols(\"x y z\")" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "expr = cos(x) + 1\n", "expr.subs(x, y)" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "expr.subs(x, 0)" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "expr = x**y\n", "expr\n" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "expr = expr.subs(y, x**y)\n", "expr\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "expr = expr.subs(y, x**x)\n", "expr" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "expr = sin(2*x) + cos(2*x)\n", "expand_trig(expr)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "expr.subs(sin(2*x), 2*sin(x)*cos(x))" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "expr = cos(x)\n", "expr.subs(x, 0)\n", "expr\n", "x" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "expr = x**4 - 4*x**3 + 4*x**2 - 2*x + 3\n", "replacements = [(x**i, y**i) for i in range(5) if i % 2 == 0]\n", "expr.subs(replacements)" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "To evaluate a numerical expression into a floating point number, use `evalf`." ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "expr = sqrt(8)\n", "expr.evalf()" ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "pi.evalf(100)" ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "pi.evalf(10000)" ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "expr = cos(2*x)\n", "expr.evalf(subs={x: 2.4})" ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "one = cos(1)**2 + sin(1)**2\n", "(one - 1).evalf()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "(one - 1).evalf(chop=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "19", "metadata": {}, "outputs": [], "source": [ "import numpy \n", "a = numpy.arange(10) \n", "expr = sin(x)\n", "f = lambdify(x, expr, \"numpy\") \n", "f(a) " ] }, { "cell_type": "code", "execution_count": null, "id": "20", "metadata": {}, "outputs": [], "source": [ "f = lambdify(x, expr, \"math\")\n", "f(0.1)" ] }, { "cell_type": "code", "execution_count": null, "id": "21", "metadata": {}, "outputs": [], "source": [ "def mysin(x):\n", " \"\"\"\n", " My sine. Note that this is only accurate for small x.\n", " \"\"\"\n", " return x\n", "f = lambdify(x, expr, {\"sin\":mysin})\n", "f(0.1)" ] }, { "cell_type": "code", "execution_count": null, "id": "22", "metadata": {}, "outputs": [], "source": [ "from sympy import init_printing\n", "init_printing() " ] }, { "cell_type": "code", "execution_count": null, "id": "23", "metadata": {}, "outputs": [], "source": [ "from sympy import init_session\n", "init_session() " ] }, { "cell_type": "code", "execution_count": null, "id": "24", "metadata": {}, "outputs": [], "source": [ "\n", "from __future__ import division\n", "from sympy import *\n", "x, y, z, t = symbols('x y z t')\n", "k, m, n = symbols('k m n', integer=True)\n", "f, g, h = symbols('f g h', cls=Function)\n", "init_printing() # doctest: +SKIP\n" ] }, { "cell_type": "code", "execution_count": null, "id": "25", "metadata": {}, "outputs": [], "source": [ "Integral(sqrt(1/x),x)" ] }, { "cell_type": "code", "execution_count": null, "id": "26", "metadata": {}, "outputs": [], "source": [ "simplify(sin(x)**2 + cos(x)**2)\n", "simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))\n", "simplify(gamma(x)/gamma(x - 2))" ] }, { "cell_type": "code", "execution_count": null, "id": "27", "metadata": {}, "outputs": [], "source": [ "simplify(x**2 + 2*x + 1)" ] }, { "cell_type": "code", "execution_count": null, "id": "28", "metadata": {}, "outputs": [], "source": [ "expand((x + 1)**2)\n", "expand((x + 2)*(x - 3))" ] }, { "cell_type": "code", "execution_count": null, "id": "29", "metadata": {}, "outputs": [], "source": [ "expand((x + 1)*(x - 2) - (x - 1)*x)" ] }, { "cell_type": "code", "execution_count": null, "id": "30", "metadata": {}, "outputs": [], "source": [ "factor(x**3 - x**2 + x - 1)\n", "factor(x**2*z + 4*x*y*z + 4*y**2*z)" ] }, { "cell_type": "code", "execution_count": null, "id": "31", "metadata": {}, "outputs": [], "source": [ "factor_list(x**2*z + 4*x*y*z + 4*y**2*z)" ] }, { "cell_type": "code", "execution_count": null, "id": "32", "metadata": {}, "outputs": [], "source": [ "expand((cos(x) + sin(x))**2)\n", "factor(cos(x)**2 + 2*cos(x)*sin(x) + sin(x)**2)" ] }, { "cell_type": "code", "execution_count": null, "id": "33", "metadata": {}, "outputs": [], "source": [ "expr = x*y + x - 3 + 2*x**2 - z*x**2 + x**3\n", "expr\n", "collected_expr = collect(expr, x)\n", "collected_expr" ] }, { "cell_type": "code", "execution_count": null, "id": "34", "metadata": {}, "outputs": [], "source": [ "collected_expr.coeff(x, 2)" ] }, { "cell_type": "code", "execution_count": null, "id": "35", "metadata": {}, "outputs": [], "source": [ "cancel((x**2 + 2*x + 1)/(x**2 + x))" ] }, { "cell_type": "code", "execution_count": null, "id": "36", "metadata": {}, "outputs": [], "source": [ "expr = 1/x + (3*x/2 - 2)/(x - 4)\n", "expr\n", "cancel(expr)" ] }, { "cell_type": "code", "execution_count": null, "id": "37", "metadata": {}, "outputs": [], "source": [ "expr = (x*y**2 - 2*x*y*z + x*z**2 + y**2 - 2*y*z + z**2)/(x**2 - 1)\n", "expr\n", "cancel(expr)" ] }, { "cell_type": "code", "execution_count": null, "id": "38", "metadata": {}, "outputs": [], "source": [ "factor(expr)" ] }, { "cell_type": "code", "execution_count": null, "id": "39", "metadata": {}, "outputs": [], "source": [ "expr = (4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x)\n", "expr\n", "apart(expr)" ] }, { "cell_type": "code", "execution_count": null, "id": "40", "metadata": {}, "outputs": [], "source": [ "acos(x)\n", "cos(acos(x))\n", "asin(1)" ] }, { "cell_type": "code", "execution_count": null, "id": "41", "metadata": {}, "outputs": [], "source": [ "trigsimp(sin(x)**2 + cos(x)**2)\n", "trigsimp(sin(x)**4 - 2*cos(x)**2*sin(x)**2 + cos(x)**4)\n", "trigsimp(sin(x)*tan(x)/sec(x))" ] }, { "cell_type": "code", "execution_count": null, "id": "42", "metadata": {}, "outputs": [], "source": [ "trigsimp(cosh(x)**2 + sinh(x)**2)\n", "trigsimp(sinh(x)/tanh(x))" ] }, { "cell_type": "code", "execution_count": null, "id": "43", "metadata": {}, "outputs": [], "source": [ "expand_trig(sin(x + y))\n", "expand_trig(tan(2*x))" ] }, { "cell_type": "code", "execution_count": null, "id": "44", "metadata": {}, "outputs": [], "source": [ "trigsimp(sin(x)*cos(y) + sin(y)*cos(x))" ] }, { "cell_type": "code", "execution_count": null, "id": "45", "metadata": {}, "outputs": [], "source": [ "x, y = symbols('x y', positive=True)\n", "a, b = symbols('a b', real=True)\n", "z, t, c = symbols('z t c')" ] }, { "cell_type": "code", "execution_count": null, "id": "46", "metadata": {}, "outputs": [], "source": [ "sqrt(x) == x**Rational(1, 2)" ] }, { "cell_type": "markdown", "id": "47", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Power" ] }, { "cell_type": "code", "execution_count": null, "id": "48", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "powsimp(x**a*x**b)\n", "powsimp(x**a*y**a)" ] }, { "cell_type": "code", "execution_count": null, "id": "49", "metadata": {}, "outputs": [], "source": [ "powsimp(t**c*z**c)" ] }, { "cell_type": "code", "execution_count": null, "id": "50", "metadata": {}, "outputs": [], "source": [ "powsimp(t**c*z**c, force=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "51", "metadata": {}, "outputs": [], "source": [ "(z*t)**2\n", "sqrt(x*y)" ] }, { "cell_type": "code", "execution_count": null, "id": "52", "metadata": {}, "outputs": [], "source": [ "powsimp(z**2*t**2)\n", "powsimp(sqrt(x)*sqrt(y))" ] }, { "cell_type": "code", "execution_count": null, "id": "53", "metadata": {}, "outputs": [], "source": [ "expand_power_exp(x**(a + b))" ] }, { "cell_type": "code", "execution_count": null, "id": "54", "metadata": {}, "outputs": [], "source": [ "expand_power_base((x*y)**a)" ] }, { "cell_type": "markdown", "id": "55", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Derivative" ] }, { "cell_type": "code", "execution_count": null, "id": "56", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "diff(cos(x), x)\n", "diff(exp(x**2), x)" ] }, { "cell_type": "code", "execution_count": null, "id": "57", "metadata": {}, "outputs": [], "source": [ "diff(x**4, x, x, x)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "58", "metadata": {}, "outputs": [], "source": [ "diff(x**4, x, 3)" ] }, { "cell_type": "code", "execution_count": null, "id": "59", "metadata": {}, "outputs": [], "source": [ "expr = exp(x*y*z)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "60", "metadata": {}, "outputs": [], "source": [ "diff(expr, x, y, y, z, z, z, z)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "61", "metadata": {}, "outputs": [], "source": [ "diff(expr, x, y, 2, z, 4)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "62", "metadata": {}, "outputs": [], "source": [ "diff(expr, x, y, y, z, 4)" ] }, { "cell_type": "code", "execution_count": null, "id": "63", "metadata": {}, "outputs": [], "source": [ "expr.diff(x, y, y, z, 4)" ] }, { "cell_type": "markdown", "id": "64", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## unevaluated derivative" ] }, { "cell_type": "markdown", "id": "65", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "These unevaluated objects are useful for delaying the evaluation of the derivative, or for printing purposes." ] }, { "cell_type": "code", "execution_count": null, "id": "66", "metadata": {}, "outputs": [], "source": [ "deriv = Derivative(expr, x, y, y, z, 4)\n", "deriv" ] }, { "cell_type": "code", "execution_count": null, "id": "67", "metadata": {}, "outputs": [], "source": [ "deriv.doit()" ] }, { "cell_type": "code", "execution_count": null, "id": "68", "metadata": {}, "outputs": [], "source": [ "m, n, a, b = symbols('m n a b')\n", "expr = (a*x + b)**m\n", "expr.diff((x, n))" ] }, { "cell_type": "markdown", "id": "69", "metadata": {}, "source": [ "Integral" ] }, { "cell_type": "code", "execution_count": null, "id": "70", "metadata": {}, "outputs": [], "source": [ "integrate(cos(x), x)" ] }, { "cell_type": "markdown", "id": "71", "metadata": {}, "source": [ "Note that SymPy does not include the constant of integration. If you want it, you can add one yourself, or rephrase your problem as a differential equation and use dsolve to solve it, which does add the constant (see Solving Differential Equations)." ] }, { "cell_type": "code", "execution_count": null, "id": "72", "metadata": {}, "outputs": [], "source": [ "integrate(exp(-x), (x, 0, oo))" ] }, { "cell_type": "code", "execution_count": null, "id": "73", "metadata": {}, "outputs": [], "source": [ "integrate(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))" ] }, { "cell_type": "code", "execution_count": null, "id": "74", "metadata": {}, "outputs": [], "source": [ "expr = integrate(x**x, x)\n", "print(expr)\n", "expr" ] }, { "cell_type": "code", "execution_count": null, "id": "75", "metadata": {}, "outputs": [], "source": [ "expr = Integral(log(x)**2, x)\n", "expr\n" ] }, { "cell_type": "code", "execution_count": null, "id": "76", "metadata": {}, "outputs": [], "source": [ "expr.doit()" ] }, { "cell_type": "code", "execution_count": null, "id": "77", "metadata": {}, "outputs": [], "source": [ "integ = Integral((x**4 + x**2*exp(x) - x**2 - 2*x*exp(x) - 2*x -\n", " exp(x))*exp(x)/((x - 1)**2*(x + 1)**2*(exp(x) + 1)), x)\n", "integ\n", "integ.doit()" ] }, { "cell_type": "code", "execution_count": null, "id": "78", "metadata": {}, "outputs": [], "source": [ "integ = Integral(sin(x**2), x)\n", "integ\n", "integ.doit()" ] }, { "cell_type": "code", "execution_count": null, "id": "79", "metadata": {}, "outputs": [], "source": [ "integ = Integral(x**y*exp(-x), (x, 0, oo))\n", "integ\n", "integ.doit()" ] }, { "cell_type": "markdown", "id": "80", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Limit" ] }, { "cell_type": "code", "execution_count": null, "id": "81", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "limit(sin(x)/x, x, 0)" ] }, { "cell_type": "code", "execution_count": null, "id": "82", "metadata": {}, "outputs": [], "source": [ "expr = x**2/exp(x)\n", "expr.subs(x, oo)\n", "limit(expr, x, oo)" ] }, { "cell_type": "code", "execution_count": null, "id": "83", "metadata": {}, "outputs": [], "source": [ "expr = Limit((cos(x) - 1)/x, x, 0)\n", "expr\n" ] }, { "cell_type": "code", "execution_count": null, "id": "84", "metadata": {}, "outputs": [], "source": [ "expr.doit()" ] }, { "cell_type": "code", "execution_count": null, "id": "85", "metadata": {}, "outputs": [], "source": [ "limit(1/x, x, 0, '+')" ] }, { "cell_type": "code", "execution_count": null, "id": "86", "metadata": {}, "outputs": [], "source": [ "limit(1/x, x, 0, '-')" ] }, { "cell_type": "markdown", "id": "87", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Series expansion" ] }, { "cell_type": "code", "execution_count": null, "id": "88", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "expr = exp(sin(x))\n", "expr.series(x, 0, 4)" ] }, { "cell_type": "code", "execution_count": null, "id": "89", "metadata": {}, "outputs": [], "source": [ "x + x**3 + x**6 + O(x**4)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "90", "metadata": {}, "outputs": [], "source": [ "x*O(1)" ] }, { "cell_type": "code", "execution_count": null, "id": "91", "metadata": {}, "outputs": [], "source": [ "expr.series(x, 0, 4).removeO()" ] }, { "cell_type": "code", "execution_count": null, "id": "92", "metadata": {}, "outputs": [], "source": [ "exp(x - 6).series(x, x0=6)" ] }, { "cell_type": "code", "execution_count": null, "id": "93", "metadata": {}, "outputs": [], "source": [ "f, g = symbols('f g', cls=Function)\n", "differentiate_finite(f(x)*g(x))" ] }, { "cell_type": "markdown", "id": "94", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Eqaution" ] }, { "cell_type": "code", "execution_count": null, "id": "95", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "Eq(x, y)" ] }, { "cell_type": "code", "execution_count": null, "id": "96", "metadata": {}, "outputs": [], "source": [ "solveset(Eq(x**2, 1), x)\n", "solveset(Eq(x**2 - 1, 0), x)\n", "solveset(x**2 - 1, x)" ] }, { "cell_type": "code", "execution_count": null, "id": "97", "metadata": {}, "outputs": [], "source": [ "solveset(x**2 - x, x)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "98", "metadata": {}, "outputs": [], "source": [ "solveset(x - x, x, domain=S.Reals)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "99", "metadata": {}, "outputs": [], "source": [ "solveset(sin(x) - 1, x, domain=S.Reals)" ] }, { "cell_type": "code", "execution_count": null, "id": "100", "metadata": {}, "outputs": [], "source": [ "solveset(exp(x), x) # No solution exists\n" ] }, { "cell_type": "code", "execution_count": null, "id": "101", "metadata": {}, "outputs": [], "source": [ "solveset(cos(x) - x, x) # Not able to find solution" ] }, { "cell_type": "code", "execution_count": null, "id": "102", "metadata": {}, "outputs": [], "source": [ "linsolve([x + y + z - 1, x + y + 2*z - 3 ], (x, y, z))" ] }, { "cell_type": "code", "execution_count": null, "id": "103", "metadata": {}, "outputs": [], "source": [ "linsolve(Matrix(([1, 1, 1, 1], [1, 1, 2, 3])), (x, y, z))" ] }, { "cell_type": "code", "execution_count": null, "id": "104", "metadata": {}, "outputs": [], "source": [ "M = Matrix(((1, 1, 1, 1), (1, 1, 2, 3)))\n", "system = A, b = M[:, :-1], M[:, -1]\n", "linsolve(system, x, y, z)" ] }, { "cell_type": "markdown", "id": "105", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Solving differential equation" ] }, { "cell_type": "code", "execution_count": null, "id": "106", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "f, g = symbols('f g', cls=Function)" ] }, { "cell_type": "code", "execution_count": null, "id": "107", "metadata": {}, "outputs": [], "source": [ "f(x)" ] }, { "cell_type": "code", "execution_count": null, "id": "108", "metadata": {}, "outputs": [], "source": [ "f(x).diff(x)" ] }, { "cell_type": "code", "execution_count": null, "id": "109", "metadata": {}, "outputs": [], "source": [ "diffeq = Eq(f(x).diff(x, x) - 2*f(x).diff(x) + f(x), sin(x))\n", "diffeq" ] }, { "cell_type": "code", "execution_count": null, "id": "110", "metadata": {}, "outputs": [], "source": [ "dsolve(diffeq, f(x))" ] }, { "cell_type": "code", "execution_count": null, "id": "111", "metadata": {}, "outputs": [], "source": [ "dsolve(f(x).diff(x)*(1 - sin(f(x))) - 1, f(x))" ] }, { "cell_type": "markdown", "id": "112", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Matrices" ] }, { "cell_type": "code", "execution_count": null, "id": "113", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "init_printing(use_unicode=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "114", "metadata": {}, "outputs": [], "source": [ "Matrix([[1, -1], [3, 4], [0, 2]])" ] }, { "cell_type": "code", "execution_count": null, "id": "115", "metadata": {}, "outputs": [], "source": [ "Matrix([1, 2, 3])" ] }, { "cell_type": "code", "execution_count": null, "id": "116", "metadata": {}, "outputs": [], "source": [ "Matrix([[1], [2], [3]])" ] }, { "cell_type": "code", "execution_count": null, "id": "117", "metadata": {}, "outputs": [], "source": [ "Matrix([[1, 2, 3]])" ] }, { "cell_type": "code", "execution_count": null, "id": "118", "metadata": {}, "outputs": [], "source": [ "Matrix([[1, 2, 3],[4,5,6]])" ] }, { "cell_type": "code", "execution_count": null, "id": "119", "metadata": {}, "outputs": [], "source": [ "M = Matrix([[1, 2, 3], [3, 2, 1]])\n", "N = Matrix([0, 1, 1])\n", "M*N" ] }, { "cell_type": "code", "execution_count": null, "id": "120", "metadata": {}, "outputs": [], "source": [ "from sympy import shape\n", "M = Matrix([[1, 2, 3], [-2, 0, 4]])\n", "M\n" ] }, { "cell_type": "code", "execution_count": null, "id": "121", "metadata": {}, "outputs": [], "source": [ "shape(M)" ] }, { "cell_type": "code", "execution_count": null, "id": "122", "metadata": {}, "outputs": [], "source": [ "M.row(0)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "123", "metadata": {}, "outputs": [], "source": [ "M.col(-1)" ] }, { "cell_type": "code", "execution_count": null, "id": "124", "metadata": {}, "outputs": [], "source": [ "M.col_del(0)\n", "M\n" ] }, { "cell_type": "code", "execution_count": null, "id": "125", "metadata": {}, "outputs": [], "source": [ "M.row_del(1)\n", "M" ] }, { "cell_type": "code", "execution_count": null, "id": "126", "metadata": {}, "outputs": [], "source": [ "M = M.row_insert(1, Matrix([[0, 4]]))\n", "M\n" ] }, { "cell_type": "code", "execution_count": null, "id": "127", "metadata": {}, "outputs": [], "source": [ "M = M.col_insert(0, Matrix([1, -2]))\n", "M" ] }, { "cell_type": "code", "execution_count": null, "id": "128", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "M = Matrix([[1, 3], [-2, 3]])\n", "N = Matrix([[0, 3], [0, 7]])\n", "M + N\n", "M*N\n", "3*M\n", "M**2\n", "M**-1\n", "N**-1" ] }, { "cell_type": "code", "execution_count": null, "id": "129", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "eye(3)\n", "eye(4)" ] }, { "cell_type": "code", "execution_count": null, "id": "130", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "ones(3, 2)" ] }, { "cell_type": "code", "execution_count": null, "id": "131", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "diag(1, 2, 3)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "132", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "diag(-1, ones(2, 2), Matrix([5, 7, 5]))" ] }, { "cell_type": "code", "execution_count": null, "id": "133", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "M = Matrix([[1, 0, 1], [2, -1, 3], [4, 3, 2]])\n", "M\n" ] }, { "cell_type": "code", "execution_count": null, "id": "134", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "M.det()" ] }, { "cell_type": "code", "execution_count": null, "id": "135", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "M = Matrix([[1, 0, 1, 3], [2, 3, 4, 7], [-1, -3, -3, -4]])\n", "M\n" ] }, { "cell_type": "code", "execution_count": null, "id": "136", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "M.rref()" ] }, { "cell_type": "code", "execution_count": null, "id": "137", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "M = Matrix([[1, 2, 3, 0, 0], [4, 10, 0, 0, 1]])\n", "M\n" ] }, { "cell_type": "code", "execution_count": null, "id": "138", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "M.nullspace()" ] }, { "cell_type": "code", "execution_count": null, "id": "139", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "M = Matrix([[1, 1, 2], [2 ,1 , 3], [3 , 1, 4]])\n", "M\n", "M.columnspace()" ] }, { "cell_type": "code", "execution_count": null, "id": "140", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "M = Matrix([[3, -2, 4, -2], [5, 3, -3, -2], [5, -2, 2, -2], [5, -2, -3, 3]])\n", "M\n" ] }, { "cell_type": "code", "execution_count": null, "id": "141", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "M.eigenvals()" ] }, { "cell_type": "code", "execution_count": null, "id": "142", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "M.eigenvects()" ] }, { "cell_type": "code", "execution_count": null, "id": "143", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "P, D = M.diagonalize()\n", "P\n", "D\n", "P*D*P**-1\n", "P*D*P**-1 == M" ] }, { "cell_type": "code", "execution_count": null, "id": "144", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "lamda = symbols('lamda')\n", "p = M.charpoly(lamda)\n", "factor(p.as_expr())" ] }, { "cell_type": "code", "execution_count": null, "id": "145", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "q = Symbol(\"q\", positive = True)\n", "m = Matrix([\n", "[-2*cosh(q/3), exp(-q), 1],\n", "[ exp(q), -2*cosh(q/3), 1],\n", "[ 1, 1, -2*cosh(q/3)]])\n", "m.nullspace() " ] }, { "cell_type": "code", "execution_count": null, "id": "146", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "import warnings\n", "def my_iszero(x):\n", " try:\n", " result = x.is_zero\n", " except AttributeError:\n", " result = None\n", " # Warnings if evaluated into None\n", " if result is None:\n", " warnings.warn(\"Zero testing of {} evaluated into None\".format(x))\n", " return result\n", "m.nullspace(iszerofunc=my_iszero) " ] }, { "cell_type": "code", "execution_count": null, "id": "147", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "def my_iszero(x):\n", " try:\n", " result = x.rewrite(exp).simplify().is_zero\n", " except AttributeError:\n", " result = None\n", " # Warnings if evaluated into None\n", " if result is None:\n", " warnings.warn(\"Zero testing of {} evaluated into None\".format(x))\n", " return result\n", "m.nullspace(iszerofunc=my_iszero) " ] }, { "cell_type": "markdown", "id": "148", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Expression Trees" ] }, { "cell_type": "code", "execution_count": null, "id": "149", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "x, y, z = symbols('x y z')" ] }, { "cell_type": "code", "execution_count": null, "id": "150", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "expr = x**2 + x*y\n", "srepr(expr)" ] }, { "cell_type": "code", "execution_count": null, "id": "151", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "x = symbols('x')" ] }, { "cell_type": "code", "execution_count": null, "id": "152", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "x = Symbol('x')" ] }, { "cell_type": "code", "execution_count": null, "id": "153", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "srepr(x**2)" ] }, { "cell_type": "code", "execution_count": null, "id": "154", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "Pow(x, 2)" ] }, { "cell_type": "code", "execution_count": null, "id": "155", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "expr = sin(x*y)/2 - x**2 + 1/y\n", "srepr(expr)" ] }, { "cell_type": "code", "execution_count": null, "id": "156", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "srepr(x - y)" ] }, { "cell_type": "code", "execution_count": null, "id": "157", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "expr = x/y\n", "srepr(expr)" ] }, { "cell_type": "code", "execution_count": null, "id": "158", "metadata": {}, "outputs": [], "source": [ "import graphviz\n" ] }, { "cell_type": "code", "execution_count": null, "id": "159", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "import graphviz\n", "\n", "# Create a new Digraph object\n", "dot = graphviz.Digraph(comment='The Visualization of x**2 + xy')\n", "\n", "# Adding nodes\n", "dot.node('A', 'Add')\n", "dot.node('B', 'Pow')\n", "dot.node('C', 'x', shape='ellipse')\n", "dot.node('D', '2', shape='ellipse')\n", "dot.node('E', 'Mul')\n", "dot.node('F', 'x', shape='ellipse') # Reused label 'x', but it's a different node in the graph context\n", "dot.node('G', 'y', shape='ellipse')\n", "\n", "# Adding edges\n", "dot.edges(['AB', 'AE'])\n", "dot.edge('B', 'C', label='0,0')\n", "dot.edge('B', 'D', label='0,1')\n", "dot.edges(['EC', 'EG'])\n", "\n", "# Generate the diagram\n", "dot_path = '/mnt/data/expr_graphviz.svg'\n", "dot.render(dot_path, format='svg', view=False)\n", "\n", "\n", "dot_path" ] }, { "cell_type": "code", "execution_count": null, "id": "160", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.8" } }, "nbformat": 4, "nbformat_minor": 5 }