# rosettacode.txt -- programming language code comparisons # 13 Jun 2014 Michael Somos # 08 Aug 2015 Michael Somos # Mathematica, Magma, Maple, Sage, Perl, PARI/GP, R # Magma "magmas/text149.htm" # an imperative, call by value, statically scoped, dynamically typed # programming language, with an essentially functional subset. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o variable name alphabet (Mma) AZaz1$ (* many special characters allows *) (PARI) AZaz1_ /* complete list */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o void value (Mma) Null (Maple) NULL (Sage) pass # only a command not value (Sage) None (Perl) undef (PARI) if(0,) /* or if(1,), while(0,), unti(1,) */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o comment (Mma) (* x *) (Magma) /* x */ (Magma) // x (Maple) # x (Sage) # x (Perl) # x (PARI) /* x */ (PARI) \\ x +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o assignment (Mma) := (* Set[] *) (Magma) := (Maple) := (Sage) = (Perl) = (PARI) = (R) <- # also = sometimes (unclear) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o statement delimiter or terminator (Mma) ; (* as term value is Null *) (Magma) ; /* value is printed */ (Maple) ; # value is printed (Maple) : # value is not printed (Sage) ; # if term a line all not printed (Perl) ; (PARI) ; /* if term a line last not printed */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o previous output value (Mma) % (* Out[] *) (Maple) % (Sage) _ (PARI) % +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o all names currently in use (Mma) Names[] (* all contexts *) (PARI) variables() (* all user variables (ver >= 2.8) *) (R) ls() +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o temporary identifiers (Mma) With[{i=v}, expr] (Mma) With[{i1=v1,i2=v2}, expr] (Magma) epxr where i is v (Magma) expr where i1 is v1 where i2 is v2 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o numeric relational operators (Mma) == != <= >= < > (* Equal[], Unequal[], LessEqual[],... *) (Magma) eq ne le ge lt gt (Maple) = <> <= >= < > (Sage) == != <= >= < > (PARI) == != <= >= < > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o string relational operators (Perl) eq ne le ge lt gt (Magma) eq ne le ge lt gt in notin +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o strict equality (Mma) === (* SameQ[]: is identical to *) (PARI) === (* must have same type *) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Boolean operators (Mma) ! && || Xor (Mma) Not And Or Xor Implies Nor Nand (Magma) not and or xor (Maple) not and or xor implies (Sage) not (Perl) ! && || (Perl) not and or # different precedence (PARI) ! && || (R) && || +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Bitwise boolean functions (PARI) bitneg(x) bitand(x,y) bitor(x,y) bitxor(x,y) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Bit extraction (PARI) bittest(x,n) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Boolean values (Mma) True False (Magma) true false (Maple) true false FAIL (Sage) True False # nonzero zero (PARI) /* nonzero zero */ (R) TRUE FALSE +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Convert Boolean to 0,1 (Mma) Boole[b] (Magma) b select 1 else 0 (Maple) `if`(b,1,0) (PARI) if(b,1,0) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Convert nonzero,zero to boolean true,false (Mma) 0!=x (Maple) not(0=x) (Sage) 0!=x (Sage) bool(x) (PARI) 0!=x (PARI) !!x +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o arithmetic operators (Mma) + - * / ^ (Magma) + - * / ^ (Maple) + - * / ^ (Sage) + - * / ^ (PARI) + - * / ^ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o type of an object (Mma) Head[x] (Mma) x[[0]] (Maple) op(0, x) (Magma) Type(x) (Sage) type(x) (PARI) type(x) (R) typeof(x) # type of elements only (R) x[0] # type of elements only +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o parent of a set (Magma) Parent(s) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o complex unit (Mma) I (Maple) I (Sage) I (PARI) I (R) 1I +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o complex number (R) 1+2i +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o infinity (Mma) Infinity (Maple) infinity (PARI) oo /* new in 2.8.0 */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o power series O(x^n) (Mma) O[x]^n (Maple) O(x^n) (Sage) O(x^n) (Pari) O(x^n) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o create a named variable (Sage) var('a') # variable named 'a' initialized +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Remove identifier assignment (Mma) x=. (Mma) Unset[x] (* same as x=. *) (Mma) Clear[x] (* remove values and definitions *) (Magma) delete x (Sage) restore('x') (Perl) undef(x) (PARI) kill(x) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Single conditional expression (Mma) If[bool, etrue, efalse] (Mma) If[bool, etrue, efalse, eneither] (Magma) bool select etrue else efalse (Maple) `if`(bool, etrue, efalse) # FAIL is false (Sage) bool if etrue else efalse (PARI) if(bool, etrue, efalse) (C) bool ? etrue : efalse +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Single conditional statement (Magma) if bool then etrue; else efalse; end if (Maple) if bool then etrue else efalse end if (Sage) if bool: ...etrue else: ...efalse (C) if (bool) etrue else efalse +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Multi conditional expression (Mma) Which[b1, etrue1, b2, etrue2, True, efalse] (PARI) if(b1, etrue1, b2, etrue2, efalse) \\ newer versions +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Multi conditional statement (Magma) if b1 then etrue1; elif b2 then etrue2; else efalse; end if (Maple) if b1 then etrue1 elif b2 then etrue2 else efalse end if (Sage) if b1: ...etrue1 elif b2: ...etrue2 else: ...efalse +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o string literals (Mma) "abc123" (* type: 'String' *) (Magma) "abc123" /* \",\n,\t */ (Maple) "abc123" # type: 'string' (Sage) "abc123" # converted to 'abc123' (Sage) 'abc123' # type: 'str' preferred (Perl) "abc123" # interpolate variables and \n\t (Perl) 'abc123' # just plain string (PARI) "abc123" /* type: 't_STR' \",\n,\t */ (R) "abc123" # type: "character" (R) 'abc123' # converted to "abc123" +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o vector literals (Mma) {3,1,4,1} (* type: 'List' *) (Magma) [3,1,4,1] /* type: 'SeqEnum' */ (Maple) [3,1,4,1] # type: 'list' (Sage) [3,1,4,1] # type: 'list' (PARI) [3,1,4,1] /* type: 't_VEC' */ (R) c(3,1,4,1) # type: "double" +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o set literals (Magma) {3,1,1,9,4} /* type: 'SetEnum' */ (Maple) {3,1,1,9,4} # type: 'set' (Sage) {3,1,1,9,4} # type: 'dict' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o multiset literals (Magma) {*3,1,1,9,4*} /* type: 'SetMulti' */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o hash or dictionary literals (Mma) <|k1->v1,k2->v2,...|> (* type: 'Association' in V10 *) (Perl) ('k1',v1,'k2',v2,...) # type: 'hash' (Perl) (k1=>v1,k2=>v2,...) (PARI) Map(k1,v1;k2,v2,...) /* type: 't_LIST' in V2.8 */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o sequence of values spliced into vector (Mma) {a1,Sequence[a2,a3],a4} (Maple) (a1,(a2,a3),a4) (Perl) (a1,(a2,a3),a4) (PARI) concat([a1,[a2,a3],a4]) (R) c((a1,c(a2,a3),a4) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o general lists (Magma) [* 1,"a",[3],true *] /* type: 'List' */ (Maple) [1,'a',[3],true,"b"] (Perl) (1,'a',(3),"b") # = (1,"a",3,"b") (PARI) [1,"a",[3]] /* type: 't_VEC' */ (PARI) List([1,"a",[3]]) /* type: 't_LIST' */ (R) list(u=1,"a",v=1i) # optional names u,v +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o make a list into a set (Mma) Union[list] (Magma) Set(list) (Maple) convert(list,set) (Sage) set(list) (PARI) Set(list) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o make a list into a multiset (Magma) Multiset(list) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o sort a list (Mma) Sort[{1,"a",{3}}] (PARI) vecsort([1,2,[3]]) /* several options */ (PARI) L=List([1,2,[3]]);listsort(L) /* sort in place */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o make a vector into an n by m matrix (R) matrix(v, n, m) # nXm matrix by columns (R) matrix(v, n, m, byrow=TRU E) # nXm matrix by rows +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o index a single element from a vector or list (Mma) v[[n]] (* v = {v[[1]],v[[2]],...} , n<=0 allowed *) (Magma) v[n] /* v = [v[1],v[2],...] , n>0 required */ (Maple) v[n] # v = [v[1],v[2],...] , n<0 allowed (Sage) v[n] # v = [v[0],v[1],...] , n<0 allowed (Perl) $v[n] # @v = ($v[0],$v[1],...) , n<0 allowed (PARI) v[n] /* v = [v[1],v[2],...] , n>0 required */ (C) v[n] /* v = [v[0],v[1],...] , no checking */ (R) v[n] # v = [v[1],v[2],...] , vector only +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o index a single element from a list (R) v[[n]] # v = list(v[[1]],v[[2]],...) (R) v$a # v = list(...,a=v$a,...) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o index a single element from a list of lists (Mma) v[[n,m]] (Mma) v[[n]][[m]] (* alternate *) (Magma) v[n,m] (Magma) v[n][m] /* alternate */ (Maple) v[n,m] (Maple) v[n][m] # alternate (Sage) v[n][m] (PARI) A[n,m] /* array or matrix only is special type */ (PARI) v[n][m] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Index multiple elements (slice) from a list (Mma) v[[m;;n]] (* {v[m],v[2],...,v[n]} *) (Mma) v[[m;;n;;d]] (* {v[m],v[m+d],...,v[n]} *) (Sage) v[:n] # (v[0],v[1],..,v[n-1] ) (Perl) @v[m..n] # ($v[m],$v[2],...,$v[n]) *) (PARI) v[m..n] /* [v[m],v[m+1],...,v[n]] /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o general index into a string (Mma) StringTake[s,{n}] (* s[n] if s == s[1]<>s[2]<>... *) (Magma) s[n] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o general index into an expression (Mma) e[[n]] (* n=0 and n<0 allowed *) (Maple) op(n, e) # n=0 and n<0 allowed +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Make range with optional step (Mma) Range[n] (* {1,2,...,n} :List *) (Mma) Range[m,n] (* {m,m+1,...,n} :List *) (Mma) Range[m,n,d] (* {m,m+d,...,n} :List *) (Maple) [m..n] # [m,m+1,...,n] :list (Sage) range(n) # [0,1,...,n-1] :list (Sage) range(m,n) # [m,m+1,...,n-1] :list (Sage) range(m,n,d) # [m,m+d,...,n-1] :list (Perl) (m..n) # (m,m+1,...,n) (PARI) [m..n] /* [m,m+1,...,n] :VEC */ (R) seq(n) # c(1,2,...n) (R) seq(m,n) # c(m,m+1,...,n) (R) seq(m,n,d) # c(m,m+d,...,n) (R) m:n # c(m,m+1,...,n) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Make a lazy evaluation range with optional step (Magma) [m..n] /* [m,m+1,...,n] */ (Magma) [m..n by d] /* [m,m+d,...,n] */ (Maple) m..n # m,m+1,...,n :.. (Sage) xrange(n) # [0,1,...,n-1] :xrange (Sage) xrange(m,n) # [m,m+1,...,n-1] :xrange (Sage) xrange(m,n,d) # [m,m+d,...,n-1] :xrange +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Perform an operation a fixed number of times (Mma) Do[f, {n}] (* f;f;...;f *) (Mma) Do[f, {x,n}] (* f;f;...;f *) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Perform an operation indexed up to a limit (Mma) Do[f[x], {x,n}] (* f[1];f[2];...;f[n] *) (Maple) for x to n do f(x) end do; # f(1);f(2);...;f(n) (Sage) for x in [1..n] : f(x) # f(1);f(2);...;f(n) (PARI) for(x=1,n,f(x)) /* f(1);f(2);...;f(n) */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Perform an operation indexed from a range (Mma) Do[f[x], {x,m,n}] (* f[m];f[m+1];...;f[n] *) (Maple) for x from m to n do f(x) end do; # f(m);f(m+1);...;f(n) (PARI) for(x=m,n,f(x)) /* f(m);f(m+1);...;f(n) */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Perform an operation indexed by a list (Mma) Do[f[x], {x,{a,b,...}] (* f[a];f[b];... *) (Mma) Scan[f, {a,b,...}] (* f[a];f[b];... *) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Perform an operation indexed from a range with step (Mma) Do[f[x], {x,m,n,d}] (* f[m];f[m+d];...;f[n] *) (Maple) for x from m to n by d do f(x) end do; # f(m);f(m+d);...;f(n) (PARI) forstep(x=m,n,d,f(x)) /* f(m);f(m+d);...;f(n) */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o 1D Table of an operation with an index (Mma) Table[f[x], {x,n}] (* {f[1],f[2],...,f[n]} *) (Mma) Table[f[x], {x,m,n}] (* {f[m],f[m+1],...,f[n]} *) (Mma) Table[f[x], {x,m,n,d}] (* {f[m],f[m+d],...,f[n]} *) (Sage) [f(x) for x in range(n)] # [f(0,f(1),...,f(n-1)] (Sage) [f(x) for x in range(m,n)] # [f(m,f(m+1),...,f(n-1)] (Sage) [f(x) for x in range(m,n,d)] # [f(m,f(m+d),...,f(n-1)] (PARI) vector(n,x,f(x)) /* [f(1),f(2),...,f(n)] */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o 1D Table of an operation indexed by a list (Mma) Table[f[x], {x,{a,b,...}] (* {f[a],f[b],...} *) (Mma) Map[f, {a,b,...}] (* {f[a],f[b],...} *) (Maple) [seq(f(x), x = [a,b,...])] # [f(a),f(b),...] (Perl) map(f, (a,b,...)) # (f(a),f(b),...) (PARI) [f(x) | x <- [a,b,...]] /* [f(a),f(b),...] */ (PARI) apply(f, [a,b,...]) /* [f(a),f(b),...] */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o 2D Table of an operation with two indices (Mma) Table[f[x,y], {x,n}, {y,m}] (Sage) matrix(n,m, +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Call a unary function (Mma) f[n] (Mma) f@n (Magma) f(n) (Maple) f(n) (Sage) f(n) (PARI) f(n) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Call a binary function (Mma) f[n,m] (Mma) f@Sequence[n,m] (Mma) f@@{n,m} (Magma) f(n,m) (Maple) f(n,m) (Sage) f(n,m) (PARI) f(n,m) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Make anonymous unary function from expression. (Mma) f(#)& (Mma) Function[n, f[n]] (Magma) func< n | f(n)> (Maple) n -> f(n) (Maple) proc(n) f(n) end (Sage) lambda n: f(n) (PARI) n -> f(n) (R) function(n) {f(n)} +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Make anonymous binary function from expression (Mma) f[#1,#2]& (Mma) Function[{n,m}, f[n,m]] (Magma) func< n,m | f(n,m)> (Maple) (n,m) -> f(n,m) (Maple) proc(n,m) f(n,m) end (PARI) (n,m) -> f(n,m) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o create context with lexical variables (Mma) Module[{x,y}, (Perl) my(x,y); (PARI) my(x,y); +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o create context with local variables (Mma) Block[{x,y}, (Perl) local(x,y); (PARI) local(x,y); +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Define a unary function returning a value with optional default value (Mma) a[n_:d] := f[n] (Magma) function a(n) return f(n); end function (Sage) def a(n) : return f(n) (Perl) sub a { my $n = shift; return f(n); } (PARI) a(n=d) = f(n) (PARI) a = n -> f(n) /* alternate using anonymous */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Define a unary postfix function (PARI) n.a = f(n) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Define a unary procedure or subroutine no return value (Mma) a[n_] := (f[n];) (Mma) a[n_] := (f[n];Null) (Magma) procedure a(n) f(n); end procedure (Sage) def a(n) : f(n) (Perl) sub a { my $n = shift; f(n); } (PARI) a(n) = f(n); if(0,) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Define a selfrecursive function (Mma) a[n_] := If[n<1,1,n a[n-1]] (Magma) function a(n) return n lt 1 select 1 else n*$$(n-1); end function; (PARI) a(n) = if(n<1,1,n*a(n-1)) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o define two mutual recurvise functions (Mma) f[n_]:=g[n]; g[n_]:=f[n] (Magma) forward f,g; f := func; g := func (PARI) f(n)=g(n); g(n)=f(n); +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o local variables in function lexical (Mma) a[n_] := Module[{v=n}, v+1] (Magma) a := function(n); local v; v:=n; return v+1; end function; (Perl) sub a { my $v=shift; $v+1} (PARI) a(n) = my(v=n); v+1 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o function with call by reference argument (Mamga) a := procedure(~n) n : n+1; end procedure; a(~x) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o store function name in a variable for alias purpose (Mma) F := Fibonacci (Magma) s := system (Maple) s := system (PARI) s = system +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o store addition name in a variable for alias purpose (Mma) A = Plus (Magma) A := '+' (Maple) A := `+` +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o make function memoized (Mma) a[n_] := a[n] = f[n] (* this is an idiom *) (Maple) a := proc(n) option remember; f(n) end # op(4,eval(a)) remember table (Sage) @CachedFunction def a(n): return f(n) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Apply a unary operation to all members of a vector and collect results (Mma) g /@ v (Mma) g(#)& /@ v (Mma) Map[g, v] (Mma) Map[g(#)&, v] (Mma) Table[g[x], {x,v}] (Magma) [g(x) : x in v] (Sage) [g(x) for x in v] (PARI) apply(g, Vec(v)) (PARI) [g(x) | x <- v] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Make a selected sublist with unary operation applied (Mma) g(#)& /@ Select[v, f(#)&] (Mma) Map[g, Select[v, f]] (Mamga) [g(x) : x in v | f(x)] (Sage) [g(x) for x in v if f(x)] (PARI) apply(g, select(f, Vec(v))) (PARI) [g(x) | x <- v, f(x)] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Make a selected sublist (Mma) Select[v, f(#)&] (Mma) Select[v, f] (Magma) [x : x in v | f(x)] (PARI) select(f, Vec(v)) (PARI) [x | x <- v, f(x)] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Number of elements of a list (Mma) Length[v] # also expression (Magma) #v (Maple) nops(v) # also expression (Sage) len(v) # also expression (PARI) #v (PARI) length(v) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o length of string in characters (Mma) StringLength["abc"] (* 3 *) (Magma) #"abc" /* 3 */ (Maple) length('abc') # 3 (Sage) len('abc') # 3 (Perl) length('abc') # 3 (PARI) length("abc") /* 3 */ (PARI) #"abc" /* 3 */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Sum of elements of a list (Mma) Total[v] (Magma) &+ v (Sage) sum(v) (PARI) vecsum(v) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Concatenate two lists (Mma) Join[v1,v2] (Magma) v1 cat v2 (Sage) v1+v2 (PARI) concat(v1,v2) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o reverse a list (Mma) Reverse[v] (* also expression *) (Perl) reverse(v) (PARI) Vecrev(v) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o append one element to list (Mma) AppendTo[v,e] (Perl) push(v,e) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o prepend one element to list (Mma) PrependTo[v,e] (Perl) unshift(v,e) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Concatenate two strings (MMa) s1 <> s2 (Magma) s1 cat s2 (Magma) s1 * s2 (Maple) s1 || s2 (Sage) join(s1,s2) (Sage) s1 + s2 (Perl) s1 . s2 (PARI) Str(s1,s2) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o concatenate string a number of times (Magma) str ^ n (Perl) str x n +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o append a string to string variable (Magma) s1 cat:= s2 (Magma) s1 *:= s2 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Concatenate multiple lists (Mma) Join[v1,v2,...] (PARI) concat([v1,v2,...]) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Concatenate multiple strings (Mma) StringJoin[s1,s2,...] (Magma) s1 cat s2 cat ... (Magma) &cat [s1,s2,...] (Sage) join([s1,s2,...],'') # with optional sep (Perl) join '',s1,s2,... # with given sep (PARI) Str(s1,s2,...) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o convert one char code into one char string (Magma) CodeToString(c1) (Perl) chr $c1 (PARI) Strchr([c1]) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o convert list of char codes into string (Perl) join('',map(chr,(c1,c2,...,cn))) (PARI) Strchr([c1,c2,...,cn]) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o convert first char string into char code (Magma) StringToCode("012") /* 48 */ (Perl) ord '012' # 48 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o convert string into list of one char strings (Magma) [ s[i] : i in [1..#s ] (Perl) split('','abc') # ('a','b','c') (PARI) Vec("abc") /* ["a","b","c"] */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o convert string into list of char codes (PARI) Vecsmall("012") /* Vecsmall([48,49,50]) */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o convert expression to string (Mma) ToString[x] (Magma) IntegerToString(x) (PARI) Str(x) (R) as.character(x) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Union of two sets (Mma) Union[v1,v2] (Magma) v1 join v2 (PARI) setunion(v1,v2) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Evaluate a string as expression (Mma) ToExpression[s] (Magma) eval(s) (Sage) eval(s) (Perl) eval(s) (PARI) eval(s) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Convert a symbol to a string (Mma) SymbolName(s) (Maple) convert(s,string) (PARI) Str(s) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Convert a string to a symbol (Mma) Symbol(s) (Maple) convert(s,symbol) (PARI) Str("'",s) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Read and execute a script file (Mma) Get["filename.m"] (Magma) load "filename" (Maple) read "filename" (Sage) load("filename.sage") (PARI) read("filename") +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Save a session (Mma) Save["filename",symbol] (Mma) Save["filename","form"] (Mma) Save["filename",{obj1,obj2,...}] (Magma) save "filename" /* workspace saved */ (Sage) save_session('filename') (PARI) writebin("filename") (PARI) writebin("filename",expr) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o Restore a session (Magma) restore "name" /* workspace restored */ (Sage) load_session('name') (PARI) read("name") +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o interactive input an expression with optional prompt (Mma) Input["prompt"] (Magma) read ident, "prompt" /* ident = input string */ (Magma) readi ident, "prompt" /* ident = input integer */ (Maple) readstat('prompt') # read statement (Sage) input("prompt") (Perl) <> # input line (PARI) input() /* no prompt */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o run a system command (Mma) Run["cmd"] (Mma) !cmd (* "!" 1st char on line *) (Magma) System("cmd") (Maple) system('cmd') (Perl) system('cmd') (PARI) system("cmd") +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o run a system command and return output value (Sage) system('cmd') # returns output as string (Sage) !cmd # "!" first nonwhitespace on line (Sage) !!cmd # returns list of lines (PARI) externstr("cmd") /* returns list of lines */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ o issue an error message and stop (Mma) Abort[] (* no message *) (Magma) error expr1,...,exprn (Maple) error 'msg' # optional parameters if reqd in msg (PARI) error("message") /* like Str() */ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ (Magma) {3, 3, 1, 1, 2} /* SetEnum = {@ 1, 2, 3 @} */ [3, 3, 1, 1, 2] /* SeqEnum */ <3, 3, 1, "", 2> /* Tup */ {@3, 3, 1, 1, 2@} /* SetIndx = {@ 3, 1, 2 @} */ {*3, 3, 1, 1, 2*} /* SetMulti = {* 1^^2, 2, 3^^2 *} */ [1..7 by 2] /* range with step */ {! x in F | P(x) !} /* formal set constructor */ {} /* null set with no universe */ { U | } /* null set with universe U */ A set in Magma is a (usually unordered) collection of objects belonging to some common structure (called the universe of the set). There are three basic types of sets: enumerated sets, whose elements are all stored explicitly (with one exception, see below); formal sets, whose elements are stored implicitly by means of a predicate that allows for testing membership; and indexed sets, which are restricted enumerated sets having a numbering on elements. In particular, enumerated and indexed sets are always finite, and formal sets are allowed to be infinite. Integers() == "Integer Ring" Type(Integers()) == "RngInt" Type(1) == "RngIntElt" Type(Type(1)) == "Cat" a: = func < n | n*n >; function a(n) return n*n; end function; Basis( ModularForms( Gamma0(1), 12), 10) ; qEigenform( EllipticCurve( [0, -1, 0, 1, 0]), 191); A := Basis( ModularForms( Gamma0(32), 2), 341); A[2] - 2*A[6]; Basis( ModularForms( Gamma0(1), 1), 18); [ Dimension(ModularForms(Gamma0(4*n), 1/2)) : n in [1..100] ]; function a(n) return Dimension(ModularForms(Gamma0(4*n),1/2)); end function; a := func < n | n*n >; a := func < n | n lt 0 select 0 else Coefficient( Basis( ModularForms( Gamma0(5), 2), n+1)[1], n) >; [ a(n) : n in {-1..10} ]; (Sage) type(1) == "" type(type(1)) == ""