https://www.primo-engineering.com/2017/11/5-3-constrained-maximum-and-minimum.html