2017-05-10 108 views
-1

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

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

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和我得到的SAM e错误。

+1

'n'似乎是一个标量而不是矩阵。是什么让你认为它是一个矩阵? – Divakar

+0

@Divakar因为我把它们相乘,它给了我这个 – Katey

+0

@Divakar(见上) – Katey

回答

2

您可以使用numpy.multiply函数。给定一个1x1数组和一个8x8数组,该函数将通过1x1数组中的元素对8x8数组中的每个元素进行多重处理。如果我正确理解你的问题,这就是你要找的。以下是文档中的使用示例

>>> 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

+0

是的,谢谢你,这就是我正在寻找! – Katey