Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Misleading of code in src/approximations/linearization.jl #415

Closed
Flawless1202 opened this issue Sep 18, 2024 · 1 comment · Fixed by #417
Closed

Misleading of code in src/approximations/linearization.jl #415

Flawless1202 opened this issue Sep 18, 2024 · 1 comment · Fixed by #417

Comments

@Flawless1202
Copy link

In the following code in src/approximations/linearization.jl:

# In case if `g(x_hat)` returns a number and inputs are numbers too
function local_linearization(r::Real, splitg::S, g::G, x_hat) where {S, G}
return local_linearization(r, splitg, g, (g, lx_hat) -> (ForwardDiff.gradient(splitg, lx_hat)::Vector{eltype(lx_hat)})', x_hat)
end
# In case if `g(x_hat)` returns a vector, but inputs are numbers
function local_linearization(r::AbstractVector, splitg::S, g::G, x_hat) where {S, G}
return local_linearization(r, splitg, g, (g, lx_hat) -> (ForwardDiff.jacobian(splitg, lx_hat)::Matrix{eltype(lx_hat)}), x_hat)
end
function local_linearization(r, splitg::S, g::G, fA::F, x_hat) where {S, G, F}
lx_hat = __as_vec(x_hat)
# `fA` calls either `gradient` or `jacobian`, depending on the type of the `r`
A = fA(splitg, lx_hat)
b = r - A * lx_hat
return (A, b)
end

As far as I am concerned, the lambda function

(g, lx_hat) -> (ForwardDiff.gradient(splitg, lx_hat)::Vector{eltype(lx_hat)})'

and

(g, lx_hat) -> (ForwardDiff.jacobian(splitg, lx_hat)::Matrix{eltype(lx_hat)})

should be

(splitg, lx_hat) -> (ForwardDiff.gradient(splitg, lx_hat)::Vector{eltype(lx_hat)})'

and

(splitg, lx_hat) -> (ForwardDiff.jacobian(splitg, lx_hat)::Matrix{eltype(lx_hat)})

Is it correct?

@bvdmitri
Copy link
Member

I believe you're right here, but fortunately, later on in line 81, we pass splitg as the g argument, making it effectively equivalent. The code is indeed a bit tricky to read, but I don't anticipate it being a real bug. We can clean it up, though.

I don't quite remember the original reasoning behind using the lambda function, but it had to do with ForwardDiff requiring a function with a single vector argument, while we wanted to handle multi-argument functions as well."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants