API Documentation
Docstrings for PiecewiseQuadratics.jl interface members can be accessed through Julia's built-in documentation system or in the list below.
Contents
Index
PiecewiseQuadratics.BoundedQuadratic
PiecewiseQuadratics.FixedMemoryPwq
PiecewiseQuadratics.Interval
PiecewiseQuadratics.PiecewiseQuadratic
Base.append!
Base.intersect
Base.isempty
Base.push!
Base.reverse
Base.sum
Base.zero
PiecewiseQuadratics.continuous_and_overlapping
PiecewiseQuadratics.derivative
PiecewiseQuadratics.domain
PiecewiseQuadratics.envelope
PiecewiseQuadratics.extend_dom
PiecewiseQuadratics.extend_dom!
PiecewiseQuadratics.get_line
PiecewiseQuadratics.get_plot
PiecewiseQuadratics.get_tangent
PiecewiseQuadratics.indicator
PiecewiseQuadratics.is_almost_point
PiecewiseQuadratics.is_convex
PiecewiseQuadratics.is_point
PiecewiseQuadratics.minimize
PiecewiseQuadratics.perspective
PiecewiseQuadratics.perspective!
PiecewiseQuadratics.prox
PiecewiseQuadratics.restrict_dom
PiecewiseQuadratics.restrict_dom!
PiecewiseQuadratics.reverse!
PiecewiseQuadratics.scale
PiecewiseQuadratics.scale!
PiecewiseQuadratics.shift
PiecewiseQuadratics.shift!
PiecewiseQuadratics.simplify
PiecewiseQuadratics.solve_quad
PiecewiseQuadratics.tilt
PiecewiseQuadratics.tilt!
Types
PiecewiseQuadratics.Interval
— TypeInterval(lb::Real, ub::Real)
Represents a univariate interval.
The fields represent:
lb
: Lower bound of the intervalub
: Upper bound of the interval
Example
julia> interval = Interval(0., 1.)
[0.00000, 1.00000]
julia> Interval()
ℝ
PiecewiseQuadratics.BoundedQuadratic
— TypeBoundedQuadratic(lb::Real, ub::Real, p::Real, q::Real, r::Real)
Represents bounded quadratic function
\[f(x) = px^2 + qx + r, ∀ x ∈ [lb, ub]\]
The fields represent:
lb
: Lower bound of the functionub
: Upper bound of the functionp
: Coefficient of the quadratic termq
: Coefficient of the linear termr
: Constant
Example
julia> bq = BoundedQuadratic(0., 5., 3., 2., 1.)
BoundedQuadratic: f(x) = 3.00000 x² + 2.00000 x + 1.00000, ∀x ∈ [0.00000, 5.00000]
PiecewiseQuadratics.PiecewiseQuadratic
— TypePiecewiseQuadratic()
PiecewiseQuadratic(f::BoundedQuadratic)
PiecewiseQuadratic(f_list::Vector{BoundedQuadratic}[; simplify_result=false])
Represents piecewise quadratic function, where each piece is a BoundedQuadratic
.
The fields represent:
f_list
: aVector
ofBoundedQuadratic
functions
Example
julia> left = BoundedQuadratic(-Inf, 0., 0., -1., 0.)
BoundedQuadratic: f(x) = - 1.00000 x , ∀x ∈ [-Inf, 0.00000]
julia> right = BoundedQuadratic(0., Inf, 0., 1., 0.)
BoundedQuadratic: f(x) = + 1.00000 x , ∀x ∈ [0.00000, Inf]
julia> pwq = PiecewiseQuadratic([left, right])
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 1.00000 x , ∀x ∈ [-Inf, 0.00000]
BoundedQuadratic: f(x) = + 1.00000 x , ∀x ∈ [0.00000, Inf]
PiecewiseQuadratics.FixedMemoryPwq
— TypeFixedMemoryPwq(len::Int64)
Represents piecewise quadratic function with a fixed-length Vector of BoundedQuadratic
s.
The fields represent:
f_list
: aVector
ofBoundedQuadratic
functionslen
: how many of thef_list
entries are filled in.
Constructors
Base.zero
— Functionzero(::Type{BoundedQuadratic})
Construct an empty BoundedQuadratic with no constraints.
Example
julia> zero(BoundedQuadratic)
BoundedQuadratic: f(x) = 0, ∀x ∈ ℝ
zero(::Type{PiecewiseQuadratic})
Construct an empty PiecewiseQuadratic with no constraints.
Example
julia> zero(PiecewiseQuadratic)
Piecewise quadratic function:
BoundedQuadratic: f(x) = 0, ∀x ∈ ℝ
PiecewiseQuadratics.indicator
— Functionindicator(dom::Interval)
indicator(lb::Real, ub::Real)
Construct a PiecewiseQuadratic that is 0
on the interval and Inf
everywhere else.
Example
julia> indicator(-5, 5)
Piecewise quadratic function:
BoundedQuadratic: f(x) = 0, ∀x ∈ [-5.00000, 5.00000]
PiecewiseQuadratics.get_line
— Functionget_line(x1::Real, y1::Real, x2::Real, y2::Real)
Construct a new unbounded BoundedQuadratic
representing a line passing through (x1, y1)
and (x2, y2)
.
Example
julia> line = get_line(1., 2., 3., 4.)
BoundedQuadratic: f(x) = + 1.00000 x + 1.00000, ∀x ∈ ℝ
Functions
Boolean
Base.isempty
— Functionisempty(A::Interval)
Return true
if the interval A
is empty (isempty(A::Interval)
).
isempty(f::BoundedQuadratic)
Return true
if the domain of the BoundedQuadratic f
is empty.
isempty(f::PiecewiseQuadratic)
Return true
if the PiecewiseQuadratic f
is empty (f_list
empty).
PiecewiseQuadratics.is_point
— Functionis_point(f::BoundedQuadratic)
Return true
if the BoundedQuadratic is defined only on a single point (lb == ub
).
See also: is_almost_point
Example
julia> bq = BoundedQuadratic(0., 5., 3., 2., 1.)
BoundedQuadratic: f(x) = 3.00000 x² + 2.00000 x + 1.00000, ∀x ∈ [0.00000, 5.00000]
julia> is_point(bq)
false
julia> bq = BoundedQuadratic(5., 5., 3., 2., 1.)
BoundedQuadratic: f(x) = 3.00000 x² + 2.00000 x + 1.00000, ∀x ∈ [5.00000, 5.00000]
julia> is_point(bq)
true
PiecewiseQuadratics.is_almost_point
— Functionis_almost_point(f::BoundedQuadratic)
Return true
if the BoundedQuadratic is approximately defined only on a single point (lb ≈ ub
).
See also: is_point
Example
julia> bq = BoundedQuadratic(5., 5. + 1e-14, 3., 2., 1.)
BoundedQuadratic: f(x) = 3.00000 x² + 2.00000 x + 1.00000, ∀x ∈ [5.00000, 5.00000]
julia> is_point(bq)
false
julia> is_almost_point(bq)
true
PiecewiseQuadratics.continuous_and_overlapping
— Functioncontinuous_and_overlapping(f::BoundedQuadratic, g::BoundedQuadratic)
Return true
if f
's right endpoint corresponds with g
's left endpoint.
PiecewiseQuadratics.is_convex
— Functionis_convex(f::PiecewiseQuadratic)
Return true
if f
is convex.
A PiecewiseQuadratic
is convex if for all i
:
f_i
is convexf_i.ub == f_{i+1}.lb
derivative(f_i)(f_i.ub) <= derivative(f_{i+1})(f_{i+1}.lb)
Example
julia> left = BoundedQuadratic(-Inf, 0., 0., -1., 0.)
BoundedQuadratic: f(x) = - 1.00000 x , ∀x ∈ [-Inf, 0.00000]
julia> right = BoundedQuadratic(0., Inf, 0., 1., 0.)
BoundedQuadratic: f(x) = + 1.00000 x , ∀x ∈ [0.00000, Inf]
julia> pwq = PiecewiseQuadratic([left, right])
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 1.00000 x , ∀x ∈ [-Inf, 0.00000]
BoundedQuadratic: f(x) = + 1.00000 x , ∀x ∈ [0.00000, Inf]
julia> is_convex(pwq)
true
Quadratic Manipulation
Base.reverse
— Functionreverse(f::BoundedQuadratic)
Return f
reversed over the y
axis. That is, given f(x)
, return f(-x)
.
See also: reverse!
Example
julia> bq = BoundedQuadratic(-1., 2., 1., 1., 1.)
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [-1.00000, 2.00000]
julia> reverse(bq)
BoundedQuadratic: f(x) = 1.00000 x² - 1.00000 x + 1.00000, ∀x ∈ [-2.00000, 1.00000]
reverse(f::PiecewiseQuadratic)
Return f
reversed over the y
axis. That is, given f(x)
, return f(-x)
.
See also: reverse
PiecewiseQuadratics.reverse!
— Functionreverse!(f::BoundedQuadratic)
reverse!(f::BoundedQuadratic, out::BoundedQuadratic)
Reverse f
inplace.
See also: reverse
reverse!(f::PiecewiseQuadratic)
reverse!(f::PiecewiseQuadratic, out::PiecewiseQuadratic)
Reverse f
inplace.
See also: reverse
PiecewiseQuadratics.scale
— Functionscale(f::BoundedQuadratic, α::Real)
Return a new BoundedQuadratic
that has been scaled by α
. That is, given f(x)
and α
, returns f(αx)
.
Note: this operation requires scaling the domain.
See also: scale!
Example
julia> bq = BoundedQuadratic(-1., 1., 1., 1., 1.)
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [-1.00000, 1.00000]
julia> scale(bq, 2.)
BoundedQuadratic: f(x) = 4.00000 x² + 2.00000 x + 1.00000, ∀x ∈ [-0.50000, 0.50000]
scale(f::PiecewiseQuadratic, α::Real)
Return a new PiecewiseQuadratic
that has been scaled by α
. That is, given f(x)
and α
, returns f(αx)
.
Note: this operation requires scaling the domain.
See also: scale!
Example
julia> f = PiecewiseQuadratic([BoundedQuadratic(-1., 0., 0., -1., 0.),
BoundedQuadratic(0., 1., 0., 1., 0.),
BoundedQuadratic(1., 5., 1., 1., 1.)])
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 1.00000 x , ∀x ∈ [-1.00000, 0.00000]
BoundedQuadratic: f(x) = + 1.00000 x , ∀x ∈ [0.00000, 1.00000]
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [1.00000, 5.00000]
julia> scale(f, 5)
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 5.00000 x , ∀x ∈ [-0.20000, 0.00000]
BoundedQuadratic: f(x) = + 5.00000 x , ∀x ∈ [0.00000, 0.20000]
BoundedQuadratic: f(x) = 25.00000 x² + 5.00000 x + 1.00000, ∀x ∈ [0.20000, 1.00000]
PiecewiseQuadratics.scale!
— Function scale!(f::BoundedQuadratic, α::Real)
scale!(f::BoundedQuadratic, α::Real, out::BoundedQuadratic)
Scale f
inplace.
See also: scale
scale!(f::PiecewiseQuadratic, α::Real)
scale!(f::PiecewiseQuadratic, α::Real, out::PiecewiseQuadratic)
Scale f
inplace.
See also: scale
PiecewiseQuadratics.perspective
— Functionperspective(f::BoundedQuadratic, α::Real)
Return the perspective function of f
. That is, given f(x)
and α
, return α * f(x / α)
.
Note: that this operation requires scaling of the domain.
See also: perspective!
Example
julia> bq = BoundedQuadratic(-1., 1., 1., 1., 1.)
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [-1.00000, 1.00000]
julia> perspective(bq, 2.)
BoundedQuadratic: f(x) = 0.50000 x² + 1.00000 x + 2.00000, ∀x ∈ [-2.00000, 2.00000]
perspective(f::PiecewiseQuadratic, α::Real)
Return the perspective function of f
. That is, given f(x)
and α
, return α * f(x / α)
.
Note: that this operation requires scaling of the domain.
See also: perspective!
Example
julia> f = PiecewiseQuadratic([BoundedQuadratic(-1., 0., 0., -5., 0.),
BoundedQuadratic(0., 2., 0., 2., 0.)])
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 5.00000 x , ∀x ∈ [-1.00000, 0.00000]
BoundedQuadratic: f(x) = + 2.00000 x , ∀x ∈ [0.00000, 2.00000]
julia> persp = perspective(f, 5.)
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 5.00000 x , ∀x ∈ [-5.00000, 0.00000]
BoundedQuadratic: f(x) = + 2.00000 x , ∀x ∈ [0.00000, 10.00000]
PiecewiseQuadratics.perspective!
— Functionperspective!(f::BoundedQuadratic, α::Real)
perspective!(f::BoundedQuadratic, α::Real, out::BoundedQuadratic)
Shift perspective of f
inplace.
See also: perspective
perspective!(f::PiecewiseQuadratic, α::Real)
perspective!(f::PiecewiseQuadratic, α::Real, out::PiecewiseQuadratic)
Shift perspective of f
inplace.
See also: perspective
PiecewiseQuadratics.shift
— Functionshift(f::BoundedQuadratic, δ::Real)
Return f
shifted along the x
-axis by δ
.
Note: for δ > 0
, this is a right shift. For δ < 0
, this is a left shift.
See also: shift!
Example
julia> bq = BoundedQuadratic(-1., 1., 1., 1., 1.)
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [-1.00000, 1.00000]
julia> shift(bq, 2.)
BoundedQuadratic: f(x) = 1.00000 x² - 3.00000 x + 3.00000, ∀x ∈ [1.00000, 3.00000]
shift(f::PiecewiseQuadratic, δ::Real)
Return f
shifted along the x
-axis by δ
.
Note: for δ > 0
, this is a right shift. For δ < 0
, this is a left shift.
See also: shift!
Example
julia> f = PiecewiseQuadratic([BoundedQuadratic(-1., 0., 0., -5., 0.),
BoundedQuadratic(0., 2., 0., 2., 0.)])
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 5.00000 x , ∀x ∈ [-1.00000, 0.00000]
BoundedQuadratic: f(x) = + 2.00000 x , ∀x ∈ [0.00000, 2.00000]
julia> shift(f, 5.)
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 5.00000 x + 25.00000, ∀x ∈ [4.00000, 5.00000]
BoundedQuadratic: f(x) = + 2.00000 x - 10.00000, ∀x ∈ [5.00000, 7.00000]
PiecewiseQuadratics.shift!
— Functionshift!(f::BoundedQuadratic, δ::Real)
shift!(f::BoundedQuadratic, δ::Real, out::BoundedQuadratic)
Shift f
inplace along the x
-axis by δ
.
See also: shift
shift!(f::PiecewiseQuadratic, δ::Real)
shift!(f::PiecewiseQuadratic, δ::Real, out::PiecewiseQuadratic)
Shift f
inplace along the x
-axis by δ
.
See also: shift
PiecewiseQuadratics.tilt
— Functiontilt(f::BoundedQuadratic, α::Real)
Return f
tilted by α
. This shifts linear coefficient q
by α
.
See also: tilt!
Example
julia> bq = BoundedQuadratic(-1., 1., 1., 1., 1.)
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [-1.00000, 1.00000]
julia> tilt(bq, 2.)
BoundedQuadratic: f(x) = 1.00000 x² + 3.00000 x + 1.00000, ∀x ∈ [-1.00000, 1.00000]
tilt(f::PiecewiseQuadratic, α::Real)
Return f
tilted by α
. This shifts linear coefficient q
by α
.
See also: tilt!
Example
julia> f = PiecewiseQuadratic([BoundedQuadratic(-1., 0., 0., -5., 0.),
BoundedQuadratic(0., 2., 0., 2., 0.)])
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 5.00000 x , ∀x ∈ [-1.00000, 0.00000]
BoundedQuadratic: f(x) = + 2.00000 x , ∀x ∈ [0.00000, 2.00000]
julia> tilt(f, 5.)
Piecewise quadratic function:
BoundedQuadratic: f(x) = 0, ∀x ∈ [-1.00000, 0.00000]
BoundedQuadratic: f(x) = + 7.00000 x , ∀x ∈ [0.00000, 2.00000]
PiecewiseQuadratics.tilt!
— Functiontilt!(f::BoundedQuadratic, α::Real)
tilt!(f::BoundedQuadratic, α::Real, out::BoundedQuadratic)
Tilt f
inplace.
See also: tilt
tilt!(f::PiecewiseQuadratic, α::Real)
tilt!(f::PiecewiseQuadratic, α::Real, out::PiecewiseQuadratic)
Tilt f
inplace.
PiecewiseQuadratics.restrict_dom
— Functionrestrict_dom(f::BoundedQuadratic, dom::Interval)
restrict_dom(f::BoundedQuadratic, lb::Real, ub::Real)
Return a new BoundedQuadratic with domain restricted to the intersect of the passed domain.
See also: restrict_dom!
Example
julia> bq = BoundedQuadratic(-10., 10., 1., 1., 1.)
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [-10.00000, 10.00000]
julia> restrict_dom(bq, 2., 3.)
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [2.00000, 3.00000]
PiecewiseQuadratics.restrict_dom!
— Functionrestrict_dom!(f::BoundedQuadratic, dom::Interval)
restrict_dom!(f::BoundedQuadratic, lb::Real, ub::Real)
restrict_dom!(f::BoundedQuadratic, lb::Real, ub::Real, out::BoundedQuadratic)
Restrict domain of f
inplace.
See also: restrict_dom
PiecewiseQuadratics.extend_dom
— Functionextend_dom(f::BoundedQuadratic)
Return an unbounded version of f
.
See also: extend_dom!
Example
julia> bq = BoundedQuadratic(-1., 1., 1., 1., 1.)
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [-1.00000, 1.00000]
julia> extend_dom(bq)
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ ℝ
PiecewiseQuadratics.extend_dom!
— Functionextend_dom!(f::BoundedQuadratic)
extend_dom!(f::BoundedQuadratic, out::BoundedQuadratic)
Remove the bounds of f
inplace.
See also: extend_dom
Base.append!
— Functionappend!(f::PiecewiseQuadratic, g::PiecewiseQuadratic; simplify_result=false)
Append a PiecewiseQuadratic g
to the right hand side of PiecewiseQuadratic f
.
Base.push!
— Function push!(f::PiecewiseQuadratic, g::BoundedQuadratic; simplify_result=false)
Add a BoundedQuadratic g
to the right hand side of PiecewiseQuadratic f
.
PiecewiseQuadratics.simplify
— Function simplify(f::PiecewiseQuadratic)
Return a simplified piecewise quadratic f
.
A BoundedQuadratic f_i
and and the current rightmost piece should be combined (to become the new rightmost piece) if
- Either of
f_i
or the rightmost piece is a point and they overlap at their lower and upper endpoints respectively f_i
and the rightmost piece have the same coefficients and they correspond at their lower and upper endpoints respectively (they're just currently separate parts of the same function).
We also eliminate functions that are points at Inf or -Inf if they come up.
Example
julia> f = PiecewiseQuadratic([BoundedQuadratic(-1., 0., 0., -1., 0.),
BoundedQuadratic(0., 0., 0., 0., 0.),
BoundedQuadratic(0., 1., 0., 1., 0.),
BoundedQuadratic(1., 2., 0., 1., 0.),
BoundedQuadratic(3., 5., 1., 1., 1.)])
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 1.00000 x , ∀x ∈ [-1.00000, 0.00000]
BoundedQuadratic: f(x) = 0, ∀x ∈ [0.00000, 0.00000]
BoundedQuadratic: f(x) = + 1.00000 x , ∀x ∈ [0.00000, 1.00000]
BoundedQuadratic: f(x) = + 1.00000 x , ∀x ∈ [1.00000, 2.00000]
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [3.00000, 5.00000]
julia> simplify(f)
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 1.00000 x , ∀x ∈ [-1.00000, 0.00000]
BoundedQuadratic: f(x) = + 1.00000 x , ∀x ∈ [0.00000, 2.00000]
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [3.00000, 5.00000]
Utility
PiecewiseQuadratics.domain
— Functiondomain(f::BoundedQuadratic)
Return the Interval on which the BoundedQuadratic is defined.
Example
julia> bq = BoundedQuadratic(0., 5., 3., 2., 1.)
BoundedQuadratic: f(x) = 3.00000 x² + 2.00000 x + 1.00000, ∀x ∈ [0.00000, 5.00000]
julia> dom = domain(bq)
[0.00000, 5.00000]
Base.intersect
— Functionintersect(f_list::Vector{BoundedQuadratic})
Intersect the domains of a list of BoundedQuadratics if possible.
Example
julia> f_list = [BoundedQuadratic(-100, 100, 1, 1, 1),
BoundedQuadratic(-50, 25, 1, 1, 1),
BoundedQuadratic(0, 50, 1, 1, 1)]
3-element Vector{BoundedQuadratic}:
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [-100.00000, 100.00000]
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [-50.00000, 25.00000]
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [0.00000, 50.00000]
julia> out, valid = intersect(f_list);
julia> valid
true
julia> out
3-element Vector{BoundedQuadratic}:
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [0.00000, 25.00000]
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [0.00000, 25.00000]
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [0.00000, 25.00000]
julia> f_list = [BoundedQuadratic(-100, 0, 1,1,1)
BoundedQuadratic(25, 50, 1,1,1)] # non-overlapping
2-element Vector{BoundedQuadratic}:
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [-100.00000, 0.00000]
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [25.00000, 50.00000]
julia> out, valid = intersect(f_list);
julia> valid
false
julia> out
2-element Vector{BoundedQuadratic}:
#undef
#undef
PiecewiseQuadratics.derivative
— Functionderivative(f::BoundedQuadratic)
Return the derivative of f
, f'
Example
julia> bq = BoundedQuadratic(3., 2., 1.)
BoundedQuadratic: f(x) = 3.00000 x² + 2.00000 x + 1.00000, ∀x ∈ ℝ
julia> derivative(bq)
BoundedQuadratic: f(x) = + 6.00000 x + 2.00000, ∀x ∈ ℝ
derivative(f::BoundedQuadratic, x::Real)
Evaluate the derivative of f
at x
, f'(x)
.
Example
julia> bq = BoundedQuadratic(3., 2., 1.)
BoundedQuadratic: f(x) = 3.00000 x² + 2.00000 x + 1.00000, ∀x ∈ ℝ
julia> derivative(bq, 2.)
14.0
Base.sum
— Functionsum(f_list::Vector{BoundedQuadratic})
Return the BoundedQuadratic sum of a list of BoundedQuadratics if it is valid, else nothing
.
Example
julia> bq = BoundedQuadratic(-1., 5., 1., 2., 3.)
BoundedQuadratic: f(x) = 1.00000 x² + 2.00000 x + 3.00000, ∀x ∈ [-1.00000, 5.00000]
julia> sum([bq, bq])
BoundedQuadratic: f(x) = 2.00000 x² + 4.00000 x + 6.00000, ∀x ∈ [-1.00000, 5.00000]
sum(f_list::Vector{PiecewiseQuadratic})
Return the PiecewiseQuadratic sum of a list of PiecewiseQuadratics.
Example
julia> f = PiecewiseQuadratic([BoundedQuadratic(-1., 0., 0., -5., 0.),
BoundedQuadratic(0., 2., 0., 2., 0.)])
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 5.00000 x , ∀x ∈ [-1.00000, 0.00000]
BoundedQuadratic: f(x) = + 2.00000 x , ∀x ∈ [0.00000, 2.00000]
julia> sum([f,f])
Piecewise quadratic function:
BoundedQuadratic: f(x) = - 10.00000 x , ∀x ∈ [-1.00000, 0.00000]
BoundedQuadratic: f(x) = + 4.00000 x , ∀x ∈ [0.00000, 2.00000]
julia> sum([f,-f])
Piecewise quadratic function:
BoundedQuadratic: f(x) = 0, ∀x ∈ [-1.00000, 0.00000]
BoundedQuadratic: f(x) = 0, ∀x ∈ [0.00000, 2.00000]
PiecewiseQuadratics.solve_quad
— Functionsolve_quad(a::Real, b::Real, c::Real)
Solve a quadratic function a x^2 + b x + c
using the quadratic formula.
PiecewiseQuadratics.get_tangent
— Functionget_tangent(f::BoundedQuadratic, x::Real)
Construct a new unbounded BoundedQuadratic
representing the (unbounded) tangent line to f
at the point x
.
Example
julia> bq = BoundedQuadratic(-1., 1., 1., 1., 1.)
BoundedQuadratic: f(x) = 1.00000 x² + 1.00000 x + 1.00000, ∀x ∈ [-1.00000, 1.00000]
julia> tangent = get_tangent(bq, 0.5)
BoundedQuadratic: f(x) = + 2.00000 x + 0.75000, ∀x ∈ ℝ
Optimization
PiecewiseQuadratics.minimize
— Functionminimize(f::BoundedQuadratic)
Return the minimum x
and f(x)
of f
over its domain.
minimize(f::PiecewiseQuadratic)
Return the minimum x
and f(x)
of f
over its domain.
PiecewiseQuadratics.envelope
— Functionenvelope(f::PiecewiseQuadratic)
Computes the greatest convex lower bound of a PiecewiseQuadratic
f
.
Example
julia> f = PiecewiseQuadratic([BoundedQuadratic(-Inf, 0., 0., 1., 0.),
BoundedQuadratic(0., 3., 0., 0., 0.)])
Piecewise quadratic function:
BoundedQuadratic: f(x) = + 1.00000 x , ∀x ∈ [-Inf, 0.00000]
BoundedQuadratic: f(x) = 0, ∀x ∈ [0.00000, 3.00000]
julia> envelope(f)
Piecewise quadratic function:
BoundedQuadratic: f(x) = + 1.00000 x - 3.00000, ∀x ∈ [-Inf, 3.00000]
PiecewiseQuadratics.prox
— Functionprox(f::PiecewiseQuadratic, u::Float64[, ρ::Float64=1.0]; use_quadratic::Bool=true)
Return the proximal operator of f
, ρ
evaluated at u
.
Note: The proximal operator of f
, rho
is denoted:
\[prox_{f, rho}(u) = argmin_{x ∈ dom(f)} f(x) + (rho / 2)||x - u||_2^2\]
See Section 6.2 of arXiv:2103.05455 for more information.
Plotting
PiecewiseQuadratics.get_plot
— Functionget_plot(f::PiecewiseQuadratic; N::Int64=1000)
Return x
, y
N
-vectors for use with plotting libraries.
Example
julia> using Plots
julia> f = PiecewiseQuadratic([
BoundedQuadratic(-Inf, 3.0, 1.0, -3.0, 3.0),
BoundedQuadratic(3.0, 4.0, 0.0, -1.0, 3.0),
BoundedQuadratic(4.0, 6.0, 2.0, -20.0, 47.0),
BoundedQuadratic(6.0, 7.5, 0.0, 1.0, -7.0),
BoundedQuadratic(7.5, Inf, 0.0, 4.0, -29.0)
])
Piecewise quadratic function:
BoundedQuadratic: f(x) = 1.00000 x² - 3.00000 x + 3.00000, ∀x ∈ [-Inf, 3.00000]
BoundedQuadratic: f(x) = - 1.00000 x + 3.00000, ∀x ∈ [3.00000, 4.00000]
BoundedQuadratic: f(x) = 2.00000 x² - 20.00000 x + 47.00000, ∀x ∈ [4.00000, 6.00000]
BoundedQuadratic: f(x) = + 1.00000 x - 7.00000, ∀x ∈ [6.00000, 7.50000]
BoundedQuadratic: f(x) = + 4.00000 x - 29.00000, ∀x ∈ [7.50000, Inf]
julia> plot(get_plot(f); grid=false, linestyle=:dash, color=:black, label="piece-wise quadratic")
Plot{Plots.GRBackend() n=1}
julia> plot!(get_plot(simplify(envelope(f))); color=:blue, la=0.5, label="envelope")
Plot{Plots.GRBackend() n=2}