From ce937d2a0507a8df05963724f65af09daea6869f Mon Sep 17 00:00:00 2001 From: Mark Baum <> Date: Sun, 24 Mar 2024 12:55:19 -0700 Subject: [PATCH 1/4] add pairwise! for Covariance types --- src/covariance.jl | 19 +++++++++++++++++-- src/variogram.jl | 10 ++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/covariance.jl b/src/covariance.jl index 4012aef..a352a9b 100644 --- a/src/covariance.jl +++ b/src/covariance.jl @@ -18,15 +18,30 @@ 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!(Γ, cov, domain) + +Evaluates covariance `cov` between all elements in the `domain` in-place, filling the matrix `Γ`. + + pairwise!(Γ, cov, domain₁, domain₂) + +Evaluates covariance `cov` between all elements of `domain₁` and `domain₂` in-place, filling the matrix `Γ`.""" +function pairwise!(Γ, cov::Covariance, args...) + pairwise!(Γ, cov.γ, args...) + Γ .*= -one(eltype(Γ)) + Γ .+= sill(cov.γ) + Γ +end + # ----------- # IO METHODS # ----------- diff --git a/src/variogram.jl b/src/variogram.jl index 5818194..8b76685 100644 --- a/src/variogram.jl +++ b/src/variogram.jl @@ -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 @@ -185,6 +190,11 @@ function pairwise(γ::Variogram, domain₁, domain₂) pairwise!(Γ, γ, domain₁, domain₂) end +""" + pairwise!(Γ, γ, 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₂) From 88c729a3448ba87bc7f44cc5e87b25645f10bb77 Mon Sep 17 00:00:00 2001 From: Mark Baum <> Date: Sun, 24 Mar 2024 18:56:23 -0700 Subject: [PATCH 2/4] add pairwise! covariance test and update function --- src/covariance.jl | 5 +++-- test/covariance.jl | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/covariance.jl b/src/covariance.jl index a352a9b..cf7f482 100644 --- a/src/covariance.jl +++ b/src/covariance.jl @@ -37,8 +37,9 @@ Evaluates covariance `cov` between all elements in the `domain` in-place, fillin Evaluates covariance `cov` between all elements of `domain₁` and `domain₂` in-place, filling the matrix `Γ`.""" function pairwise!(Γ, cov::Covariance, args...) pairwise!(Γ, cov.γ, args...) - Γ .*= -one(eltype(Γ)) - Γ .+= sill(cov.γ) + for i ∈ eachindex(Γ) + Γ[i] = sill(cov.γ) - Γ[i] + end Γ end diff --git a/test/covariance.jl b/test/covariance.jl index b9b41e3..0f06072 100644 --- a/test/covariance.jl +++ b/test/covariance.jl @@ -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)" From dd38e46921d803823401d34df01bc70d2c6a95c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Hoffimann?= Date: Mon, 25 Mar 2024 14:07:14 -0300 Subject: [PATCH 3/4] Minor adjustments --- src/covariance.jl | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/covariance.jl b/src/covariance.jl index cf7f482..17845a5 100644 --- a/src/covariance.jl +++ b/src/covariance.jl @@ -28,19 +28,20 @@ Evaluate covariance `cov` between all elements of `domain₁` and `domain₂`. pairwise(cov::Covariance, args...) = sill(cov.γ) .- pairwise(cov.γ, args...) """ - pairwise!(Γ, cov, domain) + pairwise!(C, cov, domain) -Evaluates covariance `cov` between all elements in the `domain` in-place, filling the matrix `Γ`. +Evaluates covariance `cov` between all elements in the `domain` in-place, filling the matrix `C`. - pairwise!(Γ, cov, domain₁, domain₂) + pairwise!(C, cov, domain₁, domain₂) -Evaluates covariance `cov` between all elements of `domain₁` and `domain₂` in-place, filling the matrix `Γ`.""" -function pairwise!(Γ, cov::Covariance, args...) - pairwise!(Γ, cov.γ, args...) - for i ∈ eachindex(Γ) - Γ[i] = sill(cov.γ) - Γ[i] +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 # ----------- From ad9a60191b3f81369c9d65db2bfaca5538a62043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Hoffimann?= Date: Mon, 25 Mar 2024 14:09:14 -0300 Subject: [PATCH 4/4] Update src/variogram.jl --- src/variogram.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/variogram.jl b/src/variogram.jl index 8b76685..de09bec 100644 --- a/src/variogram.jl +++ b/src/variogram.jl @@ -191,7 +191,7 @@ function pairwise(γ::Variogram, domain₁, domain₂) end """ - pairwise!(Γ, γ, domain) + pairwise!(Γ, γ, domain₁, domain₂) Evaluates covariance `γ` between all elements of `domain₁` and `domain₂` in-place, filling the matrix `Γ`. """