|
1 | 1 | # Reference : Dietmar Ratz - An Optimized Interval Slope Arithmetic and its Application
|
2 | 2 | using IntervalArithmetic, ForwardDiff
|
3 | 3 | import Base: +, -, *, /, ^, sqrt, exp, log, sin, cos, tan, asin, acos, atan
|
| 4 | +import IntervalArithmetic: mid, interval |
4 | 5 |
|
5 | 6 | function slope(f::Function, x::Interval, c::Real)
|
6 | 7 | try
|
7 |
| - f(SlopeVar(x, c)).fs |
| 8 | + f(slope_var(x, c)).s |
8 | 9 | catch y
|
9 | 10 | if isa(y, MethodError)
|
10 | 11 | ForwardDiff.derivative(f, x)
|
11 | 12 | end
|
12 | 13 | end
|
13 | 14 | end
|
14 | 15 |
|
15 |
| -struct SlopeType |
16 |
| - fx::Interval |
17 |
| - fc::Interval |
18 |
| - fs::Interval |
| 16 | +struct Slope{T} |
| 17 | + x::Interval{T} |
| 18 | + c::Interval{T} |
| 19 | + s::Interval{T} |
| 20 | + Slope{T}(a, b, c) where T = new(a, b, c) |
| 21 | + Slope{T}(c) where T = Slope{T}(c, c, 0) |
19 | 22 | end
|
20 | 23 |
|
21 |
| -function SlopeConst(c::Union{Real, Interval}) |
22 |
| - SlopeType(c, c, 0) |
| 24 | +function slope_var(v::Real) |
| 25 | + Slope{Float64}(v, v, 1) |
23 | 26 | end
|
24 | 27 |
|
25 |
| -function SlopeVar(v::Real) |
26 |
| - SlopeType(v, v, 1) |
| 28 | +function slope_var(v::Interval, c::Real) |
| 29 | + Slope{Float64}(v, c, 1) |
27 | 30 | end
|
28 | 31 |
|
29 |
| -function SlopeVar(v::Interval, c::Real) |
30 |
| - SlopeType(v, c, 1) |
| 32 | +function interval(u::Slope) |
| 33 | + u.x |
31 | 34 | end
|
32 | 35 |
|
33 |
| -function fxValue(u::SlopeType) |
34 |
| - u.fx |
| 36 | +function mid(u::Slope) |
| 37 | + u.c |
35 | 38 | end
|
36 | 39 |
|
37 |
| -function fcValue(u::SlopeType) |
38 |
| - u.fc |
| 40 | +function slope(u::Slope) |
| 41 | + u.s |
39 | 42 | end
|
40 | 43 |
|
41 |
| -function fsValue(u::SlopeType) |
42 |
| - u.fs |
| 44 | +function +(u::Slope, v::Slope) |
| 45 | + Slope{Float64}(u.x + v.x, u.c + v.c, u.s + v.s) |
43 | 46 | end
|
44 | 47 |
|
45 |
| -function +(u::SlopeType, v::SlopeType) |
46 |
| - SlopeType(u.fx + v.fx, u.fc + v.fc, u.fs + v.fs) |
| 48 | +function -(u::Slope, v::Slope) |
| 49 | + Slope{Float64}(u.x - v.x, u.c - v.c, u.s - v.s) |
47 | 50 | end
|
48 | 51 |
|
49 |
| -function -(u::SlopeType, v::SlopeType) |
50 |
| - SlopeType(u.fx - v.fx, u.fc - v.fc, u.fs - v.fs) |
| 52 | +function *(u::Slope, v::Slope) |
| 53 | + Slope{Float64}(u.x * v.x, u.c * v.c, u.s * v.c + u.x * v.s) |
51 | 54 | end
|
52 | 55 |
|
53 |
| -function *(u::SlopeType, v::SlopeType) |
54 |
| - SlopeType(u.fx * v.fx, u.fc * v.fc, u.fs * v.fc + u.fx * v.fs) |
| 56 | +function /(u::Slope, v::Slope) |
| 57 | + Slope{Float64}(u.x / v.x, u.c / v.c, (u.s - (u.c / v.c) * v.s) / v.x) |
55 | 58 | end
|
56 | 59 |
|
57 |
| -function /(u::SlopeType, v::SlopeType) |
58 |
| - SlopeType(u.fx / v.fx, u.fc / v.fc, (u.fs - (u.fc / v.fc) * v.fs) / v.fx) |
| 60 | +function +(u::Union{Interval, Real}, v::Slope) |
| 61 | + Slope{Float64}(u + v.x, u + v.c, v.s) |
59 | 62 | end
|
60 | 63 |
|
61 |
| -function +(u::Union{Interval, Real}, v::SlopeType) |
62 |
| - SlopeType(u + v.fx, u + v.fc, v.fs) |
| 64 | +function -(u::Union{Interval, Real}, v::Slope) |
| 65 | + Slope{Float64}(u - v.x, u - v.c, -v.s) |
63 | 66 | end
|
64 | 67 |
|
65 |
| -function -(u::Union{Interval, Real}, v::SlopeType) |
66 |
| - SlopeType(u - v.fx, u - v.fc, -v.fs) |
| 68 | +function *(u::Union{Interval, Real}, v::Slope) |
| 69 | + Slope{Float64}(u * v.x, u * v.c, u * v.s) |
67 | 70 | end
|
68 | 71 |
|
69 |
| -function *(u::Union{Interval, Real}, v::SlopeType) |
70 |
| - SlopeType(u * v.fx, u * v.fc, u * v.fs) |
| 72 | +function /(u::Union{Interval, Real}, v::Slope) |
| 73 | + Slope{Float64}(u / v.x, u / v.c, -(u / v.c) * (v.s / v.x)) |
71 | 74 | end
|
72 | 75 |
|
73 |
| -function /(u::Union{Interval, Real}, v::SlopeType) |
74 |
| - SlopeType(u / v.fx, u / v.fc, -(u / v.fc) * (v.fs / v.fx)) |
75 |
| -end |
76 |
| - |
77 |
| -+(v::SlopeType, u::Union{Interval, Real}) = u + v |
| 76 | ++(v::Slope, u::Union{Interval, Real}) = u + v |
78 | 77 |
|
79 |
| --(v::SlopeType, u::Union{Interval, Real}) = u - v |
80 |
| --(u::SlopeType) = u * -1 |
| 78 | +-(v::Slope, u::Union{Interval, Real}) = u - v |
| 79 | +-(u::Slope) = u * -1 |
81 | 80 |
|
82 |
| -*(v::SlopeType, u::Union{Interval, Real}) = u * v |
| 81 | +*(v::Slope, u::Union{Interval, Real}) = u * v |
83 | 82 |
|
84 |
| -/(v::SlopeType, u::Union{Interval, Real}) = u / v |
| 83 | +/(v::Slope, u::Union{Interval, Real}) = u / v |
85 | 84 |
|
86 |
| -function sqr(u::SlopeType) |
87 |
| - SlopeType(u.fx ^ 2, u.fc ^ 2, (u.fx + u.fc) * u.fs) |
| 85 | +function sqr(u::Slope) |
| 86 | + Slope{Float64}(u.x ^ 2, u.c ^ 2, (u.x + u.c) * u.s) |
88 | 87 | end
|
89 | 88 |
|
90 |
| -function ^(u::SlopeType, k::Integer) |
| 89 | +function ^(u::Slope, k::Integer) |
91 | 90 | if k == 0
|
92 |
| - return SlopeConst(1) |
| 91 | + return Slope{Float64}(1) |
93 | 92 | elseif k == 1
|
94 | 93 | return u
|
95 | 94 | elseif k == 2
|
96 | 95 | return sqr(u)
|
97 | 96 | else
|
98 |
| - hxi = interval(u.fx.lo) ^ k |
99 |
| - hxs = interval(u.fx.hi) ^ k |
| 97 | + hxi = interval(u.x.lo) ^ k |
| 98 | + hxs = interval(u.x.hi) ^ k |
100 | 99 | hx = hull(hxi, hxs)
|
101 | 100 |
|
102 |
| - if (k % 2 == 0) && (0 ∈ u.fx) |
| 101 | + if (k % 2 == 0) && (0 ∈ u.x) |
103 | 102 | hx = interval(0, hx.hi)
|
104 | 103 | end
|
105 | 104 |
|
106 |
| - hc = u.fc ^ k |
| 105 | + hc = u.c ^ k |
107 | 106 |
|
108 |
| - i = u.fx.lo - u.fc.lo |
109 |
| - s = u.fx.hi - u.fc.hi |
| 107 | + i = u.x.lo - u.c.lo |
| 108 | + s = u.x.hi - u.c.hi |
110 | 109 |
|
111 |
| - if ((i == 0) || (s == 0) || (k % 2 == 1 && Interval(0) ⪽ u.fx)) |
112 |
| - h1 = k * (u.fx ^ (k - 1)) |
| 110 | + if ((i == 0) || (s == 0) || (k % 2 == 1 && Interval(0) ⪽ u.x)) |
| 111 | + h1 = k * (u.x ^ (k - 1)) |
113 | 112 | else
|
114 |
| - if k % 2 == 0 || u.fx.lo >= 0 |
| 113 | + if k % 2 == 0 || u.x.lo >= 0 |
115 | 114 | h1 = interval((hxi.hi - hc.lo) / i, (hxs.hi - hc.lo) / s)
|
116 | 115 | else
|
117 | 116 | h1 = interval((hxs.lo - hc.hi) / s, (hxi.lo - hc.hi) / i)
|
118 | 117 | end
|
119 | 118 | end
|
120 |
| - return SlopeType(hx, hc, h1 * u.fs) |
| 119 | + return Slope{Float64}(hx, hc, h1 * u.s) |
121 | 120 | end
|
122 | 121 | end
|
123 | 122 |
|
124 |
| -function sqrt(u::SlopeType) |
125 |
| - SlopeType(sqrt(u.fx), sqrt(u.fc), u.fs / (sqrt(u.fx) + sqrt(u.fc))) |
| 123 | +function sqrt(u::Slope) |
| 124 | + Slope{Float64}(sqrt(u.x), sqrt(u.c), u.s / (sqrt(u.x) + sqrt(u.c))) |
126 | 125 | end
|
127 | 126 |
|
128 |
| -function exp(u::SlopeType) |
129 |
| - hx = exp(u.fx) |
130 |
| - hc = exp(u.fc) |
| 127 | +function exp(u::Slope) |
| 128 | + hx = exp(u.x) |
| 129 | + hc = exp(u.c) |
131 | 130 |
|
132 |
| - i = u.fx.lo - u.fc.lo |
133 |
| - s = u.fx.hi - u.fc.hi |
| 131 | + i = u.x.lo - u.c.lo |
| 132 | + s = u.x.hi - u.c.hi |
134 | 133 |
|
135 | 134 | if (i == 0 || s == 0)
|
136 | 135 | h1 = hx
|
137 | 136 | else
|
138 | 137 | h1 = interval((hx.lo - hc.lo) / i, (hx.hi - hc.hi) / s)
|
139 | 138 | end
|
140 | 139 |
|
141 |
| - SlopeType(hx, hc, h1 * u.fs) |
| 140 | + Slope{Float64}(hx, hc, h1 * u.s) |
142 | 141 | end
|
143 | 142 |
|
144 |
| -function log(u::SlopeType) |
145 |
| - hx = log(u.fx) |
146 |
| - hc = log(u.fc) |
| 143 | +function log(u::Slope) |
| 144 | + hx = log(u.x) |
| 145 | + hc = log(u.c) |
147 | 146 |
|
148 |
| - i = u.fx.lo - u.fc.lo |
149 |
| - s = u.fx.hi - u.fc.hi |
| 147 | + i = u.x.lo - u.c.lo |
| 148 | + s = u.x.hi - u.c.hi |
150 | 149 |
|
151 | 150 | if (i == 0 || s == 0)
|
152 |
| - h1 = 1 / u.fx |
| 151 | + h1 = 1 / u.x |
153 | 152 | else
|
154 | 153 | h1 = interval((hx.hi - hc.hi) / s, (hx.lo - hc.lo) / i)
|
155 | 154 | end
|
156 |
| - SlopeType(hx, hc, h1 * u.fs) |
| 155 | + Slope{Float64}(hx, hc, h1 * u.s) |
157 | 156 | end
|
158 | 157 |
|
159 |
| -function sin(u::SlopeType) # Using derivative to upper bound the slope expansion for now |
160 |
| - hx = sin(u.fx) |
161 |
| - hc = sin(u.fc) |
162 |
| - hs = cos(u.fx) |
163 |
| - SlopeType(hx, hc, hs) |
| 158 | +function sin(u::Slope) # Using derivative to upper bound the slope expansion for now |
| 159 | + hx = sin(u.x) |
| 160 | + hc = sin(u.c) |
| 161 | + hs = cos(u.x) |
| 162 | + Slope{Float64}(hx, hc, hs) |
164 | 163 | end
|
165 | 164 |
|
166 |
| -function cos(u::SlopeType) # Using derivative to upper bound the slope expansion for now |
167 |
| - hx = cos(u.fx) |
168 |
| - hc = cos(u.fc) |
169 |
| - hs = -sin(u.fx) |
170 |
| - SlopeType(hx, hc, hs) |
| 165 | +function cos(u::Slope) # Using derivative to upper bound the slope expansion for now |
| 166 | + hx = cos(u.x) |
| 167 | + hc = cos(u.c) |
| 168 | + hs = -sin(u.x) |
| 169 | + Slope{Float64}(hx, hc, hs) |
171 | 170 | end
|
172 | 171 |
|
173 |
| -function tan(u::SlopeType) # Using derivative to upper bound the slope expansion for now |
174 |
| - hx = tan(u.fx) |
175 |
| - hc = tan(u.fc) |
176 |
| - hs = (sec(u.fx)) ^ 2 |
177 |
| - SlopeType(hx, hc, hs) |
| 172 | +function tan(u::Slope) # Using derivative to upper bound the slope expansion for now |
| 173 | + hx = tan(u.x) |
| 174 | + hc = tan(u.c) |
| 175 | + hs = (sec(u.x)) ^ 2 |
| 176 | + Slope{Float64}(hx, hc, hs) |
178 | 177 | end
|
179 | 178 |
|
180 |
| -function asin(u::SlopeType) |
181 |
| - hx = asin(u.fx) |
182 |
| - hc = asin(u.fc) |
183 |
| - hs = 1 / sqrt(1 - (u.fx ^ 2)) |
184 |
| - SlopeType(hx, hc, hs) |
| 179 | +function asin(u::Slope) |
| 180 | + hx = asin(u.x) |
| 181 | + hc = asin(u.c) |
| 182 | + hs = 1 / sqrt(1 - (u.x ^ 2)) |
| 183 | + Slope{Float64}(hx, hc, hs) |
185 | 184 | end
|
186 | 185 |
|
187 |
| -function acos(u::SlopeType) |
188 |
| - hx = acos(u.fx) |
189 |
| - hc = acos(u.fc) |
190 |
| - hs = -1 / sqrt(1 - (u.fx ^ 2)) |
191 |
| - SlopeType(hx, hc, hs) |
| 186 | +function acos(u::Slope) |
| 187 | + hx = acos(u.x) |
| 188 | + hc = acos(u.c) |
| 189 | + hs = -1 / sqrt(1 - (u.x ^ 2)) |
| 190 | + Slope{Float64}(hx, hc, hs) |
192 | 191 | end
|
193 | 192 |
|
194 |
| -function atan(u::SlopeType) |
195 |
| - hx = atan(u.fx) |
196 |
| - hc = atan(u.fc) |
197 |
| - hs = 1 / 1 + (u.fx ^ 2) |
198 |
| - SlopeType(hx, hc, hs) |
| 193 | +function atan(u::Slope) |
| 194 | + hx = atan(u.x) |
| 195 | + hc = atan(u.c) |
| 196 | + hs = 1 / 1 + (u.x ^ 2) |
| 197 | + Slope{Float64}(hx, hc, hs) |
199 | 198 | end
|
0 commit comments