{"id":1180264,"date":"2025-01-15T18:34:41","date_gmt":"2025-01-15T10:34:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1180264.html"},"modified":"2025-01-15T18:34:41","modified_gmt":"2025-01-15T10:34:41","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e4%b8%8d%e5%ae%9a%e7%a7%af%e5%88%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1180264.html","title":{"rendered":"Python\u5982\u4f55\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114448\/77fcc596-3365-43fc-bd8c-36909371011c.webp\" alt=\"Python\u5982\u4f55\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528SymPy\u5e93\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97\u3001\u4f7f\u7528SciPy\u5e93\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u3002<\/strong> SymPy\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7b26\u53f7\u8ba1\u7b97\u529f\u80fd\uff0c\u800cSciPy\u5219\u662f\u4e00\u4e2a\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u9002\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684SymPy\u5e93\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>SymPy\u5e93\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u4e13\u6ce8\u4e8e\u7b26\u53f7\u6570\u5b66\u8ba1\u7b97\u3002\u5b83\u80fd\u591f\u6267\u884c\u5404\u79cd\u6570\u5b66\u64cd\u4f5c\uff0c\u5305\u62ec\u5fae\u79ef\u5206\u3001\u4ee3\u6570\u3001\u65b9\u7a0b\u6c42\u89e3\u3001\u77e9\u9635\u64cd\u4f5c\u7b49\u3002\u5bf9\u4e8e\u4e0d\u5b9a\u79ef\u5206\uff0cSymPy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684<code>integrate<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001SymPy\u5e93\u7684\u5b89\u88c5\u4e0e\u57fa\u7840\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001SymPy\u5e93\u7684\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528SymPy\u8fdb\u884c\u4e0d\u5b9a\u79ef\u5206\u8ba1\u7b97\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u8fd9\u4e2a\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\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>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165SymPy\u5e76\u5f00\u59cb\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><h4>2\u3001SymPy\u5e93\u7684\u57fa\u7840\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165SymPy\u5e93\uff0c\u5e76\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf\u3002SymPy\u7684\u7b26\u53f7\u8ba1\u7b97\u57fa\u4e8e\u7b26\u53f7\u5bf9\u8c61\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4f7f\u7528<code>symbols<\/code>\u51fd\u6570\u6765\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sympy as sp<\/p>\n<h2><strong>\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf x<\/strong><\/h2>\n<p>x = sp.symbols(&#39;x&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>integrate<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\u3002<code>integrate<\/code>\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">integral = sp.integrate(expression, variable)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>expression<\/code>\u662f\u6211\u4eec\u9700\u8981\u79ef\u5206\u7684\u8868\u8fbe\u5f0f\uff0c<code>variable<\/code>\u662f\u79ef\u5206\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97<code>x^2<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u8868\u8fbe\u5f0f x^2<\/p>\n<p>expression = x2<\/p>\n<h2><strong>\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral = sp.integrate(expression, x)<\/p>\n<p>print(integral)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u8f93\u51fa<code>x3\/3<\/code>\uff0c\u5373<code>x^2<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\u662f<code>x^3\/3<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001SymPy\u5e93\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u591a\u9879\u5f0f\u8868\u8fbe\u5f0f\u7684\u4e0d\u5b9a\u79ef\u5206<\/h4>\n<\/p>\n<p><p>SymPy\u53ef\u4ee5\u5904\u7406\u591a\u9879\u5f0f\u8868\u8fbe\u5f0f\u7684\u4e0d\u5b9a\u79ef\u5206\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97<code>3x^3 - 2x^2 + x - 5<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u591a\u9879\u5f0f\u8868\u8fbe\u5f0f<\/p>\n<p>expression = 3*x&lt;strong&gt;3 - 2*x&lt;\/strong&gt;2 + x - 5<\/p>\n<h2><strong>\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral = sp.integrate(expression, x)<\/p>\n<p>print(integral)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u8f93\u51fa<code>3*x&lt;strong&gt;4\/4 - 2*x&lt;\/strong&gt;3\/3 + x2\/2 - 5*x<\/code>\uff0c\u5373<code>3x^3 - 2x^2 + x - 5<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\u662f<code>3x^4\/4 - 2x^3\/3 + x^2\/2 - 5x<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u4e09\u89d2\u51fd\u6570\u7684\u4e0d\u5b9a\u79ef\u5206<\/h4>\n<\/p>\n<p><p>SymPy\u540c\u6837\u53ef\u4ee5\u5904\u7406\u4e09\u89d2\u51fd\u6570\u7684\u4e0d\u5b9a\u79ef\u5206\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97<code>sin(x)<\/code>\u548c<code>cos(x)<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e09\u89d2\u51fd\u6570\u8868\u8fbe\u5f0f<\/p>\n<p>expression_sin = sp.sin(x)<\/p>\n<p>expression_cos = sp.cos(x)<\/p>\n<h2><strong>\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral_sin = sp.integrate(expression_sin, x)<\/p>\n<p>integral_cos = sp.integrate(expression_cos, x)<\/p>\n<p>print(integral_sin)<\/p>\n<p>print(integral_cos)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u5206\u522b\u8f93\u51fa<code>-cos(x)<\/code>\u548c<code>sin(x)<\/code>\uff0c\u5373<code>sin(x)<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\u662f<code>-cos(x)<\/code>\uff0c<code>cos(x)<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\u662f<code>sin(x)<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u6307\u6570\u51fd\u6570\u548c\u5bf9\u6570\u51fd\u6570\u7684\u4e0d\u5b9a\u79ef\u5206<\/h4>\n<\/p>\n<p><p>SymPy\u4e5f\u53ef\u4ee5\u5904\u7406\u6307\u6570\u51fd\u6570\u548c\u5bf9\u6570\u51fd\u6570\u7684\u4e0d\u5b9a\u79ef\u5206\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97<code>e^x<\/code>\u548c<code>ln(x)<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u6307\u6570\u51fd\u6570\u548c\u5bf9\u6570\u51fd\u6570\u8868\u8fbe\u5f0f<\/p>\n<p>expression_exp = sp.exp(x)<\/p>\n<p>expression_log = sp.log(x)<\/p>\n<h2><strong>\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral_exp = sp.integrate(expression_exp, x)<\/p>\n<p>integral_log = sp.integrate(expression_log, x)<\/p>\n<p>print(integral_exp)<\/p>\n<p>print(integral_log)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u5206\u522b\u8f93\u51fa<code>exp(x)<\/code>\u548c<code>x*log(x) - x<\/code>\uff0c\u5373<code>e^x<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\u662f<code>e^x<\/code>\uff0c<code>ln(x)<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\u662f<code>x*ln(x) - x<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001SymPy\u5e93\u7684\u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><h4>1\u3001\u8ba1\u7b97\u591a\u53d8\u91cf\u51fd\u6570\u7684\u4e0d\u5b9a\u79ef\u5206<\/h4>\n<\/p>\n<p><p>SymPy\u4e0d\u4ec5\u53ef\u4ee5\u8ba1\u7b97\u5355\u53d8\u91cf\u51fd\u6570\u7684\u4e0d\u5b9a\u79ef\u5206\uff0c\u8fd8\u53ef\u4ee5\u8ba1\u7b97\u591a\u53d8\u91cf\u51fd\u6570\u7684\u4e0d\u5b9a\u79ef\u5206\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97<code>x*y<\/code>\u5173\u4e8e<code>x<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf x \u548c y<\/p>\n<p>x, y = sp.symbols(&#39;x y&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u591a\u53d8\u91cf\u51fd\u6570\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>expression = x*y<\/p>\n<h2><strong>\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral = sp.integrate(expression, x)<\/p>\n<p>print(integral)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u8f93\u51fa<code>x2*y\/2<\/code>\uff0c\u5373<code>x*y<\/code>\u5173\u4e8e<code>x<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\u662f<code>x^2*y\/2<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u542b\u6709\u53c2\u6570\u7684\u4e0d\u5b9a\u79ef\u5206<\/h4>\n<\/p>\n<p><p>SymPy\u8fd8\u53ef\u4ee5\u8ba1\u7b97\u542b\u6709\u53c2\u6570\u7684\u4e0d\u5b9a\u79ef\u5206\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97<code>a*x^2 + b*x + c<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\uff0c\u5176\u4e2d<code>a<\/code>\u3001<code>b<\/code>\u548c<code>c<\/code>\u662f\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf x, a, b, c<\/p>\n<p>x, a, b, c = sp.symbols(&#39;x a b c&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u542b\u6709\u53c2\u6570\u7684\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>expression = a*x2 + b*x + c<\/p>\n<h2><strong>\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral = sp.integrate(expression, x)<\/p>\n<p>print(integral)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u8f93\u51fa<code>a*x&lt;strong&gt;3\/3 + b*x&lt;\/strong&gt;2\/2 + c*x<\/code>\uff0c\u5373<code>a*x^2 + b*x + c<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\u662f<code>a*x^3\/3 + b*x^2\/2 + c*x<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u5b9a\u79ef\u5206\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9a\u79ef\u5206\u6765\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\u3002\u5b9a\u79ef\u5206\u7684\u8ba1\u7b97\u65b9\u6cd5\u662f\u5728<code>integrate<\/code>\u51fd\u6570\u4e2d\u6307\u5b9a\u79ef\u5206\u4e0a\u4e0b\u9650\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97<code>x^2<\/code>\u5728\u533a\u95f4[0, x]\u4e0a\u7684\u5b9a\u79ef\u5206\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u4e0d\u5b9a\u79ef\u5206\u7684\u4e00\u79cd\u8868\u8fbe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u8868\u8fbe\u5f0f x^2<\/p>\n<p>expression = x2<\/p>\n<h2><strong>\u8ba1\u7b97\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral = sp.integrate(expression, (x, 0, x))<\/p>\n<p>print(integral)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u8f93\u51fa<code>x3\/3<\/code>\uff0c\u5373<code>x^2<\/code>\u5728\u533a\u95f4[0, x]\u4e0a\u7684\u5b9a\u79ef\u5206\u662f<code>x^3\/3<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001SymPy\u5e93\u7684\u5176\u4ed6\u529f\u80fd<\/h3>\n<\/p>\n<p><h4>1\u3001\u5316\u7b80\u79ef\u5206\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>SymPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5316\u7b80\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u79ef\u5206\u7ed3\u679c\u8fdb\u884c\u5316\u7b80\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>simplify<\/code>\u51fd\u6570\u5bf9\u79ef\u5206\u7ed3\u679c\u8fdb\u884c\u5316\u7b80\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u8868\u8fbe\u5f0f<\/p>\n<p>expression = (x2 + 2*x + 1)<\/p>\n<h2><strong>\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral = sp.integrate(expression, x)<\/p>\n<h2><strong>\u5316\u7b80\u79ef\u5206\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>simplified_integral = sp.simplify(integral)<\/p>\n<p>print(simplified_integral)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u8f93\u51fa<code>x&lt;strong&gt;3\/3 + x&lt;\/strong&gt;2 + x<\/code>\uff0c\u5373\u5bf9<code>(x^2 + 2*x + 1)<\/code>\u7684\u4e0d\u5b9a\u79ef\u5206\u8fdb\u884c\u4e86\u5316\u7b80\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u9a8c\u8bc1\u79ef\u5206\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>SymPy\u63d0\u4f9b\u4e86<code>diff<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u79ef\u5206\u7ed3\u679c\u8fdb\u884c\u6c42\u5bfc\uff0c\u4ece\u800c\u9a8c\u8bc1\u79ef\u5206\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u8868\u8fbe\u5f0f<\/p>\n<p>expression = x2<\/p>\n<h2><strong>\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral = sp.integrate(expression, x)<\/p>\n<h2><strong>\u5bf9\u79ef\u5206\u7ed3\u679c\u6c42\u5bfc<\/strong><\/h2>\n<p>derivative = sp.diff(integral, x)<\/p>\n<p>print(derivative)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u8f93\u51fa<code>x2<\/code>\uff0c\u5373\u5bf9<code>x^3\/3<\/code>\u6c42\u5bfc\u5f97\u5230\u4e86\u539f\u8868\u8fbe\u5f0f<code>x^2<\/code>\uff0c\u9a8c\u8bc1\u4e86\u79ef\u5206\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001SciPy\u5e93\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u867d\u7136SymPy\u5728\u7b26\u53f7\u8ba1\u7b97\u65b9\u9762\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u6570\u503c\u79ef\u5206\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528SciPy\u5e93\u4e2d\u7684<code>quad<\/code>\u51fd\u6570\u8fdb\u884c\u6570\u503c\u79ef\u5206\u3002<\/p>\n<\/p>\n<p><h4>1\u3001SciPy\u5e93\u7684\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5SciPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scipy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165SciPy\u5e76\u5f00\u59cb\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528quad\u51fd\u6570\u8fdb\u884c\u6570\u503c\u79ef\u5206<\/h4>\n<\/p>\n<p><p>SciPy\u5e93\u4e2d\u7684<code>quad<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5b9a\u79ef\u5206\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.integrate import quad<\/p>\n<h2><strong>\u5b9a\u4e49\u88ab\u79ef\u51fd\u6570<\/strong><\/h2>\n<p>def integrand(x):<\/p>\n<p>    return x2<\/p>\n<h2><strong>\u8ba1\u7b97\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral, error = quad(integrand, 0, 1)<\/p>\n<p>print(integral)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u8f93\u51fa<code>0.33333333333333337<\/code>\uff0c\u5373<code>x^2<\/code>\u5728\u533a\u95f4[0, 1]\u4e0a\u7684\u5b9a\u79ef\u5206\u662f<code>1\/3<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u66f4\u590d\u6742\u7684\u51fd\u6570<\/h4>\n<\/p>\n<p><p>SciPy\u5e93\u7684<code>quad<\/code>\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97<code>sin(x)<\/code>\u5728\u533a\u95f4[0, pi]\u4e0a\u7684\u5b9a\u79ef\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.integrate import quad<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u88ab\u79ef\u51fd\u6570<\/strong><\/h2>\n<p>def integrand(x):<\/p>\n<p>    return np.sin(x)<\/p>\n<h2><strong>\u8ba1\u7b97\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral, error = quad(integrand, 0, np.pi)<\/p>\n<p>print(integral)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f1a\u8f93\u51fa<code>2.0<\/code>\uff0c\u5373<code>sin(x)<\/code>\u5728\u533a\u95f4[0, pi]\u4e0a\u7684\u5b9a\u79ef\u5206\u662f<code>2<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528SymPy\u5e93\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97\u548c\u4f7f\u7528SciPy\u5e93\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u3002SymPy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7b26\u53f7\u8ba1\u7b97\u529f\u80fd\uff0c\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u7c7b\u578b\u7684\u51fd\u6570\u548c\u8868\u8fbe\u5f0f\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5316\u7b80\u548c\u9a8c\u8bc1\u5de5\u5177\u3002\u800cSciPy\u5e93\u5219\u9002\u7528\u4e8e\u6570\u503c\u79ef\u5206\uff0c\u9002\u5408\u5904\u7406\u66f4\u590d\u6742\u7684\u51fd\u6570\u548c\u8868\u8fbe\u5f0f\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u8ba1\u7b97\u548c\u5206\u6790\u4e0d\u5b9a\u79ef\u5206\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u95ee\u9898\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u548c\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u4e3a\u5927\u5bb6\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u53c2\u8003\uff0c\u5e2e\u52a9\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u4e0d\u5b9a\u79ef\u5206\u8ba1\u7b97\u7684\u65b9\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e0d\u5b9a\u79ef\u5206\u7684\u8ba1\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528SymPy\u5e93\u8fdb\u884c\u4e0d\u5b9a\u79ef\u5206\u7684\u8ba1\u7b97\u3002SymPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b26\u53f7\u8ba1\u7b97\u5e93\uff0c\u80fd\u591f\u5904\u7406\u6570\u5b66\u7b26\u53f7\uff0c\u7b80\u5316\u8868\u8fbe\u5f0f\uff0c\u5e76\u8ba1\u7b97\u79ef\u5206\u3002\u9996\u5148\uff0c\u5b89\u88c5SymPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<code>pip install sympy<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>integrate<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97x\u7684\u5e73\u65b9\u7684\u4e0d\u5b9a\u79ef\u5206\u53ef\u4ee5\u5982\u4e0b\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">from sympy import symbols, integrate\n\nx = symbols(&#39;x&#39;)\nintegral_result = integrate(x**2, x)\nprint(integral_result)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u9002\u5408\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\uff1f<\/strong><br \/>\u9664\u4e86SymPy\uff0cPython\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\u3002\u4f8b\u5982\uff0cNumPy\u548cSciPy\u4e3b\u8981\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0c\u4f46SciPy\u7684<code>quad<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7528\u4e8e\u4f30\u8ba1\u5b9a\u79ef\u5206\u3002\u800c\u5bf9\u4e8e\u7b26\u53f7\u8ba1\u7b97\uff0cSymPy\u662f\u6700\u5e38\u7528\u7684\u5e93\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u60a8\u662f\u9700\u8981\u7cbe\u786e\u7684\u7b26\u53f7\u7ed3\u679c\u8fd8\u662f\u6570\u503c\u8fd1\u4f3c\u3002<\/p>\n<p><strong>\u4e0d\u5b9a\u79ef\u5206\u7684\u8ba1\u7b97\u7ed3\u679c\u5982\u4f55\u8fdb\u884c\u9a8c\u8bc1\uff1f<\/strong><br \/>\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6c42\u5bfc\u6765\u9a8c\u8bc1\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002\u6839\u636e\u5fae\u79ef\u5206\u57fa\u672c\u5b9a\u7406\uff0c\u82e5F(x)\u662ff(x)\u7684\u4e0d\u5b9a\u79ef\u5206\uff0c\u5219F&#39;(x)\u5e94\u5f53\u7b49\u4e8ef(x)\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528SymPy\u7684<code>diff<\/code>\u51fd\u6570\u6765\u5bf9\u8ba1\u7b97\u5f97\u5230\u7684\u79ef\u5206\u7ed3\u679c\u8fdb\u884c\u6c42\u5bfc\u3002\u4f8b\u5982\uff0c\u9a8c\u8bc1x\u7684\u5e73\u65b9\u7684\u4e0d\u5b9a\u79ef\u5206\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">from sympy import diff\n\noriginal_function = x**2\nintegral_result = integrate(original_function, x)\nverification = diff(integral_result, x)\nprint(verification)  # \u5e94\u8be5\u7b49\u4e8e\u539f\u51fd\u6570\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u6837\u7684\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u60a8\u7684\u4e0d\u5b9a\u79ef\u5206\u8ba1\u7b97\u662f\u6b63\u786e\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528SymPy\u5e93\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97\u3001\u4f7f\u7528SciPy\u5e93\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u3002 S [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180264"}],"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=1180264"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1180264\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1180264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1180264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1180264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}