How to set up an equation so that it equals a variable * value in and np.array

348 Views Asked by At

This is a situation where I am trying to multiply a 3x3 array by a 3x1 array of variables

Asuperworst Out[96]:  array([[7.84613528e+04, 4.07395485e+03,   4.56960842e-13],
   [4.07395485e+03, 1.43342856e+05, 7.48873168e-12],
   [4.56960842e-13, 7.48873168e-12, 1.03500000e+04]])

exmatrixmax=np.array([['exmax'],['eymax'],['exymax']])
NXMatrixMaxWorst=np.dot(Asuperworst,exmatrixmax)

NXMatrixMaxWorst=np.dot(Asuperworst,exmatrixmax)

TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe' I understand that the issue is that the float64 is incompatible with but I just need to be able to multiply these together to make something like

NXMatrixMaxWorst= np.array[[78450exmax + 4080eymax], [4.08exmax+143.3eymax],[10.35exymax]

This way I can set the NXMatrixMaxWorst equal to something such as [[NMX],[0],[0]] and run the calculation for the NMX variable and to calculate for exmax, eymax, and exymax for all values in the list

Also, for a similar situation I need to be able to determine the NXMatrixMaxWorst for a list of Asuperworst value such as

Afinal Out[100]:  [array([[69408.1197441 ,  1357.98495151,    0.],
     [ 1357.98495151,  4526.61650505,     0.        ],
     [    0.        ,     0.        ,  3450.        ]]),      
array([[0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
     [0.00000000e+00, 9.09494702e-13, 0.00000000e+00],
     [0.00000000e+00, 0.00000000e+00, 0.00000000e+00]])]

Any help that can be provided would be most appreciated. I'm new to this and it has been an uphill battle the whole way

1

There are 1 best solutions below

0
On

First problem to address here is the incompatible data types, for this you need to first check which array is causing the trouble, run this to check

Asuperworst.dtype
# and
exmatrixmax.dtype

Based on the TypeError you are seeing, one of these array is of type U32 (unsigned 32 bit). If all of your values in both the matrices remain positive always you can choose to convert the one which is not of U32 type to U32 like this

# if Asuperworst is not of U32 type
Asuperworst = Asuperworst.astype('U32')
# or if exmatrixmax is not of U32 type
exmatrixmax = exmatrixmax.astype('U32')

If positive values are not ensured, then to be on safe side you can cast the matrices to 'float64' (just replace 'U32' with 'float64'

Once the data types have been taken care of, we move on to finding NXMatrixMaxWorst as follows-

# result of a 3x3 and 3x1 matrix multiplication is a 3x1
# you can transpose this if you need a 1x3
NXMatrixMaxWorst = np.matmul(Asuperworst, exmatrixmax) #.T

Finally, If you want to calculate NXMatrixMaxWorst for multiple Asuperworst matrices follow this-

Asuperworst = [np.array([[69408.1197441 ,  1357.98495151,    0.],
              [ 1357.98495151,  4526.61650505,     0.        ],
              [    0.        ,     0.        ,  3450.        ]]),      
              np.array([[0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
                        [0.00000000e+00, 9.09494702e-13, 0.00000000e+00],
                        [0.00000000e+00, 0.00000000e+00, 0.00000000e+00]])]
# this converts a list of Asuperworst npumpy arrays into a single numpy array with an additional dimension. So the new Asuperworst would be of shape (2,3,3)
Asuperworst = np.stack(Asuperworst, axis=0)
# so now the matrix multiplication can be used to calculate two NXMatrixMaxWorst for two Asuperworst but one exmatrixmax
# the resulting dimension will be (2, 3, 1) 
NXMatrixMaxWorst = np.matmul(Asuperworst, exmatrixmax)