# Expressing the maximum of several variables using elementary functions [duplicate]

It's well-known that $$\max(a,b)=\frac{a+b+|a-b|}{2}.$$

Is there a (good) generalization to several variables? Of course $\max(a,b,c)=\max(a,\max(b,c))$ and so $$\max(a,b,c)=\frac{a+\frac{b+c+|b-c|}{2}+|a-\frac{b+c+|b-c|}{2}|}{2}$$ $$=\frac{a+0.5b+0.5c+0.5\left|b-c|+|a-0.5b-0.5c-0.5|b-c|\right|}{2}$$ but I'd like a form that shows the natural symmetry better and which doesn't have so many operations.

This is a practical problem working on a system which has an absolute value operator but no maximum and not much ability to execute conditional statements, but to be honest the real reason I'm interedted is an attempt to beautify something that is seemingly ugly.

For the practical side I need 5-10 arguments and it's acceptable to assume that all arguments are at least 0, though of course it would be much more satisfying if this latter assumption was not needed.

-

## marked as duplicate by TZakrevskiy, Macavity, Daniel Rust, Davide Giraudo, M TurgeonJan 29 '14 at 21:48

I don't think it is a dupe. There is an explicit constraint of using absolute value here and the number of variables is not restricted to 3. –  Aryabhata Apr 4 '11 at 20:06
Finding a form which has the "natural" symmetry is not difficult $\frac{1}{3}[\max(a,\max(b,c))+ \max(b,\max(c,a))+ \max(c,\max(a,b))]$ does the job. However, it involves more operations... –  Fabian Apr 4 '11 at 20:23
@Fabian: And that's the expression given by Hans Lundmark in the problem linked as a possible duplicate... –  Arturo Magidin Apr 4 '11 at 20:49
If your system can store intermediate results, then the ugliness of these formulas is misleading: the formula size is growing exponentially, but the algorithm is very simple and is just growing linearly, which is the best you can hope for anyway. –  Matt Apr 18 '11 at 16:41
@Matt: Actually, oddly enough, it cannot. Thus my interest in formula length! –  Charles Apr 18 '11 at 19:32

I think that integer division may provide a path to a different answer, depending on exactly how it works. I 'll do the formatting programming-style, not math-style since that is what I'm use to.

Suppose you have n arguments, A[1], A[2], A[3]..., A[n].

Define multiplier coefficient C[i][j] such that C[i][j] = 1 for A[i]>A[j], zero otherwise. This can be done using the absolute value trick as in the above formula.

Define a coefficient T[i] = sum (C[i][j]) / (n-1) using integer division. For subscript i belonging to the max value, the sum will be n-1, so T[i] will be 1. For other subscripts, the sum will be less than n-1, so T[i] will be zero.

Max = sum (T[i] A[i])

Probably I have something backwards, but I think the approach is workable, though maybe not better than original suggestion.

-
+1, thanks for the idea. I can't store intermediate results, though. –  Charles Apr 28 '11 at 13:57
No intermediate results necessary; it's a double summation and each coefficient only needs to be calculated once. There are a couple errors in my post which are easily fixed, however, there is a problem if the max value occurs more than once in the list and that seems more difficult. –  phv3773 May 2 '11 at 16:45
I'm in a Turing-incomplete language that doesn't have loops or variables. This formula is very large to write in that format! It would take a whole page to express the five-variable version. (Yes, the language is awful. No, I can't change.) –  Charles May 2 '11 at 17:24