Skip to content

Latest commit

 

History

History
99 lines (85 loc) · 2.46 KB

File metadata and controls

99 lines (85 loc) · 2.46 KB

Build Status Aqua QA Coverage Dev

TabularFunctions.jl

  1. Description
  2. Installation
  3. Examples

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

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

func = @piecewise_analytic begin
    0.0, x -> x
    1.0, x -> x^2
end

and this can be used like a regular julia function as follows

x = 0.5
y = func(x)
# y = 0.5
x = 1.5
y = func(x)
# y = 2.25

Note that closures are not necessary in the macro definition. The following is also valid syntax for the @piecewise_analytic macro

func = @piecewise_analytic begin
    0.0, sin(x)
    1.0, cos(x)
end

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

func = @piecewise_linear begin
    0.0, 0.0
    0.5, 1.0
    1.0, 0.0
end

x = -1.0
y = func(x)
# y = 0.0
x = 0.0
y = func(x)
# y = 0.0
x = 0.25
y = func(x)
# y = 0.5
x = 0.5
y = func(x)
# y = 1.0
x = 0.75
y = func(x)
# y = 0.5
x = 1.0
y = func(x)
# y = 0.0
x = 2.0
y = func(x)
# y = 0.0