Code Review Asked by GalacticPonderer on October 27, 2021
I have written a function to multiply two numpy arrays.
def ra(self):
"""Multiply Rotation with initial Values"""
rva = self.r_array() * self.va_array()
rva = np.sum(rva, axis=1) # Sum rows of Matrix
rva = np.array([[rva[0]], # Transpose Matrix
[rva[1]],
[rva[2]]])
where:
I feel like this should be able to be written in one line. However, self.r_array() * self.va_array()
always returns a 3 x 3 array.
Any suggestions would be greatly appreciated.
Cheers
A one liner:
np.sum(r_array*va_array, axis=1, keepdims=True)
To match r_array@va_array
, use va_array.T
in the 1liner.
Answered by hpaulj on October 27, 2021
Actually the *
operator does element-wise multiplication. So you need to use .dot() function to get the desired result.
Example :
import numpy as np
a = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
b = np.array([[1]
,[2],
[3]])
print(a * b)
print(a.dot(b))
output :
[[ 1 2 3]
[ 8 10 12]
[21 24 27]]
[[14]
[32]
[50]]
Observe that when I have used *
operator, every column in a is multiplied with b element-wise
Answered by Sai Sreenivas on October 27, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP