![](/img/trans.png)
[英]Numpy array assignment with complex values gets error: “ComplexWarning: Casting complex values to real discards the imaginary part”
[英]How to work out ''ComplexWarning: Casting complex values to real discards the imaginary part''?
我想使用具有復雜條目的矩陣來構造一個新矩陣,但它給了我警告
“復雜警告:將復雜的值轉換為真實的丟棄虛部”。
因此,新矩陣的條目都是實數。 如何使所有條目保持其想象的部分?
這是包含我原始代碼的圖像
您可以嘗試使用numpy從Matrix_1使用數組構造函數創建新矩陣。 這樣,您可以指定使用帶有dtype
復數。 像這樣的東西應該做的伎倆:
mat_ = np.array(matrix_1[:,ll,ll,:],dtype = "complex_")
我曾經做過的一些測試,以確保:
import numpy as np
abc = np.array([[1+1j,2+2j,3+3j],[4+4j,5+5j,6+6j],[7+7j,8+8j,9+9j]],dtype = "complex_") # initial
print(abc)
abc2 = np.array(abc[1:,:1],dtype = "complex_") # post transformation
print(abc2)
我知道我在哪里發出這個警告。
當我構造一個新的空矩陣時,我需要添加dtype =“complex_”。
例如,
mat__ = np.zeros((k*nboxes,k*nboxes),dtype = 'complex_')
如果我們寫的話
mat__ = np.zeros((k*nboxes,k*nboxes))
並且使用復雜矩陣來形成新矩陣,然后新矩陣將丟棄虛部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.