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

How can one get the value of a vector Constant?

+2 votes

For a scalar valued Constant object one can simply call float to get the value


a = Constant(1.0)
print float(a)

1.0

but for a vector the situation is a little more tricky


a = Constant([1.0,1.0])
print [float(item) for item in a]

TypeError: float returned non-float (type Indexed)

Does anyone have a way to get the values out of the vector Constant?

asked Jul 16, 2013 by Gabriel Balaban FEniCS User (1,210 points)

I was being driven mad looking for this exactly-- print float(a)

My question: If I hadnt found your post, how could I have realized what is the command to print the value of a Constant object ?
I could not figure this out from the documentation or by examining the output of dir(a)

1 Answer

+3 votes
 
Best answer
import numpy as np
vals = np.zeros(2)
a.eval(vals, np.zeros(3))
print vals

You also can request an ehancement of Constant interface.

answered Jul 16, 2013 by Jan Blechta FEniCS Expert (51,420 points)
selected Nov 3, 2015 by Marie E. Rognes

Thanks Jan,
Your answer works ;-)

...