Skip to content

Commit c852d31

Browse files
Apply JuliaFormatter and fix failing tests by using approximate equality
Co-authored-by: SouthEndMusic <[email protected]>
1 parent 02bfd07 commit c852d31

File tree

3 files changed

+75
-55
lines changed

3 files changed

+75
-55
lines changed

src/interpolation_caches.jl

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -816,43 +816,47 @@ function BSplineInterpolation(
816816
u, t = munge_data(u, t)
817817
n = length(t)
818818
n < d + 1 && error("BSplineInterpolation needs at least d + 1, i.e. $(d+1) points.")
819-
819+
820820
# Initialize parameter vector
821821
param_vec = zero(t)
822822
param_vec[1] = zero(eltype(t))
823823
param_vec[end] = one(eltype(t))
824-
824+
825825
# Initialize knot vector
826826
knot_vec = zeros(eltype(t), n + d + 1)
827827

828828
# Compute parameter vector based on type
829829
if pVecType == :Uniform
830830
for i in 2:(n - 1)
831-
param_vec[i] = param_vec[1] + (i - 1) * (param_vec[end] - param_vec[1]) / (n - 1)
831+
param_vec[i] = param_vec[1] +
832+
(i - 1) * (param_vec[end] - param_vec[1]) / (n - 1)
832833
end
833834
elseif pVecType == :ArcLen
834835
# Only compute arc lengths when needed, using diff and broadcasting
835-
distances = sqrt.((diff(t)).^2 .+ (diff(u)).^2)
836+
distances = sqrt.((diff(t)) .^ 2 .+ (diff(u)) .^ 2)
836837
cumulative_distances = cumsum(distances)
837838
total_distance = cumulative_distances[end]
838839
for i in 2:(n - 1)
839-
param_vec[i] = param_vec[1] + cumulative_distances[i - 1] / total_distance * (param_vec[end] - param_vec[1])
840+
param_vec[i] = param_vec[1] +
841+
cumulative_distances[i - 1] / total_distance *
842+
(param_vec[end] - param_vec[1])
840843
end
841844
end
842845

843846
# Set boundary knots using vectorized assignment
844-
knot_vec[1:d+1] .= param_vec[1]
845-
knot_vec[end-d:end] .= param_vec[end]
847+
knot_vec[1:(d + 1)] .= param_vec[1]
848+
knot_vec[(end - d):end] .= param_vec[end]
846849

847850
# Compute cumulative parameter sum for internal knots using cumsum
848-
param_cumsum = cumsum(param_vec[2:end-1])
851+
param_cumsum = cumsum(param_vec[2:(end - 1)])
849852

850853
if knotVecType == :Uniform
851854
# uniformly spaced knot vector
852855
# this method is not recommended because, if it is used with the chord length method for global interpolation,
853856
# the system of linear equations would be singular.
854857
for i in (d + 2):n
855-
knot_vec[i] = knot_vec[1] + (i - d - 1) // (n - d) * (knot_vec[end] - knot_vec[1])
858+
knot_vec[i] = knot_vec[1] +
859+
(i - d - 1) // (n - d) * (knot_vec[end] - knot_vec[1])
856860
end
857861
elseif knotVecType == :Average
858862
# average spaced knot vector
@@ -865,7 +869,7 @@ function BSplineInterpolation(
865869
index += 1
866870
end
867871
end
868-
872+
869873
# control points
870874
spline_coeffs = zeros(eltype(t), n, n)
871875
spline_coefficients!(spline_coeffs, d, knot_vec, param_vec)
@@ -888,12 +892,12 @@ function BSplineInterpolation(
888892
u, t = munge_data(u, t)
889893
n = length(t)
890894
n < d + 1 && error("BSplineInterpolation needs at least d + 1, i.e. $(d+1) points.")
891-
895+
892896
# Initialize parameter vector
893897
param_vec = zero(t)
894898
param_vec[1] = zero(eltype(t))
895899
param_vec[end] = one(eltype(t))
896-
900+
897901
# Initialize knot vector
898902
knot_vec = zeros(eltype(t), n + d + 1)
899903

@@ -902,33 +906,38 @@ function BSplineInterpolation(
902906

903907
if pVecType == :Uniform
904908
for i in 2:(n - 1)
905-
param_vec[i] = param_vec[1] + (i - 1) * (param_vec[end] - param_vec[1]) / (n - 1)
909+
param_vec[i] = param_vec[1] +
910+
(i - 1) * (param_vec[end] - param_vec[1]) / (n - 1)
906911
end
907912
elseif pVecType == :ArcLen
908913
# Only compute arc lengths when needed, using diff and broadcasting
909914
time_diffs = diff(t)
910-
spatial_diffs = [sqrt(sum((u[array_axes..., i+1] - u[array_axes..., i]).^2)) for i in 1:(n-1)]
911-
distances = sqrt.(time_diffs.^2 .+ spatial_diffs.^2)
915+
spatial_diffs = [sqrt(sum((u[array_axes..., i + 1] - u[array_axes..., i]) .^ 2))
916+
for i in 1:(n - 1)]
917+
distances = sqrt.(time_diffs .^ 2 .+ spatial_diffs .^ 2)
912918
cumulative_distances = cumsum(distances)
913919
total_distance = cumulative_distances[end]
914920
for i in 2:(n - 1)
915-
param_vec[i] = param_vec[1] + cumulative_distances[i - 1] / total_distance * (param_vec[end] - param_vec[1])
921+
param_vec[i] = param_vec[1] +
922+
cumulative_distances[i - 1] / total_distance *
923+
(param_vec[end] - param_vec[1])
916924
end
917925
end
918926

919927
# Set boundary knots using vectorized assignment
920-
knot_vec[1:d+1] .= param_vec[1]
921-
knot_vec[end-d:end] .= param_vec[end]
928+
knot_vec[1:(d + 1)] .= param_vec[1]
929+
knot_vec[(end - d):end] .= param_vec[end]
922930

923931
# Compute cumulative parameter sum for internal knots using cumsum
924-
param_cumsum = cumsum(param_vec[2:end-1])
932+
param_cumsum = cumsum(param_vec[2:(end - 1)])
925933

926934
if knotVecType == :Uniform
927935
# uniformly spaced knot vector
928936
# this method is not recommended because, if it is used with the chord length method for global interpolation,
929937
# the system of linear equations would be singular.
930938
for i in (d + 2):n
931-
knot_vec[i] = knot_vec[1] + (i - d - 1) // (n - d) * (knot_vec[end] - knot_vec[1])
939+
knot_vec[i] = knot_vec[1] +
940+
(i - d - 1) // (n - d) * (knot_vec[end] - knot_vec[1])
932941
end
933942
elseif knotVecType == :Average
934943
# average spaced knot vector
@@ -941,7 +950,7 @@ function BSplineInterpolation(
941950
index += 1
942951
end
943952
end
944-
953+
945954
# control points
946955
spline_coeffs = zeros(eltype(t), n, n)
947956
spline_coefficients!(spline_coeffs, d, knot_vec, param_vec)
@@ -1045,43 +1054,47 @@ function BSplineApprox(
10451054
u, t = munge_data(u, t)
10461055
n = length(t)
10471056
h < d + 1 && error("BSplineApprox needs at least d + 1, i.e. $(d+1) control points.")
1048-
1057+
10491058
# Initialize parameter vector
10501059
param_vec = zero(t)
10511060
param_vec[1] = zero(eltype(t))
10521061
param_vec[end] = one(eltype(t))
1053-
1062+
10541063
# Initialize knot vector
10551064
knot_vec = zeros(eltype(t), h + d + 1)
10561065

10571066
# Compute parameter vector based on type
10581067
if pVecType == :Uniform
10591068
for i in 2:(n - 1)
1060-
param_vec[i] = param_vec[1] + (i - 1) * (param_vec[end] - param_vec[1]) / (n - 1)
1069+
param_vec[i] = param_vec[1] +
1070+
(i - 1) * (param_vec[end] - param_vec[1]) / (n - 1)
10611071
end
10621072
elseif pVecType == :ArcLen
10631073
# Only compute arc lengths when needed, using diff and broadcasting
1064-
distances = sqrt.((diff(t)).^2 .+ (diff(u)).^2)
1074+
distances = sqrt.((diff(t)) .^ 2 .+ (diff(u)) .^ 2)
10651075
cumulative_distances = cumsum(distances)
10661076
total_distance = cumulative_distances[end]
10671077
for i in 2:(n - 1)
1068-
param_vec[i] = param_vec[1] + cumulative_distances[i - 1] / total_distance * (param_vec[end] - param_vec[1])
1078+
param_vec[i] = param_vec[1] +
1079+
cumulative_distances[i - 1] / total_distance *
1080+
(param_vec[end] - param_vec[1])
10691081
end
10701082
end
10711083

10721084
# Set boundary knots using vectorized assignment
1073-
knot_vec[1:d+1] .= param_vec[1]
1074-
knot_vec[end-d:end] .= param_vec[end]
1085+
knot_vec[1:(d + 1)] .= param_vec[1]
1086+
knot_vec[(end - d):end] .= param_vec[end]
10751087

10761088
# Compute cumulative parameter sum for internal knots using cumsum
1077-
param_cumsum = cumsum(param_vec[2:end-1])
1089+
param_cumsum = cumsum(param_vec[2:(end - 1)])
10781090

10791091
if knotVecType == :Uniform
10801092
# uniformly spaced knot vector
10811093
# this method is not recommended because, if it is used with the chord length method for global interpolation,
10821094
# the system of linear equations would be singular.
10831095
for i in (d + 2):h
1084-
knot_vec[i] = knot_vec[1] + (i - d - 1) // (h - d) * (knot_vec[end] - knot_vec[1])
1096+
knot_vec[i] = knot_vec[1] +
1097+
(i - d - 1) // (h - d) * (knot_vec[end] - knot_vec[1])
10851098
end
10861099
elseif knotVecType == :Average
10871100
# average spaced knot vector using improved distribution
@@ -1096,11 +1109,11 @@ function BSplineApprox(
10961109
knot_vec[d + 2] = param_interp(0.5)
10971110
else
10981111
internal_positions = range(0, 1, length = num_internal_knots)
1099-
knot_vec[(d+2):h] .= param_interp.(internal_positions)
1112+
knot_vec[(d + 2):h] .= param_interp.(internal_positions)
11001113
end
11011114
end
11021115
end
1103-
1116+
11041117
# control points
11051118
control_points = zeros(eltype(u), h)
11061119
control_points[1] = u[1]
@@ -1143,12 +1156,12 @@ function BSplineApprox(
11431156
u, t = munge_data(u, t)
11441157
n = length(t)
11451158
h < d + 1 && error("BSplineApprox needs at least d + 1, i.e. $(d+1) control points.")
1146-
1159+
11471160
# Initialize parameter vector
11481161
param_vec = zero(t)
11491162
param_vec[1] = zero(eltype(t))
11501163
param_vec[end] = one(eltype(t))
1151-
1164+
11521165
# Initialize knot vector
11531166
knot_vec = zeros(eltype(t), h + d + 1)
11541167

@@ -1157,33 +1170,38 @@ function BSplineApprox(
11571170

11581171
if pVecType == :Uniform
11591172
for i in 2:(n - 1)
1160-
param_vec[i] = param_vec[1] + (i - 1) * (param_vec[end] - param_vec[1]) / (n - 1)
1173+
param_vec[i] = param_vec[1] +
1174+
(i - 1) * (param_vec[end] - param_vec[1]) / (n - 1)
11611175
end
11621176
elseif pVecType == :ArcLen
11631177
# Only compute arc lengths when needed, using diff and broadcasting
11641178
time_diffs = diff(t)
1165-
spatial_diffs = [sqrt(sum((u[array_axes..., i+1] - u[array_axes..., i]).^2)) for i in 1:(n-1)]
1166-
distances = sqrt.(time_diffs.^2 .+ spatial_diffs.^2)
1179+
spatial_diffs = [sqrt(sum((u[array_axes..., i + 1] - u[array_axes..., i]) .^ 2))
1180+
for i in 1:(n - 1)]
1181+
distances = sqrt.(time_diffs .^ 2 .+ spatial_diffs .^ 2)
11671182
cumulative_distances = cumsum(distances)
11681183
total_distance = cumulative_distances[end]
11691184
for i in 2:(n - 1)
1170-
param_vec[i] = param_vec[1] + cumulative_distances[i - 1] / total_distance * (param_vec[end] - param_vec[1])
1185+
param_vec[i] = param_vec[1] +
1186+
cumulative_distances[i - 1] / total_distance *
1187+
(param_vec[end] - param_vec[1])
11711188
end
11721189
end
11731190

11741191
# Set boundary knots using vectorized assignment
1175-
knot_vec[1:d+1] .= param_vec[1]
1176-
knot_vec[end-d:end] .= param_vec[end]
1192+
knot_vec[1:(d + 1)] .= param_vec[1]
1193+
knot_vec[(end - d):end] .= param_vec[end]
11771194

11781195
# Compute cumulative parameter sum for internal knots using cumsum
1179-
param_cumsum = cumsum(param_vec[2:end-1])
1196+
param_cumsum = cumsum(param_vec[2:(end - 1)])
11801197

11811198
if knotVecType == :Uniform
11821199
# uniformly spaced knot vector
11831200
# this method is not recommended because, if it is used with the chord length method for global interpolation,
11841201
# the system of linear equations would be singular.
11851202
for i in (d + 2):h
1186-
knot_vec[i] = knot_vec[1] + (i - d - 1) // (h - d) * (knot_vec[end] - knot_vec[1])
1203+
knot_vec[i] = knot_vec[1] +
1204+
(i - d - 1) // (h - d) * (knot_vec[end] - knot_vec[1])
11871205
end
11881206
elseif knotVecType == :Average
11891207
# average spaced knot vector using improved distribution
@@ -1198,11 +1216,11 @@ function BSplineApprox(
11981216
knot_vec[d + 2] = param_interp(0.5)
11991217
else
12001218
internal_positions = range(0, 1, length = num_internal_knots)
1201-
knot_vec[(d+2):h] .= param_interp.(internal_positions)
1219+
knot_vec[(d + 2):h] .= param_interp.(internal_positions)
12021220
end
12031221
end
12041222
end
1205-
1223+
12061224
# control points
12071225
control_points = zeros(eltype(u), size(u)[1:(end - 1)]..., h)
12081226
control_points[array_axes..., 1] = u[array_axes..., 1]
@@ -1221,7 +1239,8 @@ function BSplineApprox(
12211239
for i in 2:(h - 1)
12221240
residual_sum = zeros(eltype(spline_coeffs), size(u)[1:(end - 1)]...)
12231241
for k in 2:(n - 1)
1224-
residual_sum = residual_sum + spline_coeffs[k, i] * data_residual[array_axes..., k]
1242+
residual_sum = residual_sum +
1243+
spline_coeffs[k, i] * data_residual[array_axes..., k]
12251244
end
12261245
coeff_matrix[array_axes..., i - 1] = residual_sum
12271246
end

test/derivative_tests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ function test_derivatives(method; args = [], kwargs = [], name::String)
3535

3636
# Interpolation transition points
3737
for _t in t[2:(end - 1)]
38-
if func isa Union{SmoothedConstantInterpolation, BSplineInterpolation, BSplineApprox}
38+
if func isa
39+
Union{SmoothedConstantInterpolation, BSplineInterpolation, BSplineApprox}
3940
# TODO fix interpolations
4041
continue
4142
else

test/interpolation_tests.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,8 @@ end
799799

800800
A = @inferred(BSplineInterpolation(u, t, 2, :ArcLen, :Average))
801801

802-
@test [A(25.0), A(80.0)] == [13.363814458968486, 10.685201117692609]
803-
@test [A(190.0), A(225.0)] == [13.437481084762863, 11.367034741256463]
802+
@test [A(25.0), A(80.0)] [13.363814458968486, 10.685201117692609]
803+
@test [A(190.0), A(225.0)] [13.437481084762863, 11.367034741256463]
804804
@test [A(t[1]), A(t[end])] == [u[1], u[end]]
805805

806806
@test_throws ErrorException("BSplineInterpolation needs at least d + 1, i.e. 4 points.") BSplineInterpolation(
@@ -914,31 +914,31 @@ end
914914
u_test = reduce(hcat, A.(t_test))
915915
@test isapprox(u_test, f_test, atol = 1e-2)
916916
end
917-
917+
918918
@testset ":Average knot distribution" begin
919919
# Test for proper knot distribution across parameter domain (Issue #439)
920920
x_test = 0:0.1:10
921921
y_test = randn(101)
922922
sp = BSplineApprox(y_test, x_test, 3, 20, :ArcLen, :Average)
923-
923+
924924
# Extract internal knots (skip the repeated boundary knots)
925925
d = sp.d
926926
h = sp.h
927-
internal_knots = sp.k[(d+2):h]
928-
927+
internal_knots = sp.k[(d + 2):h]
928+
929929
# Check that knots span a reasonable portion of the parameter domain
930930
param_range = maximum(sp.p) - minimum(sp.p)
931931
knot_coverage = maximum(internal_knots) - minimum(internal_knots)
932932
coverage_ratio = knot_coverage / param_range
933-
933+
934934
# The coverage ratio should be at least 0.8 (80% of parameter domain)
935935
# Before the fix, this was only ~0.136 (13.6%)
936936
@test coverage_ratio >= 0.8
937-
937+
938938
# Test that the interpolation works correctly
939939
@test sp(x_test[1]) y_test[1]
940940
@test sp(x_test[end]) y_test[end]
941-
941+
942942
# Test interpolation at some intermediate points
943943
test_points = [2.5, 5.0, 7.5]
944944
for t in test_points

0 commit comments

Comments
 (0)