数式でよく使われる定数をSageで使うには以下の様に表します。
|
四則演算をはじめ、Sageで使われる基本的な計算の表現方法を以下に示します。
|
1から10までの和は55ですが、これをリストとsum関数を使って計算すると、 以下の様になります。
55 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 55 55 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 55 |
リスト内包を使うと簡単にリストの要素を変更することができます。 先ほどのリストLの要素を自乗に変えて、和を求めてみましょう。
リスト内包の書式 [ 式 for 変数 in リスト ]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] 385 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] 385 |
sumと並んでリストの要素の積を求めるprodもよく使われます。 1から5までの積をprodを使って計算してみましょう。
今回は、range関数の代わりにジェネレータを使って1から5までの リストを生成し、計算してみます。
(1..5)
120 120 120 120 |
次に中学で習った式の因数分解や展開もSageを使うと簡単に計算することができます。
以下の関数f1をSageを使って展開してみます。展開にはexpand関数を使います。 $$ f1(x) = (x - 1)(x^2 -1) $$
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x - 1\right)} {\left(x^{2} - 1\right)} \newcommand{\Bold}[1]{\mathbf{#1}}x^{3} - x^{2} - x + 1 \newcommand{\Bold}[1]{\mathbf{#1}}{\left(x - 1\right)} {\left(x^{2} - 1\right)} \newcommand{\Bold}[1]{\mathbf{#1}}x^{3} - x^{2} - x + 1 |
多項式の係数は、coefficientsやcoeff関数で取得できます。
以下に、f2の係数をcoeeficents関数で取得する方法とxの2次の項の係数をcoeff関数で取得する方法を例として示します。
[[1, 0], [-1, 1], [-1, 2], [1, 3]] -1 [[1, 0], [-1, 1], [-1, 2], [1, 3]] -1 |
f1を展開した結果f2を因数分解してみましょう。因数分解にはfactor関数を使用します。
結果がf1と異なりますが、 $$ (x^2 - 1) = (x - 1)(x + 1) $$ の関係から、正しい結果になっていることが分かります。
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x - 1\right)}^{2} {\left(x + 1\right)}
\newcommand{\Bold}[1]{\mathbf{#1}}{\left(x - 1\right)}^{2} {\left(x + 1\right)}
|
分数で表される式の積分やラブラス逆変換では部分分数分解を利用することがあります。
以下の様な関数f3を因数分解して、分数式f4に変えます。 $$ f3(x) = \frac{1}{(x - 2)} + \frac{1}{(x + 2)} $$
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{x - 2} + \frac{1}{x + 2}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{x - 2} + \frac{1}{x + 2}
|
式の部分分数分解には、prtial_fraction()メソッドを使います。f4にprtial_fractionメソッドを呼び出すとf3と同じ結果となります。
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{2 \, x}{{\left(x - 2\right)} {\left(x + 2\right)}} \newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{x - 2} + \frac{1}{x + 2} \newcommand{\Bold}[1]{\mathbf{#1}}\frac{2 \, x}{{\left(x - 2\right)} {\left(x + 2\right)}} \newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{x - 2} + \frac{1}{x + 2} |
式を整理して簡単化する関数として、simplifyがあります。simplifyでは不要な項を消去するだけなので、 f1のような因数分解で整理されて式の場合には結果が変わりません。
さらに突っ込んだ整理をする場合には、simplify_fullメソッドを使います。
\newcommand{\Bold}[1]{\mathbf{#1}}i \newcommand{\Bold}[1]{\mathbf{#1}}{\left(x - 1\right)} {\left(x^{2} - 1\right)} \newcommand{\Bold}[1]{\mathbf{#1}}x^{3} - x^{2} - x + 1 \newcommand{\Bold}[1]{\mathbf{#1}}i \newcommand{\Bold}[1]{\mathbf{#1}}{\left(x - 1\right)} {\left(x^{2} - 1\right)} \newcommand{\Bold}[1]{\mathbf{#1}}x^{3} - x^{2} - x + 1 |
三角関数も数式として計算するため、$ sin (\pi/4) $も数値ではなく、 $ \frac{1}{2} \sqrt{2} $の式が返ってきます。
値を得るには、N関数を使用します。
1/2*sqrt(2) 0.707106781186548 1/2*sqrt(2) 0.707106781186548 |
また三角関数への入力単位は度ではなく、ラジアンで指定します。
度とラジアンの変換には、以下の様な関数rad, degを定義すると便利です。
|
1/2*sqrt(2) 1/2*sqrt(2) 1/4*pi 45 1/2*sqrt(2) 1/2*sqrt(2) 1/4*pi 45 |
三角関数や指数関数を含む式の簡単化には simplify_fullメソッドを使います。
例として、sin関数の倍角公式をsimplify_fullメソッドで求めてみます。
\newcommand{\Bold}[1]{\mathbf{#1}}\sin\left(2 \, x\right) \newcommand{\Bold}[1]{\mathbf{#1}}2 \, \sin\left(x\right) \cos\left(x\right) \newcommand{\Bold}[1]{\mathbf{#1}}\sin\left(2 \, x\right) \newcommand{\Bold}[1]{\mathbf{#1}}2 \, \sin\left(x\right) \cos\left(x\right) |
更に、simplify_fullでは三角関数の公式を活用して式を整理します。
以下の例では、以下の三角関数の公式を使って簡単化しています。 $$ cos^2 x + sin^2 x = 1 $$
\newcommand{\Bold}[1]{\mathbf{#1}}-\sin\left(x\right)^{2} + \cos\left(x\right)^{2} \newcommand{\Bold}[1]{\mathbf{#1}}2 \, \cos\left(x\right)^{2} - 1 \newcommand{\Bold}[1]{\mathbf{#1}}-\sin\left(x\right)^{2} + \cos\left(x\right)^{2} \newcommand{\Bold}[1]{\mathbf{#1}}2 \, \cos\left(x\right)^{2} - 1 |
高校で習った、導関数をSageを使って導いてみましょう。 関数f(x)と平均変化率g(x)を以下の様に定義します。 $$ f(x) = \frac{1}{2} x^3 $$ $$ g(x)= \frac{f(x+h)−f(x)}{h} $$
変数x, hと関数f, gをSageで定義します。
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ \frac{1}{2} \, x^{3}
\newcommand{\Bold}[1]{\mathbf{#1}}x \ {\mapsto}\ \frac{1}{2} \, x^{3}
|
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{{\left(h + x\right)}^{3} - x^{3}}{2 \, h}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{{\left(h + x\right)}^{3} - x^{3}}{2 \, h}
|
gを展開して整理すると以下の様になり、h→0の極値(limit)をとった時、hが掛からないxの2次の項のみが残ります。
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{2} \, h^{2} + \frac{3}{2} \, h x + \frac{3}{2} \, x^{2}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{2} \, h^{2} + \frac{3}{2} \, h x + \frac{3}{2} \, x^{2}
|
求める導関数は、以下の様になります。
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{3}{2} \, x^{2}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{3}{2} \, x^{2}
|
次のような特殊な関数fを考えます。 $$ f(x) = \frac{sin(x)}{x} $$
この関数にx=0を代入するとゼロ割のエラーとなりますが、limitを使ってx→0を求めると1となります。
Sageではグラフの表示でも極値をきちんと計算しているので、上記のf(x)もきちんと表示することができます。
Traceback (click to the left of this block for traceback) ... RuntimeError: power::eval(): division by zero Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_71.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("IyDnibnmrorjgarplqLmlbDjga7jg6rjg5/jg4Pjg4gKZiA9IHNpbih4KS94CnByaW50IGYoeD0wKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpycTePd/___code___.py", line 4, in <module> exec compile(u'print f(x=_sage_const_0 ) File "", line 1, in <module> File "expression.pyx", line 3652, in sage.symbolic.expression.Expression.__call__ (sage/symbolic/expression.cpp:16183) File "ring.pyx", line 655, in sage.symbolic.ring.SymbolicRing._call_element_ (sage/symbolic/ring.cpp:6633) File "expression.pyx", line 3503, in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:15547) RuntimeError: power::eval(): division by zero |
1 1 |
|
Sageでの関係式の扱い方を以下の関係式eqを例に示します。 $$ x + 2 \le 0 $$
これをSageで表現すると以下の様になります。
|
関係式から左辺、等号、不等号のオペレータ、左辺を取得するには、それぞれlhs, operator, rhs関数を使用します。
x + 2 <built-in function le> 0 x + 2 <built-in function le> 0 |
|