skip to Main Content

I current use the numpy roots function as follows:

y = numpy.roots([-2, 3, 0, -1 * x])[1]

In the above x is a column in a table that I extract to run through the function.

If possible I’d like to move this into a MySQL query. I’ve tried replicating the nuts and bolts of the function but quickly got lost when I got to eigenvalues.

I’m not even close to being a mathmatician so I’m wondering if anyone here has attempted this?

2

Answers


  1. Since MySQL is primarily designed for database operations, calculating the roots of a polynomial directly in a MySQL query does not seem very practical. Although, by using a SLF(Server Loadable Function) this should allow you to write a custom function in a another language like C or C++, then integrate said function into MySQL.

    Read more about SLF
    Here
    Or
    Here

    Login or Signup to reply.
  2. For polynomials of small order, you might be able to do it simply by using the general solution formulas. Order 1 is trivial, order two uses the good old quadratic formula we were all taught at school. Your example appears to be cubic polynomial. There is a general formula for the roots of

    ax^3 + bx^2 + cx + d = 0
    

    given by (courtesy of Wikipedia)

    cube root formula part1/3

    cube root formula part2/3

    cube root formula part3/3

    There are 1 to 3 real roots of a general cubic polynomial. The formula above can generate all roots (real and complex) if you multiply C by the cube roots of unity 1, (sqrt(-3) – 1)/2, (-sqrt(-3) – 1)/2. Mysql doesn’t support complex numbers so you’ll have to expand it all out. I concur with gitg0 that this isn’t a very practical way to approach this problem, but it looks doable with some work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search