Skip to content

Commit

Permalink
Refactor variogram files
Browse files Browse the repository at this point in the history
  • Loading branch information
juliohm committed Feb 19, 2024
1 parent c67c06f commit 42e2019
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 65 deletions.
57 changes: 16 additions & 41 deletions src/nesting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,51 +26,15 @@ end

NestedVariogram(cs, γs) = NestedVariogram{typeof(cs),typeof(γs)}(cs, γs)

(g::NestedVariogram)(h) = raw(sum(g.cs .* map-> γ(h), g.γs)))
(g::NestedVariogram)(u::Point, v::Point) = raw(sum(g.cs .* map-> γ(u, v), g.γs)))
isstationary::NestedVariogram) = all(isstationary, γ.γs)

sill(g::NestedVariogram) = raw(sum(g.cs .* map(sill, g.γs)))
nugget(g::NestedVariogram) = raw(sum(g.cs .* map(nugget, g.γs)))
Base.range(g::NestedVariogram) = maximum(range(γ) for γ in g.γs)
isstationary(g::NestedVariogram) = all(isstationary, g.γs)
isisotropic(g::NestedVariogram) = all(isisotropic, g.γs)
isisotropic::NestedVariogram) = all(isisotropic, γ.γs)

# algebraic structure
*(c, γ::Variogram) = NestedVariogram((c,), (γ,))
*(c, γ::NestedVariogram) = NestedVariogram(map(x -> c .* x, γ.cs), γ.γs)
+(γ₁::Variogram, γ₂::Variogram) = NestedVariogram((1, 1), (γ₁, γ₂))
+(γ₁::NestedVariogram, γ₂::Variogram) = NestedVariogram((γ₁.cs..., 1), (γ₁.γs..., γ₂))
+(γ₁::Variogram, γ₂::NestedVariogram) = NestedVariogram((1, γ₂.cs...), (γ₁, γ₂.γs...))
+(γ₁::NestedVariogram, γ₂::NestedVariogram) = NestedVariogram((γ₁.cs..., γ₂.cs...), (γ₁.γs..., γ₂.γs...))

"""
structures(γ)
sill::NestedVariogram) = raw(sum.cs .* map(sill, γ.γs)))

Return the individual structures of a (possibly nested)
variogram as a tuple. The structures are the total nugget
`cₒ`, and the coefficients (or contributions) `c[i]` for the
remaining non-trivial structures `g[i]` after normalization
(i.e. sill=1, nugget=0).
nugget::NestedVariogram) = raw(sum.cs .* map(nugget, γ.γs)))

## Examples
```julia
γ₁ = GaussianVariogram(nugget=1, sill=2)
γ₂ = SphericalVariogram(nugget=2, sill=3)
γ = 2γ₁ + 3γ₂
cₒ, c, g = structures(γ)
```
"""
function structures::Variogram)
cₒ = nugget(γ)
c = sill(γ) - nugget(γ)
T = typeof(c)
γ = @set γ.sill = one(T)
γ = @set γ.nugget = zero(T)
cₒ, (c,), (γ,)
end
Base.range::NestedVariogram) = maximum(range(g) for g in γ.γs)

function structures::NestedVariogram)
ks, gs = γ.cs, γ.γs
Expand All @@ -93,6 +57,17 @@ function structures(γ::NestedVariogram)
cₒ, cs, γs
end

::NestedVariogram)(h) = raw(sum.cs .* map(g -> g(h), γ.γs)))
::NestedVariogram)(u::Point, v::Point) = raw(sum.cs .* map(g -> g(u, v), γ.γs)))

# algebraic structure
*(c, γ::Variogram) = NestedVariogram((c,), (γ,))
*(c, γ::NestedVariogram) = NestedVariogram(map(x -> c .* x, γ.cs), γ.γs)
+(γ₁::Variogram, γ₂::Variogram) = NestedVariogram((1, 1), (γ₁, γ₂))
+(γ₁::NestedVariogram, γ₂::Variogram) = NestedVariogram((γ₁.cs..., 1), (γ₁.γs..., γ₂))
+(γ₁::Variogram, γ₂::NestedVariogram) = NestedVariogram((1, γ₂.cs...), (γ₁, γ₂.γs...))
+(γ₁::NestedVariogram, γ₂::NestedVariogram) = NestedVariogram((γ₁.cs..., γ₂.cs...), (γ₁.γs..., γ₂.γs...))

# -----------
# IO METHODS
# -----------
Expand Down
87 changes: 63 additions & 24 deletions src/variogram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ A theoretical variogram model (e.g. Gaussian variogram).
"""
abstract type Variogram end

"""
variotype(γ)
Return the type constructor of the variogram `γ`.
"""
function variotype end

"""
isstationary(γ)
Check if variogram `γ` possesses the 2nd-order stationary property.
"""
isstationary::Variogram) = isstationary(typeof(γ))

"""
isisotropic(γ)
Tells whether or not variogram `γ` is isotropic.
"""
isisotropic::Variogram) = isisotropic.ball)

"""
sill(γ)
Expand Down Expand Up @@ -38,11 +59,33 @@ Return the maximum range of the variogram `γ`.
Base.range::Variogram) = maximum(radii.ball))

"""
variotype(γ)
structures(γ)
Return the type constructor of the variogram `γ`.
Return the individual structures of a (possibly nested)
variogram as a tuple. The structures are the total nugget
`cₒ`, and the coefficients (or contributions) `c[i]` for the
remaining non-trivial structures `g[i]` after normalization
(i.e. sill=1, nugget=0).
## Examples
```julia
γ₁ = GaussianVariogram(nugget=1, sill=2)
γ₂ = SphericalVariogram(nugget=2, sill=3)
γ = 2γ₁ + 3γ₂
cₒ, c, g = structures(γ)
```
"""
function variotype end
function structures::Variogram)
cₒ = nugget(γ)
c = sill(γ) - nugget(γ)
T = typeof(c)
γ = @set γ.sill = one(T)
γ = @set γ.nugget = zero(T)
cₒ, (c,), (γ,)
end

"""
γ(u, v)
Expand Down Expand Up @@ -85,27 +128,6 @@ function (γ::Variogram)(U::Geometry, V::Geometry)
mean(γ(u, v) for u in us, v in vs)
end

"""
returntype(γ, u, v)
Return result type of γ(u, v).
"""
returntype::Variogram, u, v) = typeof(γ(u, v))

"""
isstationary(γ)
Check if variogram `γ` possesses the 2nd-order stationary property.
"""
isstationary::Variogram) = isstationary(typeof(γ))

"""
isisotropic(γ)
Tells whether or not variogram `γ` is isotropic.
"""
isisotropic::Variogram) = isisotropic.ball)

"""
pairwise(γ, domain)
Expand Down Expand Up @@ -167,6 +189,23 @@ function pairwise!(Γ, γ::Variogram, domain₁, domain₂)
Γ
end

"""
returntype(γ, u, v)
Return type of γ(u, v).
"""
returntype::Variogram, u, v) = typeof(γ(u, v))

"""
scale(γ, s)
Scale ranges of variogram `γ` with strictly positive scaling factor `s`.
"""
function scale::Variogram, s::Real)
V = variotype(γ)
V(s * metricball(γ); sill=sill(γ), nugget=nugget(γ))
end

# -----------
# IO METHODS
# -----------
Expand Down

0 comments on commit 42e2019

Please sign in to comment.