{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "using Symata" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Differences from Wolfram / Mathematica\n", "\n", "Symata is similar to the Wolfram language. But, there are difference in notation and behavior." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiplication\n", "In Symata, multiplication of a number and variable does not require the `*` symbol. But space between the number and variable is not allowed. All other multiplications requires an explicit `*`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ 1 + 2 \\ x,1 + 2 \\ x,6,\\text{Cos} \\! \\left( x \\right) \\ \\text{Sin} \\! \\left( x \\right) \\right] $$" ], "text/plain": [ "L\"$$ \\left[ 1 + 2 \\ x,1 + 2 \\ x,6,\\text{Cos} \\! \\left( x \\right) \\ \\text{Sin} \\! \\left( x \\right) \\right] $$\"" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[2x + 1, 2 * x + 1, 2 * 3, Cos(x) * Sin(x)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comments\n", "\n", "In Symata, comments are entered with the prefix `#`. In Mathematica, comments are entered like this `(* comment *)`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# a = 1\n", " b = 2\n", "\n", "[a,b]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List\n", "Mathematica lists are delimited by `{ }`. Symata lists are delimited by `[ ]`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ a,b,c \\right] $$" ], "text/plain": [ "L\"$$ \\left[ a,b,c \\right] $$\"" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[a,b,c]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Elements in a list may be separated by commas, as above. But, in Symata they may also be separated by a newline after a complete expression. (This does not work if the code is loaded from a file with `Get`)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ a,c + d,\\text{\"cat\"},x^{2} + 2 \\ x \\ y + y^{2} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ a,c + d,\\text{\\\"cat\\\"},x^{2} + 2 \\ x \\ y + y^{2} \\right] $$\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[\n", " a\n", " c + d\n", " \"cat\"\n", " Expand((x+y)^2)\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions\n", "\n", "Function arguments are delimited by `[ ]` in Mathematica. In Symata, arguments are delimited by `( )`" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ f \\! \\left( x \\right) $$" ], "text/plain": [ "L\"$$ f \\! \\left( x \\right) $$\"" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compound expressions\n", "\n", "There are several ways to enter compound expressions." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\text{True} $$" ], "text/plain": [ "L\"$$ \\text{True} $$\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e1 = (1,2,a+b)\n", "\n", "e2 = (1,\n", " 2,\n", " a+b)\n", "\n", "e3 = begin\n", " 1;\n", " 2;\n", " a+b\n", "end\n", "\n", "e1 == e2 == e3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Infix notation for `Map`, `Apply`, `Rule`, `ReplaceAll`, etc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Map`" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\text{Map} \\! \\left( f,list \\right) $$" ], "text/plain": [ "L\"$$ \\text{Map} \\! \\left( f,list \\right) $$\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f % list" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ f \\! \\left( a \\right) ,f \\! \\left( b \\right) ,f \\! \\left( c \\right) \\right] $$" ], "text/plain": [ "L\"$$ \\left[ f \\! \\left( a \\right) ,f \\! \\left( b \\right) ,f \\! \\left( c \\right) \\right] $$\"" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f % [a,b,c]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Apply`" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\text{Apply} \\! \\left( x,y \\right) $$" ], "text/plain": [ "L\"$$ \\text{Apply} \\! \\left( x,y \\right) $$\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x .% y" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ f \\! \\left( 1,2 \\right) $$" ], "text/plain": [ "L\"$$ f \\! \\left( 1,2 \\right) $$\"" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f .% g(1,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Rule`\n", "\n", "`Rule` can be entered in the following ways. The symbol ⇒ can be entered with `\\Rightarrow[TAB]`" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ a \\Rightarrow b,a \\Rightarrow b,a \\Rightarrow b \\right] $$" ], "text/plain": [ "L\"$$ \\left[ a \\Rightarrow b,a \\Rightarrow b,a \\Rightarrow b \\right] $$\"" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[Rule(a,b), a => b , a ⇒ b]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`RuleDelayed`" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ a\\text{:>}b,a\\text{:>}b \\right] $$" ], "text/plain": [ "L\"$$ \\left[ a\\text{:>}b,a\\text{:>}b \\right] $$\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[RuleDelayed(a,b), a .> b]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`ReplaceAll`\n", "\n", "The short \"infix\" symbol for `ReplaceAll` is `./`. In Mathematica, it is `/.`.\n", "Also note the parentheses surrounding the rule." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ a,g \\! \\left( \\left[ 2 \\right] ,x \\right) ,g \\! \\left( \\left[ 3 \\right] ,b \\right) ,g \\! \\left( \\left[ 3 \\right] ,a + b \\right) \\right] $$" ], "text/plain": [ "L\"$$ \\left[ a,g \\! \\left( \\left[ 2 \\right] ,x \\right) ,g \\! \\left( \\left[ 3 \\right] ,b \\right) ,g \\! \\left( \\left[ 3 \\right] ,a + b \\right) \\right] $$\"" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[a, x^2, b^3, (a+b)^3] ./ ( x_^n_ => g([n],x))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Patterns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Blank\n", "\n", "`x_` is a blank matching a single element. `y__` is a blank sequence, matching one or more elements" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ 1, \\left[ 2,3,4 \\right] \\right] $$" ], "text/plain": [ "L\"$$ \\left[ 1, \\left[ 2,3,4 \\right] \\right] $$\"" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(x_, y__) := [x,[y]]\n", "f(1,2,3,4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Repeated" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Mathematica, `Repeated[a]` is denoted by `a..`. In Symata, `Repeated(a)` is denoted by `a...`. Notice that in Symata, there are three dots instead of two." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{True},\\text{False} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{True},\\text{False} \\right] $$\"" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[\n", "MatchQ([a, a, b, (a + b)^3, c, c, c], [a..., b, _^3... , c...])\n", "MatchQ([a, a, (a + b)^3, c, c, c], [a..., b, _^3... , c...])\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Repeated` can be used in operator form." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "ClearAll(f)\n", "f(x_...) := x" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ 3 $$" ], "text/plain": [ "L\"$$ 3 $$\"" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(3,3,3,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Mathematica, `Repeated[expr,n]` matches at least `n` occurences of `expr`. In Symata, `Repeated(expr,n)` does the same." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{False},\\text{True} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{False},\\text{True} \\right] $$\"" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[\n", " MatchQ([1,2,3], [Repeated(_Integer,2)])\n", " MatchQ([1,2,3], [Repeated(_Integer,3)])\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### RepeatedNull\n", "\n", "In Symata `RepeatedNull` must be written in full form. (We have not yet chosen a symbol for it)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\text{True} $$" ], "text/plain": [ "L\"$$ \\text{True} $$\"" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MatchQ([], [RepeatedNull(_Integer)])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As in Mathematica, default values of optional arguments are specified using `:`." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{True},\\text{True},\\text{True} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{True},\\text{True},\\text{True} \\right] $$\"" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ClearAll(f, a, b)\n", "\n", "f(x_, y_:a, z_:b) := [x,y,z]\n", "\n", "[\n", " f(1) == [1,a,b]\n", " f(1,2) == [1,2,b]\n", " f(1,2,3) == [1,2,3]\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Named compound Patterns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Mathematica, names for complex patterns use a colon `a:(b_^c_)`. In Symata, use two colons." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ g \\! \\left( b^{b} \\right) $$" ], "text/plain": [ "L\"$$ g \\! \\left( b^{b} \\right) $$\"" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ClearAll(g,a,b)\n", "\n", "b^b ./ (a::(_^_) => g(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### PatternTest\n", "\n", "In Mathematica, `PatternTest` is given like this `p_?PrimeQ`. In Symata this is ``p_`PrimeQ` ``" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ 25 $$" ], "text/plain": [ "L\"$$ 25 $$\"" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "countprimes = Count(_`PrimeQ`) # We use the curried form of Count\n", "countprimes(Range(100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use a Julia function as the test." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{True},\\text{True},\\text{True},\\text{False} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{True},\\text{True},\\text{True},\\text{False} \\right] $$\"" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = _`J( x -> -1 < x < 1 )`\n", "\n", "[\n", "MatchQ(0, p),\n", "MatchQ(.5, p),\n", "MatchQ(-1/2, p),\n", "MatchQ(-1, p)\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Alternatives\n", "\n", "`Alternatives` is denoted by the vertical bar `|`." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\left[ 2 \\right] , \\left[ 2,\\text{\"cat\"} \\right] ,f \\! \\left( 2,3 \\right) \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\left[ 2 \\right] , \\left[ 2,\\text{\\\"cat\\\"} \\right] ,f \\! \\left( 2,3 \\right) \\right] $$\"" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ClearAll(f)\n", "\n", "f(x_, x_ | y_String) := [x, y]\n", "\n", "[ f(2,2) , f(2,\"cat\"), f(2, 3)]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Condition\n", "\n", "`Condition` must be written in full form." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{True},\\text{False} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{True},\\text{False} \\right] $$\"" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[\n", "MatchQ( -2 , Condition(x_ , x < 0))\n", "MatchQ( 2 , Condition(x_ , x < 0))\n", "]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ 1,2,y,\\text{\"cat\"} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ 1,2,y,\\text{\\\"cat\\\"} \\right] $$\"" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ClearAll(y)\n", "\n", "ReplaceAll([1, 2, 3, \"cat\"], x_Integer => Condition(y, x > 2))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ f \\! \\left( 2 \\right) ,16 \\right] $$" ], "text/plain": [ "L\"$$ \\left[ f \\! \\left( 2 \\right) ,16 \\right] $$\"" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ClearAll(f)\n", "\n", "f(x_) := Condition(x^2, x > 3)\n", "\n", "[f(2), f(4)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Version and date" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Symata version 0.4.6\n", "Julia version 1.6.0-DEV.58\n", "Python version 3.8.3\n", "SymPy version 1.5.1\n" ] } ], "source": [ "VersionInfo()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2020-05-29T22:24:48.626" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "InputForm(Now())" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.6.0-DEV", "language": "julia", "name": "julia-1.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.6.0" } }, "nbformat": 4, "nbformat_minor": 1 }