{"id":1064293,"date":"2024-12-31T16:07:47","date_gmt":"2024-12-31T08:07:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1064293.html"},"modified":"2024-12-31T16:07:49","modified_gmt":"2024-12-31T08:07:49","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e8%99%9a%e9%83%a8%e5%92%8c%e5%ae%9e%e9%83%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1064293.html","title":{"rendered":"Python\u5982\u4f55\u53d6\u865a\u90e8\u548c\u5b9e\u90e8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/4f55f613-f670-4ebb-b810-0cd96e99af47.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u53d6\u865a\u90e8\u548c\u5b9e\u90e8\" \/><\/p>\n<p><p> <strong>Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u7c7b\u578b\u6765\u5904\u7406\u548c\u83b7\u53d6\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u3002<\/strong> Python \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 <code>complex<\/code> \u7c7b\u578b\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8868\u793a\u548c\u64cd\u4f5c\u590d\u6570\u3002\u83b7\u53d6\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u76f4\u63a5\u4f7f\u7528\u590d\u6570\u5bf9\u8c61\u7684 <code>.real<\/code> \u548c <code>.imag<\/code> \u5c5e\u6027\u3001\u4f7f\u7528 <code>cmath<\/code> \u6a21\u5757\u3001\u4ee5\u53ca\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u3002<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u4f7f\u7528\u590d\u6570\u5bf9\u8c61\u7684 <code>.real<\/code> \u548c <code>.imag<\/code> \u5c5e\u6027<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4eec\u7b80\u5355\u660e\u4e86\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u53d6\u865a\u90e8\u548c\u5b9e\u90e8\uff0c\u5e76\u63a2\u8ba8\u5176\u4e2d\u7684\u7ec6\u8282\u548c\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u590d\u6570\u5bf9\u8c61\u7684 <code>.real<\/code> \u548c <code>.imag<\/code> \u5c5e\u6027<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684 <code>complex<\/code> \u7c7b\u578b\u4f7f\u5f97\u8868\u793a\u590d\u6570\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u6bcf\u4e2a\u590d\u6570\u5bf9\u8c61\u90fd\u6709\u4e24\u4e2a\u5c5e\u6027\uff1a<code>.real<\/code> \u548c <code>.imag<\/code>\uff0c\u5206\u522b\u8868\u793a\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z = 3 + 4j  # \u521b\u5efa\u4e00\u4e2a\u590d\u6570<\/p>\n<p>real_part = z.real  # \u83b7\u53d6\u5b9e\u90e8<\/p>\n<p>imaginary_part = z.imag  # \u83b7\u53d6\u865a\u90e8<\/p>\n<p>print(f&quot;\u5b9e\u90e8: {real_part}, \u865a\u90e8: {imaginary_part}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>z<\/code> \u662f\u4e00\u4e2a\u590d\u6570\uff0c<code>z.real<\/code> \u8fd4\u56de\u590d\u6570\u7684\u5b9e\u90e8\u90e8\u5206\uff0c<code>z.imag<\/code> \u8fd4\u56de\u590d\u6570\u7684\u865a\u90e8\u90e8\u5206\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u4e14\u6613\u4e8e\u4f7f\u7528<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>cmath<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>cmath<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u989d\u5916\u7684\u51fd\u6570\u6765\u5904\u7406\u590d\u6570\uff0c\u5c3d\u7ba1\u5bf9\u4e8e\u83b7\u53d6\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\uff0c<code>.real<\/code> \u548c <code>.imag<\/code> \u5c5e\u6027\u5df2\u7ecf\u8db3\u591f\uff0c\u4f46 <code>cmath<\/code> \u6a21\u5757\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cmath<\/p>\n<p>z = 3 + 4j<\/p>\n<p>real_part = cmath.polar(z)[0]  # \u83b7\u53d6\u590d\u6570\u7684\u6a21\u957f<\/p>\n<p>imaginary_part = cmath.polar(z)[1]  # \u83b7\u53d6\u590d\u6570\u7684\u76f8\u89d2<\/p>\n<p>print(f&quot;\u6a21\u957f: {real_part}, \u76f8\u89d2: {imaginary_part}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c3d\u7ba1 <code>cmath.polar<\/code> \u8fd4\u56de\u7684\u662f\u590d\u6570\u7684\u6781\u5750\u6807\u5f62\u5f0f\uff0c\u4f46\u5728\u67d0\u4e9b\u5e94\u7528\u4e2d\u8fd9\u53ef\u80fd\u662f\u6240\u9700\u8981\u7684\u3002<strong>\u6781\u5750\u6807\u5f62\u5f0f\u5728\u67d0\u4e9b\u6570\u5b66\u548c\u5de5\u7a0b\u95ee\u9898\u4e2d\u975e\u5e38\u6709\u7528<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u51fa\u4e8e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u91cd\u7528\u6027\u7684\u8003\u8651\uff0c\u5b9a\u4e49\u81ea\u5df1\u7684\u51fd\u6570\u6765\u83b7\u53d6\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u4e5f\u662f\u4e00\u79cd\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_real_part(z):<\/p>\n<p>    return z.real<\/p>\n<p>def get_imaginary_part(z):<\/p>\n<p>    return z.imag<\/p>\n<p>z = 3 + 4j<\/p>\n<p>real_part = get_real_part(z)<\/p>\n<p>imaginary_part = get_imaginary_part(z)<\/p>\n<p>print(f&quot;\u5b9e\u90e8: {real_part}, \u865a\u90e8: {imaginary_part}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u6613\u4e8e\u7ef4\u62a4<\/strong>\uff0c\u5c24\u5176\u662f\u5728\u5927\u578b\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u590d\u6570\u7684\u8868\u793a\u4e0e\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u590d\u6570\u7684\u57fa\u672c\u8868\u793a\u65b9\u6cd5\u662f\u4f7f\u7528 <code>a + bj<\/code>\uff0c\u5176\u4e2d <code>a<\/code> \u548c <code>b<\/code> \u662f\u5b9e\u6570\uff0c<code>j<\/code> \u662f\u865a\u6570\u5355\u4f4d\u3002\u8fd9\u79cd\u8868\u793a\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\uff0c\u800c\u4e14\u4e0e\u6570\u5b66\u4e0a\u7684\u590d\u6570\u8868\u793a\u65b9\u6cd5\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z1 = 3 + 4j<\/p>\n<p>z2 = 1 + 2j<\/p>\n<p>z_sum = z1 + z2  # \u590d\u6570\u76f8\u52a0<\/p>\n<p>z_diff = z1 - z2  # \u590d\u6570\u76f8\u51cf<\/p>\n<p>z_product = z1 * z2  # \u590d\u6570\u76f8\u4e58<\/p>\n<p>z_quotient = z1 \/ z2  # \u590d\u6570\u76f8\u9664<\/p>\n<p>print(f&quot;\u590d\u6570\u548c: {z_sum}, \u590d\u6570\u5dee: {z_diff}, \u590d\u6570\u79ef: {z_product}, \u590d\u6570\u5546: {z_quotient}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u590d\u6570\u7684\u57fa\u672c\u8fd0\u7b97\u5728Python\u4e2d\u975e\u5e38\u76f4\u89c2\uff0c\u7c7b\u4f3c\u4e8e\u5b9e\u6570\u7684\u8fd0\u7b97<\/strong>\uff0c\u8fd9\u4f7f\u5f97Python\u6210\u4e3a\u5904\u7406\u590d\u6570\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u610f\u4e49<\/h3>\n<\/p>\n<p><p>\u590d\u6570\u5728\u5de5\u7a0b\u3001\u7269\u7406\u548c\u8bb8\u591a\u79d1\u5b66\u9886\u57df\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u83b7\u53d6\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u5728\u8fd9\u4e9b\u9886\u57df\u4e2d\u4e5f\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5728\u7535\u8def\u5206\u6790\u4e2d<\/strong>\uff0c\u590d\u6570\u7528\u4e8e\u8868\u793a\u4ea4\u6d41\u7535\u7684\u963b\u6297\uff0c\u83b7\u53d6\u963b\u6297\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u53ef\u4ee5\u5e2e\u52a9\u5de5\u7a0b\u5e08\u5206\u6790\u7535\u8def\u7684\u884c\u4e3a\u3002<\/li>\n<li><strong>\u5728\u4fe1\u53f7\u5904\u7406\u548c\u901a\u4fe1\u4e2d<\/strong>\uff0c\u590d\u6570\u7528\u4e8e\u8868\u793a\u4fe1\u53f7\uff0c\u5b9e\u90e8\u548c\u865a\u90e8\u5206\u522b\u8868\u793a\u4fe1\u53f7\u7684\u4e0d\u540c\u6210\u5206\u3002<\/li>\n<li><strong>\u5728\u91cf\u5b50\u529b\u5b66\u4e2d<\/strong>\uff0c\u590d\u6570\u7528\u4e8e\u8868\u793a\u6ce2\u51fd\u6570\uff0c\u5b9e\u90e8\u548c\u865a\u90e8\u5206\u522b\u8868\u793a\u6ce2\u51fd\u6570\u7684\u4e0d\u540c\u90e8\u5206\uff0c\u5bf9\u7406\u89e3\u548c\u5206\u6790\u91cf\u5b50\u7cfb\u7edf\u81f3\u5173\u91cd\u8981\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u5e38\u89c1\u9519\u8bef\u548c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>j<\/code> \u800c\u4e0d\u662f <code>1j<\/code> \u6765\u8868\u793a\u865a\u6570\u5355\u4f4d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z = 3 + j  # \u9519\u8bef\u7684\u8868\u793a\u65b9\u6cd5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u8868\u793a\u65b9\u6cd5\u4f1a\u5bfc\u81f4\u9519\u8bef\uff0c\u56e0\u4e3a <code>j<\/code> \u5e76\u4e0d\u662f\u4e00\u4e2a\u5b9a\u4e49\u597d\u7684\u53d8\u91cf\u3002\u6b63\u786e\u7684\u8868\u793a\u65b9\u6cd5\u662f\u4f7f\u7528 <code>1j<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z = 3 + 1j  # \u6b63\u786e\u7684\u8868\u793a\u65b9\u6cd5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u786e\u4fdd\u6b63\u786e\u5730\u8868\u793a\u590d\u6570\u662f\u907f\u514d\u9519\u8bef\u7684\u5173\u952e<\/strong>\u3002\u6b64\u5916\uff0c\u8c03\u8bd5\u65f6\u53ef\u4ee5\u4f7f\u7528 <code>type()<\/code> \u51fd\u6570\u68c0\u67e5\u53d8\u91cf\u7684\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z = 3 + 4j<\/p>\n<p>print(type(z))  # &lt;class &#39;complex&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u590d\u6570\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u83b7\u53d6\u64cd\u4f5c\uff0cPython\u8fd8\u652f\u6301\u4e00\u4e9b\u590d\u6570\u7684\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u4f8b\u5982\u5171\u8f6d\u3001\u6a21\u957f\u548c\u76f8\u89d2\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5171\u8f6d<\/strong>\uff1a\u5171\u8f6d\u590d\u6570\u662f\u6307\u5c06\u590d\u6570\u7684\u865a\u90e8\u53d6\u53cd\u7684\u590d\u6570\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>.conjugate()<\/code> \u65b9\u6cd5\u83b7\u53d6\u5171\u8f6d\u590d\u6570\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">z = 3 + 4j<\/p>\n<p>z_conjugate = z.conjugate()<\/p>\n<p>print(f&quot;\u5171\u8f6d\u590d\u6570: {z_conjugate}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u6a21\u957f<\/strong>\uff1a\u6a21\u957f\u662f\u590d\u6570\u7684\u7edd\u5bf9\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684 <code>abs()<\/code> \u51fd\u6570\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">z = 3 + 4j<\/p>\n<p>z_magnitude = abs(z)<\/p>\n<p>print(f&quot;\u590d\u6570\u6a21\u957f: {z_magnitude}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u76f8\u89d2<\/strong>\uff1a\u76f8\u89d2\u662f\u590d\u6570\u7684\u6781\u5750\u6807\u8868\u793a\u4e2d\u7684\u89d2\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>cmath.phase()<\/code> \u51fd\u6570\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import cmath<\/p>\n<p>z = 3 + 4j<\/p>\n<p>z_phase = cmath.phase(z)<\/p>\n<p>print(f&quot;\u590d\u6570\u76f8\u89d2: {z_phase}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u590d\u6570\u5728\u79d1\u5b66\u8ba1\u7b97\u5e93\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>Python\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5982NumPy\u548cSciPy\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u590d\u6570\u64cd\u4f5c\u529f\u80fd\uff0c\u8fd9\u5bf9\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u5e94\u7528\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>NumPy\u4e2d\u7684\u590d\u6570<\/strong>\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>z = np.array([3 + 4j, 1 + 2j])<\/p>\n<p>real_parts = np.real(z)<\/p>\n<p>imaginary_parts = np.imag(z)<\/p>\n<p>print(f&quot;\u5b9e\u90e8: {real_parts}, \u865a\u90e8: {imaginary_parts}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u4f7f\u5f97\u5904\u7406\u5927\u91cf\u590d\u6570\u6570\u636e\u53d8\u5f97\u975e\u5e38\u9ad8\u6548\u548c\u4fbf\u6377\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>SciPy\u4e2d\u7684\u590d\u6570<\/strong>\uff1a<\/li>\n<\/ul>\n<p><p>SciPy\u6784\u5efa\u5728NumPy\u4e4b\u4e0a\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u7684\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528SciPy\u6765\u6c42\u89e3\u5305\u542b\u590d\u6570\u7684\u7ebf\u6027\u4ee3\u6570\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import linalg<\/p>\n<p>A = np.array([[1 + 2j, 2 + 3j], [3 + 4j, 4 + 5j]])<\/p>\n<p>b = np.array([1 + 1j, 2 + 2j])<\/p>\n<p>x = linalg.solve(A, b)<\/p>\n<p>print(f&quot;\u7ebf\u6027\u65b9\u7a0b\u7ec4\u7684\u89e3: {x}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u590d\u6570\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u4e2d\uff0c\u590d\u6570\u4e5f\u53ef\u4ee5\u53d1\u6325\u91cd\u8981\u4f5c\u7528\u3002\u4f8b\u5982\uff0c\u5728\u67d0\u4e9b\u795e\u7ecf\u7f51\u7edc\u548c\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u4e2d\uff0c\u4f7f\u7528\u590d\u6570\u53ef\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u548c\u8868\u8fbe\u80fd\u529b\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u590d\u6742\u795e\u7ecf\u7f51\u7edc<\/strong>\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u590d\u6570\u795e\u7ecf\u7f51\u7edc\u5c42<\/strong><\/h2>\n<p>class ComplexDense(tf.keras.layers.Layer):<\/p>\n<p>    def __init__(self, units):<\/p>\n<p>        super(ComplexDense, self).__init__()<\/p>\n<p>        self.units = units<\/p>\n<p>    def build(self, input_shape):<\/p>\n<p>        self.real_kernel = self.add_weight(&quot;real_kernel&quot;, shape=[int(input_shape[-1]), self.units])<\/p>\n<p>        self.imag_kernel = self.add_weight(&quot;imag_kernel&quot;, shape=[int(input_shape[-1]), self.units])<\/p>\n<p>    def call(self, inputs):<\/p>\n<p>        real_part = tf.matmul(tf.math.real(inputs), self.real_kernel) - tf.matmul(tf.math.imag(inputs), self.imag_kernel)<\/p>\n<p>        imag_part = tf.matmul(tf.math.real(inputs), self.imag_kernel) + tf.matmul(tf.math.imag(inputs), self.real_kernel)<\/p>\n<p>        return tf.complex(real_part, imag_part)<\/p>\n<h2><strong>\u4f7f\u7528\u8be5\u5c42\u6784\u5efa\u6a21\u578b<\/strong><\/h2>\n<p>model = tf.keras.Sequential([<\/p>\n<p>    ComplexDense(10)<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5904\u7406\u590d\u6570\u53ca\u5176\u5b9e\u90e8\u548c\u865a\u90e8\u7684\u64cd\u4f5c\u975e\u5e38\u7b80\u5355\u548c\u76f4\u89c2\u3002<strong>\u4f7f\u7528\u590d\u6570\u5bf9\u8c61\u7684 <code>.real<\/code> \u548c <code>.imag<\/code> \u5c5e\u6027\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5<\/strong>\uff0c\u6b64\u5916\uff0c<code>cmath<\/code> \u6a21\u5757\u548c\u79d1\u5b66\u8ba1\u7b97\u5e93\uff08\u5982NumPy\u548cSciPy\uff09\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u9ad8\u7ea7\u529f\u80fd\u3002\u65e0\u8bba\u662f\u57fa\u7840\u64cd\u4f5c\u8fd8\u662f\u9ad8\u7ea7\u5e94\u7528\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u5904\u7406\u590d\u6570\uff0c\u4f7f\u5176\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u5de5\u7a0b\u548c\u673a\u5668\u5b66\u4e60\u7b49\u9886\u57df\u5f97\u5230\u4e86\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u590d\u6570\u7c7b\u578b\u6765\u8868\u793a\u590d\u6570\u3002\u901a\u8fc7\u8bbf\u95ee\u590d\u6570\u7684 <code>.real<\/code> \u548c <code>.imag<\/code> \u5c5e\u6027\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5176\u5b9e\u90e8\u548c\u865a\u90e8\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u590d\u6570 <code>z = 3 + 4j<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>z.real<\/code> \u5f97\u5230 <code>3<\/code>\uff0c\u4f7f\u7528 <code>z.imag<\/code> \u5f97\u5230 <code>4<\/code>\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u590d\u6570\uff1f<\/strong><br \/>\u9664\u4e86\u76f4\u63a5\u4f7f\u7528 <code>.real<\/code> \u548c <code>.imag<\/code> \u5c5e\u6027\u5916\uff0cPython\u7684 <code>cmath<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u590d\u6570\u8fd0\u7b97\u529f\u80fd\uff0c\u5305\u62ec\u8ba1\u7b97\u590d\u6570\u7684\u6a21\u3001\u76f8\u4f4d\u548c\u5176\u4ed6\u6570\u5b66\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c<code>cmath.polar(z)<\/code> \u53ef\u4ee5\u8fd4\u56de\u590d\u6570\u7684\u6a21\u548c\u76f8\u4f4d\uff0c<code>cmath.rect(r, phi)<\/code> \u53ef\u4ee5\u5c06\u6781\u5750\u6807\u8f6c\u6362\u4e3a\u590d\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b9e\u90e8\u548c\u865a\u90e8\u5206\u522b\u5b58\u50a8\u5230\u4e24\u4e2a\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u590d\u6570\u5217\u8868\uff0c\u60f3\u8981\u5c06\u5b9e\u90e8\u548c\u865a\u90e8\u5206\u522b\u5b58\u50a8\u5230\u4e24\u4e2a\u72ec\u7acb\u7684\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff0c\u5047\u8bbe <code>complex_numbers<\/code> \u662f\u4e00\u4e2a\u590d\u6570\u5217\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">real_parts = [z.real for z in complex_numbers]  \nimaginary_parts = [z.imag for z in complex_numbers]  \n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u548c\u8bbf\u95ee\u5b9e\u90e8\u548c\u865a\u90e8\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u7c7b\u578b\u6765\u5904\u7406\u548c\u83b7\u53d6\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u3002 Python \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 complex \u7c7b [&hellip;]","protected":false},"author":3,"featured_media":1064306,"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\/1064293"}],"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=1064293"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064293\/revisions"}],"predecessor-version":[{"id":1064307,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064293\/revisions\/1064307"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1064306"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1064293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1064293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1064293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}