Repository: sympy/sympy

Issue:
Utility for getting generator index of FracField
FracField don't have any utility to get the index of the generator
```
R, x, y, z = ring('x y z', QQ)
R.index(x)
F, x, y, z = field('x y z', QQ)
F.index(x)
# AttributeError: 'FracField' object has no attribute 'index'
```
So a boilerplate is needed to get the index like `F.ring.index(x.numer)`

Discussion:

Patch:
diff --git a/sympy/polys/fields.py b/sympy/polys/fields.py
index c7d4c06d28..e909f32e1d 100644
--- a/sympy/polys/fields.py
+++ b/sympy/polys/fields.py
@@ -151,6 +151,12 @@ def __getnewargs__(self):
     def __hash__(self):
         return self._hash
 
+    def index(self, gen):
+        if isinstance(gen, self.dtype):
+            return self.ring.index(gen.to_poly())
+        else:
+            raise ValueError("expected a %s, got %s instead" % (self.dtype,gen))
+
     def __eq__(self, other):
         return isinstance(other, FracField) and \
             (self.symbols, self.ngens, self.domain, self.order) == \
diff --git a/sympy/solvers/diophantine/diophantine.py b/sympy/solvers/diophantine/diophantine.py
index 0138ff8abf..70e9524168 100644
--- a/sympy/solvers/diophantine/diophantine.py
+++ b/sympy/solvers/diophantine/diophantine.py
@@ -141,21 +141,21 @@ class DiophantineEquationType:
     Parameters
     ==========
 
-    equation :
+    equation
         The diophantine equation that is being solved.
-    free_symbols : list (optional)
+    free_symbols: list (optional)
         The symbols being solved for.
 
     Attributes
     ==========
 
-    total_degree :
+    total_degree
         The maximum of the degrees of all terms in the equation
-    homogeneous :
+    homogeneous
         Does the equation contain a term of degree 0
-    homogeneous_order :
+    homogeneous_order
         Does the equation contain any coefficient that is in the symbols being solved for
-    dimension :
+    dimension
         The number of symbols being solved for
     """
     name = None  # type: str
@@ -407,9 +407,6 @@ def diophantine(eq, param=symbols("t", integer=True), syms=None,
     Simplify the solution procedure of diophantine equation ``eq`` by
     converting it into a product of terms which should equal zero.
 
-    Explanation
-    ===========
-
     For example, when solving, `x^2 - y^2 = 0` this is treated as
     `(x + y)(x - y) = 0` and `x + y = 0` and `x - y = 0` are solved
     independently and combined. Each term is solved by calling
@@ -437,9 +434,6 @@ def diophantine(eq, param=symbols("t", integer=True), syms=None,
     True then permutations of the base solution and/or permutations of the
     signs of the values will be returned when applicable.
 
-    Examples
-    ========
-
     >>> from sympy.solvers.diophantine import diophantine
     >>> from sympy.abc import a, b
     >>> eq = a**4 + b**4 - (2**4 + 3**4)
@@ -670,9 +664,6 @@ def merge_solution(var, var_t, solution):
     This is used to construct the full solution from the solutions of sub
     equations.
 
-    Explanation
-    ===========
-
     For example when solving the equation `(x - y)(x^2 + y^2 - z^2) = 0`,
     solutions for each of the equations `x - y = 0` and `x^2 + y^2 - z^2` are
     found independently. Solutions for `x - y = 0` are `(x, y) = (t, t)`. But
@@ -704,9 +695,6 @@ def diop_solve(eq, param=symbols("t", integer=True)):
     """
     Solves the diophantine equation ``eq``.
 
-    Explanation
-    ===========
-
     Unlike ``diophantine()``, factoring of ``eq`` is not attempted. Uses
     ``classify_diop()`` to determine the type of the equation and calls
     the appropriate solver function.
@@ -817,9 +805,6 @@ def classify_diop(eq, _dict=True):
     '''
     Helper routine used by diop_solve() to find information about ``eq``.
 
-    Explanation
-    ===========
-
     Returns a tuple containing the type of the diophantine equation
     along with the variables (free symbols) and their coefficients.
     Variables are returned as a list and coefficients are returned
@@ -1095,9 +1080,6 @@ def base_solution_linear(c, a, b, t=None):
     """
     Return the base solution for the linear equation, `ax + by = c`.
 
-    Explanation
-    ===========
-
     Used by ``diop_linear()`` to find the base solution of a linear
     Diophantine equation. If ``t`` is given then the parametrized solution is
     returned.
@@ -1152,9 +1134,6 @@ def diop_univariate(eq):
     """
     Solves a univariate diophantine equations.
 
-    Explanation
-    ===========
-
     A univariate diophantine equation is an equation of the form
     `a_{0} + a_{1}x + a_{2}x^2 + .. + a_{n}x^n = 0` where `a_{1}, a_{2}, ..a_{n}` are
     integer constants and `x` is an integer variable.
@@ -1443,9 +1422,6 @@ def diop_DN(D, N, t=symbols("t", integer=True)):
     """
     Solves the equation `x^2 - Dy^2 = N`.
 
-    Explanation
-    ===========
-
     Mainly concerned with the case `D > 0, D` is not a perfect square,
     which is the same as the generalized Pell equation. The LMM
     algorithm [1]_ is used to solve this equation.
@@ -1737,9 +1713,6 @@ def cornacchia(a, b, m):
     r"""
     Solves `ax^2 + by^2 = m` where `\gcd(a, b) = 1 = gcd(a, m)` and `a, b > 0`.
 
-    Explanation
-    ===========
-
     Uses the algorithm due to Cornacchia. The method only finds primitive
     solutions, i.e. ones with `\gcd(x, y) = 1`. So this method can't be used to
     find the solutions of `x^2 + y^2 = 20` since the only solution to former is
@@ -1765,7 +1738,6 @@ def cornacchia(a, b, m):
 
     See Also
     ========
-
     sympy.utilities.iterables.signed_permutations
     """
     sols = set()
@@ -1803,9 +1775,6 @@ def PQa(P_0, Q_0, D):
     r"""
     Returns useful information needed to solve the Pell equation.
 
-    Explanation
-    ===========
-
     There are six sequences of integers defined related to the continued
     fraction representation of `\\frac{P + \sqrt{D}}{Q}`, namely {`P_{i}`},
     {`Q_{i}`}, {`a_{i}`},{`A_{i}`}, {`B_{i}`}, {`G_{i}`}. ``PQa()`` Returns
@@ -1866,9 +1835,6 @@ def diop_bf_DN(D, N, t=symbols("t", integer=True)):
     r"""
     Uses brute force to solve the equation, `x^2 - Dy^2 = N`.
 
-    Explanation
-    ===========
-
     Mainly concerned with the generalized Pell equation which is the case when
     `D > 0, D` is not a perfect square. For more information on the case refer
     [1]_. Let `(t, u)` be the minimal positive solution of the equation
@@ -1958,9 +1924,6 @@ def equivalent(u, v, r, s, D, N):
     Returns True if two solutions `(u, v)` and `(r, s)` of `x^2 - Dy^2 = N`
     belongs to the same equivalence class and False otherwise.
 
-    Explanation
-    ===========
-
     Two solutions `(u, v)` and `(r, s)` to the above equation fall to the same
     equivalence class iff both `(ur - Dvs)` and `(us - vr)` are divisible by
     `N`. See reference [1]_. No test is performed to test whether `(u, v)` and
@@ -2043,9 +2006,6 @@ def transformation_to_DN(eq):
     `ax^2 + bxy + cy^2 + dx + ey + f = 0`
     to more easy to deal with `X^2 - DY^2 = N` form.
 
-    Explanation
-    ===========
-
     This is used to solve the general quadratic equation by transforming it to
     the latter form. Refer [1]_ for more detailed information on the
     transformation. This function returns a tuple (A, B) where A is a 2 X 2
@@ -2632,9 +2592,6 @@ def diop_ternary_quadratic_normal(eq, parameterize=False):
     Solves the quadratic ternary diophantine equation,
     `ax^2 + by^2 + cz^2 = 0`.
 
-    Explanation
-    ===========
-
     Here the coefficients `a`, `b`, and `c` should be non zero. Otherwise the
     equation will be a quadratic binary or univariate equation. If solvable,
     returns a tuple `(x, y, z)` that satisfies the given equation. If the
@@ -3282,9 +3239,6 @@ def partition(n, k=None, zeros=False):
     Returns a generator that can be used to generate partitions of an integer
     `n`.
 
-    Explanation
-    ===========
-
     A partition of `n` is a set of positive integers which add up to `n`. For
     example, partitions of 3 are 3, 1 + 2, 1 + 1 + 1. A partition is returned
     as a tuple. If ``k`` equals None, then all possible partitions are returned
@@ -3405,7 +3359,6 @@ def sum_of_three_squares(n):
 
     See Also
     ========
-
     sum_of_squares()
     """
     special = {1:(1, 0, 0), 2:(1, 1, 0), 3:(1, 1, 1), 10: (1, 3, 0), 34: (3, 3, 4), 58:(3, 7, 0),
@@ -3485,7 +3438,6 @@ def sum_of_four_squares(n):
 
     See Also
     ========
-
     sum_of_squares()
     """
     if n == 0:
@@ -3676,7 +3628,6 @@ def sum_of_squares(n, k, zeros=False):
 
     See Also
     ========
-
     sympy.utilities.iterables.signed_permutations
     """
     yield from power_representation(n, 2, k, zeros)
@@ -3684,9 +3635,9 @@ def sum_of_squares(n, k, zeros=False):
 
 def _can_do_sum_of_squares(n, k):
     """Return True if n can be written as the sum of k squares,
-    False if it can't, or 1 if ``k == 2`` and ``n`` is prime (in which
+    False if it can't, or 1 if k == 2 and n is prime (in which
     case it *can* be written as a sum of two squares). A False
-    is returned only if it can't be written as ``k``-squares, even
+    is returned only if it can't be written as k-squares, even
     if 0s are allowed.
     """
     if k < 1:


Test patch:
diff --git a/sympy/polys/tests/test_fields.py b/sympy/polys/tests/test_fields.py
index 733816f654..df33e8c94c 100644
--- a/sympy/polys/tests/test_fields.py
+++ b/sympy/polys/tests/test_fields.py
@@ -348,3 +348,13 @@ def test_FracElement_subs():
 
 def test_FracElement_compose():
     pass
+
+def test_FracField_index():
+    a = symbols("a")
+    F, x, y, z = field('x y z', QQ)
+    assert F.index(x) == 0
+    assert F.index(y) == 1
+
+    raises(ValueError, lambda: F.index(1))
+    raises(ValueError, lambda: F.index(a))
+    pass