Hướng dẫn python complex square root - căn bậc hai phức của python
I have run across some confusing behaviour with square roots of complex numbers in python. Running this code: Show
Nội dung chính ShowShow
gives the output
A similar thing happens with
It appears these pairs of statements should give the same answer? asked Apr 12, 2016 at 21:32Apr 12, 2016 at 21:32 5 Both answers ( 5 and 6) are correct, since they are complex conjugates -- i.e. the real part is identical, and the imaginary part is sign-flipped.Looking at the code makes the behavior clear - the imaginary part of the result always has the same sign as the imaginary part of the input, as seen in lines 790 and 793:
Since 7 is 8 which is implicitly 9 you get a positive result; 0 produces 1 (for some reason; not sure why it doesn't result in 9 tbh) so your result is similarly negative. EDIT: This question has some useful discussion on the same issue. answered Apr 12, 2016 at 22:03Apr 12, 2016 at 22:03 tzamantzamantzaman 45.2k11 gold badges88 silver badges112 bronze badges11 gold badges88 silver badges112 bronze badges 5 I can answer why this is happening, but not why the behavior was chosen.
evaluates to 0.2/-0.8 which is -0.25, which is converted to a complex number by 3, while
evaluates to (0.2+0j)/(-0.8+0j) which is (-0.25-0j), which is converted to a complex number with a negative complex component. For a simpler example,
answered Apr 12, 2016 at 22:03Apr 12, 2016 at 22:03 tzamantzamanMatt Jordan 45.2k11 gold badges88 silver badges112 bronze badges8 silver badges10 bronze badges I can answer why this is happening, but not why the behavior was chosen.numpy.sqrt() được sử dụng để trả về căn bậc hai không âm của một phần tử mảng (cho mỗi phần tử của mảng). Trong bài viết này, tôi sẽ giải thích cách sử dụng căn bậc hai Numpy bằng cách sử dụng numpy.sqrt() các ví dụ. evaluates to 0.2/-0.8 which is -0.25, which is converted to a complex number by 3, while
evaluates to 0.2/-0.8 which is -0.25, which is converted to a complex number by 3, while
evaluates to 0.2/-0.8 which is -0.25, which is converted to a complex number by 3, while
evaluates to 0.2/-0.8 which is -0.25, which is converted to a complex number by 3, while
evaluates to 0.2/-0.8 which is -0.25, which is converted to a complex number by 3, while
1. Ví dụ nhanh về Gốc vuông Python NumPyNếu bạn đang vội, dưới đây là một số ví dụ nhanh về cách sử dụng hàm căn bậc hai Python NumPy. # Below are a quick examples # Example 1: numpy.sqrt() of single element arr2 = np.sqrt(45) # Example 2: Use numpy.sqrt() function to square root of numbers arr = [25, 49, 225, 64, 81, 16] arr2 = np.sqrt(arr) # Example 3: Use numpy.sqrt() function with complex numbers arr = [2+6j, -5-8j, 4-5j, 3+4j] arr2 = np.sqrt(arr) # Example 4: Use numpy.sqrt() function with negative and inifite as input values arr = [-6, np.inf, 25, -15, np.inf] arr2 = np.sqrt(arr) # Example 5: Use numpy.sqrt() function to floating-point array arr = [4.3, 8.5, 15.1, 23.7, 14.2, 7.8] arr2 = np.sqrt(arr) # Example 6: Use numpy.sqrt() function to square root from multiple array arr = np.array([[25, 64, 9, 16], [9, 4, 49, 36]]) arr2 = np.sqrt(arr) 2. Cú pháp Python NumPy Square RootSau đây là cú pháp của numpy.sqrt().numpy.sqrt(). # Syntax of python numpy.sqrt() numpy.sqrt(arr, out=None, where=True, casting='same_kind', order="K", dtype=None) 2.1 Các tham số của sqrt ()
2.2 Giá trị trả về của sqrt ()3. Cách sử dụng Hàm Gốc vuông NumPy : Nó trả về một mảng căn bậc hai của số trong mảng đầu vào. 3. Cách sử dụng Hàm Gốc vuông NumPy4. Nhận nhiều giá trị căn bậc hai của mảng NumPynumpy.sqrt(). Bạn cũng có thể lấy các giá trị bình phương của mảng NumPy bằng cách sử dụng numpy.square(). import numpy as np # Create a single element arr = np.array(25) # Use numpy.sqrt() function to get single element arr2 = np.sqrt(arr) print(arr2) # Output: [5.0] # get single element square root value arr2 = np.sqrt(45) print(arr2) # Output : # 6.708203932499369 4. Nhận nhiều giá trị căn bậc hai của mảng NumPy5. Nhận gốc hình vuông của các số phứcnumpy.array() và tính căn bậc hai của những số này bằng cách sử dụng numpy.sqrt(). Ví dụ, 05. Nhận gốc hình vuông của các số phức6. Nhận gốc vuông của các giá trị tiêu cực và vô hạnnumpy.sqrt(). Ví dụ, 16. Nhận gốc vuông của các giá trị tiêu cực và vô hạn7. Lấy căn bậc hai của Mảng NumPy với Giá trị Float numpy.sqrt() bạn cũng có thể tính căn bậc hai của giá trị âm và Vô hạn dưới dạng giá trị đầu vào của một mảng. Căn bậc hai của ma trận có số âm sẽ ném RuntimeWarning và căn bậc hai của phần tử được trả về là nan kết quả là. 27. Lấy căn bậc hai của Mảng NumPy với Giá trị Float8. Nhận gốc vuông của giá trị mảng 2-D NumPynumpy.sqrt(). 38. Nhận gốc vuông của giá trị mảng 2-D NumPy9. Kết luậnnumpy.sqrt(). 49. Kết luậnBạn cũng có thể thíchnumpy.sqrt() để tính căn bậc hai của mọi phần tử trong mảng đã cho với các ví dụ. Người giới thiệu Bạn cũng có thể thíchNgười giới thiệu |