{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": "true" }, "source": [ "# Table of Contents\n", "
" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "from sympy import Symbol, pprint\n", "a = 3\n", "b = 2\n", "pprint(a+b)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3⋅x + 2\n" ] } ], "source": [ "x = Symbol('x')\n", "eq1 = a*x+b\n", "pprint(eq1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-2/3]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sympy import solve\n", "solve(eq1, x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 連立方程式\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{x: -6, y: 2}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sympy import Matrix, solve_linear_system\n", "from sympy.abc import x, y\n", "\n", "system = Matrix(( (1, 4, 2), (-2, 1, 14)))\n", "solve_linear_system(system, x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 展開(expand) 因数分解(factor)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 2 2\n", "x + 2⋅x⋅y + y \n" ] } ], "source": [ "from sympy import *\n", "pprint(expand((x+y)**2))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " ⎛ 2 ⎞\n", "(x - 1)⋅⎝x + 1⎠\n" ] } ], "source": [ "pprint(factor(x**3 - x**2 + x - 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3点を通る2次方程式\n", "3点(1,2),(-3,4),(-1,1)を通る2次方程式を求めよ." ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from sympy import *\n", "f, x, a, b, c = symbols('f x a b c')\n", "f = a*x**2 + b*x + c" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a*x**2 + b*x + c\n" ] } ], "source": [ "print(f)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class my_func(Function):\n", " @classmethod\n", " def eval(cls, x):\n", " return a*x**2 + b*x + c" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 2 \n", "a⋅x + b⋅x + c\n" ] } ], "source": [ "pprint(my_func(x))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": true }, "outputs": [], "source": [ "eq1 = my_func(1)\n", "eq2 = my_func(-3)\n", "eq3 = my_func(-1)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a + b + c\n", "9⋅a - 3⋅b + c\n", "a - b + c\n" ] } ], "source": [ "pprint(eq1)\n", "pprint(eq2)\n", "pprint(eq3)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from sympy.abc import a, b, c\n", "\n", "system = Matrix(( (1, 1, 1, 2), (9, -3, 1, 4),\n", "(1, -1, 1, 1)))\n", "s1 = solve_linear_system(system, a, b, c)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{a: 1/2, b: 1/2, c: 1}\n" ] } ], "source": [ "pprint(s1)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1/2\n", "1/2\n", "1\n" ] } ], "source": [ "pprint(s1[a])\n", "pprint(s1[b])\n", "pprint(s1[c])" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from sympy import sin\n", "eq = sin(x+1) - x**2" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "ename": "NotImplementedError", "evalue": "multiple generators [x, sin(x + 1)]\nNo algorithms are implemented to solve equation -x**2 + sin(x + 1)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m