# Function mean unexpected behaviour

**URL:** <https://community.freefem.org/t/function-mean-unexpected-behaviour/1296>\
**Category:** General Discussion\
**Created:** [November 4, 2021, 8:03am UTC](https://community.freefem.org/t/function-mean-unexpected-behaviour/1296 "2021-11-04T08:03:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![distractor](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@distractor](https://community.freefem.org/u/distractor)\
**Post date:** [November 4, 2021, 8:03am UTC](https://community.freefem.org/t/function-mean-unexpected-behaviour/1296/1 "2021-11-04T08:03:36Z")

</div>

Could somebody please explain the behaviour of `mean` function?

For example this case works:

```auto
real x0 = 0, x1 = 1.;
real y0 = 0, y1 = 0.5;
int n = 80, m = 40;
mesh Th = square(n, m, [x0 + (x1 - x0) * x, y0 + (y1 - y0) * y]);

fespace Vh(Th, P2);

Vh r = 0;

cout << mean(r) << endl;

```

but a change as simple as different mesh:

```auto
real radius1 = 1.0;
real radius2 = 1.3;
border left(t = 0, 1){x = 0; y = radius2 - (radius2 - radius1) * t; label=3;}
border inner(t = pi / 2., 0){x = radius1 * cos(t); y = radius1 * sin(t); label=2;}
border bottom(t = 0, 1){x = radius1 + (radius2 - radius1) * t; y = 0; label=4;}
border outter(t = 0, pi / 2.){x = radius2 * cos(t); y = radius2 * sin(t); label=1;}
mesh Th = buildmesh(left(10) + inner(35) + bottom(10) + outter(50));

fespace Vh(Th, P2);

Vh r = 0;

cout << mean(r) << endl;

```

results in **execution error**. Ehm… Why and how to compute the mean in the second case?

---

<div class="post-metadata">

**Author:** ![ivan](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/ivan/32/823_2.png) [@ivan](https://community.freefem.org/u/ivan)\
**Post date:** [November 4, 2021, 5:24pm UTC](https://community.freefem.org/t/function-mean-unexpected-behaviour/1296/2 "2021-11-04T17:24:59Z")

</div>

In the FreeFem++ documentation (Third edition, Version 3.48) the keyword index has a single entry for `mean` in the following sentence:

> The characteristics of `FreeFem++` are:
> 
> - `[..]`
> - Tools to define discontinuous Galerkin finite element formulations `P0`, `P1dc`, `P2dc` and keywords: `jump`, `mean`, `intalledges`.
> - `[...]`

The list of [built-in functions](https://doc.freefem.org/references/functions.html#) also doesn’t include mean. So it seems like `mean` has a different meaning than one would typically expect.

Perhaps @frederichecht can shed more light on this.

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [November 4, 2021, 5:39pm UTC](https://community.freefem.org/t/function-mean-unexpected-behaviour/1296/3 "2021-11-04T17:39:21Z")

</div>

You are meant to use `mean` inside a `varf`. This is not computing what you want. What mean do you want in your case? If it’s arithmetic, then you can do `cout << r[].sum/r[].n << endl;`

---

<div class="post-metadata">

**Author:** ![ivan](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/ivan/32/823_2.png) [@ivan](https://community.freefem.org/u/ivan)\
**Post date:** [November 4, 2021, 5:44pm UTC](https://community.freefem.org/t/function-mean-unexpected-behaviour/1296/4 "2021-11-04T17:44:08Z")

</div>

> [@prj](#):
>
> `r[].sum/r[].n`

This would give the mean of the array of DOF’s, right?

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [November 4, 2021, 5:50pm UTC](https://community.freefem.org/t/function-mean-unexpected-behaviour/1296/5 "2021-11-04T17:50:44Z")

</div>

Yes, is that what you want?
