Skip to content
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
10 changes: 5 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ StatsAPI = "82ae8749-77ed-4fe6-ae5f-f523153014b0"

[compat]
ADTypes = "1.20.0"
Aqua = "0.8.12"
CommonSolve = "0.2.6"
ConcreteStructs = "0.2.3"
DifferentiationInterface = "0.7.13"
Expand All @@ -41,18 +40,19 @@ NonlinearSolveBase = "2.25"
NonlinearSolveFirstOrder = "2"
PrecompileTools = "1"
RecursiveArrayTools = "3.33.0, 4.0"
SafeTestsets = "0.1"
SciMLBase = "2.90, 3.1"
SciMLTesting = "1"
Setfield = "1.1.2"
StatsAPI = "1.8.0"
Test = "1.10"
TestItemRunner = "1.1.0"
julia = "1.10"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
ExplicitImports = "7d51a73a-1435-4ff3-83d9-f097790105c7"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
SciMLTesting = "09d9d899-5365-40a9-917a-5f67fddea283"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"

[targets]
test = ["Test", "TestItemRunner", "Aqua", "ExplicitImports"]
test = ["Test", "SafeTestsets", "SciMLTesting", "ExplicitImports"]
13 changes: 5 additions & 8 deletions test/qa.jl → test/explicit_imports.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
@testitem "Aqua tests" tags = [:qa, :nopre] begin
using Aqua
Aqua.test_all(CurveFit)
end

@testitem "Explicit Imports" tags = [:qa] begin
using ExplicitImports
using StatsAPI
using CurveFit
using Test
using ExplicitImports
using StatsAPI

@testset "Explicit Imports" begin
@test check_no_implicit_imports(CurveFit) === nothing
@test check_no_stale_explicit_imports(CurveFit) === nothing
@test check_no_self_qualified_accesses(CurveFit) === nothing
Expand Down
104 changes: 3 additions & 101 deletions test/expsumfit.jl
Original file line number Diff line number Diff line change
@@ -1,105 +1,7 @@
@testitem "ExpSumFit: Integration rules" tags = [:expsumfit] begin
# Trapezoidal rule
@test CurveFit.__calc_integral_rules(Rational{Int64}, 1:1; m = 1) == [1 // 2 1 // 2]
using CurveFit
using Test

# Simpson's first (1/3) rule
@test CurveFit.__calc_integral_rules(Rational{Int64}, 1:1; m = 2) ==
[1 // 3 4 // 3 1 // 3]
@test CurveFit.__calc_integral_rules(Rational{Int64}, 2:2; m = 2) ==
[2 // 3 4 // 3 0 // 1]
@test CurveFit.__calc_integral_rules(Rational{Int64}, 3:3; m = 2) ==
[3 // 5 4 // 5 -1 // 15]

# Simpson's second (3/8) rule
@test CurveFit.__calc_integral_rules(Rational{Int64}, 1:1; m = 3) ==
[3 // 8 9 // 8 9 // 8 3 // 8]
@test CurveFit.__calc_integral_rules(Rational{Int64}, 2:2; m = 3) ==
[39 // 40 27 // 10 27 // 40 3 // 20]
end

@testitem "ExpSumFit: Cumulative integrals" tags = [:expsumfit] begin
n = 4

x = collect(0:0.4:10)
y = @. 1 + sin(x)
cumints_analytic = @. [(x + 1 - cos(x)) (1 / 2 * x^2 + x - sin(x)) (
1 / 6 * x^3 +
x^2 / 2 +
cos(x) - 1
) (
1 /
24 *
x^4 +
x^3 /
6 +
sin(x) -
x
)]

@testset "m = 1 & n = 4" begin
coeffs = CurveFit.__calc_integral_rules(Float64, 1:4; m = 1)

len = length(x)
nY, mY = 1 + (len - 1) ÷ 1, 2 * n + 1

Y = Matrix{Float64}(undef, nY, mY)
S = Matrix{Float64}(undef, nY, 4 - 1)

CurveFit.__cumulative_integrals!(Y, S, coeffs, x, y, 4, 1)

@test cumints_analytic[:, 1] ≈ Y[:, 1] rtol = 3.0e-3
@test cumints_analytic[:, 2] ≈ Y[:, 2] rtol = 3.0e-3
@test cumints_analytic[:, 3] ≈ Y[:, 3] rtol = 4.0e-3
@test cumints_analytic[:, 4] ≈ Y[:, 4] rtol = 4.0e-3
end

@testset "m = 2 & n = 4" begin
coeffs = CurveFit.__calc_integral_rules(Float64, 1:4; m = 2)

len = length(x)
nY, mY = 1 + (len - 1) ÷ 2, 2 * n + 1

Y = Matrix{Float64}(undef, nY, mY)
S = Matrix{Float64}(undef, nY, 4 - 1)

CurveFit.__cumulative_integrals!(Y, S, coeffs, x, y, 4, 2)

@test cumints_analytic[1:2:end, 1] ≈ Y[:, 1] rtol = 3.0e-5
@test cumints_analytic[1:2:end, 2] ≈ Y[:, 2] rtol = 4.0e-5
@test cumints_analytic[1:2:end, 3] ≈ Y[:, 3] rtol = 5.0e-5
@test cumints_analytic[1:2:end, 4] ≈ Y[:, 4] rtol = 6.0e-5
end

x = collect(range(0, stop = 2, length = 13))
y = @. exp(x)
cumints_analytic = @. [(exp(x) - 1) (exp(x) - 1 - x) (exp(x) - 1 - x - x^2 / 2) (
exp(x) -
1 - x -
x^2 /
2 -
x^3 /
6
)]

@testset "m = 2 & n = 4" begin
coeffs = CurveFit.__calc_integral_rules(Float64, 1:4; m = 2)

len = length(x)
nY, mY = 1 + (len - 1) ÷ 2, 2 * n + 1

Y = Matrix{Float64}(undef, nY, mY)
S = Matrix{Float64}(undef, nY, 4 - 1)

CurveFit.__cumulative_integrals!(Y, S, coeffs, x, y, 4, 2)

@test cumints_analytic[1:2:end, 1] ≈ Y[:, 1] rtol = 5.0e-6
@test cumints_analytic[1:2:end, 2] ≈ Y[:, 2] rtol = 3.0e-5
@test cumints_analytic[1:2:end, 3] ≈ Y[:, 3] rtol = 3.0e-5
@test cumints_analytic[1:2:end, 4] ≈ Y[:, 4] rtol = 4.0e-5
end
end

@testitem "ExpSumFit" tags = [:expsumfit] begin
@testset "ExpSumFit" begin
x = collect(0.02:0.02:1.5)
y = @. 5 * exp(0.5 * x) + 4 * exp(-3 * x) + 2 * exp(-2 * x) - 3 * exp(0.15 * x)

Expand Down
84 changes: 84 additions & 0 deletions test/expsumfit_cumulative_integrals.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using CurveFit
using Test

@testset "ExpSumFit: Cumulative integrals" begin
n = 4

x = collect(0:0.4:10)
y = @. 1 + sin(x)
cumints_analytic = @. [(x + 1 - cos(x)) (1 / 2 * x^2 + x - sin(x)) (
1 / 6 * x^3 +
x^2 / 2 +
cos(x) - 1
) (
1 /
24 *
x^4 +
x^3 /
6 +
sin(x) -
x
)]

@testset "m = 1 & n = 4" begin
coeffs = CurveFit.__calc_integral_rules(Float64, 1:4; m = 1)

len = length(x)
nY, mY = 1 + (len - 1) ÷ 1, 2 * n + 1

Y = Matrix{Float64}(undef, nY, mY)
S = Matrix{Float64}(undef, nY, 4 - 1)

CurveFit.__cumulative_integrals!(Y, S, coeffs, x, y, 4, 1)

@test cumints_analytic[:, 1] ≈ Y[:, 1] rtol = 3.0e-3
@test cumints_analytic[:, 2] ≈ Y[:, 2] rtol = 3.0e-3
@test cumints_analytic[:, 3] ≈ Y[:, 3] rtol = 4.0e-3
@test cumints_analytic[:, 4] ≈ Y[:, 4] rtol = 4.0e-3
end

@testset "m = 2 & n = 4" begin
coeffs = CurveFit.__calc_integral_rules(Float64, 1:4; m = 2)

len = length(x)
nY, mY = 1 + (len - 1) ÷ 2, 2 * n + 1

Y = Matrix{Float64}(undef, nY, mY)
S = Matrix{Float64}(undef, nY, 4 - 1)

CurveFit.__cumulative_integrals!(Y, S, coeffs, x, y, 4, 2)

@test cumints_analytic[1:2:end, 1] ≈ Y[:, 1] rtol = 3.0e-5
@test cumints_analytic[1:2:end, 2] ≈ Y[:, 2] rtol = 4.0e-5
@test cumints_analytic[1:2:end, 3] ≈ Y[:, 3] rtol = 5.0e-5
@test cumints_analytic[1:2:end, 4] ≈ Y[:, 4] rtol = 6.0e-5
end

x = collect(range(0, stop = 2, length = 13))
y = @. exp(x)
cumints_analytic = @. [(exp(x) - 1) (exp(x) - 1 - x) (exp(x) - 1 - x - x^2 / 2) (
exp(x) -
1 - x -
x^2 /
2 -
x^3 /
6
)]

@testset "m = 2 & n = 4" begin
coeffs = CurveFit.__calc_integral_rules(Float64, 1:4; m = 2)

len = length(x)
nY, mY = 1 + (len - 1) ÷ 2, 2 * n + 1

Y = Matrix{Float64}(undef, nY, mY)
S = Matrix{Float64}(undef, nY, 4 - 1)

CurveFit.__cumulative_integrals!(Y, S, coeffs, x, y, 4, 2)

@test cumints_analytic[1:2:end, 1] ≈ Y[:, 1] rtol = 5.0e-6
@test cumints_analytic[1:2:end, 2] ≈ Y[:, 2] rtol = 3.0e-5
@test cumints_analytic[1:2:end, 3] ≈ Y[:, 3] rtol = 3.0e-5
@test cumints_analytic[1:2:end, 4] ≈ Y[:, 4] rtol = 4.0e-5
end
end
21 changes: 21 additions & 0 deletions test/expsumfit_integration_rules.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using CurveFit
using Test

@testset "ExpSumFit: Integration rules" begin
# Trapezoidal rule
@test CurveFit.__calc_integral_rules(Rational{Int64}, 1:1; m = 1) == [1 // 2 1 // 2]

# Simpson's first (1/3) rule
@test CurveFit.__calc_integral_rules(Rational{Int64}, 1:1; m = 2) ==
[1 // 3 4 // 3 1 // 3]
@test CurveFit.__calc_integral_rules(Rational{Int64}, 2:2; m = 2) ==
[2 // 3 4 // 3 0 // 1]
@test CurveFit.__calc_integral_rules(Rational{Int64}, 3:3; m = 2) ==
[3 // 5 4 // 5 -1 // 15]

# Simpson's second (3/8) rule
@test CurveFit.__calc_integral_rules(Rational{Int64}, 1:1; m = 3) ==
[3 // 8 9 // 8 9 // 8 3 // 8]
@test CurveFit.__calc_integral_rules(Rational{Int64}, 2:2; m = 3) ==
[39 // 40 27 // 10 27 // 40 3 // 20]
end
39 changes: 0 additions & 39 deletions test/generic.jl

This file was deleted.

24 changes: 24 additions & 0 deletions test/generic_cache_show.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CurveFit
using Test
using SciMLBase
using NonlinearSolveFirstOrder: LevenbergMarquardt, GaussNewton, TrustRegion

@testset "GenericNonlinearCurveFitCache show" begin
x = collect(1.0:10.0)
fn(a, x) = @. a[1] + a[2] * x
y = fn([1.0, 2.0], x)
prob = NonlinearCurveFitProblem(fn, [0.5, 0.5], x, y)

# Smoke tests with various algorithms
cache = init(prob)
@test_nowarn repr(MIME"text/plain"(), cache)

cache = init(prob, LevenbergMarquardt())
@test_nowarn repr(MIME"text/plain"(), cache)

# Smoke test with a solved problem
cache = init(prob)
sol = solve!(cache)
@test SciMLBase.successful_retcode(sol)
@test_nowarn repr(MIME"text/plain"(), cache)
end
17 changes: 17 additions & 0 deletions test/generic_curvefitsolution.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CurveFit
using Test
using SciMLBase

@testset "CurveFitSolution" begin
x = 1:10
fn(a, x) = @. 1.0 + 2.0 * x + a[1]
y = fn([1.0], x)
linear_sol = solve(CurveFitProblem(x, y), LinearCurveFitAlgorithm())
nonlinear_sol = solve(NonlinearCurveFitProblem(fn, x, y, [1.0]))

@test SciMLBase.successful_retcode(linear_sol)

# Smoke test
@test contains(repr(MIME"text/plain"(), linear_sol), "residuals mean:")
@test contains(repr(MIME"text/plain"(), nonlinear_sol), "residuals mean:")
end
23 changes: 23 additions & 0 deletions test/king_law.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CurveFit
using Test

@testset "King's Law" begin
U = range(1, stop = 20, length = 20)
A = 5.0
B = 1.5
n = 0.5
E = sqrt.(A .+ B * U .^ n)

fn(E) = ((E .^ 2 .- A) / B) .^ (1 ./ n)

prob = CurveFitProblem(E, U)
sol = solve(prob, KingCurveFitAlgorithm())

@testset for val in range(minimum(E), stop = maximum(E), length = 10)
@test sol(val) ≈ fn(val)
end

# Sigma not supported
prob_sigma = CurveFitProblem(E, U; sigma = ones(length(U)))
@test_throws AssertionError solve(prob_sigma, KingCurveFitAlgorithm())
end
Loading
Loading