Skip to content

Commit d0d3e87

Browse files
committed
improve interoperability with arrays, cleanups
1 parent 3593559 commit d0d3e87

10 files changed

Lines changed: 42 additions & 65 deletions

File tree

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "GridArrays"
22
uuid = "356ae075-8bc9-5ef9-a342-46c9ba26438c"
3-
version = "0.1.16"
3+
version = "0.2.0"
44

55
[deps]
66
CompositeTypes = "b152e2b5-7a66-4b01-a709-34e65c35f657"
@@ -14,7 +14,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1414

1515
[compat]
1616
CompositeTypes = "0.1.3"
17-
DomainSets = "0.5.14,0.6"
17+
DomainSets = "0.6.2"
1818
FastGaussQuadrature = "0.4,0.5"
1919
FillArrays = "0.13"
2020
GaussQuadrature = "0.5.7"

src/GridArrays.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ using CompositeTypes, CompositeTypes.Display
77

88
using DomainSets: endpoints
99

10-
@deprecate AbstractSubGrid SubGrid
11-
@deprecate MaskedSubGrid MaskedGrid
12-
@deprecate mapping forward_map
13-
@deprecate mapped_grid map_grid
14-
1510
## List of imports
1611

1712
import Base:
@@ -32,7 +27,7 @@ import CompositeTypes:
3227
iscomposite, component, components, ncomponents
3328

3429
import DomainSets:
35-
factors, factor, nfactors,
30+
factors,
3631
×, cross,
3732
dimension, prectype, numtype,
3833
minimum, maximum,
@@ -61,7 +56,7 @@ export ProductGrid, productgrid,
6156
export subindices, supergrid, issubindex, similar_subgrid
6257

6358
# from generic/mapped.jl
64-
export MappedGrid, map_grid, apply_map
59+
export MappedGrid, map_grid
6560

6661
# from subgrid/subgrid.jl
6762
export subgrid, mask, ProductSubGrid

src/applications/gauss.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,29 +195,19 @@ gaussjacobi(n::Int, α::T, β::T) where T = gaussjacobi(T, n, α, β)
195195

196196

197197
covering(::ChebyshevTNodes{T}) where T = ChebyshevInterval{T}()
198-
name(g::ChebyshevTNodes) = "ChebyshevT nodes"
199-
200-
name(g::ChebyshevExtremae) = "Chebyshev extremae"
201198
covering(::ChebyshevExtremae{T}) where T = ChebyshevInterval{T}()
202-
203-
name(g::ChebyshevUNodes) = "ChebyshevU nodes"
204199
covering(::ChebyshevUNodes{T}) where T = ChebyshevInterval{T}()
205-
206-
name(g::LegendreNodes) = "Legendre nodes"
207200
covering(::LegendreNodes{T}) where T = ChebyshevInterval{T}()
208201
LegendreNodes{T}(n::Int) where T = gausslegendre(T, n)[1]
209202

210-
name(g::LaguerreNodes) = "Laguerre nodes α=$(g.α)"
211203
covering(::LaguerreNodes{T}) where T = HalfLine{T}()
212204
similargrid(grid::LaguerreNodes, T, n::Int) = LaguerreNodes{T}(n, T(grid.α))
213205
LaguerreNodes(n::Int, α::T) where T = gausslaguerre(T, n, α)[1]
214206
LaguerreNodes{T}(n::Int, α::T) where T = gausslaguerre(T, n, α)[1]
215207

216-
name(g::HermiteNodes) = "Hermite nodes"
217208
covering(::HermiteNodes{T}) where T = DomainSets.FullSpace{T}()
218209
HermiteNodes{T}(n::Int) where T = gausshermite(T, n)[1]
219210

220-
name(g::JacobiNodes) = "Jacobi nodes α=$(g.α), β=$(g.β)"
221211
covering(::JacobiNodes{T}) where T = ChebyshevInterval{T}()
222212
similargrid(grid::JacobiNodes, T, n::Int) = JacobiNodes{T}(n, T(grid.α), T(grid.β))
223213
JacobiNodes{T}(n::Int, α::T, β::T) where T = gaussjacobi(T, n, α, β)[1]

src/generic/grid.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ Grids are arrays of points.
77
abstract type AbstractGrid{T,N} <: AbstractArray{T,N}
88
end
99

10-
const AbstractGrid1d{T <: Real} = AbstractGrid{T,1}
10+
const AbstractGridVector{T} = AbstractGrid{T,1}
11+
const AbstractGrid1d{T <: Number} = AbstractGrid{T,1}
12+
13+
const GridLike{T,N} = Union{AbstractGrid{T,N},AbstractArray{T,N}}
14+
const Grid1dLike{T<:Number,N} = Union{AbstractGridVector{T},AbstractVector{T}}
15+
1116

1217
prectype(::Type{G}) where {G<:AbstractGrid} = prectype(eltype(G))
1318
numtype(::Type{G}) where {G<:AbstractGrid} = numtype(eltype(G))
@@ -25,8 +30,6 @@ end
2530
unsafe_grid_getindex(grid, i...)
2631
end
2732

28-
@deprecate support(grid::AbstractGrid) covering(grid) false
29-
3033
convert(::Type{AbstractGrid{T}}, grid::AbstractGrid{T,N}) where {T,N} = grid
3134
convert(::Type{AbstractGrid{T}}, grid::AbstractGrid{S,N}) where {S,T,N} = similargrid(grid, S, size(grid))
3235

src/generic/mapped.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,14 @@ end
2424

2525
()(g::AbstractGrid1d, d::AbstractInterval) = rescale(g, infimum(d), supremum(d))
2626

27-
map_grid(grid::AbstractGrid, map) = (@warn "Interchange arguments of map_grid here"; map_grid(map, grid))
28-
map_grid(map, grid::AbstractGrid) = map_grid1(map, grid)
29-
map_grid1(map, grid::AbstractGrid) = map_grid2(map, grid)
27+
map_grid(map, grid::GridLike) = map_grid1(map, grid)
28+
map_grid1(map, grid) = map_grid2(map, grid)
3029
map_grid2(map, grid) = MappedGrid(map, grid)
3130

3231
# some simplifications
3332
map_grid1(map, g::AbstractMappedGrid) = map_grid(mapforward_map(g), supergrid(g))
3433
map_grid2(map::IdentityMap, grid) = grid
3534

36-
# Convenience function, similar to apply_map for Dictionary's
37-
apply_map(grid::AbstractGrid, map::AbstractMap) = map_grid(map, grid)
38-
3935
forward_map(g::AbstractGrid, x...) = forward_map(g)(x...)
4036
inverse_map(g::AbstractGrid, x...) = inverse_map(g)(x...)
4137

src/generic/product.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ size(g::ProductGrid) = map(length, g.grids)
77
size(g::ProductGrid, j::Int) = length(g.grids[j])
88

99
ProductGrid(grids...) = VcatGrid(grids...)
10-
ProductGrid(grids::AbstractGrid1d...) = FlatProductGrid(grids...)
10+
ProductGrid(grids::Grid1dLike...) = FlatProductGrid(grids...)
1111

1212
ProductGrid{T}(grids...) where {N,S,T <: SVector{N,S}} = VcatGrid{N,S}(grids...)
13-
ProductGrid{T}(grids::AbstractGrid1d...) where {N,S,T <: SVector{N,S}} =
13+
ProductGrid{T}(grids::Grid1dLike...) where {N,S,T <: SVector{N,S}} =
1414
FlatProductGrid{N,S}(grids...)
1515

1616
similargrid(grid::ProductGrid, ::Type{T}, dims...) where T = error()#ProductGrid([similargrid(g, eltype(T), dims[i]) for (i,g) in enumerate(components(grid))]...)
@@ -46,19 +46,21 @@ canonicalgrid(g::ProductGrid) = ProductGrid(map(canonicalgrid, components(g)))
4646
mapto_canonical(g::ProductGrid) = ProductMap(map(mapto_canonical, components(g)))
4747
mapfrom_canonical(g::ProductGrid) = ProductMap(map(mapfrom_canonical, components(g)))
4848

49+
# Pretty printing
4950
Display.combinationsymbol(d::ProductGrid) = Display.Times()
5051
Display.displaystencil(d::ProductGrid) = composite_displaystencil(d)
5152
Base.show(io::IO, mime::MIME"text/plain", d::ProductGrid) = composite_show(io, mime, d)
5253
Base.show(io::IO, d::ProductGrid) = composite_show_compact(io, d)
5354

55+
5456
"A `FlatProductGrid` is a product grid of `N` 1-D grids."
5557
struct FlatProductGrid{N,T,GG} <: ProductGrid{SVector{N,T},N}
5658
grids :: GG
5759
end
5860

59-
FlatProductGrid(grids::Vararg{AbstractGrid,N}) where {N} =
61+
FlatProductGrid(grids::Vararg{Any,N}) where {N} =
6062
FlatProductGrid{N}(grids...)
61-
function FlatProductGrid{N}(grids::Vararg{AbstractGrid,N}) where {N}
63+
function FlatProductGrid{N}(grids::Vararg{Any,N}) where {N}
6264
T = mapreduce(numtype, promote_type, grids)
6365
FlatProductGrid{N,T}(grids...)
6466
end

src/subgrid/indexed.jl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@ struct IndexSubGrid{G,I,T,N} <: SubGrid{T,N}
1010
subindices :: I
1111
domain :: Domain
1212

13-
function IndexSubGrid{G,I,T,N}(supergrid::AbstractGrid{T,N}, subindices, domain=Interval(first(supergrid), last(supergrid))) where {G,I,T,N}
13+
function IndexSubGrid{G,I,T,N}(supergrid::AbstractArray{T,N}, subindices, domain=Interval(first(supergrid), last(supergrid))) where {G,I,T,N}
1414
@assert length(subindices) <= length(supergrid)
1515
new(supergrid, subindices, domain)
1616
end
1717
end
1818

19-
IndexSubGrid(grid::AbstractGrid{T,N}, I, domain=Interval(first(grid), last(grid))) where {T,N} =
19+
IndexSubGrid(grid::AbstractArray{T,N}, I, domain=Interval(first(grid), last(grid))) where {T,N} =
2020
IndexSubGrid{typeof(grid),typeof(I),T,N}(grid, I, domain)
2121

22-
name(g::IndexSubGrid) = "Index-based subgrid"
23-
2422
subindices(g::IndexSubGrid) = g.subindices
2523

26-
similar_subgrid(g::IndexSubGrid, g2::AbstractGrid) = IndexSubGrid(g2, subindices(g))
24+
similar_subgrid(g::IndexSubGrid, g2::AbstractArray) = IndexSubGrid(g2, subindices(g))
2725

2826
length(g::IndexSubGrid) = length(subindices(g))
2927

@@ -46,11 +44,7 @@ end
4644

4745
covering(g::IndexSubGrid) = g.domain
4846

49-
50-
5147
# Check whether element grid[i] (of the underlying grid) is in the indexed subgrid.
5248
issubindex(i, g::IndexSubGrid) = in(i, subindices(g))
5349

54-
# getindex(grid::AbstractGrid, i::Range) = IndexSubGrid(grid, i)
55-
5650
getindex(grid::AbstractGrid, i::AbstractArray{Int}) = IndexSubGrid(grid, i)

src/subgrid/masked.jl

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,44 @@
44

55

66
"""
7-
struct MaskedGrid{G,M,I,T} <: SubGrid{T,1}
7+
struct MaskedGrid{T,GRID,MASK,I,D} <: SubGrid{T,1}
88
99
A MaskedGrid is a subgrid of another grid that is defined by a mask.
1010
The mask is true or false for each point in the supergrid. The set of points
1111
for which it is true make up the MaskedGrid.
1212
"""
13-
struct MaskedGrid{G,M,I,D<:Domain,T} <: SubGrid{T,1}
14-
supergrid :: G
15-
mask :: M
13+
struct MaskedGrid{T,GRID,MASK,I,D} <: SubGrid{T,1}
14+
supergrid :: GRID
15+
mask :: MASK
1616
indices :: Vector{I}
1717
M :: Int # Total number of points in the mask
1818
domain :: D
1919

20-
MaskedGrid{G,M,I,D,T}(supergrid::AbstractGrid{T}, mask, indices, domain) where {G,M,I,D,T} =
20+
MaskedGrid{T,GRID,MASK,I,D}(supergrid::AbstractArray{T}, mask, indices, domain) where {T,GRID,MASK,I,D} =
2121
new(supergrid, mask, indices, sum(mask), domain)
2222
end
2323
# TODO: In MaskedGrid, perhaps we should not be storing pointers to the points of the underlying grid, but
2424
# rather the points themselves. In that case we wouldn't need to specialize on the type of grid (parameter G can go).
2525

2626

27+
MaskedGrid(supergrid::AbstractArray{T}, mask, indices, domain) where T =
28+
MaskedGrid{T}(supergrid, mask, indices, domain)
2729

28-
function MaskedGrid(supergrid::AbstractGrid{T}, mask, indices, domain) where {T}
30+
function MaskedGrid{T}(supergrid, mask, indices, domain) where T
2931
@assert size(supergrid) == size(mask)
30-
31-
MaskedGrid{typeof(supergrid),typeof(mask),eltype(indices),typeof(domain),T}(supergrid, mask, indices, domain)
32+
MaskedGrid{T,typeof(supergrid),typeof(mask),eltype(indices),typeof(domain)}(supergrid, mask, indices, domain)
3233
end
3334

3435
# These are for the assignment to indices in the function below.
3536
convert(::Type{NTuple{N,Int}},i::CartesianIndex{N}) where {N} = ntuple(k->i[k],N)
3637

37-
MaskedGrid(supergrid::AbstractGrid, domain::Domain) =
38+
MaskedGrid(supergrid::AbstractArray, domain::Domain) =
3839
MaskedGrid(supergrid, in.(supergrid, Ref(domain)), domain)
3940

4041
# MaskedGrid(maskedgrid::MaskedGrid, domain::Domain) =
4142
# MaskedGrid(supergrid(maskedgrid), mask(maskedgrid) .& in.(supergrid(maskedgrid), domain))
4243

43-
MaskedGrid(supergrid::AbstractGrid, mask, domain) =
44+
MaskedGrid(supergrid::AbstractArray, mask, domain) =
4445
MaskedGrid(supergrid, mask, subindices(supergrid, mask), domain)
4546

4647
function subindices(supergrid, mask::BitArray)
@@ -64,7 +65,7 @@ covering(g::MaskedGrid) = g.domain
6465

6566
subindices(g::MaskedGrid) = g.indices
6667

67-
similar_subgrid(g::MaskedGrid, g2::AbstractGrid) = MaskedGrid(g2, g.mask, g.indices)
68+
similar_subgrid(g::MaskedGrid, g2::AbstractArray) = MaskedGrid(g2, g.mask, g.indices)
6869

6970

7071
# Check whether element grid[i] (of the underlying grid) is in the masked grid.
@@ -77,6 +78,4 @@ getindex(g::AbstractGrid, idx::BitArray) = MaskedGrid(g, idx, covering(g))
7778
function subgrid(grid::MaskedGrid, domain::Domain)
7879
submask = in.(supergrid(grid), Ref(domain))
7980
MaskedGrid(supergrid(grid), submask .& mask(grid), domain)
80-
# points = grid.points[mask]
81-
# ScatteredGrid(points)
8281
end

src/subgrid/product.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const ProductSubGrid = FlatProductGrid{N,T,<:NTuple{N,GRID}} where {N,T} where {
88
# R
99
# end
1010

11-
function tensorproductbitarray(vectors::Vararg{<:Union{BitVector,Vector{Bool}}, N}) where N
11+
function tensorproductbitarray(vectors::Vararg{Union{BitVector,Vector{Bool}}, N}) where N
1212
R = falses(map(length, vectors))
1313
@inbounds for i in CartesianIndices(size(R))
1414
t = true

src/subgrid/subgrid.jl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

22
"""
3-
abstract type SubGrid{T,N} <: AbstractGrid{T,N} end
4-
5-
A subgrid of an underlying grid.
3+
Supertype of lazy subgrids of a grid.
64
"""
75
abstract type SubGrid{T,N} <: SimpleLazyGrid{T,N} end
86

@@ -14,7 +12,7 @@ include("product.jl")
1412
include("boundary.jl")
1513

1614

17-
subgrid(grid::AbstractGrid, domain::Domain) = MaskedGrid(grid, domain)
15+
subgrid(grid::AbstractArray, domain::Domain) = MaskedGrid(grid, domain)
1816

1917
function subgrid(grid::AbstractEquispacedGrid, domain::AbstractInterval)
2018
a = infimum(domain)
@@ -27,12 +25,12 @@ function subgrid(grid::AbstractEquispacedGrid, domain::AbstractInterval)
2725
IndexSubGrid(grid, idx_a:idx_b, domain)
2826
end
2927

30-
function subgrid(grid::ScatteredGrid, domain::Domain)
31-
mask = in.(grid, Ref(domain))
32-
points = grid.points[mask]
33-
ScatteredGrid(points, domain)
34-
end
35-
28+
# function subgrid(grid::ScatteredGrid, domain::Domain)
29+
# mask = in.(grid, Ref(domain))
30+
# points = grid.points[mask]
31+
# ScatteredGrid(points, domain)
32+
# end
33+
#
3634
function subgrid(grid::ProductGrid, domain::ProductDomain)
3735
if ncomponents(grid) == ncomponents(domain)
3836
productgrid(map(subgrid, components(grid), components(domain))...)

0 commit comments

Comments
 (0)