{"id":977912,"date":"2024-12-27T06:33:28","date_gmt":"2024-12-26T22:33:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977912.html"},"modified":"2024-12-27T06:33:30","modified_gmt":"2024-12-26T22:33:30","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e9%ab%98%e9%98%b6%e6%96%b9%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/977912.html","title":{"rendered":"python\u5982\u4f55\u6c42\u9ad8\u9636\u65b9\u7a0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203136\/11650144-133f-46d6-aad1-f463bda60e68.webp\" alt=\"python\u5982\u4f55\u6c42\u9ad8\u9636\u65b9\u7a0b\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u6c42\u89e3\u9ad8\u9636\u65b9\u7a0b\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528NumPy\u5e93\u63d0\u4f9b\u7684roots\u51fd\u6570\u3001\u5229\u7528SymPy\u5e93\u4e2d\u7684solve\u51fd\u6570\u3001\u5e94\u7528SciPy\u5e93\u4e2d\u7684optimize\u6a21\u5757\u3002NumPy\u548cSymPy\u4fa7\u91cd\u4e8e\u4ee3\u6570\u65b9\u6cd5\uff0c\u800cSciPy\u5219\u5e38\u7528\u4e8e\u6570\u503c\u89e3\u6cd5\u3002<\/strong>\u5176\u4e2d\uff0cSymPy\u5e93\u7684solve\u51fd\u6570\u975e\u5e38\u7075\u6d3b\uff0c\u5b83\u80fd\u591f\u5904\u7406\u7b26\u53f7\u8868\u8fbe\u5f0f\u5e76\u63d0\u4f9b\u7cbe\u786e\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001NUMPY\u5e93\u4e2d\u7684ROOTS\u51fd\u6570<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u9ad8\u6548\u7684\u6570\u5b66\u51fd\u6570\uff0c\u5176\u4e2d\u5305\u62ec\u5904\u7406\u591a\u9879\u5f0f\u65b9\u7a0b\u7684\u529f\u80fd\u3002NumPy\u7684<code>roots<\/code>\u51fd\u6570\u80fd\u591f\u6c42\u89e3\u591a\u9879\u5f0f\u65b9\u7a0b\u7684\u6839\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u591a\u9879\u5f0f\u7684\u7cfb\u6570\u6570\u7ec4\uff0c\u5e76\u8fd4\u56de\u6240\u6709\u6839\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u5bfc\u5165NumPy<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8fd9\u4e2a\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u811a\u672c\u6216\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u5bfc\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528roots\u51fd\u6570\u6c42\u89e3\u9ad8\u9636\u65b9\u7a0b<\/strong><\/p>\n<\/p>\n<p><p><code>roots<\/code>\u51fd\u6570\u7528\u4e8e\u6c42\u89e3\u591a\u9879\u5f0f\u65b9\u7a0b\u3002\u5047\u8bbe\u6211\u4eec\u8981\u89e3\u51b3\u4e00\u4e2a\u56db\u6b21\u65b9\u7a0b\uff1a<\/p>\n<\/p>\n<p><p>[<\/p>\n<p>x^4 &#8211; 3x^3 + 2x^2 + x &#8211; 5 = 0<\/p>\n<p>]<\/p>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u7a0b\u53ef\u4ee5\u7528\u7cfb\u6570\u6570\u7ec4\u8868\u793a\u4e3a<code>[1, -3, 2, 1, -5]<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>roots<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">coefficients = [1, -3, 2, 1, -5]<\/p>\n<p>roots = np.roots(coefficients)<\/p>\n<p>print(&quot;Roots:&quot;, roots)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>roots<\/code>\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6839\u7684\u6570\u7ec4\u3002\u5bf9\u4e8e\u9ad8\u9636\u65b9\u7a0b\uff0c\u7ed3\u679c\u53ef\u80fd\u5305\u542b\u590d\u6570\u6839\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001SYMPY\u5e93\u4e2d\u7684SOLVE\u51fd\u6570<\/p>\n<\/p>\n<p><p>SymPy\u662f\u4e00\u4e2a\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u8ba1\u7b97\u7684Python\u5e93\u3002\u5b83\u80fd\u591f\u5904\u7406\u7b26\u53f7\u4ee3\u6570\u5e76\u8fd4\u56de\u7cbe\u786e\u89e3\u3002\u4f7f\u7528SymPy\u53ef\u4ee5\u6c42\u89e3\u591a\u9879\u5f0f\u65b9\u7a0b\u3001\u4ee3\u6570\u65b9\u7a0b\u3001\u5fae\u5206\u65b9\u7a0b\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u5bfc\u5165SymPy<\/strong><\/p>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5SymPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sympy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5bfc\u5165SymPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528solve\u51fd\u6570\u6c42\u89e3\u9ad8\u9636\u65b9\u7a0b<\/strong><\/p>\n<\/p>\n<p><p><code>solve<\/code>\u51fd\u6570\u7528\u4e8e\u6c42\u89e3\u7b26\u53f7\u65b9\u7a0b\u3002\u5047\u8bbe\u6211\u4eec\u4ecd\u7136\u8981\u89e3\u51b3\u4e0a\u9762\u7684\u56db\u6b21\u65b9\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = symbols(&#39;x&#39;)<\/p>\n<p>equation = x&lt;strong&gt;4 - 3*x&lt;\/strong&gt;3 + 2*x2 + x - 5<\/p>\n<p>roots = solve(equation, x)<\/p>\n<p>print(&quot;Roots:&quot;, roots)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>solve<\/code>\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6839\u7684\u5217\u8868\uff0c\u53ef\u80fd\u5305\u62ec\u5206\u6570\u548c\u7b26\u53f7\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001SCIPY\u5e93\u4e2d\u7684OPTIMIZE\u6a21\u5757<\/p>\n<\/p>\n<p><p>SciPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u548c\u5de5\u7a0b\u8ba1\u7b97\u7684Python\u5e93\uff0c\u5176\u4e2d\u7684<code>optimize<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f88\u591a\u6570\u503c\u4f18\u5316\u548c\u6c42\u89e3\u5de5\u5177\u3002\u5bf9\u4e8e\u975e\u7ebf\u6027\u65b9\u7a0b\u7ec4\u6216\u9700\u8981\u6570\u503c\u89e3\u7684\u65b9\u7a0b\uff0cSciPy\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u5bfc\u5165SciPy<\/strong><\/p>\n<\/p>\n<p><p>\u5b89\u88c5SciPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scipy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.optimize import fsolve<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528fsolve\u51fd\u6570\u6c42\u89e3\u9ad8\u9636\u65b9\u7a0b<\/strong><\/p>\n<\/p>\n<p><p><code>fsolve<\/code>\u51fd\u6570\u7528\u4e8e\u627e\u5230\u51fd\u6570\u7684\u6839\u3002\u5b83\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u548c\u521d\u59cb\u731c\u6d4b\u503c\u3002\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u8868\u793a\u65b9\u7a0b\uff0c\u4f7f\u7528<code>fsolve<\/code>\u6765\u627e\u5230\u5176\u6839\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def equation(x):<\/p>\n<p>    return x&lt;strong&gt;4 - 3*x&lt;\/strong&gt;3 + 2*x2 + x - 5<\/p>\n<p>initial_guess = [0, 1, 2, 3]  # \u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u521d\u59cb\u731c\u6d4b\u503c<\/p>\n<p>roots = fsolve(equation, initial_guess)<\/p>\n<p>print(&quot;Roots:&quot;, roots)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>fsolve<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u5305\u542b\u65b9\u7a0b\u7684\u6570\u503c\u89e3\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u7ed3\u8bba<\/p>\n<\/p>\n<p><p>\u5728\u6c42\u89e3\u9ad8\u9636\u65b9\u7a0b\u65f6\uff0c\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002\u5982\u679c\u9700\u8981\u7cbe\u786e\u89e3\uff0cSymPy\u7684<code>solve<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff1b\u5982\u679c\u9700\u8981\u5feb\u901f\u6570\u503c\u89e3\uff0cSciPy\u7684<code>fsolve<\/code>\u6216NumPy\u7684<code>roots<\/code>\u51fd\u6570\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u65b9\u7a0b\u6216\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u60c5\u5883\uff0cNumPy\u548cSciPy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u8ba1\u7b97\u5de5\u5177\u3002\u4e0d\u540c\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u4e86\u89e3\u5b83\u4eec\u7684\u7279\u6027\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u505a\u51fa\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6c42\u89e3\u9ad8\u9636\u65b9\u7a0b\u7684\u6839\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u7684<code>roots<\/code>\u51fd\u6570\u6765\u6c42\u89e3\u9ad8\u9636\u65b9\u7a0b\u7684\u6839\u3002\u9996\u5148\u9700\u8981\u5c06\u65b9\u7a0b\u7684\u7cfb\u6570\u4ee5\u964d\u5e42\u7684\u987a\u5e8f\u653e\u5165\u4e00\u4e2a\u5217\u8868\u6216\u6570\u7ec4\u4e2d\uff0c<code>numpy.roots()<\/code>\u5c06\u8fd4\u56de\u65b9\u7a0b\u7684\u6240\u6709\u6839\uff0c\u5305\u62ec\u590d\u6570\u6839\u3002\u4f8b\u5982\uff0c\u6c42\u89e3\u65b9\u7a0b (x^3 &#8211; 6x^2 + 11x &#8211; 6 = 0) \u7684\u6839\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ncoefficients = [1, -6, 11, -6]\nroots = np.roots(coefficients)\nprint(roots)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u80fd\u5f97\u5230\u65b9\u7a0b\u7684\u6240\u6709\u6839\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u590d\u6742\u7684\u9ad8\u9636\u65b9\u7a0b\uff1f<\/strong><br \/>\u5982\u679c\u65b9\u7a0b\u7684\u7cfb\u6570\u8f83\u590d\u6742\u6216\u8005\u662f\u7b26\u53f7\u5f62\u5f0f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sympy<\/code>\u5e93\u3002<code>sympy<\/code>\u63d0\u4f9b\u4e86\u7b26\u53f7\u8ba1\u7b97\u80fd\u529b\uff0c\u80fd\u591f\u5904\u7406\u66f4\u590d\u6742\u7684\u65b9\u7a0b\u3002\u4f7f\u7528<code>solve()<\/code>\u51fd\u6570\u53ef\u4ee5\u627e\u5230\u9ad8\u9636\u65b9\u7a0b\u7684\u89e3\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from sympy import symbols, solve\n\nx = symbols(&#39;x&#39;)\nequation = x&lt;strong&gt;3 - 6*x&lt;\/strong&gt;2 + 11*x - 6\nsolutions = solve(equation, x)\nprint(solutions)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u5f97\u5230\u89e3\uff0c\u8fd8\u80fd\u4ee5\u7b26\u53f7\u5f62\u5f0f\u5c55\u793a\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5728\u6c42\u89e3\u9ad8\u9636\u65b9\u7a0b\u65f6\u5982\u4f55\u5904\u7406\u591a\u91cd\u6839\uff1f<\/strong><br \/>\u5728\u9ad8\u9636\u65b9\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u591a\u91cd\u6839\u7684\u60c5\u51b5\u3002\u4f7f\u7528<code>numpy<\/code>\u65f6\uff0c<code>roots<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u6240\u6709\u6839\uff0c\u5305\u62ec\u591a\u91cd\u6839\uff0c\u4f46\u5982\u679c\u9700\u8981\u4e86\u89e3\u6839\u7684\u91cd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.polyder()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5bfc\u6570\u3002\u4f8b\u5982\uff0c\u6c42\u89e3\u6839\u7684\u91cd\u6570\u65f6\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ncoefficients = [1, -6, 11, -6]\nroots = np.roots(coefficients)\nderivative = np.polyder(coefficients)\nderivative_roots = np.roots(derivative)\n\nfor root in roots:\n    multiplicity = np.sum(np.isclose(root, derivative_roots))\n    print(f&quot;Root: {root}, Multiplicity: {multiplicity + 1}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u80fd\u591f\u6e05\u6670\u5730\u4e86\u89e3\u6bcf\u4e2a\u6839\u7684\u91cd\u6570\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u6c42\u89e3\u9ad8\u9636\u65b9\u7a0b\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528NumPy\u5e93\u63d0\u4f9b\u7684roots\u51fd\u6570\u3001\u5229\u7528SymPy\u5e93\u4e2d\u7684solv [&hellip;]","protected":false},"author":3,"featured_media":977918,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977912"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=977912"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977912\/revisions"}],"predecessor-version":[{"id":977919,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977912\/revisions\/977919"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/977918"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=977912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=977912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=977912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}