Created
June 24, 2025 16:10
-
-
Save hwayne/0ed045a35376c786171f9cf4b55c470f to your computer and use it in GitHub Desktop.
Finding a polynomial that looks like `max`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from z3 import * # type: ignore | |
s1, s2 = Solver(), Solver() | |
a0, a, b, c, d, e, f = Consts('a0 a b c d e f', IntSort()) | |
x, y, z = Ints('x y z') | |
t = "a*x+b*y+c*z+d*x*y+e*x*z+f*y*z+a0" | |
lambdamax = lambda x, y, z: eval(t) | |
z3max = Function('z3max', IntSort(), IntSort(), IntSort(), IntSort()) | |
s1.add(ForAll([x, y, z], z3max(x, y, z) == eval(t))) | |
inputs = [(1,2,3), (4, 2, 2), (1, 1, 1), (3, 5, 4)] | |
for g in inputs: | |
s1.add(z3max(*g) == max(*g)) | |
s2.add(lambdamax(*g) == max(*g)) | |
for s, func in [(s1, z3max), (s2, lambdamax)]: | |
if s.check() == sat: | |
m = s.model() | |
for x, y, z in inputs: | |
print(f"max([{x}, {y}, {z}]) =", m.evaluate(func(x, y, z))) | |
print(f"max([x, y, z]) = {m[a]}x + {m[b]}y", | |
f"+ {m[c]}z +", # linebreaks added for newsletter rendering | |
f"{m[d]}xy + {m[e]}xz + {m[f]}yz + {m[a0]}\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment