-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcontractors.jl
168 lines (126 loc) · 3.57 KB
/
contractors.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
export Bisection, Newton, Krawczyk
Base.isinf(X::IntervalBox) = any(isinf.(X))
IntervalArithmetic.mid(X::IntervalBox, α) = mid.(X, α)
doc"""
Contractor{F}
Abstract type for contractors.
"""
abstract type Contractor{F} end
doc"""
Bisection{F} <: Contractor{F}
Contractor type for the bisection method.
"""
struct Bisection{F} <: Contractor{F}
f::F
end
function (contractor::Bisection)(X, tol)
image = (contractor.f)(X)
if !(contains_zero(image))
return :empty, X
end
return :unknown, X
end
doc"""
newtonlike_contract(op, X, tol)
Contraction operation for contractors using the first derivative of the
function. This contraction use a bisection scheme to refine the intervals
with `:unkown` status.
Currently `Newton` and `Krawczyk` contractors uses this.
"""
function newtonlike_contract(op, C, X, tol)
# use Bisection contractor for this:
if !(contains_zero(C.f(X)))
return :empty, X
end
# given that have the Jacobian, can also do mean value form
NX = op(C.f, C.f′, X) ∩ X
isempty(NX) && return :empty, X
isinf(X) && return :unknown, NX # force bisection
if NX ⪽ X # isinterior; know there's a unique root inside
NX = refine(X -> op(C.f, C.f′, X), NX, tol)
return :unique, NX
end
return :unknown, NX
end
doc"""
Newton{F, FP} <: Contractor{F}
Contractor type for the Newton method.
# Fields
- `f::F`: function whose roots are searched
- `f::FP`: derivative or jacobian of `f`
"""
struct Newton{F,FP} <: Contractor{F}
f::F
f′::FP # use \prime<TAB> for ′
end
function (C::Newton)(X, tol)
newtonlike_contract(𝒩, C, X, tol)
end
doc"""
Single-variable Newton operator
"""
function 𝒩{T}(f, X::Interval{T})
m = Interval(mid(X, where_bisect))
m - (f(m) / ForwardDiff.derivative(f, X))
end
function 𝒩{T}(f, f′, X::Interval{T})
m = Interval(mid(X, where_bisect))
m - (f(m) / f′(X))
end
function 𝒩{T}(f, X::Interval{T}, dX::Interval{T})
m = Interval(mid(X, where_bisect))
m - (f(m) / dX)
end
doc"""
Multi-variable Newton operator.
"""
function 𝒩(f::Function, jacobian::Function, X::IntervalBox) # multidimensional Newton operator
m = IntervalBox(Interval.(mid(X, where_bisect)))
J = jacobian(SVector(X))
return IntervalBox(m - (J \ f(m)))
end
doc"""
Krawczyk{F, FP} <: Contractor{F}
Contractor type for the Krawczyk method.
# Fields
- `f::F`: function whose roots are searched
- `f::FP`: derivative or jacobian of `f`
"""
struct Krawczyk{F, FP} <: Contractor{F}
f::F
f′::FP # use \prime<TAB> for ′
end
function (C::Krawczyk)(X, tol)
newtonlike_contract(𝒦, C, X, tol)
end
doc"""
Single-variable Krawczyk operator
"""
function 𝒦(f, f′, X::Interval{T}) where {T}
m = Interval(mid(X))
Y = 1/f′(m)
m - Y*f(m) + (1 - Y*f′(X))*(X - m)
end
doc"""
Multi-variable Krawczyk operator
"""
function 𝒦(f, jacobian, X::IntervalBox{T}) where {T}
m = mid(X)
J = jacobian(X)
Y = inv(jacobian(m))
m = IntervalBox(Interval.(m))
IntervalBox(m - Y*f(m) + (I - Y*J)*(X - m))
end
"""
Generic refine operation for Krawczyk and Newton.
This function assumes that it is already known that `X` contains a unique root.
Call using e.g. `op = X -> N(f, f′, X)`
"""
function refine(op, X, tolerance=1e-16)
while diam(X) > tolerance # avoid problem with tiny floating-point numbers if 0 is a root
NX = op(X) ∩ X
NX == X && break # reached limit of precision
X = NX
end
return X
end