# Positive part of an array, maximum of two arrays

**URL:** https://community.freefem.org/t/positive-part-of-an-array-maximum-of-two-arrays/3954
**Category:** Feature Request
**Created:** [June 1, 2025, 1:50pm UTC](https://community.freefem.org/t/positive-part-of-an-array-maximum-of-two-arrays/3954 "2025-06-01T13:50:27Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [June 1, 2025, 1:50pm UTC](https://community.freefem.org/t/positive-part-of-an-array-maximum-of-two-arrays/3954/1 "2025-06-01T13:50:27Z")

</div>

Hi to the developers,  
It would be convenient to have built-in the max or min of an array and a real number, which means taking the max or min in each entry

```auto
real[int] aa(nn),res(nn);
real rr;
res=max(aa,rr);

```

would give res(i)=max(aa(i),rr)

This would simplify and fasten computations, avoiding to make explicit loops.

Then also useful would be the max (resp min) of two arrays of same length

```auto
real[int] aa(nn),bb(nn),res(nn);
res=max(aa,bb);

```

would do res(i)=max(aa(i),bb(i))  
Thanks for your consideration.

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [June 10, 2025, 12:19pm UTC](https://community.freefem.org/t/positive-part-of-an-array-maximum-of-two-arrays/3954/2 "2025-06-10T12:19:03Z")

</div>

Hi there. In case it is helpful, you can handle these more compactly with implicit loops:

```auto
for [i, ai : aa] res(i) = max(ai, rr);
for [i, ai : aa] res(i) = max(ai, bb(i));

```

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [June 10, 2025, 12:49pm UTC](https://community.freefem.org/t/positive-part-of-an-array-maximum-of-two-arrays/3954/3 "2025-06-10T12:49:32Z")

</div>

Dear Chris,  
Thanks for your suggestion. My idea was to have a “fast” operator encoded in C++,  
useful when it is called many times. Indeed the “max” and “min” are the elementary bricks to build nonlinearities. But maybe it is not possible because not available as C++ built-in commands.  
To compare with, the abs() operator is defined for an array probably because it is already defined in C++.  
It follows that we can code for example for the max of two arrays aa,bb, writing \max(a,b)=a+\max(0,b-a), and \max(0,x)=(x+|x|)/2,

```auto
real[int] aa(nn),bb(nn),res(nn),diff(nn),aux(nn),pos(nn);
diff=bb-aa;
aux=abs(diff);
pos=diff+aux;
pos*=0.5;
res=aa+pos;

```

Then it runs faster than doing a FF++ explicit or implicit loop. But it is quite heavy to write this, and it is a source of errors.

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [June 10, 2025, 8:42pm UTC](https://community.freefem.org/t/positive-part-of-an-array-maximum-of-two-arrays/3954/4 "2025-06-10T20:42:54Z")

</div>

Why not use a `func` or a `macro` to get this functionality immediately and make your code more readable? That should be much faster than an implicit/explicit loop even if its not totally optimal. FWIW, the FF developers seem friendly to pull requests if you’d like to contribute such functions. Looks like it could be added here: [Afunction.cpp](https://github.com/FreeFem/FreeFem-sources/blob/develop/src/fflib/AFunction.cpp).

```auto
func real[int] custommax(real[int] aa, real[int] bb){
  bb -= aa;
  bb += abs(bb);
  bb *= 0.5;
  return bb += aa;
}
res = custommax(aa, bb);

```

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [June 11, 2025, 9:59am UTC](https://community.freefem.org/t/positive-part-of-an-array-maximum-of-two-arrays/3954/5 "2025-06-11T09:59:10Z")

</div>

Thanks for your suggestions again. I am going to use a `func`, I did not think of that possibility. About pull requests I don’t know how to do that, and I am not able to write a C++ piece of code.
