# Element Operations

## Arithmetic Operators

The usual unary and binary ring operations are available for noncommutative polynomials, noting that multiplication is associative but noncommutative, of course.

### `+ a: AlgFrElt -> AlgFrElt`

### `- a: AlgFrElt -> AlgFrElt`

### `a + b: AlgFrElt, AlgFrElt -> AlgFrElt`

### `a - b: AlgFrElt, AlgFrElt -> AlgFrElt`

### `a * b: AlgFrElt, AlgFrElt -> AlgFrElt`

### `a ^ k: AlgFrElt, RngIntElt -> AlgFrElt`

### `a / b: AlgFrElt, AlgFrElt -> FldFunMElt`

### `a div b: AlgFrElt, AlgFrElt -> AlgFrElt`

### `a +:= b: AlgFrElt, AlgFrElt -> AlgFrElt`

### `a -:= b: AlgFrElt, AlgFrElt -> AlgFrElt`

### `a *:= b: AlgFrElt, AlgFrElt -> AlgFrElt`

### `a div:= b: AlgFrElt, AlgFrElt -> AlgFrElt`

## Equality and Membership

### `a eq b: AlgFrElt, AlgFrElt -> BoolElt`

### `a ne b: AlgFrElt, AlgFrElt -> BoolElt`

### `a in R: AlgFrElt, Rng -> BoolElt`

### `a notin R: AlgFrElt, Rng -> BoolElt`

## Predicates on Algebra Elements

### `IsZero(f): AlgFrElt -> BoolElt`

### `IsOne(f): AlgFrElt -> BoolElt`

### `IsMinusOne(f): AlgFrElt -> BoolElt`

### `IsNilpotent(f): AlgFrElt -> BoolElt`

### `IsIdempotent(f): AlgFrElt -> BoolElt`

### `IsUnit(f): AlgFrElt -> BoolElt`

### `IsZeroDivisor(f): AlgFrElt -> BoolElt`

### `IsRegular(f): AlgFrElt -> BoolElt`

### `IsIrreducible(f): AlgFrElt -> BoolElt`

### `IsPrime(f): AlgFrElt -> BoolElt`

## Coefficients, Monomials, Terms and Degree

The functions in this subsection allow one to access noncommutative polynomials.

### `Coefficients(f): AlgFrElt -> [ RngElt ]`

Given a noncommutative polynomial $f$ with coefficients in $R$, this function returns a sequence of ‘base’ coefficients, that is, a sequence of elements of $R$ occurring as coefficients of the monomials in $f$. Note that the monomials are ordered, and that the sequence of coefficients corresponds exactly to the sequence of monomials returned by `Monomials(f)`.

### `LeadingCoefficient(f): AlgFrElt -> RngElt`

Given a noncommutative polynomial $f$ with coefficients in $R$, this function returns the leading coefficient of $f$ as an element of $R$; this is the coefficient of the leading monomial of $f$, that is, the first among the monomials occurring in $f$ with respect to the ordering of monomials used in $F$.

### `TrailingCoefficient(f): AlgFrElt -> RngElt`

Given a noncommutative polynomial $f$ with coefficients in $R$, this function returns the trailing coefficient of $f$ as an element of $R$; this is the coefficient of the trailing monomial of $f$, that is, the last among the monomials occurring in $f$ with respect to the ordering of monomials used in $F$.

### `MonomialCoefficient(f, m): AlgFrElt, AlgFrElt -> RngElt`

Given a noncommutative polynomial $f$ and a monomial $m$, this function returns the coefficient with which $m$ occurs in $f$ as an element of $R$.

### `Monomials(f): AlgFrElt -> [ AlgFrElt ]`

Given a noncommutative polynomial $f\in F$, this function returns a sequence of the monomials (monoid words) occurring in $f$. Note that the monomials in $F$ are ordered, and that the sequence of monomials corresponds exactly to the sequence of coefficients returned by `Coefficients(f)`.

### `LeadingMonomial(f): AlgFrElt -> AlgFrElt`

Given a noncommutative polynomial $f\in F$ this function returns the leading monomial of $f$, that is, the first monomial element of $F$ that occurs in $f$, with respect to the ordering of monomials used in $F$.

### `Terms(f): AlgFrElt -> [ AlgFrElt ]`

Given a noncommutative polynomial $f\in F$, this function returns the sequence of (non-zero) terms of $f$ as elements of $F$. The terms are ordered according to the ordering on the monomials in $F$. Consequently the $i$-th element of this sequence of terms will be equal to the product of the $i$-th element of the sequence of coefficients and the $i$-th element of the sequence of monomials.

### `LeadingTerm(f): AlgFrElt -> AlgFrElt`

Given a noncommutative polynomial $f\in F$, this function returns the leading term of $f$ as an element of $F$; this is the product of the leading monomial and the leading coefficient that is, the first among the monomial terms occurring in $f$ with respect to the ordering of monomials used in $F$.

### `TrailingTerm(f): AlgFrElt -> RngElt`

Given a noncommutative polynomial $f\in F$, this function returns the trailing term of $f$ as an element of $F$; this is the last among the monomial terms occurring in $f$ with respect to the ordering of monomials used in $F$.

### `Length(m): AlgFrElt -> RngIntElt`

Given a noncommutative monomial (word) $m$, return the length of $m$, i.e., the number of letters of $m$. Note that this differs from the commutative case, where the number of terms in a polynomial is returned.

### `m[i]: AlgFrElt, RngIntElt -> AlgFrElt`

Given a noncommutative monomial (word) $m$ of length $l$, and an integer $i$ with $1\leq i\leq l$, return the $i$-th letter of $m$.

### `TotalDegree(f): AlgFrElt -> RngIntElt`

Given a noncommutative polynomial $f$, this function returns the total degree of $f$, which is the maximum of the lengths of all monomials that occur in $f$. If $f$ is the zero polynomial, the return value is $-1$.

### `LeadingTotalDegree(f): AlgFrElt -> RngIntElt`

Given a noncommutative polynomial, this function returns the leading total degree of $f$, which is the length of the leading monomial of $f$.

### `Example: Terms (ex-4ecfe7)`

In this example we illustrate the above access functions.

```magma
> K := RationalField();
> F<x,y,z> := FreeAlgebra(K, 3);
> f := (3*x*y - 2*y*z)*(4*x - 7*z*y) + 23*x*y*z;
> f;
-21*x*y*z*y + 14*y*z^2*y + 12*x*y*x + 23*x*y*z - 8*y*z*x
> TotalDegree(f);
4
> Coefficients(f);
[ -21, 14, 12, 23, -8 ]
> Monomials(f);
[
    x*y*z*y,
    y*z^2*y,
    x*y*x,
    x*y*z,
    y*z*x
]
> Terms(f);
[
    -21*x*y*z*y,
    14*y*z^2*y,
    12*x*y*x,
    23*x*y*z,
    -8*y*z*x
]
> MonomialCoefficient(f, x*y*z);
23
> LeadingTerm(f);
-21*x*y*z*y
> LeadingCoefficient(f);
-21
> m := Monomials(f)[1];
> m;
x*y*z*y
> Length(m);
4
> m[1];
x
> m[2];
y

```

## Evaluation

### `Evaluate(f, s): AlgFrElt, [ RngElt ] -> RngElt`

### `Evaluate(f, s): AlgFrElt, < RngElt, ..., RngElt > -> RngElt`

Given an element $f$ of a free algebra $F=R\langle x_1, \ldots, x_n\rangle$ and a sequence or tuple $s$ of ring or algebra elements of length $n$, return the value of $f$ at $s$, that is, the value obtained by substituting $x_i=s[i]$. This behaves in the same way as the `hom` constructor above. If the elements of $s$ lie in a ring and can be lifted into the coefficient ring $R$, then the result will be an element of $R$. If the elements of $s$ cannot be lifted to the coefficient ring, then an attempt is made to do a generic evaluation of $f$ at $s$. In this case, the result will be of the same type as the elements of $s$.

### `Example: Terms (ex-88ec97)`

In this example we illustrate the above access functions.

```magma
> K := RationalField();
> F<x,y,z> := FreeAlgebra(K, 3);
> g := x*y + y*z;
> g;
x*y + y*z
> Evaluate(g, [1,2,3]);
8
> Parent($1);
Rational Field
> Evaluate(g, [y,x,z]);
x*z + y*x
> Parent($1);
Finitely presented algebra of rank 3 over Rational Field
Non-commutative Graded Lexicographical Order
Variables: x, y, z

```
