Skip to content

Commit

Permalink
Merge pull request #23 from kylebeggs/feature/directional
Browse files Browse the repository at this point in the history
Feature/directional
  • Loading branch information
kylebeggs authored Mar 27, 2024
2 parents e546656 + 63dca1b commit 9f338d4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RadialBasisFunctions"
uuid = "79ee0514-adf7-4479-8807-6f72ea8967e8"
authors = ["Kyle Beggs"]
version = "0.2.0"
version = "0.2.1"

[deps]
ChunkSplitters = "ae650224-84b6-46f8-82ea-d812ca08434e"
Expand Down
26 changes: 17 additions & 9 deletions src/operators/directional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,32 @@ function directional(
return RadialBasisOperator(ℒ, data, basis; k=k)
end

function RadialBasisOperator(
::Directional,
"""
function directional(data, eval_points, v, basis; k=autoselect_k(data, basis))
Builds a `RadialBasisOperator` where the operator is the directional derivative, `Directional`.
"""
function directional(
data::AbstractVector{D},
eval_points::AbstractVector{D},
v::AbstractVector,
basis::B=PHS(3; poly_deg=2);
k::T=autoselect_k(data, basis),
) where {D<:AbstractArray,T<:Int,B<:AbstractRadialBasis}
TD = eltype(D)
adjl = find_neighbors(data, k)
N = length(adjl)
weights = ntuple(_ -> _allocate_weights(TD, N, N, k), length(ℒ.ℒ))
return RadialBasisOperator(ℒ, weights, data, data, adjl, basis)
) where {D<:AbstractArray,B<:AbstractRadialBasis,T<:Int}
f = ntuple(length(first(data))) do dim
return let dim = dim
x -> (x, 1, dim)
end
end
= Directional(f, v)
return RadialBasisOperator(ℒ, data, eval_points, basis; k=k)
end

function _update_weights!(
op::RadialBasisOperator{<:Directional}, weights::NTuple{N,AbstractMatrix}
) where {N}
v = op..v
@assert length(v) == N || length(v) == size(op)[2] "wrong size for v"
@assert length(v) == N || length(v) == size(op)[1] "wrong size for v"
if length(v) == N
for (i, ℒ) in enumerate(op..ℒ)
weights[i] .= _build_weights(ℒ, op) * v[i]
Expand Down
28 changes: 0 additions & 28 deletions src/operators/gradient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,6 @@ function gradient(
return RadialBasisOperator(ℒ, data, eval_points, basis; k=k)
end

function RadialBasisOperator(
::Gradient,
data::AbstractVector{D},
basis::B=PHS(3; poly_deg=2);
k::T=autoselect_k(data, basis),
) where {D<:AbstractArray,T<:Int,B<:AbstractRadialBasis}
TD = eltype(D)
adjl = find_neighbors(data, k)
N = length(adjl)
weights = ntuple(_ -> _allocate_weights(TD, N, N, k), length(ℒ.ℒ))
return RadialBasisOperator(ℒ, weights, data, data, adjl, basis)
end

function RadialBasisOperator(
::Gradient,
data::AbstractVector{D},
eval_points::AbstractVector{D},
basis::B=PHS(3; poly_deg=2);
k::T=autoselect_k(data, basis),
) where {D<:AbstractArray,T<:Int,B<:AbstractRadialBasis}
TD = eltype(D)
adjl = find_neighbors(data, eval_points, k)
Na = length(adjl)
Nd = length(data)
weights = ntuple(_ -> _allocate_weights(TD, Na, Nd, k), length(ℒ.ℒ))
return RadialBasisOperator(ℒ, weights, data, eval_points, adjl, basis)
end

Base.size(op::RadialBasisOperator{<:Gradient}) = size(first(op.weights))

# pretty printing
Expand Down
28 changes: 28 additions & 0 deletions src/operators/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ function RadialBasisOperator(
return RadialBasisOperator(ℒ, weights, data, eval_points, adjl, basis)
end

function RadialBasisOperator(
::VectorValuedOperator,
data::AbstractVector{D},
basis::B=PHS(3; poly_deg=2);
k::T=autoselect_k(data, basis),
) where {D<:AbstractArray,T<:Int,B<:AbstractRadialBasis}
TD = eltype(D)
adjl = find_neighbors(data, k)
N = length(adjl)
weights = ntuple(_ -> _allocate_weights(TD, N, N, k), length(ℒ.ℒ))
return RadialBasisOperator(ℒ, weights, data, data, adjl, basis)
end

function RadialBasisOperator(
::VectorValuedOperator,
data::AbstractVector{D},
eval_points::AbstractVector{D},
basis::B=PHS(3; poly_deg=2);
k::T=autoselect_k(data, basis),
) where {D<:AbstractArray,T<:Int,B<:AbstractRadialBasis}
TD = eltype(D)
adjl = find_neighbors(data, eval_points, k)
Na = length(adjl)
Nd = length(data)
weights = ntuple(_ -> _allocate_weights(TD, Na, Nd, k), length(ℒ.ℒ))
return RadialBasisOperator(ℒ, weights, data, eval_points, adjl, basis)
end

# extend Base methods
Base.length(op::RadialBasisOperator) = length(op.adjl)
Base.size(op::RadialBasisOperator) = size(op.weights)
Expand Down
14 changes: 14 additions & 0 deletions test/operators/directional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ end
@test mean_percent_error(∇vy[1], df_dx_v) < 2
@test mean_percent_error(∇vy[2], df_dy_v) < 2
end

@testset "Different Evaluation Points" begin
x2 = map(x -> SVector{2}(rand(MersenneTwister(x), 2)), 1:N)
v = map(1:length(x2)) do i
v = SVector{2}(rand(2))
return v /= norm(v)
end
∇v = directional(x, x2, v, PHS3(2))
∇vy = ∇v(y)
df_dx_v = map((df, v) -> df * v[1], df_dx.(x), v)
df_dy_v = map((df, v) -> df * v[2], df_dy.(x), v)
@test mean_percent_error(∇vy[1], df_dx_v) < 2
@test mean_percent_error(∇vy[2], df_dy_v) < 2
end

2 comments on commit 9f338d4

@kylebeggs
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/103698

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.1 -m "<description of version>" 9f338d4a9d5db4054379536d8dd35f4ccd4ffd94
git push origin v0.2.1

Please sign in to comment.