Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite Base.show for grids #33

Merged
merged 1 commit into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/grid/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ struct Composite{T<:AbstractFloat,PG,SG} <: SimpleG.ClosedGrid{T}

end

function Base.show(io::IO, grid::Composite; isSimplified=false)
if isSimplified
print(io,
"$(typeof(grid)): 1D Composite Grid with $(grid.size) grid points.\n"
)
else
print(io,
"$(typeof(grid)): 1D Log Grid\n"
* "- bound: $(grid.bound)\n"
* "- size: $(grid.size)\n"
* "- grid: $(grid.grid)\n"
* "- panel: $(typeof(grid.panel))\n"
)
end
end

# function that returns the bottom type of the grid
getbottomtype(grid::CompositeG.Composite{T,PG,SG}) where {T,PG,SG} = (SG <: CompositeG.Composite) ? (getbottomtype(grid.subgrids[1])) : (SG)

Expand Down
37 changes: 36 additions & 1 deletion src/grid/simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ Base.length(grid::AbstractGrid) = grid.size
Base.size(grid::AbstractGrid) = (grid.size,)
Base.size(grid::AbstractGrid, I::Int) = grid.size

Base.show(io::IO, grid::AbstractGrid) = print(io, grid.grid)
Base.view(grid::AbstractGrid, inds...) where {N} = Base.view(grid.grid, inds...)
# set is not allowed for grids
Base.getindex(grid::AbstractGrid, i) = grid.grid[i]
Expand All @@ -113,6 +112,25 @@ Base.IteratorSize(::Type{AbstractGrid{T}}) where {T} = Base.HasLength()
Base.IteratorEltype(::Type{AbstractGrid{T}}) where {T} = Base.HasEltype()
Base.eltype(::Type{AbstractGrid{T}}) where {T} = eltype(T)

"""
show(io::IO, grid::AbstractGrid)

Write a text representation of the AbstractGrid
`grid` to the output stream `io`.
"""
function Base.show(io::IO, grid::AbstractGrid; isSimplified=false)
if isSimplified
print(io, "$(typeof(grid)): 1D Grid with $(grid.size) grid points.\n")
else
print(io,
"$(typeof(grid)): 1D Grid with:\n"
* "- bound: $(grid.bound)\n"
* "- size: $(grid.size)\n"
* "- grid: $(grid.grid)\n"
)
end
end

"""
struct Uniform{T<:AbstractFloat} <: ClosedGrid

Expand Down Expand Up @@ -327,6 +345,23 @@ struct Log{T<:AbstractFloat} <: ClosedGrid{T}
end
end

function Base.show(io::IO, grid::Log; isSimplified=false)
if isSimplified
print(io,
"$(typeof(grid)): 1D " * (grid.d2s ? "dense to sparse" : "sparse to dense")
* " Log Grid with $(grid.size) grid points.\n"
)
else
print(io,
"$(typeof(grid)): 1D " * (grid.d2s ? "dense to sparse" : "sparse to dense")
* " Log Grid\n"
* "- bound: $(grid.bound)\n"
* "- size: $(grid.size)\n"
* "- grid: $(grid.grid)\n"
)
end
end


"""
function Base.floor(grid::Log{T}, x) where {T}
Expand Down
32 changes: 16 additions & 16 deletions test/chebyshev.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
@testset "BaryCheb" begin
println("Testing BaryCheb")
# println("Testing BaryCheb")

@testset "1D BaryCheb Tools" begin
n = 4
x, w = BaryChebTools.barychebinit(n)
println(x)
println(w)
# println(x)
# println(w)

f(t) = t
F(t) = 0.5*t^2
F(t) = 0.5 * t^2

data = f.(x)

Expand All @@ -17,14 +17,14 @@

# test integrate
vmat = BaryChebTools.vandermonde(x)
println("vandermonde:",vmat)
# println("vandermonde:",vmat)
invmat = inv(transpose(vmat))
println("invmat:",invmat)
# println("invmat:",invmat)
x1, x2 = -0.4, 0.0
b = BaryChebTools.weightcoef(x2, 1, n) - BaryChebTools.weightcoef(x1, 1, n)
println("b:",b)
# println("b:",b)
intw = BaryChebTools.calcweight(invmat, b)
println("intw:",intw)
# println("intw:",intw)
@test sum(intw .* data) ≈ F(x2) - F(x1)
@test BaryChebTools.chebint(n, x1, x2, data, invmat) ≈ F(x2) - F(x1)
end
Expand All @@ -34,7 +34,7 @@
bc = BaryChebTools.BaryCheb1D(n)

f(t) = t
F(t) = 0.5*t^2
F(t) = 0.5 * t^2

data = f.(bc.x)

Expand Down Expand Up @@ -62,9 +62,9 @@
end
end

@test isapprox(BaryChebTools.barychebND(n, [0.5, 0.5], data, w, x, DIM), f(0.5, 0.5), rtol = 1e-10)
@test isapprox(BaryChebTools.barychebND(n, [x[2], 0.5], data, w, x, DIM), f(x[2], 0.5), rtol = 1e-10)
@test isapprox(BaryChebTools.barychebND(n, [x[2], x[1]], data, w, x, DIM), f(x[2], x[1]), rtol = 1e-10)
@test isapprox(BaryChebTools.barychebND(n, [0.5, 0.5], data, w, x, DIM), f(0.5, 0.5), rtol=1e-10)
@test isapprox(BaryChebTools.barychebND(n, [x[2], 0.5], data, w, x, DIM), f(x[2], 0.5), rtol=1e-10)
@test isapprox(BaryChebTools.barychebND(n, [x[2], x[1]], data, w, x, DIM), f(x[2], x[1]), rtol=1e-10)
end

@testset "ND BaryCheb" begin
Expand All @@ -84,13 +84,13 @@
end
end

@test isapprox(BaryChebTools.interpND(data, bc, [0.4, 0.7]), f(0.4, 0.7), rtol = 1e-10)
@test isapprox(BaryChebTools.interpND(data, bc, [bc.x[2], 0.5]), f(bc.x[2], 0.5), rtol = 1e-10)
@test isapprox(BaryChebTools.interpND(data, bc, [bc.x[2], bc.x[1]]), f(bc.x[2], bc.x[1]), rtol = 1e-10)
@test isapprox(BaryChebTools.interpND(data, bc, [0.4, 0.7]), f(0.4, 0.7), rtol=1e-10)
@test isapprox(BaryChebTools.interpND(data, bc, [bc.x[2], 0.5]), f(bc.x[2], 0.5), rtol=1e-10)
@test isapprox(BaryChebTools.interpND(data, bc, [bc.x[2], bc.x[1]]), f(bc.x[2], bc.x[1]), rtol=1e-10)

x1s = [0.0, 0.0]
x2s = [0.4, 0.7]
@test isapprox(BaryChebTools.integrateND(data, bc, x1s, x2s), F(x2s...), rtol = 1e-6)
@test isapprox(BaryChebTools.integrateND(data, bc, x1s, x2s), F(x2s...), rtol=1e-6)
end

end
Loading