Subject: [Ufl] UFL and Python built-in functions

From: "Garth N. Wells" <[email-masked]>
Date: Sat, 23 Jan 2010 10:35:03 +0000

I've run into an issue when using 'exp' on a Coefficient/Function. In a
.ufl file, I can use

  M = exp(T)

where T is a Coefficient without problems. Using PyDOLFIN, I get the error:

  AttributeError: exp

Could this be that the Python version of exp is being called, rather
than the UFL version? How can I check in Python which 'exp' function is
being called?

Garth

---

From: Anders Logg <[email-masked]>
Date: Sat, 23 Jan 2010 17:17:13 +0100

> than the UFL version? How can I check in Python which 'exp' function is

print should work. With UFL, I expect you would get something like

# print exp
<function exp at 0x2673aa0>

Otherwise something like

# print exp
<ufunc 'exp'>

---

From: "Garth N. Wells" <[email-masked]>
Date: Sat, 23 Jan 2010 16:36:34 +0000

> <function exp at 0x2673aa0>

This is what I get when running ffc on a .ufl file and it works.

> <ufunc 'exp'>

This is what I get from PyDOLFIN and it breaks. Is this likely to be
what causes my problem? Any ideas on how to fix it?

Garth

---

From: David Beacham <[email-masked]>
Date: Sat, 23 Jan 2010 19:30:26 +0000

For a quick fix could you not try:

from ufl import exp

or:

import ufl and use ufl.exp(T) instead of exp(T)?

Garth N. Wells wrote:
> _______________________________________________
> Mailing list: https://launchpad.net/~ufl
> Post to     : [email-masked]
> Unsubscribe : https://launchpad.net/~ufl
> More help   : https://help.launchpad.net/ListHelp
>

---

From: Johan Hake <[email-masked]>
Date: Sat, 23 Jan 2010 11:57:15 -0800

> what causes my problem? Any ideas on how to fix it?

You can remove the last line in dolfin/__init__.py:

  from numpy import sin, cos, exp

I suppose these are exposed to the dolfin interface so they can be used when 
sub classing Expression in Python. However with ufl and the mature compile 
function interface I think these functions should be removed. 

If they are used in any demo, one can import them from the correct module, 
which should be math instead of numpy (as the usage is not vectorized), either 
as:

  from math import exp as mexp
  ...
     def eval(self, values, x):
         values[0] = mexp(x[0])

or just:

  import math
  ...
     def eval(self, values, x):
         values[0] = math.exp(x[0])

Johan

---

From: "Garth N. Wells" <[email-masked]>
Date: Sun, 24 Jan 2010 08:04:26 +0000

>   from numpy import sin, cos, exp

I removed this line and now it works great. Thanks.

Garth