{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "コード2.1■リストでベクトルを表す（その1）"
      ],
      "metadata": {
        "id": "NKnmqJptUfKA"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "oiRRdwBxUZmh"
      },
      "outputs": [],
      "source": [
        "a = [1, 2, 3]\n",
        "b = [4, 5, 6]\n",
        "c = [7, 8, 9, 10]"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "コード2.2■リストでベクトルを表す（その2）"
      ],
      "metadata": {
        "id": "RjRkzjS8Umle"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "d = ['apple', 150, 'lemon', 180]\n",
        "e = [True, False, True, True]"
      ],
      "metadata": {
        "id": "YTVJJjXhUdkn"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "コード2.3■リストの要素を取り出す"
      ],
      "metadata": {
        "id": "LXvX-cdaU3zD"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a = [1,2,3,4,5,6]\n",
        "print(a[0])\n",
        "print(a[1:3])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Nuahi33ZUeY7",
        "outputId": "2b2c6c19-b88f-469a-f396-1f28a6b8046b"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1\n",
            "[2, 3]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "コード2.4■リストの要素を追加"
      ],
      "metadata": {
        "id": "lAAU6e3YVATl"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a = [1,2,3]\n",
        "a.append(4)\n",
        "print(a)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AvnqQIp7Uee3",
        "outputId": "7283e709-73b4-4e43-82cd-c0569792e056"
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[1, 2, 3, 4]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "コード2.5■リストどうしの和や定数倍は？"
      ],
      "metadata": {
        "id": "q0Nc4cfjVFLJ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a = [1,2,3]\n",
        "b = [4,5,6]\n",
        "c = [7,8,9,10]\n",
        "d = ['apple',150,'lemon',180]\n",
        "print(a+b+c)\n",
        "print(a+d)\n",
        "print(3*a)\n",
        "print(3*d)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MHGS0y9EUeiT",
        "outputId": "ab4b7cd5-f2c0-4e34-971a-3c6903369063"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
            "[1, 2, 3, 'apple', 150, 'lemon', 180]\n",
            "[1, 2, 3, 1, 2, 3, 1, 2, 3]\n",
            "['apple', 150, 'lemon', 180, 'apple', 150, 'lemon', 180, 'apple', 150, 'lemon', 180]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "コード2.6■リストどうしの内積？"
      ],
      "metadata": {
        "id": "Fvq64vFaVUbb"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import more_itertools as mit\n",
        "a = [1,2,3]\n",
        "b = [4,5,6]\n",
        "print(mit.dotproduct(a,b))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_IUQ7AA1UelJ",
        "outputId": "415492c2-4f16-4d6a-ab1e-b99d31aaadb5"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "32\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "コード2.7■NumPy配列の成分抽出でベクトルの和，差，定数倍を計算"
      ],
      "metadata": {
        "id": "sI-yYiLmVnNp"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "a = np.array([1,2,3])\n",
        "b = np.array([4,5,6])\n",
        "print(a[0])\n",
        "print(a + b)\n",
        "print(b - a)\n",
        "print(3 * a)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FsHRbpdrVnEa",
        "outputId": "d991e8c6-759c-49c0-f589-5153e39842ba"
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1\n",
            "[5 7 9]\n",
            "[3 3 3]\n",
            "[3 6 9]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "コード2.8■NumPyでベクトルの大きさを計算"
      ],
      "metadata": {
        "id": "RwDBJEsKVmzP"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "a = np.array([1,2])\n",
        "b = np.array([2,3])\n",
        "print(np.linalg.norm(a))\n",
        "print(np.linalg.norm(b))\n",
        "print(np.linalg.norm(b - a))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AvUsRvMMVmo6",
        "outputId": "3edf2f24-4a4f-4632-e4fb-91a81ce440c9"
      },
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "2.23606797749979\n",
            "3.605551275463989\n",
            "1.4142135623730951\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "コード2.9■NumPy配列どうしの積でベクトルの内積を計算"
      ],
      "metadata": {
        "id": "Tuclix2CVmeI"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "a = np.array([1,2,3])\n",
        "b = np.array([4,5,6])\n",
        "print(a * b)\n",
        "print(sum(a * b))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kw6sgQkvVl3s",
        "outputId": "b617142a-b8cd-4167-d516-fa057535f9ee"
      },
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[ 4 10 18]\n",
            "32\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "コード2.10■NumPy配列どうしの積でベクトルの内積を計算（matmul使用）"
      ],
      "metadata": {
        "id": "SI_GHXqcVlb4"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "a = np.array([1,2,3])\n",
        "b = np.array([4,5,6])\n",
        "print(np.matmul(a,b))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "alMH1yb_VocZ",
        "outputId": "967daf3a-f2ef-4918-f721-a14e584808fb"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "32\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "コード2.11■NumPy配列で行列の積"
      ],
      "metadata": {
        "id": "1qRzyvJWWzXy"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "a = np.array([[2,1], [3,4]])\n",
        "b = np.array([[-2,1], [3,-4]])\n",
        "print(3 * a) # 行列の定数倍\n",
        "print(a * b) # 要素ごとの積\n",
        "print(np.matmul(a,b)) # 積"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zu7BFhsjWzRY",
        "outputId": "bd9fc15b-ee9a-4c00-a815-b9a737754fc2"
      },
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[ 6  3]\n",
            " [ 9 12]]\n",
            "[[ -4   1]\n",
            " [  9 -16]]\n",
            "[[ -1  -2]\n",
            " [  6 -13]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "コード2.12■NumPy配列から指数関数のグラフを描く"
      ],
      "metadata": {
        "id": "sCu1AsmRWzFh"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np # ライブラリのインポート\n",
        "import matplotlib.pyplot as plt # ライブラリのインポート\n",
        "x = np.arange(-2, 2, 0.05) # -2≤x<2の数値を0.05刻みで生成\n",
        "y = np.exp(x) # 指数関数の値を計算しyに格納\n",
        "plt.figure(figsize=(12,12)) # 図の大きさを決める\n",
        "plt.rcParams[\"font.size\"] = 18 # フォントサイズを18に\n",
        "plt.plot(x, y, label= r'$y=e^x$') # 座標をプロットし線で繋ぐ\n",
        "plt.legend() # 凡例を表示\n",
        "plt.xticks(np.arange(-2, 2.01, 0.5)) # x軸の表示範囲を設定\n",
        "plt.yticks(np.arange(0, 7.05, 0.5)) # y軸の表示範囲を設定\n",
        "plt.xlabel(r'$x$') # x軸のラベルを設定\n",
        "plt.ylabel(r'$y$') # y軸のラベルを設定\n",
        "plt.grid() # 罫線を表示\n",
        "plt.show() # グラフを表示"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 730
        },
        "id": "YOgUitZaWy-8",
        "outputId": "a4a75cff-06a0-44ac-ea90-38a99ee842e2"
      },
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 864x864 with 1 Axes>"
            ],
            "image/png": "[encoded data, 39612 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "コード2.13■NumPy配列から対数関数のグラフを描く"
      ],
      "metadata": {
        "id": "rQ5GmUP6Wy3c"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "x = np.arange(0.05, 2, 0.05) # 0.05≤x<2の数値を0.05刻みで生成\n",
        "y = np.log(x) # 対数関数の値を計算しy に格納\n",
        "plt.figure(figsize=(12,12))\n",
        "plt.rcParams[\"font.size\"] = 18\n",
        "plt.plot(x, y, label= r'$y=logx$')\n",
        "plt.legend()\n",
        "plt.xticks(np.arange(0, 2.05, 0.5))\n",
        "plt.yticks(np.arange(-3, 1.05, 0.5))\n",
        "plt.xlabel(r'$x$')\n",
        "plt.ylabel(r'$y$')\n",
        "plt.grid()\n",
        "plt.show()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 737
        },
        "id": "C41sIaH-Wyvf",
        "outputId": "25723b62-03b8-456c-ada1-d67988657a4c"
      },
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 864x864 with 1 Axes>"
            ],
            "image/png": "[encoded data, 33768 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "図2.27"
      ],
      "metadata": {
        "id": "z5Yy-hLwYe_G"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from mpl_toolkits.mplot3d import Axes3D   \n",
        "import matplotlib.pyplot as plt \n",
        "import numpy as np\n",
        "import sympy as sym\n",
        "\n",
        "scalerate = 0.02\n",
        "x1, x2 = sym.symbols('x1 x2', real=True)\n",
        "fzz = - (x1-1)**2 - (x2-1)**2 + 2\n",
        "lambdifyFunc_fzz = sym.lambdify([x1, x2], fzz)\n",
        "dfzz_dx1 = sym.diff(fzz, x1)\n",
        "lambdifyFunc_dfzz_dx1 = sym.lambdify([x1, x2], dfzz_dx1)\n",
        "dfzz_dx2 = sym.diff(fzz, x2)\n",
        "lambdifyFunc_dfzz_dx2 = sym.lambdify([x1, x2], dfzz_dx2)\n",
        "def zz1(x,y):\n",
        "  # 作った関数の引数に配列を渡す\n",
        "  zz = lambdifyFunc_fzz(x, y)\n",
        "  return zz\n",
        "\n",
        "def dzzdx1(x,y):\n",
        "  # 作った関数の引数に配列を渡す\n",
        "  dz1 = lambdifyFunc_dfzz_dx1(x, y)\n",
        "  return dz1\n",
        "\n",
        "def dzzdx2(x,y):\n",
        "  # 作った関数の引数に配列を渡す\n",
        "  dz2 = lambdifyFunc_dfzz_dx2(x, y)\n",
        "  return dz2\n",
        "\n",
        "# def zz2(x,y):\n",
        "#   zz= -np.sqrt(x**2 + y**2)\n",
        "#   return zz\n",
        "fig = plt.figure(figsize= (11.69,8.27), dpi=100 ) #プロット領域の作成\n",
        "ax = fig.gca(projection='3d') #プロット中の軸の取得。gca は\"Get Current Axes\" の略。\n",
        "\n",
        "x = np.arange(0, 2, 0.1) # x点として[-2, 2]まで0.05刻みでサンプル\n",
        "y = np.arange(0, 2, 0.1)  # y点として[-2, 2]まで0.05刻みでサンプル\n",
        "x, y = np.meshgrid(x, y)  # 上述のサンプリング点(x,y)を使ったメッシュ生成\n",
        "\n",
        "z1 = zz1(x,y)  #zz1(x,y) を計算してzz座標へ格納する。\n",
        "# z2 = zz2(x,y)\n",
        "u = dzzdx1(x,y)\n",
        "v = dzzdx2(x,y)\n",
        "# w = np.sqrt(u * u + v * v) + 1e-7\n",
        "w = u * u + v * v + 1e-7\n",
        "ax.plot_wireframe(x, y, z1, color='blue',linewidth=0.2) # ワイヤーフレームのプロット。linewidthは曲面のメッシュの線の太さ，をそれぞれ表す。\n",
        "ax.quiver(x, y, z1, u * scalerate, v * scalerate, w * scalerate, \n",
        "          pivot='tail', arrow_length_ratio=0.4)\n",
        "ax.view_init(elev=18, azim=210)  #elevはz軸方向から見た仰角，azimはx, y軸方向の方位角を指定\n",
        "\n",
        "ax.set_xlabel('$X_1$', fontsize=13)\n",
        "ax.set_ylabel('$X_2$', fontsize=13)\n",
        "ax.set_zlabel('$X_3$', fontsize=13)\n",
        "ax.set_zticks(np.arange(0,2.1,.5))\n",
        "# plt.tight_layout()\n",
        "plt.xticks(np.arange(0,2.1,.5))\n",
        "plt.yticks(np.arange(0,2.1,.5))\n",
        "plt.tick_params(labelsize=11)\n",
        "plt.xlim(0,2)\n",
        "plt.ylim(0,2)\n",
        "plt.subplots_adjust()\n",
        "plt.show()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 661
        },
        "id": "yv1SYfdCYfhA",
        "outputId": "4813346d-789a-46dc-a1e8-5c5b8512cd8a"
      },
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 1169x827 with 1 Axes>"
            ],
            "image/png": "[encoded data, 263328 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    }
  ]
}