python - 乘以一元素矩阵

标签 python python-3.x numpy matrix scalar

我想将 1x1 矩阵乘以 8x8 矩阵。显然我知道你不能将这两种形状的矩阵相乘。我的问题是如何从 1x1 矩阵中“提取”该值,以便它相当于将 8x8 矩阵乘以标量。换句话说,有没有办法将 1x1 矩阵变成标量?

这是到目前为止我的代码,其中 n 是我的 1x1 矩阵,flux 是我的 8x8 矩阵:

n=0
for i in range(delta_E.shape[0]):
    n+= 100/(210*(Sig_f_cell[i])*flux[i]*delta_E[i]*(1.6022e-13)*V_core)

flux = (np.linalg.inv(L))*G

目标:将通量乘以 n 的值

看起来 n 是一个标量,但是当我将它们相乘时,出现此错误:

ValueError                                Traceback (most recent call last)
<ipython-input-26-0df98fb5a138> in <module>()
----> 1 Design_Data (1.34,.037,90)

<ipython-input-25-5ef77d3433bc> in Design_Data(pitch, Pu_fraction,     FE_length)
201     print('Number of fuel elements : ',N_FE)
202 
--> 203     return  n*flux
204 

C:\Users\Katey\Anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py  in __mul__(self, other)
341         if isinstance(other, (N.ndarray, list, tuple)) :
342             # This promotes 1-D vectors to row vectors
--> 343             return N.dot(self, asmatrix(other))
344         if isscalar(other) or not hasattr(other, '__rmul__') :
345             return N.dot(self, other)

ValueError: shapes (1,1) and (8,1) not aligned: 1 (dim 1) != 8 (dim 0)

我也尝试过仅乘以n[0]*flux,但我得到了相同的错误。

最佳答案

您可以使用numpy.multiply函数。给定一个 1x1 数组和一个 8x8 数组,此函数会将 8x8 数组中的每个元素乘以 1x1 数组中的元素。如果我正确理解你的问题,这就是你要找的。这是文档中的使用示例

 >>> x1 = np.arange(9.0).reshape((3, 3))
 >>> x2 = np.arange(3.0)
 >>> np.multiply(x1, x2)
     array([[  0.,   1.,   4.],
            [  0.,   4.,  10.],
            [  0.,   7.,  16.]])

您可以在此处找到此功能的文档 https://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html .

关于python - 乘以一元素矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43900682/

相关文章:

python - 使用 grappler 优化并(重新)保存保存的模型

python - 从间隔列表中确定值的位置(综合解决方案)

python - 向量化逻辑跨越行的 for 循环

python - 我应该如何以及在哪里创建此事务方法?

python - 递归检测数字是否为阶乘

python - 在 Windows 中使用批处理文件静默安装 numpy.exe

python - numpy 3d 数组 -- 展平 --> 1d 数组 --> 选择 1d 中的一个元素 --> 如何知道该元素在 3d 中的索引?

python - 理解train_test_split方法

python - 为什么这个 Python 生成器/协程会丢失一个值?

python - 如何从子文件夹访问路径级别以下的文件