PiecewiseLinApprox.jl (formerly LinA.jl) is a state-of-the-art piecewise linear approximation package that approximates (or over/under-estimates) univariate differentiable
To install PiecewiseLinApprox, first, enter Pkg mode by hitting ]
, and then run the following command:
pkg> add https://github.com/LICO-labs/PiecewiseLinApprox.jl
For the full documentation, see the documentation webpage.
To linearize
julia> using PiecewiseLinApprox
julia> pwl = Linearize(:(x^2),-10,10,Absolute(2))
5-element Vector{PiecewiseLinApprox.LinearPiece}:
-16.000000000000004 x -62.00000000000003 from -10.0 to -6.0
-8.0 x -14.0 from -6.0 to -2.0
0.0 x + 2.0 from -2.0 to 2.0
8.0 x -14.0 from 2.0 to 5.999999999999999
15.999999999999996 x -61.99999999999998 from 5.999999999999999 to 10.0
Note: By default, PiecewiseLinApprox uses the hybrid heuristic algorithm. To use the (slightly slower) exact algorithm, simply add ExactLin()
as an argument.
You can now call pwl
as a Julia function such as
julia> pwl(2)
4.470129472588532
But also as an array to get the individual linear segments such as
julia> pwl[2]
-8.0 x -14.0 from -6.0 to -2.0
julia> pwl[2].xMax
-2.0
Pwl functions are native Julia functions and are compatible with Plots.jl. To plot a pwl function simply do
using Plots
plot(x->pwl(x),-10,10)
If you have used our library and wish to cite our work (which we greatly encourage) use the reference of our paper. Starring the PiecewiseLinApprox.jl repository on GitHub is also appreciated.