|
1 | | -export PolyForm, simplify_fractions, quick_cancel, flatten_fractions |
| 1 | +export PolyForm, simplify_fractions, quick_cancel, flatten_fractions, as_polynomial |
2 | 2 | using Bijections |
3 | 3 | using DynamicPolynomials: PolyVar |
4 | 4 |
|
|
43 | 43 | Base.hash(p::PolyForm, u::UInt64) = xor(hash(p.p, u), trunc(UInt, 0xbabacacababacaca)) |
44 | 44 | Base.isequal(x::PolyForm, y::PolyForm) = isequal(x.p, y.p) |
45 | 45 |
|
| 46 | + |
| 47 | +function as_polynomial(f, exprs...; polyform=false, T=Real) |
| 48 | + @assert length(exprs) >= 1 "At least one expression must be passed to `multivariatepolynomial`." |
| 49 | + |
| 50 | + pvar2sym, sym2term = get_pvar2sym(), get_sym2term() |
| 51 | + ps = map(exprs) do x |
| 52 | + if istree(x) && operation(x) == (/) |
| 53 | + num, den = arguments(x) |
| 54 | + PolyForm(num, pvar2sym, sym2term).p / |
| 55 | + PolyForm(den, pvar2sym, sym2term).p |
| 56 | + else |
| 57 | + PolyForm(x, pvar2sym, sym2term).p |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + convert_back_f = function (x) |
| 62 | + if x isa MultivariatePolynomials.RationalPoly |
| 63 | + PolyForm{T}(numerator(x), pvar2sym, sym2term) / |
| 64 | + PolyForm{T}(denominator(x), pvar2sym, sym2term) |
| 65 | + else |
| 66 | + PolyForm{T}(x, pvar2sym, sym2term) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + convert_back = polyform ? convert_back_f : unpolyize ∘ convert_back_f |
| 71 | + res = f(convert_back, ps...) |
| 72 | +end |
| 73 | + |
46 | 74 | # We use the same PVAR2SYM bijection to maintain the PolyVar <-> Sym mapping, |
47 | 75 | # When all PolyForms go out of scope in a session, we allow it to free up memory and |
48 | 76 | # start over if necessary |
|
0 commit comments