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

Function Vector Syntax

0 votes

I am not quite sure what is going wrong here..

when I run the following code

phi = interpolate(Constant(0) ,V)

a_phi = Function(V)

e_n = Function(V)
e_n.vector()[:] = 1
a_phi.vector()[:] = (1 - phi.vector()[:])
a_phi_2 = Constant(1) - phi
a_phi_3 = (e_n.vector()[:] - phi.vector()[:])

plot(phi, interactive = True)

plot(e_n, interactive = True)

plot(a_phi, interactive = True)

plot(a_phi_2, interactive = True)

I get that
phi = 0
e_n = 1
a_phi = -1 (why? I dont get this - shouldnt it be 1?)
a_phi_2 = 1
a_phi_3 = 1

is this a bug?
Or didnt I get the syntax right?

asked Jul 9, 2016 by Whistler FEniCS Novice (460 points)

1 Answer

0 votes

Hi, the following illustrates why a_phi takes the value -1.

from dolfin import *

mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, 'CG', 1)
f = interpolate(Constant(0), V)

print '1+f', (1+f.vector()[:]).array()
print '1-f', (1-f.vector()[:]).array()
print 'f-1', (f.vector()[:]-1).array()

I suspect the reason for this behavior is that __sub__(int, Vector) delegates to __sub__(Vector, int).

answered Jul 9, 2016 by OxbowQuest FEniCS User (1,360 points)
...