# How does absolute function work in UFL?

**URL:** <https://fenicsproject.discourse.group/t/how-does-absolute-function-work-in-ufl/17435>\
**Category:** General\
**Created:** [April 11, 2025, 12:02pm UTC](https://fenicsproject.discourse.group/t/how-does-absolute-function-work-in-ufl/17435 "2025-04-11T12:02:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![WjjSteve](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/wjjsteve/32/4389_2.png) [@WjjSteve](https://fenicsproject.discourse.group/u/WjjSteve)\
**Post date:** [April 11, 2025, 12:02pm UTC](https://fenicsproject.discourse.group/t/how-does-absolute-function-work-in-ufl/17435/1 "2025-04-11T12:02:54Z")

</div>

Hi I want to ask a question about the calculation of this [absolute function](https://fenics.readthedocs.io/projects/ufl/en/latest/manual/form_language.html#:~:text=abs(f)%3A%20the%20absolute%20value%20of%20f.)

If I define the following:

```auto
f = Coefficient(V)
a = abs(f) * dx

```

then what is actually happening?

In FEM, any function defined in the space V can be expressed using the basis functions:  
f\_h = \sum\_{j=1}^N\psi\_jf\_j. My concerning is that when I use high-order polynomial as basis functions, the basis function can also be negative somewhere so I wonder does it do  
\int \vert\sum\_{j=1}^N\psi\_jf\_j\vert {\rm d}x or \int \sum\_{j=1}^N\psi\_j\vert f\_j\vert {\rm d}x?

Also if do something more complicated like

```auto
f = Coefficient(V)
g = Coefficient(V)
v = TestFunction(V)
a = abs(f+g.dx(0))* v * dx

```

how does it work?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 11, 2025, 12:06pm UTC](https://fenicsproject.discourse.group/t/how-does-absolute-function-work-in-ufl/17435/2 "2025-04-11T12:06:23Z")

</div>

`abs` works on the full sum, not on the single basis function, i.e.

> [@WjjSteve](#):
>
> it do  
> \int \vert\sum\_{j=1}^N\psi\_jf\_j\vert {\rm d}x

Similarly, it will be the absolute value of \vert\sum\_{j=1}^N\psi\_jf\_j +\sum\_{j=1}^N\frac{\partial \psi\_j}{\partial x\_0} g\_j \vert

---

<div class="post-metadata">

**Author:** ![WjjSteve](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/wjjsteve/32/4389_2.png) [@WjjSteve](https://fenicsproject.discourse.group/u/WjjSteve)\
**Post date:** [April 11, 2025, 12:16pm UTC](https://fenicsproject.discourse.group/t/how-does-absolute-function-work-in-ufl/17435/3 "2025-04-11T12:16:37Z")

</div>

Thanks for quick response!

But how does it calculate the integral exactly? Using Trapazoidal rule of something like that? You know when you have some high-order basis function, then in some part the basis function is negative itself, also in the case you just showed, the function inside the absolute sign might be negative somewhere, so how is the integral computed?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 11, 2025, 12:51pm UTC](https://fenicsproject.discourse.group/t/how-does-absolute-function-work-in-ufl/17435/4 "2025-04-11T12:51:44Z")

</div>

You can either:

1. Choose your own quadrature rule
2. Let DOLFINx decide what quadrature rule to use (based on analysis of the form)

The absolute value of a coefficient at a point in the domain might have negative values in some of the basis functions, yes. However, one should sum all of them (multiplied by the coefficient) prior to calling the abs function.

I’ve added some notes on this at:

> **[The UFL forms — FEniCS Workshop](https://jsdokken.com/FEniCS-workshop/src/unified_form_language/ufl_forms.html#further-analysis-of-the-variational-form)**

---

<div class="post-metadata">

**Author:** ![WjjSteve](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/wjjsteve/32/4389_2.png) [@WjjSteve](https://fenicsproject.discourse.group/u/WjjSteve)\
**Post date:** [April 14, 2025, 7:23pm UTC](https://fenicsproject.discourse.group/t/how-does-absolute-function-work-in-ufl/17435/5 "2025-04-14T19:23:56Z")

</div>

Thanks for your reply!

I’m wondering if it’s possible to use the `abs` (absolute value) function _outside_ the UFL file?

I’m trying to implement some **high-dimensional** problems using **kronproduct**.  
For example, if I need the mass matrix \int\psi\_i\psi\_j\ {\rm d}\pmb{x} in 4D, I can define the bilinear form

```auto
a = u*v*dx

```

in 2D and assemble 2 mass matrices in 2D, then compute the kronproduct of them, which give me the mass matrix in 4D

But somehow I need to project the term that involve the absolute values, for example: |f+g|, then I usually write my UFL(in dolfinx it is just python file) file like this

```auto
a = abs(f+g) * v * dx

```

In the high-dimensional case, such forms aren’t explicitly defined, so I might need to construct them manually. Given this context, do you have any suggestions on how to handle the absolute value in this setting? I’d really appreciate any guidance—thanks so much!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 14, 2025, 7:34pm UTC](https://fenicsproject.discourse.group/t/how-does-absolute-function-work-in-ufl/17435/6 "2025-04-14T19:34:39Z")

</div>

so you want to take the absolute value in the resulting matrix for all entries?

Couldn’t you the just change the underlying data with an abs scaling post assembly?

---

<div class="post-metadata">

**Author:** ![WjjSteve](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/wjjsteve/32/4389_2.png) [@WjjSteve](https://fenicsproject.discourse.group/u/WjjSteve)\
**Post date:** [April 14, 2025, 7:45pm UTC](https://fenicsproject.discourse.group/t/how-does-absolute-function-work-in-ufl/17435/7 "2025-04-14T19:45:57Z")

</div>

No I don’t think it is possible in my case.  
The main reason is that my underlying function is very complicated, it is something like this:

|g+a\_1\partial\_{x\_1}f+a\_2\partial\_{x\_2}f+a\_3\partial\_{x\_3}f+a\_4\partial\_{x\_4}f|  
where both f and g are 4D functions, if I want to construct the form without absolute sign, I can construct the terms one by one using kronproduct, and then add them up, but there is absolute sign, I can not do that anymore since the absolute values need to be taken inside the integral.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 14, 2025, 7:49pm UTC](https://fenicsproject.discourse.group/t/how-does-absolute-function-work-in-ufl/17435/8 "2025-04-14T19:49:15Z")

</div>

Without an actual example it is quite hard for me to give any further guidance.

---

<div class="post-metadata">

**Author:** ![WjjSteve](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/wjjsteve/32/4389_2.png) [@WjjSteve](https://fenicsproject.discourse.group/u/WjjSteve)\
**Post date:** [April 22, 2025, 9:39am UTC](https://fenicsproject.discourse.group/t/how-does-absolute-function-work-in-ufl/17435/9 "2025-04-22T09:39:03Z")

</div>

It’s OK, I just found an alternative scheme, so I don’t need the absolute sign for high-dimensional problems. Thanks!
