Skip to content

Commit

Permalink
Making evaluation without dimension specified much faster
Browse files Browse the repository at this point in the history
  • Loading branch information
s-baumann committed Feb 15, 2019
1 parent 3dc4af2 commit ddb5516
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
30 changes: 2 additions & 28 deletions src/0_structs_and_generic_reversals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,8 @@ function underlying_dimensions(a::PE_Function)
return Set(keys(a.units_))
end

function evaluate(f::MultivariateFunction, coordinate::Float64)
if length(setdiff(underlying_dimensions(f), Set([default_symbol]))) == 0
coordinates = Dict([default_symbol] .=> coordinate)
return evaluate(f, coordinates)
else
error("It is not possible to use the evaluate method without using a dict unless the only variable is the default one.")
end
function evaluate(f::MultivariateFunction, coordinate::Float64; variable::Symbol = default_symbol)
return evaluate(f, Dict{Symbol,Float64}(variable => coordinate))
end

"""
Expand Down Expand Up @@ -252,10 +247,6 @@ struct Sum_Of_Functions <: MultivariateFunction
end
Base.broadcastable(e::Sum_Of_Functions) = Ref(e)

#function zero(f::Sum_Of_Functions)
# return Sum_Of_Functions([])
#end

function evaluate(f::Sum_Of_Functions, coordinates::Dict{Symbol,Float64})
if length(f.functions_) > 0
vals = evaluate.(f.functions_, Ref(coordinates))
Expand Down Expand Up @@ -283,15 +274,6 @@ function rebadge(f::Sum_Of_Functions, mapping::Dict{Symbol,Symbol})
return Sum_Of_Functions(funcs)
end

function evaluate(f::Sum_Of_Functions, coordinates::Float64)
if length(f.functions_) > 0
vals = evaluate.(f.functions_, Ref(coordinates))
return sum(vals)
else
return 0.0
end
end

function convert(::Type{Sum_Of_Functions}, f::PE_Function)
return Sum_Of_Functions(f)
end
Expand Down Expand Up @@ -495,14 +477,6 @@ end
function evaluate(f::Missing, coordinates::Dict{Symbol,Float64})
return missing
end
function evaluate(f::Piecewise_Function, coordinate::Float64)
if underlying_dimensions(f) == Set([default_symbol])
coordinates_ = Dict{Symbol,Float64}(default_symbol => coordinate)
return evaluate(f, coordinates_)
else
error("Cannot evaluate a Piecewise function without a dictionary set of coordinates unless it is a MultivariateFunction with only the default dimension being used.")
end
end

function underlying_dimensions(f::Piecewise_Function)
underlying = union(underlying_dimensions.(f.functions_)...)
Expand Down
16 changes: 16 additions & 0 deletions test/7_schumaker_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,19 @@ numerical_integral = integral(spline, lhs,rhs)

analytical = analytic_integral(lhs,rhs)
abs( analytical - numerical_integral ) < 0.001


#= Speed test of MultivariateFunctions vs Univariate vs SchumakerSpline
using SchumakerSpline
using UnivariateFunctions
x = collect(range(0.001, stop=10, length=1000))
y = log.(x)
@time schum = SchumakerSpline.Schumaker(x, y)
@time univ = UnivariateFunctions.create_quadratic_spline(x,y)
@time multiv = MultivariateFunctions.create_quadratic_spline(x,y)
@time SchumakerSpline.evaluate(schum, 7.8)
@time UnivariateFunctions.evaluate(univ, 7.8)
@time MultivariateFunctions.evaluate(multiv, 7.8)
@time MultivariateFunctions.evaluate(multiv, 7.8; variable = :default)
@time MultivariateFunctions.evaluate(multiv, Dict{Symbol,Float64}(:default => 7.8))
=#

0 comments on commit ddb5516

Please sign in to comment.