{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Unique things\n", "\n", "Many of the features and commands in Symata will be familiar from other programs. This notebook presents a few things that may be unfamiliar and not documented elsewhere. Also note that many of the \"unique\" features appear in other tutorial notebooks." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "using Symata # enter Symata mode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Version information\n", "Version information of main components." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Symata version 0.4.2-dev.4\n", "Julia version 0.7.0-beta2.1\n", "Python version 2.7.14+\n", "SymPy version 1.0\n" ] } ], "source": [ "VersionInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### OutputStyle\n", "\n", "`OutputStyle` selects the appearance of printed (or rendered) Symata expressions. See the notebook \"InteractingWithSymata\" for more details." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/markdown": [ "```\n", "OutputStyle(InputForm)\n", "```\n", "\n", "print plain 1d text output.\n", "\n", "```\n", "OutputStyle(UnicodeForm)\n", "```\n", "\n", "print 1d text output with pretty unicode characters.\n", "\n", "```\n", "OutputStyle(JupyterForm)\n", "```\n", "\n", "in a Jupyter notebook, print in typeset mathematics style using latex.\n", "\n", "```\n", "OutputStyle()\n", "```\n", "\n", "Return the current output style.\n", "\n", "`InputForm` and `UnicodeForm` give output that is valid `Symata` input for the same expression.\n" ], "text/plain": [ "\u001b[36m OutputStyle(InputForm)\u001b[39m\n", "\n", " print plain 1d text output.\n", "\n", "\u001b[36m OutputStyle(UnicodeForm)\u001b[39m\n", "\n", " print 1d text output with pretty unicode characters.\n", "\n", "\u001b[36m OutputStyle(JupyterForm)\u001b[39m\n", "\n", " in a Jupyter notebook, print in typeset mathematics style using latex.\n", "\n", "\u001b[36m OutputStyle()\u001b[39m\n", "\n", " Return the current output style.\n", "\n", " \u001b[36mInputForm\u001b[39m and \u001b[36mUnicodeForm\u001b[39m give output that is valid \u001b[36mSymata\u001b[39m input for the\n", " same expression." ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "See also CompactOutput.\n", "\n", " Attributes(OutputStyle) = [Protected]\n" ] } ], "source": [ "? OutputStyle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Timing evaluation\n", "Use `Time(True)` and `Time(False)` to toggle timing and allocation information after evaluation of each input line. Use `Timing(expr)` to time the evaluation of `expr` only.\n", "\n", "Suppress the output when using `Timing` like this" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ 0.008664348,\\text{Null} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ 0.008664348,\\text{Null} \\right] $$\"" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Timing(Range(10^5), Null) # jit compile\n", "\n", "Timing(Range(10^5), Null)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Help\n", "\n", "`?` prints help on the `Help` function.\n", "\n", "`Help(topic)`, `Help(\"topic\")`, or `? topic` prints documentation for `topic`.\n", "\n", "`h\"words\"` does a case insensitive search of the contents of help documents.\n", "\n", "Syamta uses the python package *SymPy* extensivley. `Help` prints relevant SymPy documentation along with Symata documentation. Type `ShowSymPyDocs(False)` to disable printing SymPy documentation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Big integers\n", "\n", "By default, Symata converts input integers to the Julia type `Int`. But, you may want bigger numbers:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 0 $$" ], "text/plain": [ "L\"$$ 0 $$\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2^100 # overflow" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/markdown": [ "```\n", "BigIntInput(True)\n", "```\n", "\n", "enable interpreting all integers as arbitrary precision `BigInt`s.\n", "\n", "```\n", "BigIntInput(False)\n", "```\n", "\n", "(default) disable interpreting all integers as arbitrary precision `BigInts`.\n", "\n", "```\n", "BigIntInput()\n", "```\n", "\n", "Return the current state.\n", "\n", "You can always specify that an integer should be a `BigInt` by giving `BI(n)`.\n" ], "text/plain": [ "\u001b[36m BigIntInput(True)\u001b[39m\n", "\n", " enable interpreting all integers as arbitrary precision \u001b[36mBigInt\u001b[39ms.\n", "\n", "\u001b[36m BigIntInput(False)\u001b[39m\n", "\n", " (default) disable interpreting all integers as arbitrary precision \u001b[36mBigInts\u001b[39m.\n", "\n", "\u001b[36m BigIntInput()\u001b[39m\n", "\n", " Return the current state.\n", "\n", " You can always specify that an integer should be a \u001b[36mBigInt\u001b[39m by giving \u001b[36mBI(n)\u001b[39m." ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "See also BF, BI, Big, BigFloatInput, GetPrecison, N, and SetPrecision.\n", "\n", " Attributes(BigIntInput) = [Protected]\n" ] } ], "source": [ "? BigIntInput" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "BigIntInput(True);" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 1267650600228229401496703205376 $$" ], "text/plain": [ "L\"$$ 1267650600228229401496703205376 $$\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2^100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that this only applies to input integers (at the moment)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\text{Int64} $$" ], "text/plain": [ "L\"$$ \\text{Int64} $$\"" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Head(Cos(Pi))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\text{BigInt} $$" ], "text/plain": [ "L\"$$ \\text{BigInt} $$\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Head(BI(Cos(Pi))) # Convert explicitly to BigInt " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another way to parse a single number as a `BigInt`" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 1267650600228229401496703205376 $$" ], "text/plain": [ "L\"$$ 1267650600228229401496703205376 $$\"" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "BigIntInput(False)\n", "big\"2\"^100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Passing data between Julia and Symata" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Symata's host language is Julia. There are several ways to interact with Julia in a Symata session. \n", "\n", "Julia and Symata keep separate lists of symbols. For instance, `x` may be defined in Julia, but not Symata, and vice versa.\n", "\n", "Use `SetJ` to bind (i.e. set) a Julia symbol to a Symata expression." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/markdown": [ "```\n", "SetJ(x,val)\n", "```\n", "\n", "sets the Julia symbol `x` to `val`.\n", "\n", "Variables and functions in Symata are separate from those in Julia, ie, their table of bindings to symbols are separate.\n" ], "text/plain": [ "\u001b[36m SetJ(x,val)\u001b[39m\n", "\n", " sets the Julia symbol \u001b[36mx\u001b[39m to \u001b[36mval\u001b[39m.\n", "\n", " Variables and functions in Symata are separate from those in Julia, ie,\n", " their table of bindings to symbols are separate." ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", " Attributes(SetJ) = [HoldFirst,Protected]\n" ] } ], "source": [ "? SetJ" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ x + y $$" ], "text/plain": [ "L\"$$ x + y $$\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "expr = x + y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bind the Julia variable `z` to the result of evaluating `expr`." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ x + y $$" ], "text/plain": [ "L\"$$ x + y $$\"" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "SetJ(z , expr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Execute Julia code, by enclosing it in `J( )`. We ask for the value of `z`." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ x + y $$" ], "text/plain": [ "L\"$$ x + y $$\"" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "J(Main.z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also leave Symata and return to Julia" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [], "source": [ " Julia() # begin interpreting expressions as Julia code. ;" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":x + :y" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z # Now in Julia" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The unexported Julia function symval returns the Symata binding of a symbol." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [], "source": [ "? Symata.symval" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":x + :y" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Symata.symval(:expr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do the reverse, set a Symata variable to the value of a Julia variable.\n", "\n", "Set a variable in Jula." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "1" ], "text/plain": [ "1" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1 # Set a Julia symbol (bind an identifier to a value)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [], "source": [ "isymata() # Enter Symata mode again" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ 1 $$" ], "text/plain": [ "L\"$$ 1 $$\"" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "expr2 = J( Main.a )\n", "expr2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Symata symbol `expr2` is now set to `1`." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## SymPy\n", "\n", "Symata uses the python package *SymPy* extensively. Most of the interaction with SymPy is transparent to the user. But there are several commands for controlling this interaction." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use Symata's help search feature to find commands related to SymPy." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ExpandA\n", "Part\n", "Args\n", "SymPyError\n", "Rewrite\n", "MmaSyntax\n", "ReturnSymPy\n", "ShowSymPyDocs\n", "ToSymPy\n", "PyHead\n", "ToSymata\n", "CoefficientSympy\n" ] } ], "source": [ "h\"sympy\"" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/markdown": [ "```\n", "SymPyError()\n", "```\n", "\n", "Return the most recent SymPy error message. If you see a message warning that a SymPy error has occurred, you can find the detailed error message.\n" ], "text/plain": [ "\u001b[36m SymPyError()\u001b[39m\n", "\n", " Return the most recent SymPy error message. If you see a message warning\n", " that a SymPy error has occurred, you can find the detailed error message." ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", " Attributes(SymPyError) = [Protected]\n" ] } ], "source": [ "? SymPyError" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For debugging, you can disable converting SymPy objects to Symata." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/markdown": [ "```\n", "ReturnSymPy(True)\n", "```\n", "\n", "disable conversion of expressions computed by SymPy to Symata.\n", "\n", "```\n", "ReturnSympy(False)\n", "```\n", "\n", "(default) enable conversion to Symata.\n", "\n", "```\n", "ReturnSympy()\n", "```\n", "\n", "Return the current state.\n" ], "text/plain": [ "\u001b[36m ReturnSymPy(True)\u001b[39m\n", "\n", " disable conversion of expressions computed by SymPy to Symata.\n", "\n", "\u001b[36m ReturnSympy(False)\u001b[39m\n", "\n", " (default) enable conversion to Symata.\n", "\n", "\u001b[36m ReturnSympy()\u001b[39m\n", "\n", " Return the current state." ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "See also ShowSymPyDocs, ToSymPy, and ToSymata.\n", "\n", " Attributes(ReturnSymPy) = [Protected]\n" ] } ], "source": [ "? ReturnSymPy" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/markdown": [ "```\n", "ToSymata(expr)\n", "```\n", "\n", "convert the python PyObject `expr` to a Symata expression. Normally, expressions computed by SymPy are automatically converted to Symata expressions.\n" ], "text/plain": [ "\u001b[36m ToSymata(expr)\u001b[39m\n", "\n", " convert the python PyObject \u001b[36mexpr\u001b[39m to a Symata expression. Normally,\n", " expressions computed by SymPy are automatically converted to Symata\n", " expressions." ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "See also ReturnSymPy, ShowSymPyDocs, and ToSymPy.\n", "\n", " Attributes(ToSymata) = [HoldAll,Protected]\n" ] } ], "source": [ "? ToSymata" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/markdown": [ "```\n", "ToSymPy(expr)\n", "```\n", "\n", "convert `expr` to a (python) PyObject.\n" ], "text/plain": [ "\u001b[36m ToSymPy(expr)\u001b[39m\n", "\n", " convert \u001b[36mexpr\u001b[39m to a (python) PyObject." ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "See also ReturnSymPy, ShowSymPyDocs, and ToSymata.\n", "\n", " Attributes(ToSymPy) = [Protected]\n" ] } ], "source": [ "? ToSymPy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use `ToSymPy` and `ToSymata` to convert between the languages." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "PyObject Integral(f(x), x)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OutputStyle(InputForm)\n", "pyobj = ToSymPy(Integrate(f(x),x))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{PyCall.PyObject},\\text{Integral} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{PyCall.PyObject},\\text{Integral} \\right] $$\"" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OutputStyle(JupyterForm)\n", "[Head(pyobj), PyHead(pyobj)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recover the Symata expression." ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\int f \\! \\left( x \\right) \\, \\mathbb{d}x $$" ], "text/plain": [ "L\"$$ \\int f \\! \\left( x \\right) \\, \\mathbb{d}x $$\"" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ToSymata(pyobj)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Version and date" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Symata version 0.4.2-dev.4\n", "Julia version 0.7.0-beta2.1\n", "Python version 2.7.14+\n", "SymPy version 1.0\n" ] } ], "source": [ "VersionInfo()" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2018-07-21T19:36:54.357" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "InputForm(Now())" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 0.7.0-beta2", "language": "julia", "name": "julia-0.7" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.7.0" } }, "nbformat": 4, "nbformat_minor": 1 }