{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 任意精度演算ライブラリ mpmath\n", "\n", "[mpmath](https://mpmath.org/) はPython 用の任意精度浮動小数点演算のライブラリ。\n", "以下のように pip コマンドで簡単にインストールできる。\n", "\n", "```bash\n", "pip install mpmath\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 使用例" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### オブジェクト生成" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`mpmath.mpf` で浮動小数点数のオブジェクトを作る。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import mpmath\n", "\n", "# 整数から作ってみる。\n", "mpmath.mpf(2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 文字列から作ってみる。\n", "mpmath.mpf(\"0.5\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 演算子による演算\n", "\n", "基本的な演算子についてはそのまま使用できる。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 2 の 0.5 乗で平方根を求めてみる。\n", "mpmath.mpf(2) ** mpmath.mpf(0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 精度の指定\n", "\n", "`mpmath.mp.dps`, `mpmath.mp.prec` などで精度を指定する。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 10 進数としての精度を設定\n", "mpmath.mp.dps = 50\n", "\n", "# 計算の精度が変化する。\n", "mpmath.mpf(2) ** mpmath.mpf(0.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 2 進数としての精度を設定\n", "mpmath.mp.prec = 100\n", "\n", "# 計算の精度が変化する。\n", "mpmath.mpf(2) ** mpmath.mpf(0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 各種定数\n", "\n", "数学の定数が色々と用意されている。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ここでの結果は 50 桁表示しておく。\n", "mpmath.mp.dps = 50\n", "# 円周率\n", "mpmath.pi()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 自然対数の底\n", "mpmath.e()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 黄金比\n", "mpmath.phi()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "他は [公式ドキュメント](https://mpmath.org/doc/1.3.0/index.html) を参照。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 数学の関数\n", "\n", "exp, sin など様々な数学の関数が用意されている。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mpmath.exp(mpmath.mpf(1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mpmath.sin(mpmath.mpf(\"0.25\") * mpmath.pi)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mpmath.sqrt(mpmath.mpf(3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[公式ドキュメント](https://mpmath.org/doc/1.3.0/index.html) を見ると、他にも様々な関数が実装されている。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "楕円関数 `su` のような珍しい関数もあり、サポートが幅広い。\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 数値積分\n", "\n", "数値積分の機能もある。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "例えば、\n", "\n", "$$\n", "4 \\int_0^1 \\frac{1}{1 + x^2} dx = \\pi\n", "$$\n", "\n", "を以下のように計算できる。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "4 * mpmath.quad(lambda x: 1 / (1 + x ** 2), [0, 1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "無限の区間でも計算できる。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mpmath.quad(lambda x: 1 / (1 + x ** 2), [-mpmath.inf, mpmath.inf])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 課題\n", "\n", "- 2023/12/10 時点で、まだ 2 進数、16 進数での入出力に対応していない。" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }