1
1
export Gridded
2
2
3
- struct Gridded{D<: Degree } <: InterpolationType end
4
- Gridded (:: D ) where {D<: Degree } = Gridded {D} ()
5
-
6
- griddedtype (:: Type{Gridded{D}} ) where {D<: Degree } = D
3
+ struct Gridded{D<: Degree } <: InterpolationType
4
+ degree:: D
5
+ end
7
6
8
7
const GridIndex{T} = Union{AbstractVector{T}, Tuple}
9
8
10
- # Because Ranges check bounds on getindex, it's actually faster to convert the
11
- # knots to Vectors. It's also good to take a copy, so it doesn't get modified later.
12
- struct GriddedInterpolation{T,N,TCoefs,IT<: DimSpec{Gridded} ,K<: Tuple{Vararg{AbstractVector}} } <: AbstractInterpolation{T,N,IT,OnGrid}
9
+ struct GriddedInterpolation{T,N,TCoefs,IT<: DimSpec{Gridded} ,K<: Tuple{Vararg{AbstractVector}} } <: AbstractInterpolation{T,N,IT}
13
10
knots:: K
14
11
coefs:: Array{TCoefs,N}
12
+ it:: IT
15
13
end
16
14
function GriddedInterpolation (:: Type{TWeights} , knots:: NTuple{N,GridIndex} , A:: AbstractArray{TCoefs,N} , it:: IT ) where {N,TCoefs,TWeights<: Real ,IT<: DimSpec{Gridded} ,pad}
17
15
isconcretetype (IT) || error (" The b-spline type must be a leaf type (was $IT )" )
18
16
isconcretetype (TCoefs) || warn (" For performance reasons, consider using an array of a concrete type (eltype(A) == $(eltype (A)) )" )
19
17
20
- knts = mapcollect (knots... )
21
- for (d,k) in enumerate (knts)
22
- length (k) == size (A, d) || throw (DimensionMismatch (" knot vectors must have the same number of elements as the corresponding dimension of the array" ))
23
- length (k) == 1 && error (" dimensions of length 1 not yet supported" ) # FIXME
24
- issorted (k) || error (" knot-vectors must be sorted in increasing order" )
25
- iextract (IT, d) != NoInterp || k == collect (1 : size (A, d)) || error (" knot-vector should be the range 1:$(size (A,d)) for the method Gridded{NoInterp}" )
26
- end
18
+ check_gridded (it, knots, axes (A))
27
19
c = zero (TWeights)
28
- for _ in 2 : N
29
- c *= c
30
- end
31
20
if isempty (A)
32
21
T = Base. promote_op (* , typeof (c), eltype (A))
33
22
else
@@ -36,39 +25,44 @@ function GriddedInterpolation(::Type{TWeights}, knots::NTuple{N,GridIndex}, A::A
36
25
GriddedInterpolation {T,N,TCoefs,IT,typeof(knots)} (knots, A, it)
37
26
end
38
27
39
- Base. parent (A:: GriddedInterpolation ) = A. coefs
28
+ @inline function check_gridded (itpflag, knots, axs)
29
+ flag, ax1, k1 = getfirst (itpflag), axs[1 ], knots[1 ]
30
+ if flag isa NoInterp
31
+ k1 == ax1 || error (" for NoInterp knot vector should be $ax1 , got $k1 " )
32
+ else
33
+ axes (k1, 1 ) == ax1 || throw (DimensionMismatch (" knot vectors must have the same axes as the corresponding dimension of the array" ))
34
+ end
35
+ degree (flag) isa Union{NoInterp,Constant,Linear} || error (" only Linear, Constant, and NoInterp supported, got $flag " )
36
+ length (k1) == 1 && error (" dimensions of length 1 not yet supported" ) # FIXME
37
+ issorted (k1) || error (" knot-vectors must be sorted in increasing order" )
38
+ check_gridded (getrest (itpflag), Base. tail (knots), Base. tail (axs))
39
+ end
40
+ check_gridded (:: Any , :: Tuple{} , :: Tuple{} ) = nothing
41
+ degree (flag:: Gridded ) = flag. degree
40
42
41
- # A type-stable version of map(collect, knots)
42
- mapcollect () = ()
43
- @inline mapcollect (k:: AbstractVector ) = (collect (k),)
44
- @inline mapcollect (k1:: AbstractVector , k2:: AbstractVector... ) = (collect (k1), mapcollect (k2... )... )
43
+ Base. parent (A:: GriddedInterpolation ) = A. coefs
44
+ coefficients (A:: GriddedInterpolation ) = A. coefs
45
45
46
- # Utilities for working either with scalars or tuples/tuple-types
47
- iextract (:: Type{T} , d) where {T<: Gridded } = T
48
- iextract (:: Type{T} , d) where {T<: GridType } = T
46
+ size (A:: GriddedInterpolation ) = size (A. coefs)
47
+ axes (A:: GriddedInterpolation ) = axes (A. coefs)
49
48
50
- @generated function size (itp:: GriddedInterpolation{T,N,TCoefs,IT,K,pad} , d) where {T,N,TCoefs,IT,K,pad}
51
- quote
52
- d <= $ N ? size (itp. coefs, d) - 2 * padextract ($ pad, d) : 1
53
- end
54
- end
49
+ itpflag (A:: GriddedInterpolation ) = A. it
55
50
56
51
function interpolate (:: Type{TWeights} , :: Type{TCoefs} , knots:: NTuple{N,GridIndex} , A:: AbstractArray{Tel,N} , it:: IT ) where {TWeights,TCoefs,Tel,N,IT<: DimSpec{Gridded} }
57
- GriddedInterpolation (TWeights, knots, A, it, Val {0} () )
52
+ GriddedInterpolation (TWeights, knots, A, it)
58
53
end
59
54
function interpolate (knots:: NTuple{N,GridIndex} , A:: AbstractArray{Tel,N} , it:: IT ) where {Tel,N,IT<: DimSpec{Gridded} }
60
55
interpolate (tweight (A), tcoef (A), knots, A, it)
61
56
end
62
57
63
- interpolate! (:: Type{TWeights} , knots:: NTuple{N,GridIndex} , A:: AbstractArray{Tel,N} , it:: IT ) where {TWeights,Tel,N,IT<: DimSpec{Gridded} } = GriddedInterpolation (TWeights, knots, A, it, Val {0} ())
58
+ interpolate! (:: Type{TWeights} , knots:: NTuple{N,GridIndex} , A:: AbstractArray{Tel,N} , it:: IT ) where {TWeights,Tel,N,IT<: DimSpec{Gridded} } =
59
+ GriddedInterpolation (TWeights, knots, A, it)
64
60
function interpolate! (knots:: NTuple{N,GridIndex} , A:: AbstractArray{Tel,N} , it:: IT ) where {Tel,N,IT<: DimSpec{Gridded} }
65
61
interpolate! (tweight (A), tcoef (A), knots, A, it)
66
62
end
67
63
68
- lbound (itp:: GriddedInterpolation , d) = itp. knots[d][1 ]
69
- ubound (itp:: GriddedInterpolation , d) = itp. knots[d][end ]
70
- lbound (itp:: GriddedInterpolation , d, inds) = itp. knots[d][1 ]
71
- ubound (itp:: GriddedInterpolation , d, inds) = itp. knots[d][end ]
64
+ lbounds (itp:: GriddedInterpolation ) = first .(itp. knots)
65
+ ubounds (itp:: GriddedInterpolation ) = last .(itp. knots)
72
66
73
67
include (" constant.jl" )
74
68
include (" linear.jl" )
0 commit comments