Element-wise vector multiplication


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?


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)