Element-wise vector multiplication
- mptino
- Topic Author
- New Member
Less
More
2 years 3 months ago #4486
by mptino
Element-wise vector multiplication was created by mptino
Hello,
I have been trying for a while now to multiply two basevector objects element-wise, and cannot figure it out.
I essentially have a vector d of type basevector. I want to multiply this vector by another basevector (or even regular vectorD) element wise. I don't see a transpose for vector objects, so I can't do d = d * d (this is invalid).
Also, any idea of how to convert a vectorD object to a basevector? I know if we have, say, d as type basevector. I can do d.FV() to get a flatvector (or vectorD) object. But how to go from flatVector to basevector is unknown...
Any suggestions?
Thanks,
Matthew
I have been trying for a while now to multiply two basevector objects element-wise, and cannot figure it out.
I essentially have a vector d of type basevector. I want to multiply this vector by another basevector (or even regular vectorD) element wise. I don't see a transpose for vector objects, so I can't do d = d * d (this is invalid).
Also, any idea of how to convert a vectorD object to a basevector? I know if we have, say, d as type basevector. I can do d.FV() to get a flatvector (or vectorD) object. But how to go from flatVector to basevector is unknown...
Any suggestions?
Thanks,
Matthew
2 years 3 months ago #4487
by joachim
Replied by joachim on topic Element-wise vector multiplication
You can wrap a BaseVector into a diagonal matrix (derived from BaseMatrix), and do a matrix-vector multiplication:
from ngsolve import *
x = BaseVector(5)
y = BaseVector(5)
z = BaseVector(5)
x[:] = 2
y[:] = 5
mx = DiagonalMatrix(x)
z[:] = mx * y
print (z)
Joachim
from ngsolve import *
x = BaseVector(5)
y = BaseVector(5)
z = BaseVector(5)
x[:] = 2
y[:] = 5
mx = DiagonalMatrix(x)
z[:] = mx * y
print (z)
Joachim
Time to create page: 0.104 seconds