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

Use eachindex instead of 1:length #37

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions src/interpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ interpolant.

See also [`coefficients`](@ref) and [`polynomial_coefficients`](@ref).
"""
kernel_coefficients(itp::Interpolation) = itp.c[1:length(nodeset(itp))]
kernel_coefficients(itp::Interpolation) = itp.c[eachindex(nodeset(itp))]

"""
polynomial_coefficients(itp::Interpolation)
Expand Down Expand Up @@ -217,11 +217,11 @@ function (itp::Interpolation)(x)
d = polynomial_coefficients(itp)
ps = polynomial_basis(itp)
xx = polyvars(itp)
for j in 1:length(c)
for j in eachindex(c)
s += c[j] * kernel(x, xs[j])
end

for k in 1:length(d)
for k in eachindex(d)
s += d[k] * ps[k](xx => x)
end
return s
Expand All @@ -238,7 +238,7 @@ function (diff_op::AbstractDifferentialOperator)(itp::Interpolation, x)
xs = nodeset(itp)
c = kernel_coefficients(itp)
s = 0
for j in 1:length(c)
for j in eachindex(c)
s += c[j] * diff_op(kernel, x, xs[j])
end
return s
Expand All @@ -249,7 +249,7 @@ function (pde::AbstractPDE)(itp::Interpolation, x)
xs = nodeset(itp)
c = kernel_coefficients(itp)
s = 0
for j in 1:length(c)
for j in eachindex(c)
s += c[j] * pde(kernel, x, xs[j])
end
return s
Expand Down Expand Up @@ -277,8 +277,8 @@ function kernel_inner_product(itp1, itp2)
xs = nodeset(itp1)
ys = nodeset(itp2)
s = 0
for j in 1:length(c)
for k in 1:length(d)
for j in eachindex(c)
for k in eachindex(d)
s += c[j] * d[k] * kernel(xs[j], ys[k])
end
end
Expand Down
5 changes: 3 additions & 2 deletions src/nodes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Return the separation distance of a node set ``X = \{x_1,\ldots, x_n\}`` defined
separation_distance(nodeset::NodeSet) = nodeset.q
function update_separation_distance!(nodeset::NodeSet)
# Update separation distance only if all values are assigned to prevent `UndefRefError`
if all(map(i -> isassigned(nodeset, i), 1:length(nodeset)))
if all(map(i -> isassigned(nodeset, i), eachindex(nodeset)))
q = separation_distance(nodeset.nodes)
nodeset.q = q
end
Expand All @@ -90,6 +90,7 @@ Base.size(nodeset::NodeSet) = (length(nodeset), dim(nodeset))
Base.iterate(nodeset::NodeSet, state = 1) = iterate(nodeset.nodes, state)
Base.collect(nodeset::NodeSet) = collect(nodeset.nodes)
Base.axes(nodeset::NodeSet) = axes(nodeset.nodes)
Base.eachindex(nodeset::NodeSet) = eachindex(nodeset.nodes)
Base.isassigned(nodeset::NodeSet, i::Int) = isassigned(nodeset.nodes, i)
function Base.similar(nodeset::NodeSet{Dim, RealT}) where {Dim, RealT}
NodeSet{Dim, RealT}(similar(nodeset.nodes), Inf)
Expand Down Expand Up @@ -219,7 +220,7 @@ end

function project_on_hypercube_boundary!(nodeset::NodeSet{Dim}, x_min::NTuple{Dim},
x_max::NTuple{Dim}) where {Dim}
for i in 1:length(nodeset)
for i in eachindex(nodeset)
# j = argmin([abs.(nodeset[i] .- x_min); abs.(nodeset[i] .- x_max)])
# Project to random axis
j = rand(1:Dim)
Expand Down
35 changes: 18 additions & 17 deletions test/test_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ using Plots
0.9022506660348356,
0.9297353942237024,
0.9389666269913006]
for i in 1:length(nus)
for i in eachindex(nus)
kernel8 = @test_nowarn MaternKernel{2}(nus[i])
@test_nowarn println(kernel8)
@test_nowarn display(kernel8)
Expand Down Expand Up @@ -158,6 +158,7 @@ using Plots
@test length(nodeset1) == 4
@test size(nodeset1) == (4, 2)
@test axes(nodeset1) == (1:4,)
@test eachindex(nodeset1) == 1:4
for node in nodeset1
@test node isa MVector{2, Float64}
end
Expand All @@ -173,13 +174,13 @@ using Plots
@test dim(nodeset2) == 2
@test length(nodeset2) == 4
@test size(nodeset2) == (4, 2)
for i in 1:length(nodeset1)
for i in eachindex(nodeset1)
@test nodeset1[i] == nodeset2[i]
end
sub_nodes = nodeset1[1:2]
@test length(sub_nodes) == 2
@test sub_nodes isa Vector{MVector{2, Float64}}
for i in 1:length(sub_nodes)
for i in eachindex(sub_nodes)
@test nodeset1[i] == sub_nodes[i]
end

Expand Down Expand Up @@ -245,15 +246,15 @@ using Plots
[1.0, 1.0],
[1.1, 1.3]]
@test length(nodeset1) == length(expected_nodes)
for i in 1:length(nodeset1)
for i in eachindex(nodeset1)
@test nodeset1[i] == expected_nodes[i]
end
@test_nowarn deleteat!(nodeset1, [2, 4])
expected_nodes = [
[0.0, 0.0],
[1.0, 1.0]]
@test length(nodeset1) == length(expected_nodes)
for i in 1:length(nodeset1)
for i in eachindex(nodeset1)
@test nodeset1[i] == expected_nodes[i]
end

Expand Down Expand Up @@ -318,7 +319,7 @@ using Plots
@test nodeset11 isa NodeSet{2, Float64}
@test isapprox(separation_distance(nodeset11), 0.75)
@test length(nodeset11) == length(expected_nodes)
for i in 1:length(nodeset11)
for i in eachindex(nodeset11)
@test nodeset11[i] == expected_nodes[i]
end

Expand All @@ -335,7 +336,7 @@ using Plots
@test nodeset12 isa NodeSet{2, Float64}
@test isapprox(separation_distance(nodeset12), 0.75)
@test length(nodeset12) == length(expected_nodes)
for i in 1:length(nodeset12)
for i in eachindex(nodeset12)
@test nodeset12[i] == expected_nodes[i]
end

Expand All @@ -356,7 +357,7 @@ using Plots
@test nodeset11_1 isa NodeSet{2, Float64}
@test isapprox(separation_distance(nodeset11_1), 0.5)
@test length(nodeset11_1) == length(expected_nodes)
for i in 1:length(nodeset11_1)
for i in eachindex(nodeset11_1)
@test nodeset11_1[i] == expected_nodes[i]
end

Expand All @@ -375,7 +376,7 @@ using Plots
@test nodeset12_1 isa NodeSet{2, Float64}
@test isapprox(separation_distance(nodeset12_1), 0.5)
@test length(nodeset12_1) == length(expected_nodes)
for i in 1:length(nodeset12_1)
for i in eachindex(nodeset12_1)
@test nodeset12_1[i] == expected_nodes[i]
end

Expand All @@ -393,7 +394,7 @@ using Plots
@test nodeset11_2 isa NodeSet{2, Float64}
@test isapprox(separation_distance(nodeset11_2), 0.5)
@test length(nodeset11_2) == length(expected_nodes)
for i in 1:length(nodeset11_2)
for i in eachindex(nodeset11_2)
@test nodeset11_2[i] == expected_nodes[i]
end

Expand All @@ -411,7 +412,7 @@ using Plots
@test isapprox(separation_distance(nodeset12_2), 0.5)
@test length(nodeset12_2) == length(expected_nodes)
display(nodeset12_2)
for i in 1:length(nodeset12_2)
for i in eachindex(nodeset12_2)
@test nodeset12_2[i] == expected_nodes[i]
end

Expand All @@ -432,7 +433,7 @@ using Plots
@test nodeset11_3 isa NodeSet{2, Float64}
@test isapprox(separation_distance(nodeset11_3), 0.5)
@test length(nodeset11_3) == length(expected_nodes)
for i in 1:length(nodeset11_3)
for i in eachindex(nodeset11_3)
@test nodeset11_3[i] == expected_nodes[i]
end

Expand All @@ -450,7 +451,7 @@ using Plots
@test nodeset11_4 isa NodeSet{2, Float64}
@test isapprox(separation_distance(nodeset11_4), 0.25)
@test length(nodeset11_4) == length(expected_nodes)
for i in 1:length(nodeset11_4)
for i in eachindex(nodeset11_4)
@test nodeset11_4[i] == expected_nodes[i]
end

Expand All @@ -470,7 +471,7 @@ using Plots
@test isapprox(separation_distance(nodeset12_3), 0.5)
@test length(nodeset12_3) == length(expected_nodes)
display(nodeset12_3)
for i in 1:length(nodeset12_3)
for i in eachindex(nodeset12_3)
@test nodeset12_3[i] == expected_nodes[i]
end

Expand All @@ -487,7 +488,7 @@ using Plots
@test nodeset12_4 isa NodeSet{2, Float64}
@test isapprox(separation_distance(nodeset12_4), 0.25)
@test length(nodeset12_4) == length(expected_nodes)
for i in 1:length(nodeset12_4)
for i in eachindex(nodeset12_4)
@test nodeset12_4[i] == expected_nodes[i]
end

Expand Down Expand Up @@ -542,7 +543,7 @@ using Plots
2.857536500685]
coeffs = coefficients(itp)
@test length(coeffs) == length(expected_coefficients)
for i in 1:length(coeffs)
for i in eachindex(coeffs)
@test isapprox(coeffs[i], expected_coefficients[i])
end
@test length(kernel_coefficients(itp)) == length(coeffs)
Expand All @@ -567,7 +568,7 @@ using Plots
1.0]
coeffs = coefficients(itp)
@test length(coeffs) == length(expected_coefficients)
for i in 1:length(coeffs)
for i in eachindex(coeffs)
@test isapprox(coeffs[i], expected_coefficients[i])
end
@test order(itp) == order(kernel)
Expand Down
2 changes: 1 addition & 1 deletion test/test_util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ macro test_include_example(example, args...)
atol = $atol, rtol = $rtol)
else
rhs_values = KernelInterpolation.rhs(pde, nodeset_inner)
for i in 1:length(nodeset_inner)
for i in eachindex(nodeset_inner)
@test isapprox(pde(itp, nodeset_inner[i]), rhs_values[i],
atol = $atol, rtol = $rtol)
end
Expand Down
Loading