Skip to content

Instantly share code, notes, and snippets.

@andy0130tw
Created March 20, 2023 18:44
Show Gist options
  • Save andy0130tw/f48956da310f7f2bf4e78a9ab6a1142d to your computer and use it in GitHub Desktop.
Save andy0130tw/f48956da310f7f2bf4e78a9ab6a1142d to your computer and use it in GitHub Desktop.
My sh*tting code to simplify a symbolic expression in SageMath over GF(p)
import sage
from sage.all_cmdline import * # import sage library
from sage.symbolic.operators import add_vararg, mul_vararg
def GF_simplify(_expr, p=2):
expr = expand(_expr)
if expr.operator() != add_vararg:
raise Exception('Can only simplify a sum over products.')
result = None
for term in expr.operands():
ignore_term = False
if term.operator() == mul_vararg:
for subt in term.operands():
if subt.is_integer():
n = subt.pyobject()
if n % p == 0:
ignore_term = True
if not ignore_term:
if result is None:
result = term
else:
result += term
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment