TabularFunctions.jl
Description
This is a small package to help define julia functions via either tabular data, e.g. (x, y) pairs, or tables of functions to aid in simply writing piecewise analytic functions.
Installation
Currently TabularFunctions.jl has not been registered. To install, one can do the following in the package manager
pkg> add https://github.com/Cthonios/TabularFunctions.jl/Future installation instructions
From the package manager simply type
pkg> add TabularFunctionsOr from the REPL
julia> using Pkg
julia> Pkg.add("TabularFunctions") Examples
PiecewiseAnalyticFunction
Suppose we like to define a piecewise analytic function such that $ f(x) = \begin{cases} x, & \text{if } x < 1 \ x^2, & \text{if } x \ge 1 \end{cases} $
then we can use the maco @piecewise_analytic to define the above function as follows
using Plots
using TabularFunctions
func = @piecewise_analytic begin
0.0, x -> x
1.0, x -> x^2
end;
nothing
# output
and this can be used like a regular julia function as follows
x = 0.5
y = func(x)
# output
0.5x = 1.5
y = func(x)
# output
2.25If you have loaded Plots.jl or any other plotting package that can leverage RecipesBase.jl, then you can readily plot types in TabularFunctions.jl using the following syntax (Plots.jl)
xs = -5.:0.01:5. |> collect
p = plot(xs, func)
nothing
# output
Note that closures are not necessary in the macro definition. The following is also valid syntax for the @piecewise_analytic macro
using TabularFunctions
func = @piecewise_analytic begin
0.0, sin
1.0, cos
end
nothingPiecewiseLinearFunction
If instead you need to define a function simply from sparse tabular data, you can use the @piecewise_linear macro. This creates a simple function that will exactly reproduce values at the supplied points and linearly interpolate when provided with values between those points. If the provided input lies outside the bounds, the lower or upper bound is returned respectively. An example of a triangle wave is shown below
using TabularFunctions
func = @piecewise_linear begin
0.0, 0.0
0.5, 1.0
1.0, 0.0
end
nothing
# output
x = -1.0
y = func(x)
# output
0.0x = 0.0
y = func(x)
# output
0.0x = 0.25
y = func(x)
# output
0.5x = 0.5
y = func(x)
# output
1.0x = 0.75
y = func(x)
# output
0.5x = 1.0
y = func(x)
# output
0.0x = 2.0
y = func(x)
# output
0.0Reference
TabularFunctions.AbstractTabularFunction — Typeabstract type AbstractTabularFunction{V<:(AbstractVector{<:Number})} <: FunctionTabularFunctions.PiecewiseAnalyticFunction — Typestruct PiecewiseAnalyticFunction{V<:(AbstractVector{<:Number}), Funcs} <: TabularFunctions.AbstractTabularFunction{V<:(AbstractVector{<:Number})}x_vals::AbstractVector{<:Number}funcs::Any
TabularFunctions.PiecewiseAnalyticFunction — MethodTabularFunctions.PiecewiseAnalyticFunction — MethodPiecewiseAnalyticFunction(
xs::AbstractVector{<:Number},
funcs::AbstractVector
) -> PiecewiseAnalyticFunction{V, Funcs} where {V<:(AbstractVector{<:Number}), Funcs<:NamedTuple}
TabularFunctions.PiecewiseLinearFunction — Typestruct PiecewiseLinearFunction{V1<:(AbstractVector{<:Number}), V2<:(AbstractVector{<:Number})} <: TabularFunctions.AbstractTabularFunction{V1<:(AbstractVector{<:Number})}x_vals::AbstractVector{<:Number}y_vals::AbstractVector{<:Number}
TabularFunctions.PiecewiseLinearFunction — MethodTabularFunctions.XsNotMonotonicallyIncreasing — Typestruct XsNotMonotonicallyIncreasing <: ExceptionAdapt.adapt_structure — Methodadapt_structure(
to,
func::PiecewiseAnalyticFunction
) -> PiecewiseAnalyticFunction
Adapt.adapt_structure — Methodadapt_structure(
to,
f::PiecewiseLinearFunction
) -> PiecewiseLinearFunction
KernelAbstractions.get_backend — Methodget_backend(
f::TabularFunctions.AbstractTabularFunction
) -> Any
TabularFunctions.@piecewise_analytic — MacroExample: Define a function that switches between a linear and quadratic function
func = @piecewise_analytic begin
0.0, x -> x
1.0, x -> x^2
end
func(0.)
# output
0.0
TabularFunctions.@piecewise_linear — MacroExample: Define a triangular wave of a single period
func = @piecewise_linear begin
0.0, 0.0
0.5, 1.0
1.0, 0.0
end