---
title: "Element Operations"
canonical: "https://docs.magma-maths.org/v2.30-1/BasicRings/Rationals/element.html"
source: "https://docs.magma-maths.org/v2.30-1/BasicRings/Rationals/element.md"
part: "Basic Rings"
chapter: "Rational Field"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "https://docs.magma-maths.org/v2.30-1/index.md"}
  - {"title": "Basic Rings", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/index-basic-rings.md"}
  - {"title": "Rational Field", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/Rationals/index-rationals.md"}
prev: {"title": "Structure Operations", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/Rationals/operation.md"}
next: {"title": "Finite Fields", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/FiniteFields/index-finite-fields.md"}
intrinsics:
  - {"name": "Parent", "signature": "Parent(r): FldRatElt -> FldRat"}
  - {"name": "Category", "signature": "Category(r): FldRatElt -> Cat"}
  - {"name": "+", "signature": "+ a: FldRatElt -> FldRatElt"}
  - {"name": "-", "signature": "- a: FldRatElt -> FldRatElt"}
  - {"name": "+", "signature": "a + b: FldRatElt, FldRatElt -> FldRatElt"}
  - {"name": "-", "signature": "a - b: FldRatElt, FldRatElt -> FldRatElt"}
  - {"name": "*", "signature": "a * b: FldRatElt, FldRatElt -> FldRatElt"}
  - {"name": "^", "signature": "a ^ k: FldRatElt, RngIntElt -> FldRatElt"}
  - {"name": "/", "signature": "a / b: FldRatElt, FldRatElt -> FldRatElt"}
  - {"name": "+:=", "signature": "a +:= b: FldRatElt, FldRatElt -> FldRatElt"}
  - {"name": "-:=", "signature": "a -:= b: FldRatElt, FldRatElt -> FldRatElt"}
  - {"name": "*:=", "signature": "a *:= b: FldRatElt, FldRatElt -> FldRatElt"}
  - {"name": "/:=", "signature": "a /:= b: FldRatElt, FldRatElt -> FldRatElt"}
  - {"name": "^:=", "signature": "a ^:= k: FldRatElt, RngIntElt -> FldRatElt"}
  - {"name": "Numerator", "signature": "Numerator(q): FldRatElt -> RngIntElt"}
  - {"name": "Denominator", "signature": "Denominator(q): FldRatElt -> RngIntElt"}
  - {"name": "eq", "signature": "a eq b: FldRatElt, FldRatElt -> BoolElt"}
  - {"name": "ne", "signature": "a ne b: FldRatElt, FldRatElt -> BoolElt"}
  - {"name": "in", "signature": "a in R: FldRatElt, Rng -> BoolElt"}
  - {"name": "notin", "signature": "a notin R: FldRatElt, Rng -> BoolElt"}
  - {"name": "IsIntegral", "signature": "IsIntegral(q): FldRatElt -> BoolElt"}
  - {"name": "IsZero", "signature": "IsZero(a): FldRatElt -> BoolElt"}
  - {"name": "IsOne", "signature": "IsOne(a): FldRatElt -> BoolElt"}
  - {"name": "IsMinusOne", "signature": "IsMinusOne(a): FldRatElt -> BoolElt"}
  - {"name": "IsNilpotent", "signature": "IsNilpotent(a): FldRatElt -> BoolElt"}
  - {"name": "IsIdempotent", "signature": "IsIdempotent(a): FldRatElt -> BoolElt"}
  - {"name": "IsUnit", "signature": "IsUnit(a): FldRatElt -> BoolElt"}
  - {"name": "IsZeroDivisor", "signature": "IsZeroDivisor(a): FldRatElt -> BoolElt"}
  - {"name": "IsRegular", "signature": "IsRegular(a): FldRatElt -> BoolElt"}
  - {"name": "IsIrreducible", "signature": "IsIrreducible(a): FldRatElt -> BoolElt"}
  - {"name": "IsPrime", "signature": "IsPrime(a): FldRatElt -> BoolElt"}
  - {"name": "gt", "signature": "a gt b: FldRatElt, FldRatElt -> BoolElt"}
  - {"name": "ge", "signature": "a ge b: FldRatElt, FldRatElt -> BoolElt"}
  - {"name": "lt", "signature": "a lt b: FldRatElt, FldRatElt -> BoolElt"}
  - {"name": "le", "signature": "a le b: FldRatElt, FldRatElt -> BoolElt"}
  - {"name": "Maximum", "signature": "Maximum(a, b): FldRatElt, FldRatElt -> FldRatElt"}
  - {"name": "Maximum", "signature": "Maximum(Q): [FldRatElt] -> FldRatElt"}
  - {"name": "Minimum", "signature": "Minimum(a, b): FldRatElt, FldRatElt -> FldRatElt"}
  - {"name": "Minimum", "signature": "Minimum(Q): [FldRatElt] -> FldRatElt"}
  - {"name": "ComplexConjugate", "signature": "ComplexConjugate(q): FldRatElt -> FldRatElt"}
  - {"name": "Conjugate", "signature": "Conjugate(q): FldRatElt -> FldRatElt"}
  - {"name": "Norm", "signature": "Norm(q): FldRatElt -> FldRatElt"}
  - {"name": "Trace", "signature": "Trace(q): FldRatElt -> FldRatElt"}
  - {"name": "MinimalPolynomial", "signature": "MinimalPolynomial(q): FldRatElt -> RngUPolElt"}
  - {"name": "AbsoluteValue", "signature": "AbsoluteValue(q): FldRatElt -> FldRatElt"}
  - {"name": "Abs", "signature": "Abs(q): FldRatElt -> FldRatElt"}
  - {"name": "Sign", "signature": "Sign(q): FldRatElt -> RngIntElt"}
  - {"name": "Height", "signature": "Height(q): FldRatElt -> RngIntElt"}
  - {"name": "Ceiling", "signature": "Ceiling(q): FldRatElt -> RngIntElt"}
  - {"name": "Floor", "signature": "Floor(q): FldRatElt -> RngIntElt"}
  - {"name": "Round", "signature": "Round(q): FldRatElt -> RngIntElt"}
  - {"name": "Truncate", "signature": "Truncate(q): FldRatElt -> RngIntElt"}
  - {"name": "Qround", "signature": "Qround(q, M): FldRatElt, RngIntElt -> FldRatElt"}
  - {"name": "ContinuedFraction", "signature": "ContinuedFraction(r): FldRatElt -> [ RngIntElt ]"}
  - {"name": "ContinuedFractionValue", "signature": "ContinuedFractionValue(C): [ RngIntElt ] -> FldRatElt"}
  - {"name": "HirzebruchJungContinuedFraction", "signature": "HirzebruchJungContinuedFraction(r): FldRatElt -> [ RngIntElt ]"}
  - {"name": "HJContinuedFraction", "signature": "HJContinuedFraction(r): FldRatElt -> [ RngIntElt ]"}
  - {"name": "HirzebruchJungContinuedFractionValue", "signature": "HirzebruchJungContinuedFractionValue(C): [ RngIntElt ] -> FldRatElt"}
  - {"name": "HJContinuedFractionValue", "signature": "HJContinuedFractionValue(C): [ RngIntElt ] -> FldRatElt"}
  - {"name": "RationalReconstruction", "signature": "RationalReconstruction(s): RngIntResElt -> BoolElt, FldRatElt"}
  - {"name": "RationalReconstruction", "signature": "RationalReconstruction(s): FldFinElt -> BoolElt, FldRatElt"}
  - {"name": "Valuation", "signature": "Valuation(x, p): FldRatElt, RngIntElt -> RngIntElt, FldRatElt"}
  - {"name": "Valuation", "signature": "Valuation(x, I): FldRatElt, RngIntElt -> RngIntElt, FldRatElt"}
  - {"name": "ElementToSequence", "signature": "ElementToSequence(a): FldRatElt -> [FldRatElt]"}
  - {"name": "Eltseq", "signature": "Eltseq(a): FldRatElt -> [FldRatElt]"}
examples:
  - "ex-da083f"
---

# Element Operations

A variety of different types of operations are provided for rational elements including arithmetic operations, comparison and predicates, and converting to a sequence.

## Parent and Category

<a id="function-parent-fldratelt"></a>
### `Parent(r): FldRatElt -> FldRat`

<a id="function-category-fldratelt"></a>
### `Category(r): FldRatElt -> Cat`

## Arithmetic Operators

<a id="operation-operation-fldratelt-fldratelt"></a>
### `+ a: FldRatElt -> FldRatElt`

<a id="operation-operation-fldratelt-fldratelt-2"></a>
### `- a: FldRatElt -> FldRatElt`

<a id="operation-op-plus-fldratelt-fldratelt"></a>
### `a + b: FldRatElt, FldRatElt -> FldRatElt`

<a id="operation-op-minus-fldratelt-fldratelt"></a>
### `a - b: FldRatElt, FldRatElt -> FldRatElt`

<a id="operation-op-times-fldratelt-fldratelt"></a>
### `a * b: FldRatElt, FldRatElt -> FldRatElt`

<a id="operation-op-pow-fldratelt-rngintelt"></a>
### `a ^ k: FldRatElt, RngIntElt -> FldRatElt`

<a id="operation-op-div-fldratelt-fldratelt"></a>
### `a / b: FldRatElt, FldRatElt -> FldRatElt`

<a id="operation-operation-fldratelt-fldratelt-fldratelt"></a>
### `a +:= b: FldRatElt, FldRatElt -> FldRatElt`

<a id="operation-operation-fldratelt-fldratelt-fldratelt-2"></a>
### `a -:= b: FldRatElt, FldRatElt -> FldRatElt`

<a id="operation-operation-fldratelt-fldratelt-fldratelt-3"></a>
### `a *:= b: FldRatElt, FldRatElt -> FldRatElt`

<a id="operation-operation-fldratelt-fldratelt-fldratelt-4"></a>
### `a /:= b: FldRatElt, FldRatElt -> FldRatElt`

<a id="operation-operation-fldratelt-rngintelt-fldratelt"></a>
### `a ^:= k: FldRatElt, RngIntElt -> FldRatElt`

## Numerator and Denominator

<a id="function-numerator-fldratelt"></a>
### `Numerator(q): FldRatElt -> RngIntElt`

The (integer) numerator of the rational number $q$ in reduced form.

<a id="function-denominator-fldratelt"></a>
### `Denominator(q): FldRatElt -> RngIntElt`

The (integer) denominator of the rational number $q$ in reduced form. This will always be a positive integer.

<a id="example-ex-da083f"></a>
### `Example: Numerator and Denominator (ex-da083f)`

Rational numbers are always immediately put in reduced form, that is, the greatest common divisor of numerator and denominator is taken out, and the denominator will be positive.

```magma
> Numerator(10/-4);
-5
> Denominator(10/-4);
2
```

## Equality and Membership

<a id="operation-op-eq-fldratelt-fldratelt"></a>
### `a eq b: FldRatElt, FldRatElt -> BoolElt`

<a id="operation-op-ne-fldratelt-fldratelt"></a>
### `a ne b: FldRatElt, FldRatElt -> BoolElt`

<a id="operation-op-in-fldratelt-rng"></a>
### `a in R: FldRatElt, Rng -> BoolElt`

<a id="operation-op-notin-fldratelt-rng"></a>
### `a notin R: FldRatElt, Rng -> BoolElt`

## Predicates on Ring Elements

<a id="function-isintegral-fldratelt"></a>
### `IsIntegral(q): FldRatElt -> BoolElt`

Returns `true` if the rational number $q$ is an element of the ring of integers, `false` otherwise.

<a id="function-iszero-fldratelt"></a>
### `IsZero(a): FldRatElt -> BoolElt`

<a id="function-isone-fldratelt"></a>
### `IsOne(a): FldRatElt -> BoolElt`

<a id="function-isminusone-fldratelt"></a>
### `IsMinusOne(a): FldRatElt -> BoolElt`

<a id="function-isnilpotent-fldratelt"></a>
### `IsNilpotent(a): FldRatElt -> BoolElt`

<a id="function-isidempotent-fldratelt"></a>
### `IsIdempotent(a): FldRatElt -> BoolElt`

<a id="function-isunit-fldratelt"></a>
### `IsUnit(a): FldRatElt -> BoolElt`

<a id="function-iszerodivisor-fldratelt"></a>
### `IsZeroDivisor(a): FldRatElt -> BoolElt`

<a id="function-isregular-fldratelt"></a>
### `IsRegular(a): FldRatElt -> BoolElt`

<a id="function-isirreducible-fldratelt"></a>
### `IsIrreducible(a): FldRatElt -> BoolElt`

<a id="function-isprime-fldratelt"></a>
### `IsPrime(a): FldRatElt -> BoolElt`

## Comparison

<a id="operation-op-gt-fldratelt-fldratelt"></a>
### `a gt b: FldRatElt, FldRatElt -> BoolElt`

<a id="operation-op-ge-fldratelt-fldratelt"></a>
### `a ge b: FldRatElt, FldRatElt -> BoolElt`

<a id="operation-op-lt-fldratelt-fldratelt"></a>
### `a lt b: FldRatElt, FldRatElt -> BoolElt`

<a id="operation-op-le-fldratelt-fldratelt"></a>
### `a le b: FldRatElt, FldRatElt -> BoolElt`

<a id="function-maximum-fldratelt-fldratelt"></a>
### `Maximum(a, b): FldRatElt, FldRatElt -> FldRatElt`

<a id="function-maximum-fldratelt"></a>
### `Maximum(Q): [FldRatElt] -> FldRatElt`

<a id="function-minimum-fldratelt-fldratelt"></a>
### `Minimum(a, b): FldRatElt, FldRatElt -> FldRatElt`

<a id="function-minimum-fldratelt"></a>
### `Minimum(Q): [FldRatElt] -> FldRatElt`

## Conjugates, Norm, and Trace

<a id="function-complexconjugate-fldratelt"></a>
### `ComplexConjugate(q): FldRatElt -> FldRatElt`

The complex conjugate of $q$, which will be the rational number $q$ itself.

<a id="function-conjugate-fldratelt"></a>
### `Conjugate(q): FldRatElt -> FldRatElt`

The conjugate of $q$, which will be the rational number $q$ itself.

<a id="function-norm-fldratelt"></a>
### `Norm(q): FldRatElt -> FldRatElt`

The norm (in ${\mathbb{Q}}$) of $q$, which will be the rational number $q$ itself.

<a id="function-trace-fldratelt"></a>
### `Trace(q): FldRatElt -> FldRatElt`

The trace (in ${\mathbb{Q}}$) of $q$, which will be the rational number $q$ itself.

<a id="function-minimalpolynomial-fldratelt"></a>
### `MinimalPolynomial(q): FldRatElt -> RngUPolElt`

Returns the minimal polynomial of the rational number $q$, which is the monic linear polynomial with constant coefficient $q$ in a univariate polynomial ring $R$ over the rational field. (If $R$ has not been created before with a name for its indeterminate, `$.1-q` will be returned.)

## Absolute Value and Sign

<a id="function-absolutevalue-fldratelt"></a>
### `AbsoluteValue(q): FldRatElt -> FldRatElt`

<a id="function-abs-fldratelt"></a>
### `Abs(q): FldRatElt -> FldRatElt`

The absolute value $\vert q\vert$ of a rational number $q$.

<a id="function-sign-fldratelt"></a>
### `Sign(q): FldRatElt -> RngIntElt`

Returns the sign of the rational number $q$, which is one of the integers $-1$, $0$, $1$, corresponding to the cases $q<0$, $q=0$, and $q>0$.

<a id="function-height-fldratelt"></a>
### `Height(q): FldRatElt -> RngIntElt`

The height of $q=r/s$. For $r$ and $s$ coprime, the height is defined as the maximum of the absolute value of $r$ and $s$.

## Rounding and Truncating

<a id="function-ceiling-fldratelt"></a>
### `Ceiling(q): FldRatElt -> RngIntElt`

The ceiling of the rational number $q$, that is, the least integer greater than or equal to $q$.

<a id="function-floor-fldratelt"></a>
### `Floor(q): FldRatElt -> RngIntElt`

The floor of the rational number $q$, that is, the largest integer less than or equal to $q$.

<a id="function-round-fldratelt"></a>
### `Round(q): FldRatElt -> RngIntElt`

This function returns the integer value of the rational number $q$ rounded to the nearest integer. In the case of a tie, rounding is done away from zero (that is, $i+{1\over2}$ is rounded to $i+1$, for non-negative integers $i$ and $i-{1\over2}$ is rounded to $i-1$, for non-positive integers $i$).

<a id="function-truncate-fldratelt"></a>
### `Truncate(q): FldRatElt -> RngIntElt`

This function returns the integer truncation of the rational number $q$, that is the integral part of $q$. Thus the effect is that of rounding towards $0$.

<a id="function-qround-fldratelt-rngintelt"></a>
### `Qround(q, M): FldRatElt, RngIntElt -> FldRatElt`

```magma
ContFrac: BoolElt                    Default: false
```

Finds a rational approximation $d$ of $q$ such that the denominator of $d$ is bounded by $M$. If `ContFrac` is given then an optimal approximation is computed using the continued fraction process. By default $d$ is obtained by some rounding procedure which is faster but gives worse results.

## Continued Fractions

<a id="function-continuedfraction-fldratelt"></a>
### `ContinuedFraction(r): FldRatElt -> [ RngIntElt ]`

Given a rational $r$, return the sequence of partial quotients of the continued fraction expansion of $r$.

<a id="function-continuedfractionvalue-rngintelt"></a>
### `ContinuedFractionValue(C): [ RngIntElt ] -> FldRatElt`

Given a continued fraction expansion $C$, return the rational $r$ such that $C$ equals `ContinuedFraction(r)`.

<a id="function-hirzebruchjungcontinuedfraction-fldratelt"></a>
### `HirzebruchJungContinuedFraction(r): FldRatElt -> [ RngIntElt ]`

<a id="function-hjcontinuedfraction-fldratelt"></a>
### `HJContinuedFraction(r): FldRatElt -> [ RngIntElt ]`

Given a rational $r$, return the sequence of partial quotients of the Hirzebruch-Jung continued fraction expansion of $r$.

<a id="function-hirzebruchjungcontinuedfractionvalue-rngintelt"></a>
### `HirzebruchJungContinuedFractionValue(C): [ RngIntElt ] -> FldRatElt`

<a id="function-hjcontinuedfractionvalue-rngintelt"></a>
### `HJContinuedFractionValue(C): [ RngIntElt ] -> FldRatElt`

Given a Hirzebruch-Jung continued fraction expansion $C$, return the rational $r$ so that `HJContinuedFraction(r)` equals $C$.

## Rational Reconstruction

Under certain circumstances it is useful to have a partial inverse of the function $\psi_m\colon{\mathbb{Q}}\rightarrow {\mathbb{Z}}/m{\mathbb{Z}}$ of taking residues modulo $m$ (where the obvious value of $\psi_m$ is only defined for rational numbers with denominator in smallest terms coprime to $m$); the partial inverse of the function is sometimes referred to as ‘rational reconstruction’. For $s\in {\mathbb{Z}}/m{\mathbb{Z}}$ the value of $\psi^{-1}(s)$ is the rational number $r$ for which $\psi_m(r)=s$ and, in addition, the absolute values of both the numerator and denominator of $r$ are at most $\sqrt{m/2}$; such $r$ does not always exist, but if $r$ exists it is unique.

<a id="function-rationalreconstruction-rngintreselt"></a>
### `RationalReconstruction(s): RngIntResElt -> BoolElt, FldRatElt`

<a id="function-rationalreconstruction-fldfinelt"></a>
### `RationalReconstruction(s): FldFinElt -> BoolElt, FldRatElt`

Given an element $s$ of a ring $S$ of $m$ elements, return a Boolean flag indicating whether or not a rational number $r$ exists such that for the representation $r=n/d$ in minimal terms it holds that $n\cdot d^{-1}\equiv s\bmod m$, $\vert n\vert\le\sqrt{m/2}$ and $0 < d \le\sqrt{m/2}$. If the flag is true, the element $r$ is also returned. The ring $S$ is allowed to be a residue class ring `Integers(m)` or a finite field of prime cardinality $p=m$: `FiniteField(p)`.

In addition, $s$ is allowed to be a matrix over a prime finite field, in which case the existence (and, if possible, value) of a rational reconstruction of the matrix is determined.

## Valuation

<a id="function-valuation-fldratelt-rngintelt"></a>
### `Valuation(x, p): FldRatElt, RngIntElt -> RngIntElt, FldRatElt`

<a id="function-valuation-fldratelt-rngintelt-2"></a>
### `Valuation(x, I): FldRatElt, RngIntElt -> RngIntElt, FldRatElt`

The valuation $v$ of the rational number $x$ at the prime $p$ (the prime ideal $I$). This is the difference of the valuations of the numerator and denominator of $x$. The optional second return value is the rational $u$ such that $x = p^v u$.

## Sequence Conversions

<a id="function-elementtosequence-fldratelt"></a>
### `ElementToSequence(a): FldRatElt -> [FldRatElt]`

<a id="function-fldrat-eltseq"></a>
### `Eltseq(a): FldRatElt -> [FldRatElt]`

The sequence $[a]$ for compatibility with the other field types.
