Skip to content

Instantly share code, notes, and snippets.

@maple3142
Created February 23, 2023 16:52
Show Gist options
  • Save maple3142/0bb20789d7372b7e0e822d1b91ca7867 to your computer and use it in GitHub Desktop.
Save maple3142/0bb20789d7372b7e0e822d1b91ca7867 to your computer and use it in GitHub Desktop.
sage multivariate polynomial variable substitution
def poly_sub(f, x, y):
ret = f.parent().zero()
for c, m in f:
while m % x == 0:
m //= x
c *= y
ret += c * m
return ret
def poly_sub2(f, x, y):
# https://ask.sagemath.org/question/25972/substitute-xy-by-u/#post-id-25996
Q = f.parent().quotient(x - y)
return Q(f).lift()
for R in [ZZ, Zmod(17 * 19), GF(2 ^ 8), CyclotomicField(17)]:
PR = R["x, y, z"]
x, y, z = PR.gens()
assert poly_sub((x * y + 1) ^ 7, x * y, z) == (z + 1) ^ 7
assert poly_sub2((x * y + 1) ^ 7, x * y, z) == (z + 1) ^ 7
assert poly_sub((x ^ 2 * y + 1) ^ 3, x * y, z) == (z * x + 1) ^ 3
assert poly_sub2((x ^ 2 * y + 1) ^ 3, x * y, z) == (z * x + 1) ^ 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment