I would like to have a composition of two functions f(g(x)) in a form which can be achieved by a custom Expression in python. In order to speed up assembly, I tried to implement this in C++ and pass it to Expression. Unfortunately, this fails for reasons unclear to me. Even if I comment out all everything in (B), the code still throws an exception.
Another issue is that the example won't compile with (A) although the header for Array is included.
Thanks for advise, Martin
mesh = UnitSquareMesh(5,5)
V = FunctionSpace(mesh,'CG',1)
V2 = VectorFunctionSpace(mesh,'CG',1)
g = interpolate(Expression(('x[0]+x[1]','-x[0]')),V2)
f = interpolate(Expression('1.+x[0]*x[0]'),V)
code = '''
#include "dolfin/common/Array.h"
namespace dolfin{
class MyFunc : public Expression
{
public:
// (A)
// Array<double> val(2);
boost::shared_ptr<Function> f;
boost::shared_ptr<Function> g;
MyFunc() : Expression() { }
void eval(Array<double>& values, const Array<double>& x,
const ufc::cell& c) const
{
// (B)
static Array<double> val(2);
g->eval(val, x, c);
f->eval(values, val, c);
}
};}'''
fg = Expression(code)
fg.f = f
fg.g = g
fg(0.1,0.2)