Skip to content

Use the argument order of symbolic functions in minimize - #43048

Merged
vbraun merged 1 commit into
sagemath:developfrom
ShubhJain09:fix-minimize-arg-order
Oct 28, 2026
Merged

vbraun merged 1 commit into
sagemath:developfrom
ShubhJain09:fix-minimize-arg-order

Conversation

@ShubhJain09

Copy link
Copy Markdown
Contributor

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 #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

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

None

@ShubhJain09
ShubhJain09 force-pushed the fix-minimize-arg-order branch from fc1e43a to 7329aad Compare October 25, 2026 20:22
@dimpase

dimpase commented Oct 25, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T21:00:07.225482Z 7329aad Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 7329aad58c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@github-actions

Copy link
Copy Markdown

Documentation preview for this PR (built with commit 7329aad; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@dimpase dimpase left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ok

vbraun pushed a commit to vbraun/sage that referenced this pull request Oct 27, 2026
Release Manager
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
@vbraun
vbraun merged commit 7e0e2e5 into sagemath:develop Oct 28, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants