求解 y 的多项式

发布于 2024-10-06 14:17:26 字数 166 浏览 7 评论 0原文

我正在接受一个函数(例如 y = x**2)并且需要求解 x。我知道我可以煞费苦心地手动解决这个问题,但我正在尝试寻找一种可以使用的方法。我浏览过 numpy、scipy 和 sympy,但似乎找不到我要找的东西。目前,我正在从该函数中创建一个 lambda,因此如果我能够保留该方法的格式,那就太好了,但不是必需的。

I'm taking in a function (e.g. y = x**2) and need to solve for x. I know I can painstakingly solve this manually, but I'm trying to find instead a method to use. I've browsed numpy, scipy and sympy, but can't seem to find what I'm looking for. Currently I'm making a lambda out of the function so it'd be nice if I'm able to keep that format for the the method, but not necessary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情话难免假 2024-10-13 14:17:26

如果您正在寻找数值解决方案(即只对数字感兴趣,而不是符号封闭形式解决方案),那么 SciPy.optimize 模块。对于简单的事情, newton< /a> 对于简单的多项式来说是一个很好的开始,但是你可以从那里开始。

对于符号解(也就是说得到 y = x**2 -> x = +/- sqrt(y)) SymPy 求解器 大致为您提供所需的内容。整个 SymPy 包旨在进行符号操作。

以下是使用 Python 解释器求解问题中提到的方程的示例。您需要确保安装了 SymPy 软件包,然后:

>>>> from sympy import * # we are importing everything for ease of use
>>>> x = Symbol("x")
>>>> y = Symbol("y")     # create the two variables
>>>> equation = Eq(x ** 2, y) # create the equation
>>>> solve(equation, x)
[y**(1/2), -y**(1/2)]

正如您所看到的,即使作为交互式代数系统,基础知识也是相当可行的。不像 Mathematica 那样好,但话又说回来,它是免费的,您可以将其合并到您的自己的程序。请务必阅读陷阱和陷阱部分有关如何编码适当方程的 SymPy 文档。

如果这一切都是为了获得方程的快速而肮脏的解决方案,那么总是有 Wolfram Alpha

If you are looking for numerical solutions (i.e. just interested in the numbers, not the symbolic closed form solutions), then there are a few options for you in the SciPy.optimize module. For something simple, the newton is a pretty good start for simple polynomials, but you can take it from there.

For symbolic solutions (which is to say to get y = x**2 -> x = +/- sqrt(y)) SymPy solver gives you roughly what you need. The whole SymPy package is directed at doing symbolic manipulation.

Here is an example using the Python interpreter to solve the equation that is mentioned in the question. You will need to make sure that SymPy package is installed, then:

>>>> from sympy import * # we are importing everything for ease of use
>>>> x = Symbol("x")
>>>> y = Symbol("y")     # create the two variables
>>>> equation = Eq(x ** 2, y) # create the equation
>>>> solve(equation, x)
[y**(1/2), -y**(1/2)]

As you see the basics are fairly workable, even as an interactive algebra system. Not nearly as nice as Mathematica, but then again, it is free and you can incorporate it into your own programs. Make sure to read the Gotchas and Pitfalls section of the SymPy documentation on how to encode the appropriate equations.

If all this was to get a quick and dirty solutions to equations then there is always Wolfram Alpha.

ㄟ。诗瑗 2024-10-13 14:17:26

通过 scipy.optimize.newton。它找到方程的根,即 f(x) = 0 时的 x 值。在本例中,您可以将问题转化为寻找函数 f(x) = x² - y。如果您提供计算 y 的 lambda,则可以提供通用解决方案:

def inverse(f, f_prime=None):
    def solve(y):
        return newton(lambda x: f(x) - y, 1, f_prime, (), 1E-10, 1E6)
    return solve

使用此函数非常简单:

>>> sqrt = inverse(lambda x: x**2)
>>> sqrt(2)
1.4142135623730951
>>> import math
>>> math.sqrt(2)
1.4142135623730951

根据输入函数,您可能需要将参数调整为 newton( )。当前版本使用的起始猜测值为 1,容差为 10-10,最大迭代次数为 106

为了进一步提高速度,您可以提供相关函数的导数:

>>> sqrt = inverse(lambda x: x**2, lambda x: 2*x)

事实上,如果没有它,该函数实际上使用割线法而不是牛顿拉夫森法,后者依赖于知道导数。

Use Newton-Raphson via scipy.optimize.newton. It finds roots of an equation, i.e., values of x for which f(x) = 0. In the example, you can cast the problem as looking for a root of the function f(x) = x² - y. If you supply a lambda that computes y, you can provide a general solution thus:

def inverse(f, f_prime=None):
    def solve(y):
        return newton(lambda x: f(x) - y, 1, f_prime, (), 1E-10, 1E6)
    return solve

Using this function is quite simple:

>>> sqrt = inverse(lambda x: x**2)
>>> sqrt(2)
1.4142135623730951
>>> import math
>>> math.sqrt(2)
1.4142135623730951

Depending on the input function, you may need to tune the parameters to newton(). The current version uses a starting guess of 1, a tolerance of 10-10 and a maximum iteration count of 106.

For an additional speed-up, you can supply the derivative of the function in question:

>>> sqrt = inverse(lambda x: x**2, lambda x: 2*x)

In fact, without it, the function actually uses the secant method instead of Newton-Raphson, which relies on knowing the derivative.

叫嚣ゝ 2024-10-13 14:17:26

查看 SymPy,特别是 求解器

Check out SymPy, specifically the solver.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文