{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# using Symata # when starting a stock Julia image\n", "isymata() # when starting the precompiled-with Symata image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a translation from *Mathematica* to *Symata* of the Introduction to \"*Mathematica* programming: an advanced introduction\" v 1.01 by Leonid Shifrin. Minimal changes to accomodate *Symata* were made. This text is licensed under the Creative Commons Attribution-Noncomercial-Share Alike 3.0 United States license. http://creativecommons.org/licenses/by-nc-sa/3.0/us/\n", "\n", "*Mathematica* is a registered trademark of Wolfram Research Inc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Despite the title, since this is only the introduction, there is almost nothing about programming here. Rather it illustrates some of the core principles of the language. These principles concern the part of *Symata* that follows *Mathematica* semantics. *Symata* also breaks and enhances *Mathematica* semantics by integrating smoothly with Julia." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction\n", "\n", "## First principle: everything is an expression\n", "\n", "The first principle states that every object dealt with by *Symata*, is an expression. Every *Symata* expression is either Atom, or a Normal Expression." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Atoms and the built-in AtomQ predicate\n", "Atoms are numbers, symbols and strings, and numbers are further divided into Integers, Reals, Rationals\n", "and Complex. All other objects are composite and are called Normal Expressions. It is always possible to\n", "check whether or not an expression is an atom or a composite, by acting on it with the built-in predicate\n", "AtomQ. For instance:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{True},\\text{False},\\text{True},\\text{True} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{True},\\text{False},\\text{True},\\text{True} \\right] $$\"" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ClearAll()\n", "[AtomQ(x), AtomQ(Sin(x)), AtomQ(1 + I * 2), AtomQ(2 / 3)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### *Symata* normal (composite) expressions\n", "Every normal expression (composite) is built according to a universal pattern:\n", "expr`[el1, ..., eln]`\n", "Here it is required that some symbol `` is present (it can itself be a normal expression, not necessar-\n", "ily an atom), as well as the single square brackets. Inside the parentheses, there can be zero, one or\n", "several comma-separated elements `,...,`. These elements themselves can be either atoms or\n", "normal expressions. In an expression `Sin(x)`, `` is Sin, and there is a single element ``, which is\n", "atom (as long as x is not defined as something else, but this already has to do with expression evaluation\n", "and will be discussed below). It is clear that an arbitrary *Symata* expression must have a tree-like\n", "structure, with the branches being normal (sub)expressions and leaves being atoms." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Literal equivalents of built-in functions, and FullForm command\n", "\n", "As a consequence, any built-in command/function in *Symata* has a literal/string equivalent (so that it can be represented in the above uniform way). This is most easily seen with the help of the built-in function FullForm, which shows the internal representation of any object/expression, in the way it is really\n", "\"seen\" by the kernel. For instance:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ z \\ \\text{Sin} \\! \\left( x + y \\right) ,\\text{Times(z,Sin(Plus(x,y)))} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ z \\ \\text{Sin} \\! \\left( x + y \\right) ,\\text{Times(z,Sin(Plus(x,y)))} \\right] $$\"" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[z * Sin(x +y), FullForm(z * Sin(x +y))]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The second expression in the square brackets is equivalent to the first one, but explicitly shows the structure\n", "described above." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### All normal expressions are trees - TreeForm command (not implemented)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the following example `` is\n", "Times (the multiplication command):" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Times(z,Sin(Plus(x,y)))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = z * Sin(x +y);\n", "FullForm(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Heads of expressions and the Head command\n", "In general, an expression outside the square brackets has a name - it is called a head of expression, or just\n", "head. There is a built-in function with the same name, which allows to obtain the head of an arbitrary\n", "expression. For example:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\text{Times} $$" ], "text/plain": [ "L\"$$ \\text{Times} $$\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Head(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A head of an expression may be either an atom or a normal expression itself. For example :" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ f \\! \\left( g \\right) \\! \\left( h \\right) $$" ], "text/plain": [ "L\"$$ f \\! \\left( g \\right) \\! \\left( h \\right) $$\"" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Clear(b, f, g, h, x);\n", "b = f(g)(h)(x);\n", "Head(b)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ f \\! \\left( g \\right) $$" ], "text/plain": [ "L\"$$ f \\! \\left( g \\right) $$\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Head(f(g)(h))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ f $$" ], "text/plain": [ "L\"$$ f $$\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Head(f(g))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Every expression has a head, even atoms. Heads for them include String, Symbol, Integer, Real, Rational and\n", "Complex. For instance :" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{Symbol},\\text{Int64},\\text{Symbol},\\text{Float64},\\text{String},\\text{Rational{Int64}},\\text{Complex{Int64}} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{Symbol},\\text{Int64},\\text{Symbol},\\text{Float64},\\text{String},\\text{Rational{Int64}},\\text{Complex{Int64}} \\right] $$\"" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[Head(f), Head(2), Head(Pi), Head(3.14), Head(\"abc\"), Head(2/3), Head(1 + I)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing individual parts of expressions through indexing\n", "One can access also the internal parts of an expression (those inside the parentheses), by using indexing (Part command). The following example illustrates this." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{Times},z,\\text{Sin} \\! \\left( x + y \\right) ,\\text{Sin},x + y,\\text{Plus},x,y \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{Times},z,\\text{Sin} \\! \\left( x + y \\right) ,\\text{Sin},x + y,\\text{Plus},x,y \\right] $$\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[a[0], a[1], a[2], a[2, 0], a[2, 1], a[2, 1, 0], a[2, 1, 1], a[2, 1, 2]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have just deconstructed our expression to pieces. In fact, we started from the \"stem\" and then went\n", "down along the \"branches\" to the \"leaves\" of the tree which we have seen above with the TreeForm. We\n", "see that the addresses (index sequences) which end with zero give the Heads of the subexpressions - this\n", "is a convention. In principle, any complex expression can be deconstructed in this way, and moreover, one\n", "can change its subexpressions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Levels of expressions and the Level command\n", "It is also possible to get access to the branches (subexpressions) which are at the certain distance (level)\n", "from the \"stem\". This is achieved by using a built-in Level command. Consider an example:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ z1 \\ \\text{Cos} \\! \\left( x1 + y1 \\right) + z \\ \\text{Sin} \\! \\left( x + y \\right) $$" ], "text/plain": [ "L\"$$ z1 \\ \\text{Cos} \\! \\left( x1 + y1 \\right) + z \\ \\text{Sin} \\! \\left( x + y \\right) $$\"" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Clear(a)\n", "a = z * Sin(x + y) + z1 * Cos(x1 + y1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here it is in its full form:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Plus(Times(z1,Cos(Plus(x1,y1))),Times(z,Sin(Plus(x,y))))" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FullForm(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These are the levels of the tree:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ z1 \\ \\text{Cos} \\! \\left( x1 + y1 \\right) + z \\ \\text{Sin} \\! \\left( x + y \\right) \\right] $$" ], "text/plain": [ "L\"$$ \\left[ z1 \\ \\text{Cos} \\! \\left( x1 + y1 \\right) + z \\ \\text{Sin} \\! \\left( x + y \\right) \\right] $$\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Level(a, [0])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ z1 \\ \\text{Cos} \\! \\left( x1 + y1 \\right) ,z \\ \\text{Sin} \\! \\left( x + y \\right) \\right] $$" ], "text/plain": [ "L\"$$ \\left[ z1 \\ \\text{Cos} \\! \\left( x1 + y1 \\right) ,z \\ \\text{Sin} \\! \\left( x + y \\right) \\right] $$\"" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Level(a, [1])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ z1,\\text{Cos} \\! \\left( x1 + y1 \\right) ,z,\\text{Sin} \\! \\left( x + y \\right) \\right] $$" ], "text/plain": [ "L\"$$ \\left[ z1,\\text{Cos} \\! \\left( x1 + y1 \\right) ,z,\\text{Sin} \\! \\left( x + y \\right) \\right] $$\"" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Level(a, [2])" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ x1 + y1,x + y \\right] $$" ], "text/plain": [ "L\"$$ \\left[ x1 + y1,x + y \\right] $$\"" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Level(a, [3])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ x1,y1,x,y \\right] $$" ], "text/plain": [ "L\"$$ \\left[ x1,y1,x,y \\right] $$\"" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Level(a, [4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Level[a, {n}]` gives all branches (or leaves) which have a distance of n levels down from the \"stem\". If\n", "however we need all branches that have n levels of sub - branches (or leaves), then we use a negative level\n", "`Level[a, {-n}]` :" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ x1,y1,x,y \\right] $$" ], "text/plain": [ "L\"$$ \\left[ x1,y1,x,y \\right] $$\"" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Level(a, [-1])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ x1 + y1,x + y \\right] $$" ], "text/plain": [ "L\"$$ \\left[ x1 + y1,x + y \\right] $$\"" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Level(a, [-2])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ z1,\\text{Cos} \\! \\left( x1 + y1 \\right) ,z,\\text{Sin} \\! \\left( x + y \\right) \\right] $$" ], "text/plain": [ "L\"$$ \\left[ z1,\\text{Cos} \\! \\left( x1 + y1 \\right) ,z,\\text{Sin} \\! \\left( x + y \\right) \\right] $$\"" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Level(a, [-3])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ z1 \\ \\text{Cos} \\! \\left( x1 + y1 \\right) ,z \\ \\text{Sin} \\! \\left( x + y \\right) \\right] $$" ], "text/plain": [ "L\"$$ \\left[ z1 \\ \\text{Cos} \\! \\left( x1 + y1 \\right) ,z \\ \\text{Sin} \\! \\left( x + y \\right) \\right] $$\"" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Level(a, [-4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that negative levels generally can not be reduced to positive levels - they are giving in general\n", "different types of information. What we have just described is called the Standard Level Specification in\n", "*Symata*. Many more built - in commands accept level specification as one of the arguments (often an\n", "optional one)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any function can be used also in its literal equivalent form. For instance :" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ 10,24 \\right] $$" ], "text/plain": [ "L\"$$ \\left[ 10,24 \\right] $$\"" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[Plus(1, 2, 3, 4), Times(1, 2, 3, 4)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Second principle: pattern-matching and rule substitution\n", "Another fundamental principle is so - called pattern - matching, which is a system to match rules and\n", "expressions - without it *Symata* would not know when to apply which rule. It is based on syntactic\n", "rather than semantic comparison of expressions. The main notions here are those of rules and patterns." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rewrite Rules" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "Clear(a, b, c, d, e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A typical *rule* looks like this:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ a \\Rightarrow b $$" ], "text/plain": [ "L\"$$ a \\Rightarrow b $$\"" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a => b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where in general `` and `` are some expressions. The rule just says: whenever `` is\n", "encountered, replace it by ``. For example:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ b,c,d,c \\right] $$" ], "text/plain": [ "L\"$$ \\left[ b,c,d,c \\right] $$\"" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[a, c, d, c] ./ (a => b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(the `./` symbol is a rule replacement command, to be covered later)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A pattern is essentially any expression with some part of it replaced by \"blank\" (`Blank()`), which is a\n", "placeholder for any expression - that is, instead of that part there can be anything (this is somewhat oversim-\n", "plified). The literal equivalent for `Blank[]` is the single underscore (\"`_`\") symbol. For instance, `f(x_)` means `f(anything)`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### An example of a simple pattern-defined function" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ 4,\\text{\"word\"}^{2},\\text{Newton}^{2} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ 4,\\text{\\\"word\\\"}^{2},\\text{Newton}^{2} \\right] $$\"" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Clear(f)\n", "f(x_) := x^2\n", "[f(2), f(\"word\"), f(Newton)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, the result is as shown because the definition of the function `f` is really just a substitution rule `f(anything) -> (anything)^2`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To see the internal form of this rule - how it is stored in the rule base - one can use the built-in DownValues command. With its help we see:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{HoldPattern} \\! \\left( f \\! \\left( x\\text{_} \\right) \\right) \\text{:>}x^{2} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{HoldPattern} \\! \\left( f \\! \\left( x\\text{_} \\right) \\right) \\text{:>}x^{2} \\right] $$\"" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DownValues(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will talk later about the meaning of the `HoldPattern` function. The pattern `x_` is the most simple pattern. There can be more complex patterns, both syntactically and also because patterns may have conditions attached to them, which ensure that the pattern will match only if the condition is satisfied\n", "(conditional patterns). We will cover them in detail later." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example of a function based on a restricted pattern\n", "Now let us give an example: we will restrict our function `f` to operate only on integers." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ 4,f \\! \\left( \\text{\"word\"} \\right) ,f \\! \\left( \\text{Newton} \\right) \\right] $$" ], "text/plain": [ "L\"$$ \\left[ 4,f \\! \\left( \\text{\\\"word\\\"} \\right) ,f \\! \\left( \\text{Newton} \\right) \\right] $$\"" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Clear(f)\n", "f(x_Integer) := x^2\n", "[f(2), f(\"word\"), f(Newton)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, we introduced a more complex pattern `x_Integer`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A bit about evaluation\n", "On this example we see that if there is no rule whose pattern (left hand side of the rule) matches a given\n", "expression, *Symata* returns the expression unchanged. This is at the heart of its evaluation method:\n", "to any entered expression, all rules which are in the global rule base at the moment of evaluation, are\n", "applied iteratively. Whenever some rule applies, an expression is rewritten and the process starts over.\n", "At some point, the expression becomes such that no rule can be applied to it, and this expression is the\n", "result. Since the rule base contains both system and user-defined rules (with the latter having higher\n", "priority), it gives great flexibility in manipulation of expressions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Patterns allow for multiple definitions of the same function\n", "As another starting example, let us define a function which is linear on even numbers, quadratic on odd\n", "numbers and is a Sin function for all other inputs:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "Clear(f)\n", "f(x_`EvenQ`) := x\n", "f(x_`OddQ`) := x^2\n", "f(x_) := Sin(x)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ 1,2,9,4,\\text{Sin} \\! \\left( \\frac{3}{2} \\right) ,\\text{Sin} \\! \\left( \\text{Newton} \\right) ,0 \\right] $$" ], "text/plain": [ "L\"$$ \\left[ 1,2,9,4,\\text{Sin} \\! \\left( \\frac{3}{2} \\right) ,\\text{Sin} \\! \\left( \\text{Newton} \\right) ,0 \\right] $$\"" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[f(1), f(2), f(3), f(4), f(3 / 2), f(Newton), f(Pi)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the record, built-in functions `OddQ` and `EvenQ` are predicates which return `True` if the number is odd\n", "(even) and `False` otherwise :" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{True},\\text{False},\\text{False},\\text{True} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{True},\\text{False},\\text{False},\\text{True} \\right] $$\"" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[EvenQ(2), EvenQ(3), OddQ(2), OddQ(3)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If nothing is known about the object, they give False :" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{False},\\text{False} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{False},\\text{False} \\right] $$\"" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[EvenQ(Newton), OddQ(Newton)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Automatic rule reordering\n", "Automatic rule reordering is *not* yet implemented:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{Sin} \\! \\left( 1 \\right) ,\\text{Sin} \\! \\left( 2 \\right) ,\\text{Sin} \\! \\left( 3 \\right) ,\\text{Sin} \\! \\left( 4 \\right) ,\\text{Sin} \\! \\left( \\frac{3}{2} \\right) ,\\text{Sin} \\! \\left( \\text{Newton} \\right) ,0 \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{Sin} \\! \\left( 1 \\right) ,\\text{Sin} \\! \\left( 2 \\right) ,\\text{Sin} \\! \\left( 3 \\right) ,\\text{Sin} \\! \\left( 4 \\right) ,\\text{Sin} \\! \\left( \\frac{3}{2} \\right) ,\\text{Sin} \\! \\left( \\text{Newton} \\right) ,0 \\right] $$\"" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Clear(f)\n", "f(x_) := Sin(x)\n", "f(x_`EvenQ`) := x\n", "f(x_`OddQ`) := x^2\n", "[f(1), f(2), f(3), f(4), f(3 / 2), f(Newton), f(Pi)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To see the order in which the rules are kept, we again use DownValues :" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{HoldPattern} \\! \\left( f \\! \\left( x\\text{_} \\right) \\right) \\text{:>}\\text{Sin} \\! \\left( x \\right) ,\\text{HoldPattern} \\! \\left( f \\! \\left( \\text{PatternTest} \\! \\left( x\\text{_},\\text{EvenQ} \\right) \\right) \\right) \\text{:>}x,\\text{HoldPattern} \\! \\left( f \\! \\left( \\text{PatternTest} \\! \\left( x\\text{_},\\text{OddQ} \\right) \\right) \\right) \\text{:>}x^{2} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{HoldPattern} \\! \\left( f \\! \\left( x\\text{_} \\right) \\right) \\text{:>}\\text{Sin} \\! \\left( x \\right) ,\\text{HoldPattern} \\! \\left( f \\! \\left( \\text{PatternTest} \\! \\left( x\\text{_},\\text{EvenQ} \\right) \\right) \\right) \\text{:>}x,\\text{HoldPattern} \\! \\left( f \\! \\left( \\text{PatternTest} \\! \\left( x\\text{_},\\text{OddQ} \\right) \\right) \\right) \\text{:>}x^{2} \\right] $$\"" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DownValues(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Third principle: expression evaluation\n", "The last example brings us to the third principle: the principle of expression evaluation and the rewrite\n", "rules (global rule base). It tells the following: when *Symata* encounters an arbitrary expression, it\n", "checks its global base of rewrite rules for rule(s) which correspond to a given expression (or, it is said,\n", "match the expression). A typical rewrite rule looks like `object1 -> object2`. If such a rule is found, for\n", "expression or any of the subexpressions (actually, normally in reverse order), the (sub) expression is\n", "rewritten, and the process starts over. This process goes on until no further rule in the global rule base is\n", "found which matches the expression or any of its parts. When the expression stops changing, it is returned\n", "as the answer. Please bear in mind that the picture just described is a great oversimplification, and the real\n", "evaluation process is much more subtle, although the main idea is this.\n", "\n", "The global rule base contains both rules built in the kernel and rules defined by the user. User-defined\n", "rules usually take precedence over the system rules, which makes it possible to redefine the behavior of\n", "almost any built-in function if necessary. In fact, all assignments to all variables and all function defini-\n", "tions are stored as some type of global rules, in the rule base. In this sense, there is no fundamental differ-\n", "ence between functions and variables (although there are technical differences).\n", "\n", "As a result of this behavior, we get for instance such a result:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FullForm(Sin(Pi + Pi))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The reason is that inside the kernel there are rules like `Plus[x,x]->2 x`, `Sin[2*Pi]->0`, and because the\n", "evaluation process by default starts with the innermost sub-expressions (leaves), i.e., from inside out, it\n", "produces 0 before the `FullForm` has any chance to \"look\" at the expression. The internal evaluation dynamics can be monitored with the `Trace` command:" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2<< False\n", " >>2 Sin(Plus(Pi,Pi))\n", " >>3 Sin(Pi + Pi)\n", " >>4 Pi + Pi\n", " 4<< 2Pi\n", " >>4 2Pi\n", " 4<< 2Pi\n", " 3<< 0\n", " 2<< 0\n", " >>2 0\n", " 2<< 0\n", "1<< 0\n", ">>1 0\n", "1<< 0\n" ] }, { "data": { "text/plain": [ "0" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Trace(True)\n", "FullForm(Sin(Pi + Pi))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">>1 CompoundExpression(Trace(False))\n", " >>2 Trace(False)\n" ] } ], "source": [ "Trace(False);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Summary\n", "To summarize, we have described briefly the main principles of Mathematica and along the way gave\n", "examples of use of the following built-in functions: `AtomQ`, `Head`, `FullForm`, `Level`, `Plus`, `Times`, `Trace`, `DownValues`, `OddQ`, `EvenQ`." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Symata version 0.4.6\n", "Julia version 1.6.0-DEV.116\n", "Python version 3.8.3\n", "SymPy version 1.5.1\n" ] } ], "source": [ "VersionInfo()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2020-05-31T10:22:55" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "InputForm(Now())" ] } ], "metadata": { "kernelspec": { "display_name": "Symata 1.6.0-DEV", "language": "julia", "name": "symata-1.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }