Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions perf/runbenchmarks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MultivariatePolynomials
using BenchmarkTools

let
@polyvar x y
p = x + y + 2x^2 + y^3

vars = [x, y]
vals = [1.0, 2.0]

println(@benchmark(($p)($vals, $vars)))

vars_out_of_order = [vars[2], vars[1]]
println(@benchmark(($p)($vals, $vars_out_of_order)))
end
31 changes: 17 additions & 14 deletions src/subs.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
function evalmap(vars, x::Vector, varorder::Vector{PolyVar})
vals = Any[var for var in vars]
for (i, var) in enumerate(varorder)
j = findfirst(vars, var)
# If i == 0, that means that the variable is not present
# so it is ignored
if j > 0
vals[j] = x[i]
function evalmap{T}(vars, x::Vector{T}, varorder::Vector{PolyVar})
Copy link
Member

@blegat blegat Nov 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do a shortcut like:

if vars == varorder
x
else
... # current body of evalmap
end

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea; done

if vars == varorder
x
else
vals = Vector{T}(length(vars))
for (i, var) in enumerate(varorder)
j = findfirst(vars, var)
# If i == 0, that means that the variable is not present
# so it is ignored
if j > 0
vals[j] = x[i]
end
end
for i in 1:length(vals)
@assert isdefined(vals, i) "Variable $(vars[i]) was not assigned a value"
end
vals
end
vals
end

function monoeval(z::Vector{Int}, vals::Vector)
Expand Down Expand Up @@ -39,11 +46,7 @@ end

function (p::VecPolynomial)(x::Vector, varorder)
vals = evalmap(vars(p), x, varorder)
q = zero(p)
for i in 1:length(p)
q += p.a[i] * monoeval(p.x.Z[i], vals)
end
q
sum(i -> p.a[i] * monoeval(p.x.Z[i], vals), 1:length(p))
end

function (p::MatPolynomial)(x::Vector, varorder)
Expand Down
1 change: 1 addition & 0 deletions test/REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
FactCheck
BenchmarkTools
5 changes: 5 additions & 0 deletions test/subs.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Base.Test: @inferred

facts("Substitution") do
@polyvar x[1:3]

Expand All @@ -23,4 +25,7 @@ facts("Substitution") do
P = [1 2 3; 2 4 5; 3 5 6]
p = MatPolynomial(P, x)
@fact p(ones(3), x) --> 31

p = x[1] + x[2] + 2*x[1]^2 + 3*x[1]*x[2]^2
@inferred p([1.0, 2.0], [x[1], x[2]])
end