{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Free Wolfram EngineをJupyterで使う方法\n", "\n", "* 黒木 玄\n", "* 2019-05-25~2019-06-05\n", "\n", "Wolfram Engine とそのJupyterクライアントのインストールの仕方については\n", "\n", "* https://twitter.com/genkuroki/status/1132064876174536704\n", "\n", "で解説した. このノートブックにおける解説はそこでした解説とほぼ同じである.\n", "\n", "Jupyterを最も簡単に使えるようにする方法はおそらく[Anaconda](https://www.anaconda.com/distribution/)を入れることである. [Julia言語](https://julialang.org/)からJupyterを使える環境を整備する方法については\n", "\n", "* https://nbviewer.jupyter.org/github/genkuroki/msfd28/blob/master/install.ipynb\n", "\n", "を参照せよ. Juliaは最新の優れたプログラミング言語なので使えるようにしておいた方が便利である.\n", "\n", "Wolfram言語を使用するための最も簡単な手段は\n", "\n", "* http://www.wolframcloud.com/\n", "\n", "を利用することである." ] }, { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "
\",\n", "\t\t\t(* the textual form of the result *)\n", "\t\t\t(* NOTE: the OutputForm (which ToString uses) of any expressions wrapped with, say, InputForm should\n", "\t\t\t\tbe identical to the string result of an InputForm-wrapped expression itself *)\n", "\t\t\tStringJoin[{\"\", ToString[#1], \";\"} & /@ ToCharacterCode[ToString[result], \"Unicode\"]],\n", "\t\t\t(* end the element *)\n", "\t\t\t\"\"\n", "\t\t];\n", "```\n", "\n", "を次のように書き直す:\n", "\n", "```mathematica\n", "\t(* generate HTML for the textual form of a result *)\n", "\ttoOutText[result_] := \n", "\t\tSwitch[JupyterOutTextForm, \n", "\t\t\"Plain\",\n", "\t\tStringJoin[{\"\", ToString[#1], \";\"} & /@ ToCharacterCode[ToString[result], \"Unicode\"]],\n", "\t\t\"TeX\",\n", "\t\tStringJoin[\n", "\t\t\t\"$$\",\n", "\t\t\tStringJoin[{\"\", ToString[#1], \";\"} & /@ ToCharacterCode[ToString[result], \"Unicode\"]],\n", "\t\t\t\"$$\"\n", "\t\t],\n", "\t\t_,\n", "\t\tStringJoin[\n", "\t\t\t(* preformatted *)\n", "\t\t\t\"
\",\n", "\t\t\t(* the textual form of the result *)\n", "\t\t\t(* NOTE: the OutputForm (which ToString uses) of any expressions wrapped with, say, InputForm should\n", "\t\t\t\tbe identical to the string result of an InputForm-wrapped expression itself *)\n", "\t\t\tStringJoin[{\"\", ToString[#1], \";\"} & /@ ToCharacterCode[ToString[result], \"Unicode\"]],\n", "\t\t\t(* end the element *)\n", "\t\t\t\"\"\n", "\t\t]\n", "\t\t];\n", "```\n", "\n", "これによって, 変数 `JupyterOutTextForm` が \"Plain\" と \"TeX\" の場合にのみテキストの表示のスタイルが変化する:\n", "\n", "* Plain → そのまま出力\n", "* TeX → \\\\$\\\\$~\\\\$\\\\$ で囲んで出力\n", "* デフォルト → <pre>~</pre\\> で囲んで出力(上の変更前の動作)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 数式などの画像表示の解像度の変更\n", "\n", "このノートブックはさらに以下のような変更を\n", "\n", " WolframLanguageForJupyter/WolframLanguageForJupyter/Resources/OutputHandlingUtilities.wl\n", "\n", "の[ToOutImage函数](https://github.com/WolframResearch/WolframLanguageForJupyter/blob/master/WolframLanguageForJupyter/Resources/OutputHandlingUtilities.wl#L122-L136)\n", "\n", "```mathematica\n", "\t(* generate HTML for the rasterized form of a result *)\n", "\ttoOutImage[result_] := \n", "\t\tStringJoin[\n", "\t\t\t(* display a inlined PNG image encoded in base64 *)\n", "\t\t\t\"\"\n", "\t\t];\n", "```\n", "\n", "を以下のように書き換えた環境で実行している:\n", "\n", "```mathematica\n", "\t(* generate HTML for the rasterized form of a result *)\n", "\ttoOutImage[result_] := \n", "\t\tIf[Head[JupyterImageResolution] === Integer,\n", "\t\tStringJoin[\n", "\t\t\t\"Infinity],\n", "\t\t\t\t\tIf[Head[result] === InformationDataGrid,\n", "\t\t\t\t\t\tExportByteArray[Rasterize[result, ImageFormattingWidth->800, ImageResolution->2JupyterImageResolution], \"PNG\"], \n", "\t\t\t\t\t\tExportByteArray[Rasterize[result, ImageFormattingWidth->1000, ImageResolution->2JupyterImageResolution], \"PNG\"]\n", "\t\t\t\t\t]\n", "\t\t\t\t]]\n", "\t\t\t],\n", "\t\t\tIf[Head[result] === Manipulate || Head[result] === InformationDataGrid, \n", "\t\t\t\t\"\\\">\", \n", "\t\t\t\tStringJoin[\"\\\" width=\\\"\", \n", "\t\t\t\tToString[Quotient[Rasterize[result, \"RasterSize\", ImageFormattingWidth->1000, ImageResolution->2JupyterImageResolution][[1]],2]], \n", "\t\t\t\t\"\\\">\"]\n", "\t\t\t]\n", "\t\t],\n", "\t\tStringJoin[\n", "\t\t\t(* display a inlined PNG image encoded in base64 *)\n", "\t\t\t\"\"\n", "\t\t]\n", "\t\t];\n", "```\n", "\n", "このように書き換えておくと, 変数 `JupyterImageResolution` に72(デフォルト)などの整数を代入しておくと, その2倍の解像度で画像を作って半分の大きさで表示してくれるようになる. このようにしておくと, 特に数式を画像で表示させたときの見易さが非常に上がる. \n", "\n", "* [[Feature] Retina resolution graphics #35](https://github.com/WolframResearch/WolframLanguageForJupyter/issues/35)\n", "\n", "も参照せよ.\n", "\n", "**ToOutImage改造変更履歴**\n", "\n", "* 2019-06-01: 作成.\n", "* 2019-06-02: Manipulateの処理のバグを訂正し, Manipulateを無限ループGIF動画で表示するようにした.\n", "* 2019-06-04: InformationDataGridが非常に小さく表示される問題を改善した." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "JupyterImageResolution = 84;\n", "JupyterOutTextForm = \"TeX\";\n", "\n", "TeX[x_] := ToString[TeXForm[x]]\n", "TeX[x_, y__] := StringJoin[TeX[x], TeX[y]]\n", "TeXRaw[x__, y_] := StringJoin[x, TeX[y]]\n", "\n", "MappedBy[x_] := x\n", "MappedBy[x_, F___, G_] := MappedBy[x, F] // G\n", "\n", "SetAttributes[TeXEq, HoldFirst]\n", "TeXEq[x_] := TeX[HoldForm[x] == MappedBy[x, ReleaseHold, FullSimplify]]\n", "TeXEq[x_, F__] := TeX[HoldForm[x] == MappedBy[x, ReleaseHold, F]]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 15, "metadata": { "text/html": [] }, "output_type": "execute_result" } ], "source": [ "Clear[f,x,a,b, JupyterImageResolution]\n", "Integrate[f[x], {x,a,b}]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 17, "metadata": { "text/html": [] }, "output_type": "execute_result" } ], "source": [ "JupyterImageResolution = 72;\n", "Integrate[f[x], {x,a,b}]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 19, "metadata": { "text/html": [] }, "output_type": "execute_result" } ], "source": [ "JupyterImageResolution = 84;\n", "Integrate[f[x], {x,a,b}]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\int_a^b f(x) \, dx