Skip to content

Commit d28d016

Browse files
Add Base.show for integration rules (#189)
* add Base.show for integration rules * add _kwargs_to_string function
1 parent e72b9db commit d28d016

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/integration_rules.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
# Integration Rules
33
################################################################################
44

5+
function _kwargs_to_string(kwargs)
6+
return join([string(k) * " = " * string(v) for (k, v) in pairs(kwargs)], ", ")
7+
end
8+
59
abstract type IntegrationRule end
610

711
"""
@@ -18,6 +22,10 @@ struct GaussKronrod <: IntegrationRule
1822
GaussKronrod(; kwargs...) = new(kwargs)
1923
end
2024

25+
function Base.show(io::IO, rule::GaussKronrod)
26+
print(io, "GaussKronrod(; ", _kwargs_to_string(rule.kwargs), ")")
27+
end
28+
2129
"""
2230
GaussLegendre(n)
2331
@@ -39,6 +47,10 @@ struct GaussLegendre <: IntegrationRule
3947
GaussLegendre(n::Int64) = new(n, FastGaussQuadrature.gausslegendre(n)...)
4048
end
4149

50+
function Base.show(io::IO, rule::GaussLegendre)
51+
print(io, "GaussLegendre(", rule.n, ")")
52+
end
53+
4254
"""
4355
HAdaptiveCubature(kwargs...)
4456
@@ -50,3 +62,7 @@ struct HAdaptiveCubature <: IntegrationRule
5062
kwargs::Base.Pairs
5163
HAdaptiveCubature(; kwargs...) = new(kwargs)
5264
end
65+
66+
function Base.show(io::IO, rule::HAdaptiveCubature)
67+
print(io, "HAdaptiveCubature(; ", _kwargs_to_string(rule.kwargs), ")")
68+
end

test/utils.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
import Enzyme
88
end
99

10+
@testitem "Integration Rules" begin
11+
@test sprint(show, GaussKronrod(; atol = 1e-12, rtol = 1e-10)) ==
12+
"GaussKronrod(; atol = 1.0e-12, rtol = 1.0e-10)"
13+
14+
@test sprint(show, GaussLegendre(5)) == "GaussLegendre(5)"
15+
@test sprint(show, HAdaptiveCubature()) == "HAdaptiveCubature(; )"
16+
end
17+
1018
@testitem "Utilities" setup=[Utils] begin
1119
# _KVector
1220
v = Meshes.Vec(3, 4)

0 commit comments

Comments
 (0)