{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\tHiroshi TAKEMOTO\n", "\t(take.pwave@gmail.com)\n", "\t

表記方法

\t\n", "\t

\n", "\t\tSageを使用する際に知っていると便利な約束事(表記方法)があります。\n", "\t\t代表的なものについて、以下に示します。\n", "\t

\n", "\n", "\n", "\n", "\t

python文法

\n", "\t

\n", "\t\tSageは、pythonのインタプリタを使っているため、pythonの構文がそのまま使えます。\n", "\t

\n", "\t

\n", "\t\tpythonでのコメント(実行に影響しない注釈)は、#記号から行末がコメントとして扱われます。\n", "\t\tコメントには日本語も使用できますので、式の説明やメモを入れておくと後で役立ちます。\n", "\t

\t\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "# print(1 + 3) この行はコメントです。\n", "print(1 + 3 + 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

複数の式を1行にまとめる

\n", "\t

\n", "\t\t複数の式を1行にまとめるには、式を;(セミコロン)で区切ります。\n", "\t

\n", "\t

\n", "\t\tセルを評価するとprint文やshowコマンドの結果、それと一番最後の結果が表示されます。\n", "\t\tしかし、最後の代入文の結果は表示されないため、例のように;(セミコロン)の後に変数名\n", "\t\tを入力します。\n", "\t

\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = 1 + 3; print b # 複数の式を1行にまとめるには、式を;(セミコロン)で区切ります。\n", "a = 1; a # 代入の結果は、値を返さないので;で区切って変数名を書いて代入結果を表示します。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

python変数への代入

\n", "\t

\n", "\t\t計算結果をpython変数に代入するときには、= (イコール)を使います。\n", "\t\tpython変数と断っているのは、sageの数式で使われる変数と区別するためです。\n", "\t

\n", "\t

変数aに$\\frac{1}{2} + \\frac{1}{3}$の結果を代入します。結果は、$\\frac{5}{6}$と分数で表示されます。\n", "\t\taの値を数値として出力するには、N関数またはnメソッドを使います。\n", "\t

\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5/6\n" ] }, { "data": { "text/plain": [ "0.833333333333333" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1/2 + 1/3; print a\n", "N(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

タプル

\n", "\t

\n", "\t\tタプルは、カンマで区切られた値からなるシーケンスデータ型です。\n", "\t\tタプルの出力は()カッコで括られたカンマ区切りの形式で表示されます。\n", "\t

\n", "\t

\n", "\t\tタプルの変数への代入をタプル・パッキングと呼び、逆にタプルから変数への代入をシーケンス・アンパッキングと呼びます。\n", "\t\tシーケンス・アンパッキングは変数への一括代入に使用すると便利です。\n", "\t

\n", "\t

\n", "\t\tリストや辞書と合わせるために、あえてカッコ()をつけてタプルを使用しることにします。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 3)\n", "1 2 3\n" ] } ], "source": [ "t = (1, 2, 3); print t # 変数tへのタプル(1, 2, 3)の代入(タプル・パッキング)\n", "(x, y, z) = t; print x, y, z # タプルtから変数x, y, zへの代入(シーケンス・アンパッキング)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

リスト

\n", "\t

\n", "\t\tsageでは、リストがもっとも重要なデータ構造になります。\n", "\t\tタプルとは異なり、リストでは要素の参照の他に、要素の追加、削除、結合などの操作ができます。\n", "\t

\n", "\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n", "[1, 2, 3, 4]\n", "3\n", "[2, 3]\n", "4\n", "[1, 2, 3, 4, 9, 8, 7]\n" ] } ], "source": [ "L1 = [1, 2, 3]; print L1 # リストの生成\n", "L2 = [9, 8, 7]\n", "L1.append(4); print L1 # 要素の追加\n", "print(L1[2]) # 要素の取得\n", "print(L1[1:3]) # 部分リストの取得\n", "print(L1[-1]) # 最後の要素は、-1で参照\n", "print(L1 + L2) # リストの結合 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

辞書

\n", "\t

\n", "\t\t辞書、別名「連想配列」と言われ、リストのインデックスの代わりにキーで値を取得することが\n", "\t\tできます。\n", "\t\t

\n", "\t

\n", "\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Sarah': 28, 'Mike': 31, 'John': 24}\n", "28\n" ] } ], "source": [ "ages = {'John':24, 'Sarah':28, 'Mike':31}; # 辞書の生成\n", "print(ages) # 辞書の出力\n", "print(ages['Sarah']) # 要素の取得" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

変数の宣言

\n", "\t

\n", "\t\tsageの式で変数として認識させるには変数をvar関数で宣言しなくてなりません。変数の宣言は、\n", "\t\t

\n",
    "var('変数名')\n",
    "\t\t
\n", "\t\t複数の変数を宣言する場合には、スペースを空けて指定します。 宣言される変数を参照したりする場合には、\n", "\t\t
\n",
    "x, y = var('x y')\n",
    "\t\t
\n", "\t\tのようにpython変数x, yに宣言したsage変数を代入します。\n", "\t

\n", "\n", "\n", "\n", "\t

関数の定義

\n", "\t

\n", "\t\tsageの関数には、以下の3種類の指定方法があります。必要に応じて使い分けて下さい。\n", "\t\t

\n", "\t\t例では、$sqrt(x)=x^{\\frac{1}{2}}$を返す関数をシンボリック関数、python関数、lambda関数を\n", "\t\tそれぞれ symRoot, pyRoot, lamRoot として定義し、その使い方を説明します。\n", "\t\t当然すべて、同じ結果になります。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(sqrt(2), sqrt(2), sqrt(2))\n", "(sqrt(x + y), sqrt(x + y), sqrt(x + y))\n" ] } ], "source": [ "# シンボリック関数\n", "symRoot(x) = x^(1/2)\n", "# python関数\n", "def pyRoot(x):\n", " return x^(1/2)\n", "# lambda関数\n", "lamRoot = lambda x: x^(1/2)\n", "# 関数の呼び出し\n", "print(symRoot(2), pyRoot(2), lamRoot(2))\n", "# 数式が引数の場合\n", "x, y = var('x y')\n", "print(symRoot(x+y), pyRoot(x+y), lamRoot(x+y) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

値の代入

\n", "\t

\n", "\t\t変数の値を指定して関数の値を取得するには、以下の3つの方法があります。\n", "\t\t

\t\t\n", "\t

\n", "\t

\n", "\t\t以下の例では$f(x) = a x + b$の関数をf(x)、g(x, a, b)で定義した場合のa, bの値の代入方法\n", "\t\tを示しています。subsの場合には、a=2, b=1のようにカンマで区切って値をセットする方法と辞書形式\n", "\t\tで与える方法が使用できます。var関数で変数を定義すればa=c, b=dのように別の変数への置換も可能です。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2*x + 1\n", "2*x + 1\n", "x |--> 2*x + 1\n", "x |--> 2*x + 1\n", "x |--> 2*c*x + d + 1\n" ] } ], "source": [ "# 関数f, g、変数x, a, bを定義\n", "var('x a b')\n", "f(x) = a*x + b\n", "g(x, a, b) = f\n", "print( g(x, 2, 1) )\n", "print( f(a=2, b=1) )\n", "print( f.subs(a=2, b=1) )\n", "print( f.subs({a:2, b:1}) )\n", "# 変数c, dへの置換\n", "var('c d')\n", "print( f.subs(a=2*c, b=d+1) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

規則の代入

\n", "\t

\n", "\t\t規則の代入には、substitute関数を使います。例として、関数hの$x^2$をwに置き換えてみます。\n", "\t

\n", "\t

\n", "\t\t変数の置換では行えない変換もsubstituteを使うことによって可能になります。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x^2 + 3\n", "w + 3\n" ] } ], "source": [ "var('w')\n", "h = x^2 + 3; print( h ) # 関数hの定義\n", "print( h.substitute(x^2 == w) ) # x^2をwに置き換える " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

前の出力結果の参照

\n", "\t

\n", "\t\t直前の実行結果を _ で参照することができます。 \n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1, 2, 3] " ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(_) # 直前の実行結果を_で参照し、リストの和を求めます。 " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SageMath 7.2", "language": "", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }