Skip to content

Conversation

@drrelyea
Copy link
Owner

@drrelyea drrelyea commented Feb 2, 2026

The original Python port incorrectly translated MATLAB's x'*x (conjugate transpose times x) to np.dot(x, x), which doesn't conjugate. For complex data, this produces complex results instead of real values.

Fixed 3 source code bugs in spgl1.py:

  • Line 1342: f = f + (mu/2) * np.dot(x, x) -> np.dot(np.conj(x), x)
  • Line 1537: Same fix in tau update section
  • Line 2023: Fixed both np.dot(r, r) and np.dot(x, x) in rnorm computation

Also added np.real() to 8 expressions that are mathematically real but may have tiny imaginary residuals from floating-point arithmetic:

  • rtr, f_dual, gap, aerror_dual, gtd, sts, sty computations

Fixed corresponding bugs in test_mu_parameter.py where tests computed expected values using the same incorrect formula.

Added regression tests (TestMuComplexData) that would have caught these bugs:

  • test_mu_complex_objective_is_real: Verifies rnorm is real
  • test_mu_complex_matches_manual_computation: Verifies formula
  • test_mu_complex_matches_matlab: Compares with MATLAB for complex data

The original Python port incorrectly translated MATLAB's x'*x (conjugate
transpose times x) to np.dot(x, x), which doesn't conjugate. For complex
data, this produces complex results instead of real values.

Fixed 3 source code bugs in spgl1.py:
- Line 1342: f = f + (mu/2) * np.dot(x, x) -> np.dot(np.conj(x), x)
- Line 1537: Same fix in tau update section
- Line 2023: Fixed both np.dot(r, r) and np.dot(x, x) in rnorm computation

Also added np.real() to 8 expressions that are mathematically real but
may have tiny imaginary residuals from floating-point arithmetic:
- rtr, f_dual, gap, aerror_dual, gtd, sts, sty computations

Fixed corresponding bugs in test_mu_parameter.py where tests computed
expected values using the same incorrect formula.

Added regression tests (TestMuComplexData) that would have caught these
bugs:
- test_mu_complex_objective_is_real: Verifies rnorm is real
- test_mu_complex_matches_manual_computation: Verifies formula
- test_mu_complex_matches_matlab: Compares with MATLAB for complex data

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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