Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

specialize Newton on static arrays #50

Merged
merged 4 commits into from
Jan 18, 2022
Merged

specialize Newton on static arrays #50

merged 4 commits into from
Jan 18, 2022

Conversation

ChrisRackauckas
Copy link
Member

using StaticArrays
using NonlinearSolve

function f(x, _)
    F1 = (x[1] + 3) * (x[2]^3 - 7) + 18
    F2 = sin(x[2] * exp(x[1]) - 1)
    SA[F1,F2]
end

function f!(F, x)
    F[1] = (x[1] + 3) * (x[2]^3 - 7) + 18
    F[2] = sin(x[2] * exp(x[1]) - 1)
    nothing
end

x0 = [0.1; 1.2]
x0s = SVector{size(x0)...}(x0)
x0m = MVector{size(x0)...}(x0)
prob = NonlinearProblem{false}(f, x0s)

using NLsolve, BenchmarkTools
@btime sol = solve(prob,NewtonRaphson()); # 320.000 ns (2 allocations: 128 bytes)
@btime nlsolve(f!, x0m); # 1.460 μs (35 allocations: 1.36 KiB)

```julia
using StaticArrays
using NonlinearSolve

function f(x, _)
    F1 = (x[1] + 3) * (x[2]^3 - 7) + 18
    F2 = sin(x[2] * exp(x[1]) - 1)
    SA[F1,F2]
end

function f!(F, x)
    F[1] = (x[1] + 3) * (x[2]^3 - 7) + 18
    F[2] = sin(x[2] * exp(x[1]) - 1)
    nothing
end

x0 = [0.1; 1.2]
x0s = SVector{size(x0)...}(x0)
x0m = MVector{size(x0)...}(x0)
prob = NonlinearProblem{false}(f, x0s)

using NLsolve, BenchmarkTools
@Btime sol = solve(prob,NewtonRaphson()); # 320.000 ns (2 allocations: 128 bytes)
@Btime nlsolve(f!, x0m); # 1.460 μs (35 allocations: 1.36 KiB)
```
@ChrisRackauckas
Copy link
Member Author

Before:

using StaticArrays
using NonlinearSolve

function f(x, _)
    F1 = (x[1] + 3) * (x[2]^3 - 7) + 18
    F2 = sin(x[2] * exp(x[1]) - 1)
    SA[F1,F2]
end

function f!(F, x)
    F[1] = (x[1] + 3) * (x[2]^3 - 7) + 18
    F[2] = sin(x[2] * exp(x[1]) - 1)
    nothing
end

x0 = [0.1; 1.2]
x0s = SVector{size(x0)...}(x0)
x0m = MVector{size(x0)...}(x0)
prob = NonlinearProblem{false}(f, x0s)
sol = solve(prob,NewtonRaphson())

using NLsolve, BenchmarkTools
@btime sol = solve(prob,NewtonRaphson()); # 635.928 ns (2 allocations: 128 bytes)

so it was already "working" with the immutable integrator, but this specializes for small problems.

@ChrisRackauckas ChrisRackauckas merged commit 51c443e into master Jan 18, 2022
@ChrisRackauckas ChrisRackauckas deleted the staticarrays branch January 18, 2022 11:14
avik-pal pushed a commit that referenced this pull request Nov 1, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant