@@ -70,10 +70,10 @@ function transfinite_interpolate_2d(f_lo::Tuple{Function,Function},
7070
7171 function interpolating_function (u:: NTuple{2,T} where {T<: Number }):: Number
7272 #= linear-part from edges bilinear-part from vertices =#
73- ( 1 - u[2 ])* f_lo[1 ](u[1 ]) - (1 - u[1 ])* ( 1 - u[2 ])* v_cr[1 ,1 ] +
74- ( u[2 ])* f_hi[1 ](u[1 ]) - ( u[1 ])* ( 1 - u[2 ])* v_cr[1 ,2 ] +
75- ( 1 - u[1 ])* f_lo[2 ](u[2 ]) - (1 - u[1 ])* ( u[2 ])* v_cr[2 ,1 ] +
76- ( u[1 ])* f_hi[2 ](u[2 ]) - ( u[1 ])* ( u[2 ])* v_cr[2 ,2 ]
73+ return ( 1 - u[2 ]) * f_lo[1 ](u[1 ]) - (1 - u[1 ]) * ( 1 - u[2 ]) * v_cr[1 , 1 ] +
74+ ( u[2 ]) * f_hi[1 ](u[1 ]) - (u[1 ]) * ( 1 - u[2 ]) * v_cr[1 , 2 ] +
75+ ( 1 - u[1 ]) * f_lo[2 ](u[2 ]) - (1 - u[1 ]) * ( u[2 ]) * v_cr[2 , 1 ] +
76+ ( u[1 ]) * f_hi[2 ](u[2 ]) - (u[1 ]) * ( u[2 ]) * v_cr[2 , 2 ]
7777 end
7878 return interpolating_function
7979end
@@ -107,60 +107,76 @@ julia> transfinite_interpolate_2d(([0,0.5,1], [0,0.5,1]), ([1,1.5,2], [1,1.5,2])
107107 1.0 1.5 2.0
108108```
109109"""
110- function transfinite_interpolate_2d ( # for regular Arrays
111- v_lo:: Tuple{Vector{T}, Vector{S}} where {T <: Real , S <: Real },
112- v_hi:: Tuple{Vector{T}, Vector{S}} where {T <: Real , S <: Real },
113- v_cr:: Union{Nothing, SMatrix{2, 2, T} where T <: Real} = nothing ):: Array{Real, 2}
110+ function transfinite_interpolate_2d (v_lo:: Tuple {Vector{T},
111+ Vector{S}} where {T<: Real ,S<: Real },
112+ v_hi:: Tuple {Vector{T},
113+ Vector{S}} where {T<: Real ,S<: Real };
114+ v_cr:: Union{Nothing,SMatrix{2,2,T} where T<:Real} = nothing ):: Array {Real,
115+ 2 }
114116
115117 #= check dimension-compatibility before allocating anything =#
116- length (v_lo) == length (v_hi) || throw (
117- DimensionMismatch (" incompatible grid specification ($(length (v_lo)) -d at one end but $(length (v_hi)) -d at the other)." ))
118- N = length (v_lo); errmsg = " " #= In this function, dimensions are always indexed from 1, but not so for each axis of them =#
119- for dim in eachindex (v_lo) if ! (axes (v_lo[dim]) == axes (v_hi[dim]))
120- errmsg *= " grid mismatch in dimension $(dim) ($(axes (v_lo[dim])) at one end but $(axes (v_hi[dim])) at the other)."
121- end end ; errmsg == " " || throw (DimensionMismatch (errmsg))
118+ length (v_lo) == length (v_hi) ||
119+ throw (DimensionMismatch (" incompatible grid specification ($(length (v_lo)) -d at one end but $(length (v_hi)) -d at the other)." ))
120+ N = length (v_lo)
121+ errmsg = " " #= In this function, dimensions are always indexed from 1, but not so for each axis of them =#
122+ for dim in eachindex (v_lo)
123+ if ! (axes (v_lo[dim]) == axes (v_hi[dim]))
124+ errmsg *= " grid mismatch in dimension $(dim) ($(axes (v_lo[dim])) at one end but $(axes (v_hi[dim])) at the other)."
125+ end
126+ end
127+ errmsg == " " || throw (DimensionMismatch (errmsg))
122128
123129 interpolated_array = Array {Float64} (undef, (length (v) for v in v_lo). .. )
124130 transfinite_interpolate_2d! (interpolated_array, v_lo, v_hi, v_cr)
125131 return interpolated_array
126132end
127- function transfinite_interpolate_2d ( # for OffsetArrays
128- v_lo:: Tuple{OffsetArray, OffsetArray} ,
129- v_hi:: Tuple{OffsetArray, OffsetArray} ,
130- v_cr:: Union{Nothing, SMatrix{2, 2, T} where T <: Real} = nothing ):: OffsetArray
133+ function transfinite_interpolate_2d (v_lo:: Tuple{OffsetArray,OffsetArray} ,
134+ v_hi:: Tuple{OffsetArray,OffsetArray} ;
135+ v_cr:: Union{Nothing,SMatrix{2,2,T} where T<:Real} = nothing ):: OffsetArray
131136
132137 #= check dimension-compatibility before allocating anything =#
133- length (v_lo) == length (v_hi) || throw (
134- DimensionMismatch (" incompatible grid specification ($(length (v_lo)) -d at one end but $(length (v_hi)) -d at the other)." ))
135- N = length (v_lo); errmsg = " " #= In this function, dimensions are always indexed from 1, but not so for each axis of them =#
136- for dim in eachindex (v_lo) if ! (axes (v_lo[dim]) == axes (v_hi[dim]))
137- errmsg *= " grid mismatch in dimension $(dim) ($(axes (v_lo[dim])) at one end but $(axes (v_hi[dim])) at the other)."
138- end end ; errmsg == " " || throw (DimensionMismatch (errmsg))
138+ length (v_lo) == length (v_hi) ||
139+ throw (DimensionMismatch (" incompatible grid specification ($(length (v_lo)) -d at one end but $(length (v_hi)) -d at the other)." ))
140+ N = length (v_lo)
141+ errmsg = " " #= In this function, dimensions are always indexed from 1, but not so for each axis of them =#
142+ for dim in eachindex (v_lo)
143+ if ! (axes (v_lo[dim]) == axes (v_hi[dim]))
144+ errmsg *= " grid mismatch in dimension $(dim) ($(axes (v_lo[dim])) at one end but $(axes (v_hi[dim])) at the other)."
145+ end
146+ end
147+ errmsg == " " || throw (DimensionMismatch (errmsg))
139148
140149 interpolated_array = Array {Float64} (undef, (length (v) for v in v_lo). .. )
141- interpolated_array = OffsetArray (interpolated_array, tuplejoin ((axes (v) for v in v_lo). .. ))
150+ interpolated_array = OffsetArray (interpolated_array,
151+ tuplejoin ((axes (v) for v in v_lo). .. ))
142152 transfinite_interpolate_2d! (interpolated_array, v_lo, v_hi, v_cr)
143153 return interpolated_array
144154end
145- function transfinite_interpolate_2d! ( # for regular Arrays
146- interpolated_array :: Array{T, 2} where T <: Real ,
147- v_lo :: Tuple{Vector{T}, Vector{S}} where {T <: Real , S <: Real },
148- v_hi :: Tuple{Vector{T}, Vector{S}} where {T <: Real , S <: Real },
149- v_cr :: Union{Nothing, SMatrix{2, 2, T} where T <: Real} = nothing )
150-
155+ function transfinite_interpolate_2d! (interpolated_array :: Array{T,2} where {T <: Real },
156+ v_lo :: Tuple {Vector{T} ,
157+ Vector{S}} where {T<: Real ,S <: Real },
158+ v_hi :: Tuple {Vector{T },
159+ Vector{S}} where {T <: Real ,S <: Real },
160+ v_cr :: Union{Nothing,SMatrix{2,2,T} where T<:Real} = nothing )
151161 if typeof (v_cr) <: Nothing # e.g, the following lines still assumes range ``1:2``.
152- v_cr = SA[(v_lo[1 ][begin ]+ v_lo[2 ][begin ])/ 2 (v_lo[1 ][ end ]+ v_hi[2 ][begin ])/ 2
153- (v_hi[1 ][begin ]+ v_lo[2 ][ end ])/ 2 (v_hi[1 ][ end ]+ v_hi[2 ][ end ])/ 2 ]
154- end ; M = [length (v)- 1 for v in v_lo] # this is only for scaling, and never for iterating
162+ v_cr = SA[(v_lo[1 ][begin ] + v_lo[2 ][begin ])/ 2 (v_lo[1 ][end ] + v_hi[2 ][begin ])/ 2
163+ (v_hi[1 ][begin ] + v_lo[2 ][end ])/ 2 (v_hi[1 ][end ] + v_hi[2 ][end ])/ 2 ]
164+ end
165+ M = [length (v) - 1 for v in v_lo] # this is only for scaling, and never for iterating
155166
156167 # loop Version
157168 for u in CartesianIndices (interpolated_array) #= #= Deprecated =# Iterators.product(IndexCartesian(), [eachindex(v) for v in v_lo]...) =#
158169 interpolated_array[u] =
159170 #= linear-part from edges bilinear-part from vertices =#
160- (1 - (u[2 ]- 1 )/ M[2 ])* v_lo[1 ][u[1 ]] - (1 - (u[1 ]- 1 )/ M[1 ])* (1 - (u[2 ]- 1 )/ M[2 ])* v_cr[1 ,1 ] +
161- ( (u[2 ]- 1 )/ M[2 ])* v_hi[1 ][u[1 ]] - ( (u[1 ]- 1 )/ M[1 ])* (1 - (u[2 ]- 1 )/ M[2 ])* v_cr[1 ,2 ] +
162- (1 - (u[1 ]- 1 )/ M[1 ])* v_lo[2 ][u[2 ]] - (1 - (u[1 ]- 1 )/ M[1 ])* ( (u[2 ]- 1 )/ M[2 ])* v_cr[2 ,1 ] +
163- ( (u[1 ]- 1 )/ M[1 ])* v_hi[2 ][u[2 ]] - ( (u[1 ]- 1 )/ M[1 ])* ( (u[2 ]- 1 )/ M[2 ])* v_cr[2 ,2 ]
171+ (1 - (u[2 ] - 1 ) / M[2 ]) * v_lo[1 ][u[1 ]] -
172+ (1 - (u[1 ] - 1 ) / M[1 ]) *
173+ (1 - (u[2 ] - 1 ) / M[2 ]) *
174+ v_cr[1 , 1 ] + ((u[2 ] - 1 ) / M[2 ]) * v_hi[1 ][u[1 ]] -
175+ ((u[1 ] - 1 ) / M[1 ]) * (1 - (u[2 ] - 1 ) / M[2 ]) * v_cr[1 , 2 ] +
176+ (1 - (u[1 ] - 1 ) / M[1 ]) * v_lo[2 ][u[2 ]] -
177+ (1 - (u[1 ] - 1 ) / M[1 ]) * ((u[2 ] - 1 ) / M[2 ]) * v_cr[2 , 1 ] +
178+ ((u[1 ] - 1 ) / M[1 ]) * v_hi[2 ][u[2 ]] -
179+ ((u[1 ] - 1 ) / M[1 ]) * ((u[2 ] - 1 ) / M[2 ]) * v_cr[2 , 2 ]
164180 end
165181
166182 return interpolated_array
@@ -170,18 +186,23 @@ function transfinite_interpolate_2d!(interpolated_array::OffsetArray,
170186 v_hi:: Tuple{OffsetArray,OffsetArray} ,
171187 v_cr:: Union{Nothing,SMatrix{2,2,T} where T<:Real} = nothing )
172188 if typeof (v_cr) <: Nothing # e.g, the following lines still assumes range ``1:2``.
173- v_cr = SA[(v_lo[1 ][begin ]+ v_lo[2 ][begin ])/ 2 (v_lo[1 ][ end ]+ v_hi[2 ][begin ])/ 2
174- (v_hi[1 ][begin ]+ v_lo[2 ][ end ])/ 2 (v_hi[1 ][ end ]+ v_hi[2 ][ end ])/ 2 ]
175- end ; M = [length (v)- 1 for v in v_lo] # this is only for scaling, and never for iterating
189+ v_cr = SA[(v_lo[1 ][begin ] + v_lo[2 ][begin ])/ 2 (v_lo[1 ][end ] + v_hi[2 ][begin ])/ 2
190+ (v_hi[1 ][begin ] + v_lo[2 ][end ])/ 2 (v_hi[1 ][end ] + v_hi[2 ][end ])/ 2 ]
191+ end
192+ M = [length (v) - 1 for v in v_lo] # this is only for scaling, and never for iterating
176193
177194 # loop Version
178195 for u in CartesianIndices (interpolated_array) #= #= Deprecated =# Iterators.product(IndexCartesian(), [eachindex(v) for v in v_lo]...) =#
179196 interpolated_array[u] =
180197 #= linear-part from edges bilinear-part from vertices =#
181- (1 - u[2 ]/ M[2 ])* v_lo[1 ][u[1 ]] - (1 - u[1 ]/ M[1 ])* (1 - u[2 ]/ M[2 ])* v_cr[1 ,1 ] +
182- ( u[2 ]/ M[2 ])* v_hi[1 ][u[1 ]] - ( u[1 ]/ M[1 ])* (1 - u[2 ]/ M[2 ])* v_cr[1 ,2 ] +
183- (1 - u[1 ]/ M[1 ])* v_lo[2 ][u[2 ]] - (1 - u[1 ]/ M[1 ])* ( u[2 ]/ M[2 ])* v_cr[2 ,1 ] +
184- ( u[1 ]/ M[1 ])* v_hi[2 ][u[2 ]] - ( u[1 ]/ M[1 ])* ( u[2 ]/ M[2 ])* v_cr[2 ,2 ]
198+ (1 - u[2 ] / M[2 ]) * v_lo[1 ][u[1 ]] -
199+ (1 - u[1 ] / M[1 ]) * (1 - u[2 ] / M[2 ]) * v_cr[1 , 1 ] +
200+ (u[2 ] / M[2 ]) * v_hi[1 ][u[1 ]] -
201+ (u[1 ] / M[1 ]) * (1 - u[2 ] / M[2 ]) * v_cr[1 , 2 ] +
202+ (1 - u[1 ] / M[1 ]) * v_lo[2 ][u[2 ]] -
203+ (1 - u[1 ] / M[1 ]) * (u[2 ] / M[2 ]) * v_cr[2 , 1 ] +
204+ (u[1 ] / M[1 ]) * v_hi[2 ][u[2 ]] -
205+ (u[1 ] / M[1 ]) * (u[2 ] / M[2 ]) * v_cr[2 , 2 ]
185206 end
186207 return interpolated_array
187208end
0 commit comments