Load Symata like this
using Symata;
INFO: Recompiling stale cache file /home/lapeyre/.julia/lib/v0.6/Symata.ji for module Symata.
Symata is a computer language written in Julia. After typing using Symata
, you are in Symata mode and input is interpreted as Symata language expressions.
Here is a Symata expression
(x+y)^3
In a Jupyter notebook, you can exit Symata mode and enter Julia mode by entering Julia()
. Expressions are then Julia language expressions.
Julia()
length(zeros(10)) == 10 # A Julia expression
true
In Jupyter, type isymata()
to leave Julia mode and enter Symata
mode. At the command line REPL, type =
at the beginning of a line to enter Symata
mode.
isymata() # we enter Symata mode again
Note: to leave Symata
mode and return to Julia
mode, type Julia()
in IJulia
, or backspace at the command line REPL.
Now we can enter Symata
expressions.
You enter expressions and Symata evaluates them
Cos(π * x)
A variable is set like this.
x = 1/3
Now the cosine is evaluated
Out(6)
x = 1/6
Out(6)
Clear the value of x
and the cosine can no longer be reduced.
Clear(x)
Out(6)
It is clear what happened above. If x
is not set to a value, then Cos(π * x)
can't be written in a simpler form. If we set x
to some particular values, then Cos(π * x)
can be reduced to a simpler form.
(you can skip the following the first time through)
But, the reason Symata
understands this is a consquence of the procedure it follows in evaluating (almost) all expressions. Symata
evaluates expressions to a fixed point. More precisely, when an expression is given as input, Symata
descends depth-first evaluating each subexpression to a fixed point and finally the top-level expression to a fixed point. When Cos(π * x)
is first evaluated, each of π
and x
evaluates to itself so that π * x
is already at a fixed point. Since there is no rule for evaluating Cos(π * x)
for fixed π * x
, Cos(π * x)
is also at a fixed point.
The expression x=1/3
means that, whenever x
is encountered, it should evaluate to 1/3
. The expression Out(4)
evaluates to the fourth output cell, which is Cos(π * x)
. Then π
evaluates to iteself, x
evaluates to 1
, so that π * x
evaluates to π/3
. There is a rule saying that Cos(x/3)
evaluates to 1/2
.
Clear(x)
says that x
should once again evaluate to itself. Then evaluating Out(4)
follows the same evaluation sequence, leading to Cos(π * x)
There are several kinds of assignment in Symata. The two most common are =
(or Set
) and :=
(or SetDelayed
).
Set
immediatley evaluates the right hand side and binds the left hand side to the result. SetDelayed
does not evaluate the right hand side when the assignment is made. It evaluates the right hand side every time the left hand side is subsequently evalutated and then binds the result.
The following demonstrates the difference.
x = 1
a := x
b = x
c = a
d := a
[x,a,b,c,d]
ClearAll(x)
[x,a,b,c,d]
(a = z, [x,a,b,c,d])
ClearAll(x,a,b,c,d)
Assign two variables at once
[a,b] = [x,y]
a
b
[a,b]
Swap two values
[a,b] = [b,a]
[a,b]
Expression is the central concept in Symata. In general, expressions are trees whose branches and leaves are other expressions. You can manipulate these expressions.
Every expression has a Head
. For function-like expressions, the Head
is the function name. For atomic expressions, the Head
usually is a data type.
Map(Head, [x, x + y, [x,y], Cos(x), f(x), 3, 3.0, BI(3), BF(3)]) # apply the fun
expr = Expand((x+y)^3)
FullForm(expr) # This shows the internal form. The tree is explicit
Plus(Power(x,3),Times(3,Power(x,2),y),Times(3,x,Power(y,2)),Power(y,3))
expr[2,2,1] # Return a part of the expression by index into the tree
expr[2,2,1] = z; # Replace a part of the expression
expr
Part(expr,2,2,1) # You can do the same thing with Part
Expand((x+y)^3)[4,1] # You can get parts of expressions directly
expr = Expand((x+y)^20);
expr[14:18:2] # Parts 14 through 18 with step 2
ClearAll(expr)
Define a function that collects an expression's head and arguments in a list.
headargs(f_(args__)) := [f,args]
headargs(a + b^2 + 3)
Integrate(f(x),x)
headargs(Integrate(f(x),x))
ClearAll(a,b,c,d)
Rotate the head and arguments to make a new expression.
rotheadargs(f_(args__)) := (Last([args])(f,Splat(Most([args]))))
rotheadargs( a + b + c + d)
rotheadargs( a + b + c + d + g(x))
Definition
shows the definitions associated with a symbol
ClearAll(x,a,b,c,d) # delete definitions from the previous example
a = 1
Definition(a)
a=1
a := x
Definition(a) # This overwrites the previous definition
a:=x
f(x_) := x^2
f(x_, y_) := x + y
Definition(f)
f(x_):=x^2 f(x_,y_):=(x + y)
Definition(f)
f(x_):=x^2 f(x_,y_):=(x + y)
ClearAll(f,a)
Timing((Range(10^6), Null ) ) # time a single expression
Time
toggles the timing of every expression entered. Memory allocated and the number of attempts to apply a user defined rule are also printed.
Time(True) # toggle timing all expressions. returns the previous value
Range(10^6);
0.014836 seconds (999.57 k allocations: 22.887 MB, 30.74% gc time) tryrule count: downvalue 0, upvalue 0
Time(False);
0.000052 seconds (35 allocations: 3.109 KB) tryrule count: downvalue 0, upvalue 0
Trace(True); # Trace evaluation
2<< False 1<< False
(a+b)*(a+b)
>>1 CompoundExpression(nothing,(a + b)*(a + b)) >>2 (a + b)*(a + b) >>3 a + b 3<< a + b >>3 a + b 3<< a + b 2<< (a + b)^(1 + 1) >>2 (a + b)^(1 + 1) >>3 1 + 1 3<< 2 2<< (a + b)^2 >>2 (a + b)^2 2<< (a + b)^2 1<< (a + b)^2 >>1 (a + b)^2 1<< (a + b)^2
Trace(False);
>>1 CompoundExpression(nothing,Trace(False)) >>2 Trace(False)
? LeafCount
LeafCount(expr)
gives the number of indivisible (Part
can't be taken) elements in expr
.
This amounts to counting all the Heads
and all of the arguments that are not of type Mxpr
, that is compound expressions.
A more accurate name is NodeCount
.
Help( LeafCount) Attributes(LeafCount) = [Protected]
LeafCount(Expand((a+b)^3))
ByteCount(Expand((a+b)^3))
? Depth
Depth(expr)
gives the maximum number of indices required to specify any part of expr
, plus 1
.
Help( Depth) Attributes(Depth) = [Protected]
Depth(Expand((a+b)^3))
FullForm(Expand((a+b)^3)) # Examine the tree
Plus(Power(a,3),Times(3,Power(a,2),b),Times(3,a,Power(b,2)),Power(b,3))
Expand((a+b)^3)[2,2,1] # One of the deepest parts
Here are a few ways to compute an integral
Integrate( (1+x^2)^(-1), x)
expr = 1/(1+x^2)
Integrate(expr, x)
f(x_) := 1/(1+x^2)
Integrate(expr, x)
g(x_) = expr # Note we do not use ":="
ClearAll(expr) # We did not use SetDelay, so we can delete expr
Integrate(g(y),y)
Note: Trying to use a compiled (Julia) function h = J( x -> 1/(1+x^2))
will not work.
ClearAll(f,g,expr)
Integrate(f(y), y) # The integral can no longer be reduced
A Blank
with no constraints matches everything
MatchQ(z,_)
Map(MatchQ(_), [1,"string", a+b, 1/3]) # MatchQ does Currying with the first argument
_head
is a Blank
that only matches expressions with Head
equal to head
.
FullForm(_Integer) # underscore is shorthand for Blank
Blank(Integer)
MatchQ(1, _Integer)
Use Currying to define a predicate function
myintq = MatchQ(_Integer);
Not all rational numbers are integers
Map(myintq, Range(1/2,5,1/2))
MatchQ(b^2, _^2) # Match power with exponent equal to 2
MatchQ(b^3, _^_) # Match any power
MatchQ((b+c)^3, _^_)
MatchQ(b^1, _^_)
This failed because b^1
evaluates to b
, which does not have the structure of a power
The pattern can be complex with blanks deep in an expression.
Map(MatchQ(f(x_^2)), [f(b^2), f(b^3), g(b^2)])
Specify a "function" Head
that must match
Map( MatchQ(_gg), [gg(x+y), gg(x), g(x)])
Define a predicate for positive integers
m = MatchQ(_Integer:?(Positive))
Map(m, [1,100, 0, -1, 1.0, x])
We can also put a condition on a Pattern
. This matches pairs with the first element smaller than the second.
m = MatchQ(Condition([x_, y_], x < y))
[ m([2,1]), m([1,2]), m([1,2,3]), m(1)]
Pattern
s can include Alternative
s.
m = MatchQ(_Integer | _String)
[m(1), m("zebra"), m(1.0)]
Repeated(expr)
matches one or more occurences of expr
.
MatchQ([a,a,a,b], [Repeated(a),b])
RepeatedNull
matches zero or more occurences.
MatchQ([b], [RepeatedNull(a),b])
ClearAll(m)
Rule
s are used for many things in Symata, including replacement. Replacement is a key ingredient in the implementation of functions.
When applied, this rule matches and does a replacement on any expression with Head
f
and a List
of two elements as the sole argument.
f([x_,y_]) => p(x+y)
expr = f([x+y,y]) + f(c) + g([a,b])
ReplaceAll( expr, f([x_,y_]) => p(x+y))
There are several things to note here.
The pattern x_
puts no restrictions on the match; any expression will match. The name of the pattern x
only serves to identify it later during a replacement.
Here x_
has matched x+y
, but these two uses of x
are not confused in the result. That is, in x_
, the symbol x
is a dummy variable.
The expression f(c)
has a matching Head
, but not matching arguments, so f(c)
fails to match. Likewise, the expression g([a,b])
has matching arguments, but not matching head.
The expression f([x+y,y])
matches, and the replacement is made in (a copy of) expr
. But, Symata alays evaluates expressions to a fixed point. So y+y
is replaced by 2y
, and the terms in expr
are rearranged into the canonical order.
Again, we have to be aware that matching is structural.
ReplaceAll([a/b, 1/b^2, 2/b^2] , b^n_ => d(n))
ClearAll(expr)
Named patterns that appear in more than one place must match the same expression.
ReplaceAll( [b,a,[a,b]] , [x_,y_,[x_,y_]] => 1 ) # This does not match
ReplaceAll( [a,b,[a,b]] , [x_,y_,[x_,y_]] => 1 ) # This does match
This example uses Alternative
s.
ReplaceAll( [a, b, c, d, a, b, b, b], a | b => x)
The arguments of Sequence
are spliced into expressions during evaluation.
[1,2,Sequence(a,b)]
An unmatched alternative is replaced by Sequence()
. Upon evaluation to fixed point, this empty sequence is removed.
f(x_, x_ | y_String) := [x,y]
f(2,2) # `y` does not match, so it is removed.
f(2,"cat") # Here the second Alternative matches
f(2,3) # Here the Pattern fails to match.
Alternative
s, and Pattern
s in general, can be explicit expressions, with no Blank
s.
( h(a | b) := p, [h(a), h(b), h(c), h(d)] )
ReplaceAll
replaces all matching subexpressions. We can also specify the levels. This matches at level 2 and deeper.
Replace(1 + a + f(a) + g(f(a)), a => b, 2)
This replaces only at level 2.
Replace(1 + a + f(a) + g(f(a)), a => b, [2]) == 1 + a + f(b) + g(f(a))
Rule
evaluates the right hand side once, when it is first evaluated.
ReplaceAll( [x,x,x,x,x], x => RandomReal() )
RuleDelayed
evaluates the right hand side every time it is applie
ReplaceAll( [x,x,x,x,x], RuleDelayed(x ,RandomReal()))
Except
matches everything except expressions that match its argument. The following applies the replacement at level 1.
Replace([1, 7, "Hi", 3, Indeterminate], Except(_:?(NumericQ)) => 0, 1)
Each Rule
in a List
of Rule
s is tried in turn. Matching stops after the first match. ReplaceRepeated
continues applying Rules
until the expression reaches a fixed point.
ReplaceRepeated(x^2 + y^6 , List(x => 2 + a, a => 3))
Up to this point, we have used named patterns only with a single blank, for example b_
. But, we may associate aname with any pattern expression, including a complex (compound) expression.
ReplaceAll( b^c, a::(_^_) => g(a))
Patterns
are used to implement optional arguments.
f(x_, y_:3) := x + y
[f(a+b,z), f(a+b)]
Condition
may be used in definitions like this:
ClearAll(f)
f(x_) := Condition(x^2, x > 3)
[f(4),f(3)]
We can match and replace with a Pattern
with Head
equal to Plus
ReplaceAll( z*y + b , x_ + y_ => x * y )
ReplaceAll( z*y + b +c , x_ + y_ => x * y)
This failed because Plus
with two terms does not match Plus
with three terms. But, we actually do want this to match. Implementing associative-commuatative matching is a major goal of Symata. Anyone want to give it a try ?
ClearAll(f,h,a,b,x,y)
Symata's host language is Julia, a high-performance, compiled language. It can be useful to call Juila code from Symata or to compile Symata code to Julia. Symata is also an open-source project, which means you can alter or add to it directly.
Define a compiled function to a built-in or user-defined Julia function like this
mylog = J(log )
mylog(2,2)
You can also easily write compiled code like this
f = J( (x,y) -> x^2 + y^2 )
f(3.0,4.0)
Note that we did not specify the data types. Is this really high-performance compiled code ? Yes it is. The function was compiled after we called it with two floating point numbers. If we call the function with two integers, a version (called a method) that is optimized for integers is compiled. A version optimized for an integer and a rational number or any combination of arguments can also be compiled.
[f(3,4), f(3, 1/2)]
We define two versions of the same function to see the difference in performance between compiled functions and functions defined via Rule
s.
(a = Range(0.0,100.,.01), ccossq = J( x -> cos(x)^2 ), cossq(x_) := cos(x)^2);
Time(True);
We run each test twice because compilation time is included in the first run.
Map(cossq, a);
1.869300 seconds (2.88 M allocations: 174.610 MB, 4.11% gc time) tryrule count: downvalue 10001, upvalue 0
Map(cossq, a);
1.225530 seconds (2.59 M allocations: 162.150 MB, 7.08% gc time) tryrule count: downvalue 10001, upvalue 0
Map(ccossq, a);
0.018656 seconds (43.26 k allocations: 1014.076 KB) tryrule count: downvalue 0, upvalue 0
Map(ccossq, a);
0.002087 seconds (40.11 k allocations: 867.703 KB) tryrule count: downvalue 0, upvalue 0
(Time(False), ClearAll(f,a,ccossq,cossq,mylog))
0.000358 seconds (3.18 k allocations: 106.859 KB) tryrule count: downvalue 0, upvalue 0
The compiled function is about $500$ times faster in this example. Symata Pattern
s can be compiled (automatically) as well, but this has been removed during a rewriting of the Pattern
code that is still underway. With compiled Pattern
s, the factor might be closer to $50$.
In this example we calculate an expression and compile it. The compiled code is as efficient hand-coded Julia.
expr = Integrate( x^2 * Exp(x)* Cos(x), x)
expr = Collect(expr, Exp(x))
Now we use Compile
. By default, Compile
does not evauate its arguments. So we ask explicitly for evaluation.
cexpr = Compile(Evaluate(expr))
a = Range(0.0,10.0,.01);
Timing((Map(cexpr, a), Null))
Timing((Map(cexpr, a), Null))
cexpr(2.0)
ClearAll(a,expr,cexpr)
VersionInfo()
symata version 0.3.0-dev.9 julia version 0.6.0-dev.435 python version 2.7.12 sympy version 1.0
Now()