Skip to content

Commit

Permalink
Fix functions with an obligatory unitful domain
Browse files Browse the repository at this point in the history
  • Loading branch information
gustaphe committed Apr 2, 2021
1 parent 7164808 commit cc9330f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/UnitfulRecipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,30 @@ end
x, y, f.(x',y)
end
@recipe function f(f::Function, u::Units)
recipedata = RecipesBase.apply_recipe(plotattributes,f)
(f, xmin, xmax) = recipedata[1].args
f, xmin*u, xmax*u
uf = UnitFunction(f, [u])
recipedata = RecipesBase.apply_recipe(plotattributes, uf)
(_, xmin, xmax) = recipedata[1].args
return f, xmin*u, xmax*u
end

"""
```julia
UnitFunction
```
A function, bundled with the assumed units of each of its inputs.
```julia
f(x, y) = x^2 + y
uf = UnitFunction(f, u"m", u"m^2")
uf(3, 2) == f(3u"m", 2u"m"^2) == 7u"m^2"
```
"""
struct UnitFunction <: Function
f::Function
u::Vector{Units}
end
(f::UnitFunction)(args...) = f.f((args .* f.u)...)

#===============
Attribute fixing
===============#
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ end
pl = plot(f, m)
@test xguide(pl) == string(m)
@test yguide(pl) == string(m^2)
f(x) = exp(x/(3m))
@test plot(f, u"m") isa Plots.Plot
end
end

Expand Down

0 comments on commit cc9330f

Please sign in to comment.