This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Elementwise multiplication of functions cpp

0 votes

Hi,

I need to implement an elementwise multiplication of two functions on the same 3D mesh in C++. My first approach is to implement a class called CustomFunction based on dolfin::Function and implement the operator* as :

CustomFunction operator*(dolfin::Function func, dolfin::Mesh mesh){
        dolfin::Function f(this->function_space())

        for (dolfin::MeshEntityIterator e(mesh, 0); !e.end(); ++e)
        {
            auto tmp = func.vector();
            for (dolfin::MeshEntityIterator e(mesh, 1); !e.end(); ++e)
            {
                for (dolfin::MeshEntityIterator e(mesh, 2); !e.end(); ++e)
                {
                    f[e->index()] = (*this)[e->index()]*func[e->index()]
                }
            }
        }
    }

It is basically an iteration over each dimension and it does not work. Is there any better way to do this?

Thanks in advance,
Justus

asked May 30, 2017 by Pama328 FEniCS Novice (480 points)

Hi, the above is not to a way to go. Are the functions in the same function space?

Yes they are in the same (mixed-) functionspace

Consider using the following for inplace multiplication. To fill a new vector by a pointwise product of two other vectors you can use PETSc.

...