Skip to content

Conversation

arnavk23
Copy link
Contributor

@arnavk23 arnavk23 commented Oct 3, 2025

  • Create src/nlp/defaults.jl for default implementations
  • Update src/NLPModels.jl to include defaults.jl after api.jl
  • Move default implementations from api.jl to defaults.jl
  • Replace implementations with function stubs in api.jl
  • Resolves method overwriting warnings during precompilation

Functions refactored:

  • grad(), cons(), cons!(), cons_lin(), cons_nln()
  • jth_congrad(), objcons(), objcons!(), objgrad(), objgrad!()
  • jac_structure(), jac_structure!(), jac_lin_structure(), jac_nln_structure()

Closes #385

- Create src/nlp/defaults.jl for default implementations
- Update src/NLPModels.jl to include defaults.jl after api.jl
- Move default implementations from api.jl to defaults.jl
- Replace implementations with function stubs in api.jl
- Resolves method overwriting warnings during precompilation
- Fixes GitHub issue JuliaSmoothOptimizers#385: Separate the default implementations from the api file

Functions refactored:
- grad(), cons(), cons!(), cons_lin(), cons_nln()
- jth_congrad(), objcons(), objcons!(), objgrad(), objgrad!()
- jac_structure(), jac_structure!(), jac_lin_structure(), jac_nln_structure()

All tests pass and module loads without warnings.
@arnavk23
Copy link
Contributor Author

arnavk23 commented Oct 3, 2025

@tmigot @amontoison please review this pr.

Copy link
Member

@tmigot tmigot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @arnavk23 for the PR. Are you planning to do the same for NLS in a separate PR?

arnavk23 and others added 3 commits October 4, 2025 06:54
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
@arnavk23 arnavk23 requested a review from tmigot October 4, 2025 03:42
@arnavk23 arnavk23 requested a review from tmigot October 4, 2025 15:55
@dpo
Copy link
Member

dpo commented Oct 4, 2025

Functions refactored:

@arnavk23 Could you please explain what you refactored exactly?

@arnavk23
Copy link
Contributor Author

arnavk23 commented Oct 5, 2025

Could you please explain what you refactored exactly?

@dpo

Files changed / added

  • src/NLPModels.jl

    • Updated includes to load the new defaults files after API files:
      • Added include("nlp/defaults.jl")
      • Added include("nls/defaults.jl")
    • Reason: ensure API declarations are loaded first, then default implementations.
  • src/nlp/api.jl

    • Replaced concrete method bodies with documentation + minimal stubs function foo end for many API entry points (keeps the docs while preventing duplicate definitions).
  • src/nlp/defaults.jl (new)

    • Added the default implementations for NLP model APIs (the functions moved out of src/nlp/api.jl).
    • Purpose: provide fallback behavior for models that don’t implement specialized methods.
  • src/nls/api.jl

    • Replaced concrete method bodies with stubs for the NLS API (preserved docs).
  • src/nls/defaults.jl (new)

    • Added default implementations for NLS APIs (moved out of src/nls/api.jl).

Concrete functions moved (representative list)

NLP (moved into src/nlp/defaults.jl)

  • Objective/constraint helpers: obj, obj!, objcons, objcons!, objgrad, objgrad!
  • Gradients / constraints: grad, grad!, cons, cons!
  • Jacobian helpers & structure: jac_structure, jac_structure!, jac_lin_structure, jac_nln_structure, jac_coord, jac, jac_lin_coord, jac_nln_coord
  • Jacobian products: jprod, jprod_lin, jprod_nln, jtprod, jtprod_lin, jtprod_nln
  • Operator forms: jac_op, jac_lin_op, jac_nln_op
  • Hessian helpers: jth_hess_coord, jth_hess, jth_hprod, ghjvprod

NLS (moved into src/nls/defaults.jl)

  • Residual & Jacobian: residual, residual!, jac_residual, jac_structure_residual, jac_structure_residual!, jac_coord_residual, jac_coord_residual!
  • Jacobian products: jprod_residual, jprod_residual!, jtprod_residual, jtprod_residual!
  • Jacobian operator: jac_op_residual, jac_op_residual!
  • Hessian/residual Hessian helpers: hess_residual, hess_structure_residual, hess_structure_residual!, hess_coord_residual, hess_coord_residual!, jth_hess_residual_coord!, hprod_residual!, hess_op_residual!
  • Objective/grad helpers adapted to NLS: obj, objcons!, grad!, objgrad!

Why this change fixes the problem

  • Julia throws an error during precompilation when a method is defined more than once across files that are included during precompilation time.
  • Moving implementations into defaults.jl and leaving api.jl as declarations prevents duplicate definitions (the API declares the name; the defaults file defines the fallback once).
  • Including defaults after API files in src/NLPModels.jl ensures API declarations exist before default implementations are loaded.

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 this pull request may close these issues.

Separate the default implementations from the api file
3 participants