timholy / Grid.jl

Interpolation and related operations on grids
MIT License
47 stars 26 forks source link

Improve inference for InterpGrid & InterpIrregular #34

Closed mbauman closed 10 years ago

mbauman commented 10 years ago

For some reason, Julia isn't statically determining that ndims(A) comes directly from A::Array{T}. As such, the result of @code_typed InterpGrid([1.,2.,3.], BCnan, InterpLinear) was ::InterpGrid{Float64,N,BCnan,InterpLinear}. Once indexed, it lost all inference and became ::Any.

The fix is simple: just grab N from the signature and use it instead of ndims(A).

coveralls commented 10 years ago

Coverage Status

Coverage remained the same when pulling e395886e33dd480beb08220e29207b14a1b52e54 on mbauman:grid-inference into de31cfd89517e5809b66c0085f17386ffbda66b4 on timholy:master.

coveralls commented 10 years ago

Coverage Status

Coverage increased (+0.01%) when pulling d4a2c340a0e18e93656fe2711db551151d08df06 on mbauman:grid-inference into de31cfd89517e5809b66c0085f17386ffbda66b4 on timholy:master.

mbauman commented 10 years ago

Whoops, I didn't really intend to push that second commit until this one was merged. But it's good to go, as well. May as well keep them together now.

It adds an extra parameter to InterpIrregular so the data it's interpolating can be of a different type than the grid:

julia> InterpIrregular([1.,2.,3.], big([2.,3.,4.]), 0., InterpLinear)[1.5]
2.5e+00 with 256 bits of precision

julia> InterpIrregular([1.,2.,3.], big([2.,3.,4.]), 0., InterpLinear)[1.5:2.5]
2-element Array{BigFloat,1}:
 2.5e+00
 3.5e+00

They're somewhat related, but I can split them apart if you'd prefer.

(There's also a third part… but it's bit bigger and may be a bit more disruptive. I will wait to post that one separately)

timholy commented 10 years ago

Hmm, that is surprising about the inference. Another lesson learned.

Thanks, @mbauman!