diff --git a/lib/SimpleNonlinearSolve/LICENSE b/lib/SimpleNonlinearSolve/LICENSE index 8eef16440..4d2bf6e69 100644 --- a/lib/SimpleNonlinearSolve/LICENSE +++ b/lib/SimpleNonlinearSolve/LICENSE @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. diff --git a/lib/SimpleNonlinearSolve/test/core/adjoint_tests.jl b/lib/SimpleNonlinearSolve/test/core/adjoint_tests.jl index 449801ad7..1580ade60 100644 --- a/lib/SimpleNonlinearSolve/test/core/adjoint_tests.jl +++ b/lib/SimpleNonlinearSolve/test/core/adjoint_tests.jl @@ -1,6 +1,5 @@ @testitem "Simple Adjoint Test" tags=[:adjoint] begin - using ForwardDiff, ReverseDiff, SciMLSensitivity, Tracker, Zygote, DiffEqBase, - SimpleNonlinearSolve + using ForwardDiff, ReverseDiff, SciMLSensitivity, Tracker, Zygote, DiffEqBase ff(u, p) = u .^ 2 .- p diff --git a/lib/SimpleNonlinearSolve/test/core/least_squares_tests.jl b/lib/SimpleNonlinearSolve/test/core/least_squares_tests.jl new file mode 100644 index 000000000..005c463ff --- /dev/null +++ b/lib/SimpleNonlinearSolve/test/core/least_squares_tests.jl @@ -0,0 +1,41 @@ +@testitem "Nonlinear Least Squares" tags=[:core] begin + using LinearAlgebra + + true_function(x, θ) = @. θ[1] * exp(θ[2] * x) * cos(θ[3] * x + θ[4]) + + θ_true = [1.0, 0.1, 2.0, 0.5] + x = [-1.0, -0.5, 0.0, 0.5, 1.0] + y_target = true_function(x, θ_true) + + function loss_function(θ, p) + ŷ = true_function(p, θ) + return ŷ .- y_target + end + + function loss_function!(resid, θ, p) + ŷ = true_function(p, θ) + @. resid = ŷ - y_target + return + end + + θ_init = θ_true .+ 0.1 + prob_oop = NonlinearLeastSquaresProblem{false}(loss_function, θ_init, x) + + @testset "Solver: $(nameof(typeof(solver)))" for solver in [ + SimpleNewtonRaphson(AutoForwardDiff()), SimpleGaussNewton(AutoForwardDiff()), + SimpleNewtonRaphson(AutoFiniteDiff()), SimpleGaussNewton(AutoFiniteDiff())] + sol = solve(prob_oop, solver) + @test norm(sol.resid, Inf) < 1e-12 + end + + prob_iip = NonlinearLeastSquaresProblem( + NonlinearFunction{true}(loss_function!, resid_prototype = zeros(length(y_target))), + θ_init, x) + + @testset "Solver: $(nameof(typeof(solver)))" for solver in [ + SimpleNewtonRaphson(AutoForwardDiff()), SimpleGaussNewton(AutoForwardDiff()), + SimpleNewtonRaphson(AutoFiniteDiff()), SimpleGaussNewton(AutoFiniteDiff())] + sol = solve(prob_iip, solver) + @test norm(sol.resid, Inf) < 1e-12 + end +end