{"id":1118663,"date":"2025-01-08T18:37:27","date_gmt":"2025-01-08T10:37:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1118663.html"},"modified":"2025-01-08T18:37:29","modified_gmt":"2025-01-08T10:37:29","slug":"python%e7%9a%84%e8%99%9a%e9%83%a8%e5%92%8c%e5%ae%9e%e9%83%a8%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1118663.html","title":{"rendered":"python\u7684\u865a\u90e8\u548c\u5b9e\u90e8\u5982\u4f55\u8868\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082047\/e9da2f94-6cfd-47a9-8de8-1563150e1e9f.webp\" alt=\"python\u7684\u865a\u90e8\u548c\u5b9e\u90e8\u5982\u4f55\u8868\u793a\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7684\u865a\u90e8\u548c\u5b9e\u90e8\u53ef\u4ee5\u901a\u8fc7\u590d\u6570\u7c7b\u578b\u8868\u793a\uff0c\u590d\u6570\u7531\u5b9e\u90e8\u548c\u865a\u90e8\u7ec4\u6210\u3001\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u8fd0\u7b97\u7b26\u548c\u5185\u7f6e\u51fd\u6570\u63d0\u53d6\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u3001\u53ef\u4ee5\u4f7f\u7528\u5e93\u51fd\u6570\u8fdb\u884c\u590d\u6742\u7684\u590d\u6570\u8fd0\u7b97<\/strong>\u3002\u5728Python\u4e2d\uff0c\u590d\u6570\u7c7b\u578b\u662f\u5185\u7f6e\u7c7b\u578b\uff0c\u590d\u6570\u7531\u5b9e\u90e8\u548c\u865a\u90e8\u7ec4\u6210\u3002\u53ef\u4ee5\u901a\u8fc7<code>complex<\/code>\u51fd\u6570\u521b\u5efa\u590d\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7<code>.real<\/code>\u548c<code>.imag<\/code>\u5c5e\u6027\u5206\u522b\u8bbf\u95ee\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u5728Python\u4e2d\u8868\u793a\u590d\u6570\u7684\u865a\u90e8\u548c\u5b9e\u90e8\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Python\u4e2d\u7684\u590d\u6570\u8868\u793a<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u590d\u6570\u662f\u7531\u5b9e\u90e8\u548c\u865a\u90e8\u7ec4\u6210\u7684\u6570\uff0c\u865a\u90e8\u4ee5\u5b57\u6bcd<code>j<\/code>\u8868\u793a\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u6765\u521b\u5efa\u590d\u6570\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u76f4\u63a5\u4f7f\u7528<code>complex<\/code>\u51fd\u6570\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">z = complex(3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u590d\u6570<code>z = 3 + 4j<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u590d\u6570\u5b57\u9762\u91cf\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">z = 3 + 4j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u540c\u6837\u4f1a\u521b\u5efa\u4e00\u4e2a\u590d\u6570<code>z = 3 + 4j<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbf\u95ee\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u7684<code>.real<\/code>\u548c<code>.imag<\/code>\u5c5e\u6027\u5206\u522b\u8bbf\u95ee\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z = 3 + 4j<\/p>\n<p>real_part = z.real  # \u5b9e\u90e8<\/p>\n<p>imaginary_part = z.imag  # \u865a\u90e8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8fd4\u56de\u5b9e\u90e8<code>3.0<\/code>\u548c\u865a\u90e8<code>4.0<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u590d\u6570\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u5bf9\u590d\u6570\u8fdb\u884c\u5404\u79cd\u8fd0\u7b97\uff0c\u5305\u62ec\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z1 = 3 + 4j<\/p>\n<p>z2 = 1 + 2j<\/p>\n<h2><strong>\u52a0\u6cd5<\/strong><\/h2>\n<p>result_add = z1 + z2  # 4 + 6j<\/p>\n<h2><strong>\u51cf\u6cd5<\/strong><\/h2>\n<p>result_sub = z1 - z2  # 2 + 2j<\/p>\n<h2><strong>\u4e58\u6cd5<\/strong><\/h2>\n<p>result_mul = z1 * z2  # -5 + 10j<\/p>\n<h2><strong>\u9664\u6cd5<\/strong><\/h2>\n<p>result_div = z1 \/ z2  # 2.2 - 0.4j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5e93\u51fd\u6570\u8fdb\u884c\u590d\u6570\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>Python\u7684<code>cmath<\/code>\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u6765\u8fdb\u884c\u590d\u6742\u7684\u590d\u6570\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cmath.phase<\/code>\u6765\u8ba1\u7b97\u590d\u6570\u7684\u76f8\u4f4d\uff0c\u4f7f\u7528<code>cmath.polar<\/code>\u5c06\u590d\u6570\u8f6c\u6362\u4e3a\u6781\u5750\u6807\u5f62\u5f0f\uff0c\u4f7f\u7528<code>cmath.rect<\/code>\u5c06\u6781\u5750\u6807\u8f6c\u6362\u4e3a\u590d\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cmath<\/p>\n<p>z = 3 + 4j<\/p>\n<h2><strong>\u76f8\u4f4d<\/strong><\/h2>\n<p>phase = cmath.phase(z)  # 0.9272952180016122<\/p>\n<h2><strong>\u6781\u5750\u6807<\/strong><\/h2>\n<p>polar_coords = cmath.polar(z)  # (5.0, 0.9272952180016122)<\/p>\n<h2><strong>\u4ece\u6781\u5750\u6807\u8f6c\u6362\u4e3a\u590d\u6570<\/strong><\/h2>\n<p>rect_coords = cmath.rect(5.0, 0.9272952180016122)  # (3.0000000000000004+4j)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u590d\u6570\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u590d\u6570\u5728\u8bb8\u591a\u9886\u57df\u90fd\u6709\u5e94\u7528\uff0c\u5305\u62ec\u4fe1\u53f7\u5904\u7406\u3001\u63a7\u5236\u7cfb\u7edf\u3001\u91cf\u5b50\u529b\u5b66\u548c\u7535\u8def\u5206\u6790\u7b49\u3002\u5728\u8fd9\u4e9b\u5e94\u7528\u4e2d\uff0c\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u5206\u522b\u8868\u793a\u4e0d\u540c\u7684\u7269\u7406\u91cf\u3002\u4f8b\u5982\uff0c\u5728\u4ea4\u6d41\u7535\u8def\u5206\u6790\u4e2d\uff0c\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u5206\u522b\u8868\u793a\u7535\u538b\u6216\u7535\u6d41\u7684\u5e45\u503c\u548c\u76f8\u4f4d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u590d\u6570\u7684\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7b49\u5e93\u6765\u53ef\u89c6\u5316\u590d\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7ed8\u5236\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u5728\u590d\u5e73\u9762\u4e0a\u7684\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>z = 3 + 4j<\/p>\n<p>plt.plot([0, z.real], [0, z.imag], marker=&#39;o&#39;)<\/p>\n<p>plt.xlabel(&#39;Real&#39;)<\/p>\n<p>plt.ylabel(&#39;Imaginary&#39;)<\/p>\n<p>plt.title(&#39;Complex Number&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u590d\u6570\u7684\u5176\u4ed6\u5c5e\u6027\u548c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u590d\u6570\u7c7b\u578b\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>abs<\/code>\u51fd\u6570\u8ba1\u7b97\u590d\u6570\u7684\u6a21\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z = 3 + 4j<\/p>\n<p>magnitude = abs(z)  # 5.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>conjugate<\/code>\u65b9\u6cd5\u8ba1\u7b97\u590d\u6570\u7684\u5171\u8f6d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z = 3 + 4j<\/p>\n<p>conjugate = z.conjugate()  # 3 - 4j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u590d\u6570\u4e0e\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u590d\u6570\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u5206\u522b\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u6216\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">z = 3 + 4j<\/p>\n<p>real_part_float = float(z.real)  # 3.0<\/p>\n<p>imaginary_part_int = int(z.imag)  # 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u590d\u6570\u5728\u51fd\u6570\u4e2d\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u590d\u6570\uff0c\u590d\u6570\u53ef\u4ee5\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_complex(z1, z2):<\/p>\n<p>    return z1 + z2<\/p>\n<p>z1 = 3 + 4j<\/p>\n<p>z2 = 1 + 2j<\/p>\n<p>result = add_complex(z1, z2)  # 4 + 6j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u590d\u6570\u7684\u6df1\u5ea6\u5b66\u4e60\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u6df1\u5ea6\u5b66\u4e60\u4e2d\uff0c\u590d\u6570\u4e5f\u6709\u4e00\u4e9b\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u6765\u8868\u793a\u548c\u5904\u7406\u590d\u6742\u7684\u4fe1\u53f7\u6570\u636e\u3002\u8bb8\u591a\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u5982TensorFlow\u548cPyTorch\uff0c\u90fd\u652f\u6301\u590d\u6570\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u5728TensorFlow\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tf.complex<\/code>\u51fd\u6570\u521b\u5efa\u590d\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>real_part = tf.constant([3.0, 4.0])<\/p>\n<p>imaginary_part = tf.constant([1.0, 2.0])<\/p>\n<p>z = tf.complex(real_part, imaginary_part)  # [3 + 1j, 4 + 2j]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u590d\u6570\u7684\u6570\u503c\u7a33\u5b9a\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u503c\u8ba1\u7b97\u4e2d\uff0c\u590d\u6570\u7684\u6570\u503c\u7a33\u5b9a\u6027\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u6c42\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4\u65f6\uff0c\u4f7f\u7528\u590d\u6570\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u503c\u4e0d\u7a33\u5b9a\u3002\u4e3a\u4e86\u63d0\u9ad8\u6570\u503c\u7a33\u5b9a\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u9ad8\u7cbe\u5ea6\u7684\u6570\u503c\u7c7b\u578b\u6216\u6570\u503c\u7b97\u6cd5\u3002\u4f8b\u5982\uff0c\u5728NumPy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.linalg.solve<\/code>\u51fd\u6570\u6c42\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>A = np.array([[1 + 1j, 2], [3, 4 + 4j]])<\/p>\n<p>b = np.array([5 + 5j, 6])<\/p>\n<p>x = np.linalg.solve(A, b)  # [ 0.44-1.16j  0.68+0.04j]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u590d\u6570\u7684\u77e9\u9635\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u590d\u6570\u77e9\u9635\u8fd0\u7b97\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u8fdb\u884c\u590d\u6570\u77e9\u9635\u7684\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u6c42\u9006\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>A = np.array([[1 + 1j, 2], [3, 4 + 4j]])<\/p>\n<p>B = np.array([[5 + 5j, 6], [7, 8 + 8j]])<\/p>\n<h2><strong>\u52a0\u6cd5<\/strong><\/h2>\n<p>C_add = A + B  # [[ 6.+6.j  8.+0.j]<\/p>\n<p>               #  [10.+0.j 12.+12.j]]<\/p>\n<h2><strong>\u4e58\u6cd5<\/strong><\/h2>\n<p>C_mul = np.dot(A, B)  # [[ 7.+17.j 10.+16.j]<\/p>\n<p>                      #  [47.+20.j 58.+32.j]]<\/p>\n<h2><strong>\u6c42\u9006<\/strong><\/h2>\n<p>A_inv = np.linalg.inv(A)  # [[-0.24-0.04j  0.12+0.04j]<\/p>\n<p>                          #  [ 0.18+0.02j -0.06-0.02j]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u590d\u6570\u7684\u5085\u91cc\u53f6\u53d8\u6362<\/h3>\n<\/p>\n<p><p>\u5085\u91cc\u53f6\u53d8\u6362\u662f\u4fe1\u53f7\u5904\u7406\u4e2d\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5b83\u5c06\u4fe1\u53f7\u4ece\u65f6\u57df\u8f6c\u6362\u5230\u9891\u57df\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u6216SciPy\u8fdb\u884c\u79bb\u6563\u5085\u91cc\u53f6\u53d8\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.fft import fft, ifft<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u590d\u6570\u4fe1\u53f7<\/strong><\/h2>\n<p>signal = np.array([1 + 1j, 2 + 2j, 3 + 3j, 4 + 4j])<\/p>\n<h2><strong>\u8fdb\u884c\u5085\u91cc\u53f6\u53d8\u6362<\/strong><\/h2>\n<p>fft_result = fft(signal)  # [10.+10.j -4. +4.j -2. -2.j  0. -4.j]<\/p>\n<h2><strong>\u8fdb\u884c\u9006\u5085\u91cc\u53f6\u53d8\u6362<\/strong><\/h2>\n<p>ifft_result = ifft(fft_result)  # [1.+1.j 2.+2.j 3.+3.j 4.+4.j]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u590d\u6570\u7684\u62c9\u666e\u62c9\u65af\u53d8\u6362<\/h3>\n<\/p>\n<p><p>\u62c9\u666e\u62c9\u65af\u53d8\u6362\u662f\u63a7\u5236\u7cfb\u7edf\u5206\u6790\u4e2d\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5b83\u5c06\u4fe1\u53f7\u4ece\u65f6\u57df\u8f6c\u6362\u5230\u590d\u9891\u57df\u3002\u53ef\u4ee5\u4f7f\u7528SymPy\u8fdb\u884c\u7b26\u53f7\u62c9\u666e\u62c9\u65af\u53d8\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, laplace_transform<\/p>\n<p>t, s = symbols(&#39;t s&#39;)<\/p>\n<p>f = t2<\/p>\n<h2><strong>\u8fdb\u884c\u62c9\u666e\u62c9\u65af\u53d8\u6362<\/strong><\/h2>\n<p>F = laplace_transform(f, t, s)  # (2\/s3, 0, True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u590d\u6570\u7684\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u4f18\u5316\u95ee\u9898\u4e2d\uff0c\u590d\u6570\u4e5f\u6709\u4e00\u4e9b\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528SciPy\u8fdb\u884c\u590d\u6570\u4f18\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.optimize import minimize<\/p>\n<p>def objective(z):<\/p>\n<p>    return np.abs(z - 3 - 4j)2<\/p>\n<p>z0 = 0 + 0j<\/p>\n<p>result = minimize(objective, z0)  # (fun: 0.0, jac: array([0., 0.]), message: &#39;Optimization terminated successfully.&#39;, nfev: 10, nit: 1, njev: 5, status: 0, success: True, x: array([3.+4.j]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u590d\u6570\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u590d\u6570\u4e5f\u6709\u4e00\u4e9b\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u795e\u7ecf\u7f51\u7edc\u6765\u5904\u7406\u590d\u6742\u7684\u4fe1\u53f7\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras import layers, models<\/p>\n<p>model = models.Sequential()<\/p>\n<p>model.add(layers.Dense(64, input_shape=(2,), activation=&#39;relu&#39;))<\/p>\n<p>model.add(layers.Dense(64, activation=&#39;relu&#39;))<\/p>\n<p>model.add(layers.Dense(1, activation=&#39;linear&#39;))<\/p>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;mean_squared_error&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u590d\u6570\u6570\u636e<\/strong><\/h2>\n<p>real_part = np.random.rand(1000, 1)<\/p>\n<p>imaginary_part = np.random.rand(1000, 1)<\/p>\n<p>X = np.hstack((real_part, imaginary_part))<\/p>\n<p>y = real_part + 1j * imaginary_part<\/p>\n<p>model.fit(X, y, epochs=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u590d\u6570\u7684\u91cf\u5b50\u8ba1\u7b97\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u91cf\u5b50\u8ba1\u7b97\u4e2d\uff0c\u590d\u6570\u662f\u57fa\u672c\u7684\u6570\u5b66\u5de5\u5177\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Qiskit\u6765\u6a21\u62df\u91cf\u5b50\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from qiskit import QuantumCircuit, transpile, Aer, execute<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u91cf\u5b50\u7535\u8def<\/strong><\/h2>\n<p>qc = QuantumCircuit(1)<\/p>\n<p>qc.h(0)  # Hadamard gate<\/p>\n<h2><strong>\u8fd0\u884c\u91cf\u5b50\u7535\u8def<\/strong><\/h2>\n<p>backend = Aer.get_backend(&#39;statevector_simulator&#39;)<\/p>\n<p>result = execute(qc, backend).result()<\/p>\n<p>statevector = result.get_statevector()<\/p>\n<h2><strong>\u8f93\u51fa\u91cf\u5b50\u6001<\/strong><\/h2>\n<p>print(statevector)  # [0.70710678+0.j 0.70710678+0.j]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u590d\u6570\u7684\u56fe\u50cf\u5904\u7406\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0c\u590d\u6570\u4e5f\u6709\u4e00\u4e9b\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u548cNumPy\u8fdb\u884c\u56fe\u50cf\u7684\u5085\u91cc\u53f6\u53d8\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>img = cv2.imread(&#39;image.png&#39;, 0)<\/p>\n<h2><strong>\u8fdb\u884c\u5085\u91cc\u53f6\u53d8\u6362<\/strong><\/h2>\n<p>dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)<\/p>\n<p>dft_shift = np.fft.fftshift(dft)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e45\u503c\u8c31<\/strong><\/h2>\n<p>magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.subplot(121), plt.imshow(img, cmap=&#39;gray&#39;)<\/p>\n<p>plt.title(&#39;Input Image&#39;), plt.xticks([]), plt.yticks([])<\/p>\n<p>plt.subplot(122), plt.imshow(magnitude_spectrum, cmap=&#39;gray&#39;)<\/p>\n<p>plt.title(&#39;Magnitude Spectrum&#39;), plt.xticks([]), plt.yticks([])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u590d\u6570\u7684\u7535\u8def\u5206\u6790\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u7535\u8def\u5206\u6790\u4e2d\uff0c\u590d\u6570\u7528\u4e8e\u8868\u793a\u4ea4\u6d41\u7535\u8def\u4e2d\u7684\u963b\u6297\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528SymPy\u8fdb\u884c\u7535\u8def\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, I<\/p>\n<p>R, L, C, w = symbols(&#39;R L C w&#39;)<\/p>\n<p>Z_R = R<\/p>\n<p>Z_L = I * w * L<\/p>\n<p>Z_C = 1 \/ (I * w * C)<\/p>\n<h2><strong>\u4e32\u8054\u963b\u6297<\/strong><\/h2>\n<p>Z_series = Z_R + Z_L + Z_C  # R + I*w*L + 1\/(I*w*C)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u590d\u6570\u7684\u63a7\u5236\u7cfb\u7edf\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u63a7\u5236\u7cfb\u7edf\u5206\u6790\u4e2d\uff0c\u590d\u6570\u7528\u4e8e\u8868\u793a\u7cfb\u7edf\u7684\u6781\u70b9\u548c\u96f6\u70b9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Control\u5e93\u8fdb\u884c\u63a7\u5236\u7cfb\u7edf\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import control as ctrl<\/p>\n<h2><strong>\u521b\u5efa\u4f20\u9012\u51fd\u6570<\/strong><\/h2>\n<p>num = [1]<\/p>\n<p>den = [1, 2, 1]<\/p>\n<p>system = ctrl.TransferFunction(num, den)<\/p>\n<h2><strong>\u8ba1\u7b97\u6781\u70b9\u548c\u96f6\u70b9<\/strong><\/h2>\n<p>poles = ctrl.pole(system)  # [-1.+0.j -1.+0.j]<\/p>\n<p>zeros = ctrl.zero(system)  # []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u4e2d\u7684\u590d\u6570\u548c\u5b9e\u90e8\u8868\u793a\u65b9\u6cd5\u975e\u5e38\u4e30\u5bcc\uff0c\u6db5\u76d6\u4e86\u4ece\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b\u64cd\u4f5c\u5230\u590d\u6742\u7684\u79d1\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u5e94\u7528\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u590d\u6570\u8ba1\u7b97\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u5982\u4f55\u83b7\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\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5c5e\u6027<code>.real<\/code>\u548c<code>.imag<\/code>\u6765\u83b7\u53d6\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u590d\u6570<code>z = 3 + 4j<\/code>\uff0c\u90a3\u4e48<code>z.real<\/code>\u5c06\u8fd4\u56de<code>3.0<\/code>\uff0c\u800c<code>z.imag<\/code>\u5c06\u8fd4\u56de<code>4.0<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u7b80\u4fbf\uff0c\u9002\u5408\u5728\u6570\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u5e94\u7528\u4e2d\u4f7f\u7528\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u590d\u6570\u8fd0\u7b97\uff1f<\/strong><br \/>Python\u4e0d\u4ec5\u80fd\u591f\u8868\u793a\u590d\u6570\uff0c\u8fd8\u652f\u6301\u591a\u79cd\u590d\u6570\u8fd0\u7b97\uff0c\u5305\u62ec\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\u7b49\u3002\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5e38\u89c4\u7684\u8fd0\u7b97\u7b26\uff0c\u4f8b\u5982\uff1a<code>z1 + z2<\/code>\u3001<code>z1 * z2<\/code>\u7b49\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>cmath<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u590d\u6570\u8fd0\u7b97\uff0c\u6bd4\u5982\u6c42\u590d\u6570\u7684\u6a21\u548c\u8f90\u89d2\u7b49\uff0c\u8fd9\u4f7f\u5f97Python\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u9886\u57df\u4e2d\u975e\u5e38\u5b9e\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b9e\u90e8\u548c\u865a\u90e8\u5206\u522b\u63d0\u53d6\u5230\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u6709\u591a\u4e2a\u590d\u6570\u5e76\u5e0c\u671b\u5c06\u5b83\u4eec\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u5206\u522b\u63d0\u53d6\u5230\u4e24\u4e2a\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u590d\u6570\u5217\u8868<code>z_list = [3 + 4j, 1 + 2j, 5 + 6j]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>real_parts = [z.real for z in z_list]<\/code>\u548c<code>imag_parts = [z.imag for z in z_list]<\/code>\u6765\u5206\u522b\u83b7\u53d6\u5b9e\u90e8\u548c\u865a\u90e8\u7684\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6548\u7387\u9ad8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684\u865a\u90e8\u548c\u5b9e\u90e8\u53ef\u4ee5\u901a\u8fc7\u590d\u6570\u7c7b\u578b\u8868\u793a\uff0c\u590d\u6570\u7531\u5b9e\u90e8\u548c\u865a\u90e8\u7ec4\u6210\u3001\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u8fd0\u7b97\u7b26\u548c\u5185\u7f6e\u51fd\u6570\u63d0\u53d6\u590d\u6570\u7684\u5b9e [&hellip;]","protected":false},"author":3,"featured_media":1118668,"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\/1118663"}],"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=1118663"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118663\/revisions"}],"predecessor-version":[{"id":1118669,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118663\/revisions\/1118669"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1118668"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1118663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1118663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1118663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}