Positive part of an array, maximum of two arrays

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

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

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.

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

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

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,

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.

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.

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);

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.

1 Like