Skip to content

Add .value() method for expression evaluation and refactor variable retrieval#3

Merged
SteveDiamond merged 4 commits into
cvxpy:mainfrom
haozhu10015:feat/expr-value
Mar 8, 2026
Merged

Add .value() method for expression evaluation and refactor variable retrieval#3
SteveDiamond merged 4 commits into
cvxpy:mainfrom
haozhu10015:feat/expr-value

Conversation

@haozhu10015
Copy link
Copy Markdown
Contributor

Summary

This PR closes #1.

  • Adds Expr::value(&solution) to evaluate any expression — variables, sub-expressions, or composite atoms — at an
    optimal solution
  • Adds Index<(usize, usize)> for Array to enable natural result[(row, col)] indexing on returned values
  • Updates all examples to use .value() for variable retrieval; least_squares demonstrates both &solution[&var] and var.value(&solution) as equivalent APIs
  • Adds expression evaluation demos where sub-expressions carry meaningful insight: fitted values in least squares, actual return in portfolio, fit/regularization split in LASSO

haozhu10015 and others added 2 commits March 8, 2026 08:45
@haozhu10015
Copy link
Copy Markdown
Contributor Author

While writing the .value() tests I noticed that sum_squares(Ax + c) in the objective was silently returning the wrong answer when the expression had a nonzero offset or a non-identity coefficient matrix. The old canonicalization fell through to an SOC path that minimized ‖Ax + c‖ instead of ‖Ax + c‖^2, so the reported objective was always the square root of the correct value.

A fix is ready on feat/sum-squares-fix (rebased on top of this branch). Happy to bundle it here or keep it as a follow-up PR.

Copy link
Copy Markdown
Collaborator

@SteveDiamond SteveDiamond left a comment

Choose a reason for hiding this comment

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

Thanks for fixing!

@SteveDiamond
Copy link
Copy Markdown
Collaborator

Let's do the second fix as a follow up.

@SteveDiamond
Copy link
Copy Markdown
Collaborator

Actually could you get the CI passing? Some minor issues.

@haozhu10015
Copy link
Copy Markdown
Contributor Author

haozhu10015 commented Mar 8, 2026

The lint is now fixed but the MSRV error is because the latest zmij and also quote does not support rust 1.70, shall we bump the MSRV to 1.71 or cap the zmij version (< 1.0.21)?

@SteveDiamond
Copy link
Copy Markdown
Collaborator

The lint is now fixed but the MSRV error is because the latest zmij does not support rust 1.70, shall we bump the MSRV to 1.71 or cap the zmij version (< 1.0.21)?

Lets bump MSRV to 1.71

@SteveDiamond SteveDiamond merged commit 72f90fc into cvxpy:main Mar 8, 2026
10 checks passed
@haozhu10015 haozhu10015 deleted the feat/expr-value branch March 8, 2026 23:01
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.

API improvement for expression values

2 participants