---
title : AbstractAlgebra cheatsheet
author : Tommy Hofmann
date: 19th June 2019
---
#### Matrices
Let $R$ be a ring, $A$ an array with entries in $R$ and length($A$) = $mn$ where $m,n \in \mathbb{N}$.
Moreover, let $MS$ be the space of $m \times n$ matrices over $R$.
Code | Mathematics
:---------- | ----------:
`matrix(A, 2, 2, [1, 2, 3, 4])` | Construct the matrix over $R$ with rows $1, 2$ and $3, 4$.
`base_ring(M)` | Returns the base ring $R$ of the matrix space MS that the matrix belongs to
`M = matrix(R, m, n, A)` | Returns the $m \times n$ matrix with values in $A$ (row-wise)
`parent(M)` | Returns the parent matrix space of $m$ rows and $n$ columns over $R$
`M[i,j]` | Returns the entry of the matrix M at row $i$ and column $j$
`identity_matrix(R, n)` | Returns the $n \times n$ identity matrix over $R$
`zero_matrix(R, m, n)` | Returns the $m \times n$ zero matrix over $R$.
`nrows(M)` | Returns $m$
`ncols(M)` | Returns $n$
`iszero(M)` | Returns `true` if the matrix $M$ is the zero matrix, otherwise `false`
`isone(M)` | Returns `true` if the matrix $M$ is the identity matrix, otherwise `false`
`iszero_row(M,i)` | Returns `true` if the $i$-th row of the matrix $M$ is zero
`iszero_column(M,i)` | Returns `true` if the $i$-th column of the matrix $M$ is zero
`sub(M, m1, n1, m2, n2)` | Return a copy of the submatrix of $M$ from $(m1, n1)$ to $(m2, n2)$ inclusive
`size(M)` | Returns tuple $(m, n)$
`issquare(M)` | Returns `true` if the matrix $M$ is a square matrix, otherwise `false`
`M^i` | Returns $i$ times $M*...*M$, requires $i \geq 0$ and that the matrix $M$ is square
`transpose(M)` | Returns the transpose of $M$
`gram(M)` | Returns the Gram matrix of $M$
`tr(M)` | Returns the trace of the matrix $M$, requires the matrix to be square.
`content(M)` | Returns the greatest common divisor of all entries of $M$, assuming it exists
`rref(M)` |If $R$ is a field, returns a tuple $(r, A)$ consisting of the rank $r$ of $M$ and a reduced row echelon form $A$ of $M$ otherwise returns tuple $(r, d, A)$ with a denominator $d$ in $R$ such that $A/d$ is the reduced row echelon form of $M$.
`isrref(M)` | Return `true` if $M$ is in reduced row echelon form, otherwise return`false`
`det(M)` | Returns the determinant of the matrix $M$, assumes $M$ is square
`minors(M, k)` | Returns an array consisting of the k-minors of $M$
`rank(M)` | Returns the rank of the matrix $M$
`solve(M, b)` | Given a non-singular $n\times n$ matrix $M$ over a field and an $n\times m$ matrix $b$ over the same field, return an $n\times m$ matrix $x$ such that $Mx = b$
`solve_rational(M, b)` | Given a non-singular $n\times n$ matrix $M$ over a ring and an $n\times m$ $b$ matrix over the same ring, return a tuple $x, d$ consisting of an $n\times m$ matrix $x$ and a denominator $d$ such that $Mx = db$.
`pseudo_inv(M)` | Given a non-singular $n\times n$ matrix $M$ return a tuple $X, d$ consisting of an $n\times n$ matrix $X$ and a denominator $d$ such that $MX = dI_n$.
`can_solve_left_reduced_triu(b, M)` | Returns a tuple ($flag$, x) where $flag$ is set to true if $xM = b$ has a solution, where $M$ is an $m\times n$ matrix in (upper triangular) Hermite normal form or reduced row echelon form and $b$ and $x$ are row vectors with $m$ columns. If there is no solution, $flag$ is set to `false` and $x$ is set to the zero row
`inv(M)` | Given a non-singular $n\times n$ matrix $M$ over a ring, return an $n\times n$ matrix $X$ such that $MX = I_n$
`nullspace(M)` | Returns a tuple $(\nu, N)$ consisting of the nullity $\nu$ of $M$ and a basis $N$ (of column vectors) for the right nullspace of $M$, i.e. such that $MN$ is the zero matrix
`kernel(M; side = :right)` | Returns a tuple $(n, B)$, where n is the rank of the kernel and $B$ is a basis for it. If side is $:right$ or not specified, the right kernel is computed, i.e. the matrix of columns whose span gives the right kernel space. If side is $:left$, the left kernel is computed
`hessenberg(M)` | Returns the Hessenberg form of $M$
`ishessenberg(M)` | Returns `true` if $M$ is in Hessenberg form, otherwise returns `false`
`popov(M)` | Returns the Popov form of $M$
`charpoly(Rx, M)` | Returns the characteristic polynomial $p$ of the matrix $M$. The polynomial ring $Rx$ of the resulting polynomial must be supplied and the matrix is assumed to be square
`minpoly(Rx, M)` | Returns the minimal polynomial $p$ of the matrix $M$. The polynomial ring $Rx$ of the resulting polynomial must be supplied and the matrix must be square.
`hnf(M)` | Returns the upper right row Hermite normal form of $M$
`hnf_with_transform(M)` | Returns the tuple $H, U$ consisting of the upper right row Hermite normal form $H$ of $M$ together with invertible matrix $U$ such that $UM = H$
`snf(M)` | Returns the upper right row Smith normal form of $M$
`snf_with_transform(M)` | Returns the tuple $S, V, U$ consisting of the Smith normal form $S$ of $M$ together with matrices $V$ and $U$ such that $V*M*U = S$
`similarity!(M, i, r)` | Applies a similarity transform to the $n\times n$ matrix $M$ in-place. Let $P$ be the $n\times n$ identity matrix that has had all zero entries of row $i$ replaced with $r$ where $r \in R$, then the transform applied is equivalent to $M = P^{-1}MP$
`swap_rows(M, i, j)` | Returns a matrix with the entries of $M$, where the $i$th and $j$th row are swapped
`swap_cols(M, i, j)` | Returns a matrix with the entries of $M$, where the $i$th and $j$th column are swapped
`invert_rows(M)` | Returns a matrix with the entries of $M$, where the $i$th and $j - i$th row is swapped for $1 \leq i \leq j/2$. Here $j$ is the number of rows of $M$
`invert_cols(M)` | Returns a matrix with the entries of $M$, where the $j$th and $i - j$th column is swapped for $1 \leq j \leq i/2$. Here $i$ is the number of columns of$M$
`hcat(M1, M2)` | Returns the horizontal concatenation of $M1$ and $M2$. Assumes that the number of rows is the same in $M1$ and $M2$
`vcat(M1, M2)` | Returns the vertical concatenation of $M1$ and $M2$. Assumes that the number of columns is the same in $M1$ and $M2$
`P * M` | Applies the pemutation $P$ to the rows of the matrix $M$ and returns the result
`kronecker_product(M1, M2)` | Returns the Kronecker product of the matrices $M1$ and $M2$
`powers(M, j)` | Returns an array $Ar$ of matrices where $Ar[i + 1] = M^i$ for $i = 0..j$
`divexact(M, r)` | Returns $M/r$, i.e. the matrix where each of the entries has been divided by $r$ where $r \in R$. Each division is expected to be exact
#### Multivariate Polynomial Rings
Let $R$ be a ring.
Code | Mathematics
:---------- | ----------:
`T, (x1,x2,x3) = PolynomialRing(R,["x1","x2","x3"])` | Given a base ring and an array of strings specifying how the generators should be printed, returns a tuple `T, (x1, x2, x3)` representing the polynomial ring $T = R[x1, x2, x3]$ with lexiographic order(default can be changed) and the corresponding generators
`ordering(T)` | Returns the ordering of the given polynomial ring as a symbol. The options are `:lex`, `:deglex` and `:degrevlex`
`base_ring(T)` | Returns the base ring $R$ of the polynomial ring $T$
`nvars(T)` | Returns the number of variables of the polynomial ring $T$
`gens(T)` | Returns an array of all generators of the given polynomial ring
`gen(T,i)` | Returns the $i$-th generator of the given polynomial ring
`f = x1 + x3^2` | Constructs the polynomial $f$ of $T$
`f = T([R(1), R(2)],[[1,0,0],[0,0,3]])` | Constructs the polynomial $f = x1+2*x3^3$ of $T$
`vars(f)` | Returns the variables actually occuring in the polynomial $f$
`var_index(x)` | Returns the index of the given variable $x$. If $x$ is not a variable in a multivariate polynomial ring, an exception is raised
`coeff(f, [1, 3], [0, 2])` | Returns the coefficient of $x1^0*x3^2$ in the polynomial $f$ (assuming variables $x1, x2, x3$ in that order)
`coeff(f, [x1, x3], [0, 2])` | Returns the coefficient of $x1^0*x3^2$ in the polynomial $f$
`ishomogeneous(f)` | Returns `true` if the given polynomial $f$ is homogeneous with respect to the standard grading and `false` otherwise
`coeff(f, i)` | Returns the coefficient of the $i$-th term of the polynomial $f$
`monomial(f, i)` | Returns the monomial of the $i$-th term of the polynomial $f$
`lc(f)` | Returns the leading coefficient of the polynomial $f$
`lm(f)` | Returns the leading monomial of the polynomial $f$.
`lt(f)` | Returns the leading term of the polynomial $f$
`term(f, i)` | Returns the $i$-th nonzero term of the polynomial $f$
`degrees(f)` | Returns an array of the degrees of the polynomial $f$ in terms of each variable
`total_degree(f)` | Returns the total degree of $f$
`length(f)` | Returns the number of terms of the polynomial $f$
`ismonomial(f)` | Returns `true` if the given polynomial $f$ has precisely one term whose coefficient is one
`isconstant(f)` | Returns `true` if $f$ is a constant polynomial
`deflation(f)` | Returns the deflation parameters for the exponents of the polynomial $f$
`divides(f,g)` | If the polynomial $g$ divides $f$, returns Tuple (true,h) such that $f=gh$
`evaluate(f,[1,2,3])` | Evaluate the polynomial by substituting each variable in $f$ by the corresponding value in the array, i.e. returns $10 = 1 + 3^2$
`f(1,2,3)` | Evaluate the polynomial at the supplied values, which may be any ring elements, i.e. returns $10 = 1 + 3^2$
`derivative(f, x)` |Return the partial derivative of $f$ with respect to the variable $x$ of the polynomial ring
`setcoeff!(f, i, c)` | Set the coefficient of the i-th term of the polynomial $f$ to $c$
`exponent_vector(f, i)` | Returns a vector of exponents, corresponding to the exponent vector of the i-th term of the polynomial
`coeff(f, exps)` | Returns the coefficient of the term with the given exponent vector, or zero if there is no such term
`sort_terms(f)` | Sort the terms of the given polynomial $f$ according to the polynomial ring ordering. Zero terms and duplicate exponents are ignored