Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 411ad53

Browse files
committed
Add support for f.jac in SimpleNewtonRaphson
1 parent 976758f commit 411ad53

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/raphson.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ function SciMLBase.__solve(prob::NonlinearProblem,
6060
end
6161

6262
for i in 1:maxiters
63-
if alg_autodiff(alg)
63+
if DiffEqBase.has_jac(prob.f)
64+
dfx = prob.f.jac(x, prob.p)
65+
fx = f(x)
66+
elseif alg_autodiff(alg)
6467
fx, dfx = value_derivative(f, x)
6568
elseif x isa AbstractArray
6669
fx = f(x)

test/basictests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using SimpleNonlinearSolve
22
using StaticArrays
33
using BenchmarkTools
44
using DiffEqBase
5+
using LinearAlgebra
56
using Test
67

78
const BATCHED_BROYDEN_SOLVERS = Broyden[]
@@ -489,3 +490,17 @@ for alg in (BATCHED_BROYDEN_SOLVERS..., BATCHED_LBROYDEN_SOLVERS...)
489490
@test sol.retcode == ReturnCode.Success
490491
@test abs.(sol.u) sqrt.(p)
491492
end
493+
494+
## Specified Jacobian
495+
496+
f, u0 = (u, p) -> u .* u .- p, randn(3)
497+
498+
f_jac(u, p) = begin diagm(2 * u) end
499+
500+
p = [2.0, 1.0, 5.0];
501+
502+
probN = NonlinearProblem(NonlinearFunction(f, jac = f_jac), u0, p)
503+
504+
sol = solve(probN, SimpleNewtonRaphson())
505+
506+
@test abs.(sol.u) sqrt.(p)

0 commit comments

Comments
 (0)