{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Unique things\n", "\n", "The title of this notebook does not mean \"fantastic, amazing things\". The title does describe more or less literally the content. 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": [ { "data": { "application/javascript": [ "$(\"div.input_prompt\").css({\"color\": \"blue\"})" ] }, "metadata": {}, "output_type": "display_data" } ], "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.3.0-dev.7\n", "julia version 0.6.0-dev.435\n", "python version 2.7.12\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 }, "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": [ "```\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" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Help( OutputStyle)\n", "\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." ] }, { "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": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "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": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 0 $$" ], "text/plain": [ "L\"$$ 0 $$\"" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2^100 # overflow" ] }, { "cell_type": "code", "execution_count": 5, "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": [ "```\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" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Help( BigIntInput)\n", "\n", "See also BF, BI, Big, BigFloatInput, N, and SetPrecision.\n", "\n", " Attributes(BigIntInput) = [Protected]\n" ] } ], "source": [ "? BigIntInput" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "BigIntInput(True);" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 1267650600228229401496703205376 $$" ], "text/plain": [ "L\"$$ 1267650600228229401496703205376 $$\"" ] }, "execution_count": 7, "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": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\text{Int64} $$" ], "text/plain": [ "L\"$$ \\text{Int64} $$\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Head(Cos(Pi))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\text{BigInt} $$" ], "text/plain": [ "L\"$$ \\text{BigInt} $$\"" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Head( BI(Cos(Pi))) # Convert explicitly to BigInt " ] }, { "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": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ x + y $$" ], "text/plain": [ "L\"$$ x + y $$\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "expr = x + y" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "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": [ "```\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" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Help( SetJ)\n", "\n", "\n", " Attributes(SetJ) = [HoldFirst,Protected]\n" ] } ], "source": [ "? SetJ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bind the Julia variable `z` to the result of evaluating `expr`." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ x + y $$" ], "text/plain": [ "L\"$$ x + y $$\"" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "SetJ(z , expr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Execute Julia code, by enclosing it in `:( )`. We ask for the value of `z`." ] }, { "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": [ "J( Main.z )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also leave Symata and return to Julia" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "$(\"div.input_prompt\").css({\"color\": \"green\"})" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ " Julia() # begin interpreting expressions as Julia code. ;" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":x + :y" ] }, "execution_count": 15, "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": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/markdown": [ "```\n", "symval(s::SJSym)\n", "```\n", "\n", "return the bound value of the `Symata` symbol with Julia-Symbol name `s`.\n", "\n", "```\n", "symval(s::SSJSym)\n", "```\n", "\n", "Return the value bound to the Symata symbol `s`.\n" ], "text/plain": [ "```\n", "symval(s::SJSym)\n", "```\n", "\n", "return the bound value of the `Symata` symbol with Julia-Symbol name `s`.\n", "\n", "```\n", "symval(s::SSJSym)\n", "```\n", "\n", "Return the value bound to the Symata symbol `s`.\n" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "? Symata.symval" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":x + :y" ] }, "execution_count": 17, "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": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1 # Set a Julia symbol (bind an identifier to a value)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "$(\"div.input_prompt\").css({\"color\": \"blue\"})" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "isymata() # Enter Symata mode again" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ 1 $$" ], "text/plain": [ "L\"$$ 1 $$\"" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(expr2 = J( Main.a ); 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": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Rewrite\n", "SymPyError\n", "ExpandA\n", "ReturnSymPy\n", "ToSymata\n", "ShowSymPyDocs\n", "ToSymPy\n" ] } ], "source": [ "h\"sympy\"" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/markdown": [ "```\n", "SymPyError()\n", "```\n", "\n", "returns 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": [ "```\n", "SymPyError()\n", "```\n", "\n", "returns 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" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Help( SymPyError)\n", "\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": 23, "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": [ "```\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" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Help( ReturnSymPy)\n", "\n", "See also ShowSymPyDocs, ToSymPy, and ToSymata.\n", "\n", " Attributes(ReturnSymPy) = [Protected]\n" ] } ], "source": [ "? ReturnSymPy" ] }, { "cell_type": "code", "execution_count": 24, "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": [ "```\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" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Help( ToSymata)\n", "\n", "See also ReturnSymPy, ShowSymPyDocs, and ToSymPy.\n", "\n", " Attributes(ToSymata) = [HoldAll,Protected]\n" ] } ], "source": [ "? ToSymata" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/markdown": [ "```\n", "ToSymPy(expr)\n", "```\n", "\n", "convert `expr` to a (python) PyObject.\n" ], "text/plain": [ "```\n", "ToSymPy(expr)\n", "```\n", "\n", "convert `expr` to a (python) PyObject.\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Help( ToSymPy)\n", "\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": 26, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "PyObject Integral(f(x), x)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(OutputStyle(InputForm), pyobj = ToSymPy(Integrate(f(x),x)))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\text{PyCall.PyObject} $$" ], "text/plain": [ "L\"$$ \\text{PyCall.PyObject} $$\"" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "( OutputStyle(JupyterForm), Head(pyobj))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recover the Symata expression." ] }, { "cell_type": "code", "execution_count": 28, "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": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ToSymata(pyobj)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Version and date" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "symata version 0.3.0-dev.7\n", "julia version 0.6.0-dev.435\n", "python version 2.7.12\n", "sympy version 1.0\n" ] } ], "source": [ "VersionInfo()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 2016-11-28T22:51:13.381 $$" ], "text/plain": [ "L\"$$ 2016-11-28T22:51:13.381 $$\"" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Now()" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 0.6.0-dev", "language": "julia", "name": "julia-0.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.6.0" } }, "nbformat": 4, "nbformat_minor": 1 }