Repository: mpmath/mpmath
Pull request #779 (2024-04-06 08:27:43)

Deprecate current (descending) order of coefficients in polyval(), etc
Affected functions: chebyfit(), polyval(), polyroots() and findpoly().

First step for #142

Please adapt your code to use asc=True (lowest degree - first) order of polynomial coefficients.  This will be a new default for mpmath releases *after* the next release, v1.4.


Patch:
diff --git a/mpmath/calculus/approximation.py b/mpmath/calculus/approximation.py
index faee5b1..b13c659 100644
--- a/mpmath/calculus/approximation.py
+++ b/mpmath/calculus/approximation.py
@@ -1,5 +1,8 @@
+import warnings
+
 from .calculus import defun
 
+
 #----------------------------------------------------------------------------#
 #                              Approximation methods                         #
 #----------------------------------------------------------------------------#
@@ -35,7 +38,7 @@ def chebT(ctx, a=1, b=0):
         Ta, Tb = Tmp, Ta
 
 @defun
-def chebyfit(ctx, f, interval, N, error=False):
+def chebyfit(ctx, f, interval, N, error=False, asc=None):
     r"""
     Computes a polynomial of degree `N-1` that approximates the
     given function `f` on the interval `[a, b]`. With ``error=True``,
@@ -54,6 +57,9 @@ def chebyfit(ctx, f, interval, N, error=False):
     example, it could be used to turn a slow mpmath function
     into a fast machine-precision version of the same.)
 
+    If *asc=False*, descending order of coefficients is used (the term
+    of largest degree - first).
+
     **Examples**
 
     Here we use :func:`~mpmath.chebyfit` to generate a low-degree approximation
@@ -61,15 +67,15 @@ def chebyfit(ctx, f, interval, N, error=False):
 
         >>> from mpmath import mp, chebyfit, cos, nprint, polyval
         >>> mp.pretty = True
-        >>> poly, err = chebyfit(cos, [1, 2], 5, error=True)
+        >>> poly, err = chebyfit(cos, [1, 2], 5, error=True, asc=True)
         >>> nprint(poly)
-        [0.00291682, 0.146166, -0.732491, 0.174141, 0.949553]
+        [0.949553, 0.174141, -0.732491, 0.146166, 0.00291682]
         >>> nprint(err, 12)
         1.61351758081e-5
 
     The polynomial can be evaluated using ``polyval``::
 
-        >>> nprint(polyval(poly, 1.6), 12)
+        >>> nprint(polyval(poly, 1.6, asc=True), 12)
         -0.0291858904138
         >>> nprint(cos(1.6), 12)
         -0.0291995223013
@@ -77,7 +83,7 @@ def chebyfit(ctx, f, interval, N, error=False):
     Sampling the true error at 1000 points shows that the error
     estimate generated by ``chebyfit`` is remarkably good::
 
-        >>> error = lambda x: abs(cos(x) - polyval(poly, x))
+        >>> error = lambda x: abs(cos(x) - polyval(poly, x, asc=True))
         >>> nprint(max([error(1+n/1000.) for n in range(1000)]), 12)
         1.61349954245e-5
 
@@ -122,18 +128,23 @@ def chebyfit(ctx, f, interval, N, error=False):
         for (k, Tk) in zip(range(N), T):
             for i in range(len(Tk)):
                 d[i] += c[k]*Tk[i]
-        d = d[::-1]
         # Estimate maximum error
         err = ctx.zero
         for k in range(N):
             x = ctx.cos(ctx.pi*k/N) * (b-a)*h + (b+a)*h
-            err = max(err, abs(f(x) - ctx.polyval(d, x)))
+            err = max(err, abs(f(x) - ctx.polyval(d, x, asc=True)))
     finally:
         ctx.prec = orig
+    if asc is None:
+        warnings.warn("Descending (wrt powers) order of polynomial "
+                      "coefficients is deprecated, please adapt you "
+                      "code to use ascending order, asc=True.",
+                      DeprecationWarning)
+        asc = False
     if error:
-        return d, +err
+        return d if asc else d[::-1], +err
     else:
-        return d
+        return d if asc else d[::-1]
 
 @defun
 def fourier(ctx, f, interval, N):
diff --git a/mpmath/calculus/differentiation.py b/mpmath/calculus/differentiation.py
index 91935ce..72c1ef2 100644
--- a/mpmath/calculus/differentiation.py
+++ b/mpmath/calculus/differentiation.py
@@ -1,5 +1,6 @@
 from .calculus import defun
 
+
 try:
     iteritems = dict.iteritems
 except AttributeError:
@@ -559,12 +560,11 @@ def taylor(ctx, f, x, n, **options):
     and supported keyword options.
 
     Note that to evaluate the Taylor polynomial as an approximation
-    of `f`, e.g. with :func:`~mpmath.polyval`, the coefficients must be reversed,
-    and the point of the Taylor expansion must be subtracted from
+    of `f`, the point of the Taylor expansion must be subtracted from
     the argument:
 
         >>> p = taylor(exp, 2.0, 10)
-        >>> polyval(p[::-1], 2.5 - 2.0)
+        >>> polyval(p, 2.5 - 2.0, asc=True)
         12.1824939606092
         >>> exp(2.5)
         12.1824939607035
@@ -608,7 +608,7 @@ def pade(ctx, a, L, M):
         >>> a = taylor(f, 0, 6)
         >>> p, q = pade(a, 3, 3)
         >>> x = 10
-        >>> polyval(p[::-1], x)/polyval(q[::-1], x)
+        >>> polyval(p, x, asc=True)/polyval(q, x, asc=True)
         1.38169105566806
         >>> f(x)
         1.38169855941551
diff --git a/mpmath/calculus/odes.py b/mpmath/calculus/odes.py
index 0c002e2..e5f250d 100644
--- a/mpmath/calculus/odes.py
+++ b/mpmath/calculus/odes.py
@@ -1,5 +1,6 @@
 from bisect import bisect
 
+
 class ODEMethods:
     pass
 
@@ -246,7 +247,7 @@ def odefun(ctx, F, x0, y0, tol=None, degree=None, method='taylor', verbose=False
     series_data = [(ser, x0, xb)]
     # We will be working with vectors of Taylor series
     def mpolyval(ser, a):
-        return [ctx.polyval(s[::-1], a) for s in ser]
+        return [ctx.polyval(s, a, asc=True) for s in ser]
     # Find nearest expansion point; compute if necessary
     def get_series(x):
         if x < x0:
diff --git a/mpmath/calculus/polynomials.py b/mpmath/calculus/polynomials.py
index c48e124..acfef7f 100644
--- a/mpmath/calculus/polynomials.py
+++ b/mpmath/calculus/polynomials.py
@@ -1,19 +1,22 @@
+import warnings
+
 from .calculus import defun
 
+
 #----------------------------------------------------------------------------#
 #                                Polynomials                                 #
 #----------------------------------------------------------------------------#
 
 # XXX: extra precision
 @defun
-def polyval(ctx, coeffs, x, derivative=False):
+def polyval(ctx, coeffs, x, derivative=False, asc=None):
     r"""
-    Given coefficients `[c_n, \ldots, c_2, c_1, c_0]` and a number `x`,
+    Given coefficients `[c_0, c_1, c_2, \ldots, c_n]` and a number `x`,
     :func:`~mpmath.polyval` evaluates the polynomial
 
     .. math ::
 
-        P(x) = c_n x^n + \ldots + c_2 x^2 + c_1 x + c_0.
+        P(x) = c_0 + c_1 x + c_2 x^2 \ldots c_n x^n
 
     If *derivative=True* is set, :func:`~mpmath.polyval` simultaneously
     evaluates `P(x)` with the derivative, `P'(x)`, and returns the
@@ -21,19 +24,29 @@ def polyval(ctx, coeffs, x, derivative=False):
 
         >>> from mpmath import mp, polyval
         >>> mp.pretty = True
-        >>> polyval([3, 0, 2], 0.5)
+        >>> polyval([2, 0, 3], 0.5, asc=True)
         2.75
-        >>> polyval([3, 0, 2], 0.5, derivative=True)
+        >>> polyval([2, 0, 3], 0.5, derivative=True, asc=True)
         (2.75, 3.0)
 
+    If *asc=False*, descending order of coefficients is used (the term
+    of largest degree - first).
+
     The coefficients and the evaluation point may be any combination
     of real or complex numbers.
     """
     if not coeffs:
         return ctx.zero
-    p = ctx.convert(coeffs[0])
+    if asc is None:
+        warnings.warn("Descending (wrt powers) order of polynomial "
+                      "coefficients is deprecated, please adapt you "
+                      "code to use ascending order, asc=True.",
+                      DeprecationWarning)
+        asc = False
+        coeffs = coeffs[::-1]
+    p = ctx.convert(coeffs[-1])
     q = ctx.zero
-    for c in coeffs[1:]:
+    for c in reversed(coeffs[:-1]):
         if derivative:
             q = p + x*q
         p = c + x*p
@@ -44,7 +57,7 @@ def polyval(ctx, coeffs, x, derivative=False):
 
 @defun
 def polyroots(ctx, coeffs, maxsteps=50, cleanup=True, extraprec=10,
-        error=False, roots_init=None):
+              error=False, roots_init=None, asc=None):
     """
     Computes all roots (real or complex) of a given polynomial.
 
@@ -56,19 +69,22 @@ def polyroots(ctx, coeffs, maxsteps=50, cleanup=True, extraprec=10,
     With *error=True*, :func:`~mpmath.polyroots` returns a tuple *(roots, err)*
     where *err* is an estimate of the maximum error among the computed roots.
 
+    If *asc=False*, descending order of coefficients is used (the term
+    of largest degree - first).
+
     **Examples**
 
     Finding the three real roots of `x^3 - x^2 - 14x + 24`::
 
         >>> from mpmath import mp, polyroots, nprint, sqrt, polyval
         >>> mp.pretty = True
-        >>> nprint(polyroots([1,-1,-14,24]), 4)
+        >>> nprint(polyroots([24,-14,-1,1],asc=True), 4)
         [-4.0, 2.0, 3.0]
 
     Finding the two complex conjugate roots of `4x^2 + 3x + 2`, with an
     error estimate::
 
-        >>> roots, err = polyroots([4,3,2], error=True)
+        >>> roots, err = polyroots([2,3,4], error=True, asc=True)
         >>> for r in roots:
         ...     print(r)
         ...
@@ -78,16 +94,16 @@ def polyroots(ctx, coeffs, maxsteps=50, cleanup=True, extraprec=10,
         >>> err
         2.22044604925031e-16
         >>>
-        >>> polyval([4,3,2], roots[0])
+        >>> polyval([2,3,4], roots[0], asc=True)
         (2.22044604925031e-16 + 0.0j)
-        >>> polyval([4,3,2], roots[1])
+        >>> polyval([2,3,4], roots[1], asc=True)
         (2.22044604925031e-16 + 0.0j)
 
     The following example computes all the 5th roots of unity; that is,
     the roots of `x^5 - 1`::
 
         >>> mp.dps = 20
-        >>> for r in polyroots([1, 0, 0, 0, 0, -1]):
+        >>> for r in polyroots([-1, 0, 0, 0, 0, 1], asc=True):
         ...     print(r)
         ...
         1.0
@@ -116,7 +132,7 @@ def polyroots(ctx, coeffs, maxsteps=50, cleanup=True, extraprec=10,
     typically compute all roots of an arbitrary polynomial to high precision::
 
         >>> mp.dps = 60
-        >>> for r in polyroots([1, 0, -10, 0, 1]):
+        >>> for r in polyroots([1, 0, -10, 0, 1], asc=True):
         ...     print(r)
         ...
         -3.14626436994197234232913506571557044551247712918732870123249
@@ -156,17 +172,25 @@ def polyroots(ctx, coeffs, maxsteps=50, cleanup=True, extraprec=10,
         # Constant polynomial with no roots
         return []
 
+    if asc is None:
+        warnings.warn("Descending (wrt powers) order of polynomial "
+                      "coefficients is deprecated, please adapt you "
+                      "code to use ascending order, asc=True.",
+                      DeprecationWarning)
+        asc = False
+        coeffs = coeffs[::-1]
+
     orig = ctx.prec
     tol = +ctx.eps
     with ctx.extraprec(extraprec):
         deg = len(coeffs) - 1
         # Must be monic
-        lead = ctx.convert(coeffs[0])
+        lead = ctx.convert(coeffs[-1])
         if lead == 1:
             coeffs = [ctx.convert(c) for c in coeffs]
         else:
             coeffs = [c/lead for c in coeffs]
-        f = lambda x: ctx.polyval(coeffs, x)
+        f = lambda x: ctx.polyval(coeffs, x, asc=True)
         if roots_init is None:
             roots = [ctx.mpc((0.4+0.9j)**n) for n in range(deg)]
         else:
diff --git a/mpmath/function_docs.py b/mpmath/function_docs.py
index 3ce5409..db483ee 100644
--- a/mpmath/function_docs.py
+++ b/mpmath/function_docs.py
@@ -2141,7 +2141,7 @@
 
     >>> nsum(lambda k: 1/(k**2-2*k+3), [0, inf])
     1.694361433907061256154665
-    >>> nprint(polyroots([1,-2,3]))
+    >>> nprint(polyroots([3,-2,1], asc=True))
     [(1.0 - 1.41421j), (1.0 + 1.41421j)]
     >>> r1 = 1-sqrt(2)*j
     >>> r2 = r1.conjugate()
@@ -5550,7 +5550,7 @@
 on the interval `[-1, 1]`::
 
     >>> for n in range(5):
-    ...     nprint(polyroots(taylor(lambda x: legendre(n, x), 0, n)[::-1]))
+    ...     nprint(polyroots(taylor(lambda x: legendre(n, x), 0, n), asc=True))
     ...
     []
     [0.0]
@@ -7606,7 +7606,7 @@
 can be checked to agree with the list of primitive roots::
 
     >>> p = taylor(lambda x: cyclotomic(6,x), 0, 6)[:3]
-    >>> for r in polyroots(p[::-1]):
+    >>> for r in polyroots(p, asc=True):
     ...     print(r)
     ...
     (0.5 - 0.8660254037844386467637232j)
diff --git a/mpmath/identification.py b/mpmath/identification.py
index 3f31fef..6a6ed0e 100644
--- a/mpmath/identification.py
+++ b/mpmath/identification.py
@@ -3,8 +3,11 @@
 and derivative algorithms for constant recognition.
 """
 
+import warnings
+
 from .libmp import int_types, sqrt_fixed
 
+
 # round to nearest integer (can be done more elegantly...)
 def round_fixed(x, prec):
     return ((x + (1<<(prec-1))) >> prec) << prec
@@ -307,7 +310,7 @@ def pslq(ctx, x, tol=None, maxcoeff=1000, maxsteps=100, verbose=False):
         print("Could not find an integer relation. Norm bound: %s" % norm)
     return None
 
-def findpoly(ctx, x, n=1, **kwargs):
+def findpoly(ctx, x, n=1, asc=None, **kwargs):
     r"""
     ``findpoly(x, n)`` returns the coefficients of an integer
     polynomial `P` of degree at most `n` such that `P(x) \approx 0`.
@@ -324,6 +327,9 @@ def findpoly(ctx, x, n=1, **kwargs):
     For large values of `n`, it is recommended to run :func:`~mpmath.findpoly`
     at high precision; preferably 50 digits or more.
 
+    If *asc=False*, descending order of coefficients is used (the term
+    of largest degree - first).
+
     **Examples**
 
     By default (degree `n = 1`), :func:`~mpmath.findpoly` simply finds a linear
@@ -332,15 +338,15 @@ def findpoly(ctx, x, n=1, **kwargs):
         >>> from mpmath import (mp, findpoly, nprint, polyval, polyroots,
         ...                     sqrt, pi, phi, euler, findroot)
         >>> mp.pretty = True
-        >>> findpoly(0.7)
-        [-10, 7]
+        >>> findpoly(0.7, asc=True)
+        [7, -10]
 
     The generated coefficient list is valid input to ``polyval`` and
     ``polyroots``::
 
-        >>> nprint(polyval(findpoly(phi, 2), phi), 1)
+        >>> nprint(polyval(findpoly(phi, 2, asc=True), phi, asc=True), 1)
         -2.0e-16
-        >>> for r in polyroots(findpoly(phi, 2)):
+        >>> for r in polyroots(findpoly(phi, 2, asc=True), asc=True):
         ...     print(r)
         ...
         -0.618033988749895
@@ -350,15 +356,15 @@ def findpoly(ctx, x, n=1, **kwargs):
     solutions to quadratic equations. As we find here, `1+\sqrt 2`
     is a root of the polynomial `x^2 - 2x - 1`::
 
-        >>> findpoly(1+sqrt(2), 2)
-        [1, -2, -1]
-        >>> findroot(lambda x: x**2 - 2*x - 1, 1)
+        >>> findpoly(1+sqrt(2), 2, asc=True)
+        [-1, -2, 1]
+        >>> findroot(lambda x: x**2 - 2*x - 1, 1, asc=True)
         2.4142135623731
 
     Despite only containing square roots, the following number results
     in a polynomial of degree 4::
 
-        >>> findpoly(sqrt(2)+sqrt(3), 4)
+        >>> findpoly(sqrt(2)+sqrt(3), 4, asc=True)
         [1, 0, -10, 0, 1]
 
     In fact, `x^4 - 10x^2 + 1` is the *minimal polynomial* of
@@ -376,7 +382,7 @@ def findpoly(ctx, x, n=1, **kwargs):
     We can verify that `\pi` is not an algebraic number of degree 3 with
     coefficients less than 1000::
 
-        >>> findpoly(pi, 3)
+        >>> findpoly(pi, 3, asc=True)
         >>>
 
     It is always possible to find an algebraic approximation of a number
@@ -388,12 +394,12 @@ def findpoly(ctx, x, n=1, **kwargs):
 
     One example of each method is shown below::
 
-        >>> findpoly(pi, 4)
-        [95, -545, 863, -183, -298]
-        >>> findpoly(pi, 3, maxcoeff=10000)
-        [836, -1734, -2658, -457]
-        >>> findpoly(pi, 3, tol=1e-7)
-        [-4, 22, -29, -2]
+        >>> findpoly(pi, 4, asc=True)
+        [-298, -183, 863, -545, 95]
+        >>> findpoly(pi, 3, maxcoeff=10000, asc=True)
+        [-457, -2658, -1734, 836]
+        >>> findpoly(pi, 3, tol=1e-7, asc=True)
+        [-2, -29, 22, -4]
 
     It is unknown whether Euler's constant is transcendental (or even
     irrational). We can use :func:`~mpmath.findpoly` to check that if is
@@ -401,7 +407,8 @@ def findpoly(ctx, x, n=1, **kwargs):
     at least 7 and a coefficient of magnitude at least 1000000::
 
         >>> mp.dps = 200
-        >>> findpoly(euler, 6, maxcoeff=10**6, tol=1e-100, maxsteps=1000)
+        >>> findpoly(euler, 6, maxcoeff=10**6, tol=1e-100,
+        ...          maxsteps=1000, asc=True)
         >>>
 
     Note that the high precision and strict tolerance is necessary
@@ -416,12 +423,18 @@ def findpoly(ctx, x, n=1, **kwargs):
         raise ValueError("n cannot be less than 1")
     if x == 0:
         return [1, 0]
+    if asc is None:
+        warnings.warn("Descending (wrt powers) order of polynomial "
+                      "coefficients is deprecated, please adapt you "
+                      "code to use ascending order, asc=True.",
+                      DeprecationWarning)
+        asc = False
     xs = [ctx.mpf(1)]
     for i in range(1,n+1):
         xs.append(x**i)
         a = ctx.pslq(xs, **kwargs)
         if a is not None:
-            return a[::-1]
+            return a if asc else a[::-1]
 
 def fracgcd(p, q):
     x, y = p, q
@@ -818,7 +831,7 @@ def addsolution(s):
         # Watch out for existing fractional powers of fractions
         logs = []
         for a, s in constants:
-            if not sum(bool(ctx.findpoly(ctx.ln(a)/ctx.ln(i),1)) for i in ilogs):
+            if not sum(bool(ctx.findpoly(ctx.ln(a)/ctx.ln(i),1,asc=True)) for i in ilogs):
                 logs.append((ctx.ln(a), s))
         logs = [(ctx.ln(i),str(i)) for i in ilogs] + logs
         r = ctx.pslq([ctx.ln(x)] + [a[0] for a in logs], tol, M)


Test patch:
diff --git a/mpmath/tests/test_calculus.py b/mpmath/tests/test_calculus.py
index d68840a..7d62dd5 100644
--- a/mpmath/tests/test_calculus.py
+++ b/mpmath/tests/test_calculus.py
@@ -7,78 +7,96 @@
 
 def test_approximation():
     f = lambda x: cos(2-2*x)/x
-    p, err = chebyfit(f, [2, 4], 8, error=True)
+    p, err = chebyfit(f, [2, 4], 8, error=True, asc=True)
     assert err < 1e-5
     for i in range(10):
         x = 2 + i/5.
-        assert abs(polyval(p, x) - f(x)) < err
+        assert abs(polyval(p, x, asc=True) - f(x)) < err
+
+def test_chebyfit_deprecated():
+    f = lambda x: cos(2-2*x)/x
+    with pytest.deprecated_call():
+        p, err = chebyfit(f, [2, 4], 8, error=True)
+    assert err < 1e-5
+    p = p[::-1]
+    for i in range(10):
+        x = 2 + i/5.
+        assert abs(polyval(p, x, asc=True) - f(x)) < err
 
 def test_limits():
     assert limit(lambda x: (x-sin(x))/x**3, 0).ae(mpf(1)/6)
     assert limit(lambda n: (1+1/n)**n, inf).ae(e)
 
 def test_polyval():
-    assert polyval([], 3) == 0
-    assert polyval([0], 3) == 0
-    assert polyval([5], 3) == 5
+    assert polyval([], 3, asc=True) == 0
+    assert polyval([0], 3, asc=True) == 0
+    assert polyval([5], 3, asc=True) == 5
     # 4x^3 - 2x + 5
-    p = [4, 0, -2, 5]
-    assert polyval(p,4) == 253
-    assert polyval(p,4,derivative=True) == (253, 190)
+    p = [5, -2, 0, 4]
+    assert polyval(p,4,asc=True) == 253
+    assert polyval(p,4,derivative=True,asc=True) == (253, 190)
+
+def test_polyval_deprecated():
+    with pytest.deprecated_call():
+        p = [4, 0, -2, 5]
+        assert polyval(p,4) == 253
 
 def test_polyroots():
-    p = polyroots([1,-4])
+    p = polyroots([-4,1], asc=True)
     assert p[0].ae(4)
-    p, q = polyroots([1,2,3])
+    p, q = polyroots([3,2,1], asc=True)
     assert p.ae(-1 - sqrt(2)*j)
     assert q.ae(-1 + sqrt(2)*j)
     #this is not a real test, it only tests a specific case
-    assert polyroots([1]) == []
-    pytest.raises(ValueError, lambda: polyroots([0]))
+    assert polyroots([1], asc=True) == []
+    pytest.raises(ValueError, lambda: polyroots([0], asc=True))
+
+def test_polyroots_deprecated():
+    with pytest.deprecated_call():
+        p, q = polyroots([1,2,3])
+        assert p.ae(-1 - sqrt(2)*j)
+        assert q.ae(-1 + sqrt(2)*j)
 
 def test_polyroots_legendre():
     n = 64
-    coeffs = [11975573020964041433067793888190275875, 0,
-        -190100434726484311252477736051902332000, 0,
-        1437919688271127330313741595496589239248, 0,
-        -6897338342113537600691931230430793911840, 0,
-        23556405536185284408974715545252277554280, 0,
-        -60969520211303089058522793175947071316960, 0,
-        124284021969194758465450309166353645376880, 0,
-        -204721258548015217049921875719981284186016, 0,
-        277415422258095841688223780704620656114900, 0,
-        -313237834141273382807123548182995095192800, 0,
-        297432255354328395601259515935229287637200, 0,
-        -239057700565161140389797367947941296605600, 0,
-        163356095386193445933028201431093219347160, 0,
-        -95158890516229191805647495979277603503200, 0,
-        47310254620162038075933656063247634556400, 0,
-        -20071017111583894941305187420771723751200, 0,
-        7255051932731034189479516844750603752850, 0,
-        -2228176940331017311443863996901733412640, 0,
-        579006552594977616773047095969088431600, 0,
-        -126584428502545713788439446082310831200, 0,
-        23112325428835593809686977515028663000, 0,
-        -3491517141958743235617737161547844000, 0,
-        431305058712550634988073414073557200, 0,
-        -42927166660756742088912492757452000, 0,
-        3378527005707706553294038781836500, 0,
-        -205277590220215081719131470288800, 0,
-        9330799555464321896324157740400, 0,
-        -304114948474392713657972548576, 0,
-        6695289961520387531608984680, 0,
-        -91048139350447232095702560, 0,
-        659769125727878493447120, 0,
-        -1905929106580294155360, 0,
-        916312070471295267]
+    coeffs = [916312070471295267, 0, -1905929106580294155360, 0,
+              659769125727878493447120, 0, -91048139350447232095702560, 0,
+              6695289961520387531608984680, 0, -304114948474392713657972548576,
+              0, 9330799555464321896324157740400, 0,
+              -205277590220215081719131470288800, 0,
+              3378527005707706553294038781836500, 0,
+              -42927166660756742088912492757452000, 0,
+              431305058712550634988073414073557200, 0,
+              -3491517141958743235617737161547844000, 0,
+              23112325428835593809686977515028663000, 0,
+              -126584428502545713788439446082310831200, 0,
+              579006552594977616773047095969088431600, 0,
+              -2228176940331017311443863996901733412640, 0,
+              7255051932731034189479516844750603752850, 0,
+              -20071017111583894941305187420771723751200, 0,
+              47310254620162038075933656063247634556400, 0,
+              -95158890516229191805647495979277603503200, 0,
+              163356095386193445933028201431093219347160, 0,
+              -239057700565161140389797367947941296605600, 0,
+              297432255354328395601259515935229287637200, 0,
+              -313237834141273382807123548182995095192800, 0,
+              277415422258095841688223780704620656114900, 0,
+              -204721258548015217049921875719981284186016, 0,
+              124284021969194758465450309166353645376880, 0,
+              -60969520211303089058522793175947071316960, 0,
+              23556405536185284408974715545252277554280, 0,
+              -6897338342113537600691931230430793911840, 0,
+              1437919688271127330313741595496589239248, 0,
+              -190100434726484311252477736051902332000, 0,
+              11975573020964041433067793888190275875]
 
     with mp.workdps(3):
         with pytest.raises(mp.NoConvergence):
             polyroots(coeffs, maxsteps=5, cleanup=True, error=False,
-                      extraprec=n*10)
+                      extraprec=n*10, asc=True)
 
         roots = polyroots(coeffs, maxsteps=50, cleanup=True, error=False,
-                    extraprec=n*10)
+                          extraprec=n*10, asc=True)
         roots = [str(r) for r in roots]
         assert roots == \
             ['-0.999', '-0.996', '-0.991', '-0.983', '-0.973', '-0.961',
@@ -94,39 +112,36 @@ def test_polyroots_legendre():
 
 def test_polyroots_legendre_init():
     extra_prec = 100
-    coeffs = [11975573020964041433067793888190275875, 0,
-        -190100434726484311252477736051902332000, 0,
-        1437919688271127330313741595496589239248, 0,
-        -6897338342113537600691931230430793911840, 0,
-        23556405536185284408974715545252277554280, 0,
-        -60969520211303089058522793175947071316960, 0,
-        124284021969194758465450309166353645376880, 0,
-        -204721258548015217049921875719981284186016, 0,
-        277415422258095841688223780704620656114900, 0,
-        -313237834141273382807123548182995095192800, 0,
-        297432255354328395601259515935229287637200, 0,
-        -239057700565161140389797367947941296605600, 0,
-        163356095386193445933028201431093219347160, 0,
-        -95158890516229191805647495979277603503200, 0,
-        47310254620162038075933656063247634556400, 0,
-        -20071017111583894941305187420771723751200, 0,
-        7255051932731034189479516844750603752850, 0,
-        -2228176940331017311443863996901733412640, 0,
-        579006552594977616773047095969088431600, 0,
-        -126584428502545713788439446082310831200, 0,
-        23112325428835593809686977515028663000, 0,
-        -3491517141958743235617737161547844000, 0,
-        431305058712550634988073414073557200, 0,
-        -42927166660756742088912492757452000, 0,
-        3378527005707706553294038781836500, 0,
-        -205277590220215081719131470288800, 0,
-        9330799555464321896324157740400, 0,
-        -304114948474392713657972548576, 0,
-        6695289961520387531608984680, 0,
-        -91048139350447232095702560, 0,
-        659769125727878493447120, 0,
-        -1905929106580294155360, 0,
-        916312070471295267]
+    coeffs = [916312070471295267, 0, -1905929106580294155360, 0,
+              659769125727878493447120, 0, -91048139350447232095702560, 0,
+              6695289961520387531608984680, 0, -304114948474392713657972548576,
+              0, 9330799555464321896324157740400, 0,
+              -205277590220215081719131470288800, 0,
+              3378527005707706553294038781836500, 0,
+              -42927166660756742088912492757452000, 0,
+              431305058712550634988073414073557200, 0,
+              -3491517141958743235617737161547844000, 0,
+              23112325428835593809686977515028663000, 0,
+              -126584428502545713788439446082310831200, 0,
+              579006552594977616773047095969088431600, 0,
+              -2228176940331017311443863996901733412640, 0,
+              7255051932731034189479516844750603752850, 0,
+              -20071017111583894941305187420771723751200, 0,
+              47310254620162038075933656063247634556400, 0,
+              -95158890516229191805647495979277603503200, 0,
+              163356095386193445933028201431093219347160, 0,
+              -239057700565161140389797367947941296605600, 0,
+              297432255354328395601259515935229287637200, 0,
+              -313237834141273382807123548182995095192800, 0,
+              277415422258095841688223780704620656114900, 0,
+              -204721258548015217049921875719981284186016, 0,
+              124284021969194758465450309166353645376880, 0,
+              -60969520211303089058522793175947071316960, 0,
+              23556405536185284408974715545252277554280, 0,
+              -6897338342113537600691931230430793911840, 0,
+              1437919688271127330313741595496589239248, 0,
+              -190100434726484311252477736051902332000, 0,
+              11975573020964041433067793888190275875]
 
     roots_init =  matrix(['-0.999', '-0.996',  '-0.991', '-0.983', '-0.973',
                           '-0.961', '-0.946',  '-0.93',  '-0.911', '-0.889',
@@ -143,15 +158,16 @@ def test_polyroots_legendre_init():
                           '0.983',   '0.991',   '0.996',  '0.999',  '1.0'])
     with mp.workdps(2*mp.dps):
         roots_exact = polyroots(coeffs, maxsteps=50, cleanup=True, error=False,
-                                extraprec=2*extra_prec)
+                                extraprec=2*extra_prec, asc=True)
     with pytest.raises(mp.NoConvergence):
         polyroots(coeffs, maxsteps=5, cleanup=True, error=False,
-                  extraprec=extra_prec)
+                  extraprec=extra_prec, asc=True)
     roots,err = polyroots(coeffs, maxsteps=5, cleanup=True, error=True,
-                          extraprec=extra_prec,roots_init=roots_init)
+                          extraprec=extra_prec,roots_init=roots_init, asc=True)
     assert max(matrix(roots_exact)-matrix(roots).apply(abs)) < err
     roots1,err1 = polyroots(coeffs, maxsteps=25, cleanup=True, error=True,
-                            extraprec=extra_prec,roots_init=roots_init[:60])
+                            extraprec=extra_prec,roots_init=roots_init[:60],
+                            asc=True)
     assert max(matrix(roots_exact)-matrix(roots1).apply(abs)) < err1
 
 def test_pade():
@@ -165,8 +181,8 @@ def test_pade():
         a.append(one/k)
     p, q = pade(a, N//2, N//2)
     for x in arange(0, 1, 0.1):
-        r = polyval(p[::-1], x)/polyval(q[::-1], x)
-        assert(r.ae(exp(x), 1.0e-10))
+        r = polyval(p, x, asc=True)/polyval(q, x, asc=True)
+        assert r.ae(exp(x), 1.0e-10)
 
 def test_fourier():
     c, s = fourier(lambda x: x+1, [-1, 2], 2)
diff --git a/mpmath/tests/test_identify.py b/mpmath/tests/test_identify.py
index eb9cadb..3aae861 100644
--- a/mpmath/tests/test_identify.py
+++ b/mpmath/tests/test_identify.py
@@ -1,4 +1,6 @@
-from mpmath import e, exp, identify, log, mp, pi, pslq, zeta
+import pytest
+
+from mpmath import e, exp, findpoly, identify, log, mp, pi, pslq, sqrt, zeta
 
 
 def test_pslq():
@@ -17,3 +19,7 @@ def test_identify():
     assert identify(3, full=True) == ['3', '3', '1/(1/3)', 'sqrt(9)',
         '1/sqrt((1/9))', '(sqrt(12)/2)**2', '1/(sqrt(12)/6)**2']
     assert identify(pi+1, {'a':+pi}) == '(1 + 1*a)'
+
+def test_findpoly_deprecated():
+    with pytest.deprecated_call():
+        assert findpoly(1+sqrt(2), 2) == [1, -2, -1]
diff --git a/mpmath/tests/test_rootfinding.py b/mpmath/tests/test_rootfinding.py
index 1689e34..b92d98b 100644
--- a/mpmath/tests/test_rootfinding.py
+++ b/mpmath/tests/test_rootfinding.py
@@ -51,7 +51,7 @@ def test_bisection():
     assert findroot(lambda x: x**2-1,(0,2),solver='bisect') == 1
 
 def test_mnewton():
-    f = lambda x: polyval([1,3,3,1],x)
+    f = lambda x: polyval([1,3,3,1],x,asc=True)
     x = findroot(f, -0.9, solver='mnewton')
     assert abs(f(x)) < eps