英文:
Calculate roots of polynomial in MySQL
问题
我目前使用numpy的roots
函数如下:
y = numpy.roots([-2, 3, 0, -1 * x])[1]
在上面的代码中,x
是从表中提取出来的一列数据,然后通过该函数进行计算。
如果可能的话,我想将这个操作移到一个MySQL查询中。我尝试复制该函数的一些基本操作,但当涉及到特征值时很快就迷失了。
我甚至离数学家还差得远,所以我想知道这里是否有人尝试过这个?
英文:
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?
答案1
得分: 1
MySQL主要设计用于数据库操作,直接在MySQL查询中计算多项式的根似乎不太实际。但是,通过使用SLF(服务器可加载函数),您可以在另一种语言如C或C++中编写自定义函数,然后将该函数集成到MySQL中。
英文:
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.
答案2
得分: 1
以下是翻译好的部分:
对于低阶多项式,您可以尝试使用一般解的公式来解决。一阶多项式很简单,二阶多项式可以使用我们在学校学到的二次公式来解决。您的示例似乎是三次多项式。有一个关于根的一般公式如下:
ax^3 + bx^2 + cx + d = 0
该公式由维基百科提供,如下:
一般来说,一般三次多项式有1到3个实根。上述公式可以生成所有根(实根和复根),如果您将C乘以1和复数单位的立方根,即1,(sqrt(-3) - 1)/2,(-sqrt(-3) - 1)/2。MySQL不支持复数,因此您需要将其全部展开。我同意gitg0的观点,这并不是解决这个问题的非常实际的方法,但通过一些工作,看起来是可行的。
英文:
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)
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论