Skip to content

Commit 46ca17f

Browse files
committed
convert_back callback in as_polynomial
1 parent 20f377e commit 46ca17f

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/polyform.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ Base.hash(p::PolyForm, u::UInt64) = xor(hash(p.p, u), trunc(UInt, 0xbabacacabab
4444
Base.isequal(x::PolyForm, y::PolyForm) = isequal(x.p, y.p)
4545

4646

47-
function as_polynomial(f, exprs...; T=Real)
47+
function as_polynomial(f, exprs...; polyform=false, T=Real)
4848
@assert length(exprs) >= 1 "At least one expression must be passed to `multivariatepolynomial`."
4949

5050
pvar2sym, sym2term = get_pvar2sym(), get_sym2term()
5151
ps = map(x->PolyForm(x, pvar2sym, sym2term), exprs)
52-
PolyForm{T}(f(map(x->x.p, ps)...), pvar2sym, sym2term) # substitute back
52+
convert_back = polyform ? x -> PolyForm{T}(x, pvar2sym, sym2term) :
53+
x -> unpolyize(PolyForm{T}(x, pvar2sym, sym2term)) # substitute back
54+
res = f(convert_back, map(x->x.p, ps)...)
5355
end
5456

5557
# We use the same PVAR2SYM bijection to maintain the PolyVar <-> Sym mapping,

test/polyform.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ end
8181
@testset "as_polynomial" begin
8282

8383
@syms x y
84-
@eqtest as_polynomial(gcd, 10x+10y, 5*(x^2-y^2)) == 5x + 5y
85-
@test as_polynomial(gcd, 10x+10y, 5*(x^2-y^2), polyform=true) isa PolyForm
86-
@test repr(as_polynomial(gcd, 10x+10y, 5*(x^2-y^2), polyform=true)) == repr(5x + 5y)
84+
@eqtest as_polynomial((f, x, y) -> f(gcd(x, y)), 10x+10y, 5*(x^2-y^2)) == 5x + 5y
85+
@test as_polynomial((f, x, y) -> f(gcd(x, y)), 10x+10y, 5*(x^2-y^2), polyform=true) isa PolyForm
86+
@test repr(as_polynomial((f, x, y) -> f(gcd(x, y)), 10x+10y, 5*(x^2-y^2), polyform=true)) == repr(5x + 5y)
8787
end

0 commit comments

Comments
 (0)