{"id":1098611,"date":"2025-01-08T15:22:44","date_gmt":"2025-01-08T07:22:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098611.html"},"modified":"2025-01-08T15:22:47","modified_gmt":"2025-01-08T07:22:47","slug":"python%e4%b8%ad%e8%a7%a3%e6%96%b9%e7%a8%8b%e5%a6%82%e4%bd%95%e5%8f%aa%e5%8f%96%e6%ad%a3%e6%a0%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098611.html","title":{"rendered":"Python\u4e2d\u89e3\u65b9\u7a0b\u5982\u4f55\u53ea\u53d6\u6b63\u6839"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062717\/9747ede1-09ea-42be-8912-5950c51e4433.webp\" alt=\"Python\u4e2d\u89e3\u65b9\u7a0b\u5982\u4f55\u53ea\u53d6\u6b63\u6839\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u89e3\u65b9\u7a0b\u53ea\u53d6\u6b63\u6839\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528sympy\u5e93\u6765\u89e3\u51b3\u3002sympy\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b26\u53f7\u6570\u5b66\u5e93\uff0c\u63d0\u4f9b\u4e86\u6c42\u89e3\u65b9\u7a0b\u3001\u7b80\u5316\u8868\u8fbe\u5f0f\u3001\u7b26\u53f7\u5fae\u79ef\u5206\u7b49\u529f\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528sympy.solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b\u3001\u8fc7\u6ee4\u6b63\u6839\u3001\u4f7f\u7528sympy.symbols()\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf\u3002<\/strong> \u4f8b\u5982\uff0c\u5728\u89e3\u51b3\u4e00\u4e2a\u7b80\u5355\u7684\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.solve()\u51fd\u6570\u6765\u83b7\u5f97\u6240\u6709\u7684\u6839\uff0c\u7136\u540e\u901a\u8fc7\u8fc7\u6ee4\u53ea\u4fdd\u7559\u6b63\u6839\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528sympy.solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0csympy\u5e93\u63d0\u4f9b\u4e86sympy.solve()\u51fd\u6570\u6765\u6c42\u89e3\u65b9\u7a0b\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165sympy\u5e93\uff0c\u5e76\u5b9a\u4e49\u6211\u4eec\u7684\u65b9\u7a0b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.symbols()\u51fd\u6570\u6765\u5b9a\u4e49\u65b9\u7a0b\u4e2d\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve<\/p>\n<h2><strong>\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>x = symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u65b9\u7a0b<\/strong><\/h2>\n<p>equation = x2 - 4<\/p>\n<h2><strong>\u4f7f\u7528solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solutions = solve(equation, x)<\/p>\n<p>print(solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8fc7\u6ee4\u6b63\u6839<\/h3>\n<\/p>\n<p><p>sympy.solve()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u89e3\u7684\u5217\u8868\u3002\u4e3a\u4e86\u53ea\u4fdd\u7559\u6b63\u6839\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5217\u8868\u89e3\u6790\u6765\u8fc7\u6ee4\u6389\u8d1f\u6839\u548c\u590d\u6570\u6839\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528is_real\u5c5e\u6027\u6765\u68c0\u67e5\u6839\u662f\u5426\u4e3a\u5b9e\u6570\uff0c\u5e76\u4f7f\u7528\u5927\u4e8e\u96f6\u7684\u6761\u4ef6\u6765\u68c0\u67e5\u6839\u662f\u5426\u4e3a\u6b63\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fc7\u6ee4\u6b63\u6839<\/p>\n<p>positive_solutions = [sol for sol in solutions if sol.is_real and sol &gt; 0]<\/p>\n<p>print(positive_solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528sympy.symbols()\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u65b9\u7a0b\u65f6\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528sympy.symbols()\u51fd\u6570\u6765\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u89e3\u4e00\u4e2a\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0bx^2 &#8211; 4 = 0\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u53d8\u91cfx\uff0c\u5e76\u5c06\u5176\u4ee3\u5165\u65b9\u7a0b\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve<\/p>\n<h2><strong>\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>x = symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u65b9\u7a0b<\/strong><\/h2>\n<p>equation = x2 - 4<\/p>\n<h2><strong>\u4f7f\u7528solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solutions = solve(equation, x)<\/p>\n<h2><strong>\u8fc7\u6ee4\u6b63\u6839<\/strong><\/h2>\n<p>positive_solutions = [sol for sol in solutions if sol.is_real and sol &gt; 0]<\/p>\n<p>print(positive_solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u590d\u6742\u65b9\u7a0b<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u65b9\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u6c42\u89e3\uff0c\u5e76\u8fc7\u6ee4\u6b63\u6839\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4e09\u6b21\u65b9\u7a0bx^3 &#8211; 6x^2 + 11x &#8211; 6 = 0\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u6765\u6c42\u89e3\uff0c\u5e76\u8fc7\u6ee4\u6b63\u6839\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve<\/p>\n<h2><strong>\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>x = symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u65b9\u7a0b<\/strong><\/h2>\n<p>equation = x&lt;strong&gt;3 - 6*x&lt;\/strong&gt;2 + 11*x - 6<\/p>\n<h2><strong>\u4f7f\u7528solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solutions = solve(equation, x)<\/p>\n<h2><strong>\u8fc7\u6ee4\u6b63\u6839<\/strong><\/h2>\n<p>positive_solutions = [sol for sol in solutions if sol.is_real and sol &gt; 0]<\/p>\n<p>print(positive_solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u591a\u53d8\u91cf\u65b9\u7a0b<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u591a\u53d8\u91cf\u65b9\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.solve()\u51fd\u6570\u5e76\u4f20\u5165\u591a\u4e2a\u53d8\u91cf\u3002sympy.solve()\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u89e3\u7684\u5b57\u5178\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u6765\u8fc7\u6ee4\u6b63\u6839\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve<\/p>\n<h2><strong>\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>x, y = symbols(&#39;x y&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u65b9\u7a0b<\/strong><\/h2>\n<p>equation1 = x + y - 4<\/p>\n<p>equation2 = x - y - 2<\/p>\n<h2><strong>\u4f7f\u7528solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solutions = solve((equation1, equation2), (x, y))<\/p>\n<h2><strong>\u8fc7\u6ee4\u6b63\u6839<\/strong><\/h2>\n<p>positive_solutions = [sol for sol in solutions if sol[x].is_real and sol[x] &gt; 0 and sol[y].is_real and sol[y] &gt; 0]<\/p>\n<p>print(positive_solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u542b\u53c2\u6570\u7684\u65b9\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u65b9\u7a0b\u53ef\u80fd\u5305\u542b\u53c2\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.symbols()\u51fd\u6570\u6765\u5b9a\u4e49\u8fd9\u4e9b\u53c2\u6570\uff0c\u5e76\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u6765\u6c42\u89e3\u548c\u8fc7\u6ee4\u6b63\u6839\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve<\/p>\n<h2><strong>\u5b9a\u4e49\u53d8\u91cf\u548c\u53c2\u6570<\/strong><\/h2>\n<p>x, a = symbols(&#39;x a&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u65b9\u7a0b<\/strong><\/h2>\n<p>equation = x2 - a<\/p>\n<h2><strong>\u4f7f\u7528solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solutions = solve(equation, x)<\/p>\n<h2><strong>\u8fc7\u6ee4\u6b63\u6839<\/strong><\/h2>\n<p>positive_solutions = [sol for sol in solutions if sol.is_real and sol &gt; 0]<\/p>\n<p>print(positive_solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u4e0d\u7b49\u5f0f<\/h3>\n<\/p>\n<p><p>sympy\u5e93\u8fd8\u63d0\u4f9b\u4e86sympy.solve_univariate_inequality()\u51fd\u6570\u6765\u6c42\u89e3\u4e0d\u7b49\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u6c42\u89e3\u4e0d\u7b49\u5f0f\uff0c\u5e76\u8fc7\u6ee4\u6b63\u89e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve_univariate_inequality<\/p>\n<h2><strong>\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>x = symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0d\u7b49\u5f0f<\/strong><\/h2>\n<p>inequality = x2 - 4 &gt; 0<\/p>\n<h2><strong>\u4f7f\u7528solve_univariate_inequality()\u51fd\u6570\u6c42\u89e3\u4e0d\u7b49\u5f0f<\/strong><\/h2>\n<p>solution = solve_univariate_inequality(inequality, x)<\/p>\n<h2><strong>\u8fc7\u6ee4\u6b63\u89e3<\/strong><\/h2>\n<p>positive_solution = solution &amp; (x &gt; 0)<\/p>\n<p>print(positive_solution)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u590d\u6570\u6839<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u65b9\u7a0b\u53ef\u80fd\u6709\u590d\u6570\u6839\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.solve()\u51fd\u6570\u5e76\u8fc7\u6ee4\u6389\u590d\u6570\u6839\uff0c\u53ea\u4fdd\u7559\u5b9e\u6570\u6839\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528is_real\u5c5e\u6027\u6765\u68c0\u67e5\u6839\u662f\u5426\u4e3a\u5b9e\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve<\/p>\n<h2><strong>\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>x = symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u65b9\u7a0b<\/strong><\/h2>\n<p>equation = x2 + 1<\/p>\n<h2><strong>\u4f7f\u7528solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solutions = solve(equation, x)<\/p>\n<h2><strong>\u8fc7\u6ee4\u5b9e\u6570\u6839<\/strong><\/h2>\n<p>real_solutions = [sol for sol in solutions if sol.is_real]<\/p>\n<p>print(real_solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u5e26\u6709\u5bf9\u6570\u7684\u65b9\u7a0b<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5e26\u6709\u5bf9\u6570\u7684\u65b9\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.solve()\u51fd\u6570\u6765\u6c42\u89e3\uff0c\u5e76\u8fc7\u6ee4\u6b63\u6839\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u65b9\u7a0blog(x) = 2\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.log()\u51fd\u6570\u6765\u5b9a\u4e49\u5bf9\u6570\uff0c\u5e76\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u6765\u6c42\u89e3\u548c\u8fc7\u6ee4\u6b63\u6839\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve, log<\/p>\n<h2><strong>\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>x = symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u65b9\u7a0b<\/strong><\/h2>\n<p>equation = log(x) - 2<\/p>\n<h2><strong>\u4f7f\u7528solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solutions = solve(equation, x)<\/p>\n<h2><strong>\u8fc7\u6ee4\u6b63\u6839<\/strong><\/h2>\n<p>positive_solutions = [sol for sol in solutions if sol.is_real and sol &gt; 0]<\/p>\n<p>print(positive_solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5904\u7406\u5e26\u6709\u6307\u6570\u7684\u65b9\u7a0b<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5e26\u6709\u6307\u6570\u7684\u65b9\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.solve()\u51fd\u6570\u6765\u6c42\u89e3\uff0c\u5e76\u8fc7\u6ee4\u6b63\u6839\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u65b9\u7a0be^x = 4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.exp()\u51fd\u6570\u6765\u5b9a\u4e49\u6307\u6570\uff0c\u5e76\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u6765\u6c42\u89e3\u548c\u8fc7\u6ee4\u6b63\u6839\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve, exp<\/p>\n<h2><strong>\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>x = symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u65b9\u7a0b<\/strong><\/h2>\n<p>equation = exp(x) - 4<\/p>\n<h2><strong>\u4f7f\u7528solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solutions = solve(equation, x)<\/p>\n<h2><strong>\u8fc7\u6ee4\u6b63\u6839<\/strong><\/h2>\n<p>positive_solutions = [sol for sol in solutions if sol.is_real and sol &gt; 0]<\/p>\n<p>print(positive_solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5904\u7406\u5e26\u6709\u4e09\u89d2\u51fd\u6570\u7684\u65b9\u7a0b<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5e26\u6709\u4e09\u89d2\u51fd\u6570\u7684\u65b9\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.solve()\u51fd\u6570\u6765\u6c42\u89e3\uff0c\u5e76\u8fc7\u6ee4\u6b63\u6839\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u65b9\u7a0bsin(x) = 0.5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.sin()\u51fd\u6570\u6765\u5b9a\u4e49\u4e09\u89d2\u51fd\u6570\uff0c\u5e76\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u6765\u6c42\u89e3\u548c\u8fc7\u6ee4\u6b63\u6839\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve, sin<\/p>\n<h2><strong>\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>x = symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u65b9\u7a0b<\/strong><\/h2>\n<p>equation = sin(x) - 0.5<\/p>\n<h2><strong>\u4f7f\u7528solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solutions = solve(equation, x)<\/p>\n<h2><strong>\u8fc7\u6ee4\u6b63\u6839<\/strong><\/h2>\n<p>positive_solutions = [sol for sol in solutions if sol.is_real and sol &gt; 0]<\/p>\n<p>print(positive_solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5904\u7406\u5e26\u6709\u5206\u6570\u7684\u65b9\u7a0b<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5e26\u6709\u5206\u6570\u7684\u65b9\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.solve()\u51fd\u6570\u6765\u6c42\u89e3\uff0c\u5e76\u8fc7\u6ee4\u6b63\u6839\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u65b9\u7a0b1\/x = 2\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u6765\u6c42\u89e3\u548c\u8fc7\u6ee4\u6b63\u6839\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, solve<\/p>\n<h2><strong>\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>x = symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u65b9\u7a0b<\/strong><\/h2>\n<p>equation = 1\/x - 2<\/p>\n<h2><strong>\u4f7f\u7528solve()\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solutions = solve(equation, x)<\/p>\n<h2><strong>\u8fc7\u6ee4\u6b63\u6839<\/strong><\/h2>\n<p>positive_solutions = [sol for sol in solutions if sol.is_real and sol &gt; 0]<\/p>\n<p>print(positive_solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u4f7f\u7528sympy\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u89e3\u65b9\u7a0b\u5e76\u53ea\u53d6\u6b63\u6839\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528sympy.solve()\u51fd\u6570\u6765\u6c42\u89e3\u65b9\u7a0b\uff0c\u5e76\u4f7f\u7528Python\u7684\u5217\u8868\u89e3\u6790\u6765\u8fc7\u6ee4\u6389\u8d1f\u6839\u548c\u590d\u6570\u6839\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684\u65b9\u7a0b\uff0c\u5305\u62ec\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u3001\u590d\u6742\u65b9\u7a0b\u3001\u591a\u53d8\u91cf\u65b9\u7a0b\u3001\u542b\u53c2\u6570\u7684\u65b9\u7a0b\u3001\u4e0d\u7b49\u5f0f\u3001\u5e26\u6709\u5bf9\u6570\u3001\u6307\u6570\u3001\u4e09\u89d2\u51fd\u6570\u548c\u5206\u6570\u7684\u65b9\u7a0b\u3002sympy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7b26\u53f7\u8ba1\u7b97\u529f\u80fd\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u8fdb\u884c\u65b9\u7a0b\u6c42\u89e3\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u89e3\u65b9\u7a0b\u5e76\u63d0\u53d6\u6b63\u6839\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sympy<\/code>\u5e93\u6765\u89e3\u65b9\u7a0b\u3002\u901a\u8fc7\u5b9a\u4e49\u65b9\u7a0b\u5e76\u4f7f\u7528<code>sympy.solve()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u5f97\u6240\u6709\u89e3\u3002\u82e5\u8981\u4ec5\u63d0\u53d6\u6b63\u6839\uff0c\u53ef\u4ee5\u5728\u89e3\u7684\u5217\u8868\u4e2d\u7b5b\u9009\u51fa\u5927\u4e8e\u96f6\u7684\u89e3\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from sympy import symbols, Eq, solve\n\nx = symbols(&#39;x&#39;)\nequation = Eq(x**2 - 4, 0)  # \u793a\u4f8b\u65b9\u7a0b x^2 - 4 = 0\nsolutions = solve(equation, x)\npositive_solutions = [sol for sol in solutions if sol &gt; 0]\nprint(positive_solutions)  # \u8f93\u51fa\u6b63\u6839\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u89e3\u65b9\u7a0b\u65f6\uff0c\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u9009\u62e9\uff1f<\/strong><br \/>\u9664\u4e86<code>sympy<\/code>\uff0c\u8fd8\u6709\u5176\u4ed6\u5e93\u5982<code>numpy<\/code>\u548c<code>scipy<\/code>\u53ef\u4ee5\u5904\u7406\u6570\u503c\u89e3\u3002\u5728\u5904\u7406\u975e\u7ebf\u6027\u65b9\u7a0b\u65f6\uff0c<code>scipy.optimize<\/code>\u4e2d\u7684<code>fsolve<\/code>\u53ef\u4ee5\u627e\u5230\u6839\u3002\u5982\u679c\u9700\u8981\u7cbe\u786e\u89e3\uff0c<code>sympy<\/code>\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u5bf9\u4e8e\u4e00\u822c\u7684\u7ebf\u6027\u65b9\u7a0b\u7ec4\uff0c<code>numpy.linalg.solve()<\/code>\u4e5f\u975e\u5e38\u6709\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u590d\u6742\u65b9\u7a0b\u7684\u6b63\u6839\u63d0\u53d6\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5305\u542b\u590d\u6570\u89e3\u7684\u590d\u6742\u65b9\u7a0b\uff0c\u4f7f\u7528<code>sympy<\/code>\u5e93\u4f9d\u7136\u6709\u6548\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u89e3\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u6765\u7b5b\u9009\u6b63\u6839\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from sympy import I  # \u5f15\u5165\u865a\u6570\u5355\u4f4d\nequation = Eq(x**2 + 1, 0)  # \u793a\u4f8b\u65b9\u7a0b x^2 + 1 = 0\nsolutions = solve(equation, x)\npositive_real_solutions = [sol for sol in solutions if sol.is_real and sol &gt; 0]\nprint(positive_real_solutions)  # \u8f93\u51fa\u6b63\u7684\u5b9e\u6839\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u53ea\u83b7\u53d6\u5b9e\u6570\u90e8\u5206\u4e3a\u6b63\u7684\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u89e3\u65b9\u7a0b\u53ea\u53d6\u6b63\u6839\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528sympy\u5e93\u6765\u89e3\u51b3\u3002sympy\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b26\u53f7\u6570\u5b66\u5e93\uff0c\u63d0\u4f9b\u4e86\u6c42\u89e3 [&hellip;]","protected":false},"author":3,"featured_media":1098616,"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\/1098611"}],"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=1098611"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098611\/revisions"}],"predecessor-version":[{"id":1098620,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098611\/revisions\/1098620"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098616"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}