Elementwise multiplication in variational form / Hadamard product

Hi community

I want to perform the operation A .* v, for example

\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} .* \begin{bmatrix} 1 \\ 0 \\ 1 \end{bmatrix} = \begin{bmatrix} 1 & 0 & 3 \\ 4 & 0 & 6 \\ 7 & 0 & 9 \end{bmatrix}

The matrix A is a mass matrix form and b is a logical array of int type which results in a modified A, like

from dolfin import *

mesh = UnitSquareMesh(2,2)

W = FunctionSpace(W)
u  = TrialFunction(W)
w  = TestFunction(W)

vec = [1 0 1 0 0 0 0 0 0]
b = Function(W)
b.vector().set_local(vec)

# the following is incorrect and will not give me the result that I desire (that is element wise multiplication)
Mb = inner(u,v)*b*dx

Is there a way to do this operation and get the correct modified Mb in ufl form, because the ultimate aim is interact it with other forms.

Conversely,

from dolfin import *
import numpy as np

mesh = UnitSquareMesh(2,2)

W = FunctionSpace(W)
u  = TrialFunction(W)
w  = TestFunction(W)

b = np.array([1 0 1 0 0 0 0 0 0])
M = assemble(inner(u,v)*dx)
M_arr = M.array()

Mb = M_array * b[:, np.newaxis] # This is the correct Mb

Is there a way to convert Mb to ufl form or make it “interactable” with other forms in Legacy FEniCS?

Thanks!

To me this operation doesn’t seem like a “ufl-operation”, as you are trying to work on the DOF level of the matrix.

Could you add some more context as to why you want to “deactivate” certain columns of your mass matrix, and why this has to happen on the UFL level and not as a post-processing step with for instance PETSc’s zeroRows (after transposing the matrix)?