using Symata; (x+y)^3 Julia() length(zeros(10)) == 10 # A Julia expression isymata() # we enter Symata mode again expr = Cos(π * x) x = 1/3 expr x = 1/6 expr Clear(x) expr 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) [a,b] = [x,y] a b [a,b] [a,b] = [b,a] [a,b] Map(Head, [x, x + y, [x,y], Cos(x), f(x), 3, 3.0, BI(3), BF(3)]) expr = Expand((x+y)^3) FullForm(expr) # This shows the internal form. The tree is explicit 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); # The semi-colon suppresses printing the return value. expr[14:18:2] # Parts 14 through 18 with step 2 ClearAll(expr) headargs(f_(args__)) := [f,args] headargs(a + b^2 + 3) Integrate(f(x),x) headargs(Integrate(f(x),x)) ClearAll(a,b,c,d) rotheadargs(f_(args__)) := (Last([args])(f,Splat(Most([args])))) rotheadargs( a + b + c + d) rotheadargs( a + b + c + d + g(x)) ClearAll(x,a,b,c,d) # delete definitions from the previous example a = 1 Definition(a) a := x Definition(a) # This overwrites the previous definition f(x_) := x^2 f(x_, y_) := x + y Definition(f) Definition(f) ClearAll(f,a) Timing((Range(10^6), Null )) # time a single expression Time(True) # Enable timing all evaluation. Returns the previous value Range(10^6); Time(False); # disable timing all evaluation. Trace(True); # Trace evaluation (a+b)*(a+b) Trace(False); ? LeafCount LeafCount(Expand((a+b)^3)) ByteCount(Expand((a+b)^3)) ? Depth Depth(Expand((a+b)^3)) FullForm(Expand((a+b)^3)) # Examine the tree Expand((a+b)^3)[2,2,1] # One of the deepest parts 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) ClearAll(f,g,expr) # Integrate(f(y), y) # The integral can no longer be reduced MatchQ(z,_) # Blank matchs z Map(MatchQ(_), [1,"string", a+b, 1/3]) # MatchQ does Currying with the first argument FullForm(_Integer) # underscore is shorthand for Blank MatchQ(1, _Integer) myintq = MatchQ(_Integer); 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, _^_) Map(MatchQ(f(x_^2)), [f(b^2), f(b^3), g(b^2)]) Map(MatchQ(_gg), [gg(x+y), gg(x), g(x)]) m = MatchQ(_Integer`Positive`) Map(m, [1,100, 0, -1, 1.0, x]) m = MatchQ(Condition([x_, y_], x < y)) Map(m, [[2, 1], [1, 2], [1,2,3], 1]) m = MatchQ(_Integer | _String) Map(m, [1, "zebra", 1.0]) MatchQ([a,a,a,b], [Repeated(a),b]) MatchQ([b], [RepeatedNull(a),b]) ClearAll(m) f([x_, y_]) => p(x + y) expr = f([x + y, y]) + f(c) + g([a, b]); ReplaceAll(expr, f([x_, y_]) => p(x + y)) ReplaceAll([a/b, 1/b^2, 2/b^2] , b^n_ => d(n)) ClearAll(expr) 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 ReplaceAll( [a, b, c, d, a, b, b, b], a | b => x) [1,2,Sequence(a,b)] 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. h(a | b) := p [h(a), h(b), h(c), h(d)] Replace(1 + a + f(a) + g(f(a)), a => b, 2) Replace(1 + a + f(a) + g(f(a)), a => b, [2]) == 1 + a + f(b) + g(f(a)) ReplaceAll([x, x, x, x, x], x => RandomReal() ) ReplaceAll([x, x, x, x, x], RuleDelayed(x ,RandomReal())) # FIXME: This example is broken. Replace([1, 7, "Hi", 3, Indeterminate], Except(_`Numeric`) => 0, 1) ReplaceRepeated(x^2 + y^6 , List(x => 2 + a, a => 3)) ReplaceAll( b^c, a::(_^_) => g(a)) f(x_, y_:3) := x + y [f(a+b,z), f(a+b)] ClearAll(f) f(x_) := Condition(x^2, x > 3) [f(4),f(3)] ReplaceAll(z * y + b , x_ + y_ => x * y) ReplaceAll(z * y + b + c, x_ + y_ => x * y) ClearAll(f,h,a,b,x,y) mylog = J(log ) mylog(2, 2) f = J((x,y) -> x^2 + y^2) f(3.0,4.0) [f(3,4), f(3, 1/2)] (a = Range(0.0,100.,.01), ccossq = J(x -> cos(x)^2), cossq(x_) := cos(x)^2); Time(True); Map(cossq, a); Map(cossq, a); Map(ccossq, a); Map(ccossq, a); (Time(False), ClearAll(f,a,ccossq,cossq,mylog)) expr = Integrate( x^2 * Exp(x)* Cos(x), x) expr = Collect(expr, Exp(x)) cexpr = Compile([x], Evaluate(expr)) a = Range(0.0, 10.0, 1.0) Map(cexpr, a) a = Range(0.0,10.0,.01); Timing((Map(cexpr, a), Null)) # ClearAll(a,expr,cexpr) VersionInfo() InputForm(Now())