Skip to content

Overloads for LinearProblems with ForwardDiff Dual numbers #621

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

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

jClugstor
Copy link
Member

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Addresses #607

This gives the same result as when Dual numbers go through the actual solver, and can handle cases when either A or b do not depend on anything, i.e. when only one of A or b consists of Dual numbers.

@jClugstor
Copy link
Member Author

@oscardssmith I need to make sure that the results here are the same as when the Dual numbers actually go through the solver. I tried using invoke, but I can't seem to make it go through the more general solve. Any ideas?

@oscardssmith
Copy link
Member

You can run __solve on the duals.

@jClugstor
Copy link
Member Author

LinearSolve doesn't have __solve. But A \ b works with Dual numbers, so I can just compare with that since they should be very close.

@jClugstor
Copy link
Member Author

jClugstor commented Jun 3, 2025

Left to do:

  • Make sure that Dual u0 is handled and passed to the iterative solvers
    - [ ] Make sure that SciMLOperators are handled
  • Make sure that things relying on LinearCache can still work with DualLinearCache

@jClugstor
Copy link
Member Author

This just needs a fix for remaking LinearProblems with u0 (#624), and then the tests should pass.

So far this doesn't handle when A is a SciMLOperator, but it doesn't affect it either, in that case it will just go through the solver like before. It might be ok if that's handled later?

I did have to add a DualLinearCache. I overloaded getproperty for A and b, so that if cache = DualCache and cache.A or cache.b is accessed, it get's the A and b from the underlying LinearCache used in the primal part of the solve. Also overloaded setproperty! so that if somebody sets A or b with dual numbers it also sets the partials in the cache, used for the dual part of the solve. That way everything should be consistent with initializing a cache and updating A and b like it shows in the docs.

That does mean that if somebody is relying on the type of the output of init, or the fields of LinearCache when solving with Dual numbers it will break. I could add overloads for DualLinearCache so that if someone tries accessing it with the name of a field of LinearCache it accesses the underlying primal LinearCache to mitigate that problem.

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.

2 participants