using Symata
Symata is similar to the Wolfram language. But, there are difference in notation and behavior.
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 *
.
[2x + 1, 2 * x + 1, 2 * 3, Cos(x) * Sin(x)]
In Symata, comments are entered with the prefix #
. In Mathematica, comments are entered like this (* comment *)
# a = 1
b = 2
[a,b]
Mathematica lists are delimited by { }
. Symata lists are delimited by [ ]
[a,b,c]
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
)
[
a
c + d
"cat"
Expand((x+y)^2)
]
Function arguments are delimited by [ ]
in Mathematica. In Symata, arguments are delimited by ( )
f(x)
There are several ways to enter compound expressions.
e1 = (1,2,a+b)
e2 = (1,
2,
a+b)
e3 = begin
1;
2;
a+b
end
e1 == e2 == e3
Map
, Apply
, Rule
, ReplaceAll
, etc.¶Map
f % list
f % [a,b,c]
Apply
x .% y
f .% g(1,2)
Rule
Rule
can be entered in the following ways. The symbol ⇒ can be entered with \Rightarrow[TAB]
[Rule(a,b), a => b , a ⇒ b]
RuleDelayed
[RuleDelayed(a,b), a .> b]
ReplaceAll
The short "infix" symbol for ReplaceAll
is ./
. In Mathematica, it is /.
.
Also note the parentheses surrounding the rule.
[a, x^2, b^3, (a+b)^3] ./ ( x_^n_ => g([n],x))
x_
is a blank matching a single element. y__
is a blank sequence, matching one or more elements
f(x_, y__) := [x,[y]]
f(1,2,3,4)
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.
[
MatchQ([a, a, b, (a + b)^3, c, c, c], [a..., b, _^3... , c...])
MatchQ([a, a, (a + b)^3, c, c, c], [a..., b, _^3... , c...])
]
Repeated
can be used in operator form.
ClearAll(f)
f(x_...) := x
f(3,3,3,3)
In Mathematica, Repeated[expr,n]
matches at least n
occurences of expr
. In Symata, Repeated(expr,n)
does the same.
[
MatchQ([1,2,3], [Repeated(_Integer,2)])
MatchQ([1,2,3], [Repeated(_Integer,3)])
]
In Symata RepeatedNull
must be written in full form. (We have not yet chosen a symbol for it)
MatchQ([], [RepeatedNull(_Integer)])
As in Mathematica, default values of optional arguments are specified using :
.
ClearAll(f, a, b)
f(x_, y_:a, z_:b) := [x,y,z]
[
f(1) == [1,a,b]
f(1,2) == [1,2,b]
f(1,2,3) == [1,2,3]
]
In Mathematica, names for complex patterns use a colon a:(b_^c_)
. In Symata, use two colons.
ClearAll(g,a,b)
b^b ./ (a::(_^_) => g(a))
In Mathematica, PatternTest
is given like this p_?PrimeQ
. In Symata this is p_`PrimeQ`
countprimes = Count(_`PrimeQ`) # We use the curried form of Count
countprimes(Range(100))
You can use a Julia function as the test.
p = _`J( x -> -1 < x < 1 )`
[
MatchQ(0, p),
MatchQ(.5, p),
MatchQ(-1/2, p),
MatchQ(-1, p)
]
Alternatives
is denoted by the vertical bar |
.
ClearAll(f)
f(x_, x_ | y_String) := [x, y]
[ f(2,2) , f(2,"cat"), f(2, 3)]
Condition
must be written in full form.
[
MatchQ( -2 , Condition(x_ , x < 0))
MatchQ( 2 , Condition(x_ , x < 0))
]
ClearAll(y)
ReplaceAll([1, 2, 3, "cat"], x_Integer => Condition(y, x > 2))
ClearAll(f)
f(x_) := Condition(x^2, x > 3)
[f(2), f(4)]
VersionInfo()
Symata version 0.4.1-dev.3 Julia version 0.7.0-beta2.1 Python version 2.7.14+ SymPy version 1.0
InputForm(Now())
2018-07-20T13:17:54.738