|
| 1 | +# Polynomial Normal Form |
| 2 | + |
| 3 | +""" |
| 4 | + labels!(dict, t) |
| 5 | +
|
| 6 | +Find all terms that are not + and * and replace them |
| 7 | +with a symbol, store the symbol => term mapping in `dict`. |
| 8 | +""" |
| 9 | +function labels! end |
| 10 | + |
| 11 | +# Turn a Term into a multivariate polynomial |
| 12 | +function labels!(dicts, t::Sym) |
| 13 | + sym2term, term2sym = dicts |
| 14 | + if !haskey(term2sym, t) |
| 15 | + sym2term[t] = t |
| 16 | + term2sym[t] = t |
| 17 | + end |
| 18 | + return t |
| 19 | +end |
| 20 | + |
| 21 | +function labels!(dicts, t) |
| 22 | + if t isa Integer |
| 23 | + return t |
| 24 | + elseif t isa Term && (operation(t) == (*) || operation(t) == (+) || operation(t) == (-)) |
| 25 | + tt = arguments(t) |
| 26 | + return Term{symtype(t)}(operation(t), map(x->labels!(dicts, x), arguments(t))) |
| 27 | + elseif t isa Term && operation(t) == (^) && length(arguments(t)) > 1 && isnonnegint(arguments(t)[2]) |
| 28 | + return Term{symtype(t)}(operation(t), map(x->labels!(dicts, x), arguments(t))) |
| 29 | + else |
| 30 | + sym2term, term2sym = dicts |
| 31 | + if haskey(term2sym, t) |
| 32 | + return term2sym[t] |
| 33 | + end |
| 34 | + if t isa Term |
| 35 | + tt = arguments(t) |
| 36 | + sym = Sym{symtype(t)}(gensym(nameof(operation(t)))) |
| 37 | + sym2term[sym] = Term{symtype(t)}(operation(t), |
| 38 | + map(x->to_mpoly(x, dicts)[1], arguments(t))) |
| 39 | + else |
| 40 | + sym = Sym{symtype(t)}(gensym("literal")) |
| 41 | + sym2term[sym] = t |
| 42 | + end |
| 43 | + |
| 44 | + term2sym[t] = sym |
| 45 | + |
| 46 | + return sym |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +ismpoly(x) = x isa MPoly || x isa Integer |
| 51 | +isnonnegint(x) = x isa Integer && x >= 0 |
| 52 | + |
| 53 | +const mpoly_rules = RuleSet([@rule(~x::ismpoly - ~y::ismpoly => ~x + -1 * (~y)) |
| 54 | + @acrule(~x::ismpoly + ~y::ismpoly => ~x + ~y) |
| 55 | + @rule(+(~x) => ~x) |
| 56 | + @acrule(~x::ismpoly * ~y::ismpoly => ~x * ~y) |
| 57 | + @rule(*(~x) => ~x) |
| 58 | + @rule((~x::ismpoly)^(~a::isnonnegint) => (~x)^(~a))]) |
| 59 | +function to_mpoly(t, dicts=(OrderedDict{Sym, Any}(), OrderedDict{Any, Sym}())) |
| 60 | + # term2sym is only used to assign the same |
| 61 | + # symbol for the same term -- in other words, |
| 62 | + # it does common subexpression elimination |
| 63 | + |
| 64 | + sym2term, term2sym = dicts |
| 65 | + labeled = labels!((sym2term, term2sym), t) |
| 66 | + |
| 67 | + if isempty(sym2term) |
| 68 | + return labeled, [] |
| 69 | + end |
| 70 | + |
| 71 | + ks = sort(collect(keys(sym2term)), lt=<ₑ) |
| 72 | + R, vars = PolynomialRing(ZZ, String.(nameof.(ks))) |
| 73 | + |
| 74 | + replace_with_poly = Dict{Sym,MPoly}(zip(ks, vars)) |
| 75 | + t_poly = substitute(labeled, replace_with_poly, fold=false) |
| 76 | + simplify(t_poly, EmptyCtx(), rules=mpoly_rules), |
| 77 | + sym2term, |
| 78 | + reverse(ks) |
| 79 | +end |
| 80 | + |
| 81 | +function to_term(x, dict, syms) |
| 82 | + dict = copy(dict) |
| 83 | + for (k, v) in dict |
| 84 | + dict[k] = _to_term(v, dict, syms) |
| 85 | + end |
| 86 | + _to_term(x, dict, syms) |
| 87 | +end |
| 88 | + |
| 89 | +function _to_term(x::MPoly, dict, syms) |
| 90 | + |
| 91 | + function mul_coeffs(exps) |
| 92 | + monics = [e == 1 ? syms[i] : syms[i]^e for (i, e) in enumerate(reverse(exps)) if !iszero(e)] |
| 93 | + if length(monics) == 1 |
| 94 | + return monics[1] |
| 95 | + elseif length(monics) == 0 |
| 96 | + return 1 |
| 97 | + else |
| 98 | + return Term(*, monics) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + monoms = [mul_coeffs(exponent_vector(x, i)) for i in 1:x.length] |
| 103 | + if length(monoms) == 0 |
| 104 | + return 0 |
| 105 | + end |
| 106 | + if length(monoms) == 1 |
| 107 | + t = !isone(x.coeffs[1]) ? monoms[1] * x.coeffs[1] : monoms[1] |
| 108 | + else |
| 109 | + t = Term(+, map((x,y)->isone(y) ? x : y*x, monoms, x.coeffs[1:length(monoms)])) |
| 110 | + end |
| 111 | + |
| 112 | + substitute(t, dict, fold=false) |
| 113 | +end |
| 114 | + |
| 115 | +function _to_term(x, dict, vars) |
| 116 | + if haskey(dict, x) |
| 117 | + return dict[x] |
| 118 | + else |
| 119 | + return x |
| 120 | + end |
| 121 | +end |
| 122 | + |
| 123 | +function _to_term(x::Term, dict, vars) |
| 124 | + t=Term{symtype(x)}(operation(x), _to_term.(arguments(x), (dict,), (vars,))) |
| 125 | +end |
| 126 | + |
| 127 | +<ₑ(a::MPoly, b::MPoly) = false |
0 commit comments