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

How to sum functions?

+3 votes

Hello all.
If i have two functions in FEniCS: f1 and f2, that defined on the same mesh how can i apply mathematical operators via C++ interface to them? I want to sum or divide them. (f3= f1+f2; f4=f1/f2, f2!=0).

Mesh mesh("./mesh.xml");
Function f1 (mesh,"./f1.xml");
Function f2 (mesh,"./f2.xml");
Function f3 (mesh);
Function f4 (mesh);
//f3=f1+f2;
//f4=f1/f2;
asked Jul 8, 2013 by mgrviper FEniCS Novice (370 points)

2 Answers

+4 votes
 
Best answer

With the present development version of DOLFIN you should be able to do simple linear combinations, like your first example:

f3=a*f1+f2/b;

where a and b are scalars. If that does not work you should report an issue at bitbucket.

There are no support for Function division. Then you need to declare a projection form in UFL and solve a linear system.

answered Jul 8, 2013 by johanhake FEniCS Expert (22,480 points)
selected Jul 18, 2013 by mgrviper

Thanks for answer!
Is there any way to do it with current stable version (1.2.0)?

[dolfin 1.2.0] How to sum functions and multiply function by scalar?
+2 votes

It is not quite clear what do you mean by division of two finite element functions. But if you think of DOF-wise division this could be done using numpy interface (as Vector interface supports only division by scalar). This should then done the job

f4.vector()[:] = f1.vector().array()/f2.vector().array()

assuming all the DOFs in f2.vector() are non-zero. Note that this might be quite slow for large vectors.

answered Jul 9, 2013 by Jan Blechta FEniCS Expert (51,420 points)
edited Jul 9, 2013 by Jan Blechta
So, what would be the C++ equivalent of f1.vector().array()?
...