数组维度不同时如何计算表达式
How to evaluate an expression when array dimensions are different
我想评估以下功能:
inclination=np.pi/6
def power(inclination,phi):
h1=1.7
h2=0.5
D = np.arange(0.5, 12.0, 0.1)
r = np.sqrt((h1-h2)**2 + D**2)
freq = 865.7
lmb = 300/freq
H = D**2/(D**2+2*h1*h2)
theta = 4*np.pi*h1*h2/(lmb*D)
q_e = H**2*(np.sin(theta))**2 + (1 - H*np.cos(theta))**2
sigma = 1.94
N_1 = np.random.normal(0,sigma,D.shape)
rnd = 10**(-N_1/10)
F = 10
power=0.2
alpha=inclination + np.arcsin((h1-h2)/r)
gain=3.136*(np.tan(alpha)*np.sin(np.pi/2*np.cos(alpha)*np.sin(phi)))**2
y=10*np.log10( 1000*(power*gain*1.622*((lmb)**2) *0.5*1) / (((4*np.pi*r)**2) *1.2*1*F)*q_e*rnd )
return y
问题是调用函数returns出错。
phi=np.arange(-np.pi/2, np.pi/2, np.pi/32)
power(np.pi/6,phi)
ValueError Traceback (most recent call last)
<ipython-input-32-268e3287b75a> in <module>()
----> 1 power(np.pi/6,phi)
<ipython-input-30-4b89d23682ce> in power(inclination, phi)
19
20 alpha=inclination + np.arcsin((h1-h2)/r)
---> 21 gain=3.136*(np.tan(alpha)*np.sin(np.pi/2*np.cos(alpha)*np.sin(phi)))**2
22 print(gain)
23 y=10*np.log10( 1000*(power*gain*1.622*((lmb)**2) *0.5*1) / (((4*np.pi*r)**2) *1.2*1*F)*q_e*rnd )
ValueError: operands could not be broadcast together with shapes (115,) (32,)
可能是因为它抱怨 gain
行中的操作具有不同维度的矩阵 alpha
和 phi
。
问题是我想使用不同的矩阵维度,并保存一个 txt
文件,其中第一列是 r
,第二列是 phi
,第三列是 y
的值].
您需要向其中一个数组添加维度才能启用 broadcasting
>>> alpha = np.ones(115, dtype=np.uint8)
>>> phi = np.ones(32, dtype=np.uint8)
>>> c = alpha[:,None] * phi
>>> alpha.shape, alpha[:,None].shape, phi.shape, c.shape
((115,), (115, 1), (32,), (115, 32))
假设 alpha.shape
是 (115,)
:
`
>>> gain=3.136*(np.tan(alpha[:,None])*np.sin(np.pi/2*np.cos(alpha[:,None])*np.sin(phi)))**2
>>> gain.shape
(115, 32)
我想评估以下功能:
inclination=np.pi/6
def power(inclination,phi):
h1=1.7
h2=0.5
D = np.arange(0.5, 12.0, 0.1)
r = np.sqrt((h1-h2)**2 + D**2)
freq = 865.7
lmb = 300/freq
H = D**2/(D**2+2*h1*h2)
theta = 4*np.pi*h1*h2/(lmb*D)
q_e = H**2*(np.sin(theta))**2 + (1 - H*np.cos(theta))**2
sigma = 1.94
N_1 = np.random.normal(0,sigma,D.shape)
rnd = 10**(-N_1/10)
F = 10
power=0.2
alpha=inclination + np.arcsin((h1-h2)/r)
gain=3.136*(np.tan(alpha)*np.sin(np.pi/2*np.cos(alpha)*np.sin(phi)))**2
y=10*np.log10( 1000*(power*gain*1.622*((lmb)**2) *0.5*1) / (((4*np.pi*r)**2) *1.2*1*F)*q_e*rnd )
return y
问题是调用函数returns出错。
phi=np.arange(-np.pi/2, np.pi/2, np.pi/32)
power(np.pi/6,phi)
ValueError Traceback (most recent call last)
<ipython-input-32-268e3287b75a> in <module>()
----> 1 power(np.pi/6,phi)
<ipython-input-30-4b89d23682ce> in power(inclination, phi)
19
20 alpha=inclination + np.arcsin((h1-h2)/r)
---> 21 gain=3.136*(np.tan(alpha)*np.sin(np.pi/2*np.cos(alpha)*np.sin(phi)))**2
22 print(gain)
23 y=10*np.log10( 1000*(power*gain*1.622*((lmb)**2) *0.5*1) / (((4*np.pi*r)**2) *1.2*1*F)*q_e*rnd )
ValueError: operands could not be broadcast together with shapes (115,) (32,)
可能是因为它抱怨 gain
行中的操作具有不同维度的矩阵 alpha
和 phi
。
问题是我想使用不同的矩阵维度,并保存一个 txt
文件,其中第一列是 r
,第二列是 phi
,第三列是 y
的值].
您需要向其中一个数组添加维度才能启用 broadcasting
>>> alpha = np.ones(115, dtype=np.uint8)
>>> phi = np.ones(32, dtype=np.uint8)
>>> c = alpha[:,None] * phi
>>> alpha.shape, alpha[:,None].shape, phi.shape, c.shape
((115,), (115, 1), (32,), (115, 32))
假设 alpha.shape
是 (115,)
:
`
>>> gain=3.136*(np.tan(alpha[:,None])*np.sin(np.pi/2*np.cos(alpha[:,None])*np.sin(phi)))**2
>>> gain.shape
(115, 32)