diff --git a/src/GEK.jl b/src/GEK.jl index c5fc52cdc..f0ff0ef17 100644 --- a/src/GEK.jl +++ b/src/GEK.jl @@ -163,9 +163,10 @@ end # follows from the arithmetic, so a dual number is not truncated back to the # design's type. function _gek_r(k::GEK, val) - n = length(k.x) - d = length(k.x[1]) - ks = [exp(-sum(k.theta[l] * (val[l] - k.x[i][l])^2 for l in 1:d)) for i in 1:n] + x, theta = k.x, k.theta + n = length(x) + d = length(x[1]) + ks = [exp(-sum(theta[l] * (val[l] - x[i][l])^2 for l in 1:d)) for i in 1:n] # Flattened in `_gek_deriv_index` order — point by point, and within a point, # coordinate by coordinate — so the layout matches `_gek_covariance`. The # index arithmetic inverts `n + (i - 1) * d + l`; a nested comprehension @@ -173,7 +174,7 @@ function _gek_r(k::GEK, val) # accumulates with `push!`. dks = [ let i = (j - 1) ÷ d + 1, l = (j - 1) % d + 1 - 2 * k.theta[l] * (val[l] - k.x[i][l]) * ks[i] + 2 * theta[l] * (val[l] - x[i][l]) * ks[i] end for j in 1:(n * d) ] diff --git a/src/InverseDistanceSurrogate.jl b/src/InverseDistanceSurrogate.jl index a0a179d64..219c816c3 100644 --- a/src/InverseDistanceSurrogate.jl +++ b/src/InverseDistanceSurrogate.jl @@ -62,8 +62,10 @@ function (inverSurr::InverseDistanceSurrogate)(val) _check_dimension(inverSurr, val) # A row-matrix query would broadcast into a d x d outer difference. point = _as_point(val) + # Keep the mutable surrogate out of the closures differentiated by Zygote. + x, y = inverSurr.x, inverSurr.y p = inverSurr.p - d = [norm(point .- inverSurr.x[i]) for i in eachindex(inverSurr.x)] + d = [norm(point .- x[i]) for i in eachindex(x)] d_min = minimum(d) # A query on a sample point returns that response, averaged over ties, # instead of Inf / Inf. Tested through the weight rather than `d_min == 0` @@ -71,12 +73,12 @@ function (inverSurr::InverseDistanceSurrogate)(val) # nonzero derivative is not equal to zero. if !isfinite(d_min^(-p)) hits = findall(dᵢ -> !isfinite(dᵢ^(-p)), d) - return sum(inverSurr.y[i] for i in hits) / length(hits) + return sum(y[i] for i in hits) / length(hits) end # (d / d_min)^(-p) is the same convex combination as d^(-p) with the # largest weight pinned at one, so nothing overflows or underflows. w = (d ./ d_min) .^ (-p) - return sum(w[i] * inverSurr.y[i] for i in eachindex(w)) / sum(w) + return sum(w[i] * y[i] for i in eachindex(w)) / sum(w) end function SurrogatesBase.update!(inverSurr::InverseDistanceSurrogate, x_new, y_new) diff --git a/src/Kriging.jl b/src/Kriging.jl index c76cf95f0..6a432fdd7 100644 --- a/src/Kriging.jl +++ b/src/Kriging.jl @@ -104,10 +104,11 @@ end # containers (`(5.0)[1] === 5.0`), so one implementation serves both the scalar # and the multidimensional case. function _kriging_r(k::Kriging, val) - d = length(k.x[1]) + x, theta, p = k.x, k.theta, k.p + d = length(x[1]) return [ - exp(-sum(k.theta[l] * abs(val[l] - k.x[i][l])^k.p[l] for l in 1:d)) - for i in eachindex(k.x) + exp(-sum(theta[l] * abs(val[l] - x[i][l])^p[l] for l in 1:d)) + for i in eachindex(x) ] end diff --git a/src/Lobachevsky.jl b/src/Lobachevsky.jl index 611933cd5..31f2415c1 100644 --- a/src/Lobachevsky.jl +++ b/src/Lobachevsky.jl @@ -145,10 +145,11 @@ end function (loba::LobachevskySurrogate)(val::Number) _check_dimension(loba, val) + x, alpha, n = loba.x, loba.alpha, loba.n return _loba_combine( loba.coeff, - j -> phi_nj1D(val, loba.x[j], loba.alpha, loba.n), - length(loba.x) + j -> phi_nj1D(val, x[j], alpha, n), + length(x) ) end @@ -195,10 +196,11 @@ end function (loba::LobachevskySurrogate)(val) _check_dimension(loba, val) + x, alpha, n = loba.x, loba.alpha, loba.n return _loba_combine( loba.coeff, - j -> phi_njND(val, loba.x[j], loba.alpha, loba.n), - length(loba.x) + j -> phi_njND(val, x[j], alpha, n), + length(x) ) end