## `nthcoeff` -- the n-th non-zero coefficient of a polynomial

#### Introduction

`nthcoeff(`p, n`)` returns the `n`-th non-zero coefficient of the polynomial `p`.

#### Call(s)

`nthcoeff(p, <vars,> n <, order>)`

#### Parameters

`p` | - | a polynomial of type `DOM_POLY` or a polynomial expression
`vars` | - | a list of indeterminates of the polynomial: typically, identifiers or indexed identifiers
`n` | - | a positive integer
`order` | - | the term ordering: either `LexOrder` or `DegreeOrder` or `DegInvLexOrder` or a user-defined term ordering of type `Dom::MonomOrdering`. The default is the lexicographical ordering LexOrder.

#### Returns

an element of the coefficient domain of the polynomial. An expression is returned if a polynomial expression is used as input. `FAIL` is returned if `n` is larger than the actual number of terms.

### Overloadable:

`p`

#### Related Functions

`coeff`, `collect`, `degree`, `degreevec`, `ground`, `lcoeff`, `ldegree`, `lmonomial`, `lterm`, `nterms`, `nthmonomial`, `nthterm`, `poly`, `poly2list`, `tcoeff`

#### Details

- The argument `p` can either be a polynomial expression, or a polynomial generated by `poly`, or an element of some polynomial domain overloading `nthcoeff`.
- If a list of indeterminates is provided, then `p` is regarded as a polynomial in these indeterminates. Note that the specified list does not have to coincide with the indeterminates of the input polynomial.
- The ``first'' coefficient is the leading coefficient as returned by `lcoeff`, the ``last'' coefficient is the trailing coefficient as returned by `tcoeff`.
- `nthcoeff` returned the `n`-th non-zero coefficient with respect to the lexicographical ordering, unless a different ordering is specified via the argument `order`. Cf. example 3.
- The result of `nthcoeff` is not fully evaluated. Evaluation can be enforced by the function `eval`. Cf. example 4.
- A zero polynomial has no terms: `nthcoeff` returns `FAIL`.
- `nthcoeff` is a library routine. If no term ordering is specified, the arguments are passed to a fast kernel routine.

#### Example 1

We give some self explaining examples:

    >> p := poly(100*x^100 + 49*x^49 + 7*x^7, [x]):
       nthcoeff(p, 1), nthcoeff(p, 2), nthcoeff(p, 3)

                                    100, 49, 7

    >> nthcoeff(p, 4)

                                       FAIL

    >> nthcoeff(poly(0, [x]), 1)

                                       FAIL

    >> delete p:

#### Example 2

We demonstrate how the indeterminates influence the result:

    >> p := 2*x^2*y + 3*x*y^2 + 6:
       nthcoeff(p, [x, y], 2), nthcoeff(p, [y, x], 2)

                                       3, 2

    >> p := poly(2*x^2*y + 3*x*y^2 + 6, [x, y]):
       nthcoeff(p, 1), nthcoeff(p, [y, x], 1)

                                       2, 3

    >> delete p:

#### Example 3

We demonstrate the effect of various term orders:

    >> p := poly(5*x^4 + 4*x^3*y*z^2 + 3*x^2*y^3*z + 2, [x, y, z])

                         4      3    2      2  3
                 poly(5 x  + 4 x  y z  + 3 x  y  z + 2, [x, y, z])

    >> nthcoeff(p, 1), nthcoeff(p, 1, DegreeOrder),
       nthcoeff(p, 1, DegInvLexOrder)

                                      5, 4, 3

The following call uses the reverse lexicographical order on 3 indeterminates:

    >> nthcoeff(p, 1, Dom::MonomOrdering(RevLex(3)))

                                         3

    >> delete p:

#### Example 4

We demonstrate the evaluation strategy of `nthcoeff`:

    >> p := poly(3*x^3 + 6*x^2*y^2 + 2, [x]): y := 4:
       nthcoeff(p, 2)

                                          2
                                       6 y

Evaluation is enforced by `eval`:

    >> eval(%)

                                        96

    >> delete p, y:

#### Changes

- Now it is possible to specify user defined term orderings.
- Indeterminates can now be specified for polynomials of type `DOM_POLY` as well.
- In previous MuPAD releases, `nthcoeff` was a kernel function.

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000