Skip to content

Instantly share code, notes, and snippets.

@paniq
Last active November 8, 2024 13:39
Show Gist options
  • Save paniq/3f882c50f1790e323482 to your computer and use it in GitHub Desktop.
Save paniq/3f882c50f1790e323482 to your computer and use it in GitHub Desktop.
useful min/max/abs/sign identities
max(-x,-y) = -min(x,y)
min(-x,-y) = -max(x,y)
abs(x) = abs(-x)
abs(x) = max(x,-x) = -min(x,-x)
abs(x*a) = if (a >= 0) abs(x)*a
(a < 0) -abs(x)*a
// basically any commutative operation
min(x,y) + max(x,y) = x + y
min(x,y) ^ max(x,y) = x ^ y
min(x,y) * max(x,y) = x * y
min(x,y) = (x+y-abs(x-y))/2
max(x,y) = (x+y+abs(x-y))/2
min(x+a,y+a) = min(x,y)+a
max(x+a,y+a) = max(x,y)+a
min(x*a,y*a) = if (a >= 0) min(x,y)*a
(a < 0) max(x,y)*a
max(x*a,y*a) = if (a >= 0) max(x,y)*a
(a < 0) min(x,y)*a
max(x,0)^2 = x*max(x,0)
x = sign(x) * abs(x)
abs(x) = sign(x) * x
x/abs(x) = abs(x)/x
sign(x) = floor(x / (abs(x)+1)) - floor(-x / (abs(x)+1))
(without zero:)
sign(x) = floor(x / (abs(x)+1))*2+1
sign(x) ~= x/sqrt(x^2 + E^2), E -> 0
abs(x-y) <= abs(x-C) + abs(C-y)
abs(x+y) <= abs(x) + abs(y)
abs(x-y) >= abs(abs(x) - abs(y))
abs(x*y) = abs(x)*abs(y)
max(abs(x),abs(y))*2 = abs(x) + abs(y) + abs(abs(x) - abs(y)) = abs(x + y) + abs(x - y)
a + abs(b) = max(a + b, a - b)
# assuming a,b,x in [1,-1]
true = 1
false = -1
not x = -x
a and b = (1+a)*(1+b)/2 - 1
a or b = 1 - (1-a)*(1-b)/2
a xor b = -a*b
a > b = sign(a - b) # assuming sign(0) == 1
a < b = sign(b - a) # assuming sign(0) == 1
u == v = abs(sign(u - v))*2-1 # assuming sign(0) == 0
x?u:v = (u + v + x*(u - v))/2 = (u*(1 + x) + v*(1 - x))/2
@RYNO8
Copy link

RYNO8 commented Jun 28, 2021

also:
min(max(a,b),c)=max(min(a,c),b)

min(min(a,b),b)=min(a,b)
max(a+c,b+c)=c+max(a,b)

@turion
Copy link

turion commented Jan 5, 2023

min(max(a,b),c)=max(min(a,c),b)

That's not correct. Assume a = 1, b = 3, c = 2.

min(max(1,3),2) = min(3,2) = 2
max(min(1,2), 3) = max(2, 3) = 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment