<commit_before>

function lanczos_kernel{NumType <: Number}(x::NumType, a::Float64)
    abs(x) < a ? sinc(x) * sinc(x / a): zero(NumType)
end


function sinc_with_derivatives{NumType <: Number}(x::NumType)
    x_pi = pi * x
    sinc_x = sinc(x)
    sinc_x_d = (cos(x_pi) - sinc_x) / x
    sinc_x_h = -pi * (pi * sinc_x + 2 * sinc_x_d / x_pi)
    return sinc_x, sinc_x_d, sinc_x_h
end


# A function without checking a.  Factored out for testing with ForwardDiff..
function lanczos_kernel_with_derivatives_nocheck{NumType <: Number}(
    x::NumType, a::Float64)
    sinc_x, sinc_x_d, sinc_x_h = sinc_with_derivatives(x)
    sinc_xa, sinc_xa_d, sinc_xa_h = sinc_with_derivatives(x / a)

    return sinc_x * sinc_xa,
           sinc_x_d * sinc_xa + sinc_x * sinc_xa_d / a,
           sinc_x_h * sinc_xa + 2 * sinc_x_d * sinc_xa_d / a +
              sinc_x * sinc_xa_h / (a ^ 2)
end


function lanczos_kernel_with_derivatives{NumType <: Number}(x::NumType, a::Float64)
    if abs(x) > a
        return 0, 0, 0
    end
    return lanczos_kernel_with_derivatives_nocheck(x, a)
end


# Interpolate the PSF to the pixel values.
function lanczos_interpolate!{NumType <: Number}(
        image::Matrix{SensitiveFloat{NumType}},
        psf_image::Matrix{Float64},
        object_loc::Vector{NumType},
        lanczos_width::Int,
        wcs_jacobian::Matrix{Float64},
        calculate_gradient::Bool,
        calculate_hessian::Bool)
    @show image[1].local_P
    @assert image[1].local_P == length(StarPosParams)

    a = Float64(lanczos_width)
    h_psf_width = (size(psf_image, 1) + 1) / 2.0
    w_psf_width = (size(psf_image, 2) + 1) / 2.0

    param_ids = getids(StarPosParams)

    # These are sensitive floats representing derviatives of the Lanczos kernel.
    kernel = SensitiveFloat{NumType}(length(StarPosParams), 1,
                                     calculate_gradient, calculate_hessian)
    kernel_h = SensitiveFloat{NumType}(length(StarPosParams), 1,
                                       calculate_gradient, calculate_hessian)

    # Pre-compute terms for transforming derivatives to world coordinates.
    if calculate_gradient
        k_h_grad = wcs_jacobian' * Float64[1, 0]
        k_w_grad = wcs_jacobian' * Float64[0, 1]

        if calculate_hessian
            k_h_hess = wcs_jacobian' * Float64[1 0; 0 0] * wcs_jacobian
            k_w_hess = wcs_jacobian' * Float64[0 0; 0 1] * wcs_jacobian
        end
    end

    # h, w are pixel coordinates.
    for h = 1:size(image, 1), w = 1:size(image, 2)

        # h_psf, w_psf are in psf coordinates.
        # The PSF is centered at object_loc + psf_width.<commit_msg>removed show statement<commit_after>

function lanczos_kernel{NumType <: Number}(x::NumType, a::Float64)
    abs(x) < a ? sinc(x) * sinc(x / a): zero(NumType)
end


function sinc_with_derivatives{NumType <: Number}(x::NumType)
    x_pi = pi * x
    sinc_x = sinc(x)
    sinc_x_d = (cos(x_pi) - sinc_x) / x
    sinc_x_h = -pi * (pi * sinc_x + 2 * sinc_x_d / x_pi)
    return sinc_x, sinc_x_d, sinc_x_h
end


# A function without checking a.  Factored out for testing with ForwardDiff..
function lanczos_kernel_with_derivatives_nocheck{NumType <: Number}(
    x::NumType, a::Float64)
    sinc_x, sinc_x_d, sinc_x_h = sinc_with_derivatives(x)
    sinc_xa, sinc_xa_d, sinc_xa_h = sinc_with_derivatives(x / a)

    return sinc_x * sinc_xa,
           sinc_x_d * sinc_xa + sinc_x * sinc_xa_d / a,
           sinc_x_h * sinc_xa + 2 * sinc_x_d * sinc_xa_d / a +
              sinc_x * sinc_xa_h / (a ^ 2)
end


function lanczos_kernel_with_derivatives{NumType <: Number}(x::NumType, a::Float64)
    if abs(x) > a
        return 0, 0, 0
    end
    return lanczos_kernel_with_derivatives_nocheck(x, a)
end


# Interpolate the PSF to the pixel values.
function lanczos_interpolate!{NumType <: Number}(
        image::Matrix{SensitiveFloat{NumType}},
        psf_image::Matrix{Float64},
        object_loc::Vector{NumType},
        lanczos_width::Int,
        wcs_jacobian::Matrix{Float64},
        calculate_gradient::Bool,
        calculate_hessian::Bool)
    @assert image[1].local_P == length(StarPosParams)

    a = Float64(lanczos_width)
    h_psf_width = (size(psf_image, 1) + 1) / 2.0
    w_psf_width = (size(psf_image, 2) + 1) / 2.0

    param_ids = getids(StarPosParams)

    # These are sensitive floats representing derviatives of the Lanczos kernel.
    kernel = SensitiveFloat{NumType}(length(StarPosParams), 1,
                                     calculate_gradient, calculate_hessian)
    kernel_h = SensitiveFloat{NumType}(length(StarPosParams), 1,
                                       calculate_gradient, calculate_hessian)

    # Pre-compute terms for transforming derivatives to world coordinates.
    if calculate_gradient
        k_h_grad = wcs_jacobian' * Float64[1, 0]
        k_w_grad = wcs_jacobian' * Float64[0, 1]

        if calculate_hessian
            k_h_hess = wcs_jacobian' * Float64[1 0; 0 0] * wcs_jacobian
            k_w_hess = wcs_jacobian' * Float64[0 0; 0 1] * wcs_jacobian
        end
    end

    # h, w are pixel coordinates.
    for h = 1:size(image, 1), w = 1:size(image, 2)

        # h_psf, w_psf are in psf coordinates.
        # The PSF is centered at object_loc + psf_width.
{'fraction_non_alphanumeric': 0.10087552341073468, 'fraction_numerical': 0.020365435858393604, 'mean_word_length': 2.869661266568483, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3388130', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1959, 'n_words': 425}