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

ベクトルと行列

\n", "\t

\n", "\t\tベクトルと行列の計算は、幅広い分野で使われています。\n", "\t\tSageを使ってベクトルと行列の特徴的な計算方法について見ていきましょう。\t\t\n", "\t

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

ベクトルの生成

\n", "\t

\n", "\tsageでベクトルを生成する関数は、vector関数です。vector関数の使い方を以下に示します。\n", "

\n",
    "vector(値のリスト)\n",
    "または、\n",
    "vector(環, 値のリスト)\n",
    "
\n", "\t\n", "\t

\n", "\t

\n", "\t\tベクトルの生成例として、$v=(2, 1, 3), w=(1, 1,-4)$の例を以下に示します。環については後ほど紹介します。\t\t\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# jupyter用のdisplayメソッド\n", "from IPython.display import display, Latex, HTML, Math, JSON" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(2, 1, 3)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ベクトルと行列\n", "v = vector([2,1,3]); v" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1, 1, -4)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w = vector([1,1,-4]); w" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(3, 2, -1)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ベクトルの和\n", "v+w" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

ベクトルの計算

\n", "\t

\n", "\t\tSageではベクトルの和と差は通常の変数と同じように+, -記号で表すこととができます。\n", "\t

\n", "\t

\n", "\t\tベクトルのスカラー倍は、ベクトルの各要素にスカラを掛けた形になります。\n", "\t

\n", "\t

\n", "\t\tベクトルの絶対値は、ベクトル用各要素の自乗の和を平方根となり、ベクトルの距離は2つのベクトルの差の絶対値となります。\n", "\t

\n", "\t

\n", "\t\tこのようにSageではベクトルに対して、変数と同じように計算ができる点が特徴です。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "(a1 + b1, a2 + b2, a3 + b3)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "(a1*c, a2*c, a3*c)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "sqrt(a1^2 + a2^2 + a3^2)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "sqrt((a1 - b1)^2 + (a2 - b2)^2 + (a3 - b3)^2)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "var('a1 a2 a3 b1 b2 b3 c')\n", "A = vector([a1, a2, a3])\n", "B = vector([b1, b2, b3])\n", "show (A+B) # ベクトルの和\n", "show (c*A) # ベクトルのスカラー倍\n", "show (abs(A)) # ベクトルの絶対値\n", "show (abs(A-B)) # ベクトルの距離" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

ベクトルの内積

\n", "\t

\n", "\t\tベクトルには、内積と外積という特徴的な2つの演算があります。\n", "\t

\n", "\t

\n", "\t\t内積は、2つのベクトルの各要素の積の和として、以下のように定義されます。\n", "$$\n", "\t\t\\mathbf{v}\\cdot\\mathbf{w} = \\Sigma_{i=1}^N v_i w_i\n", "$$\n", "\t

\n", "\t

\n", "\t\t先に生成したベクトルA, Bに対して内積を取ると、定義どおりの結果が得られます。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "a1*b1 + a2*b2 + a3*b3" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "AdB = A.dot_product(B)\n", "show(AdB)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

\n", "\t\t内積の大きな特徴に、2つのベクトルのなす角度$\\theta$との間に以下の関係があります。\n", "$$\n", "\t\t\\mathbf{v}\\cdot\\mathbf{w} = |\\mathbf{v}||\\mathbf{w}| cos \\theta\n", "$$\t\t\n", "\t\tこの性質を使って、ベクトルの類似を計算するのに、$cos(\\theta)$がよく使われます。\n", "\t

\n", "\t

\n", "\t\tまた、2つのベクトルが直行する場合には、$cos \\theta = 0$から、\n", "\t\tベクトルの内積はベクトルの直交判定にもよく利用されます。\n", "\t

\n", "\t

\n", "\t\t以下に、$v=(2, 1, 3), w=(1, 1,-4)$の内積の結果とベクトルv, wのなす角度$\\theta$を\n", "\t\t求めています。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-9" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v.dot_product(w)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "124.537583786181\n" ] } ], "source": [ "deg = lambda x : N(x * 180/pi)\n", "# vとwからcos(th)を計算\n", "cos_th = v.dot_product(w)/(abs(v)*abs(w))\n", "th = arccos(cos_th)\n", "print deg(th)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

\n", "\t\t3次元プロットで、ベクトルv,wをプロットしたのが以下の図です。\n", "\t\t2つのベクトルのなす角度が約120度であることが、見て取れます。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "Graphics3d Object" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ZERO = vector([0, 0, 0])\n", "v_line = line([ZERO, v], rgbcolor='blue')\n", "w_line = line([ZERO, w], rgbcolor='green')\n", "vw_line = line([ZERO, v.cross_product(w)], rgbcolor='red')\n", "(v_line+w_line).show(aspect_ratio=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

ベクトルの外積

\n", "\t

\n", "\t\t同様にベクトルの外積$\\mathbf{v}\\times\\mathbf{w}$は、cross_product関数で計算します。\n", "\t

\n", "\t

\n", "\t\t3次元ベクトルAとBの外積の結果は、以下のようなります。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "(-a3*b2 + a2*b3, a3*b1 - a1*b3, -a2*b1 + a1*b2)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "AcB = A.cross_product(B)\n", "show(AcB)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

\n", "\t\tちょっと覚えにくいので、以下の性質を使って覚えると良いでしょう。\n", "$$\n", "\t\t\\mathbf{A}\\times\\mathbf{B} = \n", "\t\t\t\\left| \\begin{array}{ccc}\n", "\t\t\t\ti & j & k \\\\\n", "\t\t\t\ta_{1} & a_{2} & a_{3} \\\\\n", "\t\t\t\tb_{1} & b_{2} & b_{3}\n", "\t\t\t\\end{array} \\right|\n", "$$\t\t\n", "\t

\n", "\t

\n", "\t\tx, y, z方向の単位ベクトルU=(i, j, k)を定義し、U, A, Bからなる行列mを作り、\n", "\t\tそのdetを求める、結果をそれぞれ、$i, j, k$でまとめると、\n", "$$\n", "\t\t(a_2 b_3 - a_3 b_2) i + (-a_1 b_3 + a_3 b_1) j + (a_1 b_2 - a_2 b_1) k\n", "$$\n", "\t\tと先の外積の結果と対応することが分かります。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "[ i j k]\n", "[a1 a2 a3]\n", "[b1 b2 b3]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "-a3*b2*i + a2*b3*i + a3*b1*j - a1*b3*j - a2*b1*k + a1*b2*k" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "var('i j k')\n", "U = vector([i, j, k])\n", "m = matrix([U, A, B]); show(m)\n", "dm = det(m); show(expand(dm))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

\n", "\tベクトルの外積の図形的特徴は、外積の方向はベクトルvからベクトルwにねじを回して進む方向となり、\n", "\tその大きさはベクトルvとベクトルwの作る平行四辺形の面積となります。\n", "$$\n", "\t|\\mathbf{v}\\times\\mathbf{w}| = |\\mathbf{v}||\\mathbf{w}|sin(\\theta)\n", "$$\t\t\n", "\t

\n", "\t

\n", "\t\tSageを使ってベクトルv, wの外積を求め、その値が $|v||w| cos (\\theta)$と一致することを見てみましょう。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(-7, 11, 1) 13.0766968306220 13.0766968306220\n" ] } ], "source": [ "VcW = v.cross_product(w)\n", "# thは内積で求めた結果を利用\n", "print VcW, N(abs(VcW)), N(abs(v)*abs(w)*sin(th))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

\n", "\t\t3次元プロットで、ベクトルv,wと外積の結果をプロットしたのが以下の図です。\n", "\t\t図を少し回転すると、ベクトルvからベクトルwに回したときのねじの進行方向に外積ベクトルがのびている\n", "\t\tことが確認できます。\t\n", "\t

\n", "\t\n", "" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "Graphics3d Object" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "vw_line = line([ZERO, v.cross_product(w)], rgbcolor='red')\n", "# 図を回転しないと確認しずらいが、vからwにねじを回した方向になっている\n", "(v_line+w_line+vw_line).show(aspect_ratio=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

行列の生成

\n", "\t

\n", "\t\t行列は、matrix関数で生成します。\n", "\t

\n", "\t
\n",
    "\tmatrix(行列の要素のリスト)\n",
    "\tまたは\n",
    "\tmatrix(環, 行列の要素のリスト)\n",
    "\t
\t\t\n", "\t

\n", "\t\t生成された行列の要素は、配列と同じようにカギ括弧[]に要素のインデックスを指定することで、\n", "\t\t参照できます。行を取得する場合には、行のインデックスを指定し、列を取得するにはcolumnメソッド\n", "\t\t列のインデックスを指定することで所望の情報を得ることができます。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "[1 2 3]\n", "[3 2 1]\n", "[1 2 1]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "M = matrix([[1,2,3],[3,2,1],[1,2,1]])\n", "show(M)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(3, 2, 1)\n", "3\n", "(2, 2, 2)\n" ] } ], "source": [ "print M[1] # 2行目(インデックスでは1)を取得\n", "print M[0, 2] # 1行目3列の要素を取得\n", "print M.column(1) # 2列目を取得" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

\n", "\t\t行列とベクトルの積は、*演算子を使って行い、その結果としてベクトルが返されます。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(-9, 1, -1)\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Mw = M*w; print M*w\n", "type(Mw)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

行列の基本演算

\n", "\t

\n", "\t\t行列の基本計算をSageを使ってみてみましょう。行列AとBの和、スカラー倍、積の結果を\n", "\t\t以下に示します。行列の積は、順序によって結果が異なることに注意してください。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "[a11 a12]\n", "[a21 a22]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[b11 b12]\n", "[b21 b22]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[a11 + b11 a12 + b12]\n", "[a21 + b21 a22 + b22]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[a11*c a12*c]\n", "[a21*c a22*c]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[a11*b11 + a12*b21 a11*b12 + a12*b22]\n", "[a21*b11 + a22*b21 a21*b12 + a22*b22]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[a11*b11 + a21*b12 a12*b11 + a22*b12]\n", "[a11*b21 + a21*b22 a12*b21 + a22*b22]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "var('a11 a12 a21 a22 b11 b12 b21 b22')\n", "A = matrix([[a11, a12], [a21, a22]])\n", "B = matrix([[b11, b12], [b21, b22]])\n", "show(A)\n", "show(B)\n", "show(A+B)\t# 行列の和\n", "show(c*A)\t# 行列のスカラー倍\n", "show(A*B)\t# 行列の積(AB順)\n", "show(B*A)\t# 行列の積(BA順)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

単位行列

\n", "\t単位行列は、identity_matrixで生成します。identity_matrixの引数には、行列の次数を指定します。\n", "" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "[1 0 0]\n", "[0 1 0]\n", "[0 0 1]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Im = identity_matrix(3)\n", "show(Im)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

対角行列

\n", "\t対角行列は、diagonal_matrixで生成します。\n", "
\n",
    "diagonal_matrix(対角要素のリスト)\n",
    "
\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "[1 0 0]\n", "[0 2 0]\n", "[0 0 3]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "D = diagonal_matrix([1, 2, 3])\n", "show(D)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

行列の解

\n", "\t

\n", "\t行列MにベクトルXを掛けて、ベクトルYとなる場合、行列MとベクトルYからXを求めるメソッドがsolve_rightです。\n", "$$\n", "\t\\mathbf{M}\\mathbf{X} = \\mathbf{Y}\n", "$$\n", "\t

\n", "\t

\n", "\t\tsolve_rightの例を以下に示します。\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "[1 2 3]\n", "[3 2 1]\n", "[1 2 1]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "(0, -4, -1)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "M = matrix([[1,2,3],[3,2,1],[1,2,1]]); show(M)\n", "Y = vector([0,-4,-1]); show(Y)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "(-3/2, 0, 1/2)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# solve_rightを使って求める\n", "X = M.solve_right(Y); view(X)\n", "# octaveの左除算オペレータ\\を使って求める\n", "X = M \\ Y\n", "show(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t$\\mathbf{M}\\mathbf{X}$を計算すると、ベクトルY(0, -4, -1)となることから解が正しいことが確認できます。\n", "" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(0, -4, -1)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M*X" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

連立方程式と行列の解の関係

\n", "\t

\n", "\t\t先の行列式は、以下のような連立方程式と一致します。\n", "$$\n", "\t\\left\\{\\begin{array}{rrr}\n", "\tx_1 + 2 x_2 + 3 x_3 & = & 0 \\\\\n", "\t3 x_1 + 2 x_2 + x_3 & = & -4 \\\\\n", "\tx_1 + 2 x_2 + x_3 & = & -1\n", "\t\\end{array}\\right.\t\n", "$$\t\t\n", "\t

\n", "\t

\n", "\t\t上記の連立方程式をsolve関数で計算結果とXの値が同じになることをSageで確かめてみましょう。\n", "\t

\n", "\t\n", "" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "[[x1 == (-3/2), x2 == 0, x3 == (1/2)]]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "(x1, x2, x3) = var('x1,x2, x3')\n", "eq = [ [ x1 + 2*x2 + 3*x3 == 0], [3*x1 + 2*x2 + x3 == -4], [x1 + 2*x2 + x3 == -1]]\n", "sol = solve(eq, [x1, x2, x3]); show(sol)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

転置行列

\n", "\t

\n", "\t\t転置行列は、transpose関数で取得できます。\n", "\t

\n", "\t

\n", "\t\t転置行列の性質は、\n", "\t\t

\n", "\t\t最後の性質は、行列の掛け合わせる順序を入れ替えるときに便利です。\t\n", "\t

\n", "" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "[a11 a21]\n", "[a12 a22]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[b11 b21]\n", "[b12 b22]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "At = A.transpose(); show(At) # 転置行列\n", "Bt = B.transpose(); show(Bt)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(A^T)^T = A\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "[a11 a12]\n", "[a21 a22]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "(A+B)^T = A^T + B^T\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "[a11 + b11 a21 + b21]\n", "[a12 + b12 a22 + b22]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[a11 + b11 a21 + b21]\n", "[a12 + b12 a22 + b22]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "print(\"(A^T)^T = A\")\n", "show(At.transpose())\n", "print(\"(A+B)^T = A^T + B^T\")\n", "show(At + Bt)\n", "show((A+B).transpose())" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(AB)^T = B^T A^T\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "[a11*b11 + a12*b21 a21*b11 + a22*b21]\n", "[a11*b12 + a12*b22 a21*b12 + a22*b22]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[a11*b11 + a21*b12 a11*b21 + a21*b22]\n", "[a12*b11 + a22*b12 a12*b21 + a22*b22]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "print(\"(AB)^T = B^T A^T\")\n", "show((A*B).transpose())\n", "show(At*Bt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

行列式

\n", "\t

\n", "\t行列式detは、以下のように定義されます。\n", "$$\n", "\t\tdet(\\mathbf{A}) = \n", "\t\t\t\\left| \\begin{array}{cc}\n", "\t\t\t\ta_{11} & a_{12} \\\\\n", "\t\t\t\ta_{21} & a_{21}\n", "\t\t\t\\end{array} \\right|\n", "$$\t\t\t\t\n", "\t

\n", "\t

\n", "\t\t行列式は、逆行列の計算に使われるので、よく逆行列が存在するか否かの判別に使用されます。\n", "\t

\n", "\t

\n", "\t\tSageでは、行列式はdet関数を使って計算されます。\n", "\t

\n", "\t\n", "" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "-a12*a21 + a11*a22" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show(A.det())" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n" ] } ], "source": [ "Mdet = M.det(); print Mdet" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t\n", "\t

逆行列

\n", "\t

\n", "\t\t逆行列は、inverse関数で取得できます。\n", "\t

\n", "\t

\n", "\t\t逆行列の性質は、以下の通りです。\n", "\t\t

\t\t\n", "\t

\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "[ 0 1/2 -1/2]\n", "[-1/4 -1/4 1]\n", "[ 1/2 0 -1/2]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Minv = M.inverse()\n", "show(Minv)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "$(A^{-1})^{-1} = A$\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "[1 2 3]\n", "[3 2 1]\n", "[1 2 1]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "print(\"$(A^{-1})^{-1} = A$\")\n", "show(Minv.inverse())" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(A^{T})^{-1} = (A^{-1})^{T}\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "[ 0 -1/4 1/2]\n", "[ 1/2 -1/4 0]\n", "[-1/2 1 -1/2]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "[ 0 -1/4 1/2]\n", "[ 1/2 -1/4 0]\n", "[-1/2 1 -1/2]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "print(\"(A^{T})^{-1} = (A^{-1})^{T}\")\n", "show(M.transpose().inverse())\n", "show(M.inverse().transpose())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\t

\n", "\t

\n", "\t\t環は、数の集合を表します。\n", "\t\tよく使われる環を以下に示します。\n", "\t\t