Skip to content

fix function inference #1022

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

Merged
merged 4 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 1 addition & 2 deletions src/problems/optimization_problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ function OptimizationProblem(
OptimizationProblem{isinplace(f)}(f, args...; kwargs...)
end
function OptimizationProblem(f, args...; kwargs...)
isinplace(f, 2, has_two_dispatches = false)
OptimizationProblem{true}(OptimizationFunction{true}(f), args...; kwargs...)
OptimizationProblem(OptimizationFunction(f), args...; kwargs...)
end

function OptimizationFunction(
Expand Down
22 changes: 15 additions & 7 deletions src/scimlfunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4199,7 +4199,10 @@ IntervalNonlinearFunction(f::IntervalNonlinearFunction; kwargs...) = f
struct NoAD <: AbstractADType end

(f::OptimizationFunction)(args...) = f.f(args...)
OptimizationFunction(args...; kwargs...) = OptimizationFunction{true}(args...; kwargs...)
function OptimizationFunction(f, args...; kwargs...)
isinplace(f, 2, outofplace_param_number=2)
OptimizationFunction{true}(f, args...; kwargs...)
end

function OptimizationFunction{iip}(f, adtype::AbstractADType = NoAD();
grad = nothing, fg = nothing, hess = nothing, hv = nothing, fgh = nothing,
Expand Down Expand Up @@ -4251,8 +4254,9 @@ end
(f::MultiObjectiveOptimizationFunction)(args...) = f.f(args...)

# Convenience constructor
function MultiObjectiveOptimizationFunction(args...; kwargs...)
MultiObjectiveOptimizationFunction{true}(args...; kwargs...)
function MultiObjectiveOptimizationFunction(f, args...; kwargs...)
isinplace(f, 2, outofplace_param_number=2)
MultiObjectiveOptimizationFunction{true}(f, args...; kwargs...)
end

# Constructor with keyword arguments
Expand Down Expand Up @@ -4339,15 +4343,17 @@ function BVPFunction{iip, specialize, twopoint}(f, bc;
if iip_f
jac = update_coefficients! #(J,u,p,t)
else
jac = (u, p, t) -> update_coefficients!(deepcopy(jac_prototype), u, p, t)
jac_prototype_copy = deepcopy(jac_prototype)
jac = (u, p, t) -> update_coefficients!(jac_prototype_copy, u, p, t)
end
end

if bcjac === nothing && isa(bcjac_prototype, AbstractSciMLOperator)
if iip_bc
bcjac = update_coefficients! #(J,u,p,t)
else
bcjac = (u, p, t) -> update_coefficients!(deepcopy(bcjac_prototype), u, p, t)
bcjac_prototype_copy = deepcopy(bcjac_prototype)
bcjac = (u, p, t) -> update_coefficients!(bcjac_prototype_copy, u, p, t)
end
end

Expand Down Expand Up @@ -4512,15 +4518,17 @@ function DynamicalBVPFunction{iip, specialize, twopoint}(f, bc;
if iip_f
jac = update_coefficients! #(J,u,p,t)
else
jac = (u, p, t) -> update_coefficients!(deepcopy(jac_prototype), u, p, t)
jac_prototype_copy = deepcopy(jac_prototype)
jac = (u, p, t) -> update_coefficients!(jac_prototype_copy, u, p, t)
end
end

if bcjac === nothing && isa(bcjac_prototype, AbstractSciMLOperator)
if iip_bc
bcjac = update_coefficients! #(J,u,p,t)
else
bcjac = (u, p, t) -> update_coefficients!(deepcopy(bcjac_prototype), u, p, t)
bcjac_prototype_copy = deepcopy(jac_prototype)
bcjac = (u, p, t) -> update_coefficients!(bcjac_prototype_copy, u, p, t)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/aqua.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ end
# for method_ambiguity in ambs
# @show method_ambiguity
# end
@warn "Number of method ambiguities: $(length(ambs))"
!isempty(ambs) &&@warn "Number of method ambiguities: $(length(ambs))"
@test length(ambs) ≤ 8
end

Expand Down
16 changes: 8 additions & 8 deletions test/function_building_error_messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ ofboth(u, p, t) = u
ofboth(du, u, p, t) = du .= u

ODEFunction(ofboth)
ODEFunction{true}(ofboth)
ODEFunction{false}(ofboth)
@inferred ODEFunction{true}(ofboth)
@inferred ODEFunction{false}(ofboth)

jac(u, t) = [1.0]
@test_throws SciMLBase.TooFewArgumentsError ODEFunction(fiip, jac = jac)
Expand Down Expand Up @@ -428,8 +428,8 @@ nfboth(u, p) = u
nfboth(du, u, p) = du .= u

NonlinearFunction(nfboth)
NonlinearFunction{true}(nfboth)
NonlinearFunction{false}(nfboth)
@inferred NonlinearFunction{true}(nfboth)
@inferred NonlinearFunction{false}(nfboth)

njac(u) = [1.0]
@test_throws SciMLBase.TooFewArgumentsError NonlinearFunction(nfiip, jac = njac)
Expand Down Expand Up @@ -520,8 +520,8 @@ bcfboth(u, p, t) = u
bcfboth(du, u, p, t) = du .= u

BVPFunction(bfboth, bcfboth)
BVPFunction{true}(bfboth, bcfboth)
BVPFunction{false}(bfboth, bcfboth)
@inferred BVPFunction{true}(bfboth, bcfboth)
@inferred BVPFunction{false}(bfboth, bcfboth)

bjac(u, t) = [1.0]
bcjac(u, t) = [1.0]
Expand Down Expand Up @@ -663,8 +663,8 @@ dbcfboth(du, u, p, t) = u
dbcfboth(res, du, u, p, t) = res .= du .- u

DynamicalBVPFunction(dbfboth, dbcfboth)
DynamicalBVPFunction{true}(dbfboth, dbcfboth)
DynamicalBVPFunction{false}(dbfboth, dbcfboth)
@inferred DynamicalBVPFunction{true}(dbfboth, dbcfboth)
@inferred DynamicalBVPFunction{false}(dbfboth, dbcfboth)

dbjac(du, u, t) = [1.0]
dbcjac(du, u, t) = [1.0]
Expand Down
Loading