Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)

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