{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\t
\n", " jupyterでhtml.tableを使うと以前のようにcssが有効にならないので、table関連のCSSを最初に読み込みます。\n", "
\n", "\n", " また、jupyterのノートブックでもSageのノートブック同様に使えるようにsage_util.pyを読み込みます。\n", "
\n", "" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "\n", "\t\t数式でよく使われる定数をSageで使うには以下の様に表します。\n", "\t
\n", "\n", " 注意)無限大は、oを2個並べたooです。\n", "
\n", "" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
円周率 | $\\pi$ | pi |
自然体数の底 | $e$ | e |
虚数単位 | $i$ | I |
無限大 | $+\\infty$ | +Infinity |
\n", "\t\t四則演算をはじめ、Sageで使われる基本的な計算の表現方法を以下に示します。\n", "\t
\n", "\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "積 | $a b$ | a*b |
商 | $\\frac{a}{b}$ | a/b |
累乗 | $a^{b}$ | a^b |
平方根 | $\\sqrt{x}$ | sqrt(x) |
n乗根 | $x^{\\left(\\frac{1}{n}\\right)}$ | x^(1/n) |
絶対値 | ${\\left| x \\right|}$ | abs(x) |
自然対数 | $\\log\\left(x\\right)$ | log(x) |
階乗 | $n!$ | factorial(n) |
\n", "\t\tリスト内包を使うと簡単にリストの要素を変更することができます。\n", "\t\t先ほどのリストLの要素を自乗に変えて、和を求めてみましょう。\n", "
\n", "\tリスト内包の書式\n", "\t[ 式 for 変数 in リスト ]\n", "\n", "\t\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n", "385\n" ] } ], "source": [ "L2 = [i^2 for i in L]; print L2 \n", "print sum(L2) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t
\n", "\t\tsumと並んでリストの要素の積を求めるprodもよく使われます。\n", "\t\t1から5までの積をprodを使って計算してみましょう。\n", "\t
\n", "\t\n", "\t\t今回は、range関数の代わりにジェネレータを使って1から5までの\n", "\t\tリストを生成し、計算してみます。\n", "
\n", "\t(1..5)\n", "\n", "\t\t\n", "\t\n", "" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "120\n", "120\n" ] } ], "source": [ "# 1から5までの積を計算\n", "print 1*2*3*4*5 \n", "# リスト生成をまとめて\n", "print prod(1..5) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t
\n", "\t\t次に中学で習った式の因数分解や展開もSageを使うと簡単に計算することができます。\n", "\t
\n", "\n", "\n", "\n", "\t\n", "\t\t以下の関数f1をSageを使って展開してみます。展開にはexpand関数を使います。\n", "$$\n", "\t\tf1(x) = (x - 1)(x^2 -1)\n", "$$\t\t\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "(x^2 - 1)*(x - 1)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "x^3 - x^2 - x + 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "f1 = (x - 1)*(x^2 - 1); show(f1) # f1を定義\n", "f2 = expand(f1); show(f2) # f1を展開し、f2に代入" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\t多項式の係数は、coefficientsやcoefficient関数で取得できます。\n", "\t
\n", "\t\n", "\t\t以下に、f2の係数をcoeeficents関数で取得する方法とxの2次の項の係数をcoefficient関数で取得する方法を例として示します。\n", "\t
\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 0], [-1, 1], [-1, 2], [1, 3]]\n", "-1\n" ] } ], "source": [ "# 多項式の係数\n", "print f2.coefficients(x) # 変数の[係数, 次数]のペアのリストを返す\n", "print f2.coefficient(x, 2) # 変数と次数を指定して係数をを返す" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\tf1を展開した結果f2を因数分解してみましょう。因数分解にはfactor関数を使用します。\n", "\t
\n", "\t\n", "\t\t結果がf1と異なりますが、\n", "$$\n", "\t\t(x^2 - 1) = (x - 1)(x + 1)\n", "$$\t\t\n", "\t\tの関係から、正しい結果になっていることが分かります。\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "(x + 1)*(x - 1)^2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show(factor(f2)) # f2を因数分解する" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\t分数で表される式の積分やラブラス逆変換では部分分数分解を利用することがあります。\n", "\t
\n", "\t\n", "\t\t以下の様な関数f3を因数分解して、分数式f4に変えます。\n", "$$\n", "\t\tf3(x) = \\frac{1}{(x - 2)} + \\frac{1}{(x + 2)}\n", "$$\t\t\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "1/(x + 2) + 1/(x - 2)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# 分数の因数分解\n", "f3 = 1/(x+2)+1/(x-2)\n", "show(f3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\t式の部分分数分解には、prtial_fraction()メソッドを使います。f4にprtial_fractionメソッドを呼び出すとf3と同じ結果となります。\n", "\t
\n", "\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "2*x/((x + 2)*(x - 2))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "1/(x + 2) + 1/(x - 2)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "f4 = factor(f3); show(f4) # 分数式f3の因数分解\n", "show(f4.partial_fraction()) # 部分分数分解" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\t式を整理して簡単化する関数として、simplifyがあります。simplifyでは不要な項を消去するだけなので、\n", "\t\tf1のような因数分解で整理されて式の場合には結果が変わりません。\n", "\t
\n", "\t\n", "\t\tさらに突っ込んだ整理をする場合には、simplify_fullメソッドを使います。\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "I" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "(x^2 - 1)*(x - 1)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "x^3 - x^2 - x + 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show(simplify(I + x -x)) # 簡単化で不要な項を消去\n", "show(simplify(f1)) # 簡単化ではf1はそのまま\n", "show(f1.simplify_full()) # simplify_fullメソッドを使うと展開されて整理される " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\t三角関数も数式として計算するため、$ sin (\\pi/4) $も数値ではなく、\n", "\t\t$ \\frac{1}{2} \\sqrt{2} $の式が返ってきます。\n", "\t
\n", "\t\n", "\t\t値を得るには、N関数を使用します。\n", "\t
\n", "\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1/2*sqrt(2) 0.707106781186548\n" ] } ], "source": [ "print sin(pi/4), N(sin(pi/4)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\tまた三角関数への入力単位は度ではなく、ラジアンで指定します。\n", "\t
\n", "\t\n", "\t\t度とラジアンの変換には、以下の様な関数rad, degを定義すると便利です。\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 度とラジアンの変換関数\n", "rad(x) = x*pi/180\n", "deg(x) = x*180/pi" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1/2*sqrt(2) 1/2*sqrt(2)\n", "1/4*pi 45\n" ] } ], "source": [ "print sin(rad(45)), sin(pi/4)\n", "print atan(1), deg(atan(1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\t三角関数や指数関数を含む式の簡単化には simplify_fullメソッドを使います。\n", "\t
\n", "\t\n", "\t\t例として、sin関数の倍角公式をsimplify_fullメソッドで求めてみます。\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "sin(2*x)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "2*cos(x)*sin(x)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# 倍角公式\n", "fs = sin(2*x)\n", "show(fs)\n", "show(fs.simplify_full())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\t更に、simplify_fullでは三角関数の公式を活用して式を整理します。\n", "\t
\n", "\t\n", "\t\t以下の例では、以下の三角関数の公式を使って簡単化しています。\n", "$$\n", "\t\tcos^2 x + sin^2 x = 1\n", "$$\t\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "cos(x)^2 - sin(x)^2" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "2*cos(x)^2 - 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "f4 = cos(x)^2-sin(x)^2\n", "show(f4) # cos(x)^2 + sin(x)^2 = 1を使って\n", "show(f4.simplify_full()) # 簡素化する" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\t高校で習った、導関数をSageを使って導いてみましょう。\n", "\t\t関数f(x)と平均変化率g(x)を以下の様に定義します。\n", "$$\n", "\t\tf(x) = \\frac{1}{2} x^3\n", "$$\n", "$$\n", "\t\tg(x)= \\frac{f(x+h)−f(x)}{h}\n", "$$\n", "\t
\n", "\t\n", "\t\t変数x, hと関数f, gをSageで定義します。\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "x |--> 1/2*x^3" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x, h = var('x h')\n", "f(x) = x^3/2\n", "show(f)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "1/2*((h + x)^3 - x^3)/h" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# 平均変化率\n", "g =(f(x + h) - f(x))/h\n", "show(g)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\tgを展開して整理すると以下の様になり、h→0の極値(limit)をとった時、hが掛からないxの2次の項のみが残ります。\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "1/2*h^2 + 3/2*h*x + 3/2*x^2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# 展開して整理すると\n", "g1 = g.simplify_full()\n", "show(g1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\t求める導関数は、以下の様になります。\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "3/2*x^2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# h→0の極値を取ると導関数が求まる\n", "g2 = limit(g1, h=0)\n", "show(g2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\t次のような特殊な関数fを考えます。\n", "$$\n", "\t\tf(x) = \\frac{sin(x)}{x}\n", "$$\t\t\n", "\t
\n", "\t\n", "\t\tこの関数にx=0を代入するとゼロ割のエラーとなりますが、limitを使ってx→0を求めると1となります。\n", "\t
\n", "\t\n", "\t\tSageではグラフの表示でも極値をきちんと計算しているので、上記のf(x)もきちんと表示することができます。\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ValueError", "evalue": "power::eval(): division by zero", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\n", "\t\tSageでの関係式の扱い方を以下の関係式eqを例に示します。\n", "$$\n", "\t\tx + 2 \\le 0\n", "$$\n", "\t
\n", "\t\n", "\t\tこれをSageで表現すると以下の様になります。\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "eq = x + 2 <= 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t\t関係式から左辺、等号、不等号のオペレータ、左辺を取得するには、それぞれlhs, operator, rhs関数を使用します。\n", "\t
\n", "" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x + 2\n", "