UFL

sum([list-of-forms]) doesn't work

Bug #1060380 reported by Patrick Farrell
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
UFL
Fix Released
Low
Martin Sandve Alnæs

Bug Description

==================================================
from dolfin import *

mesh = UnitInterval(10)
V = FunctionSpace(mesh, "CG", 1)
M = MixedFunctionSpace([V, V, V])

tests = TestFunctions(M)
trials = TrialFunctions(M)
a = sum([inner(test, trial)*dx for (test, trial) in zip(tests, trials)])
==================================================

fails with:

Traceback (most recent call last):
  File "sum-of-forms.py", line 9, in <module>
    a = sum([inner(test, trial)*dx for (test, trial) in zip(tests, trials)])
TypeError: unsupported operand type(s) for +: 'int' and 'Form'

This is because the Python sum() command starts with the integer 0 by default, and 0 + Form is not defined. It does take an optional start argument, but there's no "empty Form" that would make the command work.

Could some SWIG magician please make the __add__ of Forms allow for the addition of 0 onto a Form?

Revision history for this message
Mikael Mortensen (mikael-mortensen) wrote : Re: [Bug 1060380] [NEW] sum([list-of-forms]) doesn't work

I would like to see that as well.

If you're in need of a fix I currently do:

==============================
from dolfin import *
import operator

mesh = UnitInterval(10)
V = FunctionSpace(mesh, "CG", 1)
M = MixedFunctionSpace([V, V, V])

tests = TestFunctions(M)
trials = TrialFunctions(M)
a = reduce(operator.add, [inner(test, trial)*dx for (test, trial) in zip(tests, trials)])
===============================

which works, but is not as pretty as sum.

Best regards

Mikael

Den Oct 2, 2012 kl. 8:18 PM skrev Patrick Farrell:

> Public bug reported:
>
> ==================================================
> from dolfin import *
>
> mesh = UnitInterval(10)
> V = FunctionSpace(mesh, "CG", 1)
> M = MixedFunctionSpace([V, V, V])
>
> tests = TestFunctions(M)
> trials = TrialFunctions(M)
> a = sum([inner(test, trial)*dx for (test, trial) in zip(tests, trials)])
> ==================================================
>
> fails with:
>
> Traceback (most recent call last):
> File "sum-of-forms.py", line 9, in <module>
> a = sum([inner(test, trial)*dx for (test, trial) in zip(tests, trials)])
> TypeError: unsupported operand type(s) for +: 'int' and 'Form'
>
> This is because the Python sum() command starts with the integer 0 by
> default, and 0 + Form is not defined. It does take an optional start
> argument, but there's no "empty Form" that would make the command work.
>
> Could some SWIG magician please make the __add__ of Forms allow for the
> addition of 0 onto a Form?
>
> ** Affects: dolfin
> Importance: Undecided
> Status: New
>
> --
> You received this bug notification because you are a member of DOLFIN
> Team, which is subscribed to DOLFIN.
> https://bugs.launchpad.net/bugs/1060380
>
> Title:
> sum([list-of-forms]) doesn't work
>
> Status in DOLFIN:
> New
>
> Bug description:
> ==================================================
> from dolfin import *
>
> mesh = UnitInterval(10)
> V = FunctionSpace(mesh, "CG", 1)
> M = MixedFunctionSpace([V, V, V])
>
> tests = TestFunctions(M)
> trials = TrialFunctions(M)
> a = sum([inner(test, trial)*dx for (test, trial) in zip(tests, trials)])
> ==================================================
>
> fails with:
>
> Traceback (most recent call last):
> File "sum-of-forms.py", line 9, in <module>
> a = sum([inner(test, trial)*dx for (test, trial) in zip(tests, trials)])
> TypeError: unsupported operand type(s) for +: 'int' and 'Form'
>
> This is because the Python sum() command starts with the integer 0 by
> default, and 0 + Form is not defined. It does take an optional start
> argument, but there's no "empty Form" that would make the command
> work.
>
> Could some SWIG magician please make the __add__ of Forms allow for
> the addition of 0 onto a Form?
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/dolfin/+bug/1060380/+subscriptions

Revision history for this message
Patrick Farrell (pefarrell) wrote :

Yes, another workaround (which is what I ended up doing) is

sum(forms[1:], form[0])

but this is also ugly!

Revision history for this message
Johan Hake (johan-hake) wrote :

  a = sum([inner(test, trial)*dx for (test, trial) in \
           zip(tests, trials)], zero()*dx)

works and is pretty concise.

On 10/02/2012 08:18 PM, Patrick Farrell wrote:
> Public bug reported:
>
> ==================================================
> from dolfin import *
>
> mesh = UnitInterval(10)
> V = FunctionSpace(mesh, "CG", 1)
> M = MixedFunctionSpace([V, V, V])
>
> tests = TestFunctions(M)
> trials = TrialFunctions(M)
> a = sum([inner(test, trial)*dx for (test, trial) in zip(tests, trials)])
> ==================================================
>
> fails with:
>
> Traceback (most recent call last):
> File "sum-of-forms.py", line 9, in <module>
> a = sum([inner(test, trial)*dx for (test, trial) in zip(tests, trials)])
> TypeError: unsupported operand type(s) for +: 'int' and 'Form'
>
> This is because the Python sum() command starts with the integer 0 by
> default, and 0 + Form is not defined. It does take an optional start
> argument, but there's no "empty Form" that would make the command work.
>
> Could some SWIG magician please make the __add__ of Forms allow for the
> addition of 0 onto a Form?

No SWIG magic is required here as it is pure UFL logics. I guess one
could argue that:

  a + 0

should be valid syntax.

Johan

>
> ** Affects: dolfin
> Importance: Undecided
> Status: New
>

Changed in dolfin:
assignee: nobody → Martin Sandve Alnæs (martinal)
status: New → Fix Committed
affects: dolfin → ufl
Changed in ufl:
importance: Undecided → Low
milestone: none → 1.1.0-alpha
Changed in ufl:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.