Skip to content

Commit 79a828f

Browse files
committed
Add automatic slope evaluation
1 parent bfd84fb commit 79a828f

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

src/slopes.jl

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Reference : Dietmar Ratz - An Optimized Interval Slope Arithmetic and its Application
2+
using IntervalArithmetic
3+
import Base: +, -, *, /, ^, sqrt, exp, log
4+
5+
function slope(f::Function, x::Interval, c::Real)
6+
f(SlopeVar(x, c)).fs
7+
end
8+
9+
struct SlopeType
10+
fx::Interval
11+
fc::Interval
12+
fs::Interval
13+
end
14+
15+
function SlopeConst(c::Union{Real, Interval})
16+
SlopeType(c, c, 0)
17+
end
18+
19+
function SlopeVar(v::Real)
20+
SlopeType(v, v, 1)
21+
end
22+
23+
function SlopeVar(v::Interval, c::Real)
24+
SlopeType(v, c, 1)
25+
end
26+
27+
function fxValue(u::SlopeType)
28+
u.fx
29+
end
30+
31+
function fcValue(u::SlopeType)
32+
u.fc
33+
end
34+
35+
function fsValue(u::SlopeType)
36+
u.fs
37+
end
38+
39+
function +(u::SlopeType, v::SlopeType)
40+
SlopeType(u.fx + v.fx, u.fc + v.fc, u.fs + v.fs)
41+
end
42+
43+
function -(u::SlopeType, v::SlopeType)
44+
SlopeType(u.fx - v.fx, u.fc - v.fc, u.fs - v.fs)
45+
end
46+
47+
function *(u::SlopeType, v::SlopeType)
48+
SlopeType(u.fx * v.fx, u.fc * v.fc, u.fs * v.fc + u.fx * v.fs)
49+
end
50+
51+
function /(u::SlopeType, v::SlopeType)
52+
SlopeType(u.fx / v.fx, u.fc / v.fc, (u.fs - (u.fc / v.fc) * v.fs) / v.fx)
53+
end
54+
55+
function +(u::Union{Interval, Real}, v::SlopeType)
56+
SlopeType(u + v.fx, u + v.fc, v.fs)
57+
end
58+
59+
function -(u::Union{Interval, Real}, v::SlopeType)
60+
SlopeType(u - v.fx, u - v.fc, -v.fs)
61+
end
62+
63+
function *(u::Union{Interval, Real}, v::SlopeType)
64+
SlopeType(u * v.fx, u * v.fc, u * v.fs)
65+
end
66+
67+
function /(u::Union{Interval, Real}, v::SlopeType)
68+
SlopeType(u / v.fx, u / v.fc, -(u / v.fc) * (v.fs / v.fx))
69+
end
70+
71+
+(v::SlopeType, u::Union{Interval, Real}) = u + v
72+
73+
-(v::SlopeType, u::Union{Interval, Real}) = u - v
74+
75+
*(v::SlopeType, u::Union{Interval, Real}) = u * v
76+
77+
/(v::SlopeType, u::Union{Interval, Real}) = u / v
78+
79+
function sqr(u::SlopeType)
80+
SlopeType(u.fx ^ 2, u.fc ^ 2, (u.fx + u.fc) * u.fs)
81+
end
82+
83+
function ^(u::SlopeType, k::Integer)
84+
if k == 0
85+
return SlopeConst(1)
86+
elseif k == 1
87+
return u
88+
elseif k == 2
89+
return sqr(u)
90+
else
91+
hxi = interval(u.fx.lo) ^ k
92+
hxs = interval(u.fx.hi) ^ k
93+
hx = hull(hxi, hxs)
94+
95+
if (k % 2 == 0) && (0 u.fx)
96+
hx = interval(0, hx.hi)
97+
end
98+
99+
hc = u.fc ^ k
100+
101+
i = u.fx.lo - u.fc.lo
102+
s = u.fx.hi - u.fc.hi
103+
104+
if ((i == 0) || (s == 0) || (k % 2 == 1 && Interval(0) u.fx))
105+
h1 = k * (u.fx ^ (k - 1))
106+
else
107+
if k % 2 == 0 || u.fx.lo >= 0
108+
h1 = interval((hxi.hi - hc.lo) / i, (hxs.hi - hc.lo) / s)
109+
else
110+
h1 = interval((hxs.lo - hc.hi) / s, (hxi.lo - hc.hi) / i)
111+
end
112+
end
113+
return SlopeType(hx, hc, h1 * u.fs)
114+
end
115+
end
116+
117+
function sqrt(u::SlopeType)
118+
SlopeType(sqrt(u.fx), sqrt(u.fc), u.fs / (sqrt(u.fx) + sqrt(u.fc)))
119+
end
120+
121+
function exp(u::SlopeType)
122+
hx = exp(u.fx)
123+
hc = exp(u.fc)
124+
125+
i = u.fx.lo - u.fc.lo
126+
s = u.fx.hi - u.fc.hi
127+
128+
if (i == 0 || s == 0)
129+
h1 = hx
130+
else
131+
h1 = interval((hx.lo - hc.lo) / i, (hx.hi - hc.hi) / s)
132+
end
133+
134+
SlopeType(hx, hc, h1 * u.fs)
135+
end
136+
137+
function log(u::SlopeType)
138+
hx = log(u.fx)
139+
hc = log(u.fc)
140+
141+
i = u.fx.lo - u.fc.lo
142+
s = u.fx.hi - u.fc.hi
143+
144+
if (i == 0 || s == 0)
145+
h1 = 1 / u.fx
146+
else
147+
h1 = interval((hx.hi - hc.hi) / s, (hx.lo - hc.lo) / i)
148+
end
149+
SlopeType(hx, hc, h1 * u.fs)
150+
end

0 commit comments

Comments
 (0)