{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Interacting with Symata\n", "\n", "## Output\n", "\n", "### Three diplay styles\n", "\n", "Symata runs in either the ipython (Jupyter) notebook or the Julia REPL (or even in a dumb terminal). In the Julia REPL two output styles are available, `InputForm` and `UnicodeForm`. In the notebook, a third style, called `JupyterForm`, is available. The notebook starts with the `JupyterForm` style." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "using Symata # load the package and enter Symata mode" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\int_{0}^{\\infty}f \\! \\left( \\mathbb{i} + x \\right) \\, \\mathbb{d}x $$" ], "text/plain": [ "L\"$$ \\int_{0}^{\\infty}f \\! \\left( \\mathbb{i} + x \\right) \\, \\mathbb{d}x $$\"" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Integrate(f(x + I), [x, 0, Infinity])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is the same thing in `InputForm` style" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Integrate(f(I + x),[x,0,Infinity])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OutputStyle(InputForm)\n", "Out(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In `UnicodeForm` style, some of the symbols are pretty printed." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Integrate(f(𝕚 + x),[x,0,∞])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OutputStyle(UnicodeForm)\n", "Out(2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Γ(a)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Assume(a, Positive)\n", "gammaint = Integrate(E^(-x) * x^(a-1), [x,0,Infinity])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In `InputForm` style" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Gamma(a)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OutputStyle(InputForm)\n", "gammaint" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And back to `JupyterForm` style" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\Gamma \\! \\left( a \\right) $$" ], "text/plain": [ "L\"$$ \\Gamma \\! \\left( a \\right) $$\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OutputStyle(JupyterForm)\n", "gammaint" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### InputForm output style\n", "\n", "`InputForm` style output is closest to the internal form of the expression. It can be copied and pasted as input. (This is also true of `UnicodeForm` style)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "x^3 + 3*x^2*y + 3x*y^2 + y^3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OutputStyle(InputForm), Expand((x + y)^3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Plus(Power(x,3),Times(3,Power(x,2),y),Times(3,x,Power(y,2)),Power(y,3))" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FullForm(Out(8)) # Internal form of the previous output" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "x^3 + 3*x^2*y + 3x*y^2 + y^3" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Plus(Power(x,3),Times(3,Power(x,2),y),Times(3,x,Power(y,2)),Power(y,3)) # This is also valid input" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare this to `JupyterForm` output style which cannot in general be copied as input." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\int_{0}^{\\infty}g \\! \\left( x \\right) \\, \\mathbb{d}x $$" ], "text/plain": [ "L\"$$ \\int_{0}^{\\infty}g \\! \\left( x \\right) \\, \\mathbb{d}x $$\"" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OutputStyle(JupyterForm);\n", "\n", "Integrate(g(x), [x,0,Infinity])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is what we get by cutting and pasting from the typset integral in Out[12]: ∫∞0g(x)𝕕x. Not valid input. But, you can still refer to the output cell:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 1 $$" ], "text/plain": [ "L\"$$ 1 $$\"" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g(x_) := Exp(-x)\n", "\n", "Out(11)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In `InputForm` style, you can make the output more compact or less compact like this:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "x^3 + 3 * x^2 * y + 3x * y^2 + y^3" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OutputStyle(InputForm), CompactOutput(False), Out(10)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "x^3 + 3*x^2*y + 3x*y^2 + y^3" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "CompactOutput(True), Out(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### JupyterForm output style\n", "\n", "Here are some examples of formatting with `JupyterForm` output style" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\frac{\\frac{1}{2} + a^{b}}{x + y} $$" ], "text/plain": [ "L\"$$ \\frac{\\frac{1}{2} + a^{b}}{x + y} $$\"" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OutputStyle(JupyterForm), (1/2 + a^b)/(x+y) " ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\sum _{\\substack{i=0 \\\\ j=1}}^{\\infty,n}h \\! \\left( i,j \\right) + \\sum _{\\substack{i=0 \\\\ j=0}}^{\\infty}g \\! \\left( i,j \\right) $$" ], "text/plain": [ "L\"$$ \\sum _{\\substack{i=0 \\\\ j=1}}^{\\infty,n}h \\! \\left( i,j \\right) + \\sum _{\\substack{i=0 \\\\ j=0}}^{\\infty}g \\! \\left( i,j \\right) $$\"" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sum(g(i,j), [i,0,Infinity], [j,0,Infinity]) + Sum(h(i,j), [i,0,Infinity], [j,1,n])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ \\int_{0}^{1} \\mathbb{d}x\\int_{0}^{1} \\mathbb{d}y \\, g \\! \\left( x,y \\right) $$" ], "text/plain": [ "L\"$$ \\int_{0}^{1} \\mathbb{d}x\\int_{0}^{1} \\mathbb{d}y \\, g \\! \\left( x,y \\right) $$\"" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Integrate(g(x,y), [x,0,1], [y,0,1])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ a\\text{<}b \\, \\text{&&} \\, b\\text{<} \\left( \\frac{c}{d} \\right) $$" ], "text/plain": [ "L\"$$ a\\text{<}b \\, \\text{&&} \\, b\\text{<} \\left( \\frac{c}{d} \\right) $$\"" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a < b < c/d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To cut and past from Jupyter to another application, right click on the displayed output. Then choose from the menu which format you like, such as the $\\LaTeX$ source. (You can also choose math display options here)\n", "\n", "To get an expression that can be cut and pasted as `Symata` input, say to text editor, you can either switch formats using `OutputStyle(InputForm)`, or wrap a single expression in `InputForm`." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "a < b && b < (c*d^(-1))" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "InputForm( a< b < c/d )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Formatting Numbers\n", "\n", "You can change the way numbers are printed like this." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 3.141592653589793 $$" ], "text/plain": [ "L\"$$ 3.141592653589793 $$\"" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(Pi)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 3.141592653589793e10 $$" ], "text/plain": [ "L\"$$ 3.141592653589793e10 $$\"" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(Pi) * 10^10" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [], "source": [ "FloatFormat(Short);" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 3.14159 $$" ], "text/plain": [ "L\"$$ 3.14159 $$\"" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(Pi)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ 3.14159e+10 $$" ], "text/plain": [ "L\"$$ 3.14159e+10 $$\"" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(Pi) * 10^10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Print three digits" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [], "source": [ "FloatFormat(3);" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/latex": [ "$$ 3.14 $$" ], "text/plain": [ "L\"$$ 3.14 $$\"" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(Pi)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 3.14e+10 $$" ], "text/plain": [ "L\"$$ 3.14e+10 $$\"" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(Pi) * 10^10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Changing the number of digits printed does not change the precision of the number." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 16 $$" ], "text/plain": [ "L\"$$ 16 $$\"" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Precision(N(Pi))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The format of BigFloat numbers is not affected by `FloatFormat`" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ 3.14159265358979323846264338327950288419716939937512 $$" ], "text/plain": [ "L\"$$ 3.14159265358979323846264338327950288419716939937512 $$\"" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(Pi,50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use `BigFloatFormat` for this" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [], "source": [ "BigFloatFormat(Short);" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 3.14159 $$" ], "text/plain": [ "L\"$$ 3.14159 $$\"" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(Pi,50)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 50 $$" ], "text/plain": [ "L\"$$ 50 $$\"" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Precision(N(Pi,50))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [], "source": [ "FloatFormat(Full);" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ 3.141592653589793 $$" ], "text/plain": [ "L\"$$ 3.141592653589793 $$\"" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(Pi)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [], "source": [ "BigFloatFormat(Full);" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ 3.14159265358979323846264338327950288419716939937512 $$" ], "text/plain": [ "L\"$$ 3.14159265358979323846264338327950288419716939937512 $$\"" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N(Pi,50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Input\n", "\n", "### Unicode input\n", "\n", "\n", "Unicode input is inherited from Julia and the ipython/Jupyter notebook. In both the REPL and the notebook, you can enter a $\\LaTeX$ macro and hit `TAB` to convert it to a symbol. For instance, `\\alpha[TAB]` gives α. You can also copy/paste unicode symbols into the notebook.\n", "\n", "A few symbols, such as π, are intepreted as builtin Symata symbols.\n" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": true }, "outputs": [], "source": [ "OutputStyle(InputForm);" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[I + a + E + Pi,I + a + E + Pi]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[π + 𝕖 + 𝕚 + a, Pi + E + I + a] # \\pi + \\Bbbe + \\Bbbi" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [], "source": [ "OutputStyle(UnicodeForm);" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[𝕚 + a + 𝕖 + π,𝕚 + a + 𝕖 + π]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[π + 𝕖 + 𝕚 + a, Pi + E + I + a]" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [], "source": [ "OutputStyle(JupyterForm);" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\mathbb{i} + a + \\mathbb{e} + \\pi ,\\mathbb{i} + a + \\mathbb{e} + \\pi \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\mathbb{i} + a + \\mathbb{e} + \\pi ,\\mathbb{i} + a + \\mathbb{e} + \\pi \\right] $$\"" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[π + 𝕖 + 𝕚 + a, Pi + E + I + a]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Pi` and π, etc. refer to the same symbol." ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{True},\\text{True},\\text{True},\\text{True},\\text{True} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{True},\\text{True},\\text{True},\\text{True},\\text{True} \\right] $$\"" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[Pi == π, E == 𝕖, I == 𝕚, EulerGamma == γ, Gamma == Γ]" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ -1,-1 \\right] $$" ], "text/plain": [ "L\"$$ \\left[ -1,-1 \\right] $$\"" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Cos([Pi, π])" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### TAB completion\n", "\n", "In `Symata`, in both the command line REPL and `IJulia`, you can use TAB completion to complete built-in and user-defined symbols, in the same way that you do in `Julia`.\n", "\n", "In `IJulia`, typing `Shift-TAB` when the cursor is at the end of a built-in symbol displays a pop-up with the doc string, if available. (This feature may be broken in both Julia and Symata)\n", "\n", "You can get the same doc string (and the corresponding `sympy` document) by typing `?` followed by the symbol." ] }, { "cell_type": "code", "execution_count": 45, "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": [ "\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": [ "### Version and date" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Symata version 0.4.1-dev.3\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": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2018-07-20T13:19:13.094" ] }, "execution_count": 47, "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 }