首页 > 解决方案 > Numpy matmul over object 数据类型说明

问题描述

numpy.matmul当谈到over的操作时,我有点困惑dtype=object

有人可以解释以下是如何工作的吗?特别是在最后一次操作“总计”中获得的内容。我使用这种方法(可能是错误的)将矩阵乘法替换为循环迭代。

为了粗略地了解我在做什么,这种计算的结果将用于生成 2 个热图,垂直为“z”,水平为“g_in”,颜色条值每次对应于第一个/第二个“总计”值的元素。这是对同时具有可变距离“z”和输入光束角“g_in”的 ABCD 射线传播的虚拟计算。

编辑代码;

z = np.linspace(0, 10, 11) # distance
g_in = np.linspace(-5, 5, 11) #input angle
f_0, f_1 = 1, 1 #some constants

A = np.array([[1, z], [0, 1]], dtype = object)
B = np.array([[1, 0], [-1/(f_0), 1]], dtype = object)
C = np.array([[1, 2*f_0], [0, 1]], dtype = object)
D = np.array([[1, 0], [-1/(f_0), 1]], dtype = object)
E = np.array([[1, z], [0, 1]], dtype = object)
F = np.array([[1, 0], [-1/(f_1), 1]], dtype = object)
G = np.array([[1, f_1], [0, 1]], dtype = object)

H = np.matmul(G,F)
I = np.matmul(H,E)
J = np.matmul(I,D)
K = np.matmul(J,C)
L = np.matmul(K,B)

M = np.matmul(L, A)

print('Matrix M=',M)

col1 = np.empty((2, 1),dtype=object)
col1[:, 0] = [0, g_in]

print('Matrix col1[:,0]=',col1[:,0])

total = np.matmul(M, col1[:,0])

print('Matrix total=',total)

y_out = np.transpose(total[0].tolist())
g_out = np.transpose(total[1].tolist())

y_out_ = np.expand_dims(y_out, axis=0)
g_out_ = np.expand_dims(g_out, axis=0)

fig, ax1 = plt.subplots(nrows=1,
ncols=1,sharex=True,sharey=True, figsize=(8, 6))
f1=ax1.imshow(y_out_, extent=  
[theta_in.min(),theta_in.max(),z_f.min(),z_f.max()],    
vmin=y_out_.min(),vmax=y_out_.max(), aspect="auto",
cmap='YlGnBu')
cb1=fig.colorbar(f1,orientation='vertical')
cb1.set_label(r'$y_{out}$',size=15)
ax1.set_ylabel(r'z', fontsize=20)
ax1.tick_params(axis='both', which='major', labelsize=20)
ax1.tick_params(axis='both', which='minor', labelsize=20)
ax1.autoscale(tight=True)

fig, ax2 = plt.subplots(nrows=1, ncols=1,sharex=True,figsize
(8, 6))
f2=ax2.imshow(g_out_, extent= 

[theta_in.min(),theta_in.max(),z_f.min(),z_f.max()],   

vmin=g_out_.min(),vmax=g_out_.max(), aspect="auto",
cmap='YlGnBu')
cb2=fig.colorbar(f2,orientation='vertical')
cb2.set_label(r'$g_{out}$',size=15)
ax2.set_xlabel(r' Angle, $θ_{in}$', fontsize=20)
ax2.set_ylabel(r'z', fontsize=20)
ax2.tick_params(axis='both', which='major', labelsize=20)
ax2.tick_params(axis='both', which='minor', labelsize=20)
ax2.autoscale(tight=True)

以上代码输出;

在此处输入图像描述

非常感谢,

标签: pythonnumpyscientific-software

解决方案


减小尺寸z

In [154]: z = np.linspace(0, 10, 5) # distance
     ...: g_in = np.linspace(-5, 5, 5) #input angle
     ...: f_0, f_1 = 1, 1 #some constants
     ...: 
In [155]: z
Out[155]: array([ 0. ,  2.5,  5. ,  7.5, 10. ])

只有一对数组:

In [156]: A = np.array([[1, z], [0, 1]], dtype = object)
     ...: B = np.array([[1, 0], [-1/(f_0), 1]], dtype = object)
In [157]: A
Out[157]: 
array([[1, array([ 0. ,  2.5,  5. ,  7.5, 10. ])],
       [0, 1]], dtype=object)
In [158]: B
Out[158]: 
array([[1, 0],
       [-1.0, 1]], dtype=object)

他们的matmul:

In [159]: A@B
Out[159]: 
array([[array([ 1. , -1.5, -4. , -6.5, -9. ]),
        array([ 0. ,  2.5,  5. ,  7.5, 10. ])],
       [-1.0, 1]], dtype=object)
In [160]: _.shape
Out[160]: (2, 2)

这相当于:

In [162]: np.array([[1-1*z, 0+1*z],[0*1-1, 0+1]], object)
Out[162]: 
array([[array([ 1. , -1.5, -4. , -6.5, -9. ]),
        array([ 0. ,  2.5,  5. ,  7.5, 10. ])],
       [-1, 1]], dtype=object)

所有其他产品都以相同的方式生产。

col1是具有数组元素的 (2,1)。 E与 相同A。其余的有数字元素,不需要是对象 dtype。

在 [166]: np.array([[0],[g_in]], object) Out[166]: array([[0], [array([-5. , -2.5, 0. , 2.5, 5 . ])]], dtype=object)

total结构上像

In [167]: B@A@col1[:,0]
Out[167]: 
array([array([ 0.  , -6.25,  0.  , 18.75, 50.  ]),
       array([ -5.  ,   3.75,  -0.  , -16.25, -45.  ])], dtype=object)

A (2,2)@(2,2)@(2,) => (2,),但带有数组元素。由于数组具有相同的形状,因此可以将其简化为数字 dtype:

In [171]: np.stack(Out[167])
Out[171]: 
array([[  0.  ,  -6.25,   0.  ,  18.75,  50.  ],
       [ -5.  ,   3.75,  -0.  , -16.25, -45.  ]])

推荐阅读