Python矩阵与一系列变量相乘

2024-06-01 04:12:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一名国际研究生,最近正在学习光学。 我遇到一个硬件问题,要求我使用Python解决一个光学问题

基本上,我必须乘以三个矩阵并绘制波长(X轴)与最终矩阵的一个分量

所以

我有一个特定的变量范围(500~600),它将是我的X轴

有三个矩阵 I12 L2 I23

它们都是2x2矩阵

在L2矩阵中,使用变量lam(x轴变量)

我想这就是我收到错误信息的原因。 基本上,这个(L2矩阵)应该是一个矩阵数组,但我不知道如何做到这一点,而I12和I23矩阵只是一个没有范围变量的矩阵

[因此最终结果应该与这张照片一样。][1]

希望这仍然是可以理解的。请理解英语不是我的第一语言

我在下面附上了我的代码

import numpy as np
import cmath

lmin, lmax, lstep = 500, 600, 0.1
lam = np.arange(lmin, lmax + lstep, lstep)

d = 5e-6

n1 = 1
n2 = 2.6
n3 = 1.45

r12, t12 = (n1 - n2) / (n1 + n2), (2 * n1) / (n1 + n2)
r23, t23 = (n2 - n3) / (n2 + n3), (2 * n2) / (n2 + n3)

psi = 2 * np.pi * n2 * d / (lam*1e-9)

# Transfer Matrix at the 1st Border (without 1/t12)
I12 = [[1, r12], [r12, 1]]
print(f'I12 Matrix:\n{I12}')

# Transfer Matrix in the Slab
L2 = [[cmath.exp(psi * -1j), 0], [0, cmath.exp(psi * 1j)]] << I got problem here
print(f'L2 Matrix:\n{L2}')

# Transfer Matrix at the 2nd Border (without 1/t23)
I23 = [[1, r23], [r23, 1]]
print(f'I23 Matrix:\n{I23}')

# Total Transfer Matrix: I12 X L2 X I23
coeff = (1 / t12) * (1 / t23)
M1 = np.matmul(L2, I23)

错误代码

I12 Matrix:
[[1, -0.4444444444444445], [-0.4444444444444445, 1]]
Traceback (most recent call last):
  File "D:\OneDrive - Northeastern University\Desktop\SynologyDrive\Northeastern\courses\EECE7284_Optical Properties of Matter\HW5_3.py", line 24, in <module>
    L2 = [[cmath.exp(psi * -1j), 0], [0, cmath.exp(psi * 1j)]]
TypeError: only length-1 arrays can be converted to Python scalars


  [1]: https://i.stack.imgur.com/Vp7Cn.png

Tags: np矩阵matrixtransfercmathn2l2psi
2条回答

我做了

L2 = [[np.exp(psi * -1j), 0], [0, np.exp(psi * 1j)]] 

就像这样,它成功了

问题是psi本身就是一个数组,所以您试图创建一个“3d”L2矩阵。当然,numpy不知道您试图实现什么,并引发了一个错误。 有(至少)两种方法可以解决您的问题:

以符号方式计算最终矩阵,最后替换数值。这可以通过sympy实现:

import sympy as sm
from sympy import symbols

d, lam, n1, n2, n3 = symbols('d, la, n1, n2, n3', real=True, positive=True)
r12, t12 = (n1 - n2) / (n1 + n2), (2 * n1) / (n1 + n2)
r23, t23 = (n2 - n3) / (n2 + n3), (2 * n2) / (n2 + n3)
psi = 2 * sm.pi * n2 * d / (lam)
I12 = sm.Matrix([[1, r12], [r12, 1]])
L2 = sm.Matrix([[sm.exp(psi * -sm.I), 0], [0, sm.exp(psi * sm.I)]])
I23 = sm.Matrix([[1, r23], [r23, 1]])
M1 = L2*I23
print(M1)

给予

Matrix([[exp(-2*I*pi*d*n2/la), (n2 - n3)*exp(-2*I*pi*d*n2/la)/(n2 + n3)], 
        [(n2 - n3)*exp(2*I*pi*d*n2/la)/(n2 + n3), exp(2*I*pi*d*n2/la)]])

现在只需使用您需要的公式。

另一种方法是迭代所有lam值:

import numpy as np

lmin, lmax, lstep = 500, 600, 0.1
lam = np.arange(lmin, lmax + lstep, lstep)

d = 5e-6

n1 = 1
n2 = 2.6
n3 = 1.45

r12, t12 = (n1 - n2) / (n1 + n2), (2 * n1) / (n1 + n2)
r23, t23 = (n2 - n3) / (n2 + n3), (2 * n2) / (n2 + n3)

result = np.zeros(len(lam),dtype=complex)
for ii in range(len(lam)):
    wavelength = lam[ii]
    psi = 2 * np.pi * n2 * d / (wavelength*1e-9)
    
    # Transfer Matrix at the 1st Border (without 1/t12)
    I12 = np.array([[1, r12], [r12, 1]])
    # print(f'I12 Matrix:\n{I12}')
    
    # Transfer Matrix in the Slab
    L2 = np.array([[np.exp(psi * -1j), 0], [0, np.exp(psi * 1j)]])
    # print(f'L2 Matrix:\n{L2}')
    
    # Transfer Matrix at the 2nd Border (without 1/t23)
    I23 = np.array([[1, r23], [r23, 1]])
    # print(f'I23 Matrix:\n{I23}')
    
    # Total Transfer Matrix: I12 X L2 X I23
    # coeff = (1 / t12) * (1 / t23)
    M1 = np.matmul(L2, I23)

    # get the value you need from M1
    result[ii] = M1[0][1]

result变量现在包含lam范围内的计算值。有更有效和/或更优雅的方法可以在不使用for的情况下使用numpy进行迭代,但我认为在这种情况下没有必要使用它们

相关问题 更多 >