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
7 changes: 7 additions & 0 deletions src/approximations/linearization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ function local_linearization(r::Real, splitg::S, x_hat::H) where {S, H}
return local_linearization_split(r, fA, x_hat)
end

# In case if `g(x_hat)` returns a vector, but input is a number
function local_linearization(result::AbstractVector, g::G, x_hat::Tuple{T}) where {G, T <: Real}
A = ForwardDiff.derivative(g, first(x_hat))
b = result - A * first(x_hat)
return (A, b)
end

# In case if `g(x_hat)` returns a vector, but inputs are numbers
function local_linearization(r::AbstractVector, splitg::S, x_hat::H) where {S, H}
# `r` is a vector, so we need to use `jacobian` instead of `gradient`
Expand Down
1 change: 1 addition & 0 deletions test/approximations/linearization_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
@test @inferred(approximate(Linearization(), (x, y) -> x - y, (1, 2))) == ([1 -1], 0)
@test @inferred(approximate(Linearization(), (x, y) -> x .- y, ([1.0, 2.0], 1.0))) == ([1.0 0.0 -1.0; 0.0 1.0 -1.0], [0.0, 0.0])
@test @inferred(approximate(Linearization(), (x, y) -> x .- y, ([1.0, 2.0], [1.0, 1.0]))) == ([1.0 0.0 -1.0 0.0; 0.0 1.0 0.0 -1.0], [0.0, 0.0])
@test @inferred(approximate(Linearization(), (x) -> x .- [1, 1], (1.0,))) == ([1.0, 1.0], [-1.0, -1.0])
end
end