Repository navigation
Use the argument order of symbolic functions in minimize - #43048
Conversation
fc1e43a to
7329aad
Compare
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Documentation preview for this PR (built with commit 7329aad; changes) is ready! 🎉 |
sagemathgh-43048: Use the argument order of symbolic functions in `minimize` ### Issue For a callable symbolic function `minimize` evaluates the function with its variables in alphabetical order but builds the gradient in the order of its arguments When the arguments are not in alphabetical order the two do not match and the result is wrong ``` sage: x, y = SR.var('x, y') sage: f(y, x) = (x - 1)^2 + 10*(y - 2)^2 sage: minimize(f, [0, 0]) (0.0, 0.0) sage: minimize(f, [0, 0], algorithm='simplex') (0.9999850850183114, 2.000005155466706) ``` The minimizer is `(2.0, 1.0)` because the first argument is `y` The gradient based algorithms return a point that is not a minimum at all and the others return the coordinates in the wrong order A function with an argument that does not appear in it such as `g(x, y) = (x - 3)^2` raises an empty `ValueError` The same bug was fixed for `minimize_constrained` in sagemath#32511 ### Fix Use `func.arguments()` instead of `func.variables()` as in `minimize_constrained` For an expression that is not callable `arguments()` gives the same as `variables()` so nothing changes there ### Impact `minimize` now gives the right answer for every algorithm when the arguments of a symbolic function are not in alphabetical order or when some of them do not appear in it ### Testing Added regression doctests for the default and simplex and ncg algorithms and a loop over all six algorithms with three arguments in a shuffled order and a test for an unused argument With the fix reverted all five new doctests fail with the wrong results shown above or the `ValueError` I also checked all six algorithms on functions with two and three arguments in different orders and with unused arguments against `scipy.optimize.minimize` and all 16 cases now give the right result while 10 of them were wrong before The doctests in `optimize.py` pass with five different random seeds against a passagemath 10.8 install except for two `find_root` examples that need `primecountpy` and `plot` which are not installed there and fail without this change too ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies None URL: sagemath#43048 Reported by: Shubh Jain Reviewer(s): Dima Pasechnik
Issue
For a callable symbolic function
minimizeevaluates the function with its variables in alphabetical order but builds the gradient in the order of its argumentsWhen the arguments are not in alphabetical order the two do not match and the result is wrong
The minimizer is
(2.0, 1.0)because the first argument isyThe gradient based algorithms return a point that is not a minimum at all and the others return the coordinates in the wrong order
A function with an argument that does not appear in it such as
g(x, y) = (x - 3)^2raises an emptyValueErrorThe same bug was fixed for
minimize_constrainedin #32511Fix
Use
func.arguments()instead offunc.variables()as inminimize_constrainedFor an expression that is not callable
arguments()gives the same asvariables()so nothing changes thereImpact
minimizenow gives the right answer for every algorithm when the arguments of a symbolic function are not in alphabetical order or when some of them do not appear in itTesting
Added regression doctests for the default and simplex and ncg algorithms and a loop over all six algorithms with three arguments in a shuffled order and a test for an unused argument
With the fix reverted all five new doctests fail with the wrong results shown above or the
ValueErrorI also checked all six algorithms on functions with two and three arguments in different orders and with unused arguments against
scipy.optimize.minimizeand all 16 cases now give the right result while 10 of them were wrong beforeThe doctests in
optimize.pypass with five different random seeds against a passagemath 10.8 install except for twofind_rootexamples that needprimecountpyandplotwhich are not installed there and fail without this change too📝 Checklist
⌛ Dependencies
None