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 TabularFunctions

Or 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.5
x = 1.5
y = func(x)

# output
2.25

If 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
nothing

PiecewiseLinearFunction

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.0
x = 0.0
y = func(x)
# output
0.0
x = 0.25
y = func(x)
# output
0.5
x = 0.5
y = func(x)
# output
1.0
x = 0.75
y = func(x)
# output
0.5
x = 1.0
y = func(x)
# output
0.0
x = 2.0
y = func(x)
# output
0.0

Reference

TabularFunctions.PiecewiseAnalyticFunctionType
struct PiecewiseAnalyticFunction{V<:(AbstractVector{<:Number}), Funcs} <: TabularFunctions.AbstractTabularFunction{V<:(AbstractVector{<:Number})}
  • x_vals::AbstractVector{<:Number}

  • funcs::Any

source
TabularFunctions.PiecewiseLinearFunctionType
struct PiecewiseLinearFunction{V1<:(AbstractVector{<:Number}), V2<:(AbstractVector{<:Number})} <: TabularFunctions.AbstractTabularFunction{V1<:(AbstractVector{<:Number})}
  • x_vals::AbstractVector{<:Number}

  • y_vals::AbstractVector{<:Number}

source
TabularFunctions.@piecewise_analyticMacro

Example: 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
source