Skip to content
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
17 changes: 13 additions & 4 deletions src/grid_discretized.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ struct DiscretizedGrid{D} <: Grid{D}
function DiscretizedGrid{D}(
Rs, lower_bound, upper_bound, variablenames, base, indextable, includeendpoint
) where {D}
lower_bound = _to_tuple(Val(D), lower_bound)
upper_bound = _to_tuple(Val(D), upper_bound)
lower_bound = _to_float_tuple(Val(D), lower_bound)
upper_bound = _to_float_tuple(Val(D), upper_bound)
base = _to_tuple(Val(D), base)
includeendpoint = _to_tuple(Val(D), includeendpoint)
for d in 1:D
Expand All @@ -119,14 +119,23 @@ end
# ============================================================================

function _check_bounds_dim(::Val{D}, lower_bound, upper_bound) where {D}
if lower_bound isa NTuple && length(lower_bound) != D
if lower_bound isa Tuple && length(lower_bound) != D
throw(ArgumentError(lazy"Got lower_bound with length $(length(lower_bound)); expected $D for DiscretizedGrid{$D}."))
end
if upper_bound isa NTuple && length(upper_bound) != D
if upper_bound isa Tuple && length(upper_bound) != D
throw(ArgumentError(lazy"Got upper_bound with length $(length(upper_bound)); expected $D for DiscretizedGrid{$D}."))
end
end

function _to_float_tuple(::Val{D}, x::Tuple) where {D}
if length(x) != D
throw(ArgumentError(lazy"Got tuple with length $(length(x)); expected $D."))
end
return ntuple(d -> Float64(x[d]), D)
end

_to_float_tuple(::Val{D}, x) where {D} = ntuple(_ -> Float64(x), D)

function _adjust_upper_bounds(upper_bound, lower_bound, includeendpoint, base, Rs, ::Val{D}) where D
base = _to_tuple(Val(D), base)
includeendpoint = _to_tuple(Val(D), includeendpoint)
Expand Down
9 changes: 9 additions & 0 deletions test/discretizedgrid_misc_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ end
@test_throws ArgumentError DiscretizedGrid{2}(3, (0.0, 1.0), (1.0,))
end

@testitem "DiscretizedGrid mixed-type bounds" begin
R = 10
N = 2^R
g = DiscretizedGrid{2}((R, R), (0.0, 0), (2.0, N - 1); includeendpoint=(false, true))

@test QuanticsGrids.lower_bound(g) == (0.0, 0.0)
@test QuanticsGrids.grid_max(g)[2] ≈ N - 1
end

@testitem "DiscretizedGrid 0-dimensional show method" begin
g = DiscretizedGrid(())
@test try
Expand Down
Loading