I am a complete beginner to SageMath. I was wondering how I could recreate the maximum-minimum identity using it. Any idea?
Asked
Active
Viewed 118 times
1 Answers
0
Let us work on the first maximum-minimum formula :
$${\displaystyle {\max\{x_{1},x_{2},\ldots ,x_{n}\}=\\ \sum _{i=1}^{n}x_{i}-\sum _{i<j}\min\{x_{i},x_{j}\}+\sum _{i<j<k}\min\{x_{i},x_{j},x_{k}\}-\cdots \\\qquad \cdots +\left(-1\right)^{n+1}\min\{x_{1},x_{2},\ldots ,x_{n}\}}}\tag{1}$$
Here is a SAGE implementation of the computation of the RHS of (1), taking advantage of the fact that "Combination(x)" outputs the list of subsets of $x$ by increasing sizes:
x=[5,2,8,7]
C=Combinations(x)
C= [C[i] for i in range(1,2^len(x))]; # removal of the (initial) void set
show(C)
sig=-1; # signum
su=0; # sum in the RHS
L=0; # length of the current subset
for comb in C:
if len(comb)>L:
sig=-sig;
L+=1;
su+=sig*min(comb)
print(su)
In the present case, the output is $8$, the "max" element of list $x$.
For the second case, just replace "min" by "max".
Jean Marie
- 88,997
-
There is a natural connection between maximum-minimums identity and inclusion-exclusion principle: see answers there – Jean Marie Nov 05 '24 at 21:10
def mymax(x): return sum([min(comb)*(-1)^(len(comb)+1) for comb in Combinations(x) if comb])anddef mymin(x): return sum([max(comb)*(-1)^(len(comb)+1) for comb in Combinations(x) if comb])? Or are you looking for a symbolic expansion? – rickhg12hs Jun 26 '20 at 12:35