Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pairwise! for Covariance types #5

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/covariance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,32 @@ Evaluate the covariance at objects `x₁` and `x₁`.

"""
pairwise(cov, domain)

Evaluate covariance `cov` between all elements in the `domain`.

pairwise(cov, domain₁, domain₂)

Evaluate covariance `cov` between all elements of `domain₁` and `domain₂`.
"""
pairwise(cov::Covariance, args...) = sill(cov.γ) .- pairwise(cov.γ, args...)

"""
pairwise!(C, cov, domain)

Evaluates covariance `cov` between all elements in the `domain` in-place, filling the matrix `C`.

pairwise!(C, cov, domain₁, domain₂)

Evaluates covariance `cov` between all elements of `domain₁` and `domain₂` in-place, filling the matrix `C`.
"""
function pairwise!(C, cov::Covariance, args...)
pairwise!(C, cov.γ, args...)
for i in eachindex(Γ)
C[i] = sill(cov.γ) - C[i]
end
C
end

# -----------
# IO METHODS
# -----------
Expand Down
10 changes: 10 additions & 0 deletions src/variogram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ function pairwise(γ::Variogram, domain)
pairwise!(Γ, γ, domain)
end

"""
pairwise!(Γ, γ, domain)
Evaluates covariance `γ` between all elements in the `domain` in-place, filling the matrix `Γ`.
"""
function pairwise!(Γ, γ::Variogram, domain)
n = length(domain)
@inbounds for j in 1:n
Expand Down Expand Up @@ -185,6 +190,11 @@ function pairwise(γ::Variogram, domain₁, domain₂)
pairwise!(Γ, γ, domain₁, domain₂)
end

"""
pairwise!(Γ, γ, domain₁, domain₂)
Evaluates covariance `γ` between all elements of `domain₁` and `domain₂` in-place, filling the matrix `Γ`.
"""
function pairwise!(Γ, γ::Variogram, domain₁, domain₂)
m = length(domain₁)
n = length(domain₂)
Expand Down
5 changes: 5 additions & 0 deletions test/covariance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
@test eltype(Γ_f) == Float32
@test issymmetric(Γ_f)

𝒟 = PointSet(Matrix(1.0I, 3, 3))
Γ = Matrix{Float64}(undef, 3, 3)
GeoStatsFunctions.pairwise!(Γ, GaussianCovariance(range=1.0, sill=1.0, nugget=1.0), 𝒟)
@test issymmetric(Γ)

# shows
cov = CircularCovariance()
@test sprint(show, cov) == "CircularCovariance(sill: 1.0, nugget: 0.0, range: 1.0, distance: Euclidean)"
Expand Down