{"id":1144802,"date":"2025-01-08T23:02:51","date_gmt":"2025-01-08T15:02:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1144802.html"},"modified":"2025-01-08T23:02:55","modified_gmt":"2025-01-08T15:02:55","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e8%a7%a3%e4%b8%80%e5%85%83%e4%ba%8c%e6%ac%a1%e6%96%b9%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1144802.html","title":{"rendered":"python\u5982\u4f55\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181720\/3d7002f2-2ca3-47f5-989f-5f9521a35ff8.webp\" alt=\"python\u5982\u4f55\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\" \/><\/p>\n<p><p> <strong>Python\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u3001NumPy\u5e93\u3001SymPy\u5e93\u7b49\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e8c\u6b21\u65b9\u7a0b\u7684\u6c42\u6839\u516c\u5f0f\uff0c\u5229\u7528Python\u7684\u6570\u5b66\u5e93\u6765\u5b9e\u73b0\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u6c42\u89e3<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u6c42\u6839\u516c\u5f0f\uff0c\u8ba1\u7b97\u7b80\u5355\u3001\u7ed3\u679c\u7cbe\u786e<\/strong>\u3002\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u6807\u51c6\u5f62\u5f0f\u4e3a\uff1aax\u00b2 + bx + c = 0\uff0c\u5176\u6c42\u6839\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ x = \\frac{{-b \\pm \\sqrt{{b^2 &#8211; 4ac}}}}{{2a}} ]<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def solve_quadratic(a, b, c):<\/p>\n<p>    # \u8ba1\u7b97\u5224\u522b\u5f0f<\/p>\n<p>    discriminant = b2 - 4*a*c<\/p>\n<p>    if discriminant &gt;= 0:<\/p>\n<p>        root1 = (-b + math.sqrt(discriminant)) \/ (2*a)<\/p>\n<p>        root2 = (-b - math.sqrt(discriminant)) \/ (2*a)<\/p>\n<p>        return root1, root2<\/p>\n<p>    else:<\/p>\n<p>        return None  # \u65e0\u5b9e\u6570\u6839<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>a, b, c = 1, -3, 2<\/p>\n<p>roots = solve_quadratic(a, b, c)<\/p>\n<p>if roots:<\/p>\n<p>    print(f&quot;\u65b9\u7a0b\u7684\u6839\u4e3a: {roots[0]} \u548c {roots[1]}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8be5\u65b9\u7a0b\u65e0\u5b9e\u6570\u6839&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u6c42\u89e3<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u5b66\u51fd\u6570\u6765\u7b80\u5316\u8ba1\u7b97\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u591a\u9879\u5f0f\u7684\u6839\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def solve_quadratic_numpy(a, b, c):<\/p>\n<p>    coefficients = [a, b, c]<\/p>\n<p>    roots = np.roots(coefficients)<\/p>\n<p>    return roots<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>a, b, c = 1, -3, 2<\/p>\n<p>roots = solve_quadratic_numpy(a, b, c)<\/p>\n<p>print(f&quot;\u65b9\u7a0b\u7684\u6839\u4e3a: {roots[0]} \u548c {roots[1]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528SymPy\u5e93\u6c42\u89e3<\/p>\n<\/p>\n<p><p>SymPy\u662f\u4e00\u4e2a\u4e13\u4e3a\u7b26\u53f7\u8ba1\u7b97\u8bbe\u8ba1\u7684Python\u5e93\uff0c\u5b83\u53ef\u4ee5\u5904\u7406\u4ee3\u6570\u65b9\u7a0b\u3001\u5fae\u79ef\u5206\u7b49\u590d\u6742\u6570\u5b66\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, Eq, solve<\/p>\n<p>def solve_quadratic_sympy(a, b, c):<\/p>\n<p>    x = symbols(&#39;x&#39;)<\/p>\n<p>    equation = Eq(a*x2 + b*x + c, 0)<\/p>\n<p>    roots = solve(equation, x)<\/p>\n<p>    return roots<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>a, b, c = 1, -3, 2<\/p>\n<p>roots = solve_quadratic_sympy(a, b, c)<\/p>\n<p>print(f&quot;\u65b9\u7a0b\u7684\u6839\u4e3a: {roots}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528Scipy\u5e93\u6c42\u89e3<\/p>\n<\/p>\n<p><p>SciPy\u5e93\u63d0\u4f9b\u4e86\u66f4\u52a0\u4e30\u5bcc\u7684\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\uff0c\u5305\u62ec\u6570\u503c\u79ef\u5206\u3001\u4f18\u5316\u3001\u6c42\u89e3\u5fae\u5206\u65b9\u7a0b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.optimize import fsolve<\/p>\n<p>def quadratic_function(x, a, b, c):<\/p>\n<p>    return a*x2 + b*x + c<\/p>\n<p>def solve_quadratic_scipy(a, b, c):<\/p>\n<p>    root1 = fsolve(quadratic_function, x0=0, args=(a, b, c))[0]<\/p>\n<p>    root2 = fsolve(quadratic_function, x0=1, args=(a, b, c))[0]<\/p>\n<p>    return root1, root2<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>a, b, c = 1, -3, 2<\/p>\n<p>roots = solve_quadratic_scipy(a, b, c)<\/p>\n<p>print(f&quot;\u65b9\u7a0b\u7684\u6839\u4e3a: {roots[0]} \u548c {roots[1]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u65f6\uff0c\u9700\u8981\u8003\u8651\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u5982\u5224\u522b\u5f0f\u4e3a\u96f6\u548c\u8d1f\u6570\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5224\u522b\u5f0f\u4e3a\u96f6<\/strong>\uff1a\u65b9\u7a0b\u6709\u4e00\u4e2a\u5b9e\u6570\u6839\u3002<\/li>\n<li><strong>\u5224\u522b\u5f0f\u4e3a\u8d1f\u6570<\/strong>\uff1a\u65b9\u7a0b\u65e0\u5b9e\u6570\u6839\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def solve_quadratic_special_cases(a, b, c):<\/p>\n<p>    discriminant = b2 - 4*a*c<\/p>\n<p>    if discriminant &gt; 0:<\/p>\n<p>        root1 = (-b + math.sqrt(discriminant)) \/ (2*a)<\/p>\n<p>        root2 = (-b - math.sqrt(discriminant)) \/ (2*a)<\/p>\n<p>        return root1, root2<\/p>\n<p>    elif discriminant == 0:<\/p>\n<p>        root = -b \/ (2*a)<\/p>\n<p>        return root, root<\/p>\n<p>    else:<\/p>\n<p>        return None  # \u65e0\u5b9e\u6570\u6839<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>a, b, c = 1, -2, 1<\/p>\n<p>roots = solve_quadratic_special_cases(a, b, c)<\/p>\n<p>if roots:<\/p>\n<p>    print(f&quot;\u65b9\u7a0b\u7684\u6839\u4e3a: {roots[0]} \u548c {roots[1]}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8be5\u65b9\u7a0b\u65e0\u5b9e\u6570\u6839&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u3002\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u8ba1\u7b97\u7b80\u5355\u4e14\u7ed3\u679c\u7cbe\u786e\uff1bNumPy\u5e93\u9002\u5408\u5904\u7406\u591a\u9879\u5f0f\u7684\u6839\uff1bSymPy\u5e93\u4e13\u4e3a\u7b26\u53f7\u8ba1\u7b97\u8bbe\u8ba1\uff0c\u529f\u80fd\u5f3a\u5927\uff1bSciPy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u89e3\u51b3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p><strong>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u5176\u80cc\u540e\u7684\u6570\u5b66\u539f\u7406\u548c\u5b9e\u73b0\u65b9\u5f0f\uff0c\u90fd\u662f\u63d0\u9ad8\u7f16\u7a0b\u80fd\u529b\u548c\u6570\u5b66\u7d20\u517b\u7684\u91cd\u8981\u9014\u5f84\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u6839\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u5b66\u5e93\u4e2d\u7684<code>math<\/code>\u6a21\u5757\u6765\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u3002\u901a\u8fc7\u6c42\u89e3\u5224\u522b\u5f0f\uff08b\u00b2 &#8211; 4ac\uff09\uff0c\u53ef\u4ee5\u786e\u5b9a\u65b9\u7a0b\u7684\u6839\u7684\u6570\u91cf\u548c\u7c7b\u578b\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165<code>math<\/code>\u6a21\u5757\uff0c\u5b9a\u4e49\u65b9\u7a0b\u7684\u7cfb\u6570a\u3001b\u548cc\uff0c\u7136\u540e\u8ba1\u7b97\u5224\u522b\u5f0f\uff0c\u5e76\u6839\u636e\u5224\u522b\u5f0f\u7684\u503c\u5224\u65ad\u6839\u7684\u7c7b\u578b\uff08\u5b9e\u6839\u3001\u91cd\u6839\u6216\u865a\u6839\uff09\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u7b80\u5355\u7684Python\u5e93\u53ef\u4ee5\u5e2e\u52a9\u89e3\u51b3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982<code>numpy<\/code>\u548c<code>sympy<\/code>\u7b49\u5e93\u6765\u7b80\u5316\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u6c42\u89e3\u8fc7\u7a0b\u3002<code>numpy<\/code>\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684<code>roots<\/code>\u51fd\u6570\uff0c\u800c<code>sympy<\/code>\u5219\u53ef\u4ee5\u901a\u8fc7\u7b26\u53f7\u8ba1\u7b97\u6765\u627e\u5230\u65b9\u7a0b\u7684\u7cbe\u786e\u89e3\u3002\u8fd9\u4e9b\u5e93\u4f7f\u5f97\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u56fe\u5f62\u5316\u5c55\u793a\u6839\u7684\u5206\u5e03\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u865a\u6839\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5f53\u65b9\u7a0b\u7684\u5224\u522b\u5f0f\u5c0f\u4e8e\u96f6\u65f6\uff0c\u8bf4\u660e\u5b58\u5728\u865a\u6839\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cmath<\/code>\u6a21\u5757\u6765\u5904\u7406\u590d\u6570\u6839\u3002\u901a\u8fc7\u8ba1\u7b97\u590d\u6570\u5f62\u5f0f\u7684\u6839\uff0c\u53ef\u4ee5\u5f97\u5230\u65b9\u7a0b\u7684\u5b8c\u6574\u89e3\u3002\u4ee3\u7801\u4e2d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>cmath.sqrt<\/code>\u6765\u8ba1\u7b97\u865a\u6839\uff0c\u4ece\u800c\u83b7\u5f97\u6b63\u786e\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u9a8c\u8bc1\u6c42\u89e3\u7684\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u7ed3\u679c\u662f\u5426\u6b63\u786e\uff1f<\/strong><br \/>\u9a8c\u8bc1\u7ed3\u679c\u53ef\u4ee5\u901a\u8fc7\u5c06\u6c42\u5f97\u7684\u6839\u4ee3\u5165\u539f\u65b9\u7a0b\u4e2d\uff0c\u68c0\u67e5\u662f\u5426\u6210\u7acb\u3002\u5982\u679c\u4ee3\u5165\u540e\u7ed3\u679c\u4e3a0\uff0c\u5219\u8bf4\u660e\u6839\u662f\u6b63\u786e\u7684\u3002\u53ef\u4ee5\u5728Python\u4e2d\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff0c\u63a5\u53d7\u6839\u548c\u65b9\u7a0b\u7cfb\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u8ba1\u7b97\u65b9\u7a0b\u7684\u503c\u5e76\u5224\u65ad\u662f\u5426\u63a5\u8fd1\u4e8e\u96f6\uff0c\u4ee5\u6b64\u6765\u9a8c\u8bc1\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u3001NumPy\u5e93\u3001SymPy\u5e93\u7b49\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e8c [&hellip;]","protected":false},"author":3,"featured_media":1144813,"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\/1144802"}],"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=1144802"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144802\/revisions"}],"predecessor-version":[{"id":1144816,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144802\/revisions\/1144816"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144813"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1144802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1144802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1144802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}