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

What is the correct (fastest) way to assign a Function to another Function in python?

+2 votes
f = Function(Q)
g = Function(Q)
f.vector()[:] = g.vector() #does this work in parallel?
f.vector()[:] = g.vector()[:]
f.vector().set_local(g.vector().array())
f.assign(g) #apparently this would break pointers to f.split()
asked Feb 6, 2014 by chaffra FEniCS User (1,830 points)
edited Feb 6, 2014 by chaffra

1 Answer

+4 votes

I have tested this quite thoroughly and in my experience the fastest approach is none of the above, rather

f.vector().zero()
f.vector().axpy(1.0, g.vector())

All of the methods should work in parallel afaik.

answered Feb 7, 2014 by mikael-mortensen FEniCS Expert (29,340 points)
...