Skip to content

Add arity property to form#470

Open
schnellerhase wants to merge 11 commits into
mainfrom
schnellerhase/form-arity
Open

Add arity property to form#470
schnellerhase wants to merge 11 commits into
mainfrom
schnellerhase/form-arity

Conversation

@schnellerhase

@schnellerhase schnellerhase commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

There is currently no better way than len(form.arguments()) to check for the arity/rank of a form. Introduces property for simpler access. Since rank is already associated with tensor ranks in ufl, use the name arity.

If a form contains an ambiguous arity, i.e. terms with different argument counts None is returned.

Ref FEniCS/dolfinx#4118.

@schnellerhase schnellerhase self-assigned this Mar 19, 2026
@schnellerhase schnellerhase requested a review from jorgensd March 19, 2026 11:06
@jorgensd

Copy link
Copy Markdown
Member

A form doesn’t necessarily have a slingle arity, as each individual integrand can have different arities. This is for instance why things such as ‘ufl.system’ exists, as it would split the form by rank 2 and rank 1 systems.

if we want to have arity as a property, it should probably be max arity.

@schnellerhase

Copy link
Copy Markdown
Contributor Author

I changed over to return int | None. If the arity is the same for all integrals it is returned, if we have different arities it returns None. This allows to differentiate u * v * dx and u * v * dx + v * dx- which would with the max have both returned 2.

@schnellerhase schnellerhase changed the title Add Form arity property Add arity property to form May 15, 2026
Comment thread ufl/form.py Outdated
if not self._integrals:
return 0

from ufl.algorithms.analysis import extract_terminals_with_domain

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this here to avoid circular imports?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly. Also used in other parts of form.py in the same manner.

Comment thread ufl/form.py Outdated
Comment thread ufl/form.py
Comment on lines +362 to +363
if len(set(arg.part() for arg in args)) > 1:
raise RuntimeError("Arity does not support mixed arguments in an integral.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What case does this cover?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works around the problems arising from having mixed arguments present in the form https://github.com/FEniCS/ufl/pull/470/changes#diff-40318d7ac6b526c958053e25f90768d68e58d7c26e6479fe580f7420f53de86fR146-R159. Probably this is too restrictive. But I am not quite sure what the right checks should be here instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants