py_Numpy update 2: Polynomial functions

Continuing from the previous post, this post looks at the polynomial functions in the revised py_Numpy spreadsheet. The updated code and spreadsheet can be downloaded from:

 py_SciPy.zip

The available functions call the Numpy functions for solution and evaluation of polynomials, plus some additional functions with closed-form solutions for polynomials of up to quartic order. The available functions for finding the roots of polynomial equations are shown below (click on any image for full-size view):

Numpy iterative functions:

  • py_PolyRoots: Find the real and complex roots of a polynomial equation, calling the Numpy polyroots function. Coefficients are listed in increasing order of x.
  • py_PolyRoots: Provides an option for the order of coefficients. By default list in decrasing powers of x
  • py_PolyRootsC: Accepts complex values for the input coefficients. Also has an option to list coefficients in descending powers of x, default = ascending powers.
  • py_Polyroots2: Find the real and complex roots of a polynomial equation, calling the Numpy Polynomial function and .roots method.

Closed-form functions:

  • py_Quadratic: Find the real and complex roots of a quadratic equation: a x^2 + b x + c = 0
  • py_Cubic: Find the real roots of a cubic equation: a x^3 + b x^2 + c x + d = 0
  • py_CubicC: Find the real and complex roots of a cubic equation: a x^3 + b x^2 + c x + d = 0
  • py_CubicT: Find the real roots of a cubic equation: a x^3 + b x^2 + c x + d = 0, alternative solution method.
  • py_Quartic:  Find the real and complex roots of a quartic equation: a x^4 + b x^3 + c x62 + d X + e = 0

Calculation times for 100,000 iterations are shown in the screenshot below, with plain Python code, and with the Numba JIT compiler.

More functions:

  • py_PolyFromRoots: Return the polynomial with the given roots
  • py_PolyVal Evaluate a polynomial for a given x value
  • py_PolyFit Fit a polynomial function to XY data
  • py_PolyCompanion Returns the companion matrix to an array of polynomial coefficients in ascending order
  • py_PolyDer Differentiate a polynomial
  • py_PolyInt Integrate a polynomial
  • py_PolyAdd Add one polynomial to another
  • py_PolySub Subtract one polynomial from another
  • py_PolyMul Multiply one polynomial by another
  • py_PolyDiv Divide one polynomial by another
  • py_PolyPow Raise a polynomial to a power
This entry was posted in Curve fitting, Excel, Link to Python, Maths, Newton, NumPy and SciPy, PyXLL, UDFs and tagged , , , , , , . Bookmark the permalink.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.