-
Notifications
You must be signed in to change notification settings - Fork 1
/
tableau.jl
300 lines (238 loc) · 9.48 KB
/
tableau.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
@doc raw"""
Holds the tableau of a Runge-Kutta method
```math
\begin{aligned}
Q_{n,i} &= q_{n} + h \sum \limits_{j=1}^{s} a_{ij} \, v(t_{n} + c_j \Delta t, Q_{n,j}) , &
q_{n+1} &= q_{n} + h \sum \limits_{i=1}^{s} b_{i} \, v(t_{n} + c_j \Delta t, Q_{n,i}) , \\
\end{aligned}
```
Parameters:
* `T`: datatype of coefficient arrays
Fields:
* `name`: symbolic name of the tableau
* `o`: order of the method
* `s`: number of stages
* `a`: coefficients $a_{ij}$ with $ 1 \le i,j \le s$
* `b`: weights $b_{i}$ with $ 1 \le i \le s$
* `c`: nodes $c_{i}$ with $ 1 \le i \le s$
* `R∞`: stability function at infinity
Constructors:
```julia
Tableau{T}(name, o, s, a, b, c)
Tableau{T}(name, o, a, b, c)
Tableau(name::Symbol, o::Int, s::Int, a::AbstractMatrix, b::AbstractVector, c::AbstractVector)
Tableau(name::Symbol, o::Int, a::AbstractMatrix, b::AbstractVector, c::AbstractVector)
Tableau(name::Symbol, o::Int, t::AbstractMatrix)
```
The last constructor accepts an $(s+1) \times (s+1)$ array that holds the whole tableau in the form
of a Butcher tableau, i.e.,
c | a
---|---
| b
"""
struct Tableau{T, S, RT <: Union{Real,Missing}, L} <: AbstractTableau{T}
@TableauHeader
a::SMatrix{S,S,T,L}
b::SVector{S,T}
c::SVector{S,T}
â::SMatrix{S,S,T,L}
b̂::SVector{S,T}
ĉ::SVector{S,T}
R∞::RT
function Tableau{T}(name, o, s, a, b, c; R∞=missing) where {T}
@assert s > 0 "Number of stages must be > 0"
@assert s == size(a,1) == size(a,2) == length(b) == length(c)
ã = SMatrix{s,s}(convert(Matrix{T}, a))
b̃ = SVector{s}(convert(Vector{T}, b))
c̃ = SVector{s}(convert(Vector{T}, c))
â = SMatrix{s,s}(a .- ã)
b̂ = SVector{s}(b .- b̃)
ĉ = SVector{s}(c .- c̃)
new{T, s, typeof(R∞), s*s}(name,o,s,ã,b̃,c̃,â,b̂,ĉ,R∞)
end
function Tableau{T}(name, o, a, b, c; kwargs...) where {T}
Tableau{T}(name, o, length(c), a, b, c; kwargs...)
end
end
Tableau(name::Symbol, o::Int, s::Int, a::AbstractMatrix{AT}, b::AbstractVector{BT}, c::AbstractVector{CT}; kwargs...) where {AT,BT,CT} = Tableau{promote_type(AT,BT,CT)}(name,o,s,a,b,c; kwargs...)
Tableau(name::Symbol, o::Int, a::AbstractMatrix, b::AbstractVector, c::AbstractVector; kwargs...) = Tableau(name,o,length(c),a,b,c; kwargs...)
function Tableau(name::Symbol, o::Int, t::AbstractMatrix{T}; kwargs...) where {T}
@assert size(t,1) == size(t,2)
local s = size(t,1)-1
local a = copy(t[1:s, 2:s+1])
local b = copy(t[s+1, 2:s+1])
local c = copy(t[1:s, 1 ])
Tableau{T}(name, o, s, a, b, c; kwargs...)
end
Base.hash(tab::Tableau, h::UInt) = hash(tab.o, hash(tab.s, hash(tab.a, hash(tab.b, hash(tab.c, hash(tab.â, hash(tab.b̂, hash(tab.ĉ, hash(:Tableau, h)))))))))
Base.:(==)(tab1::Tableau, tab2::Tableau) = (tab1.o == tab2.o
&& tab1.s == tab2.s
&& tab1.a == tab2.a
&& tab1.b == tab2.b
&& tab1.c == tab2.c
&& tab1.â == tab2.â
&& tab1.b̂ == tab2.b̂
&& tab1.ĉ == tab2.ĉ
&& ((ismissing(tab1.R∞) && ismissing(tab2.R∞)) || (tab1.R∞ == tab2.R∞)))
Base.isapprox(tab1::Tableau, tab2::Tableau; kwargs...) = (
tab1.o == tab2.o
&& tab1.s == tab2.s
&& ((ismissing(tab1.R∞) && ismissing(tab2.R∞)) || (tab1.R∞ == tab2.R∞))
&& isapprox(tab1.a, tab2.a; kwargs...)
&& isapprox(tab1.b, tab2.b; kwargs...)
&& isapprox(tab1.c, tab2.c; kwargs...))
Base.isequal(tab1::Tableau{T1}, tab2::Tableau{T2}) where {T1,T2} = (tab1 == tab2 && T1 == T2 && tab1.name == tab2.name)
Base.eltype(::Tableau{T}) where {T} = T
function to_array(tab::Tableau{T}) where {T}
local s = tab.s
local arr = zeros(T, s+1, s+1)
arr[1:s, 1 ] .= tab.c
arr[s+1, 2:s+1] .= tab.b
arr[1:s, 2:s+1] .= tab.a
return arr
end
Base.convert(::Type{Tableau}, t::AbstractMatrix; name::Symbol=:nonamespecified, o::Int=0) = Tableau(name, o, t)
Base.convert(::Type{Matrix{T}}, tab::Tableau) where {T} = convert(Matrix{T}, to_array(tab))
Base.convert(::Type{Matrix}, tab::Tableau{T}) where {T} = convert(Matrix{T}, tab)
Base.convert(::Type{Array{T}}, tab::Tableau) where {T} = convert(Matrix{T}, tab)
Base.convert(::Type{Array}, tab::Tableau{T}) where {T} = convert(Matrix{T}, tab)
"""
```julia
from_file(dir::AbstractString, name::AbstractString)
```
Read Runge-Kutta tableau from the file `<name>.tsv` in the directory `dir`.
"""
function from_file(dir::AbstractString, name::AbstractString)
file = string(dir, "/", name, ".tsv")
# Reads and parse Tableau metadata from file
f = open(file, "r")
header = readline(f)
close(f)
if header[1] == '#'
header = split(header[2:end])
else
header = ()
end
if length(header) ≥ 1
o = Base.parse(Int, header[1])
else
o = 0
end
if length(header) ≥ 2
s = Base.parse(Int, header[2])
else
s = 0
end
if length(header) ≥ 3
T = Core.eval(Main, Meta.parse(header[3]))
else
T = Float64
end
# TODO Read data in original format (e.g., Rational).
# For this we need to save tableaus as jld or hdf5.
# tab_array = readdlm(file, T)
tab_array = readdlm(file, comments=true)
if s == 0
s = size(tab_array, 1) - 1
end
@assert s == size(tab_array, 1) - 1 == size(tab_array, 2) - 1
@info("Reading Runge-Kutta tableau $(name) with $(s) stages and order $(o) from file\n$(file)")
Tableau(Symbol(name), o, tab_array)
end
"""
```julia
to_file(dir::AbstractString, tab::Tableau)
```
Write Runge-Kutta tableau to the file `<tab.name>.tsv` in the directory `dir`.
"""
function to_file(dir::AbstractString, tab::Tableau{T}) where {T}
# tab_array = zeros(T, S+1, S+1)
# tab_array[1:S, 2:S+1] = tab.a
# tab_array[S+1, 2:S+1] = tab.b
# tab_array[1:S, 1] = tab.c
# tab_array[S+1, 1] = tab.order
tab_array = convert(Matrix, tab)
header = string("# ", tab.o, " ", tab.s, " ", T, "\n")
file = string(dir, "/", tab.name, ".tsv")
@info("Writing Runge-Kutta tableau $(tab.name) with $(tab.s) stages and order $(tab.o) to file\n$(file)")
f = open(file, "w")
write(f, header)
writedlm(f, float(tab_array))
close(f)
# TODO Write data in original format (e.g., Rational).
end
GeometricBase.name(tab::Tableau) = tab.name
GeometricBase.order(tab::Tableau) = tab.o
nstages(tab::Tableau) = tab.s
eachstage(tab::Tableau) = 1:nstages(tab)
coefficients(tab::Tableau) = tab.a
weights(tab::Tableau) = tab.b
nodes(tab::Tableau) = tab.c
GeometricBase.description(tab::Tableau) = "$(tab.name) with $(tab.s) $(tab.s == 1 ? "stage" : "stages") and order $(tab.o)"
GeometricBase.reference(tab::Tableau) = reference(Val(tab.name))
isexplicit(tab::Tableau) = istrilstrict(tab.a) && tab.c[1] == 0
isimplicit(tab::Tableau) = !isexplicit(tab)
isdiagonallyimplicit(tab::Tableau) = tab.s != 1 && !istrilstrict(tab.a) && istril(tab.a)
isfullyimplicit(tab::Tableau) = (tab.s == 1 && tab.a[1,1] != 0) || (!istrilstrict(tab.a) && !istril(tab.a))
const tf_butcher_tableau = TextFormat(
up_right_corner = ' ',
up_left_corner = ' ',
bottom_left_corner = ' ',
bottom_right_corner = ' ',
up_intersection = ' ',
left_intersection = ' ',
right_intersection = ' ',
middle_intersection = ' ',
bottom_intersection = ' ',
column = '│',
row = ' '
)
function show_coefficients(io::IO, tab::Tableau)
arr = convert(Matrix{Any}, tab)
arr[tab.s+1,1] = ""
pretty_table(io, arr,
tf = tf_butcher_tableau,
vlines = [1],
body_hlines = [tab.s],
body_hlines_format = ('─','┼','─','─'),
equal_columns_width = true,
show_header = false)
end
function Base.string(tab::Tableau)
strio = IOBuffer()
show_coefficients(strio, tab)
String(take!(strio))
end
"""
```julia
Base.show(io::IO, tab::Tableau)
```
Pretty-print Runge-Kutta tableau.
"""
function Base.show(io::IO, tab::Tableau)
print(io, "\nRunge-Kutta Tableau $(description(tab)):\n")
show_coefficients(io, tab)
end
"""
```julia
Base.show(io::IO, ::MIME"text/markdown", tab::Tableau)
```
Generate and print a nice markdown table for the Runge-Kutta tableau.
"""
function Base.show(io::IO, ::MIME"text/markdown", tab::Tableau)
show(io, "text/markdown", Markdown.parse("Runge-Kutta Tableau $(tab.name) with $(tab.s) stages and order $(tab.o):"))
tab_arr = convert(Matrix{Any}, tab)
tab_arr[tab.s+1,1] = ""
strio = IOBuffer()
pretty_table(strio, LatexCell.(tab_arr),
backend = Val(:latex),
vlines = [1],
hlines = [tab.s],
show_header = false)
tab_latex = String(take!(strio))
tab_markdown = replace(tab_latex, "tabular" => "array")
# tab_markdown = replace(tab_markdown, "\\begin{table}" => "```math")
# tab_markdown = replace(tab_markdown, "\\end{table}" => "```")
tab_markdown = "```math\n" * tab_markdown * "```\n"
print(io, tab_markdown)
end