Computer Algebra II: Pari/GP, Magma, GAP, Singular

a side-by-side reference sheet

pari/gpmagmagapsingular
version used

2.72.214.74.0
show version

$gp --version$ gap -h$singular -vb grammar and invocation pari/gpmagmagapsingular interpreter$ cat hello.gp
print("Hello, World!")
quit

$gp -q hello.gp Hello, World!$ singular -b foo.sing
repl

$gponline calculator$ gap$singular block delimiters { } braces cannot be nested function( ) end if then elif then else fi while do od for do od { } statement separatornewline or ; Newlines don't separate statements inside braces. A semicolon suppresses echoing value of previous expression. ; A line can be broken anywhere, even inside a numeric literal or string, if the newline is preceded by a backslash: \ ; Two trailing semicolons ;; suppress echoing value of previous expression. ; end-of-line comment 1 + 1 \\ addition1 + 1; // addition1 + 1; # addition// comment multiple line comment 1 + /* addition */ 11 + /* addition */ 1;none/* comment line another comment */ variables and expressions pari/gpmagmagapsingular assignmentx = 3.14a := 3;a := 3;int a; a = 3; // It is an error to assign to undeclared variable. parallel assignment[a, b] = [3, 4]noneint a, b; a, b = 3, 4; compound assignment+= -= *= /= \= \/= %= \\ bit operations: <<= >>= nonenone increment and decrementpostmodifiers: x++ x-- nonex++ x-- non-referential identifierany unassigned identifier is non-referentialnonenonenone identifier as valuex = 3 y = 'x global variablevariables are global by default local variabletmp = 19 add(x, y, z) = { \\ don't overwrite global tmp: my(tmp = x + y); tmp + z } \\ local keyword declares dynamic scope null none null test none undefined variable access treated as an unknown numbererror remove variable bindingkill(x)delete x; conditional expression if(x > 0, x, -x)if x lt 0 then -x; else x; end if; arithmetic and logic pari/gpmagmagapsingular true and false 1 0true falsetrue false1 0 falsehoods 0 0.0 Mod(0, 5) Pol() [0, 0, 0] [0, 0; 0, 0] [[0, 0], 0] falsefalse0 logical operators&& || !not true or (true and false);not true or (true and false)! 1 || (1 && 0) relational operators== != > < >= <=eq ne lt gt le ge eq raises an error when the operands are of different type. cmpeq does not. = <> < > <= >=== != < > <= >= <> is a synonym for != arithmetic operators+ - * / %+ - * / mod+ - * / mod the operators + - * / are overloaded for integers, rationals, and floats; other arithmetic functions aren't and there are no implicit conversions; use constructors to convert: Rat(3.1) Float(3) Float(31/10) Operators are for integers only unless base ring with coefficient field is declared. + - * / % mod is synonym for % integer division a \ b divrem(a, b) \\ rounded integer division: a \/ b a := 7; b := 3; div(a, b); QuoInt(a, b);int a, b = 7, 3; a div b; // a / b also performs integer division when // no base ring is declared. integer division by zeroerrorUser errorerrorerror float division7 / 3depending upon the types of a and b, the value can be an exact rational, a machine float, or an arbitrary precision float: a / b ring r = real,(x,y,z),(dp); 3.1 / 7.2; float division by zeroerrorRuntime errorerrorerror power2 ^ 322 ^ 32;2 ^ 322 ^ 16 2 ** 16 sqrt sqrt(2)Sqrt(2);2.0 ^ 0.5none sqrt -1 1.000 * I-1.0 ^ 0.5 evaluates to -1.none transcendental functionsexp log none sin cos tan asin acos atan none Exp Log Sin Cos Tan Arcsin Arccos Arctan Arctan2 arguments must be floats; no implicit conversion of integers to floats: Exp Log Sin Cos Tan Asin Acos Atan Atan2(y, x) none transcendental constants π and Euler's number Pi exp(1)FLOAT.PI FLOAT.ELIB "general.lib"; // print specified number of digits: number_pi(100); number_e(100); float truncation round towards zero, round to nearest integer, round down, round up truncate(x) round(x) floor(x) ceil(x) Trunc Round Floor Ceil absolute value and signum abs(x) sign(x) AbsInt no absolute value for floats? SignInt SignFloat LIB "general.lib"; absValue(-7); ring r = real,(x,y,z),(dp); absValue(-7.1); integer overflow none, has arbitrary length integer typenone, has arbitrary length integer typenone, has arbitrary length integer typemodular arithmetic with warning float overflow error# prints as inf: FLOAT.INFINTY rational construction2 / 72 / 7;2 / 7 rational decomposition x = 2 / 7 numerator(x) denominator(x) Numerator(2 / 7); Denominator(2 / 7 ); x := 2 / 7; NumeratorRat(x); DenominatorRat(x); decimal approximation2 / 7 + 0. \\ change precision to 100: \p 100 2 / 7 + 0. complex construction 1 + 3 * Inone complex decomposition real and imaginary part, argument and modulus, conjugate real(z) imag(z) arg(z) abs(z) conj(z) none random number uniform integer, uniform float random(100) random(1.0) Random(0, 99); ?? rs := RandomSource(IsMersenneTwister); Random(rs, 0, 99); ?? random seed set, get setrand(17) getrand() SetSeed(42); seed := GetSeed((); rs := RandomSource(IsMersenneTwister, 17); State(rs); bit operators\\ left shift: 5 << 1 \\ right shift: 5 >> 1 none binary, octal, and hex literalsnone radix\\ 42 as powers of 7 up to 9th power: 42 + O(7^10) IntegerToString(42, 7);none to array of digits\\ base 10: digits(1234) \\ base 2: digits(1234, 2) \\ number of digits in base 10: sizedigits(1234) ListOfDigits(1234); # other bases? strings pari/gpmagmagapsingular string literal "don't say \"no\"""don't say \"no\"""don't say \"no\""string s = "don't say \"no\""; newline in literal no; use \n escapeA line break in a string literal results in a newline in the string unless preceded by a backslash.noYes; runaway strings are possible; there is a predefined string variable newline which contains a single newline character. literal escapes\n \t \" \\\" \\ \n \r \t\b \c \n \r \" \' \\ \ooo when writing to a buffered output stream, encountering a \c causes a flush of output. \" \\ concatenate Str("one ", "two ", "three") concat("one ", "two ", "three") "one " cat "two " cat "three"; "one " * "two " * "three"; &cat ["one ", "two ", "three "]; &* ["one ", "two ", "three "]; Concatenation("one ", "two ", "three");string s = "one" + "two" + "three"; replicatehbar := "-" ^ 80; translate caseUppercaseString("foo"); LowercaseString("FOO"); trim none number to string Str(8) \\ implicit conversion to string: concat("value: ", 8) "value: " * IntegerToString(8);Concatenation("value: ", String(8));"value: " + string(8) string to number7 + eval("12") 73.9 + eval(".037") 7 + Int("12"); 73.9 + Float(".037"); string joina := ["foo", "bar", "baz"]; JoinStringsWithSeparator(a, ","); split Split("foo,bar,baz", ",");SplitString("foo,bar,baz", ","); substitute# replace all occurrences: ReplacedString("do re mi mi", "mi", "ma"); length length("hello") #"hello" # "hello";Length("hello");size("hello"); index of substringIndex("hello", "el"); Position("hello", "el"); /* both return 0 if substring not found */ // evaluates to 2: find("hello", "el"); extract substringSubstring("hello", 2, 2);s := "hello"; s{[2..3]}; // start index and substring length: "hello"[2, 2] character literal 'h' character lookups := "hello"; # the character 'h': s; # cannot use index notation on string literal // the character h: "hello" chr and ordStrchr() Vecsmall("A") CharInt(65) IntChar('A') delete characterss := "disemvowel me"; # no retval; modifies s in place: RemoveCharacters(s, "aeiou"); arrays pari/gpmagmagapsingular literal\\ [1, 2, 3] is a vector literal: List([1, 2, 3]) a := [1, 2, 3];[1, 2, 3]; # creates array with gap at fourth index; # reading a causes an error: a := [1, 2, 3, , 5]; list a= 1, 2, 3; list a = list(1, 2, 3); // Singular lists are fixed length. size length(List([1, 2, 3])) #List([1, 2, 3]) # [1, 2, 3];Length([1, 2, 3]);size(a); lookup\\ access time is O(1). \\ indices start at one: List([1, 2, 3]) // indices start at one: [6, 7, 8]; # indices start at one: a := [1, 2, 3]; a; // indices start at one: a; update listput(a, 7, 1)a := 7;a := 7;a = 7; out-of-bounds behaviorout of allowed range errorRuntime error on lookup. For update, size of array is increased if necessary; runtime error to look up unassigned slot in between assigned slots. Lookups result in errors; arrays can have gaps which also cause lookup errors. An update will expand the array, possibly creating gaps. error element indexnone# returns 3: Position([7, 8, 9, 9], 9); # returns [3, 4]: Positions([7, 8, 9, 9], 9); slice none array of integers as indexnone manipulate backa = List([6, 7, 8]) listput(a, 9) elem = listpop(a) a := [6, 7, 8]; // a not modified: a2 := Append(a, 9); // a2 not modified: a3 := Prune(a2); // a modified: Append(~a, 9); Prune(~a); a = [6, 7, 8]; Add(a, 9); elem := Remove(a); list a = list(6, 7, 8); list a2 = insert(a, 9, size(a)); int popme = a2[size(a2)]; list a3 = delete(a2, size(a2)); manipulate fronta = List([6, 7, 8]); listinsert(a, 5, 1); elem = a; listpop(a, 1); head List([1, 2, 3]) tail none consa = List([1, 2, 3]); listinsert(a, 1, 1); concatenate concat(List([1, 2, 3]), List([4, 5, 6]))Concatenation([1, 2, 3], [4, 5, 6]);list a1 = 1, 2, 3; list a2 = 4, 5, 6; list a3 = a1 + a2; replicate copy a2 = a iterate a = List([1, 2, 3]) for(i=1, length(a), print(a[i])) Perform([1, 2, 3], function(x) Print(x); Print("\n"); end); reverse a = List([1, 2, 3]) a2 = listcreate() while(i > 0, listput(a2, a[i]); i—) Reversed([1, 2, 3]) sorta = List([3,1,4,2]) listsort(a) a A := [3, 1, 4, 2] Sort(A); dedupe Set([1, 2, 2, 3])Set([1, 2, 2, 3]); Unique([1, 2, 2, 3]); membership \\ returns 1-based index of first occurrence \\ or 0 if not found: setsearch([1, 2, 3], 2) 2 in [1, 2, 3] intersection setintersect([1, 2], [2, 3, 4])Intersection(Set([1, 2]), Set([2, 3, 4])); union setunion([1, 2], [2, 3, 4])Union(Set([1, 2]), Set([2, 3, 4])); relative complement, symmetric differencesetminus([1, 2, 3], ) ?? mapapply(x -> x * x, [1, 2, 3])A := [1, 2, 3]; # modifies A: Apply(A, x -> x * x); filter select(x -> x > 2, [1, 2, 3]) reduce universal and existential tests min and max elementvecmin([1, 2, 3]) vecmax([1, 2, 3]) Minimum([1, 2, 3]) Maximum([1, 2, 3]) shuffle and sampleShuffle([1, 2, 3, 4]) flatten one level, completely # completely: Flat([1, [2, [3, 4]]]) zip cartesian productCartesian([1, 2, 3], ["a", "b", "c"]) sets pari/gpmagmagapsingular literal\\ sets are vectors whose \\ elements are strictly increasing Set([1, 2, 3]) {1, 2, 3}; size#Set([1, 2, 3])# {1, 2, 3}; add elements := {1, 2, 3}; Inlude(~s, 4); remove elements := {1, 2, 3}; Exclude(~s, 1); membership test\\ returns 1-based index of first occurrence \\ or 0 if not found: setsearch([1, 2, 3], 2) 7 in {6, 7, 8}; disjoint testIsDisjoint({1, 2, 3}, {2, 3, 4}); unionsetunion([1, 2], [2, 3, 4])// {1, 2, 3, 4}: s := {1, 2, 3} join {2, 3, 4}; intersectionsetintersect([1, 2], [2, 3, 4])// {2, 3}: s := {1, 2, 3} meet {2, 3, 4}; relative complementsetminus([1, 2, 3], )// {1}: s := {1, 2, 3} diff {2, 3, 4}; arithmetic sequences pari/gpmagmagapsingular unit difference[1 .. 100] vector(100, i, i) [1 .. 100];[1 .. 100] difference of 10vector(10, i, 10 * i - 9)[1 .. 100 by 10];[1,11 .. 91] difference of 0.1vector(1000 - 9, i, i / 10 + 9 / 10)[0.1 * i: i in [1 .. 1000]];none dictionaries pari/gpmagmagapsingular literal d = Map(["t", 1; "f", 0])// None, just empty constructor: d := AssociativeArray(); d["t"] := 1; d["f"] := 0; # dictionary constructor; type of keys derived # from first arg: d := NewDictionary("", true); AddDictionary(d, "t", 1); AddDictionary(d, "f", 0); # record literal with identifier keys: r := rec(t := 1, f := 0); # record literal with string keys: r2 := rec(("t") := 1, ("f") := 0); size #d# d;# no way to get size of dictionary? Length(RecNames(r)); lookup mapget(d, "t")d["t"];LookupDictionary(d, "t"); # the same key can be looked up with identifier # or string notation: r.t; r.("t"); updatemapput(d, "f", -1)d["f"] := -1;AddDictionary(d, "f", -1); r.f := -1; r2.("f") := -1; missing key behavior mapget raises errorRuntime error# returns special object "fail": LookupDictionary(d, "not_a_key"); # raises an error: r.not_a_key; is key present mapisdefined(d, "t")IsDefined(d, "t");KnowsDictionary(d, "t"); # RecNames returns keys as strings: "t" in RecNames(r); deletemapdelete(d, "t")Remove(~d, "t"); iterate # no way to iterate over dictionary? for i in RecNames(r) do Print(r.(i)); od; keys and values as arraysKeys(d); ?? RecNames(r); functions pari/gpmagmagapsingular define functionadd(x, y) = x + y \\ function body w/ sequence of statements: say(s1, s2, s3) = print(s1); print(s2); print(s3) \\ function body w/ newlines: dire(s1, s2, s3) = { print(s1); print(s2); print(s3); } add := function(a, b) return a + b; end function; // no return value: show := procedure(s) print(s); end procedure; add := function(x, y) return x + y; end; proc add(int x, int y) { return(x + y); } invoke functionadd(3, 7)add(3, 7);add(3, 7);add(3, 7); undefine functionkill(add) redefine functionadd(x, y, z) = x + y + z overload functionnone missing function behavior"not a function" error missing argument behaviorset to zero extra argument behavior"too many parameters" error default argumentmylog(x = 1, base = 10) = log(x) / log(base) \\ log10(3): mylog(3) \\ ln(3): mylog(3, exp(1)) \\ ln(1): mylog(, exp(1)) \\ If neither caller nor function definition \\ provide a value, zero is used. return value anonymous functionf = (x, y) -> x + y f(1, 2) # unary functions only? f := x -> x * x; f2 := function(x, y) return 2 * x + 3 * y; end; variable number of arguments pass array elements as separate arguments execution control pari/gpmagmagapsingular ifif(x > 0, \ print("positive"), \ if(x < 0, \ print("negative"), \ print("zero"))) if n gt 0 then print "positive"; else if n lt 0 then print "negative"; else print "zero"; end if; end if; if x > 0 then Print("positive\n"); elif x < 0 then Print("negative\n"); else Print("zero\n"); fi; int x = -3; if (x > 0) { print("positive"); } else { if (x < 0) { print("negative"); } else { print("zero"); } }; whilei = 0 while(i < 10, print(i); i++) i := 0; while i lt 10 do print i; i := i + 1; end while; i := 0; while i < 10 do Print(i, "\n"); i := i + 1; od; int i = 0; while (i < 10) { print(i); i++; }; forfor(i = 0, 9, print(i))for i := 0 to 9 by 1 do print i; end for; for i in [0..9] do Print(i, "\n"); od; int i; for (i = 0; i < 10; i++) { print(i); }; breakbreakbreakbreak continuenextcontinuecontinue exceptions pari/gpmagmagapsingular raise exceptionerror("failed")error "failed";Error("failed"); handle exceptioniferr(error("failed"), E, \ print(errname(E), ": ", component(E, 1))) try error "failed"; catch e print "caught error"; end try; uncaught exception behaviorError() invokes the GAP debugger. Type Quit; to return to REPL. streams pari/gpmagmagapsingular write line to stdoutprint("hello")print "hello";Print("hello");print("hello"); read file into array of stringsa = readstr("/etc/hosts") processes and environment pari/gpmagmagapsingular environment variablegetenv("HOME") external commandsystem("ls /etc") command substitution\\ array of output lines: lines = externstr("ls /etc") libraries and namespaces pari/gpmagmagapsingular load libraryRead('foo.g');LIB "add.sing"; define library$ cat add.sing
version="1.0"
category="misc"

proc add(int x, int y) {
return(x + y);
}
library pathSearches current directory; additional directories can be added to the search path by adding them separated by colons to the/ SINGULARPATH //environment variable.
reflection
pari/gpmagmagapsingular
list function documentation?
get function documentation? tanhelp killall;
query data typetype(x)Type(x);typeof(x);
list types\t
list variables in scopevariable()listvar();
list built-in functions?*
list metacommands?\
search documentation??? modulus??DirectProduct
vectors
pari/gpmagmagapsingular
vector literal[1, 2, 3]# row vector is same as array:
[1, 2, 3]
// A ring must be declared. The elements of the vector are not
// limited to the coefficient field, but can be any element of
// the ring.

ring r = read, (x), dp;

vector v= [1.5, 3.2, 7.1];
constant vector

all zeros, all ones
vector(100, i, 0)
vector(100, i, 1)
vector coordinate\\ indices start at one:
[1, 2, 3]
vec := [1, 2, 3];
# indices start at one:
v;
v;
vector dimension

length([1, 2, 3])
#[1, 2, 3]
Length([1, 2, 3])nrows(v);
element-wise arithmetic operators+ -+ - * /+ -
vector length mismatch

scalar multiplication3 * [1, 2, 3]
[1, 2, 3] * 3
3 * [1, 2, 3];
[1, 2, 3] * 3;
3 * v;
v * 3;

// Scalar can be any ring element:
(1 + x) * v;
dot product[1, 1, 1] * [2, 2, 2] ~[1, 1, 1] * [2, 2, 2]
cross product
normsvec = [1, 2, 3]

normlp(vec, 1)
normlp(vec, 2)
normlp(vec)
orthonormal basis
matrices
pari/gpmagmagapsingular
literal[1, 2; 3, 4]

\\ from rows:
row1 = [1, 2]
row2 = [3, 4]
matconcat([row1; row2])
[[1, 2], [3, 4]]// A ring must be declared. The elements of the matrix are not
// limited to the coefficient field, but can be any element of
// the ring.

ring r = read, (x), dp;

matrix m = 1, 2, 3, 4;
construct from columnscol1 = [1, 3]~
col2 = [2, 4]~
matconcat([col1, col2])
construct from submatricesA = [1, 2; 3, 4]
B = [4, 3; 2, 1]
\\ 4x4 matrix:
C = matconcat([A, B; B, A])
constant matricesmatrix(3, 3, i, j, 0)
matrix(3, 3, i, j, 1)

\\ 3x3 Hilbert matrix:
matrix(3, 3, i, j, 1 / (i + j - 1))
diagonal matrices
and identity
matdiagonal([1, 2, 3])
matid(3)
DiagonalMat([1, 2, 3])
IdentityMat(3)
dimensions\\ [3, 2]:
matsize([1, 2; 3, 4; 5, 6])
# returns [3, 2]:
DimensionsMat([[1, 2], [3, 4], [5, 6]])
nrows(m);
ncols(m);
element lookup\\ top left corner:
A[1, 1]
A := [[1, 2], [3, 4]];

# top left corner:
A
matrix m = 1, 2, 3, 4;

m;
extract row\\ first row:
[1, 2; 3, 4][1, ]
extract column\\ first column:
[1, 2; 3, 4][, 1]
extract submatrixA = [1, 2, 3; 4, 5, 6; 7, 8, 9]

vecextract(A, "1..2", "1..2")
element-wise operators+ -+ -+ -
productA = [1, 2; 3, 4]
B = [4, 3; 2, 1]
A * B
A := [[1, 2], [3, 4]];
B := [[4, 3], [2, 1]];
A * B;
matrix m = 1, 2, 3, 4;
matrix m2 = 4, 3, 2, 1;

m * m2;
power[1, 2; 3, 4] ^ 3[[1, 2], [3, 4]] ^ 3
exponential
log
kronecker productA := [[1, 2], [3, 4]];
B := [[4, 3], [2, 1]];
KroneckerProduct(A, B);
norms
transposeA~
mattranspose(A)
conjugate transposeconj([1, I; 2, -I] ~)
inverse[1, 2; 3, 4] ^ -1
1 / [1, 2; 3, 4]
Inverse([[1, 2], [3, 4]])
row echelon form
pseudoinverse
determinantmatdet([1, 2; 3, 4])Determinant([[1, 2], [3, 4]])matrix m = 1, 2, 3, 4;

det(m);
tracetrace([1, 2; 3, 4])Trace([[1, 2], [3, 4]])matrix m = 1, 2, 3, 4;

trace(m);
rankmatrank([1, 1; 0, 0])RankMat([[1, 1], [0, 0]])
nullspace basismatker([1, 1; 0, 0])
range basismatimage([1, 1; 0, 0])
eigenvalues[vals, vecs] = mateigen([1, 2; 3, 4], flag=1)
eigenvectorsmateigen([1, 2; 3, 4])
singular value decomposition
qr decompositionmatqr([1, 2; 3, 4])
solve system of equationsA = [1, 2; 3, 4]
matsolve(A, [2, 3]~)
combinatorics
pari/gpmagmagapsingular
factorial10!Factorial(10);Factorial(10);LIB "general.lib";

factorial(10);
binomial coefficientbinomial(10, 3)Binomial(10, 3);Binomial(10, 3);LIB "general.lib";

binomial(10, 3);
multinomial coefficientMultinomial(12, [3, 4, 5]);
integer partitions

and count
partitions(10)

length(partitions(10))
Partitions(10);

NumberOfPartitions(10);
Partitions(10);

NrPartitions(10);
set partitions

and Bell number
stirling(10, 3, 2)
sum(i=1, 10, stirling(10, i, 2))
StirlingSecond(10, 3);

Bell(10);
permutations with k disjoint cyclesabs(stirling(n, k, 1))Abs(StirlingFirst(n, k));
fibonacci number

and lucas number
fibonacci(10)Fibonacci(10);
Lucas(10);
bernoulli numberbernfrac(100)BernoulliNumber(100);
catalan numberCatalan(10);
number theory
pari/gpmagmagapsingular
pseudoprime testispseudoprime(7)IsProbablyPrime(7);IsPrimeInt(7);
true prime test

isprime(7)IsPrime(7);
divisorsdivisors(100)// [1, 2, 4, 5, 10, 20, 25, 50, 100]:
Divisors(100);
DivisorsInt(100);
prime factors\\ [2,2; 3,1; 7,1]:
factor(84)
// [2, 3, 7]:
PrimeDivisors(84);
# [ 2, 2, 3, 7 ]:
FactorsInt(84);
next prime

and preceding
nextprime(1000)
precprime(1000)
NextPrime(1000);
PreviousPrime(1000);
NextPrimeInt(1000);
PrevPrimeInt(1000);
?
prime(1000);
nth prime\\ first 100 primes: primes(100)
primes(100)
NthPrime(100);
prime counting functionprimepi(100)LIB "general.lib";

size(primes(1, 100));
divmod

divrem(7, 3)
greatest common divisor

and relatively prime test
gcd(14, 21)
gcd(gcd(14, 21), 777)
Gcd(14, 21);
Gcd(Gcd(14, 21), 777);
GcdInt(14, 21);
GcdInt(GcdInt(14, 21), 777);
extended euclidean algorithm\\ [2, -1, 1]:
gcdext(3, 5)
ret := Gcdex(3, 5);

# 2:
ret.coeff1;
# -1:
ret.coeff2;
# 1:
ret.gcd;
least common multiplelcm(14, 21)Lcm(14, 21);LcmInt(14, 21);
integer residuesMod(2, 5) + Mod(3, 5)
Mod(2, 5) - Mod(3, 5)
Mod(2, 5) * Mod(3, 5)
Mod(2, 5)^2
r := ZmodnZ(5);
fam := ElementsFamily(FamilyObj(r));;

ZmodnZObj(fam, 2) + ZmodnZObj(fam, 3);
multiplicative inverseMod(2, 7)^-1

\\ raises error:
Mod(2, 4)^-1
r := ZmodnZ(7);
fam := ElementsFamily(FamilyObj(r));;

ZmodnZObj(2, 7)^-1;
chinese remainder theorem\\ Mod(173, 187):
chinese(Mod(3, 17), Mod(8, 11))
# 173:
ChineseRem([17, 11], [3, 8]);
lift integer residue\\ 7:
lift(-17, 12)
\\ -5:
centerlift(-17, 12)
euler totient

eulerphi(256)Phi(256);
multiplicative orderznorder(Mod(7, 108))OrderMod(7, 108);
primitive rootsznprimroot(11)PrimitiveRootMod(11);
discrete logarithmznlog(10, Mod(2, 11))
znlog(Mod(10, 11), Mod(2, 11))
# arg: 10, base: 2, modulus: 11
LogMod(10, 2, 11);
carmichael functionlcm(znstar(561))Lambda(561);
kronecker symbol

and jacobi symbol
kronecker(3, 5)Jacobi(3, 5);
moebius functionmoebius(11)MoebiusMu(11);MoebiusMu(11);
riemann zeta functionzeta(2)
mangoldt lambda
dirichlet character
elliptic curves
pari/gpmagmagapsingular
elliptic curve from coefficients\\ ellinit([a, b, c, d, e]) where
\\
\\   y^2 + axy + by = x^3 + cx^2 + dx + e
\\

e0 = ellinit([0,0,1,-7,6])

\\ ellinit([a, b]) where
\\
\\   y^2 = x^3 + ax + b
\\

e1 = ellinit([-1, 0])
discriminante0.disc
conductorellglobalred(e0)
singularity teste0.disc == 0
convert to minimal modele0 = ellinit([6, -3, 9, -16, -14])
e = ellminimalmodel(e0)
coordinate transformation on pointe0 = ellinit([6, -3, 9, -16, -14])
e = ellminimalmodel(e0, &v)
\\ minimal to original:
ellchangepointinv([0, 0], v)
\\ original to minimal:
ellchangepoint([-2, 2], v)
coordinate transformation on curve: ellchangecurvee0 = ellinit([6, -3, 9, -16, -14])
e = ellminimalmodel(e0, &v)
\\ same as e0:
ellchangecurve(e, v)
point on curve testellisoncurve(e, [0, 2])
abscissa to ordinates\\ vector of size 0, 1, or 2:
ellordinate(e, 0)
group identity
group operationelladd(e, [0, 2], [1, -1])
group inverseellneg(e, [0, 2])
group multiplicationellmul(e, [0, 2], 3)
canonical height of pointellheight(e, [0, -3])
order of point\\ returns 0 for infinite order:
ellorder(e, [0, 2])

ellorder(e1, [0, 0])
torsion subgroupe1 = ellinit([-1, 0])

\\ returns [t, v1, v2]:
\\
\\   t: order of torsion group
\\   v1: orders of component cyclic groups
\\   v2: generators of same cyclic groups
\\

elltors(e1)
analytic rank\\ first value is rank:
[a, b] = ellanalyticrank(e)

\\ recompute second value to higher precision:
\p 100
b = ellL1(e, a)
L-function valueelllseries(e, 1 + I)
L-function coefficients\\ tenth coefficient:
ellak(e, 10)
\\ first ten coefficients:
ellan(e, 10)
rational and algebraic numbers
pari/gpmagmagapsingular
to continued fraction\p 100
contfrac(Pi)
from continued fraction
p-adic number\\ p is 2 and precision in powers of 2 is 100:
1/2 + O(2^100)
lift p-adic to rationallift(1/2 + O(2^100))
gaussian integer normnorm(1 + I)
quadratic extension\\ make w equal to sqrt(D)/4:
D = -4
polynomials
pari/gpmagmagapsingular
from expression with indeterminates(x - 1) * (x - 2)

(1+x)^2 * (2+y)^3
// A ring must be declared before a polynomial can be defined.
// To define a ring, one specifies (1) the coefficient field
// (or ring), (2) the indeterminate variables, and (3) the term
// ordering.

ring r = integer, (x, y), dp;

poly p1 = (x - 1) * (x - 2);
poly p2 = (1 + x)^2 * (2 + y)^3;
from coefficient arrayPol([1, -3, 2])

@@\\@ zero-degree coefficient first:
Polrev([2, -3, 1])
to coefficient arrayVec((x+1)^10)poly p = (1 + x)^10;
coeffs(p, x);
lookup coefficientpolcoeff((x+1)^10, 3)
substitute indeterminate\\ replace x with 3:
subst((x-1)*(x-2), x, 3)
\\ replace x with (x-1):
subst((x-1)*(x-2), x, (x-1))
subst((x - 1) * (x - 2), x, 3);

subst((x - 1) * (x - 2), x, (x - 1));
degreepoldegree((x-1)^10)poly p = (1 + x)^10;
deg(p);
operations+ - * /
division and remainder
expand polynomialPolynomials are displayed in expanded form.
factor polynomialring r = 7, (x), dp;

// Doesn't work in ring with real or int. coefficients:
factorize(x^2 + 3*x + 2);
collect termsring r = integer, (x, y), dp;

coeffs((x + 2*y + 1)^10, x);
factorfactor(x^2-1)
rootspolroots(x^3+3*x^2+2*x-1)
greatest common divisorp1 = x^3 + 2*x^2 -x -2
p2 = x^3 -7*x + 6
gcd(p1, p2)
resultantpolresultant((x-1)*(x-2), (x-3)^2)
discriminantpoldisc((x+1)*(x-2))
homogenity testhomog((x + y)^2);
groebner basisnone
specify orderingnone
symmetric polynomialnone
cyclotomic polynomialpolcyclo(10)
hermite polynomialpolhermite(4)
chebyshev polynomial

first and second kind
polchebyshev(4, 1)
polychebyshev(4, 2)
interpolation polynomialpolinterpolate([1, 2, 3], [2, 4, 7])
characteristic polynomialcharpoly([1, 2; 3, 4])
minimal polynomial
piecewise polynomial
rational function(x - 1) / (x - 2)^2
partial fraction decomposition
special functions
pari/gpmagmagapsingular
gammagamma(1/2)
hyperbolicsinh cosh tanh
elliptic integerals
bessel functionsbesselh1 besselh2 besseli besselj besseljh besselk besseln
Riemann zetazeta(2)
permutations
pari/gpmagmagapsingular
permutation from disjoint cyclesS4 := Sym(4);
p := S4!(1, 2)(3, 4);
p := (1, 2)(3, 4);
permutation from listS4 := Sym(4);
p2 := elt<S4 | 2, 1, 4, 3>;
p2 := PermList([2, 1, 4, 3]);
permutation from two lists# must be positive integers:
p := MappingPermListList([6, 8, 4, 2], [2, 4, 6, 8])
act on element1 ^ p;

# preimage of 1 under p:
1 / p;
act on list
composeS4 := Sym(4);

S4!(1,2)(3,4) * S4!(1,3);
(1, 2)(3, 4) * (1, 3);
invertS3 := Sym(3);

S3!(1,2,3) ^ -1;
Inverse(S3!(1,2,3));
(1, 2, 3) ^ -1;
powerS5 := Sym(5);

S5!(1,2,3,4,5) ^ 3;
(1, 2, 3, 4, 5) ^ 3;
orderS3 := Sym(3);

Order(S3!(1,2,3));
Order((1, 2, 3));
supportMovedPoints((1, 3, 5)(7, 8));
number of inversions
parity
to inversion vector
from inversion vector
all permutations
random permutation
groups
pari/gpmagmagapsingular
named groups

symmetric, alternating, cyclic, dihedral
S4 := Sym(4);
A4 := AlternatingGroup(4);
Z5 := CyclicGroup(5);
D10 := DihedralGroup(10);
S4 := SymmetricGroup(4);
A4 := AlternatingGroup(4);
Z5 := CyclicGroup(5);
# argument is order of group, not vertices
# of polygon:

D10 := DihedralGroup(2 * 10);
group by orderNumberOfSmallGroups(8);

// first group of order 8:
G := SmallGroup(8, 1);
# number of groups of order 8:
Length(AllSmallGroups(8));

# first group of order 8:
G := AllSmallGroups(8);
group from permutation generatorsS5 := Sym(5);
p1 := S5!(1, 3, 5, 2);
p2 := S5!(1, 2);
G := PermutationGroup<5 | p1, p2>;
G := Group((1, 3, 5, 2), (1, 2));

# or
G := GroupWithGenerators([(1, 3, 5, 2), (1, 2)]);
direct productZ3 := CyclicGroup(3);
A4 := AlternatingGroup(4);
G := DirectProduct(z3, a4);
Z3 := CyclicGroup(3);
A4 := AlternatingGroup(4);
G := DirectProduct(Z3, A4);
free product

F := FreeProduct(CyclicGroup(3), CyclicGroup(2));
Z := FreeGroup(1);

# free group with 2 generators:
F := FreeGroup("a", "b");
group from presentationF := FreeGroup( "a", "b" );
G := F / [ F.1^2, F.2^3, (F.1 * F.2)^5 ];
all elementsPerform(Enumerator(Z3), function(e)
Print(e);
Print("\n");
end);
Generators(s10);

// notation for individual generators:
S10.1;
S10.2;
S10 := SymmetricGroup(10);
# return generators in an array:
GeneratorsOfGroup(S10);

# notation for individual generators:
S10.1;
S10.2;
identity elementId(G);Identity(G);
One(G);
random elementRandom(G);Random(G)
group operatione1 := Random(G);
e2 := Random(G);
e1 * e2;
e1 := Random(G);
e2 := Random(G);
e1 * e2;
inverse elementInverse(e1);
// or:
e1 ^ -1;
Inverse(e1);
# or:
e1^-1;
generator word for elementS10 := SymmetricGroup(10);
Factorization(S10, (1,3,8,10,5,9,2,7));
elements by generator word lengthS6 := SymmetricGroup(6);
GrowthFunctionOfGroup(S6);
element orderD10 := DihedralGroup(10);
Order(D10.1);
Order(D10.2);
D10 := DihedralGroup(2 * 10);

Order(D10.1);
Order(D10.2);
identify group// C2*C4:
GroupName(SmallGroup(8, 2));

// <8, 1>:
IdentifyGroup(CyclicGroup(8));
# C4 x C2:
StructureDescription(AllSmallGroups(8));
group to presentationFPGroup(AlternatingGroup(10));
group orderOrder(Sym(4));Size(SymmetricGroup(4));
cyclic testIsCyclic(AlternatingGroup(10));IsCyclic(AlternatingGroup(10));
abelian testIsAbelian(CyclicGroup(10));IsAbelian(CyclicGroup(10));
subgroups
pari/gpmagmagapsingular
all subgroupsSubgroups(Sym(4));AllSubgroups(SymmetricGroup(4));
subgroup latticeSubgroupLattice(Sym(4));S4 := SymmetricGroup(4);
lat := LatticeSubgroups(S4);
DotFileLatticeSubgroups(lat, "lattice.dot");
# dot -Tpng < lattice.dot > lattice.png
maximal subgroupsMaximalSubgroups(Sym(4));MaximalSubgroups(S4);
frattini subgroupD4 := DihedralGroup(4);
FrattiniSubgroup(D4);
FrattiniSubgroup(DihedralGroup(2 * 4));
subgroup from generatorsG := Group((1, 3, 5, 7), (2, 4));
H := Subgroup(G, [(2, 4)]);
normal subgroupsNormalSubgroups(Sym(4));NormalSubgroups(S4);
cosetsRightCoset()
CanonicalRightCosetElement()
CosetDecomposition()
RightTraversal(G, U)
quotient group
centerZ4 := CyclicGroup(4);
D6 := DihedralGroup(6);
G := DirectProduct(Z4, D6);
center := Centre(G);
g := DirectProduct(CyclicGroup(4), DihedralGroup(2 * 6));
Center(g);
centralizerg := SymmetricGroup(5);
h := Centralizer(g, (1, 3)(4, 5));
normalizerS4 := SymmetricGroup(4);
G := Group([(1,2)(3,4)]);
Normalizer(S4, G);
commutator# e1^-1 * e2^-1 * e1 * e2:
Comm(e1, e2);
commutator subgroupG1 := Group((1,2,3),(1,2));
G2 := Group((2,3,4),(3,4));
CommutatorSubgroup(G1, G2);
subgroup test
subgroup indexIndex(Sym(4), AlternatingGroup(4));Index(G, H);
normal testIsNormal(G, H);
subnormal testIsSubnormal(G, H);
nonabelian simple groups# argument is list of orders:
AllSmallNonabelianSimpleGroups([1..10000]);
simple testIsSimple(SymmetricGroup(4));
solvable testIsSolvable(SymmetricGroup(4));
derived seriesDerivedSeriesOfGroup(SymmetricGroup(4));
characteristic testS4 := SymmetricGroup(4);
H := Subgroup(s4, [(1,4)(2,3), (1,3)(2,4), (2,4,3)]);
IsCharacteristicSubgroup(S4, H);
semidirect product
group homomorphisms
pari/gpmagmagapsingular
all homomorphismsS4 := SymmetricGroup(4);
S3 := SymmetricGroup(3);
AllHomomorphisms(S3, S4);
all homomorphims classesAllHomomorphismClasses(S3, S4);
endomorphisms and automorphismsAllEndomorphisms(S4);
AllAutomorphisms(S4);
homomorphism from generator imageshom := GroupHomomorphismByImages(S3, S4,
[(1,2,3), (1,2)],
[(2,3,4), (2,3)]);

# uses generators of S3:
hom := GroupHomomorphismByImages(S3, S4,
[(2,3,4), (2,3)]);
surjective testIsSurjective(hom);
injective testIsInjective(hom);
bijective testIsBijective(hom);
kernelKernel(AllHomomorphisms(S3, S4));
imageImage(AllHomomorphisms(S3, S4));
actions
pari/gpmagmagapsingular
conjugate element# (1,2,3)^-1 * (1,2) * (1,2,3):
(1,2)^(1,2,3)
conjugate setS3 := SymmetricGroup(3);
S3^(3,4);
(3,4)^S3;
conjugacy classS4 := SymmetricGroup(4);
AsList(ConjugacyClass(S4, (1,2,3)));
conjugate groupConjugateGroup(SymmetricGroup(4), (4, 5));
conjugacy classesConjugacyClasses(SymmetricGroup(4));
stabilizer
orbit
transitive test
____________________________________________________________________________________________________________________________________________________________________________________________________________________________

# Combinatorics

## factorial

n! is the product of the integers 1 through n.

n! is the number of permutations or bijections on a set of n elements.

## integer partitions

The number of ways of writing a positive integer n as a sum of positive integers.

When counting integer partitions, 2 + 1 and 1 + 2 are not considered to be distinct ways of summing to 3. In other words, one is counting the multisets which sum to n.

# Groups

A group is a set G and a binary operator—written here as*—which takes elements of the set as operands and obeys the following axioms:

• Closure: For every g and h in G, g * h is also in G.
• Identity: There exists e in G such that for all g in G, e * g = g * e = g.
• Associativity: For all f, g and h in G, (f * g) * h = f * (g * h).
• Inverse: For every g in G, there exists g' in G such that g * g' = g' * g = e.

Abelian groups obey an additional axiom:

• Commutativity: For all g and h in G, g * h = h * g.

The order of a group is the number elements in the set. The smallest group is the trivial group, which contains a single element which is the identity. It has order 1.

The integers, rationals, real numbers, and complex numbers are Abelian groups under addition; zero is the identity element. The integers and rationals are countably infinite. The real numbers and complex numbers are uncountably infinite.

The integers modulo n are an Abelian group under addition; zero is the identity number. The group is finite and has order n.

The non-zero rationals, non-zero real numbers, and non-zero complex numbers are Abelian groups under multiplication; one is the identity element.

A permutation is a bijection on a set of n elements. The permutations of size n form a group under composition; the group is non-Abelian when n > 2. The identity permutation which maps each element of the set to itself is the group identity. The order of the group is n!.

The classical Lie groups are examples of infinite, non-Abelian groups. In all cases the group operation is matrix multiplication:

 GL(n, ℝ) general linear group of degree n invertible n×n matrices SL(n, ℝ) special linear group of degree n n×n matrices with determinant one O(n, ℝ) orthogonal group of degree n n×n orthogonal matrices; i.e. MMT = I SO(n, ℝ) special orthogonal group of degree n n×n orthogonal matrices with determinant one U(n, ℂ) unitary group of degree n n×n unitary matrices; i.e. MM* = I SU(n, ℂ) special unitary group of degree n n×n unitary matrices with determinant one

## named groups

Some well-known groups can be specified by name.

The symmetric group Sn is the group of permutations on a set of n elements.

The alternating group An is the group of even permutations on a set of n elements.

The cyclic group Z/n is a group generated by a single element of order n. It is Abelian group and is isomorphic to the integers modulo n under addition.

The dihedral group Dn is the symmetry group of the regular n-polygon.

## group from permutation generators

gap:

When a group is created using GroupByGenerators, the generators returned by GeneratorsOfGroup will not necessarily be the same as the generators provided to the constructor.

If the group is created using GroupWithGenerators, then the generators returned by GeneratorsOfGroup will be the same.

## direct product

The direct product is a group defined on the Cartesian product of two groups.

Given groups G and H, elements g, g' in G, and elements h, h' in H, the group operation is (g, h) * (g', h') = (g * g', h * h').

## free product

A free product is group defined on equivalence classes of words consisting of elements from two groups G and H.

The equivalence relation is defined in terms of reductions. Two adjacent elements in the word from the same group can be replaced by their product, and the identity element of either group can be removed.

The group operation is concatenation.

The free product is an infinite group when both groups are non-trivial.

gap

Some functions do not appear to work on free products: Order() and Random().

## all elements

How to iterate through all elements in a group.

## identity element

The group identity e.

For all g in G, e * g = g * e = g.

## random element

How to select an element from the group randomly.

How

## inverse element

Each element g in a group has an inverse g-1 such that g * g-1 = g-1 * g = e where e is the identity element.

## element order

For an element g in a finite group, there must be a positive integer n such that gn is the identity element. The order of an element is the smallest such n.

## group to presentation

Provide a presentation for the group.

## group order

How many elements are in the group.

## cyclic test

Is the group generated by single element.

## abelian test

Is the group operation commutative.

# Subroups

A subgroup is a subset of a group which is itself a group.

A nontrivial group always has at least two subgroups: the group itself and the trivial subgroup. A proper subgroup is a subgroup which is not equal to the group itself.

One test whether a subset H of a group G is a subgroup is verify that H is nonempty and for every x and y in H, xy-1 is in H.

Lagrange's theorem and Sylow Theorems

## all subgroups

A list of all subgroups in a group.

## subgroup lattice

The subgroups arranged by inclusion in a lattice.

## maximal subgroups

A maximal subgroup is a proper subgroup which is not contained in any other proper subgroup.

## frattini subgroup

The Frattini subgroup is the intersection of all maximal subgroups.

## subgroup from generators

gap:

ClosureGroup finds the smallest group containing a group and a list of elements, some of which might not be in the group which is the first argument.

## normal subgroups

The normal subgroups of a group.

A subgroup H of a group G is normal if its left and right cosets coincide. That is gH = Hg for all g in G. Another way to express this is that H is invariant under conjugation, or that H = g-1Hg for all g in G.

The significance of normal subgroups is that we can define a group operation on the cosets using representatives if and only if the subgroup is normal.

The first group isomorphism theorem states that the kernel of a group homomorphism is a normal subgroup.

For n ≥ 5, the only proper non-trivial normal subgroup of Sn is An.

# Group Homomorphisms

A homomorphism is a function φ from (G, *) to (H, *') such that

• φ(x * y) = φ(x) *' φ(y) for all x, y ∈ G.

An isomorphism is a bijective homomorphism.

If an isomorphism exists between two groups, they are said to be isomorphic. Isomorphic groups are in a sense the same; group theory is the study of properties which are invariant under isomorphism. Elsewhere we may speak of two isomorphic groups as being the same group.

# Actions

A group G is said to act on a set A if there is an operation ⋅: G × A → A such that

• g₁⋅(g₂⋅a) = (g₁*g₂)⋅a for all g₁, g₂ ∈ G and a ∈ A
• e⋅a = a for all a ∈ A where e is the identity in A

# GAP

GAP - Reference Manual

Singular Manual