我正在编写一些代码,以将单端参数转换为混合模式参数。基本上,具有1xm数组元素的nxn矩阵需要与nxn标量矩阵相乘。
这是我的丑陋代码,有效,我注释掉了我做张量乘法失败的尝试。 “ SS”函数返回一个1xm数组。我也有这些丑陋的初始化矩阵MM和MM2,我不知道该如何避免。
def SS(snp,x,y):
return np.squeeze(eval("snp.s"+str(x)+str(y)+".s_db"))
def convert_mixmode(snp, portmap):
result = int(file[file.find(".s")+len(".s"):file.rfind("p")])
S = []
M = 1/np.sqrt(2)*np.matrix([[1,-1,0,0],[1,1,0,0],[0,0,1,-1],[0,0,1,1]])
MI = np.linalg.inv(M)
m=0; l=0;
while l < result:
A = portmap[m][0]; B = portmap[m][1]; C= portmap[m+1][0]; D = portmap[m+1][1];
S = np.array([[SS(snp,A,A),SS(snp,A,C),SS(snp,A,B),SS(snp,A,D)],[SS(snp,C,A),SS(snp,B,B),SS(snp,C,B),SS(snp,B,D)],[SS(snp,B,A),SS(snp,B,C),SS(snp,C,C),SS(snp,B,D)],[SS(snp,D,A),SS(snp,D,C),SS(snp,D,B),SS(snp,D,D)]])
MM = np.array([[np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2])],[np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2])],[np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2])],[np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2])]])
#MM = np.tensordot(M,np.tensordot(S,np.linalg.inv(M),axes=([0],[0])),axes=([0],[0]))
for i in range(4):
for j in range(4):
for k in range(4):
MM[i,j]=MM[i,j]+S[i,k]*MI[k,j]
MM2 = np.array([[np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2])],[np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2])],[np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2])],[np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2]),np.zeros(S.shape[2])]])
for i in range(4):
for j in range(4):
for k in range(4):
MM2[i,j]=MM2[i,j]+MM[i,k]*M[k,j]
plt.figure()
plt.plot(MM2[0,2])
plt.show()
m+=1; l+=4
return
portmap1 = [[1,2],[3,4],[5,6],[7,8],[9,10],[11,12],[13,14],[15,16]]
portmap2 = [[1,3],[2,4],[5,7],[6,8],[9,11],[10,12],[13,15],[14,16]]
portmap3 = [[1,9],[2,10],[3,11],[4,12],[5,13],[6,14],[7,15],[8,16]]
for file in files:
print(file)
snp = rf.Network(file)
convert_mixmode(snp, portmap1)
我现在的结果应该是4x4矩阵。最终,它将是一堆4x4矩阵的串联,构成一个4x4m或4mx4矩阵,其中snp文件中m = n / 4。有什么想法或建议吗?