{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "# 数値計算入門\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" }, "tags": [] }, "source": [ "## よいプログラムを書くために注意すべきこと\n", "\n", "* プログラムにコメントを含める\n", "\n", "* 意味のある変数名を使用する\n", "\n", "* 適切なタイプの変数を使用する\n", "\n", "* 定数を定義する\n", "\n", "* 実行に時間がかかる場合は、プログラム全体の部分的な結果と更新を出力します\n", "\n", "* プログラムを明確にレイアウトする\n", "\n", "* プログラムを不必要に複雑にしない\n", "\n", "* 最初に関数をインポートする\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## 復習\n", "\n", "### 基本的な変数と型\n", " integers、float、bollean、complexなど\n", "\n", "\n", "### 変数と型\n", " 通常の変数名は文字で始まる必要があります。慣例により、変数名は小文字で始まり、クラス名は大文字で始まります。\n", " \n", " 変数名には、英数字 a ~ z、A ~ Z、0 ~ 9、および _ などの一部の特殊文字を含めることができます。\n", " \n", " 変数名として使用できない Python キーワードがいくつかあります。$ and, as, assert, print......\n", " \n", " Python の代入演算子は = です。Python は動的型付け言語であり、C/C++ とは異なり、変数の型を指定する必要はありません\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0 1.0\n" ] } ], "source": [ "a = 1\n", "c = True\n", "d = 1+1.0j\n", "b = 2.0\n", "B = 1.0\n", "print(b, B)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 \n", "2.0 \n", "True \n", "(1+1j) \n" ] } ], "source": [ "print(a, type(a))\n", "print(b, type(b))\n", "print(c, type(c))\n", "print(d, type(d))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### クイズ\n", "1. 複素変数の実部と虚部を見つける方法は? " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1+2j)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex_number = 1+2.0j\n", "complex_number" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.0\n", "2.0\n" ] } ], "source": [ "print(complex_number.real)\n", "print(complex_number.imag)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### 算術演算子と比較演算子\n", " ほとんどの算術演算子と比較演算子を使用することができます。\n", " \n", " 算術演算子 +, -, *, /, // (整数除算), '**' 乗数\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 -1 2 0.5 0 2\n", "False False True\n" ] } ], "source": [ "x, y = 1, 2\n", "print(x+y, x-y, x*y, x/y, x//y, y**x)\n", "print(x>y, x==y, x= x.\n", " \n", " comb(n, k, /)\n", " Number of ways to choose k items from n items without repetition and without order.\n", " \n", " Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates\n", " to zero when k > n.\n", " \n", " Also called the binomial coefficient because it is equivalent\n", " to the coefficient of k-th term in polynomial expansion of the\n", " expression (1 + x)**n.\n", " \n", " Raises TypeError if either of the arguments are not integers.\n", " Raises ValueError if either of the arguments are negative.\n", " \n", " copysign(x, y, /)\n", " Return a float with the magnitude (absolute value) of x but the sign of y.\n", " \n", " On platforms that support signed zeros, copysign(1.0, -0.0)\n", " returns -1.0.\n", " \n", " cos(x, /)\n", " Return the cosine of x (measured in radians).\n", " \n", " cosh(x, /)\n", " Return the hyperbolic cosine of x.\n", " \n", " degrees(x, /)\n", " Convert angle x from radians to degrees.\n", " \n", " dist(p, q, /)\n", " Return the Euclidean distance between two points p and q.\n", " \n", " The points should be specified as sequences (or iterables) of\n", " coordinates. Both inputs must have the same dimension.\n", " \n", " Roughly equivalent to:\n", " sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))\n", " \n", " erf(x, /)\n", " Error function at x.\n", " \n", " erfc(x, /)\n", " Complementary error function at x.\n", " \n", " exp(x, /)\n", " Return e raised to the power of x.\n", " \n", " exp2(x, /)\n", " Return 2 raised to the power of x.\n", " \n", " expm1(x, /)\n", " Return exp(x)-1.\n", " \n", " This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.\n", " \n", " fabs(x, /)\n", " Return the absolute value of the float x.\n", " \n", " factorial(n, /)\n", " Find n!.\n", " \n", " Raise a ValueError if x is negative or non-integral.\n", " \n", " floor(x, /)\n", " Return the floor of x as an Integral.\n", " \n", " This is the largest integer <= x.\n", " \n", " fmod(x, y, /)\n", " Return fmod(x, y), according to platform C.\n", " \n", " x % y may differ.\n", " \n", " frexp(x, /)\n", " Return the mantissa and exponent of x, as pair (m, e).\n", " \n", " m is a float and e is an int, such that x = m * 2.**e.\n", " If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.\n", " \n", " fsum(seq, /)\n", " Return an accurate floating point sum of values in the iterable seq.\n", " \n", " Assumes IEEE-754 floating point arithmetic.\n", " \n", " gamma(x, /)\n", " Gamma function at x.\n", " \n", " gcd(*integers)\n", " Greatest Common Divisor.\n", " \n", " hypot(...)\n", " hypot(*coordinates) -> value\n", " \n", " Multidimensional Euclidean distance from the origin to a point.\n", " \n", " Roughly equivalent to:\n", " sqrt(sum(x**2 for x in coordinates))\n", " \n", " For a two dimensional point (x, y), gives the hypotenuse\n", " using the Pythagorean theorem: sqrt(x*x + y*y).\n", " \n", " For example, the hypotenuse of a 3/4/5 right triangle is:\n", " \n", " >>> hypot(3.0, 4.0)\n", " 5.0\n", " \n", " isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)\n", " Determine whether two floating point numbers are close in value.\n", " \n", " rel_tol\n", " maximum difference for being considered \"close\", relative to the\n", " magnitude of the input values\n", " abs_tol\n", " maximum difference for being considered \"close\", regardless of the\n", " magnitude of the input values\n", " \n", " Return True if a is close in value to b, and False otherwise.\n", " \n", " For the values to be considered close, the difference between them\n", " must be smaller than at least one of the tolerances.\n", " \n", " -inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n", " is, NaN is not close to anything, even itself. inf and -inf are\n", " only close to themselves.\n", " \n", " isfinite(x, /)\n", " Return True if x is neither an infinity nor a NaN, and False otherwise.\n", " \n", " isinf(x, /)\n", " Return True if x is a positive or negative infinity, and False otherwise.\n", " \n", " isnan(x, /)\n", " Return True if x is a NaN (not a number), and False otherwise.\n", " \n", " isqrt(n, /)\n", " Return the integer part of the square root of the input.\n", " \n", " lcm(*integers)\n", " Least Common Multiple.\n", " \n", " ldexp(x, i, /)\n", " Return x * (2**i).\n", " \n", " This is essentially the inverse of frexp().\n", " \n", " lgamma(x, /)\n", " Natural logarithm of absolute value of Gamma function at x.\n", " \n", " log(...)\n", " log(x, [base=math.e])\n", " Return the logarithm of x to the given base.\n", " \n", " If the base not specified, returns the natural logarithm (base e) of x.\n", " \n", " log10(x, /)\n", " Return the base 10 logarithm of x.\n", " \n", " log1p(x, /)\n", " Return the natural logarithm of 1+x (base e).\n", " \n", " The result is computed in a way which is accurate for x near zero.\n", " \n", " log2(x, /)\n", " Return the base 2 logarithm of x.\n", " \n", " modf(x, /)\n", " Return the fractional and integer parts of x.\n", " \n", " Both results carry the sign of x and are floats.\n", " \n", " nextafter(x, y, /)\n", " Return the next floating-point value after x towards y.\n", " \n", " perm(n, k=None, /)\n", " Number of ways to choose k items from n items without repetition and with order.\n", " \n", " Evaluates to n! / (n - k)! when k <= n and evaluates\n", " to zero when k > n.\n", " \n", " If k is not specified or is None, then k defaults to n\n", " and the function returns n!.\n", " \n", " Raises TypeError if either of the arguments are not integers.\n", " Raises ValueError if either of the arguments are negative.\n", " \n", " pow(x, y, /)\n", " Return x**y (x to the power of y).\n", " \n", " prod(iterable, /, *, start=1)\n", " Calculate the product of all the elements in the input iterable.\n", " \n", " The default start value for the product is 1.\n", " \n", " When the iterable is empty, return the start value. This function is\n", " intended specifically for use with numeric values and may reject\n", " non-numeric types.\n", " \n", " radians(x, /)\n", " Convert angle x from degrees to radians.\n", " \n", " remainder(x, y, /)\n", " Difference between x and the closest integer multiple of y.\n", " \n", " Return x - n*y where n*y is the closest integer multiple of y.\n", " In the case where x is exactly halfway between two multiples of\n", " y, the nearest even value of n is used. The result is always exact.\n", " \n", " sin(x, /)\n", " Return the sine of x (measured in radians).\n", " \n", " sinh(x, /)\n", " Return the hyperbolic sine of x.\n", " \n", " sqrt(x, /)\n", " Return the square root of x.\n", " \n", " tan(x, /)\n", " Return the tangent of x (measured in radians).\n", " \n", " tanh(x, /)\n", " Return the hyperbolic tangent of x.\n", " \n", " trunc(x, /)\n", " Truncates the Real x to the nearest Integral toward 0.\n", " \n", " Uses the __trunc__ magic method.\n", " \n", " ulp(x, /)\n", " Return the value of the least significant bit of the float x.\n", "\n", "DATA\n", " e = 2.718281828459045\n", " inf = inf\n", " nan = nan\n", " pi = 3.141592653589793\n", " tau = 6.283185307179586\n", "\n", "FILE\n", " /Users/iikubo/miniconda3/lib/python3.11/lib-dynload/math.cpython-311-darwin.so\n", "\n", "\n" ] } ], "source": [ "help(math)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 文字列" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to 融合基礎情報学I \n" ] } ], "source": [ "Strings = \"Welcome to 融合基礎情報学I\"\n", "print(Strings, type(Strings))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "welcome to 融合基礎情報学I\n" ] } ], "source": [ "# 文字列計算もできます\n", "str1 = 'welcome to' \n", "str2 = ' 融合基礎情報学I'\n", "print(str1+str2)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### リスト" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Welcome to 融合基礎情報学'] \n" ] } ], "source": [ "Lists = ['Welcome to 融合基礎情報学']\n", "print(Lists, type(Lists))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### forループ\n", "Pythonでは、リストなどの反復可能なオブジェクトと一緒にfor ループが使用されます。\n", "\n", "基本的な構文は次のとおりです。\n", "\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "1\n", "complete\n", "2\n", "4\n", "complete\n", "3\n", "9\n", "complete\n" ] } ], "source": [ "for x in [1,2,3]:\n", " print(x)\n", " print(x**2)\n", " print('complete')\n", " " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### 条件分岐\n", "if文を使って条件により処理を分岐させます。基本的な構文は次のとおりです。\n", "\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "true\n" ] } ], "source": [ "x=3\n", "y=3\n", "if (x>=y):\n", " print(\"true\") \n", "else:\n", " print(\"false\")\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### whileループ\n", "while文を使って処理を繰り返し行わせることができます。基本的な構文は次のとおりです。\n", "\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cycle-----------: 0 nm\n", "cycle-----------: 1 nm\n", "cycle-----------: 2 nm\n", "cycle-----------: 3 nm\n", "cycle-----------: 4 nm\n", "cycle-----------: 5 nm\n", "cycle-----------: 6 nm\n", "cycle-----------: 7 nm\n", "cycle-----------: 8 nm\n", "cycle-----------: 9 nm\n" ] } ], "source": [ "i = 0\n", "while True:\n", " print(\"cycle-----------: \", i, 'nm')\n", " #i = i + 1 works\n", " #i++ not work\n", " i+=1\n", " if i == 10:\n", " break" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" } }, "nbformat": 4, "nbformat_minor": 4 }