2

I want to multiply a 1x1 matrix by an 8x8 matrix. Obviously I know that you cannot multiply matrices of these two shapes. My question is how to I "extract" the value from the 1x1 matrix so that it's equivalent to multiplying the 8x8 matrix by a scalar. In other words is there a way to turn the 1x1 matrix into a scalar?

Here's my code so far, where n is my 1x1 matrix and flux is my 8x8 matrix:

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

Goal: to multiply flux by the value of n

It appears that n is a scalar but when I multiply them I get this error:

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)

I have also tried just multiplying n[0]*flux and I get the same error .

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Katey
  • 85
  • 1
  • 2
  • 9

1 Answers1

3

You can use the numpy.multiply function. Given a 1x1 array and an 8x8 array this function will multiple each element in the 8x8 array by the element in the 1x1 array. If I understand your question correctly, this is what you're looking for. Here's a usage example from the documentation

 >>> 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.]])

You can find the documentation for this function here https://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html .

MCI
  • 888
  • 1
  • 7
  • 13