Skip to content

Commit

Permalink
Merge branch 'main' of ../SimpleNonlinearSolve.jl into ap/simplenonli…
Browse files Browse the repository at this point in the history
…nearsolve_history
  • Loading branch information
avik-pal committed Nov 1, 2024
2 parents eb3df11 + b3db1a2 commit 1b24ae4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/SimpleNonlinearSolve/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -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.
SOFTWARE.
3 changes: 1 addition & 2 deletions lib/SimpleNonlinearSolve/test/core/adjoint_tests.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down
41 changes: 41 additions & 0 deletions lib/SimpleNonlinearSolve/test/core/least_squares_tests.jl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1b24ae4

Please sign in to comment.