← Back to team overview

dolfin team mailing list archive

Re: Constant in PyDOLFIN

 

On Saturday 23 January 2010 14:53:14 Garth N. Wells wrote:
> Johan Hake wrote:
> > On Saturday 23 January 2010 06:00:32 Garth N. Wells wrote:
> >> In C++, we can treat a Constant as a double, e.g.
> >>
> >>   Constant dt(0.1)
> >>   double t = 0.0l
> >>   t += dt
> >>
> >> but this doesn't work in Python. Is there an elegant was to do this? Or
> >> is it just a case of defining __add__, etc in constant.py?
> >
> > No, as you are not adding something to Constant ;)
> >
> > But we might want to rename the double() operator in cpp.Constant to
> > __float__(self) in the SWIG interface, so one can do the above.

It turned out that we already have this. However it does not kick in because 
the ufl.Coefficient implements __radd__, which get called and t becomes an 
ufl.Coefficient. Quite weird if you ask me. 

Not sure we want to remove the __radd__ method from ufl. 

A quick fix would be to:

  t += float(dt)

which invokes the wrapped double operator. Another example where C++ is 
simpler than Python.

Johan



References